-
-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathEWD998.tla
209 lines (173 loc) · 7.79 KB
/
EWD998.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
------------------------------- MODULE EWD998 -------------------------------
(***************************************************************************)
(* TLA+ specification of an algorithm for distributed termination *)
(* detection on a ring, due to Shmuel Safra, published as EWD 998: *)
(* Shmuel Safra's version of termination detection. *)
(* https://www.cs.utexas.edu/users/EWD/ewd09xx/EWD998.PDF *)
(***************************************************************************)
EXTENDS Integers, FiniteSets, Utils
N == 5
Node == 0 .. N-1
Color == {"white", "black"}
Token == [pos : Node, q : Int, color : Color]
VARIABLES
\* @type: Int -> Bool;
active, \* activation status of nodes
\* @type: Int -> Str;
color, \* color of nodes
\* @type: Int -> Int;
counter, \* nb of sent messages - nb of rcvd messages per node
\* @type: Int -> Int;
pending, \* nb of messages in transit to node
\* @type: [pos: Int, q: Int, color: Str];
token \* token structure
vars == <<active, color, counter, pending, token>>
TypeOK ==
/\ active \in [Node -> BOOLEAN]
/\ color \in [Node -> Color]
/\ counter \in [Node -> Int]
/\ pending \in [Node -> Nat]
/\ token \in Token
------------------------------------------------------------------------------
Init ==
(* EWD840 but nodes *)
/\ active \in [Node -> BOOLEAN]
/\ color \in [Node -> Color]
(* Rule 0 *)
/\ counter = [i \in Node |-> 0] \* c properly initialized
/\ pending = [i \in Node |-> 0]
/\ token = [pos |-> 0, q |-> 0, color |-> "black"]
InitiateProbe ==
(* Rules 1 + 5 + 6 *)
/\ token.pos = 0
/\ \* previous round not conclusive if:
\/ token.color = "black"
\/ color[0] = "black"
\/ counter[0] + token.q > 0
/\ token' = [pos |-> N-1, q |-> 0, color |-> "white"]
/\ color' = [ color EXCEPT ![0] = "white" ]
\* The state of the nodes remains unchanged by token-related actions.
/\ UNCHANGED <<active, counter, pending>>
PassToken(i) ==
(* Rules 2 + 4 + 7 *)
/\ ~ active[i] \* If machine i is active, keep the token.
/\ token.pos = i
/\ token' = [token EXCEPT !.pos = @ - 1,
!.q = @ + counter[i],
!.color = IF color[i] = "black" THEN "black" ELSE @]
/\ color' = [ color EXCEPT ![i] = "white" ]
\* The state of the nodes remains unchanged by token-related actions.
/\ UNCHANGED <<active, counter, pending>>
PassTokenEnabled(i) ==
/\ ~active[i] \* If machine i is active, keep the token.
/\ token.pos = i
InitiateProbeEnabled ==
/\ token.pos = 0
/\ \* previous round not conclusive if:
\/ token.color = "black"
\/ color[0] = "black"
\/ counter[0] + token.q > 0
SystemEnabled ==
\/ \E i \in Node \ {0}: PassTokenEnabled(i)
\/ InitiateProbeEnabled
System == \/ InitiateProbe
\/ \E i \in Node \ {0} : PassToken(i)
-----------------------------------------------------------------------------
SendMsg(i) ==
\* Only allowed to send msgs if node i is active.
/\ active[i]
(* Rule 0 *)
/\ counter' = [counter EXCEPT ![i] = @ + 1]
\* Non-deterministically choose a receiver node.
/\ \E j \in Node \ {i} : pending' = [pending EXCEPT ![j] = @ + 1]
\* Note that we don't blacken node i as in EWD840 if node i
\* sends a message to node j with j > i
/\ UNCHANGED <<active, color, token>>
RecvMsg(i) ==
/\ pending[i] > 0
/\ pending' = [pending EXCEPT ![i] = @ - 1]
(* Rule 0 *)
/\ counter' = [counter EXCEPT ![i] = @ - 1]
(* Rule 3 *)
/\ color' = [ color EXCEPT ![i] = "black" ]
\* Receipt of a message activates i.
/\ active' = [ active EXCEPT ![i] = TRUE ]
/\ UNCHANGED <<token>>
Deactivate(i) ==
/\ active[i]
/\ active' = [active EXCEPT ![i] = FALSE]
/\ UNCHANGED <<color, counter, pending, token>>
Environment == \E i \in Node : SendMsg(i) \/ RecvMsg(i) \/ Deactivate(i)
Plus(a, b) == a + b
-----------------------------------------------------------------------------
Next == (System \/ Environment)
Spec == Init /\ [][Next]_vars /\ WF_vars(System)
-----------------------------------------------------------------------------
(***************************************************************************)
(* Bound the otherwise infinite state space that TLC has to check. *)
(***************************************************************************)
StateConstraint ==
/\ \A i \in Node : counter[i] <= 3 /\ pending[i] <= 3
/\ token.q <= 9
-----------------------------------------------------------------------------
(***************************************************************************)
(* Main safety property: if there is a white token at node 0 and there are *)
(* no in-flight messages then every node is inactive. *)
(***************************************************************************)
terminationDetected ==
/\ token.pos = 0
/\ token.color = "white"
/\ token.q + counter[0] = 0
/\ color[0] = "white"
/\ ~ active[0]
/\ pending[0] = 0
(***************************************************************************)
(* The number of messages on their way. "in-flight" *)
(***************************************************************************)
B == FoldFunction(Plus, 0, pending)
(***************************************************************************)
(* The system has terminated if no node is active and there are no *)
(* in-flight messages. *)
(***************************************************************************)
Termination ==
/\ \A i \in Node : ~ active[i]
/\ B = 0
TerminationDetection ==
terminationDetected => Termination
(***************************************************************************)
(* Safra's inductive invariant *)
(***************************************************************************)
Inv ==
/\ P0:: B = FoldFunction(Plus, 0, counter)
(* (Ai: t < i < N: machine nr.i is passive) /\ *)
(* (Si: t < i < N: ci.i) = q *)
/\ \/ P1:: /\ \A i \in {x \in Node: (token.pos+1) <= x}: ~ active[i] \* machine nr.i is passive
/\ IF token.pos = N-1
THEN token.q = 0
ELSE token.q = FoldFunctionOnSet(Plus, 0, counter, {x \in Node: (token.pos+1) <= x})
(* (Si: 0 <= i <= t: c.i) + q > 0. *)
\/ P2:: FoldFunctionOnSet(Plus, 0, counter, {x \in Node: x <= token.pos}) + token.q > 0
(* Ei: 0 <= i <= t : machine nr.i is black. *)
\/ P3:: \E i \in {x \in Node: x <= token.pos} : color[i] = "black"
(* The token is black. *)
\/ P4:: token.color = "black"
(***************************************************************************)
(* Liveness property: termination is eventually detected. *)
(***************************************************************************)
Liveness ==
[](Termination => <>terminationDetected)
(***************************************************************************)
(* The algorithm implements the specification of termination detection *)
(* in a ring with asynchronous communication. *)
(* The parameters of module AsyncTerminationDetection are instantiated *)
(* by the symbols of the same name of the present module. *)
(***************************************************************************)
TD == INSTANCE AsyncTerminationDetection
THEOREM Spec => TD!Spec
=============================================================================
Checked with TLC in 01/2021 with two cores on a fairly modern desktop
and the given state constraint StateConstraint above:
| N | Diameter | Distinct States | States | Time |
| --- | --- | --- | --- | --- |
| 3 | 60 | 1.3m | 10.1m | 42 s |
| 4 | 105 | 219m | 2.3b | 50 m |