-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathraft.pml
425 lines (390 loc) · 15.3 KB
/
raft.pml
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
/*
Author: Xingyu Xie
*/
#define MAX_TERM 3 // 1 to 3
#define MAX_LOG 2 // 0 to 1
#define MAX_SERVER // 1 to 3
#define NIL 10 // a number that won't be used
// message channels
typedef AppendEntry {
byte term, leaderCommit, index, prevLogTerm
};
typedef AppendEntryChannels {
chan ch[3] = [1] of { AppendEntry };
};
AppendEntryChannels ae_ch[3];
typedef AppendEntryResponse {
byte term;
bool success
};
typedef AppendEntryResponseChannels {
chan ch[3] = [1] of { AppendEntryResponse };
};
AppendEntryResponseChannels aer_ch[3];
typedef RequestVote {
byte term, lastLogIndex, lastLogTerm
};
typedef RequestVoteChannels {
chan ch[3] = [1] of { RequestVote };
};
RequestVoteChannels rv_ch[3];
typedef RequestVoteResponse {
byte term;
bool voteGranted
};
typedef RequestVoteResponseChannels {
chan ch[3] = [1] of { RequestVoteResponse };
};
RequestVoteResponseChannels rvr_ch[3];
// The following variables are actually local,
// we move them globally, because SPIN doesn't support
// that represent LTL with local variables.
mtype:State = { leader, candidate, follower };
mtype:State state[3];
byte currentTerm[3];
typedef Logs {
byte log[2];
};
Logs log[3];
byte commitIndex[3];
// If commitIndex reaches MAX_LOG, the whole system is nearly full.
// There's no need to run further.
proctype server(byte serverId) {
state[serverId] = follower;
byte votedFor = NIL;
// helpers
byte i;
bool votesGranted[3];
RequestVote rv;
byte lastLogTerm, lastLogIndex;
RequestVoteResponse rvr;
bool logOk;
AppendEntry ae;
AppendEntryResponse aer;
do // main loop
:: // timeout
(state[serverId] == candidate || state[serverId] == follower) ->
atomic {
state[serverId] = candidate;
currentTerm[serverId] = currentTerm[serverId] + 1;
end_max_term: if // end if the limit is reached. Note that MAX_TERM is reachable here, which just shows the de#tention
:: (currentTerm[serverId] <= MAX_TERM) -> skip
fi
votedFor = serverId;
votesGranted[0] = 0; votesGranted[1] = 0; votesGranted[2] = 0;
votesGranted[serverId] = 1;
}
:: // restart
state[serverId] = follower
:: // request vote
(state[serverId] == candidate) ->
atomic {
rv.term = currentTerm[serverId];
if
:: (log[serverId].log[0] == 0) ->
rv.lastLogTerm = 0;
rv.lastLogIndex = 0
:: (log[serverId].log[0] != 0 && log[serverId].log[1] == 0) ->
rv.lastLogTerm = log[serverId].log[0];
rv.lastLogIndex = 1
:: (log[serverId].log[0] != 0 && log[serverId].log[1] != 0) ->
rv.lastLogTerm = log[serverId].log[1];
rv.lastLogIndex = 2
fi
if
:: (serverId != 0) ->
end_rv_0: rv_ch[serverId].ch[0]!rv
:: (serverId != 1) ->
end_rv_1: rv_ch[serverId].ch[1]!rv
:: (serverId != 2) ->
end_rv_2: rv_ch[serverId].ch[2]!rv
fi
}
:: // become leader
(state[serverId] == candidate && (votesGranted[0] + votesGranted[1] + votesGranted[2] > 1)) ->
state[serverId] = leader;
:: // handle RequestVote
(rv_ch[0].ch[serverId]?[rv] || rv_ch[1].ch[serverId]?[rv] || rv_ch[2].ch[serverId]?[rv]) ->
atomic {
// calculate the id of the sender
if
:: rv_ch[0].ch[serverId]?rv -> i = 0
:: rv_ch[1].ch[serverId]?rv -> i = 1
:: rv_ch[2].ch[serverId]?rv -> i = 2
fi
assert(i != serverId);
// update terms
if
:: (rv.term > currentTerm[serverId]) ->
currentTerm[serverId] = rv.term;
state[serverId] = follower;
votedFor = NIL
:: (rv.term <= currentTerm[serverId]) ->
skip
fi
if
:: (log[serverId].log[0] == 0) ->
lastLogTerm = 0;
lastLogIndex = 0
:: (log[serverId].log[0] != 0 && log[serverId].log[1] == 0) ->
lastLogTerm = log[serverId].log[0];
lastLogIndex = 1
:: (log[serverId].log[0] != 0 && log[serverId].log[1] != 0) ->
lastLogTerm = log[serverId].log[1];
lastLogIndex = 2
fi
logOk = rv.lastLogTerm > lastLogTerm || rv.lastLogTerm == lastLogTerm && rv.lastLogIndex >= lastLogIndex;
rvr.voteGranted = rv.term == currentTerm[serverId] && logOk && (votedFor == NIL || votedFor == i);
rvr.term = currentTerm[serverId];
if
:: rvr.voteGranted -> votedFor = i
:: !rvr.voteGranted -> skip
fi
end_rvr: rvr_ch[serverId].ch[i]!rvr
}
:: // handle RequestVoteResponse
(rvr_ch[0].ch[serverId]?[rvr] || rvr_ch[1].ch[serverId]?[rvr] || rvr_ch[2].ch[serverId]?[rvr]) ->
atomic {
// calculate the id of the sender
if
:: rvr_ch[0].ch[serverId]?rvr -> i = 0
:: rvr_ch[1].ch[serverId]?rvr -> i = 1
:: rvr_ch[2].ch[serverId]?rvr -> i = 2
fi
assert(i != serverId);
if
:: (rvr.term > currentTerm[serverId]) -> // update terms
currentTerm[serverId] = rvr.term;
state[serverId] = follower;
votedFor = NIL
:: (rvr.term == currentTerm[serverId] && rvr.voteGranted) ->
votesGranted[i] = 1
:: !(rvr.term > currentTerm[serverId]) && !(rvr.term == currentTerm[serverId] && rvr.voteGranted) ->
skip
fi
}
:: // append entries
(state[serverId] == leader) ->
atomic {
if
:: (serverId != 0) -> i = 0
:: (serverId != 1) -> i = 1
:: (serverId != 2) -> i = 2
fi
ae.term = currentTerm[serverId];
ae.leaderCommit = commitIndex[serverId];
if
:: (log[serverId].log[0] != log[i].log[0]) ->
ae.index = 0
:: (log[serverId].log[1] != 0 && log[serverId].log[0] == log[i].log[0] && log[serverId].log[1] != log[i].log[1]) ->
ae.index = 1
ae.prevLogTerm = log[i].log[0]
:: ae.index = NIL
fi
end_ae: ae_ch[serverId].ch[i]!ae
}
:: // handle AppendEntry
(ae_ch[0].ch[serverId]?[ae] || ae_ch[1].ch[serverId]?[ae] || ae_ch[2].ch[serverId]?[ae]) ->
atomic {
// calculate the id of the sender
if
:: ae_ch[0].ch[serverId]?ae -> i = 0
:: ae_ch[1].ch[serverId]?ae -> i = 1
:: ae_ch[2].ch[serverId]?ae -> i = 2
fi
assert(i != serverId);
// update terms
if
:: (ae.term > currentTerm[serverId]) ->
currentTerm[serverId] = ae.term;
state[serverId] = follower;
votedFor = NIL
:: (ae.term <= currentTerm[serverId]) ->
skip
fi
assert(ae.term <= currentTerm[serverId]);
// return to follower state
if
:: (ae.term == currentTerm[serverId] && state[serverId] == candidate) ->
state[serverId] = follower;
votedFor = NIL
:: (ae.term != currentTerm[serverId] || state[serverId] != candidate) ->
skip
fi
assert(!(ae.term == currentTerm[serverId]) || (state[serverId] == follower));
logOk = ae.index == 0 || (ae.index == 1 && ae.prevLogTerm == log[serverId].log[0]);
aer.term = currentTerm[serverId];
if
:: (ae.term < currentTerm[i] || ae.term == currentTerm[serverId] && state[serverId] == follower && !logOk) -> // reject request
aer.success = 0;
end_aer_rej: aer_ch[serverId].ch[i]!aer
:: (ae.term == currentTerm[serverId] && state[serverId] == follower && logOk) ->
aer.success = 1;
log[serverId].log[ae.index] = ae.term;
// Direct assignment is admissible here.
// Because our MAX_LOG is small enough (2).
// leaderCommit is either 0 or 1.
// If leaderCommit is 0, commitIndex of the server must be 0.
// If leaderCommit is 1, commitIndex of the server can be 0.
commitIndex[serverId] = ae.leaderCommit;
end_aer_acc: aer_ch[serverId].ch[i]!aer
fi
}
:: // handle AppendEntryResponse
(aer_ch[0].ch[serverId]?[aer] || aer_ch[1].ch[serverId]?[aer] || aer_ch[2].ch[serverId]?[aer]) ->
atomic {
// calculate the id of the sender
if
:: aer_ch[0].ch[serverId]?aer -> i = 0
:: aer_ch[1].ch[serverId]?aer -> i = 1
:: aer_ch[2].ch[serverId]?aer -> i = 2
fi
assert(i != serverId);
if
:: (aer.term > currentTerm[serverId]) -> // update terms
currentTerm[serverId] = aer.term;
state[serverId] = follower;
votedFor = NIL
:: (aer.term < currentTerm[serverId]) ->
skip
:: (aer.term == currentTerm[serverId] && aer.success && state[serverId] == leader) ->
// advance commit index
// as we only have 3 servers
// one success AppendEntry means committed
end_commitIndex: if // end if commitIndex reaches the limit
:: (commitIndex[serverId] == 0 && log[i].log[0] == log[serverId].log[0]) ->
commitIndex[serverId] = 1
// this is a little tricky
// we do NOT skip if commitIndex[serverId] should be 2
:: (commitIndex[serverId] == 1 && !(log[serverId].log[1] != 0 && log[i].log[1] == log[serverId].log[1])) ->
skip; // actually this case won't be reached
fi
:: (aer.term == currentTerm[serverId] && !(aer.success && state[serverId] == leader)) ->
skip
fi
}
:: // client request
(state[serverId] == leader && log[serverId].log[1] == 0) ->
if
:: log[serverId].log[0] == 0 ->
log[serverId].log[0] = currentTerm[serverId]
:: log[serverId].log[0] != 0 ->
log[serverId].log[1] = currentTerm[serverId]
fi
od
};
init {
run server(0);
run server(1);
run server(2)
}
ltl electionSafety {
always !(
(state[0] == leader && state[1] == leader && currentTerm[0] == currentTerm[1])
|| (state[0] == leader && state[2] == leader && currentTerm[0] == currentTerm[2])
|| (state[1] == leader && state[2] == leader && currentTerm[1] == currentTerm[2])
)
}
// for scalability of SPIN, we split the huge complete formula into small formulas
ltl leaderAppendOnly00 {
always (
state[0] == leader implies (
(log[0].log[0] == 0)
|| ((log[0].log[0] == 1) weakuntil (state[0] != leader))
|| ((log[0].log[0] == 2) weakuntil (state[0] != leader))
|| ((log[0].log[0] == 3) weakuntil (state[0] != leader))
)
)
}
ltl leaderAppendOnly01 {
always (
state[0] == leader implies (
(log[0].log[1] == 0)
|| ((log[0].log[1] == 1) weakuntil (state[0] != leader))
|| ((log[0].log[1] == 2) weakuntil (state[0] != leader))
|| ((log[0].log[1] == 3) weakuntil (state[0] != leader))
)
)
}
ltl leaderAppendOnly10 {
always (
state[1] == leader implies (
(log[1].log[0] == 0)
|| ((log[1].log[0] == 1) weakuntil (state[1] != leader))
|| ((log[1].log[0] == 2) weakuntil (state[1] != leader))
|| ((log[1].log[0] == 3) weakuntil (state[1] != leader))
)
)
}
ltl leaderAppendOnly11 {
always (
state[1] == leader implies (
(log[1].log[1] == 0)
|| ((log[1].log[1] == 1) weakuntil (state[1] != leader))
|| ((log[1].log[1] == 2) weakuntil (state[1] != leader))
|| ((log[1].log[1] == 3) weakuntil (state[1] != leader))
)
)
}
ltl leaderAppendOnly20 {
always (
state[2] == leader implies (
(log[2].log[0] == 0)
|| ((log[2].log[0] == 1) weakuntil (state[2] != leader))
|| ((log[2].log[0] == 2) weakuntil (state[2] != leader))
|| ((log[2].log[0] == 3) weakuntil (state[2] != leader))
)
)
}
ltl leaderAppendOnly21 {
always (
state[2] == leader implies (
(log[2].log[1] == 0)
|| ((log[2].log[1] == 1) weakuntil (state[2] != leader))
|| ((log[2].log[1] == 2) weakuntil (state[2] != leader))
|| ((log[2].log[1] == 3) weakuntil (state[2] != leader))
)
)
}
ltl logMatching {
always (
((log[0].log[1] != 0 && log[0].log[1] == log[1].log[1])
implies (log[0].log[0] == log[1].log[0]))
&& ((log[0].log[1] != 0 && log[0].log[1] == log[2].log[1])
implies (log[0].log[0] == log[2].log[0]))
&& ((log[1].log[1] != 0 && log[1].log[1] == log[2].log[1])
implies (log[1].log[0] == log[2].log[0]))
)
}
// 这里我们已知被 commit 的 entry 就不会再改了,这需要基于这样一个观察:
// 每一个 server 在将来都可能成为 leader
ltl leaderCompleteness {
always (
(
(commitIndex[0] == 1) implies
always (
((state[1] == leader) implies (log[0].log[0] == log[1].log[0]))
&& ((state[2] == leader) implies (log[0].log[0] == log[2].log[0]))
)
) && (
(commitIndex[1] == 1) implies
always (
((state[0] == leader) implies (log[1].log[0] == log[0].log[0]))
&& ((state[2] == leader) implies (log[1].log[0] == log[2].log[0]))
)
) && (
(commitIndex[2] == 1) implies
always (
((state[0] == leader) implies (log[2].log[0] == log[0].log[0]))
&& ((state[1] == leader) implies (log[2].log[0] == log[1].log[0]))
)
)
)
}
ltl stateMachineSafety {
always (
((commitIndex[0] == 1 && commitIndex[1] == 1) implies (log[0].log[0] == log[1].log[0]))
&& ((commitIndex[0] == 1 && commitIndex[2] == 1) implies (log[0].log[0] == log[2].log[0]))
&& ((commitIndex[1] == 1 && commitIndex[2] == 1) implies (log[1].log[0] == log[2].log[0]))
)
}