-
Notifications
You must be signed in to change notification settings - Fork 0
/
TwoPhaseLocking.tla
268 lines (234 loc) · 10.2 KB
/
TwoPhaseLocking.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
-------------------------- MODULE TwoPhaseLocking --------------------------
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,
writeSet,
READ, (* read lock *)
WRITE (* write lock *)
vars == <<
transact,
history,
state,
store,
writeSet,
READ,
WRITE
>>
(***************************************************************************)
(* Transaction is a set of all possible transactions *)
(***************************************************************************)
Transaction ==
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]
InitValues == [obj \in Object |-> [proc |-> 0]]
Init ==
/\ \E tx \in [Proc -> Transaction] : transact = tx
/\ history = <<>>
/\ state = [proc \in Proc |-> "Init"]
/\ store = InitValues
/\ writeSet = [proc \in Proc |-> [obj \in Object |-> {}]]
/\ READ = [obj \in Object |-> {}]
/\ WRITE = [obj \in Object |-> {}]
bufferWrite(self, obj, val) ==
writeSet' = [writeSet EXCEPT ![self][obj] = {val}]
ANY(S) == CHOOSE s \in S : TRUE
bufferRead(self, obj) ==
IF writeSet[self][obj] # {} THEN ANY(writeSet[self][obj]) ELSE store[obj]
bufferFlush(self) ==
store' = [obj \in Object |-> bufferRead(self, 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])
ReadLongDurationLock(self, hd, tl) ==
/\ state[self] \in {"Init", "Running"}
/\ hd.f = "Read"
/\ WRITE[hd.obj] \in {{}, {self}}
/\ READ' = [READ EXCEPT ![hd.obj] = READ[hd.obj] \cup {self}]
/\ recordHistoryValue(self, hd, tl, bufferRead(self, hd.obj))
/\ IF state[self] = "Init"
THEN /\ state' = [state EXCEPT ![self] = "Running"]
/\ UNCHANGED <<store, writeSet, WRITE>>
ELSE UNCHANGED <<state, store, writeSet, WRITE>>
ReadShortDurationLock(self, hd, tl) ==
/\ state[self] \in {"Init", "Running"}
/\ hd.f = "Read"
/\ WRITE[hd.obj] \in {{}, {self}}
/\ recordHistoryValue(self, hd, tl, bufferRead(self, hd.obj))
/\ IF state[self] = "Init"
THEN /\ state' = [state EXCEPT ![self] = "Running"]
/\ UNCHANGED <<store, writeSet, READ, WRITE>>
ELSE UNCHANGED <<state, store, writeSet, READ, WRITE>>
Read(self, hd, tl) == ReadLongDurationLock(self, hd, tl)
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}]
/\ READ[hd.obj] \in SUBSET WRITE'[hd.obj]
/\ bufferWrite(self, hd.obj, [proc |-> self])
/\ recordHistoryValue(self, hd, tl, [proc |-> self])
/\ IF state[self] = "Init"
THEN /\ state' = [state EXCEPT ![self] = "Running"]
/\ UNCHANGED <<store, READ>>
ELSE UNCHANGED <<state, store, READ>>
Commit(self, hd, tl) ==
/\ state[self] \in {"Init", "Running"}
/\ hd.f = "Commit"
/\ recordHistory(self, hd, tl)
/\ bufferFlush(self)
/\ state' = [state EXCEPT ![self] = "Commit"]
/\ READ' = [obj \in Object |-> READ[obj] \ {self}]
/\ WRITE' = [obj \in Object |-> WRITE[obj] \ {self}]
/\ UNCHANGED <<writeSet>>
Abort(victim, reason) ==
/\ state[victim] \in {"Init", "Running"}
/\ appendHistory(victim, [f |-> "Abort", reason|-> reason])
/\ state' = [state EXCEPT ![victim] = "Abort"]
/\ READ' = [obj \in Object |-> READ[obj] \ {victim}]
/\ WRITE' = [obj \in Object |-> WRITE[obj] \ {victim}]
/\ UNCHANGED <<transact, store, writeSet>>
(***************************************************************************)
(* 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 : s[obj].proc = store[obj].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
: viewEq(InitValues, concat(perm, 1, Cardinality(Tx), <<>>))
\* /\ \/ Cardinality(Tx) < 2
\* \/ PrintT(<<history, concat(perm, 1, Cardinality(Tx), <<>>)>>)
(***************************************************************************)
(* ViewSerializableInCommitOrder is stronger than ViewSerializable in that *)
(* serialization order is restricted to commit order. *)
(***************************************************************************)
ViewSerializableInCommitOrder ==
LET commit == SelectSeq(history, LAMBDA x: x.op.f = "Commit")
tx == [i \in 1..Len(commit)
|-> SelectSeq(history, LAMBDA x: x.proc = commit[i].proc)]
serialHistory == concat(tx, 1, Len(tx), <<>>)
IN viewEq(InitValues, serialHistory)
\* /\ \/ Len(tx) < 2
\* \/ PrintT(<<history, serialHistory>>)
(***************************************************************************)
(* Invariants are a set of state predicates to assert that all states and *)
(* locks are consistent, and if some of transactions successfully commit, *)
(* the history of the committed transactions is serializable. *)
(***************************************************************************)
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
: store[obj] \in Value
/\ \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}
/\ \A obj \in Object
: Cardinality(WRITE[obj]) # 0 => READ[obj] \in SUBSET WRITE[obj]
Invariants ==
/\ TypeOK
/\ LockOK
/\ ViewSerializableInCommitOrder
(***************************************************************************)
(* 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 = "Read"
-> edges(WRITE[hd.obj] \ {from})
[] hd.f = "Write"
-> edges((READ[hd.obj] \cup 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. *)
(***************************************************************************)
EventuallyAllCommitOrAbort ==
<>[](\A proc \in Proc : state[proc] \in {"Commit", "Abort"})
Properties == EventuallyAllCommitOrAbort
THEOREM Spec => []Invariants /\ Properties
=============================================================================
\* Modification History
\* Last modified Sat Mar 17 14:37:51 JST 2018 by takayuki
\* Created Sat Feb 17 10:34:44 JST 2018 by takayuki