-
Notifications
You must be signed in to change notification settings - Fork 44
/
crdt_test.go
1012 lines (867 loc) · 21.2 KB
/
crdt_test.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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package crdt
import (
"context"
"errors"
"fmt"
"math/rand"
"os"
"sync"
"sync/atomic"
"testing"
"time"
"github.com/dgraph-io/badger"
blockstore "github.com/ipfs/boxo/blockstore"
"github.com/ipfs/boxo/ipld/merkledag"
mdutils "github.com/ipfs/boxo/ipld/merkledag/test"
cid "github.com/ipfs/go-cid"
ds "github.com/ipfs/go-datastore"
query "github.com/ipfs/go-datastore/query"
dssync "github.com/ipfs/go-datastore/sync"
dstest "github.com/ipfs/go-datastore/test"
badgerds "github.com/ipfs/go-ds-badger"
ipld "github.com/ipfs/go-ipld-format"
log "github.com/ipfs/go-log/v2"
"github.com/multiformats/go-multihash"
)
var numReplicas = 15
var debug = false
const (
mapStore = iota
badgerStore
)
var store int = mapStore
func init() {
dstest.ElemCount = 10
}
type testLogger struct {
name string
l log.StandardLogger
}
func (tl *testLogger) Debug(args ...interface{}) {
args = append([]interface{}{tl.name}, args...)
tl.l.Debug(args...)
}
func (tl *testLogger) Debugf(format string, args ...interface{}) {
args = append([]interface{}{tl.name}, args...)
tl.l.Debugf("%s "+format, args...)
}
func (tl *testLogger) Error(args ...interface{}) {
args = append([]interface{}{tl.name}, args...)
tl.l.Error(args...)
}
func (tl *testLogger) Errorf(format string, args ...interface{}) {
args = append([]interface{}{tl.name}, args...)
tl.l.Errorf("%s "+format, args...)
}
func (tl *testLogger) Fatal(args ...interface{}) {
args = append([]interface{}{tl.name}, args...)
tl.l.Fatal(args...)
}
func (tl *testLogger) Fatalf(format string, args ...interface{}) {
args = append([]interface{}{tl.name}, args...)
tl.l.Fatalf("%s "+format, args...)
}
func (tl *testLogger) Info(args ...interface{}) {
args = append([]interface{}{tl.name}, args...)
tl.l.Info(args...)
}
func (tl *testLogger) Infof(format string, args ...interface{}) {
args = append([]interface{}{tl.name}, args...)
tl.l.Infof("%s "+format, args...)
}
func (tl *testLogger) Panic(args ...interface{}) {
args = append([]interface{}{tl.name}, args...)
tl.l.Panic(args...)
}
func (tl *testLogger) Panicf(format string, args ...interface{}) {
args = append([]interface{}{tl.name}, args...)
tl.l.Panicf("%s "+format, args...)
}
func (tl *testLogger) Warn(args ...interface{}) {
args = append([]interface{}{tl.name}, args...)
tl.l.Warn(args...)
}
func (tl *testLogger) Warnf(format string, args ...interface{}) {
args = append([]interface{}{tl.name}, args...)
tl.l.Warnf("%s "+format, args...)
}
type mockBroadcaster struct {
ctx context.Context
chans []chan []byte
myChan chan []byte
dropProb *atomic.Int64 // probability of dropping a message instead of receiving it
t testing.TB
}
func newBroadcasters(t testing.TB, n int) ([]*mockBroadcaster, context.CancelFunc) {
ctx, cancel := context.WithCancel(context.Background())
broadcasters := make([]*mockBroadcaster, n)
chans := make([]chan []byte, n)
dropP := &atomic.Int64{}
for i := range chans {
chans[i] = make(chan []byte, 300)
broadcasters[i] = &mockBroadcaster{
ctx: ctx,
chans: chans,
myChan: chans[i],
dropProb: dropP,
t: t,
}
}
return broadcasters, cancel
}
func (mb *mockBroadcaster) Broadcast(ctx context.Context, data []byte) error {
var wg sync.WaitGroup
randg := rand.New(rand.NewSource(time.Now().UnixNano()))
for i, ch := range mb.chans {
n := randg.Int63n(100)
if n < mb.dropProb.Load() {
continue
}
wg.Add(1)
go func(i int) {
defer wg.Done()
randg := rand.New(rand.NewSource(int64(i)))
// randomize when we send a little bit
if randg.Intn(100) < 30 {
// Sleep for a very small time that will
// effectively be pretty random
time.Sleep(time.Nanosecond)
}
timer := time.NewTimer(5 * time.Second)
defer timer.Stop()
select {
case ch <- data:
case <-timer.C:
mb.t.Errorf("broadcasting to %d timed out", i)
}
}(i)
wg.Wait()
}
return nil
}
func (mb *mockBroadcaster) Next(ctx context.Context) ([]byte, error) {
select {
case data := <-mb.myChan:
return data, nil
case <-ctx.Done():
return nil, ErrNoMoreBroadcast
case <-mb.ctx.Done():
return nil, ErrNoMoreBroadcast
}
}
type mockDAGSvc struct {
ipld.DAGService
bs blockstore.Blockstore
}
func (mds *mockDAGSvc) Add(ctx context.Context, n ipld.Node) error {
return mds.DAGService.Add(ctx, n)
}
func (mds *mockDAGSvc) Get(ctx context.Context, c cid.Cid) (ipld.Node, error) {
nd, err := mds.DAGService.Get(ctx, c)
if err != nil {
return nd, err
}
return nd, nil
}
func (mds *mockDAGSvc) GetMany(ctx context.Context, cids []cid.Cid) <-chan *ipld.NodeOption {
return mds.DAGService.GetMany(ctx, cids)
}
func storeFolder(i int) string {
return fmt.Sprintf("test-badger-%d", i)
}
func makeStore(t testing.TB, i int) ds.Datastore {
t.Helper()
switch store {
case mapStore:
return dssync.MutexWrap(ds.NewMapDatastore())
case badgerStore:
folder := storeFolder(i)
err := os.MkdirAll(folder, 0700)
if err != nil {
t.Fatal(err)
}
badgerOpts := badger.DefaultOptions("")
badgerOpts.SyncWrites = false
badgerOpts.MaxTableSize = 1048576
opts := badgerds.Options{Options: badgerOpts}
dstore, err := badgerds.NewDatastore(folder, &opts)
if err != nil {
t.Fatal(err)
}
return dstore
default:
t.Fatal("bad store type selected for tests")
return nil
}
}
func makeNReplicas(t testing.TB, n int, opts *Options) ([]*Datastore, func()) {
bcasts, bcastCancel := newBroadcasters(t, n)
bs := mdutils.Bserv()
dagserv := merkledag.NewDAGService(bs)
replicaOpts := make([]*Options, n)
for i := range replicaOpts {
if opts == nil {
replicaOpts[i] = DefaultOptions()
} else {
copy := *opts
replicaOpts[i] = ©
}
replicaOpts[i].Logger = &testLogger{
name: fmt.Sprintf("r#%d: ", i),
l: DefaultOptions().Logger,
}
replicaOpts[i].RebroadcastInterval = time.Second * 5
replicaOpts[i].NumWorkers = 5
replicaOpts[i].DAGSyncerTimeout = time.Second
}
replicas := make([]*Datastore, n)
for i := range replicas {
dagsync := &mockDAGSvc{
DAGService: dagserv,
bs: bs.Blockstore(),
}
var err error
replicas[i], err = New(
makeStore(t, i),
// ds.NewLogDatastore(
// makeStore(t, i),
// fmt.Sprintf("crdt-test-%d", i),
// ),
ds.NewKey("crdttest"),
dagsync,
bcasts[i],
replicaOpts[i],
)
if err != nil {
t.Fatal(err)
}
}
if debug {
log.SetLogLevel("crdt", "debug")
}
closeReplicas := func() {
bcastCancel()
for i, r := range replicas {
err := r.Close()
if err != nil {
t.Error(err)
}
os.RemoveAll(storeFolder(i))
}
}
return replicas, closeReplicas
}
func makeReplicas(t testing.TB, opts *Options) ([]*Datastore, func()) {
return makeNReplicas(t, numReplicas, opts)
}
func TestCRDT(t *testing.T) {
ctx := context.Background()
replicas, closeReplicas := makeReplicas(t, nil)
defer closeReplicas()
k := ds.NewKey("hi")
err := replicas[0].Put(ctx, k, []byte("hola"))
if err != nil {
t.Fatal(err)
}
time.Sleep(time.Second)
for _, r := range replicas {
v, err := r.Get(ctx, k)
if err != nil {
t.Error(err)
}
if string(v) != "hola" {
t.Error("bad content: ", string(v))
}
}
}
func TestCRDTReplication(t *testing.T) {
ctx := context.Background()
nItems := 50
randGen := rand.New(rand.NewSource(time.Now().UnixNano()))
replicas, closeReplicas := makeReplicas(t, nil)
defer closeReplicas()
// Add nItems choosing the replica randomly
for i := 0; i < nItems; i++ {
k := ds.RandomKey()
v := []byte(fmt.Sprintf("%d", i))
n := randGen.Intn(len(replicas))
err := replicas[n].Put(ctx, k, v)
if err != nil {
t.Fatal(err)
}
}
time.Sleep(500 * time.Millisecond)
// Query all items
q := query.Query{
KeysOnly: true,
}
results, err := replicas[0].Query(ctx, q)
if err != nil {
t.Fatal(err)
}
defer results.Close()
rest, err := results.Rest()
if err != nil {
t.Fatal(err)
}
if len(rest) != nItems {
t.Fatalf("expected %d elements", nItems)
}
// make sure each item has arrived to every replica
for _, res := range rest {
for _, r := range replicas {
ok, err := r.Has(ctx, ds.NewKey(res.Key))
if err != nil {
t.Error(err)
}
if !ok {
t.Error("replica should have key")
}
}
}
// give a new value for each item
for _, r := range rest {
n := randGen.Intn(len(replicas))
err := replicas[n].Put(ctx, ds.NewKey(r.Key), []byte("hola"))
if err != nil {
t.Error(err)
}
}
time.Sleep(200 * time.Millisecond)
// query everything again
results, err = replicas[0].Query(ctx, q)
if err != nil {
t.Fatal(err)
}
defer results.Close()
total := 0
for r := range results.Next() {
total++
if r.Error != nil {
t.Error(err)
}
k := ds.NewKey(r.Key)
for i, r := range replicas {
v, err := r.Get(ctx, k)
if err != nil {
t.Error(err)
}
if string(v) != "hola" {
t.Errorf("value should be hola for %s in replica %d", k, i)
}
}
}
if total != nItems {
t.Fatalf("expected %d elements again", nItems)
}
for _, r := range replicas {
list, _, err := r.heads.List(ctx)
if err != nil {
t.Fatal(err)
}
t.Log(list)
}
//replicas[0].PrintDAG()
//fmt.Println("==========================================================")
//replicas[1].PrintDAG()
}
// TestCRDTPriority tests that given multiple concurrent updates from several
// replicas on the same key, the resulting values converge to the same key.
//
// It does this by launching one go routine for every replica, where it replica
// writes the value #replica-number repeteadly (nItems-times).
//
// Finally, it puts a final value for a single key in the first replica and
// checks that all replicas got it.
//
// If key priority rules are respected, the "last" update emitted for the key
// K (which could have come from any replica) should take place everywhere.
func TestCRDTPriority(t *testing.T) {
ctx := context.Background()
nItems := 50
replicas, closeReplicas := makeReplicas(t, nil)
defer closeReplicas()
k := ds.NewKey("k")
var wg sync.WaitGroup
wg.Add(len(replicas))
for i, r := range replicas {
go func(r *Datastore, i int) {
defer wg.Done()
for j := 0; j < nItems; j++ {
err := r.Put(ctx, k, []byte(fmt.Sprintf("r#%d", i)))
if err != nil {
t.Error(err)
}
}
}(r, i)
}
wg.Wait()
time.Sleep(5000 * time.Millisecond)
var v, lastv []byte
var err error
for i, r := range replicas {
v, err = r.Get(ctx, k)
if err != nil {
t.Error(err)
}
t.Logf("Replica %d got value %s", i, string(v))
if lastv != nil && string(v) != string(lastv) {
t.Error("value was different between replicas, but should be the same")
}
lastv = v
}
err = replicas[0].Put(ctx, k, []byte("final value"))
if err != nil {
t.Fatal(err)
}
time.Sleep(1000 * time.Millisecond)
for i, r := range replicas {
v, err := r.Get(ctx, k)
if err != nil {
t.Error(err)
}
if string(v) != "final value" {
t.Errorf("replica %d has wrong final value: %s", i, string(v))
}
}
//replicas[14].PrintDAG()
//fmt.Println("=======================================================")
//replicas[1].PrintDAG()
}
func TestCRDTCatchUp(t *testing.T) {
ctx := context.Background()
nItems := 50
replicas, closeReplicas := makeReplicas(t, nil)
defer closeReplicas()
r := replicas[len(replicas)-1]
br := r.broadcaster.(*mockBroadcaster)
br.dropProb.Store(101)
// this items will not get to anyone
for i := 0; i < nItems; i++ {
k := ds.RandomKey()
err := r.Put(ctx, k, nil)
if err != nil {
t.Fatal(err)
}
}
time.Sleep(100 * time.Millisecond)
br.dropProb.Store(0)
// this message will get to everyone
err := r.Put(ctx, ds.RandomKey(), nil)
if err != nil {
t.Fatal(err)
}
time.Sleep(500 * time.Millisecond)
q := query.Query{KeysOnly: true}
results, err := replicas[0].Query(ctx, q)
if err != nil {
t.Fatal(err)
}
defer results.Close()
rest, err := results.Rest()
if err != nil {
t.Fatal(err)
}
if len(rest) != nItems+1 {
t.Fatal("replica 0 did not get all the things")
}
}
func TestCRDTPrintDAG(t *testing.T) {
ctx := context.Background()
nItems := 5
replicas, closeReplicas := makeReplicas(t, nil)
defer closeReplicas()
// this items will not get to anyone
for i := 0; i < nItems; i++ {
k := ds.RandomKey()
err := replicas[0].Put(ctx, k, nil)
if err != nil {
t.Fatal(err)
}
}
err := replicas[0].PrintDAG(ctx)
if err != nil {
t.Fatal(err)
}
}
func TestCRDTHooks(t *testing.T) {
ctx := context.Background()
var put int64
var deleted int64
opts := DefaultOptions()
opts.PutHook = func(k ds.Key, v []byte) {
atomic.AddInt64(&put, 1)
}
opts.DeleteHook = func(k ds.Key) {
atomic.AddInt64(&deleted, 1)
}
replicas, closeReplicas := makeReplicas(t, opts)
defer closeReplicas()
k := ds.RandomKey()
err := replicas[0].Put(ctx, k, nil)
if err != nil {
t.Fatal(err)
}
err = replicas[0].Delete(ctx, k)
if err != nil {
t.Fatal(err)
}
time.Sleep(100 * time.Millisecond)
if atomic.LoadInt64(&put) != int64(len(replicas)) {
t.Error("all replicas should have notified Put", put)
}
if atomic.LoadInt64(&deleted) != int64(len(replicas)) {
t.Error("all replicas should have notified Remove", deleted)
}
}
func TestCRDTBatch(t *testing.T) {
ctx := context.Background()
opts := DefaultOptions()
opts.MaxBatchDeltaSize = 500 // bytes
replicas, closeReplicas := makeReplicas(t, opts)
defer closeReplicas()
btch, err := replicas[0].Batch(ctx)
if err != nil {
t.Fatal(err)
}
// This should be batched
k := ds.RandomKey()
err = btch.Put(ctx, k, make([]byte, 200))
if err != nil {
t.Fatal(err)
}
if _, err := replicas[0].Get(ctx, k); err != ds.ErrNotFound {
t.Fatal("should not have commited the batch")
}
k2 := ds.RandomKey()
err = btch.Put(ctx, k2, make([]byte, 400))
if err != nil {
t.Fatal(err)
}
if _, err := replicas[0].Get(ctx, k2); err != nil {
t.Fatal("should have commited the batch: delta size was over threshold")
}
err = btch.Delete(ctx, k)
if err != nil {
t.Fatal(err)
}
if _, err := replicas[0].Get(ctx, k); err != nil {
t.Fatal("should not have committed the batch")
}
err = btch.Commit(ctx)
if err != nil {
t.Fatal(err)
}
time.Sleep(100 * time.Millisecond)
for _, r := range replicas {
if _, err := r.Get(ctx, k); err != ds.ErrNotFound {
t.Error("k should have been deleted everywhere")
}
if _, err := r.Get(ctx, k2); err != nil {
t.Error("k2 should be everywhere")
}
}
}
func TestCRDTNamespaceClash(t *testing.T) {
ctx := context.Background()
opts := DefaultOptions()
replicas, closeReplicas := makeReplicas(t, opts)
defer closeReplicas()
k := ds.NewKey("path/to/something")
err := replicas[0].Put(ctx, k, nil)
if err != nil {
t.Fatal(err)
}
time.Sleep(100 * time.Millisecond)
k = ds.NewKey("path")
ok, _ := replicas[0].Has(ctx, k)
if ok {
t.Error("it should not have the key")
}
_, err = replicas[0].Get(ctx, k)
if err != ds.ErrNotFound {
t.Error("should return err not found")
}
err = replicas[0].Put(ctx, k, []byte("hello"))
if err != nil {
t.Fatal(err)
}
v, err := replicas[0].Get(ctx, k)
if err != nil {
t.Fatal(err)
}
if string(v) != "hello" {
t.Error("wrong value read from database")
}
err = replicas[0].Delete(ctx, ds.NewKey("path/to/something"))
if err != nil {
t.Fatal(err)
}
v, err = replicas[0].Get(ctx, k)
if err != nil {
t.Fatal(err)
}
if string(v) != "hello" {
t.Error("wrong value read from database")
}
}
var _ ds.Datastore = (*syncedTrackDs)(nil)
type syncedTrackDs struct {
ds.Datastore
syncs map[ds.Key]struct{}
set *set
}
func (st *syncedTrackDs) Sync(ctx context.Context, k ds.Key) error {
st.syncs[k] = struct{}{}
return st.Datastore.Sync(ctx, k)
}
func (st *syncedTrackDs) isSynced(k ds.Key) bool {
prefixStr := k.String()
mustBeSynced := []ds.Key{
st.set.elemsPrefix(prefixStr),
st.set.tombsPrefix(prefixStr),
st.set.keyPrefix(keysNs).Child(k),
}
for k := range st.syncs {
synced := false
for _, t := range mustBeSynced {
if k == t || k.IsAncestorOf(t) {
synced = true
break
}
}
if !synced {
return false
}
}
return true
}
func TestCRDTSync(t *testing.T) {
ctx := context.Background()
opts := DefaultOptions()
replicas, closeReplicas := makeReplicas(t, opts)
defer closeReplicas()
syncedDs := &syncedTrackDs{
Datastore: replicas[0].set.store,
syncs: make(map[ds.Key]struct{}),
set: replicas[0].set,
}
replicas[0].set.store = syncedDs
k1 := ds.NewKey("/hello/bye")
k2 := ds.NewKey("/hello")
k3 := ds.NewKey("/hell")
err := replicas[0].Put(ctx, k1, []byte("value1"))
if err != nil {
t.Fatal(err)
}
err = replicas[0].Put(ctx, k2, []byte("value2"))
if err != nil {
t.Fatal(err)
}
err = replicas[0].Put(ctx, k3, []byte("value3"))
if err != nil {
t.Fatal(err)
}
err = replicas[0].Sync(ctx, ds.NewKey("/hello"))
if err != nil {
t.Fatal(err)
}
if !syncedDs.isSynced(k1) {
t.Error("k1 should have been synced")
}
if !syncedDs.isSynced(k2) {
t.Error("k2 should have been synced")
}
if syncedDs.isSynced(k3) {
t.Error("k3 should have not been synced")
}
}
func TestCRDTBroadcastBackwardsCompat(t *testing.T) {
ctx := context.Background()
mh, err := multihash.Sum([]byte("emacs is best"), multihash.SHA2_256, -1)
if err != nil {
t.Fatal(err)
}
cidV0 := cid.NewCidV0(mh)
opts := DefaultOptions()
replicas, closeReplicas := makeReplicas(t, opts)
defer closeReplicas()
cids, err := replicas[0].decodeBroadcast(ctx, cidV0.Bytes())
if err != nil {
t.Fatal(err)
}
if len(cids) != 1 || !cids[0].Equals(cidV0) {
t.Error("should have returned a single cidV0", cids)
}
data, err := replicas[0].encodeBroadcast(ctx, cids)
if err != nil {
t.Fatal(err)
}
cids2, err := replicas[0].decodeBroadcast(ctx, data)
if err != nil {
t.Fatal(err)
}
if len(cids2) != 1 || !cids[0].Equals(cidV0) {
t.Error("should have reparsed cid0", cids2)
}
}
func BenchmarkQueryElements(b *testing.B) {
ctx := context.Background()
replicas, closeReplicas := makeNReplicas(b, 1, nil)
defer closeReplicas()
for i := 0; i < b.N; i++ {
k := ds.RandomKey()
err := replicas[0].Put(ctx, k, make([]byte, 2000))
if err != nil {
b.Fatal(err)
}
}
b.ResetTimer()
q := query.Query{
KeysOnly: false,
}
results, err := replicas[0].Query(ctx, q)
if err != nil {
b.Fatal(err)
}
defer results.Close()
totalSize := 0
for r := range results.Next() {
if r.Error != nil {
b.Error(r.Error)
}
totalSize += len(r.Value)
}
b.Log(totalSize)
}
func TestRandomizeInterval(t *testing.T) {
prevR := 100 * time.Second
for i := 0; i < 1000; i++ {
r := randomizeInterval(100 * time.Second)
if r < 70*time.Second || r > 130*time.Second {
t.Error("r was ", r)
}
if prevR == r {
t.Log("r and prevR were equal")
}
prevR = r
}
}
func TestCRDTPutPutDelete(t *testing.T) {
replicas, closeReplicas := makeNReplicas(t, 2, nil)
defer closeReplicas()
ctx := context.Background()
br0 := replicas[0].broadcaster.(*mockBroadcaster)
br0.dropProb.Store(101)
br1 := replicas[1].broadcaster.(*mockBroadcaster)
br1.dropProb.Store(101)
k := ds.NewKey("k1")
// r0 - put put delete
err := replicas[0].Put(ctx, k, []byte("r0-1"))
if err != nil {
t.Fatal(err)
}
err = replicas[0].Put(ctx, k, []byte("r0-2"))
if err != nil {
t.Fatal(err)
}
err = replicas[0].Delete(ctx, k)
if err != nil {
t.Fatal(err)
}
// r1 - put
err = replicas[1].Put(ctx, k, []byte("r1-1"))
if err != nil {
t.Fatal(err)
}
br0.dropProb.Store(0)
br1.dropProb.Store(0)
time.Sleep(15 * time.Second)
r0Res, err := replicas[0].Get(ctx, ds.NewKey("k1"))
if err != nil {
if !errors.Is(err, ds.ErrNotFound) {
t.Fatal(err)
}
}
r1Res, err := replicas[1].Get(ctx, ds.NewKey("k1"))
if err != nil {
t.Fatal(err)
}
closeReplicas()
if string(r0Res) != string(r1Res) {
fmt.Printf("r0Res: %s\nr1Res: %s\n", string(r0Res), string(r1Res))
t.Log("r0 dag")
replicas[0].PrintDAG(ctx)
t.Log("r1 dag")
replicas[1].PrintDAG(ctx)
t.Fatal("r0 and r1 should have the same value")
}
}
func TestMigration0to1(t *testing.T) {
replicas, closeReplicas := makeNReplicas(t, 1, nil)
defer closeReplicas()
replica := replicas[0]
ctx := context.Background()
nItems := 200
var keys []ds.Key
// Add nItems
for i := 0; i < nItems; i++ {
k := ds.RandomKey()
keys = append(keys, k)
v := []byte(fmt.Sprintf("%d", i))
err := replica.Put(ctx, k, v)
if err != nil {
t.Fatal(err)
}
}
// Overwrite n/2 items 5 times to have multiple tombstones per key
// later...
for j := 0; j < 5; j++ {
for i := 0; i < nItems/2; i++ {
v := []byte(fmt.Sprintf("%d", i))
err := replica.Put(ctx, keys[i], v)
if err != nil {
t.Fatal(err)
}
}
}
// delete keys
for i := 0; i < nItems/2; i++ {
err := replica.Delete(ctx, keys[i])
if err != nil {
t.Fatal(err)
}
}
// And write them again
for i := 0; i < nItems/2; i++ {
err := replica.Put(ctx, keys[i], []byte("final value"))
if err != nil {
t.Fatal(err)
}
}
// And now we manually put the wrong value
for i := 0; i < nItems/2; i++ {
valueK := replica.set.valueKey(keys[i].String())
err := replica.set.store.Put(ctx, valueK, []byte("wrong value"))
if err != nil {
t.Fatal(err)
}
err = replica.set.setPriority(ctx, replica.set.store, keys[i].String(), 1)
if err != nil {
t.Fatal(err)
}
}
err := replica.migrate0to1(ctx)
if err != nil {
t.Fatal(err)