Raft TLA+形式化验证

TLA+

------------------------------- MODULE Raft -------------------------------

EXTENDS Naturals, FiniteSets, Sequences, TLC

CONSTANTS Server, Value, Follower, Candidate, Leader, Nil, 
    RequestVoteRequest, RequestVoteResponse, AppendEntriesRequest, 
    AppendEntriesResponse

VARIABLES messages, elections, allLogs, currentTerm, state, votedFor, log, 
    commitIndex, votesResponded, votesGranted, voterLog, nextIndex, matchIndex

leaderVars == <<nextIndex, matchIndex, elections>>
logVars == <<log, commitIndex>>
candidateVars == <<votesResponded, votesGranted, voterLog>>
serverVars == <<currentTerm, state, votedFor>>
vars == <<messages, allLogs, serverVars, candidateVars, leaderVars, logVars>>

Quorum == {
   
   i \in SUBSET Server: Cardinality(i)*2 > Cardinality(Server)}

LastTerm(xlog) == IF Len(xlog) # 0 THEN xlog[Len(xlog)].term ELSE 0

WithMessage(m, msgs) == 
    IF m \in DOMAIN msgs THEN
        [msgs EXCEPT ![m] = msgs[m] + 1]
    ELSE
        msgs @@ (m :> 1)

WithoutMessage(m, msgs) ==
    IF m \in DOMAIN msgs THEN
        [msgs EXCEPT ![m] = msgs[m] - 1]
    ELSE
        msgs

Send(m) == messages' = WithMessage(m, messages)

Discard(m) == messages' = WithoutMessage(m, messages)

Reply(response, request) ==
    messages' = WithoutMessage(request, WithMessage(response, messages))

Min(m) == CHOOSE x \in m: \A y \in m: y >= x

Max(m) == CHOOSE x \in m: \A y \in m: y <= x


----

InitHistoryVars == 
    /\ elections = {
   
   }
    /\ allLogs = {
   
   }
    /\ voterLog = [i \in Server |-> [j \in {
   
   } |-> <<>> ]]

InitServerVars == 
    /\ currentTerm = [i \in Server |-> 1]
    /\ state = [i \in Server |-> Follower]
    /\ votedFor = [i \in Server |-> Nil]

InitCandidateVars == 
    /\ votesResponded = [i \in Server |-> {
   
   }]
    /\ votesGranted = [i \in Server |-> {
   
   }]

InitLeaderVars == 
    /\ nextIndex = [i \in Server |-> [j \in Server |-> 1]]
    /\ matchIndex = [i \in Server |-> [j \in Server |-> 0]]

InitLogVars == 
    /\ log = [i \in Server |-> <<>>]
    /\ commitIndex = [i \in Server |-> 0]

Init == 
    /\ messages = [ m \in {
   
   } |-> 0 ]
    /\ InitHistoryVars
    /\ InitServerVars
    /\ InitCandidateVars
    /\ InitLeaderVars
    /\ InitLogVars

Restart(i) ==
    /\ state' = [state EXCEPT ![i] = Follower]
    /\ votesResponded' = [votesResponded EXCEPT ![i] = {
   
   }]
    /\ votesGranted' = [votesGranted EXCEPT ![i] = {
   
   }]
    /\ voterLog' = [voterLog EXCEPT ![i] = [j \in {
   
   } |-> <<>>]]
    /\ nextIndex' = [nextIndex EXCEPT ![i] = [j \in Server |-> 1]]
    /\ matchIndex' = [matchIndex EXCEPT ![i] = [j \in Server |-> 0]]
    /\ commitIndex' = [commitIndex EXCEPT ![i] = 0]
    /\ UNCHANGED <<messages, currentTerm, votedFor, log, elections>>

Timeout(i) == 
    /\ state[i] \in {
   
   Follower, Candidate}
    /\ state' = [state EXCEPT ![i] = Candidate]
    /\ currentTerm' = [currentTerm EXCEPT ![i] = @ + 1]
    /\ votedFor' = [votedFor EXCEPT ![i] = Nil]
    /\ votesResponded' = [votesResponded EXCEPT ![i
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值