-
Notifications
You must be signed in to change notification settings - Fork 61
/
persister_test.go
1383 lines (1182 loc) · 32.8 KB
/
persister_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
// Copyright 2016-Present Couchbase, Inc.
//
// Use of this software is governed by the Business Source License included
// in the file licenses/BSL-Couchbase.txt. As of the Change Date specified
// in that file, in accordance with the Business Source License, use of this
// software will be governed by the Apache License, Version 2.0, included in
// the file licenses/APL2.txt.
package moss
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"sort"
"strings"
"sync"
"testing"
"time"
)
// Implementation of mock lower-level iterator, using map that's
// cloned and sorted on creation.
type TestPersisterIterator struct {
pos int
kvpairs map[string][]byte // immutable.
keys []string // immutable, sorted.
endkey string
}
// NewTestPersisterIterator returns an iterator, cloning the provided
// kvpairs.
func NewTestPersisterIterator(kvpairs map[string][]byte,
startkey, endkey string) *TestPersisterIterator {
rv := &TestPersisterIterator{
kvpairs: kvpairs,
endkey: endkey,
}
for k := range rv.kvpairs {
rv.keys = append(rv.keys, k)
}
sort.Strings(rv.keys)
rv.pos = sort.SearchStrings(rv.keys, string(startkey))
return rv
}
func (i *TestPersisterIterator) Close() error {
i.kvpairs = nil
i.keys = nil
return nil
}
func (i *TestPersisterIterator) Next() error {
i.pos++
if i.pos >= len(i.keys) {
return ErrIteratorDone
}
return nil
}
func (i *TestPersisterIterator) SeekTo(seekToKey []byte) error {
return naiveSeekTo(i, seekToKey, 0)
}
func (i *TestPersisterIterator) Current() ([]byte, []byte, error) {
if i.pos >= len(i.keys) {
return nil, nil, ErrIteratorDone
}
k := i.keys[i.pos]
if i.endkey != "" && strings.Compare(k, i.endkey) >= 0 {
return nil, nil, ErrIteratorDone
}
return []byte(k), i.kvpairs[k], nil
}
func (i *TestPersisterIterator) CurrentEx() (entryEx EntryEx,
key, val []byte, err error) {
k, v, err := i.Current()
if err != nil {
return EntryEx{OperationSet}, nil, nil, err
}
return EntryEx{OperationSet}, k, v, err
}
// Implementation of mock lower-level test persister, using a map
// that's cloned on updates and with key sorting whenever an iterator
// is needed.
type TestPersister struct {
// stable snapshots through writes blocking reads
mutex sync.RWMutex
kvpairs map[string][]byte
}
// NewTestPersister returns a TestPersister instance that can be used
// to test lower-level persistence features.
func NewTestPersister() *TestPersister {
return &TestPersister{
kvpairs: map[string][]byte{},
}
}
func (p *TestPersister) cloneLOCKED() *TestPersister {
c := NewTestPersister()
for k, v := range p.kvpairs {
c.kvpairs[k] = v
}
return c
}
func (p *TestPersister) Close() error {
// ensure any writes in progress finish
p.mutex.Lock()
defer p.mutex.Unlock()
p.kvpairs = nil
return nil
}
func (p *TestPersister) Get(key []byte,
readOptions ReadOptions) ([]byte, error) {
p.mutex.RLock()
defer p.mutex.RUnlock()
return p.kvpairs[string(key)], nil
}
func (p *TestPersister) StartIterator(
startKeyInclusive, endKeyExclusive []byte,
iteratorOptions IteratorOptions) (Iterator, error) {
p.mutex.RLock() // closing iterator unlocks
defer p.mutex.RUnlock()
return NewTestPersisterIterator(p.cloneLOCKED().kvpairs,
string(startKeyInclusive), string(endKeyExclusive)), nil
}
func (p *TestPersister) Update(higher Snapshot) (*TestPersister, error) {
p.mutex.Lock()
defer p.mutex.Unlock()
c := p.cloneLOCKED()
if higher != nil {
iter, err := higher.StartIterator(nil, nil, IteratorOptions{
IncludeDeletions: true,
SkipLowerLevel: true,
})
if err != nil {
return nil, err
}
defer iter.Close()
var readOptions ReadOptions
for {
ex, key, val, err := iter.CurrentEx()
if err == ErrIteratorDone {
break
}
if err != nil {
return nil, err
}
switch ex.Operation {
case OperationSet:
c.kvpairs[string(key)] = val
case OperationDel:
delete(c.kvpairs, string(key))
case OperationMerge:
val, err = higher.Get(key, readOptions)
if err != nil {
return nil, err
}
if val != nil {
c.kvpairs[string(key)] = val
} else {
delete(c.kvpairs, string(key))
}
default:
return nil, fmt.Errorf("moss TestPersister, update,"+
" unexpected operation, ex: %v", ex)
}
err = iter.Next()
if err == ErrIteratorDone {
break
}
if err != nil {
return nil, err
}
}
}
return c, nil
}
// ----------------------------------------------------
// TestPersister tests that the persister is invoked as expected.
func Test1Persister(t *testing.T) {
runTestPersister(t, 1)
}
func Test10Persister(t *testing.T) {
runTestPersister(t, 10)
}
func Test1000Persister(t *testing.T) {
runTestPersister(t, 1000)
}
func runTestPersister(t *testing.T, numItems int) {
// create a new instance of our mock lower-level persister
lowerLevelPersister := newTestPersister()
lowerLevelUpdater := func(higher Snapshot) (Snapshot, error) {
p, err := lowerLevelPersister.Update(higher)
if err != nil {
return nil, err
}
lowerLevelPersister = p
return p, nil
}
persisterCh := make(chan string)
onEvent := func(event Event) {
if event.Kind == EventKindPersisterProgress {
persisterCh <- "persisterProgress"
}
}
// create new collection configured to use lower level persister
m, err := NewCollection(
CollectionOptions{
LowerLevelInit: lowerLevelPersister,
LowerLevelUpdate: lowerLevelUpdater,
OnEvent: onEvent,
})
if err != nil || m == nil {
t.Fatalf("expected moss")
}
// FIXME possibly replace start with manual persister invocations?
// this would require some refactoring
err = m.Start()
if err != nil {
t.Fatalf("error starting moss: %v", err)
}
// create new batch to set some keys
b, err := m.NewBatch(0, 0)
if err != nil {
t.Fatalf("error creating new batch: %v", err)
}
// also create a child batch
childB, err := b.NewChildCollectionBatch("child1", BatchOptions{0, 0})
if err != nil {
t.Fatalf("error creating new child batch: %v", err)
}
itemLoader := func(b Batch, numItems int) {
// put numItems in
for i := 0; i < numItems; i++ {
k := fmt.Sprintf("%d", i)
b.Set([]byte(k), []byte(k))
}
}
itemLoader(b, numItems)
itemLoader(childB, numItems)
err = m.ExecuteBatch(b, WriteOptions{})
if err != nil {
t.Fatalf("error executing batch: %v", err)
}
ss0, err := m.Snapshot()
if err != nil || ss0 == nil {
t.Fatalf("error snapshoting: %v", err)
}
childNames, err := ss0.ChildCollectionNames()
if err != nil {
t.Fatalf("error getting child collection names: %v", err)
}
if len(childNames) != 1 {
t.Fatalf("Unable to retrieve child snapshot")
}
childSnap, err := ss0.ChildCollectionSnapshot("child1")
if err != nil || ss0 == nil {
t.Fatalf("error getting child snapshot: %v", err)
}
// cleanup that batch
err = b.Close()
if err != nil {
t.Fatalf("error closing batch: %v", err)
}
ss1, err := m.Snapshot()
if err != nil || ss1 == nil {
t.Fatalf("error snapshoting: %v", err)
}
// wait for persister to run
<-persisterCh
ss2, err := m.Snapshot()
if err != nil || ss2 == nil {
t.Fatalf("error snapshoting: %v", err)
}
checkSnapshot := func(msg string, ss Snapshot, expectedNum int) {
for i := 0; i < numItems; i++ {
k := fmt.Sprintf("%d", i)
var v []byte
v, err = ss.Get([]byte(k), ReadOptions{})
if err != nil {
t.Fatalf("error %s getting key: %s, %v", msg, k, err)
}
if string(v) != k {
t.Errorf("expected %s value for key: %s to be %s, got %s", msg, k, k, v)
}
}
var iter Iterator
iter, err = ss.StartIterator(nil, nil, IteratorOptions{})
if err != nil {
t.Fatalf("error %s checkSnapshot iter, err: %v", msg, err)
}
n := 0
var lastKey []byte
for {
var ex EntryEx
var key, val []byte
ex, key, val, err = iter.CurrentEx()
if err == ErrIteratorDone {
break
}
if err != nil {
t.Fatalf("error %s iter currentEx, err: %v", msg, err)
}
n++
if ex.Operation != OperationSet {
t.Fatalf("error %s iter op, ex: %v, err: %v", msg, ex, err)
}
cmp := bytes.Compare(lastKey, key)
if cmp >= 0 {
t.Fatalf("error %s iter cmp: %v, err: %v", msg, cmp, err)
}
if bytes.Compare(key, val) != 0 {
t.Fatalf("error %s iter key != val: %v, %v", msg, key, val)
}
lastKey = key
err = iter.Next()
if err == ErrIteratorDone {
break
}
if err != nil {
t.Fatalf("error %s iter next, err: %v", msg, err)
}
}
if n != expectedNum {
t.Fatalf("error %s iter expectedNum: %d, got: %d", msg, expectedNum, n)
}
iter.Close()
}
checkSnapshot("lowerLevelPersister", lowerLevelPersister, numItems)
checkSnapshot("ss0", ss0, numItems)
checkSnapshot("ss1", ss1, numItems)
checkSnapshot("ss2", ss2, numItems)
checkSnapshot("ss0:child1", childSnap, numItems)
// cleanup that batch
err = b.Close()
if err != nil {
t.Fatalf("error closing batch: %v", err)
}
// open new batch
b, err = m.NewBatch(0, 0)
if err != nil {
t.Fatalf("error creating new batch: %v", err)
}
// delete the values we just set
for i := 0; i < numItems; i++ {
k := fmt.Sprintf("%d", i)
b.Del([]byte(k))
}
err = b.DelChildCollection("child1")
if err != nil {
t.Fatalf("error deleting child collection: %v", err)
}
err = m.ExecuteBatch(b, WriteOptions{})
if err != nil {
t.Fatalf("error executing batch: %v", err)
}
ssd0, err := m.Snapshot()
if err != nil || ssd0 == nil {
t.Fatalf("error snapshoting: %v", err)
}
childNames, err = ssd0.ChildCollectionNames()
if len(childNames) > 0 {
t.Fatalf("error child snapshot not deleted: %v", err)
}
// cleanup that batch
err = b.Close()
if err != nil {
t.Fatalf("error closing batch: %v", err)
}
ssd1, err := m.Snapshot()
if err != nil || ssd1 == nil {
t.Fatalf("error snapshoting: %v", err)
}
<-persisterCh
go func() {
for range persisterCh { /* EAT */
}
}()
ssd2, err := m.Snapshot()
if err != nil || ssd2 == nil {
t.Fatalf("error snapshoting: %v", err)
}
// check that values are now gone
checkGetsGone := func(ss Snapshot) {
for i := 0; i < numItems; i++ {
k := fmt.Sprintf("%d", i)
var v []byte
v, err = ss.Get([]byte(k), ReadOptions{})
if err != nil {
t.Fatalf("error getting key: %s, %v", k, err)
}
if v != nil {
t.Errorf("expected no value for key: %s, got %s", k, v)
}
}
}
checkGetsGone(lowerLevelPersister)
checkGetsGone(ssd0)
checkGetsGone(ssd1)
checkGetsGone(ssd2)
// Check that our old snapshots are still stable.
checkSnapshot("ss0", ss0, numItems)
checkSnapshot("ss1", ss1, numItems)
checkSnapshot("ss2", ss2, numItems)
// cleanup moss
err = m.Close()
if err != nil {
t.Fatalf("error closing moss: %v", err)
}
}
// TestPersisterError ensures that if the provided LowerLevelUpdate
// method returns an error, the configured OnError callback is
// invoked
func TestPersisterError(t *testing.T) {
onErrorCh := make(chan string)
customOnError := func(err error) {
onErrorCh <- "error expected!"
}
// create a new instance of our mock lower-level persister
lowerLevelPersister := newTestPersister()
lowerLevelUpdater := func(higher Snapshot) (Snapshot, error) {
return nil, fmt.Errorf("test error")
}
gotPersistence := false
onEvent := func(event Event) {
if event.Kind == EventKindPersisterProgress {
gotPersistence = true
}
}
// create new collection configured to use lower level persister
m, err := NewCollection(
CollectionOptions{
LowerLevelInit: lowerLevelPersister,
LowerLevelUpdate: lowerLevelUpdater,
OnError: customOnError,
OnEvent: onEvent,
})
if err != nil || m == nil {
t.Fatalf("expected moss")
}
// FIXME possibly replace start with manual persister invocations?
// this would require some refactoring
err = m.Start()
if err != nil {
t.Fatalf("error starting moss: %v", err)
}
// create new batch to set some keys
b, err := m.NewBatch(0, 0)
if err != nil {
t.Fatalf("error creating new batch: %v", err)
}
// put 100 values in
for i := 0; i < 1000; i++ {
k := fmt.Sprintf("%d", i)
b.Set([]byte(k), []byte(k))
}
err = m.ExecuteBatch(b, WriteOptions{})
if err != nil {
t.Fatalf("error executing batch: %v", err)
}
// wait for persister to run
msg := <-onErrorCh
if msg != "error expected!" {
t.Errorf("expected error callback")
}
if gotPersistence {
t.Errorf("expected no persistence due to error")
}
}
// -----------------------------------------------------------------------------
// implementation of mock lower-level test persister and iterator,
// with COW, using map that's cloned on updates and with key sorting
// whenever an iterator is needed.
type testPersisterIterator struct {
pos int
kvpairs map[string][]byte // immutable.
keys []string // immutable, sorted.
endkey string
}
func newTestPersisterIterator(kvpairs map[string][]byte,
startkey, endkey string) *testPersisterIterator {
rv := &testPersisterIterator{
kvpairs: kvpairs,
endkey: endkey,
}
for k := range rv.kvpairs {
rv.keys = append(rv.keys, k)
}
sort.Strings(rv.keys)
rv.pos = sort.SearchStrings(rv.keys, string(startkey))
return rv
}
func (i *testPersisterIterator) Close() error {
i.kvpairs = nil
i.keys = nil
return nil
}
func (i *testPersisterIterator) Next() error {
i.pos++
if i.pos >= len(i.keys) {
return ErrIteratorDone
}
return nil
}
func (i *testPersisterIterator) SeekTo(seekToKey []byte) error {
return naiveSeekTo(i, seekToKey, 0)
}
func (i *testPersisterIterator) Current() ([]byte, []byte, error) {
if i.pos >= len(i.keys) {
return nil, nil, ErrIteratorDone
}
k := i.keys[i.pos]
if i.endkey != "" && strings.Compare(k, i.endkey) >= 0 {
return nil, nil, ErrIteratorDone
}
return []byte(k), i.kvpairs[k], nil
}
func (i *testPersisterIterator) CurrentEx() (entryEx EntryEx,
key, val []byte, err error) {
k, v, err := i.Current()
if err != nil {
return EntryEx{OperationSet}, nil, nil, err
}
return EntryEx{OperationSet}, k, v, err
}
// Implements the moss.Snapshot interface
type testPersister struct {
// stable snapshots through writes blocking reads
mutex sync.RWMutex
kvpairs map[string][]byte
childSnapshots map[string]*testPersister
}
func newTestPersister() *testPersister {
return &testPersister{
kvpairs: map[string][]byte{},
childSnapshots: make(map[string]*testPersister),
}
}
func (p *testPersister) cloneLOCKED() *testPersister {
c := newTestPersister()
for k, v := range p.kvpairs {
c.kvpairs[k] = v
}
return c
}
// ChildCollectionNames returns an array of child collection name strings.
func (p *testPersister) ChildCollectionNames() ([]string, error) {
var childCollections = make([]string, len(p.childSnapshots))
idx := 0
for name := range p.childSnapshots {
childCollections[idx] = name
idx++
}
return childCollections, nil
}
// ChildCollectionSnapshot returns a Snapshot on a given child
// collection by its name.
func (p *testPersister) ChildCollectionSnapshot(childCollectionName string) (
Snapshot, error) {
childSnapshot, exists := p.childSnapshots[childCollectionName]
if !exists {
return nil, ErrNoSuchCollection
}
return childSnapshot, nil
}
func (p *testPersister) Close() error {
// ensure any writes in progress finish
p.mutex.Lock()
defer p.mutex.Unlock()
p.kvpairs = nil
return nil
}
func (p *testPersister) Get(key []byte,
readOptions ReadOptions) ([]byte, error) {
p.mutex.RLock()
defer p.mutex.RUnlock()
return p.kvpairs[string(key)], nil
}
func (p *testPersister) StartIterator(
startKeyInclusive, endKeyExclusive []byte,
iteratorOptions IteratorOptions) (Iterator, error) {
p.mutex.RLock() // closing iterator unlocks
defer p.mutex.RUnlock()
return newTestPersisterIterator(p.cloneLOCKED().kvpairs,
string(startKeyInclusive), string(endKeyExclusive)), nil
}
func (p *testPersister) Update(higher Snapshot) (*testPersister, error) {
p.mutex.Lock()
defer p.mutex.Unlock()
c := p.cloneLOCKED()
if higher != nil {
iter, err := higher.StartIterator(nil, nil, IteratorOptions{
IncludeDeletions: true,
SkipLowerLevel: true,
})
if err != nil {
return nil, err
}
defer iter.Close()
var readOptions ReadOptions
for {
ex, key, val, err := iter.CurrentEx()
if err == ErrIteratorDone {
break
}
if err != nil {
return nil, err
}
switch ex.Operation {
case OperationSet:
c.kvpairs[string(key)] = val
case OperationDel:
delete(c.kvpairs, string(key))
case OperationMerge:
val, err = higher.Get(key, readOptions)
if err != nil {
return nil, err
}
if val != nil {
c.kvpairs[string(key)] = val
} else {
delete(c.kvpairs, string(key))
}
default:
return nil, fmt.Errorf("moss testPersister, update,"+
" unexpected operation, ex: %v", ex)
}
err = iter.Next()
if err == ErrIteratorDone {
break
}
if err != nil {
return nil, err
}
}
}
return c, nil
}
func TestPersistMergeOps_MB19667(t *testing.T) {
// Need to arrange that...
// - stack dirty top = empty
// - stack dirty mid = [ various merge ops Z ]
// - stack dirty base = [ more merge ops Y ]
// - lower-level has stuff (X)
//
// Then persister runs and...
// - stack dirty base = [ (empty) ]
// - lower-level has more stuff (X + Y)
//
// But, stack dirty mid was (incorrectly) pointing at old lower
// level snapshot (X), which doesn't have anything from Y. Then,
// when persister runs again, you'd end up incorrectly with X + Z
// when you wanted X + Y + Z.
//
var mlock sync.Mutex
events := map[EventKind]int{}
var eventCh chan EventKind
var onPersistCh chan bool
mo := &MergeOperatorStringAppend{Sep: ":"}
lowerLevelPersister := newTestPersister()
lowerLevelUpdater := func(higher Snapshot) (Snapshot, error) {
if onPersistCh != nil {
<-onPersistCh
}
p, err := lowerLevelPersister.Update(higher)
if err != nil {
return nil, err
}
lowerLevelPersister = p
p.mutex.RLock()
defer p.mutex.RUnlock()
return p.cloneLOCKED(), nil
}
m, err := NewCollection(CollectionOptions{
MergeOperator: mo,
LowerLevelInit: lowerLevelPersister,
LowerLevelUpdate: lowerLevelUpdater,
OnEvent: func(e Event) {
mlock.Lock()
events[e.Kind]++
eventCh2 := eventCh
mlock.Unlock()
if eventCh2 != nil {
eventCh2 <- e.Kind
}
},
})
if err != nil || m == nil {
t.Errorf("expected moss")
}
mc := m.(*collection)
// Note that we don't Start()'ed the collection, so it doesn't
// have the merger background goroutines runnning. But we do
// kickoff the background persister goroutine...
go mc.runPersister()
mergeVal := func(v string) {
var b Batch
b, err = m.NewBatch(0, 0)
if err != nil || b == nil {
t.Errorf("expected b ok")
}
b.Merge([]byte("k"), []byte(v))
err = m.ExecuteBatch(b, WriteOptions{})
if err != nil {
t.Errorf("expected execute batch ok")
}
b.Close()
}
mergeVal("X")
// Pretend to be the merger, moving stack dirty top into base, and
// notify and wait for the persister.
mc.m.Lock()
mc.stackDirtyBase = mc.stackDirtyTop
mc.stackDirtyTop = nil
waitDirtyOutgoingCh := make(chan struct{})
mc.waitDirtyOutgoingCh = waitDirtyOutgoingCh
mc.stackDirtyBaseCond.Broadcast()
mc.m.Unlock()
<-waitDirtyOutgoingCh
// At this point...
// - stackDirtyTop : empty
// - stackDirtyMid : empty
// - stackDirtyBase : empty
// - lowerLevel : X
mc.m.Lock()
if mc.stackDirtyTop != nil || mc.stackDirtyMid != nil || mc.stackDirtyBase != nil {
t.Errorf("expected X state")
}
if mc.lowerLevelSnapshot == nil {
t.Errorf("unexpected llss X state")
}
v, err := mc.lowerLevelSnapshot.Get([]byte("k"), ReadOptions{})
if err != nil {
t.Errorf("expected get ok")
}
if string(v) != ":X" {
t.Errorf("expected :X, got: %s", v)
}
mc.m.Unlock()
// --------------------------------------------
mergeVal("Y")
// Pretend to be the merger, moving stack dirty top into base,
// but don't notify the persister.
stackDirtyMid, _, _, _, _ :=
mc.snapshot(snapshotSkipClean|snapshotSkipDirtyBase, nil, false)
mc.m.Lock()
mc.stackDirtyBase = stackDirtyMid
mc.stackDirtyTop = nil
mc.m.Unlock()
// At this point...
// - stackDirtyTop : empty
// - stackDirtyMid : empty
// - stackDirtyBase : Y (and points to lowerLevel X)
// - lowerLevel : X
mc.m.Lock()
if mc.stackDirtyTop != nil || mc.stackDirtyMid != nil || mc.stackDirtyBase == nil {
t.Errorf("expected X/Y state")
}
if mc.lowerLevelSnapshot == nil {
t.Errorf("unexpected llss X/Y state")
}
v, err = mc.lowerLevelSnapshot.Get([]byte("k"), ReadOptions{})
if err != nil {
t.Errorf("expected get ok")
}
if string(v) != ":X" {
t.Errorf("expected :X, got: %s", v)
}
mc.m.Unlock()
// --------------------------------------------
mergeVal("Z")
// Pretend to be the merger, moving stack dirty top into mid,
// but don't notify the persister.
stackDirtyMid, _, _, _, _ =
mc.snapshot(snapshotSkipClean|snapshotSkipDirtyBase, nil, false)
mc.m.Lock()
mc.stackDirtyMid = stackDirtyMid
mc.stackDirtyTop = nil
mc.m.Unlock()
// At this point...
// - stackDirtyTop : empty
// - stackDirtyMid : Z (and points to lowerLevel X)
// - stackDirtyBase : Y (and points to lowerLevel X)
// - lowerLevel : X
mc.m.Lock()
if mc.stackDirtyTop != nil || mc.stackDirtyMid == nil || mc.stackDirtyBase == nil {
t.Errorf("expected X/Y/Z state")
}
if mc.lowerLevelSnapshot == nil {
t.Errorf("unexpected llss X/Y/Z state")
}
v, err = mc.lowerLevelSnapshot.Get([]byte("k"), ReadOptions{})
if err != nil {
t.Errorf("expected get ok")
}
if string(v) != ":X" {
t.Errorf("expected :X, got: %s", v)
}
if len(mc.stackDirtyMid.a) != 1 {
t.Errorf("expected stackDirtyMid len of 1")
}
if mc.stackDirtyMid.lowerLevelSnapshot == nil {
t.Errorf("expected stackDirtyMid.lowerLevelSnapshot")
}
v, err = mc.stackDirtyMid.lowerLevelSnapshot.Get([]byte("k"), ReadOptions{})
if err != nil {
t.Errorf("expected get ok")
}
if string(v) != ":X" {
t.Errorf("expected :X, got: %s", v)
}
if mc.stackDirtyBase.lowerLevelSnapshot == nil {
t.Errorf("expected stackDirtyBase.lowerLevelSnapshot")
}
v, err = mc.stackDirtyBase.lowerLevelSnapshot.Get([]byte("k"), ReadOptions{})
if err != nil {
t.Errorf("expected get ok")
}
if string(v) != ":X" {
t.Errorf("expected :X, got: %s", v)
}
if mc.stackDirtyBase.lowerLevelSnapshot != mc.stackDirtyMid.lowerLevelSnapshot {
t.Errorf("expected same snapshots")
}
if mc.stackDirtyBase.lowerLevelSnapshot != mc.lowerLevelSnapshot {
t.Errorf("expected same snapshots")
}
mc.m.Unlock()
// --------------------------------------------
checkVal := func(msg, expected string) {
var ss Snapshot
ss, err = m.Snapshot()
if err != nil {
t.Errorf("%s - expected ss ok", msg)
}
var getv []byte
getv, err = ss.Get([]byte("k"), ReadOptions{})
if err != nil || string(getv) != expected {
t.Errorf("%s - expected Get %s, got: %s, err: %v", msg, expected, getv, err)
}
var iter Iterator
iter, err = ss.StartIterator(nil, nil, IteratorOptions{})
if err != nil || iter == nil {
t.Errorf("%s - expected iter", msg)
}
var k []byte
k, v, err = iter.Current()
if err != nil {
t.Errorf("%s - expected iter current no err", msg)
}
if string(k) != "k" {
t.Errorf("%s - expected iter current key k", msg)
}
if string(v) != expected {
t.Errorf("%s - expected iter current val expected: %v, got: %s", msg, expected, v)
}
if iter.Next() != ErrIteratorDone {
t.Errorf("%s - expected only 1 value in iterator", msg)
}
ss.Close()
}