-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathchecker.go
442 lines (411 loc) Β· 11 KB
/
checker.go
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
package porcupine
import (
"sort"
"sync/atomic"
"time"
)
type entryKind bool
const (
callEntry entryKind = false
returnEntry entryKind = true
)
type entry struct {
kind entryKind
value interface{}
id int
time int64
clientId int
}
type LinearizationInfo struct {
history [][]entry // for each partition, a list of entries
partialLinearizations [][][]int // for each partition, a set of histories (list of ids)
annotations []annotation
}
// PartialLinearizations returns partial linearizations found during the
// linearizability check, as sets of operation IDs.
//
// For each partition, it returns a set of possible linearization histories,
// where each history is represented as a sequence of operation IDs. If the
// history is linearizable, this will contain a complete linearization. If not
// linearizable, it contains the maximal partial linearizations found.
func (li *LinearizationInfo) PartialLinearizations() [][][]int {
return li.partialLinearizations
}
// PartialLinearizationsOperations returns partial linearizations found during
// the linearizability check, as sets of sequences of [Operation].
//
// For each partition, it returns a set of possible linearization histories,
// where each history is represented as a sequence of [Operation]. If the
// history is linearizable, this will contain a complete linearization. If not
// linearizable, it contains the maximal partial linearizations found.
func (li *LinearizationInfo) PartialLinearizationsOperations() [][][]Operation {
result := make([][][]Operation, len(li.history))
for p, partition := range li.history {
// reconstruct operations based on entries
callMap := make(map[int]entry)
retMap := make(map[int]entry)
for _, e := range partition {
if e.kind == callEntry {
callMap[e.id] = e
} else {
retMap[e.id] = e
}
}
opMap := make(map[int]Operation)
for id, call := range callMap {
ret, ok := retMap[id]
if !ok {
// this should never happen, because the LinearizationInfo
// object should always contain valid partial linearizations,
// where there is a return for every call
panic("cannot find corresponding return for call")
}
opMap[id] = Operation{
ClientId: call.clientId,
Input: call.value,
Call: call.time,
Output: ret.value,
Return: ret.time,
}
}
partials := make([][]Operation, len(li.partialLinearizations[p]))
for i, linearization := range li.partialLinearizations[p] {
partials[i] = make([]Operation, len(linearization))
for j, id := range linearization {
op, exists := opMap[id]
if !exists {
// this should never happen, because the LinearizationInfo
// object should always contain valid partial
// linearizations, where every ID in the partial
// linearization is in the history
panic("cannot find operation for given id in linearization")
}
partials[i][j] = op
}
}
result[p] = partials
}
return result
}
type byTime []entry
func (a byTime) Len() int {
return len(a)
}
func (a byTime) Swap(i, j int) {
a[i], a[j] = a[j], a[i]
}
func (a byTime) Less(i, j int) bool {
if a[i].time != a[j].time {
return a[i].time < a[j].time
}
// if the timestamps are the same, we need to make sure we order calls
// before returns
return a[i].kind == callEntry && a[j].kind == returnEntry
}
func makeEntries(history []Operation) []entry {
var entries []entry = nil
id := 0
for _, elem := range history {
entries = append(entries, entry{
callEntry, elem.Input, id, elem.Call, elem.ClientId})
entries = append(entries, entry{
returnEntry, elem.Output, id, elem.Return, elem.ClientId})
id++
}
sort.Sort(byTime(entries))
return entries
}
type node struct {
value interface{}
match *node // call if match is nil, otherwise return
id int
next *node
prev *node
}
func insertBefore(n *node, mark *node) *node {
if mark != nil {
beforeMark := mark.prev
mark.prev = n
n.next = mark
if beforeMark != nil {
n.prev = beforeMark
beforeMark.next = n
}
}
return n
}
func length(n *node) int {
l := 0
for n != nil {
n = n.next
l++
}
return l
}
func renumber(events []Event) []Event {
var e []Event
m := make(map[int]int) // renumbering
id := 0
for _, v := range events {
if r, ok := m[v.Id]; ok {
e = append(e, Event{v.ClientId, v.Kind, v.Value, r})
} else {
e = append(e, Event{v.ClientId, v.Kind, v.Value, id})
m[v.Id] = id
id++
}
}
return e
}
func convertEntries(events []Event) []entry {
var entries []entry
for i, elem := range events {
kind := callEntry
if elem.Kind == ReturnEvent {
kind = returnEntry
}
// use index as "time"
entries = append(entries, entry{kind, elem.Value, elem.Id, int64(i), elem.ClientId})
}
return entries
}
func makeLinkedEntries(entries []entry) *node {
var root *node = nil
match := make(map[int]*node)
for i := len(entries) - 1; i >= 0; i-- {
elem := entries[i]
if elem.kind == returnEntry {
entry := &node{value: elem.value, match: nil, id: elem.id}
match[elem.id] = entry
insertBefore(entry, root)
root = entry
} else {
entry := &node{value: elem.value, match: match[elem.id], id: elem.id}
insertBefore(entry, root)
root = entry
}
}
return root
}
type cacheEntry struct {
linearized bitset
state interface{}
}
func cacheContains(model Model, cache map[uint64][]cacheEntry, entry cacheEntry) bool {
for _, elem := range cache[entry.linearized.hash()] {
if entry.linearized.equals(elem.linearized) && model.Equal(entry.state, elem.state) {
return true
}
}
return false
}
type callsEntry struct {
entry *node
state interface{}
}
func lift(entry *node) {
entry.prev.next = entry.next
entry.next.prev = entry.prev
match := entry.match
match.prev.next = match.next
if match.next != nil {
match.next.prev = match.prev
}
}
func unlift(entry *node) {
match := entry.match
match.prev.next = match
if match.next != nil {
match.next.prev = match
}
entry.prev.next = entry
entry.next.prev = entry
}
func checkSingle(model Model, history []entry, computePartial bool, kill *int32) (bool, []*[]int) {
entry := makeLinkedEntries(history)
n := length(entry) / 2
linearized := newBitset(uint(n))
cache := make(map[uint64][]cacheEntry) // map from hash to cache entry
var calls []callsEntry
// longest linearizable prefix that includes the given entry
longest := make([]*[]int, n)
state := model.Init()
headEntry := insertBefore(&node{value: nil, match: nil, id: -1}, entry)
for headEntry.next != nil {
if atomic.LoadInt32(kill) != 0 {
return false, longest
}
if entry.match != nil {
matching := entry.match // the return entry
ok, newState := model.Step(state, entry.value, matching.value)
if ok {
newLinearized := linearized.clone().set(uint(entry.id))
newCacheEntry := cacheEntry{newLinearized, newState}
if !cacheContains(model, cache, newCacheEntry) {
hash := newLinearized.hash()
cache[hash] = append(cache[hash], newCacheEntry)
calls = append(calls, callsEntry{entry, state})
state = newState
linearized.set(uint(entry.id))
lift(entry)
entry = headEntry.next
} else {
entry = entry.next
}
} else {
entry = entry.next
}
} else {
if len(calls) == 0 {
return false, longest
}
// longest
if computePartial {
callsLen := len(calls)
var seq []int = nil
for _, v := range calls {
if longest[v.entry.id] == nil || callsLen > len(*longest[v.entry.id]) {
// create seq lazily
if seq == nil {
seq = make([]int, len(calls))
for i, v := range calls {
seq[i] = v.entry.id
}
}
longest[v.entry.id] = &seq
}
}
}
callsTop := calls[len(calls)-1]
entry = callsTop.entry
state = callsTop.state
linearized.clear(uint(entry.id))
calls = calls[:len(calls)-1]
unlift(entry)
entry = entry.next
}
}
// longest linearization is the complete linearization, which is calls
seq := make([]int, len(calls))
for i, v := range calls {
seq[i] = v.entry.id
}
for i := 0; i < n; i++ {
longest[i] = &seq
}
return true, longest
}
func fillDefault(model Model) Model {
if model.Partition == nil {
model.Partition = noPartition
}
if model.PartitionEvent == nil {
model.PartitionEvent = noPartitionEvent
}
if model.Equal == nil {
model.Equal = shallowEqual
}
if model.DescribeOperation == nil {
model.DescribeOperation = defaultDescribeOperation
}
if model.DescribeState == nil {
model.DescribeState = defaultDescribeState
}
return model
}
func checkParallel(model Model, history [][]entry, computeInfo bool, timeout time.Duration) (CheckResult, LinearizationInfo) {
ok := true
timedOut := false
results := make(chan bool, len(history))
longest := make([][]*[]int, len(history))
kill := int32(0)
for i, subhistory := range history {
go func(i int, subhistory []entry) {
ok, l := checkSingle(model, subhistory, computeInfo, &kill)
longest[i] = l
results <- ok
}(i, subhistory)
}
var timeoutChan <-chan time.Time
if timeout > 0 {
timeoutChan = time.After(timeout)
}
count := 0
loop:
for {
select {
case result := <-results:
count++
ok = ok && result
if !ok && !computeInfo {
atomic.StoreInt32(&kill, 1)
break loop
}
if count >= len(history) {
break loop
}
case <-timeoutChan:
timedOut = true
atomic.StoreInt32(&kill, 1)
break loop // if we time out, we might get a false positive
}
}
var info LinearizationInfo
if computeInfo {
// make sure we've waited for all goroutines to finish,
// otherwise we might race on access to longest[]
for count < len(history) {
<-results
count++
}
// return longest linearizable prefixes that include each history element
partialLinearizations := make([][][]int, len(history))
for i := 0; i < len(history); i++ {
var partials [][]int
// turn longest into a set of unique linearizations
set := make(map[*[]int]struct{})
for _, v := range longest[i] {
if v != nil {
set[v] = struct{}{}
}
}
for k := range set {
arr := make([]int, len(*k))
copy(arr, *k)
partials = append(partials, arr)
}
partialLinearizations[i] = partials
}
info.history = history
info.partialLinearizations = partialLinearizations
}
var result CheckResult
if !ok {
result = Illegal
} else {
if timedOut {
result = Unknown
} else {
result = Ok
}
}
return result, info
}
func checkEvents(model Model, history []Event, verbose bool, timeout time.Duration) (CheckResult, LinearizationInfo) {
model = fillDefault(model)
partitions := model.PartitionEvent(history)
l := make([][]entry, len(partitions))
for i, subhistory := range partitions {
l[i] = convertEntries(renumber(subhistory))
}
return checkParallel(model, l, verbose, timeout)
}
func checkOperations(model Model, history []Operation, verbose bool, timeout time.Duration) (CheckResult, LinearizationInfo) {
model = fillDefault(model)
partitions := model.Partition(history)
l := make([][]entry, len(partitions))
for i, subhistory := range partitions {
l[i] = makeEntries(subhistory)
}
return checkParallel(model, l, verbose, timeout)
}