/
exactly_once_none_atomic.tla
304 lines (252 loc) · 12.5 KB
/
exactly_once_none_atomic.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
---- MODULE exactly_once_none_atomic ----
EXTENDS TLC, Integers, Sequences, FiniteSets
CONSTANTS MessageCount, DupCount, NULL
Range(T) == { T[x] : x \in DOMAIN T }
IsEmpty(T) == Cardinality(T) = 0
WithId(T, id) == { x \in T: x.msgId = id }
Processes == 1..2
MessageIds == 1..MessageCount
DupIds == 1..DupCount
VersionIds == 0..2*MessageCount
TxIdx == 0..MessageCount*DupCount
(*--algorithm outbox
variables
inputQueue = [id : MessageIds, dupId : DupIds],
outQueue = {},
store = <<[ver |-> 0, msgId |-> NULL, txId |-> NULL]>>,
outbox = [id \in MessageIds \union TxIdx |-> NULL],
processed = { },
gVer = 0,
lVer = [pid \in Process |-> 0]
define
InputMessages == [id : MessageIds, dupId : DupIds]
OutQueueMessages == [msgId : MessageIds, ver : VersionIds]
TypeInvariant ==
/\ inputQueue \in SUBSET InputMessages
/\ outQueue \in SUBSET outQueueMessages
/\ processed \in SUBSET InputMessages
/\ Range(store) \in SUBSET [ver : VersionIds, msgId : MessageIds \union {NULL}, txId : TxIdx \union {NULL} ]
/\ Range(outbox) \in SUBSET (OutQueueMessages \union {NULL})
AtMostOneStateChange ==
\A id \in MessageIds : Cardinality(WithId(Range(store),id)) <= 2
AtMostOneoutQueueMsg ==
\A id \in MessageIds : Cardinality(WithId(outQueue, id)) <= 1
ConsistentStateAndoutQueue ==
LET InState(id) == CHOOSE x \in WithId(Range(store), id) : TRUE
InoutQueue(id) == CHOOSE x \in WithId(outQueue, id) : TRUE
IN \A m \in processed: InState(m.id).ver = InoutQueue(m.id).ver
Safety == AtMostOneStateChange /\ AtMostOneoutQueueMsg /\ ConsistentStateAndoutQueue
Termination == <>(/\ \A self \in Processes: pc[self] = "LockInMsg"
/\ IsEmpty(inputQueue))
end define;
macro Fail() begin
goto MainLoop;
end macro;
macro CommitOutbox(txId) begin
outbox[outbox[txId].msgId] := outbox[txId];
end macro;
macro CommitState(ver, msgId, txId) begin
with latest = Head(store) do
if latest.ver = ver then
either
store := <<[ver |-> latest.ver + 1, msgId |-> msgId, txId |-> txId]>> \o store ;
or
Fail();
end either;
else
Fail();
end if;
end with;
end macro;
macro CleanupState(ver) begin
with latest = Head(store) do
if latest.ver = ver then
either
store := <<[ver |-> latest.ver + 1, msgId |-> latest.msgId, txId |-> NULL]>> \o store;
or
Fail();
end either;
else
Fail();
end if;
end with;
end macro;
fair process HandlerThread \in Processes
variables
msg,
txId,
stateVer
begin
MainLoop:
while TRUE do
LockInMsg:
await ~ IsEmpty(inputQueue);
with m \in inputQueue do
inputQueue := { i \in inputQueue : i /= m};
msg := m;
end with;
LoadState:
with s = Head(store) do
txId := s.txId;
stateVer := s.ver;
end with;
if(txId /= NULL) then
Redo_OutboxCommit: CommitOutbox(txId);
Redo_StateCleanup: CleanupState(stateVer);
end if;
Process:
if outbox[msg.id] = NULL then
txId := (msg.id-1)*DupCount + msg.dupId;
StageOutbox: outbox[txId] := [msgId |-> msg.id, ver |-> stateVer];
StateCommit: CommitState(stateVer, msg.id, txId);
OutboxCommit: CommitOutbox(txId);
StateCleanup: CleanupState(stateVer);
end if;
SendAndAck:
outQueue := outQueue \union {outbox[msg.id]};
processed := processed \union {msg};
end while;
end process;
end algorithm; *)
\* BEGIN TRANSLATION - the hash of the PCal code: PCal-b66170bb80c17cb19a196c70f5a37e21
CONSTANT defaultInitValue
VARIABLES inputQueue, outQueue, store, outbox, outboxStagging, processed, pc
(* define statement *)
InputMessages == [id : MessageIds, dupId : DupIds]
outQueueMessages == [msgId : MessageIds, ver : VersionIds]
TypeInvariant ==
/\ inputQueue \in SUBSET InputMessages
/\ outQueue \in SUBSET outQueueMessages
/\ processed \in SUBSET InputMessages
/\ Range(store) \in SUBSET [ver : VersionIds, msgId : MessageIds \union {NULL}, txId : TxIdx \union {NULL} ]
/\ Range(outbox) \in SUBSET (outQueueMessages \union {NULL})
/\ Range(outboxStagging) \in SUBSET (outQueueMessages \union {NULL})
AtMostOneStateChange ==
\A id \in MessageIds : Cardinality(WithId(Range(store),id)) <= 2
AtMostOneoutQueueMsg ==
\A id \in MessageIds : Cardinality(WithId(outQueue, id)) <= 1
ConsistentStateAndoutQueue ==
LET InState(id) == CHOOSE x \in WithId(Range(store), id) : TRUE
InoutQueue(id) == CHOOSE x \in WithId(outQueue, id) : TRUE
IN \A m \in processed: InState(m.id).ver = InoutQueue(m.id).ver
Safety == AtMostOneStateChange /\ AtMostOneoutQueueMsg /\ ConsistentStateAndoutQueue
Termination == <>(/\ \A self \in Processes: pc[self] = "LockInMsg"
/\ IsEmpty(inputQueue))
VARIABLES msg, txId, stateVer
vars == << inputQueue, outQueue, store, outbox, outboxStagging, processed, pc,
msg, txId, stateVer >>
ProcSet == (Processes)
Init == (* Global variables *)
/\ inputQueue = [id : MessageIds, dupId : DupIds]
/\ outQueue = {}
/\ store = <<[ver |-> 0, msgId |-> NULL, txId |-> NULL]>>
/\ outbox = [r \in MessageIds |-> NULL]
/\ outboxStagging = [t \in TxIdx |-> NULL]
/\ processed = { }
(* Process HandlerThread *)
/\ msg = [self \in Processes |-> defaultInitValue]
/\ txId = [self \in Processes |-> defaultInitValue]
/\ stateVer = [self \in Processes |-> defaultInitValue]
/\ pc = [self \in ProcSet |-> "MainLoop"]
MainLoop(self) == /\ pc[self] = "MainLoop"
/\ pc' = [pc EXCEPT ![self] = "LockInMsg"]
/\ UNCHANGED << inputQueue, outQueue, store, outbox,
outboxStagging, processed, msg, txId,
stateVer >>
LockInMsg(self) == /\ pc[self] = "LockInMsg"
/\ ~ IsEmpty(inputQueue)
/\ \E m \in inputQueue:
/\ inputQueue' = { i \in inputQueue : i /= m}
/\ msg' = [msg EXCEPT ![self] = m]
/\ pc' = [pc EXCEPT ![self] = "LoadState"]
/\ UNCHANGED << outQueue, store, outbox, outboxStagging,
processed, txId, stateVer >>
LoadState(self) == /\ pc[self] = "LoadState"
/\ LET s == Head(store) IN
/\ txId' = [txId EXCEPT ![self] = s.txId]
/\ stateVer' = [stateVer EXCEPT ![self] = s.ver]
/\ IF (txId'[self] /= NULL)
THEN /\ pc' = [pc EXCEPT ![self] = "Redo_OutboxCommit"]
ELSE /\ pc' = [pc EXCEPT ![self] = "Process"]
/\ UNCHANGED << inputQueue, outQueue, store, outbox,
outboxStagging, processed, msg >>
Redo_OutboxCommit(self) == /\ pc[self] = "Redo_OutboxCommit"
/\ LET msgId == outboxStagging[txId[self]].msgId IN
outbox' = [outbox EXCEPT ![msgId] = outboxStagging[txId[self]]]
/\ pc' = [pc EXCEPT ![self] = "Redo_StateCleanup"]
/\ UNCHANGED << inputQueue, outQueue, store,
outboxStagging, processed, msg,
txId, stateVer >>
Redo_StateCleanup(self) == /\ pc[self] = "Redo_StateCleanup"
/\ LET latest == Head(store) IN
IF latest.ver = stateVer[self]
THEN /\ \/ /\ store' = <<[ver |-> latest.ver + 1, msgId |-> latest.msgId, txId |-> NULL]>> \o store
/\ pc' = [pc EXCEPT ![self] = "Process"]
\/ /\ pc' = [pc EXCEPT ![self] = "MainLoop"]
/\ store' = store
ELSE /\ pc' = [pc EXCEPT ![self] = "MainLoop"]
/\ store' = store
/\ UNCHANGED << inputQueue, outQueue, outbox,
outboxStagging, processed, msg,
txId, stateVer >>
Process(self) == /\ pc[self] = "Process"
/\ IF outbox[msg[self].id] = NULL
THEN /\ txId' = [txId EXCEPT ![self] = (msg[self].id-1)*DupCount + msg[self].dupId]
/\ pc' = [pc EXCEPT ![self] = "StageOutbox"]
ELSE /\ pc' = [pc EXCEPT ![self] = "SendAndAck"]
/\ txId' = txId
/\ UNCHANGED << inputQueue, outQueue, store, outbox,
outboxStagging, processed, msg, stateVer >>
StageOutbox(self) == /\ pc[self] = "StageOutbox"
/\ outboxStagging' = [outboxStagging EXCEPT ![txId[self]] = [msgId |-> msg[self].id, ver |-> stateVer[self]]]
/\ pc' = [pc EXCEPT ![self] = "StateCommit"]
/\ UNCHANGED << inputQueue, outQueue, store, outbox,
processed, msg, txId, stateVer >>
StateCommit(self) == /\ pc[self] = "StateCommit"
/\ LET latest == Head(store) IN
IF latest.ver = stateVer[self]
THEN /\ \/ /\ store' = <<[ver |-> latest.ver + 1, msgId |-> (msg[self].id), txId |-> txId[self]]>> \o store
/\ pc' = [pc EXCEPT ![self] = "OutboxCommit"]
\/ /\ pc' = [pc EXCEPT ![self] = "MainLoop"]
/\ store' = store
ELSE /\ pc' = [pc EXCEPT ![self] = "MainLoop"]
/\ store' = store
/\ UNCHANGED << inputQueue, outQueue, outbox,
outboxStagging, processed, msg, txId,
stateVer >>
OutboxCommit(self) == /\ pc[self] = "OutboxCommit"
/\ LET msgId == outboxStagging[txId[self]].msgId IN
outbox' = [outbox EXCEPT ![msgId] = outboxStagging[txId[self]]]
/\ pc' = [pc EXCEPT ![self] = "StateCleanup"]
/\ UNCHANGED << inputQueue, outQueue, store,
outboxStagging, processed, msg, txId,
stateVer >>
StateCleanup(self) == /\ pc[self] = "StateCleanup"
/\ LET latest == Head(store) IN
IF latest.ver = stateVer[self]
THEN /\ \/ /\ store' = <<[ver |-> latest.ver + 1, msgId |-> latest.msgId, txId |-> NULL]>> \o store
/\ pc' = [pc EXCEPT ![self] = "SendAndAck"]
\/ /\ pc' = [pc EXCEPT ![self] = "MainLoop"]
/\ store' = store
ELSE /\ pc' = [pc EXCEPT ![self] = "MainLoop"]
/\ store' = store
/\ UNCHANGED << inputQueue, outQueue, outbox,
outboxStagging, processed, msg, txId,
stateVer >>
SendAndAck(self) == /\ pc[self] = "SendAndAck"
/\ outQueue' = (outQueue \union {outbox[msg[self].id]})
/\ processed' = (processed \union {msg[self]})
/\ pc' = [pc EXCEPT ![self] = "MainLoop"]
/\ UNCHANGED << inputQueue, store, outbox, outboxStagging,
msg, txId, stateVer >>
HandlerThread(self) == MainLoop(self) \/ LockInMsg(self) \/ LoadState(self)
\/ Redo_OutboxCommit(self)
\/ Redo_StateCleanup(self) \/ Process(self)
\/ StageOutbox(self) \/ StateCommit(self)
\/ OutboxCommit(self) \/ StateCleanup(self)
\/ SendAndAck(self)
Next == (\E self \in Processes: HandlerThread(self))
Spec == /\ Init /\ [][Next]_vars
/\ \A self \in Processes : WF_vars(HandlerThread(self))
\* END TRANSLATION - the hash of the generated TLA code (remove to silence divergence warnings): TLA-acf7cb8a0a7a1b5e07100b01a6a2338f
====