-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathraft_leader_election.maude
219 lines (204 loc) · 9.34 KB
/
raft_leader_election.maude
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
fmod SOUP {D :: TRIV} is
pr NAT .
sort Soup{D} .
subsort D$Elt < Soup{D} .
op empty : -> Soup{D} [ctor] .
op _ _ : Soup{D} Soup{D} -> Soup{D} [ctor assoc comm id: empty] .
op _\in_ : D$Elt Soup{D} -> Bool .
op length : Soup{D} -> Nat .
op _ - _ : Soup{D} D$Elt -> Soup{D} .
var E : D$Elt .
var S : Soup{D} .
eq E E = E .
eq E \in E S = true .
eq E \in S = false [owise] .
eq length(empty) = 0 .
eq length((E S)) = length(S) + 1 .
eq (E S) - E = S .
eq S - E = S [owise] .
endfm
fmod ROLE is
sort Role .
sorts Leader Candidate Follower .
subsorts Leader Candidate Follower < Role .
op leader : -> Leader [ctor] .
op candidate : -> Candidate [ctor] .
op follower : -> Follower [ctor] .
endfm
fmod SERVER-ID is
sort ServerID .
ops null s0 s1 s2 : -> ServerID [ctor] .
endfm
view ServerID from TRIV to SERVER-ID is
sort Elt to ServerID .
endv
fmod REQUEST-VOTE-REQUEST is
pr NAT .
pr SERVER-ID .
sort RequestVoteRequest .
op requestVoteRequest : Nat ServerID -> RequestVoteRequest [ctor] .
op term : RequestVoteRequest -> Nat .
op candidateID : RequestVoteRequest -> ServerID .
var T : Nat .
var I : ServerID .
eq term(requestVoteRequest(T, I)) = T .
eq candidateID(requestVoteRequest(T, I)) = I .
endfm
fmod REQUEST-VOTE-RESPONSE is
pr NAT .
sort RequestVoteResponse .
op requestVoteResponse : Nat Bool -> RequestVoteResponse [ctor] .
op term : RequestVoteResponse -> Nat .
op voteGranted : RequestVoteResponse -> Bool .
var T : Nat .
var B : Bool .
eq term(requestVoteResponse(T, B)) = T .
eq voteGranted(requestVoteResponse(T, B)) = B .
endfm
fmod APPEND-ENTRIES-REQUEST is
pr NAT .
pr SERVER-ID .
sort AppendEntriesRequest .
op appendEntriesRequest : Nat ServerID -> AppendEntriesRequest [ctor] .
op term : AppendEntriesRequest -> Nat .
op leaderID : AppendEntriesRequest -> ServerID .
var T : Nat .
var I : ServerID .
eq term(appendEntriesRequest(T, I)) = T .
eq leaderID(appendEntriesRequest(T, I)) = I .
endfm
fmod APPEND-ENTRIES-RESPONSE is
pr NAT .
sort AppendEntriesResponse .
op appendEntriesResponse : Nat Bool -> AppendEntriesResponse [ctor] .
op term : AppendEntriesResponse -> Nat .
op success : AppendEntriesResponse -> Bool .
var T : Nat .
var B : Bool .
eq term(appendEntriesResponse(T, B)) = T .
eq success(appendEntriesResponse(T, B)) = B .
endfm
fmod MESSAGE is
pr REQUEST-VOTE-REQUEST .
pr REQUEST-VOTE-RESPONSE .
pr APPEND-ENTRIES-REQUEST .
pr APPEND-ENTRIES-RESPONSE .
sort Msg .
op msg : ServerID ServerID RequestVoteRequest -> Msg [ctor] .
op msg : ServerID ServerID RequestVoteResponse -> Msg [ctor] .
op msg : ServerID ServerID AppendEntriesRequest -> Msg [ctor] .
op msg : ServerID ServerID AppendEntriesResponse -> Msg [ctor] .
endfm
view Msg from TRIV to MESSAGE is
sort Elt to Msg .
endv
fmod NETWORK is
pr SOUP{ServerID} .
pr SOUP{Msg} .
op mkRequestVoteRequests : ServerID RequestVoteRequest Soup{ServerID} -> Soup{Msg} .
op mkAppendEntriesRequests : ServerID AppendEntriesRequest Soup{ServerID} -> Soup{Msg} .
vars S0 S1 : ServerID .
var Ss : Soup{ServerID} .
var RVReq : RequestVoteRequest .
var AEReq : AppendEntriesRequest .
eq mkRequestVoteRequests(S0, RVReq, empty) = empty .
eq mkRequestVoteRequests(S0, RVReq, S1 Ss) = msg(S0, S1, RVReq) mkRequestVoteRequests(S0, RVReq, Ss) .
eq mkAppendEntriesRequests(S0, AEReq, empty) = empty .
eq mkAppendEntriesRequests(S0, AEReq, S1 Ss) = msg(S0, S1, AEReq) mkAppendEntriesRequests(S0, AEReq, Ss) .
endfm
fmod OCOM is
pr SOUP{ServerID} .
pr NETWORK .
pr ROLE .
sort OCom .
op term[_]:_ : ServerID Nat -> OCom [ctor] .
op role[_]:_ : ServerID Role -> OCom [ctor] .
op votedBy[_]:_ : ServerID ServerID -> OCom [ctor] .
op votedFor[_]:_ : ServerID Soup{ServerID} -> OCom [ctor] .
op servers:_ : Soup{ServerID} -> OCom [ctor] .
op network:_ : Soup{Msg} -> OCom [ctor] .
endfm
view OCom from TRIV to OCOM is
sort Elt to OCom .
endv
fmod CONFIG is
pr NAT .
pr SOUP{OCom} .
sort Config .
op {_} : Soup{OCom} -> Config [ctor] .
op n : -> Nat .
op majority : -> Nat .
op maxTerm : -> Nat .
op init : -> Config .
eq n = 3 .
eq maxTerm = 2 .
eq majority = n quo 2 + 1 .
eq init = {(term[s0]: 0) (term[s1]: 0) (term[s2]: 0)
(role[s0]: follower) (role[s1]: follower) (role[s2]: follower)
(votedBy[s0]: null) (votedBy[s1]: null) (votedBy[s2]: null)
(votedFor[s0]: empty) (votedFor[s1]: empty) (votedFor[s2]: empty)
(servers: (s0 s1 s2))
(network: empty)} .
endfm
mod RAFT is
pr CONFIG .
var R : Role .
vars T U : Nat .
vars S0 S1 S2 : ServerID .
var VB : ServerID .
vars VF0 VF1 : Soup{ServerID} .
var Ss : Soup{ServerID} .
var B : Bool .
var NW : Soup{Msg} .
var OCs : Soup{OCom} .
crl [timeout] : {(term[S0]: T) (role[S0]: R) (votedBy[S0]: VB) (votedFor[S0]: VF0) OCs} =>
{(term[S0]: T + 1) (role[S0]: candidate) (votedBy[S0]: S0) (votedFor[S0]: S0) OCs}
if (R == follower or R == candidate) and T < maxTerm .
crl [requestVote] : {(term[S0]: T) (role[S0]: candidate) (servers: Ss) (network: NW) OCs} =>
{(term[S0]: T) (role[S0]: candidate) (servers: Ss)
(network: (NW mkRequestVoteRequests(S0, requestVoteRequest(T, S0), Ss - S0))) OCs}
if T <= maxTerm .
crl [appendEntriesWithEmptyLog] : {(term[S0]: T) (role[S0]: leader) (servers: Ss) (network: NW) OCs} =>
{(term[S0]: T) (role[S0]: leader) (servers: Ss)
(network:(NW mkAppendEntriesRequests(S0, appendEntriesRequest(T, S0), Ss - S0))) OCs}
if T <= maxTerm .
crl [handleRequestVoteRequest] : {(term[S0]: T) (role[S0]: R) (votedBy[S0]: VB) (votedFor[S0]: VF0)
(network: (msg(S1, S0, requestVoteRequest(U, S1)) NW)) OCs} =>
{(term[S0]: if U > T then U else T fi) (role[S0]: if U > T then follower else R fi)
(votedBy[S0]: if ((VB == null or VB == S1) and U >= T) then S1 else VB fi)
(votedFor[S0]: if U > T then empty else VF0 fi)
(network: if ((VB == null or VB == S1) and U >= T)
then (msg(S1, S0, requestVoteRequest(U, S1)) NW
msg(S0, S1, requestVoteResponse(U, true)))
else (msg(S1, S0, requestVoteRequest(U, S1)) NW
msg(S0, S1, requestVoteResponse((if U > T then U else T fi), false))) fi) OCs}
if T <= maxTerm .
crl [handleRequestVoteResponse] : {(term[S0]: T) (role[S0]: R) (votedBy[S0]: VB) (votedFor[S0]: VF0) (network: (msg(S1, S0, requestVoteResponse(U, B)) NW)) OCs} =>
{(term[S0]: if U > T then U else T fi)
(role[S0]: if U > T
then follower
else (if length(VF1) >= majority then leader else R fi) fi)
(votedBy[S0]: if U > T then null else VB fi)
(votedFor[S0]: if U > T then empty else VF1 fi)
(network: (msg(S1, S0, requestVoteResponse(U, B)) NW)) OCs}
if T <= maxTerm
/\ VF1 := if T == U and B == true and R == candidate then (VF0 S1) else VF0 fi .
crl [handleAppendEntriesRequest] : {(term[S0]: T) (role[S0]: R) (votedBy[S0]: VB) (votedFor[S0]: VF0)
(network: (msg(S1, S0, appendEntriesRequest(U, S1)) NW)) OCs} =>
{(term[S0]: if U > T then U else T fi) (role[S0]: if U >= T then follower else R fi)
(votedBy[S0]: if U > T then null else VB fi) (votedFor[S0]: if U > T then empty else VF0 fi)
(network: if U > T
then (msg(S1, S0, appendEntriesRequest(U, S1)) NW
msg(S0, S1, appendEntriesResponse(U, false)))
else (msg(S1, S0, appendEntriesRequest(U, S1)) NW
msg(S0, S1, appendEntriesResponse(T, true))) fi) OCs}
if T <= maxTerm .
crl [handleAppendEntriesResponse] : {(term[S0]: T) (role[S0]: R) (votedBy[S0]: VB) (votedFor[S0]: VF0)
(network: (msg(S1, S0, appendEntriesResponse(U, B)) NW)) OCs} =>
{(term[S0]: if U > T then U else T fi) (role[S0]: if U > T then follower else R fi)
(votedBy[S0]: if U > T then null else VB fi) (votedFor[S0]: if U > T then empty else VF0 fi)
(network: (msg(S1, S0, appendEntriesResponse(U, B)) NW)) OCs}
if T <= maxTerm .
endm
--- search [1] in RAFT : init =>* {(role[S0]: leader) OCs} .
--- search [1] in RAFT : init =>* {(term[S0]: T) (term[S1]: T) (role[S0]: leader) (role[S1]: leader) OCs} .