-
Notifications
You must be signed in to change notification settings - Fork 75
/
checker.go
104 lines (90 loc) · 2.04 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
package paxi
import (
"sort"
"github.com/ailidani/paxi/lib"
)
// A simple linearizability checker based on https://pdos.csail.mit.edu/6.824/papers/fb-consistency.pdf
type checker struct {
*lib.Graph
}
func newChecker() *checker {
return &checker{
Graph: lib.NewGraph(),
}
}
func (c *checker) add(o *operation) {
if c.Graph.Has(o) {
// already in graph from lookahead
return
}
c.Graph.Add(o)
for v := range c.Graph.Vertices() {
if v.(*operation).happenBefore(*o) {
//c.AddEdge(o, v)
c.AddEdge(v, o)
}
}
}
func (c *checker) remove(o *operation) {
c.Remove(o)
}
func (c *checker) clear() {
c.Graph = lib.NewGraph()
}
// match finds the first matching write operation to the given read operation
func (c *checker) match(read *operation) *operation {
//for _, v := range c.Graph.BFSReverse(read) {
for v := range c.Graph.Vertices() {
if read.output == v.(*operation).input {
return v.(*operation)
}
}
return nil
}
// matched write inherits edges read
func (c *checker) merge(read, write *operation) {
for s := range c.To(read) {
if s.(*operation) != write {
c.Graph.AddEdge(s.(*operation), write)
}
}
// refine response time of merged vertex
if read.end < write.end {
write.end = read.end
}
c.Graph.Remove(read)
}
func (c *checker) linearizable(history []*operation) []*operation {
c.clear()
sort.Sort(byTime(history))
anomaly := make([]*operation, 0)
for i, o := range history {
c.add(o)
// o is read operation
if o.input == nil {
// look ahead for concurrent writes
for j := i + 1; j < len(history) && o.concurrent(*history[j]); j++ {
// next operation is write
if history[j].output == nil {
c.add(history[j])
}
}
match := c.match(o)
if match != nil {
c.merge(o, match)
}
cycle := c.Graph.Cycle()
if cycle != nil {
anomaly = append(anomaly, o)
for _, u := range cycle {
for _, v := range cycle {
if c.Graph.From(u).Has(v) && u.(*operation).start > v.(*operation).end {
c.Graph.RemoveEdge(u, v)
}
}
}
}
}
}
return anomaly
}