-
Notifications
You must be signed in to change notification settings - Fork 0
/
SnapshotIsolation.tla
279 lines (246 loc) · 10.4 KB
/
SnapshotIsolation.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
------------------------- MODULE SnapshotIsolation -------------------------
EXTENDS FiniteSets, Naturals, Sequences, TLC, Graphs
CONSTANT Proc, Object, MAXTXOPS
(***************************************************************************)
(* Proc is a set of non-zero process IDs, i.e. TXIDs. Note that TXID #0 *)
(* is a hypothetical transaction that wrote all initial values in store. *)
(* Object represents a set of objects in database. MAXTXOPS is the max *)
(* number of read and write operations in a transaction. *)
(***************************************************************************)
ASSUME
/\ Proc \subseteq Nat \ {0}
/\ Proc \cap Object = {}
/\ MAXTXOPS \in Nat \ {0}
VARIABLE
transact,
history,
state,
store,
ts,
writeSet, (* writes of uncommitted transaction *)
startTS,
commitTS,
WRITE (* write lock *)
vars == <<
transact,
history,
state,
store,
ts,
writeSet,
startTS,
commitTS,
WRITE
>>
ANY(S) == CHOOSE s \in S : TRUE
(***************************************************************************)
(* Transaction is a set of all possible transactions *)
(***************************************************************************)
Transact ==
LET Op == [f : {"Read", "Write"}, obj : Object]
seq(S) == UNION {[1..n -> S] : n \in 1..MAXTXOPS}
IN {Append(op, [f |-> "Commit"]) : op \in seq(Op)}
State == {"Init", "Running", "Commit", "Abort"}
Value == [proc : {0} \cup Proc]
Version == [ver : Nat, val : Value]
InitValues == [obj \in Object |-> {[ver |-> 0, val |-> [proc |-> 0]]}]
Init ==
/\ \E tx \in [Proc -> Transact] : transact = tx
/\ history = <<>>
/\ state = [proc \in Proc |-> "Init"]
/\ store = InitValues
/\ ts = 0
/\ writeSet = [proc \in Proc |-> [obj \in Object |-> {}]]
/\ startTS = [proc \in Proc |-> 0]
/\ commitTS = [proc \in Proc |-> 0]
/\ WRITE = [obj \in Object |-> {}]
(***************************************************************************)
(* snapshotWrite records writes of running, uncommitted transactions *)
(***************************************************************************)
snapshotWrite(self, obj, val) ==
writeSet' = [writeSet EXCEPT ![self][obj] = {val}]
(***************************************************************************)
(* snapshotRead reads an object in writeSet if exists, then the latest *)
(* version in store among ones visible to the transaction *)
(***************************************************************************)
snapshotRead(self, obj, ver) ==
IF writeSet[self][obj] # {}
THEN ANY(writeSet[self][obj])
ELSE LET h == {i \in store[obj] : i.ver <= ver}
s == CHOOSE i \in h : \A j \in h: i.ver >= j.ver
IN s.val
(***************************************************************************)
(* snapshotCommit reflects writeSet into store *)
(***************************************************************************)
snapshotCommit(self, ver) ==
store' = [obj \in Object |->
IF writeSet[self][obj] # {}
THEN store[obj] \cup
{[ver |-> commitTS'[self],
val |-> ANY(writeSet[self][obj])]}
ELSE store[obj]]
recordHistoryValue(self, hd, tl, val) ==
/\ history' = Append(history, [proc |-> self, op |-> hd, val |-> val])
/\ transact' = [transact EXCEPT ![self] = tl]
recordHistory(self, hd, tl) ==
/\ history' = Append(history, [proc |-> self, op |-> hd])
/\ transact' = [transact EXCEPT ![self] = tl]
appendHistory(self, op) ==
/\ history' = Append(history, [proc |-> self, op |-> op])
Read(self, hd, tl) ==
/\ state[self] \in {"Init", "Running"}
/\ hd.f = "Read"
/\ IF state[self] = "Init"
THEN /\ startTS' = [startTS EXCEPT ![self] = ts]
/\ state' = [state EXCEPT ![self] = "Running"]
ELSE UNCHANGED <<state, startTS>>
/\ LET val == snapshotRead(self, hd.obj, startTS'[self])
IN recordHistoryValue(self, hd, tl, val)
/\ UNCHANGED <<store, ts, writeSet, commitTS, WRITE>>
Write(self, hd, tl) ==
/\ state[self] \in {"Init", "Running"}
/\ hd.f = "Write"
/\ WRITE[hd.obj] \in {{}, {self}}
/\ WRITE' = [WRITE EXCEPT ![hd.obj] = WRITE[hd.obj] \cup {self}]
/\ IF state[self] = "Init"
THEN startTS' = [startTS EXCEPT ![self] = ts]
ELSE UNCHANGED <<startTS>>
(* Detect Write-Write conflict *)
/\ IF {i \in store[hd.obj] : i.ver > startTS'[self]} # {}
THEN state' = [state EXCEPT ![self] = "Abort"]
ELSE IF state[self] = "Init"
THEN state' = [state EXCEPT ![self] = "Running"]
ELSE UNCHANGED <<state>>
/\ IF state'[self] = "Running"
THEN LET val == [proc |-> self]
IN /\ snapshotWrite(self, hd.obj, val)
/\ recordHistoryValue(self, hd, tl, val)
ELSE UNCHANGED <<transact, history, writeSet>>
/\ UNCHANGED <<store, ts, commitTS>>
Commit(self, hd, tl) ==
/\ state[self] \in {"Init", "Running"}
/\ hd.f = "Commit"
/\ WRITE' = [obj \in Object |-> WRITE[obj] \ {self}]
/\ ts' = ts + 1
/\ commitTS' = [commitTS EXCEPT ![self] = ts']
/\ state' = [state EXCEPT ![self] = "Commit"]
/\ snapshotCommit(self, commitTS'[self])
/\ recordHistory(self, hd, tl)
/\ UNCHANGED <<writeSet, startTS>>
Abort(victim, reason) ==
/\ state[victim] \in {"Init", "Running"}
/\ WRITE' = [obj \in Object |-> WRITE[obj] \ {victim}]
/\ state' = [state EXCEPT ![victim] = "Abort"]
/\ appendHistory(victim, [f |-> "Abort", reason|-> reason])
/\ UNCHANGED <<transact, store, ts, writeSet, startTS, commitTS>>
TypeOK ==
/\ \A idx \in DOMAIN history
: LET e == history[idx]
IN /\ e.proc \in Proc
/\ \/ /\ e.op.f \in {"Read", "Write"}
/\ e.op.obj \in Object
/\ e.val \in Value
\/ e.op.f \in {"Commit", "Abort"}
/\ \A proc \in Proc
: state[proc] \in State
/\ \A obj \in Object
: \A t \in store[obj] : t \in Version
/\ \A proc \in Proc
: \A obj \in Object
: Cardinality(writeSet[proc][obj]) \in {0, 1}
LockOK ==
/\ \A obj \in Object
: Cardinality(WRITE[obj]) \in {0,1}
Invariants ==
/\ TypeOK
/\ LockOK
(***************************************************************************)
(* ViewSerializable asserts that, if some of transactions successfully *)
(* commit, the history of the committed transactions is view-serializable. *)
(***************************************************************************)
RECURSIVE viewEq(_, _)
viewEq(s, hist) ==
IF hist = <<>>
THEN (********************************************************************)
(* transaction updates store on commit, so store has only writes *)
(* successfully committed. *)
(********************************************************************)
\A obj \in Object
: \E i \in store[obj]
: \A j \in store[obj]
: i.ver >= j.ver /\ s[obj].proc = i.val.proc
ELSE LET hd == Head(hist)
IN CASE hd.op.f = "Read"
-> s[hd.op.obj] = hd.val /\ viewEq(s, Tail(hist))
[] hd.op.f = "Write"
-> viewEq([s EXCEPT ![hd.op.obj] = hd.val], Tail(hist))
[] OTHER
-> viewEq(s, Tail(hist))
RECURSIVE concat(_, _, _, _)
concat(f, n, size, acc) ==
IF n > size THEN acc ELSE concat(f, n+1, size, acc \o f[n])
ViewSerializable ==
LET commit == SelectSeq(history, LAMBDA x: x.op.f = "Commit")
Tx == {SelectSeq(history, LAMBDA x: x.proc = proc)
: proc \in {commit[i].proc : i \in 1..Len(commit)}}
perms == {f \in [1..Cardinality(Tx) -> Tx]
: \A tx \in Tx
: \E i \in 1..Cardinality(Tx) : f[i] = tx}
IN \E perm \in perms
: LET serialHistory == concat(perm, 1, Cardinality(Tx), <<>>)
IN viewEq([obj \in Object |-> [proc |-> 0]], serialHistory)
\* /\ \/ Cardinality(Tx) < 2
\* \/ PrintT(<<history, serialHistory>>)
(***************************************************************************)
(* WaitingFor builds a set of edges of dependency graph in which a cycle *)
(* occurs when deadlock has happened. *)
(***************************************************************************)
WaitingFor[proc \in Proc] ==
IF state[proc] # "Running"
THEN {}
ELSE LET from == proc
hd == Head(transact[from])
edges(TO) == {<<from, to>> : to \in TO}
IN CASE hd.f = "Write"
-> edges(WRITE[hd.obj] \ {from})
[] OTHER -> {}
(***************************************************************************)
(* Resolve aborts a transaction involved in deadlock and try to resolve. *)
(***************************************************************************)
Resolve(self) ==
LET edge == UNION {WaitingFor[proc] : proc \in Proc}
node == UNION {{from, to} : <<from, to>> \in edge}
dependency == [edge |-> edge, node |-> node]
IN \E victim \in {t \in node : IsInCycle(t, dependency)}
: Abort(victim, "deadlock")
Next ==
\/ \E self \in Proc
: \/ /\ transact[self] # <<>>
/\ \/ LET hd == Head(transact[self])
tl == Tail(transact[self])
IN \/ Read(self, hd, tl)
\/ Write(self, hd, tl)
\/ Commit(self, hd, tl)
\/ Resolve(self)
\/ Abort(self, "voluntary")
\/ /\ \A proc \in Proc : state[proc] \in {"Commit", "Abort"}
/\ UNCHANGED vars
Spec == Init /\ [][Next]_vars /\ WF_vars(Next)
(***************************************************************************)
(* A temporal property asserts that all transactions eventually commit or *)
(* abort without deadlock. *)
(***************************************************************************)
EventuallyAllCommitOrAbort ==
<>[](\A proc \in Proc : state[proc] \in {"Commit", "Abort"})
(***************************************************************************)
(* A temporal property asserts that some transactions eventually commit. *)
(***************************************************************************)
EventuallySomeCommit ==
<>[](\E proc \in Proc : state[proc] = "Commit")
Properties ==
/\ EventuallyAllCommitOrAbort
THEOREM Spec => []Invariants /\ Properties
=============================================================================
\* Modification History
\* Last modified Sat Mar 17 15:37:46 JST 2018 by takayuki
\* Created Mon Feb 12 21:27:20 JST 2018 by takayuki