forked from tlaplus/Examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEWD687aPlusCal.tla
185 lines (166 loc) · 7.63 KB
/
EWD687aPlusCal.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
-------------------------- MODULE EWD687aPlusCal --------------------------
(***************************************************************************)
(* PlusCal representation of the algorithm by Dijkstra and Scholten for *)
(* detecting the termination of a distributed computation initiated by a *)
(* designated root node. The algorithm maintains a spanning tree that *)
(* contains all active nodes. Inactive leaf nodes detach themselves from *)
(* the tree, but note that a node may later be reactivated by receiving *)
(* a message and may reattach itself to the tree. When the root node has *)
(* no more children and becomes inactive, it declares termination. *)
(* *)
(* E.W. Dijkstra, C.S. Scholten: Termination detection for diffusing *)
(* computations. Information Processing Letters, 11 (1):1–4, 1980. *)
(***************************************************************************)
EXTENDS Integers, FiniteSets, TLC
CONSTANT
Node, \* the set of active nodes
initiator, \* initiator node
maxMsg \* maximum number of pending messages for bounding the state space
ASSUME /\ IsFiniteSet(Node)
/\ initiator \in Node
none == CHOOSE x : x \notin Node
(*
--algorithm DS {
variable
(* has termination been detected? *)
terminationDetected = FALSE,
(* For every node we keep the following counters:
- number of base messages received, per sending node
- number of ack messages received *)
network = [n \in Node |-> [msg |-> [snd \in Node |-> 0], ack |-> 0]];
define {
(* operators for sending and receiving messages:
the result is the network resulting from the operation *)
sendMsg(net, from, to) == [net EXCEPT ![to].msg[from] = @+1]
pendingMsg(net, to, from) == net[to].msg[from] > 0
receiveMsg(net, to, from) == [net EXCEPT ![to].msg[from] = @-1]
sendAck(net, to) == [net EXCEPT ![to].ack = @+1]
pendingAck(net, to) == net[to].ack > 0
receiveAck(net, to) == [net EXCEPT ![to].ack = @-1]
}
fair process (node \in Node)
variables active = (self = initiator),
parent = IF self = initiator THEN self ELSE none,
activeSons = 0;
{
l: while (TRUE) {
either { \* send a (base) message to some other node
when active;
with (dst \in Node \ {self}) {
network := sendMsg(network, self, dst)
};
activeSons := activeSons + 1
} or { \* terminate
when active;
active := FALSE
} or { \* receive a base message
with (snd \in Node) {
when pendingMsg(network, self, snd);
active := TRUE;
with (net = receiveMsg(network, self, snd)) {
\* accept sender as parent if there is none, else send ack
if (parent = none) {
parent := snd;
network := net
} else {
network := sendAck(net, snd)
}
}
}
} or { \* receive an ack
when pendingAck(network, self);
network := receiveAck(network, self);
activeSons := activeSons - 1
} or { \* detach or declare termination
when (~active /\ activeSons = 0 /\ parent # none);
if (parent = self) {
terminationDetected := TRUE
} else {
network := sendAck(network, parent);
};
parent := none
}
}
}
}
*)
\* BEGIN TRANSLATION (chksum(pcal) = "2130f031" /\ chksum(tla) = "7d13cf45")
VARIABLES terminationDetected, network
(* define statement *)
sendMsg(net, from, to) == [net EXCEPT ![to].msg[from] = @+1]
pendingMsg(net, to, from) == net[to].msg[from] > 0
receiveMsg(net, to, from) == [net EXCEPT ![to].msg[from] = @-1]
sendAck(net, to) == [net EXCEPT ![to].ack = @+1]
pendingAck(net, to) == net[to].ack > 0
receiveAck(net, to) == [net EXCEPT ![to].ack = @-1]
VARIABLES active, parent, activeSons
vars == << terminationDetected, network, active, parent, activeSons >>
ProcSet == (Node)
Init == (* Global variables *)
/\ terminationDetected = FALSE
/\ network = [n \in Node |-> [msg |-> [snd \in Node |-> 0], ack |-> 0]]
(* Process node *)
/\ active = [self \in Node |-> (self = initiator)]
/\ parent = [self \in Node |-> IF self = initiator THEN self ELSE none]
/\ activeSons = [self \in Node |-> 0]
node(self) == \/ /\ active[self]
/\ \E dst \in Node \ {self}:
network' = sendMsg(network, self, dst)
/\ activeSons' = [activeSons EXCEPT ![self] = activeSons[self] + 1]
/\ UNCHANGED <<terminationDetected, active, parent>>
\/ /\ active[self]
/\ active' = [active EXCEPT ![self] = FALSE]
/\ UNCHANGED <<terminationDetected, network, parent, activeSons>>
\/ /\ \E snd \in Node:
/\ pendingMsg(network, self, snd)
/\ active' = [active EXCEPT ![self] = TRUE]
/\ LET net == receiveMsg(network, self, snd) IN
IF parent[self] = none
THEN /\ parent' = [parent EXCEPT ![self] = snd]
/\ network' = net
ELSE /\ network' = sendAck(net, snd)
/\ UNCHANGED parent
/\ UNCHANGED <<terminationDetected, activeSons>>
\/ /\ pendingAck(network, self)
/\ network' = receiveAck(network, self)
/\ activeSons' = [activeSons EXCEPT ![self] = activeSons[self] - 1]
/\ UNCHANGED <<terminationDetected, active, parent>>
\/ /\ (~active[self] /\ activeSons[self] = 0 /\ parent[self] # none)
/\ IF parent[self] = self
THEN /\ terminationDetected' = TRUE
/\ UNCHANGED network
ELSE /\ network' = sendAck(network, parent[self])
/\ UNCHANGED terminationDetected
/\ parent' = [parent EXCEPT ![self] = none]
/\ UNCHANGED <<active, activeSons>>
Next == (\E self \in Node: node(self))
Spec == /\ Init /\ [][Next]_vars
/\ \A self \in Node : WF_vars(node(self))
\* END TRANSLATION
StateConstraint ==
/\ \A n \in Node : network[n].ack <= 2
/\ \A m,n \in Node : network[m].msg[n] <= maxMsg
TypeOK ==
/\ terminationDetected \in BOOLEAN
/\ network \in [Node -> [msg : [Node -> Nat], ack : Nat]]
/\ active \in [Node -> BOOLEAN]
/\ parent \in [Node -> Node \union {none}]
/\ activeSons \in [Node -> Nat]
Quiescence ==
/\ \A n \in Node : ~active[n]
/\ \A n \in Node : network[n].ack = 0
/\ \A m,n \in Node : network[n].msg[m] = 0
(***************************************************************************)
(* The main safety property requires that termination is detected only *)
(* when the system is indeed quiescent. *)
(***************************************************************************)
TerminationDetection == terminationDetected => Quiescence
(***************************************************************************)
(* Conversely, liveness requires that if the system becomes quiescent *)
(* (which is not guaranteed) then termination is eventually detected. *)
(***************************************************************************)
Liveness == Quiescence ~> terminationDetected
=============================================================================
\* Modification History
\* Last modified Tue Feb 09 17:33:02 CET 2021 by merz
\* Created Tue Feb 09 11:32:36 CET 2021 by merz