-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathcosmos_client.tla
384 lines (309 loc) · 16.4 KB
/
cosmos_client.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
--------------------------- MODULE cosmos_client ----------------------------
(***************************************************************************)
(* Microsoft Azure Cosmos DB TLA+ specification for the five consistency *)
(* levels the service offers. The spec focuses on the consistency *)
(* guarantees Cosmos DB provides to the clients, without the details of *)
(* the protocol implementation. *)
(***************************************************************************)
EXTENDS Naturals, Integers, Reals, Sequences, FiniteSets, TLC
(***************************************************************************)
(* Number of regions *)
(***************************************************************************)
CONSTANT NumRegions
CONSTANT NumWriteRegions
ASSUME NumRegions \in Nat
ASSUME NumWriteRegions >= 1 /\ NumWriteRegions <= NumRegions
(***************************************************************************)
(* Number of clients per region for modeling *)
(***************************************************************************)
CONSTANT NumClientsPerRegion
ASSUME NumClientsPerRegion \in Nat
(***************************************************************************)
(* MaxNumOp max number of operations from client *)
(***************************************************************************)
CONSTANT MaxNumOp
(***************************************************************************)
(* Consistency level *)
(* (1) strong (Linearizability) *)
(* (2) bounded (Bounded Staleness) *)
(* (3) session *)
(* (4) prefix (Consistent Prefix) *)
(* (5) eventual *)
(***************************************************************************)
CONSTANT Consistency
ASSUME Consistency \in {"strong", "bounded_staleness", "session", "consistent_prefix", "eventual"}
(* The bounded version differences in Bounded Staleness consistency *)
CONSTANT K
ASSUME K \in Nat
(* All regions in topology *)
Regions == 1..NumRegions
(* All writable regions in topology *)
WriteRegions == 1..NumWriteRegions
(* All clients with local region *)
Clients == {<<r, j>> : r \in Regions, j \in 1..NumClientsPerRegion}
(***************************************************************************)
(* All possible operations in history *)
(***************************************************************************)
Operations == [type: {"write"}, data: Nat, region: WriteRegions, client: Clients]
\union [type: {"read"}, data: Nat, region: Regions, client: Clients]
(*
--algorithm cosmos_client
{
variables (* Max staleness. Strong is a special case of bounded with K = 1 *)
Bound = CASE Consistency = "strong" -> 1
[] Consistency = "bounded_staleness" -> K
[] Consistency = "session" -> MaxNumOp
[] Consistency = "consistent_prefix" -> MaxNumOp
[] Consistency = "eventual" -> MaxNumOp;
(* Client operation history *)
History = <<>>;
(* Latest data value in each region *)
Data = [r \in Regions |-> 0];
(* Tentative log in each region *)
Database = [r \in Regions |-> <<>>];
(* Value used by clients *)
value = 0;
define
{
\* Removing duplicates from a sequence:
RECURSIVE RemDupRec(_,_)
RemDupRec(es, seen) == IF es = <<>> THEN <<>>
ELSE IF es[1] \in seen THEN RemDupRec(Tail(es), seen)
ELSE <<es[1]>> \o RemDupRec(Tail(es), seen \cup {es[1]})
RemoveDuplicates(es) == RemDupRec(es, {})
SetMax(S) == IF S = {} THEN -1
ELSE CHOOSE i \in S : \A j \in S : i >= j
SeqToSet(s) == {s[i] : i \in DOMAIN s}
Last(s) == s[Len(s)]
MaxLen(c) == LET region == CHOOSE i \in Regions : \A j \in Regions : Len(c[i]) >= Len(c[j])
IN Len(c[region])
MinLen(c) == LET region == CHOOSE i \in Regions : \A j \in Regions : Len(c[i]) <= Len(c[j])
IN Len(c[region])
}
(* -------------------------------------------------------------- *)
(* --------------------- CLIENT ACTIONS ------------------------- *)
(* -------------------------------------------------------------- *)
(* Regular write at local region *)
macro write(v)
{
if (self[1] \in WriteRegions)
{
when \A i, j \in Regions : Data[i] - Data[j] < Bound;
Database[self[1]] := Append(@, v);
Data[self[1]] := v;
History := Append(History, [type |-> "write",
data |-> v,
region |-> self[1],
client |-> self]);
session_token := v;
}
}
(* Reads with consistency checks *)
macro read()
{
(* We check session token for session consistency *)
when Consistency /= "session" \/ Data[self[1]] >= session_token;
(* We check global value for strong consistency *)
when Consistency /= "strong" \/ \A i, j \in Regions : Data[i] = Data[j];
History := Append(History, [type |-> "read",
data |-> Data[self[1]],
region |-> self[1],
client |-> self]);
session_token := Data[self[1]];
}
(* -------------------------------------------------------------- *)
(* --------------------- REGION ACTIONS ------------------------- *)
(* -------------------------------------------------------------- *)
(* Asynchronously replicates from source region to destination region and merges data history *)
macro replicate()
{
with (s \in WriteRegions; d \in Regions)
{
Database[d] := RemoveDuplicates(SortSeq(Database[d] \o Database[s], <));
if (Len(Database[d]) > 0)
{
Data[d] := Last(Database[d]);
}
}
}
(* -------------------------------------------------------------- *)
(* -------------------- CLIENT PROCESSES ------------------------ *)
(* -------------------------------------------------------------- *)
fair process (client \in Clients)
variable session_token = 0;
numOp = 0;
{
client_actions:
while(numOp < MaxNumOp)
{
numOp := numOp + 1;
either
{
write:
value := value + 1;
write(value);
}
or read: read();
}
}
(* -------------------------------------------------------------- *)
(* -------------------- SERVER PROCESSES ------------------------ *)
(* -------------------------------------------------------------- *)
fair process (CosmosDB = <<0, 0>>)
{
database_action:
while(TRUE)
{
replicate();
}
}
}
*)
\* BEGIN TRANSLATION
VARIABLES Bound, History, Data, Database, value, pc
(* define statement *)
RECURSIVE RemDupRec(_,_)
RemDupRec(es, seen) == IF es = <<>> THEN <<>>
ELSE IF es[1] \in seen THEN RemDupRec(Tail(es), seen)
ELSE <<es[1]>> \o RemDupRec(Tail(es), seen \cup {es[1]})
RemoveDuplicates(es) == RemDupRec(es, {})
SetMax(S) == IF S = {} THEN -1
ELSE CHOOSE i \in S : \A j \in S : i >= j
SeqToSet(s) == {s[i] : i \in DOMAIN s}
Last(s) == s[Len(s)]
MaxLen(c) == LET region == CHOOSE i \in Regions : \A j \in Regions : Len(c[i]) >= Len(c[j])
IN Len(c[region])
MinLen(c) == LET region == CHOOSE i \in Regions : \A j \in Regions : Len(c[i]) <= Len(c[j])
IN Len(c[region])
VARIABLES session_token, numOp
vars == << Bound, History, Data, Database, value, pc, session_token, numOp >>
ProcSet == (Clients) \cup {<<0, 0>>}
Init == (* Global variables *)
/\ Bound = (CASE Consistency = "strong" -> 1
[] Consistency = "bounded_staleness" -> K
[] Consistency = "session" -> MaxNumOp
[] Consistency = "consistent_prefix" -> MaxNumOp
[] Consistency = "eventual" -> MaxNumOp)
/\ History = <<>>
/\ Data = [r \in Regions |-> 0]
/\ Database = [r \in Regions |-> <<>>]
/\ value = 0
(* Process client *)
/\ session_token = [self \in Clients |-> 0]
/\ numOp = [self \in Clients |-> 0]
/\ pc = [self \in ProcSet |-> CASE self \in Clients -> "client_actions"
[] self = <<0, 0>> -> "database_action"]
client_actions(self) == /\ pc[self] = "client_actions"
/\ IF numOp[self] < MaxNumOp
THEN /\ numOp' = [numOp EXCEPT ![self] = numOp[self] + 1]
/\ \/ /\ pc' = [pc EXCEPT ![self] = "write"]
\/ /\ pc' = [pc EXCEPT ![self] = "read"]
ELSE /\ pc' = [pc EXCEPT ![self] = "Done"]
/\ numOp' = numOp
/\ UNCHANGED << Bound, History, Data, Database, value,
session_token >>
write(self) == /\ pc[self] = "write"
/\ value' = value + 1
/\ IF self[1] \in WriteRegions
THEN /\ \A i, j \in Regions : Data[i] - Data[j] < Bound
/\ Database' = [Database EXCEPT ![self[1]] = Append(@, value')]
/\ Data' = [Data EXCEPT ![self[1]] = value']
/\ History' = Append(History, [type |-> "write",
data |-> value',
region |-> self[1],
client |-> self])
/\ session_token' = [session_token EXCEPT ![self] = value']
ELSE /\ TRUE
/\ UNCHANGED << History, Data, Database,
session_token >>
/\ pc' = [pc EXCEPT ![self] = "client_actions"]
/\ UNCHANGED << Bound, numOp >>
read(self) == /\ pc[self] = "read"
/\ Consistency /= "session" \/ Data[self[1]] >= session_token[self]
/\ Consistency /= "strong" \/ \A i, j \in Regions : Data[i] = Data[j]
/\ History' = Append(History, [type |-> "read",
data |-> Data[self[1]],
region |-> self[1],
client |-> self])
/\ session_token' = [session_token EXCEPT ![self] = Data[self[1]]]
/\ pc' = [pc EXCEPT ![self] = "client_actions"]
/\ UNCHANGED << Bound, Data, Database, value, numOp >>
client(self) == client_actions(self) \/ write(self) \/ read(self)
database_action == /\ pc[<<0, 0>>] = "database_action"
/\ \E s \in WriteRegions:
\E d \in Regions:
/\ Database' = [Database EXCEPT ![d] = RemoveDuplicates(SortSeq(Database[d] \o Database[s], <))]
/\ IF Len(Database'[d]) > 0
THEN /\ Data' = [Data EXCEPT ![d] = Last(Database'[d])]
ELSE /\ TRUE
/\ Data' = Data
/\ pc' = [pc EXCEPT ![<<0, 0>>] = "database_action"]
/\ UNCHANGED << Bound, History, value, session_token, numOp >>
CosmosDB == database_action
Next == CosmosDB
\/ (\E self \in Clients: client(self))
Spec == /\ Init /\ [][Next]_vars
/\ \A self \in Clients : WF_vars(client(self))
/\ WF_vars(CosmosDB)
\* END TRANSLATION
-----------------------------------------------------------------------------
(* enable these invariants in model checker *)
(* Check elements in History are type of Opertion *)
TypeOK == {History[i] : i \in DOMAIN History} \subseteq Operations
(* Read value in any regional database history *)
AnyReadPerRegion(r) == \A i \in DOMAIN History : /\ History[i].type = "read"
/\ History[i].region = r
=> History[i].data \in SeqToSet(Database[r]) \union {0}
(* Operation in history h is monitonic *)
Monotonic(h) == \A i, j \in DOMAIN h : i <= j => h[i].data <= h[j].data
(* Reads in region r are monotonic *)
MonotonicReadPerRegion(r) == LET reads == [i \in {j \in DOMAIN History : /\ History[j].type = "read"
/\ History[j].region = r}
|-> History[i]]
IN Monotonic(reads)
(* Reads from client c are monotonic *)
MonotonicReadPerClient(c) == LET reads == [i \in {j \in DOMAIN History : /\ History[j].type = "read"
/\ History[j].client = c}
|-> History[i]]
IN Monotonic(reads)
MonotonicWritePerRegion(r) == LET writes == [i \in {j \in DOMAIN History : /\ History[j].type = "write"
/\ History[j].region = r}
|-> History[i]]
IN Monotonic(writes)
(* Clients read their own writes *)
ReadYourWrite == \A i, j \in DOMAIN History : /\ i < j
/\ History[i].type = "write"
/\ History[j].type = "read"
/\ History[i].client = History[j].client
=> History[j].data >= History[i].data
(* Read the latest writes *)
ReadAfterWrite == \A i, j \in DOMAIN History : /\ i < j
/\ History[i].type = "write"
/\ History[j].type = "read"
=> History[j].data >= History[i].data
Linearizability == \A i, j \in DOMAIN History : /\ i < j
=> History[j].data >= History[i].data
LastOperation(c) == LET i == SetMax({j \in DOMAIN History : History[j].client = c})
IN IF i > 0 THEN History[i] ELSE <<>>
BoundedStaleness == /\ \A i, j \in Regions : Data[i] - Data[j] <= K
/\ \A r \in Regions : MonotonicReadPerRegion(r)
/\ ReadYourWrite
ConsistentPrefix == \A r \in Regions : /\ MonotonicWritePerRegion(r)
/\ AnyReadPerRegion(r)
Strong == /\ Linearizability
/\ Monotonic(History)
/\ ReadAfterWrite
Session == /\ \A c \in Clients : MonotonicReadPerClient(c)
/\ ReadYourWrite
Eventual == \A i \in DOMAIN History :
LET r == History[i].region
IN History[i].data \in {Database[r][j] : j \in DOMAIN Database[r]} \union {0}
Invariant == /\ TypeOK
/\ CASE Consistency = "strong" -> Strong
[] Consistency = "bounded_staleness" -> BoundedStaleness
[] Consistency = "session" -> Session
[] Consistency = "consistent_prefix" -> ConsistentPrefix
[] Consistency = "eventual" -> Eventual
Liveness == <>[] (\A i, j \in Regions : Database[i] = Database[j])
=============================================================================
\* Authored by Cosmos DB