-
Notifications
You must be signed in to change notification settings - Fork 0
/
NOPaxos.tla
648 lines (582 loc) · 25.5 KB
/
NOPaxos.tla
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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
------------------------------ MODULE NOPaxos ----------------------------------
(*
Specifies the NOPaxos protocol.
*)
EXTENDS Naturals, FiniteSets, Sequences
--------------------------------------------------------------------------------
(* `^\textbf{\large Constants}^' *)
\* The set of replicas and an ordering of them
CONSTANTS Replicas, ReplicaOrder
ASSUME IsFiniteSet(Replicas) /\ ReplicaOrder \in Seq(Replicas)
\* Message sequencers
CONSTANT NumSequencers \* Normally infinite, assumed finite for model checking
Sequencers == (1..NumSequencers)
\* Set of possible values in a client request and a special null value
CONSTANTS Values, NoOp
\* Replica Statuses
CONSTANTS StNormal, StViewChange, StGapCommit
\* Message Types
CONSTANTS MClientRequest, \* Sent by client to sequencer
MMarkedClientRequest, \* Sent by sequencer to replicas
MRequestReply, \* Sent by replicas to client
MSlotLookup, \* Sent by followes to get the value of a slot
MSlotLookupRep, \* Sent by the leader with a value/NoOp
MGapCommit, \* Sent by the leader to commit a gap
MGapCommitRep, \* Sent by the followers to ACK a gap commit
MViewChangeReq, \* Sent when leader/sequencer failure detected
MViewChange, \* Sent to ACK view change
MStartView, \* Sent by new leader to start view
MSyncPrepare, \* Sent by the leader to ensure log durability
MSyncRep, \* Sent by followers as ACK
MSyncCommit \* Sent by leaders to indicate stable log
(*
`^\textbf{Message Schemas}^'
ViewIDs == [ leaderNum |-> n \in (1..), sessNum |-> n \in (1..) ]
ClientRequest
[ mtype |-> MClientRequest,
value |-> v \in Values ]
MarkedClientRequest
[ mtype |-> MMarkedClientRequest,
dest |-> r \in Replicas,
value |-> v \in Values,
sessNum |-> s \in Sequencers,
sessMsgNum |-> n \in (1..) ]
RequestReply
[ mtype |-> MRequestReply,
sender |-> r \in Replicas,
viewID |-> v \in ViewIDs,
request |-> v \in Values \cup {NoOp},
logSlotNum |-> n \in (1..) ]
SlotLookup
[ mtype |-> MSlotLookup,
dest |-> r \in Replicas,
sender |-> r \in Replicas,
viewID |-> v \in ViewIDs,
sessMsgNum |-> n \in (1..) ]
GapCommit
[ mtype |-> MGapCommit,
dest |-> r \in Replicas,
viewID |-> v \in ViewIDs,
slotNumber |-> n \in (1..) ]
GapCommitRep
[ mtype |-> MGapCommitRep,
dest |-> r \in Replicas,
sender |-> r \in Replicas,
viewID |-> v \in ViewIDs,
slotNumber |-> n \in (1..) ]
ViewChangeReq
[ mtype |-> MViewChangeReq,
dest |-> r \in Replicas,
viewID |-> v \in ViewIDs ]
ViewChange
[ mtype |-> MViewChange,
dest |-> r \in Replicas,
sender |-> r \in Replicas,
viewID |-> v \in ViewIDs,
lastNormal |-> v \in ViewIDs,
sessMsgNum |-> n \in (1..),
log |-> l \in (1..) \times (Values \cup {NoOp}) ]
StartView
[ mtype |-> MStartView,
dest |-> r \in Replicas,
viewID |-> v \in ViewIDs,
log |-> l \in (1..) \times (Values \cup {NoOp}),
sessMsgNum |-> n \in (1..) ]
SyncPrepare
[ mtype |-> MSyncPrepare,
dest |-> r \in Replicas,
sender |-> r \in Replicas,
viewID |-> v \in ViewIDs,
sessMsgNum |-> n \in (1..),
log |-> l \in (1..) \times (Values \cup {NoOp}) ]
SyncRep
[ mtype |-> MSyncRep,
dest |-> r \in Replicas,
sender |-> r \in Replicas,
viewID |-> v \in ViewIDs,
logSlotNumber |-> n \in (1..) ]
SyncCommit
[ mtype |-> MSyncCommit,
dest |-> r \in Replicas,
sender |-> r \in Replicas,
viewID |-> v \in ViewIDs,
log |-> l \in (1..) \times (Values \cup {NoOp}),
sessMsgNum |-> n \in (1..) ]
*)
--------------------------------------------------------------------------------
(* `^\textbf{\large Variables}^' *)
\* `^\textbf{Network State}^'
VARIABLE messages \* Set of all messages sent
networkVars == << messages >>
InitNetworkState == messages = {}
\* `^\textbf{Sequencer State}^'
VARIABLE seqMsgNums
sequencerVars == << seqMsgNums >>
InitSequencerState == seqMsgNums = [ s \in Sequencers |-> 1 ]
\* `^\textbf{Replica State}^'
VARIABLES vLog, \* Log of values and gaps
vSessMsgNum, \* The number of messages received in the OUM session
vReplicaStatus, \* One of StNormal, StViewChange, and StGapCommit
vViewID, \* Current viewID replicas recognize
vLastNormView, \* Last views in which replicas had status StNormal
vViewChanges, \* Used for logging view change votes
vCurrentGapSlot, \* Used for gap commit at leader
vGapCommitReps, \* Used for logging gap commit reps at leader
vSyncPoint, \* Synchronization point for replicas
vTentativeSync, \* Used by leader to mark current syncing point
vSyncReps \* Used for logging sync reps at leader
replicaVars == << vLog, vViewID, vSessMsgNum, vLastNormView, vViewChanges,
vGapCommitReps, vCurrentGapSlot, vReplicaStatus,
vSyncPoint, vTentativeSync, vSyncReps >>
InitReplicaState ==
/\ vLog = [ r \in Replicas |-> << >> ]
/\ vViewID = [ r \in Replicas |->
[ sessNum |-> 1, leaderNum |-> 1 ] ]
/\ vLastNormView = [ r \in Replicas |->
[ sessNum |-> 1, leaderNum |-> 1 ] ]
/\ vSessMsgNum = [ r \in Replicas |-> 1 ]
/\ vViewChanges = [ r \in Replicas |-> {} ]
/\ vGapCommitReps = [ r \in Replicas |-> {} ]
/\ vCurrentGapSlot = [ r \in Replicas |-> 0 ]
/\ vReplicaStatus = [ r \in Replicas |-> StNormal ]
/\ vSyncPoint = [ r \in Replicas |-> 0 ]
/\ vTentativeSync = [ r \in Replicas |-> 0 ]
/\ vSyncReps = [ r \in Replicas |-> {} ]
\* `^\textbf{Set of all vars}^'
vars == << networkVars, sequencerVars, replicaVars >>
\* `^\textbf{Initial state}^'
Init == /\ InitNetworkState
/\ InitSequencerState
/\ InitReplicaState
--------------------------------------------------------------------------------
(* `^\textbf{\large Helpers}^' *)
Max(s) == CHOOSE x \in s : \A y \in s : x >= y
\* `^\textbf{View ID Helpers}^'
Leader(viewID) == ReplicaOrder[(viewID.leaderNum % Len(ReplicaOrder)) +
(IF viewID.leaderNum >= Len(ReplicaOrder)
THEN 1 ELSE 0)]
ViewLe(v1, v2) == /\ v1.sessNum <= v2.sessNum
/\ v1.leaderNum <= v2.leaderNum
ViewLt(v1, v2) == ViewLe(v1, v2) /\ v1 /= v2
\* `^\textbf{Network Helpers}^'
\* Add a message to the network
Send(ms) == messages' = messages \cup ms
\* `^\textbf{Log Manipulation Helpers}^'
(* Combine logs, taking a NoOp for any slot that has a NoOp and a Value
otherwise. *)
CombineLogs(ls) ==
LET
combineSlot(xs) == IF NoOp \in xs THEN
NoOp
ELSE IF xs = {} THEN
NoOp
ELSE
CHOOSE x \in xs : x /= NoOp
range == Max({ Len(l) : l \in ls})
IN
[i \in (1..range) |->
combineSlot({l[i] : l \in { k \in ls : i <= Len(k) }})]
\* Insert x into log l at position i (which should be <= Len(l) + 1)
ReplaceItem(l, i, x) ==
[ j \in 1..Max({Len(l), i}) |-> IF j = i THEN x ELSE l[j] ]
\* Subroutine to send an MGapCommit message
SendGapCommit(r) ==
LET
slot == Len(vLog[r]) + 1
IN
/\ Leader(vViewID[r]) = r
/\ vReplicaStatus[r] = StNormal
/\ vReplicaStatus' = [ vReplicaStatus EXCEPT ![r] = StGapCommit ]
/\ vGapCommitReps' = [ vGapCommitReps EXCEPT ![r] = {} ]
/\ vCurrentGapSlot' = [ vCurrentGapSlot EXCEPT ![r] = slot ]
/\ Send({[ mtype |-> MGapCommit,
dest |-> d,
slotNumber |-> slot,
viewID |-> vViewID[r] ] : d \in Replicas})
/\ UNCHANGED << sequencerVars, vLog, vViewID, vSessMsgNum,
vLastNormView, vViewChanges, vSyncPoint,
vTentativeSync, vSyncReps >>
--------------------------------------------------------------------------------
(* `^\textbf{\large Main Spec}^' *)
Quorums == {R \in SUBSET(Replicas) : Cardinality(R) * 2 > Cardinality(Replicas)}
(*
A request is committed if a quorum sent replies with matching view-id and
log-slot-num, where one of the replies is from the leader. The following
predicate is true iff value v is committed in slot i.
`~TODO: add temporal formula stating Committed implies always Committed
(this is obvious, though, because nothing gets taken out of messages).~'
*)
Committed(v, i) ==
\E M \in SUBSET ({m \in messages : /\ m.mtype = MRequestReply
/\ m.logSlotNum = i
/\ m.request = v }) :
\* Sent from a quorum
/\ { m.sender : m \in M } \in Quorums
\* Matching view-id
/\ \E m1 \in M : \A m2 \in M : m1.viewID = m2.viewID
\* One from the leader
/\ \E m \in M : m.sender = Leader(m.viewID)
(*
We only provide the ordering layer here. This is an easier guarantee to
provide than saying the execution is equivalent to a linear one. We don't
currently model execution, and that's a much harder predicate to compute.
*)
Linearizability ==
LET
maxLogPosition == Max({1} \cup
{ m.logSlotNum : m \in {m \in messages : m.mtype = MRequestReply } })
IN ~(\E v1, v2 \in Values \cup { NoOp } :
/\ v1 /= v2
/\ \E i \in (1 .. maxLogPosition) :
/\ Committed(v1, i)
/\ Committed(v2, i)
)
SyncSafety == \A r \in Replicas :
\A i \in 1..vSyncPoint[r] :
Committed(vLog[r][i], i)
--------------------------------------------------------------------------------
(* `^\textbf{\large Message Handlers and Actions}^' *)
\* `^\textbf{Client action}^'
\* Send a request for value v
ClientSendsRequest(v) == /\ Send({[ mtype |-> MClientRequest,
value |-> v ]})
/\ UNCHANGED << sequencerVars, replicaVars >>
\* `^\textbf{Normal Case Handlers}^'
\* Sequencer s receives MClientRequest, m
HandleClientRequest(m, s) ==
LET
smn == seqMsgNums[s]
IN
/\ Send({[ mtype |-> MMarkedClientRequest,
dest |-> r,
value |-> m.value,
sessNum |-> s,
sessMsgNum |-> smn ] : r \in Replicas})
/\ seqMsgNums' = [ seqMsgNums EXCEPT ![s] = smn + 1 ]
/\ UNCHANGED replicaVars
\* Replica r receives MMarkedClientRequest, m
HandleMarkedClientRequest(r, m) ==
/\ vReplicaStatus[r] = StNormal
\* Normal case
/\ \/ /\ m.sessNum = vViewID[r].sessNum
/\ m.sessMsgNum = vSessMsgNum[r]
/\ vLog' = [ vLog EXCEPT ![r] = Append(vLog[r], m.value) ]
/\ vSessMsgNum' = [ vSessMsgNum EXCEPT ![r] = vSessMsgNum[r] + 1 ]
/\ Send({[ mtype |-> MRequestReply,
request |-> m.value,
viewID |-> vViewID[r],
logSlotNum |-> Len(vLog'[r]),
sender |-> r ]})
/\ UNCHANGED << sequencerVars,
vViewID, vLastNormView, vCurrentGapSlot, vGapCommitReps,
vViewChanges, vReplicaStatus, vSyncPoint,
vTentativeSync, vSyncReps >>
\* SESSION-TERMINATED Case
\/ /\ m.sessNum > vViewID[r].sessNum
/\ LET
newViewID == [ sessNum |-> m.sessNum,
leaderNum |-> vViewID[r].leaderNum ]
IN
/\ Send({[ mtype |-> MViewChangeReq,
dest |-> d,
viewID |-> newViewID ] : d \in Replicas})
/\ UNCHANGED << replicaVars, sequencerVars >>
\* DROP-NOTIFICATION Case
\/ /\ m.sessNum = vViewID[r].sessNum
/\ m.sessMsgNum > vSessMsgNum[r]
\* If leader, commit a gap
/\ \/ /\ r = Leader(vViewID[r])
/\ SendGapCommit(r)
\* Otherwise, ask the leader
\/ /\ r /= Leader(vViewID[r])
/\ Send({[ mtype |-> MSlotLookup,
viewID |-> vViewID[r],
dest |-> Leader(vViewID[r]),
sender |-> r,
sessMsgNum |-> vSessMsgNum[r] ]})
/\ UNCHANGED << replicaVars, sequencerVars >>
\* `^\textbf{Gap Commit Handlers}^'
\* Replica r receives SlotLookup, m
HandleSlotLookup(r, m) ==
LET
logSlotNum == Len(vLog[r]) + 1 - (vSessMsgNum[r] - m.sessMsgNum)
IN
/\ m.viewID = vViewID[r]
/\ Leader(vViewID[r]) = r
/\ vReplicaStatus[r] = StNormal
/\ \/ /\ logSlotNum <= Len(vLog[r])
/\ Send({[ mtype |-> MMarkedClientRequest,
dest |-> m.sender,
value |-> vLog[r][logSlotNum],
sessNum |-> vViewID[r].sessNum,
sessMsgNum |-> m.sessMsgNum ]})
/\ UNCHANGED << replicaVars, sequencerVars >>
\/ /\ logSlotNum = Len(vLog[r]) + 1
/\ SendGapCommit(r)
\* Replica r receives GapCommit, m
HandleGapCommit(r, m) ==
/\ m.viewID = vViewID[r]
/\ m.slotNumber <= Len(vLog[r]) + 1
/\ \/ vReplicaStatus[r] = StNormal
\/ vReplicaStatus[r] = StGapCommit
/\ vLog' = [ vLog EXCEPT ![r] = ReplaceItem(vLog[r], m.slotNumber, NoOp) ]
\* Increment the msgNumber if necessary
/\ IF m.slotNumber > Len(vLog[r]) THEN
vSessMsgNum' = [ vSessMsgNum EXCEPT ![r] = vSessMsgNum[r] + 1 ]
ELSE
UNCHANGED vSessMsgNum
/\ Send({[ mtype |-> MGapCommitRep,
dest |-> Leader(vViewID[r]),
sender |-> r,
slotNumber |-> m.slotNumber,
viewID |-> vViewID[r] ],
[ mtype |-> MRequestReply,
request |-> NoOp,
viewID |-> vViewID[r],
logSlotNum |-> m.slotNumber,
sender |-> r ]})
/\ UNCHANGED << sequencerVars, vGapCommitReps, vViewID, vCurrentGapSlot,
vReplicaStatus, vLastNormView, vViewChanges,
vSyncPoint, vTentativeSync, vSyncReps >>
\* Replica r receives GapCommitRep, m
HandleGapCommitRep(r, m) ==
/\ vReplicaStatus[r] = StGapCommit
/\ m.viewID = vViewID[r]
/\ Leader(vViewID[r]) = r
/\ m.slotNumber = vCurrentGapSlot[r]
/\ vGapCommitReps' =
[ vGapCommitReps EXCEPT ![r] = vGapCommitReps[r] \cup {m} ]
\* When there's enough, resume StNormal and process more messages
/\ LET isViewPromise(M) == /\ { n.sender : n \in M } \in Quorums
/\ \E n \in M : n.sender = r
gCRs == { n \in vGapCommitReps'[r] :
/\ n.mtype = MGapCommitRep
/\ n.viewID = vViewID[r]
/\ n.slotNumber = vCurrentGapSlot[r] }
IN
IF isViewPromise(gCRs) THEN
vReplicaStatus' = [ vReplicaStatus EXCEPT ![r] = StNormal ]
ELSE
UNCHANGED vReplicaStatus
/\ UNCHANGED << sequencerVars, networkVars, vLog, vViewID, vCurrentGapSlot,
vSessMsgNum, vLastNormView, vViewChanges, vSyncPoint,
vTentativeSync, vSyncReps >>
\* `^\textbf{Failure Cases}^'
\* Replica r starts a Leader change
StartLeaderChange(r) ==
LET
newViewID == [ sessNum |-> vViewID[r].sessNum,
leaderNum |-> vViewID[r].leaderNum + 1 ]
IN
/\ Send({[ mtype |-> MViewChangeReq,
dest |-> d,
viewID |-> newViewID ] : d \in Replicas})
/\ UNCHANGED << replicaVars, sequencerVars >>
\* `^\textbf{View Change Handlers}^'
\* Replica r gets MViewChangeReq, m
HandleViewChangeReq(r, m) ==
LET
currentViewID == vViewID[r]
newSessNum == Max({currentViewID.sessNum, m.viewID.sessNum})
newLeaderNum == Max({currentViewID.leaderNum, m.viewID.leaderNum})
newViewID == [ sessNum |-> newSessNum, leaderNum |-> newLeaderNum ]
IN
/\ currentViewID /= newViewID
/\ vReplicaStatus' = [ vReplicaStatus EXCEPT ![r] = StViewChange ]
/\ vViewID' = [ vViewID EXCEPT ![r] = newViewID ]
/\ vViewChanges' = [ vViewChanges EXCEPT ![r] = {} ]
/\ Send({[ mtype |-> MViewChange,
dest |-> Leader(newViewID),
sender |-> r,
viewID |-> newViewID,
lastNormal |-> vLastNormView[r],
sessMsgNum |-> vSessMsgNum[r],
log |-> vLog[r] ]} \cup
\* Send the MViewChangeReqs in case this is an entirely new view
{[ mtype |-> MViewChangeReq,
dest |-> d,
viewID |-> newViewID ] : d \in Replicas})
/\ UNCHANGED << vCurrentGapSlot, vGapCommitReps, vLog, vSessMsgNum,
vLastNormView, sequencerVars, vSyncPoint,
vTentativeSync, vSyncReps >>
\* Replica r receives MViewChange, m
HandleViewChange(r, m) ==
\* Add the message to the log
/\ vViewID[r] = m.viewID
/\ vReplicaStatus[r] = StViewChange
/\ Leader(vViewID[r]) = r
/\ vViewChanges' =
[ vViewChanges EXCEPT ![r] = vViewChanges[r] \cup {m}]
\* If there's enough, start the new view
/\ LET
isViewPromise(M) == /\ { n.sender : n \in M } \in Quorums
/\ \E n \in M : n.sender = r
vCMs == { n \in vViewChanges'[r] :
/\ n.mtype = MViewChange
/\ n.viewID = vViewID[r] }
\* Create the state for the new view
normalViews == { n.lastNormal : n \in vCMs }
lastNormal == (CHOOSE v \in normalViews : \A v2 \in normalViews :
ViewLe(v2, v))
goodLogs == { n.log : n \in
{ o \in vCMs : o.lastNormal = lastNormal } }
\* If updating seqNum, revert sessMsgNum to 0; otherwise use latest
newMsgNum ==
IF lastNormal.sessNum = vViewID[r].sessNum THEN
Max({ n.sessMsgNum : n \in
{ o \in vCMs : o.lastNormal = lastNormal } })
ELSE
0
IN
IF isViewPromise(vCMs) THEN
Send({[ mtype |-> MStartView,
dest |-> d,
viewID |-> vViewID[r],
log |-> CombineLogs(goodLogs),
sessMsgNum |-> newMsgNum ] : d \in Replicas })
ELSE
UNCHANGED networkVars
/\ UNCHANGED << vReplicaStatus, vViewID, vLog, vSessMsgNum, vCurrentGapSlot,
vGapCommitReps, vLastNormView, sequencerVars, vSyncPoint,
vTentativeSync, vSyncReps >>
\* Replica r receives a MStartView, m
HandleStartView(r, m) ==
(*
Note how I handle this. There was actually a bug in prose description in the
paper where the following guard was underspecified.
*)
/\ \/ ViewLt(vViewID[r], m.viewID)
\/ vViewID[r] = m.viewID /\ vReplicaStatus[r] = StViewChange
/\ vLog' = [ vLog EXCEPT ![r] = m.log ]
/\ vSessMsgNum' = [ vSessMsgNum EXCEPT ![r] = m.sessMsgNum ]
/\ vReplicaStatus' = [ vReplicaStatus EXCEPT ![r] = StNormal ]
/\ vViewID' = [ vViewID EXCEPT ![r] = m.viewID ]
/\ vLastNormView' = [ vLastNormView EXCEPT ![r] = m.viewID ]
\* Send replies (in the new view) for all log items
/\ Send({[ mtype |-> MRequestReply,
request |-> m.log[i],
viewID |-> m.viewID,
logSlotNum |-> i,
sender |-> r ] : i \in (1..Len(m.log))})
/\ UNCHANGED << sequencerVars,
vViewChanges, vCurrentGapSlot, vGapCommitReps, vSyncPoint,
vTentativeSync, vSyncReps >>
\* `^\textbf{Synchronization handlers}^'
\* Leader replica r starts synchronization
StartSync(r) ==
/\ Leader(vViewID[r]) = r
/\ vReplicaStatus[r] = StNormal
/\ vSyncReps' = [ vSyncReps EXCEPT ![r] = {} ]
/\ vTentativeSync' = [ vTentativeSync EXCEPT ![r] = Len(vLog[r]) ]
/\ Send({[ mtype |-> MSyncPrepare,
sender |-> r,
dest |-> d,
viewID |-> vViewID[r],
sessMsgNum |-> vSessMsgNum[r],
log |-> vLog[r] ] : d \in Replicas })
/\ UNCHANGED << sequencerVars, vLog, vViewID, vSessMsgNum, vLastNormView,
vCurrentGapSlot, vViewChanges, vReplicaStatus,
vGapCommitReps, vSyncPoint >>
\* Replica r receives MSyncPrepare, m
HandleSyncPrepare(r, m) ==
LET
newLog == m.log \o SubSeq(vLog[r], Len(m.log) + 1, Len(vLog[r]))
newMsgNum == vSessMsgNum[r] + (Len(newLog) - Len(vLog[r]))
IN
/\ vReplicaStatus[r] = StNormal
/\ m.viewID = vViewID[r]
/\ m.sender = Leader(vViewID[r])
/\ vLog' = [ vLog EXCEPT ![r] = newLog ]
/\ vSessMsgNum' = [ vSessMsgNum EXCEPT ![r] = newMsgNum ]
/\ Send({[ mtype |-> MSyncRep,
sender |-> r,
dest |-> m.sender,
viewID |-> vViewID[r],
logSlotNumber |-> Len(m.log) ]} \cup
{[ mtype |-> MRequestReply,
request |-> vLog'[r][i],
viewID |-> vViewID[r],
logSlotNum |-> i,
sender |-> r ] : i \in 1..Len(vLog'[r])})
/\ UNCHANGED << sequencerVars, vViewID, vLastNormView, vCurrentGapSlot,
vViewChanges, vReplicaStatus, vGapCommitReps,
vSyncPoint, vTentativeSync, vSyncReps >>
\* Replica r receives MSyncRep, m
HandleSyncRep(r, m) ==
/\ m.viewID = vViewID[r]
/\ vReplicaStatus[r] = StNormal
/\ vSyncReps' = [ vSyncReps EXCEPT ![r] = vSyncReps[r] \cup { m } ]
/\ LET isViewPromise(M) == /\ { n.sender : n \in M } \in Quorums
/\ \E n \in M : n.sender = r
sRMs == { n \in vSyncReps'[r] :
/\ n.mtype = MSyncRep
/\ n.viewID = vViewID[r]
/\ n.logSlotNumber = vTentativeSync[r] }
committedLog == IF vTentativeSync[r] >= 1 THEN
SubSeq(vLog[r], 1, vTentativeSync[r])
ELSE
<< >>
IN
IF isViewPromise(sRMs) THEN
Send({[ mtype |-> MSyncCommit,
sender |-> r,
dest |-> d,
viewID |-> vViewID[r],
log |-> committedLog,
sessMsgNum |-> vSessMsgNum[r] -
(Len(vLog[r]) - Len(committedLog)) ] :
d \in Replicas })
ELSE
UNCHANGED networkVars
/\ UNCHANGED << sequencerVars, vLog, vViewID, vSessMsgNum, vLastNormView,
vCurrentGapSlot, vViewChanges, vReplicaStatus,
vGapCommitReps, vSyncPoint, vTentativeSync >>
\* Replica r receives MSyncCommit, m
HandleSyncCommit(r, m) ==
LET
newLog == m.log \o SubSeq(vLog[r], Len(m.log) + 1, Len(vLog[r]))
newMsgNum == vSessMsgNum[r] + (Len(newLog) - Len(vLog[r]))
IN
/\ vReplicaStatus[r] = StNormal
/\ m.viewID = vViewID[r]
/\ m.sender = Leader(vViewID[r])
/\ vLog' = [ vLog EXCEPT ![r] = newLog ]
/\ vSessMsgNum' = [ vSessMsgNum EXCEPT ![r] = newMsgNum ]
/\ vSyncPoint' = [ vSyncPoint EXCEPT ![r] = Len(m.log) ]
/\ UNCHANGED << sequencerVars, networkVars, vViewID, vLastNormView,
vCurrentGapSlot, vViewChanges, vReplicaStatus,
vGapCommitReps, vTentativeSync, vSyncReps >>
--------------------------------------------------------------------------------
(* `^\textbf{\large Main Transition Function}^' *)
Next == \* Handle Messages
\/ \E m \in messages :
\E s \in Sequencers
: /\ m.mtype = MClientRequest
/\ HandleClientRequest(m, s)
\/ \E m \in messages : /\ m.mtype = MMarkedClientRequest
/\ HandleMarkedClientRequest(m.dest, m)
\/ \E m \in messages : /\ m.mtype = MViewChangeReq
/\ HandleViewChangeReq(m.dest, m)
\/ \E m \in messages : /\ m.mtype = MViewChange
/\ HandleViewChange(m.dest, m)
\/ \E m \in messages : /\ m.mtype = MStartView
/\ HandleStartView(m.dest, m)
\/ \E m \in messages : /\ m.mtype = MSlotLookup
/\ HandleSlotLookup(m.dest, m)
\/ \E m \in messages : /\ m.mtype = MGapCommit
/\ HandleGapCommit(m.dest, m)
\/ \E m \in messages : /\ m.mtype = MGapCommitRep
/\ HandleGapCommitRep(m.dest, m)
\/ \E m \in messages : /\ m.mtype = MSyncPrepare
/\ HandleSyncPrepare(m.dest, m)
\/ \E m \in messages : /\ m.mtype = MSyncRep
/\ HandleSyncRep(m.dest, m)
\/ \E m \in messages : /\ m.mtype = MSyncCommit
/\ HandleSyncCommit(m.dest, m)
\* Client Actions
\/ \E v \in Values : ClientSendsRequest(v)
\* Start synchronization
\/ \E r \in Replicas : StartSync(r)
\* Failure case
\/ \E r \in Replicas : StartLeaderChange(r)
================================================================================