-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathmemdb.qnt
230 lines (200 loc) · 7.44 KB
/
memdb.qnt
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
module memdb {
import basicSpells.* from "./basicspells"
type Key = int
type Value = int
type Request = {id: int, generation: int, kvs: Set[(Key, Value)]}
type Response = {id: int}
pure def minKey = 1
pure def maxKey = 10
pure def minValue = 0
pure val maxValue = 10
// state ========================================
// a simple map that is used to check against
var correctDB: Key -> Value
var mutableDB: Key -> Value
// the one that is in flush
var immutableDB: Key -> Value
// the ids of requests that the client is waiting for.
// It means client will handle a response only once (no matter how many times it retried).
// Note the response may not be from the first request, it can come from a retry request, and the resp of the first req is lost.
// Client only handles responses whose id is in the tracker
// When client receieves a response, or when client retries a request, it drops the id of the request.
var clientWaitingReqIds: Set[int]
// mock TiKV
var tikvDB: Key -> {generation: int, value: Value}
var writtenKeys: Set[Key]
// requests are always in flight, so it naturally imitates network retries.
// If we wanna make differences when retrying, e.g. attach a retry flag. Modify this.
var inflightRequests: Set[Request]
var inflightResponses: Set[Response]
var nextID: int
var nextGeneration: int
// functions ====================================
val state = {
correctDB: correctDB,
writtenKeys: writtenKeys,
mutableDB: mutableDB,
immutableDB: immutableDB,
tikvDB: tikvDB,
inflightRequests: inflightRequests,
inflightResponses: inflightResponses,
nextID: nextID,
}
def readFromcorrectDB(k: Key):Value = {
correctDB.get(k)
}
def read(k: Key): Value = {
if (mutableDB.has(k)) {
mutableDB.get(k)
} else if (immutableDB.has(k)) {
immutableDB.get(k)
} else if (tikvDB.has(k)) {
tikvDB.get(k).value
} else {
-1
}
}
// actions ======================================
action unchanged = all {
correctDB' = correctDB,
writtenKeys' = writtenKeys,
mutableDB' = mutableDB,
tikvDB' = tikvDB,
immutableDB' = immutableDB,
inflightRequests' = inflightRequests,
inflightResponses' = inflightResponses,
nextID' = nextID,
nextGeneration' = nextGeneration,
clientWaitingReqIds' = clientWaitingReqIds,
}
action write(k: Key, v: Value): bool = all {
size(writtenKeys) <= 5,
correctDB' = correctDB.put(k,v),
writtenKeys' = writtenKeys.union(Set(k)),
mutableDB' = mutableDB.put(k,v),
tikvDB' = tikvDB,
immutableDB' = immutableDB,
inflightRequests' = inflightRequests,
nextID' = nextID,
nextGeneration' = nextGeneration,
inflightResponses' = inflightResponses,
clientWaitingReqIds' = clientWaitingReqIds,
}
action send(req: Request): bool = all {
inflightRequests' = inflightRequests.union(Set(req)),
clientWaitingReqIds' = clientWaitingReqIds.union(Set(req.id)),
}
action flush = all {
size(immutableDB.keys()) == 0,
immutableDB' = mutableDB,
mutableDB' = Map(),
// Assumption: there is only one batch that contains all keys. In this spec we don't care about batches
send(
{
id: nextID,
kvs: mutableDB.keys().map(k => (k, mutableDB.get(k))),
generation: nextGeneration,
}
),
nextID' = nextID + 1,
nextGeneration' = nextGeneration + 1,
writtenKeys' = writtenKeys,
tikvDB' = tikvDB,
correctDB' = correctDB,
inflightResponses' = inflightResponses,
}
action doTikvWrite(req: Request): bool = all {
req.kvs.forall(kv => tikvDB.getOrElse(kv._1, {generation: -1, value: -1}).generation < req.generation),
tikvDB' = req.kvs.fold(tikvDB, (db, kv) => db.put(kv._1, {generation: req.generation, value: kv._2})),
inflightResponses' = inflightResponses.union(Set({id: req.id})),
inflightRequests' = inflightRequests,
mutableDB' = mutableDB,
immutableDB' = immutableDB,
writtenKeys' = writtenKeys,
correctDB' = correctDB,
nextID' = nextID,
nextGeneration' = nextGeneration,
clientWaitingReqIds' = clientWaitingReqIds,
}
// This is for test only, specify the request to process in TiKV
action doTikvWriteWithId(id: int): bool = {
nondet req = oneOf(inflightRequests.filter(r => r.id == id))
doTikvWrite(req)
}
action tikvWrite: bool = all {
size(inflightRequests) > 0,
{
nondet req = oneOf(inflightRequests)
doTikvWrite(req)
}
}
action clientReceiveResponse = {
val validRespSet = inflightResponses.filter(resp => clientWaitingReqIds.contains(resp.id))
all {
size(validRespSet) > 0,
nondet resp = oneOf(validRespSet)
nondet req = oneOf(inflightRequests.filter(r => r.id == resp.id))
doClientReceiveResponse(resp, req)
}
}
action doClientReceiveResponse(resp: Response, req: Request): bool = all {
inflightResponses' = inflightResponses.setRemove(resp),
immutableDB' = immutableDB.mapRemoveAll(req.kvs.map(kv => kv._1)),
inflightRequests' = inflightRequests,
mutableDB' = mutableDB,
writtenKeys' = writtenKeys,
tikvDB' = tikvDB,
correctDB' = correctDB,
nextID' = nextID,
nextGeneration' = nextGeneration,
clientWaitingReqIds' = clientWaitingReqIds.setRemove(req.id),
}
action init() = all {
correctDB' = Map(),
writtenKeys' = Set(),
mutableDB' = Map(),
immutableDB' = Map(),
tikvDB' = Map(),
inflightRequests' = Set(),
inflightResponses' = Set(),
nextID' = 1,
clientWaitingReqIds' = Set(),
nextGeneration' = 1,
}
action step() = {
nondet k = oneOf(minKey.to(maxKey))
nondet v = oneOf(minValue.to(maxValue))
any {
// user action
write(k,v),
// client action
flush,
clientReceiveResponse,
// server action
tikvWrite,
}
}
// Tests ========================================
run retryRequestOverWrite = init
.then(write(1,1))
.then(flush)
.then(doTikvWriteWithId(1))
.then(clientReceiveResponse)
.then(write(1,2))
.then(flush)
.then(doTikvWriteWithId(2))
.then(clientReceiveResponse)
.then(doTikvWriteWithId(1))
.fail() // stale req cannot succeed
// Invariants and properties ====================
val typeOK = all {
forall(correctDB.keys(), k => {
correctDB.get(k) >= minValue and correctDB.get(k) <= maxValue
})
}
val equivalence = {
forall(writtenKeys, k => {
readFromcorrectDB(k) == read(k)
})
}
}