-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoy_documents_test.go
More file actions
249 lines (219 loc) · 5.75 KB
/
Copy pathtoy_documents_test.go
File metadata and controls
249 lines (219 loc) · 5.75 KB
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
package yin
import (
"math/rand"
"testing"
)
const toyGSetDeltaKind = "toy-g-set"
type toyGSet struct {
replica ReplicaID
values map[int]struct{}
version VersionVector
}
func newToyGSet(replica ReplicaID) *toyGSet {
return &toyGSet{
replica: replica,
values: make(map[int]struct{}),
}
}
func (s *toyGSet) Add(value int) {
if s.values == nil {
s.values = make(map[int]struct{})
}
s.values[value] = struct{}{}
s.version = s.version.Increment(s.replica)
}
func (s *toyGSet) Merge(other Document) (changed bool) {
otherSet, ok := other.(*toyGSet)
if !ok || otherSet == nil {
return false
}
return s.applyDelta(cloneIntSet(otherSet.values), otherSet.version)
}
func (s *toyGSet) ExtractDelta(since VersionVector) Delta {
switch since.Compare(s.version) {
case VersionVectorEqual, VersionVectorDominates:
return nil
default:
return toyGSetDelta{
values: cloneIntSet(s.values),
version: s.version,
}
}
}
func (s *toyGSet) ApplyDelta(d Delta) (changed bool) {
if d == nil {
return false
}
switch delta := d.(type) {
case toyGSetDelta:
return s.applyDelta(delta.values, delta.version)
case *toyGSetDelta:
if delta == nil {
return false
}
return s.applyDelta(delta.values, delta.version)
default:
return false
}
}
func (s *toyGSet) Equal(other Document) bool {
otherSet, ok := other.(*toyGSet)
if !ok || otherSet == nil || len(s.values) != len(otherSet.values) || !s.version.Equal(otherSet.version) {
return false
}
for value := range s.values {
if _, exists := otherSet.values[value]; !exists {
return false
}
}
return true
}
func (s *toyGSet) Version() VersionVector {
return s.version.Clone()
}
func (s *toyGSet) applyDelta(values map[int]struct{}, version VersionVector) (changed bool) {
if s.values == nil {
s.values = make(map[int]struct{}, len(values))
}
for value := range values {
if _, exists := s.values[value]; exists {
continue
}
s.values[value] = struct{}{}
changed = true
}
joined := s.version.Join(version)
if !joined.Equal(s.version) {
s.version = joined
changed = true
}
return changed
}
type toyGSetDelta struct {
values map[int]struct{}
version VersionVector
}
func (d toyGSetDelta) Kind() string {
return toyGSetDeltaKind
}
func (d toyGSetDelta) Version() VersionVector {
return d.version.Clone()
}
func cloneIntSet(values map[int]struct{}) map[int]struct{} {
clone := make(map[int]struct{}, len(values))
for value := range values {
clone[value] = struct{}{}
}
return clone
}
const brokenAdditiveCounterDeltaKind = "broken-additive-counter"
type brokenAdditiveCounter struct {
replica ReplicaID
total int
version VersionVector
}
func (c *brokenAdditiveCounter) Add(value int) {
c.total += value
c.version = c.version.Increment(c.replica)
}
func (c *brokenAdditiveCounter) Merge(other Document) (changed bool) {
otherCounter, ok := other.(*brokenAdditiveCounter)
if !ok || otherCounter == nil {
return false
}
return c.applyDelta(otherCounter.total, otherCounter.version)
}
func (c *brokenAdditiveCounter) ExtractDelta(since VersionVector) Delta {
switch since.Compare(c.version) {
case VersionVectorEqual, VersionVectorDominates:
return nil
default:
return brokenAdditiveCounterDelta{
total: c.total,
version: c.version,
}
}
}
func (c *brokenAdditiveCounter) ApplyDelta(d Delta) (changed bool) {
if d == nil {
return false
}
switch delta := d.(type) {
case brokenAdditiveCounterDelta:
return c.applyDelta(delta.total, delta.version)
case *brokenAdditiveCounterDelta:
if delta == nil {
return false
}
return c.applyDelta(delta.total, delta.version)
default:
return false
}
}
func (c *brokenAdditiveCounter) Equal(other Document) bool {
otherCounter, ok := other.(*brokenAdditiveCounter)
return ok && otherCounter != nil && c.total == otherCounter.total && c.version.Equal(otherCounter.version)
}
func (c *brokenAdditiveCounter) Version() VersionVector {
return c.version.Clone()
}
func (c *brokenAdditiveCounter) applyDelta(total int, version VersionVector) (changed bool) {
beforeTotal := c.total
beforeVersion := c.version
c.total += total
c.version = c.version.Join(version)
return c.total != beforeTotal || !c.version.Equal(beforeVersion)
}
type brokenAdditiveCounterDelta struct {
total int
version VersionVector
}
func (d brokenAdditiveCounterDelta) Kind() string {
return brokenAdditiveCounterDeltaKind
}
func (d brokenAdditiveCounterDelta) Version() VersionVector {
return d.version.Clone()
}
func TestHarnessDetectsBrokenMerge(t *testing.T) {
const seed int64 = 0x5eed_c0de
correct := convergenceConfig[int]{
name: "toy G-Set",
seed: seed,
replicas: 4,
operations: 64,
mergeRounds: 3,
newReplica: func(replica int) Document {
return newToyGSet(harnessReplicaID("toy-gset", replica))
},
generateOp: func(r *rand.Rand, step int, replica int) int {
return replica*1_000 + step*10 + r.Intn(10)
},
apply: func(replica Document, op int) {
replica.(*toyGSet).Add(op)
},
}
if _, err := runConvergenceHarness(correct); err != nil {
t.Fatalf("correct toy CRDT did not converge: %v", err)
}
broken := convergenceConfig[int]{
name: "broken additive counter",
seed: seed,
replicas: 4,
operations: 64,
mergeRounds: 3,
newReplica: func(replica int) Document {
return &brokenAdditiveCounter{replica: harnessReplicaID("broken-counter", replica)}
},
generateOp: func(r *rand.Rand, step int, replica int) int {
return 1 + r.Intn(7) + replica + step%3
},
apply: func(replica Document, op int) {
replica.(*brokenAdditiveCounter).Add(op)
},
}
if _, err := runConvergenceHarness(broken); err == nil {
t.Fatal("broken toy CRDT unexpectedly converged; harness did not catch non-idempotent merge")
} else {
t.Logf("harness caught broken merge as expected: %v", err)
}
}