forked from dgraph-io/badger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
levels_test.go
1051 lines (972 loc) · 34 KB
/
levels_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 2019 Dgraph Labs, Inc. and Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package badger
import (
"fmt"
"math"
"testing"
"time"
"github.com/dgraph-io/badger/v2/options"
"github.com/dgraph-io/badger/v2/pb"
"github.com/dgraph-io/badger/v2/table"
"github.com/dgraph-io/badger/v2/y"
"github.com/stretchr/testify/require"
)
// createAndOpen creates a table with the given data and adds it to the given level.
func createAndOpen(db *DB, td []keyValVersion, level int) {
opts := table.Options{
BlockSize: db.opt.BlockSize,
BloomFalsePositive: db.opt.BloomFalsePositive,
ChkMode: options.NoVerification,
}
b := table.NewTableBuilder(opts)
defer b.Close()
// Add all keys and versions to the table.
for _, item := range td {
key := y.KeyWithTs([]byte(item.key), uint64(item.version))
val := y.ValueStruct{Value: []byte(item.val), Meta: item.meta}
b.Add(key, val, 0)
}
fname := table.NewFilename(db.lc.reserveFileID(), db.opt.Dir)
tab, err := table.CreateTable(fname, b)
if err != nil {
panic(err)
}
if err := db.manifest.addChanges([]*pb.ManifestChange{
newCreateChange(tab.ID(), level, 0, tab.CompressionType()),
}); err != nil {
panic(err)
}
db.lc.levels[level].Lock()
// Add table to the given level.
db.lc.levels[level].tables = append(db.lc.levels[level].tables, tab)
db.lc.levels[level].Unlock()
}
type keyValVersion struct {
key string
val string
version int
meta byte
}
func TestCheckOverlap(t *testing.T) {
t.Run("overlap", func(t *testing.T) {
// This test consists of one table on level 0 and one on level 1.
// There is an overlap amongst the tables but there is no overlap
// with rest of the levels.
t.Run("same keys", func(t *testing.T) {
runBadgerTest(t, nil, func(t *testing.T, db *DB) {
l0 := []keyValVersion{{"foo", "bar", 3, 0}}
l1 := []keyValVersion{{"foo", "bar", 2, 0}}
createAndOpen(db, l0, 0)
createAndOpen(db, l1, 1)
// Level 0 should overlap with level 0 tables.
require.True(t, db.lc.checkOverlap(db.lc.levels[0].tables, 0))
// Level 1 should overlap with level 0 tables (they have the same keys).
require.True(t, db.lc.checkOverlap(db.lc.levels[0].tables, 1))
// Level 2 and 3 should not overlap with level 0 tables.
require.False(t, db.lc.checkOverlap(db.lc.levels[0].tables, 2))
require.False(t, db.lc.checkOverlap(db.lc.levels[1].tables, 2))
require.False(t, db.lc.checkOverlap(db.lc.levels[0].tables, 3))
require.False(t, db.lc.checkOverlap(db.lc.levels[1].tables, 3))
})
})
t.Run("overlapping keys", func(t *testing.T) {
runBadgerTest(t, nil, func(t *testing.T, db *DB) {
l0 := []keyValVersion{{"a", "x", 1, 0}, {"b", "x", 1, 0}, {"foo", "bar", 3, 0}}
l1 := []keyValVersion{{"foo", "bar", 2, 0}}
createAndOpen(db, l0, 0)
createAndOpen(db, l1, 1)
// Level 0 should overlap with level 0 tables.
require.True(t, db.lc.checkOverlap(db.lc.levels[0].tables, 0))
require.True(t, db.lc.checkOverlap(db.lc.levels[1].tables, 1))
// Level 1 should overlap with level 0 tables, "foo" key is common.
require.True(t, db.lc.checkOverlap(db.lc.levels[0].tables, 1))
// Level 2 and 3 should not overlap with level 0 tables.
require.False(t, db.lc.checkOverlap(db.lc.levels[0].tables, 2))
require.False(t, db.lc.checkOverlap(db.lc.levels[0].tables, 3))
})
})
})
t.Run("non-overlapping", func(t *testing.T) {
runBadgerTest(t, nil, func(t *testing.T, db *DB) {
l0 := []keyValVersion{{"a", "x", 1, 0}, {"b", "x", 1, 0}, {"c", "bar", 3, 0}}
l1 := []keyValVersion{{"foo", "bar", 2, 0}}
createAndOpen(db, l0, 0)
createAndOpen(db, l1, 1)
// Level 1 should not overlap with level 0 tables
require.False(t, db.lc.checkOverlap(db.lc.levels[0].tables, 1))
// Level 2 and 3 should not overlap with level 0 tables.
require.False(t, db.lc.checkOverlap(db.lc.levels[0].tables, 2))
require.False(t, db.lc.checkOverlap(db.lc.levels[0].tables, 3))
})
})
}
func getAllAndCheck(t *testing.T, db *DB, expected []keyValVersion) {
db.View(func(txn *Txn) error {
opt := DefaultIteratorOptions
opt.AllVersions = true
opt.InternalAccess = true
it := txn.NewIterator(opt)
defer it.Close()
i := 0
for it.Rewind(); it.Valid(); it.Next() {
item := it.Item()
v, err := item.ValueCopy(nil)
require.NoError(t, err)
// fmt.Printf("k: %s v: %d val: %s\n", item.key, item.Version(), v)
require.Less(t, i, len(expected), "DB has more number of key than expected")
expect := expected[i]
require.Equal(t, expect.key, string(item.Key()), "expected key: %s actual key: %s",
expect.key, item.Key())
require.Equal(t, expect.val, string(v), "key: %s expected value: %s actual %s",
item.key, expect.val, v)
require.Equal(t, expect.version, int(item.Version()),
"key: %s expected version: %d actual %d", item.key, expect.version, item.Version())
require.Equal(t, expect.meta, item.meta,
"key: %s expected meta: %d meta %d", item.key, expect.meta, item.meta)
i++
}
require.Equal(t, len(expected), i, "keys examined should be equal to keys expected")
return nil
})
}
func TestCompaction(t *testing.T) {
// Disable compactions and keep single version of each key.
opt := DefaultOptions("").WithNumCompactors(0).WithNumVersionsToKeep(1)
opt.managedTxns = true
t.Run("level 0 to level 1", func(t *testing.T) {
runBadgerTest(t, &opt, func(t *testing.T, db *DB) {
l0 := []keyValVersion{{"foo", "bar", 3, 0}, {"fooz", "baz", 1, 0}}
l01 := []keyValVersion{{"foo", "bar", 2, 0}}
l1 := []keyValVersion{{"foo", "bar", 1, 0}}
// Level 0 has table l0 and l01.
createAndOpen(db, l0, 0)
createAndOpen(db, l01, 0)
// Level 1 has table l1.
createAndOpen(db, l1, 1)
// Set a high discard timestamp so that all the keys are below the discard timestamp.
db.SetDiscardTs(10)
getAllAndCheck(t, db, []keyValVersion{
{"foo", "bar", 3, 0}, {"foo", "bar", 2, 0},
{"foo", "bar", 1, 0}, {"fooz", "baz", 1, 0},
})
cdef := compactDef{
thisLevel: db.lc.levels[0],
nextLevel: db.lc.levels[1],
top: db.lc.levels[0].tables,
bot: db.lc.levels[1].tables,
t: db.lc.levelTargets(),
}
cdef.t.baseLevel = 1
require.NoError(t, db.lc.runCompactDef(-1, 0, cdef))
// foo version 2 should be dropped after compaction.
getAllAndCheck(t, db, []keyValVersion{{"foo", "bar", 3, 0}, {"fooz", "baz", 1, 0}})
})
})
t.Run("level 0 to level 1 with duplicates", func(t *testing.T) {
runBadgerTest(t, &opt, func(t *testing.T, db *DB) {
// We have foo version 3 on L0 because we gc'ed it.
l0 := []keyValVersion{{"foo", "barNew", 3, 0}, {"fooz", "baz", 1, 0}}
l01 := []keyValVersion{{"foo", "bar", 4, 0}}
l1 := []keyValVersion{{"foo", "bar", 3, 0}}
// Level 0 has table l0 and l01.
createAndOpen(db, l0, 0)
createAndOpen(db, l01, 0)
// Level 1 has table l1.
createAndOpen(db, l1, 1)
// Set a high discard timestamp so that all the keys are below the discard timestamp.
db.SetDiscardTs(10)
getAllAndCheck(t, db, []keyValVersion{
{"foo", "bar", 4, 0}, {"foo", "barNew", 3, 0},
{"fooz", "baz", 1, 0},
})
cdef := compactDef{
thisLevel: db.lc.levels[0],
nextLevel: db.lc.levels[1],
top: db.lc.levels[0].tables,
bot: db.lc.levels[1].tables,
t: db.lc.levelTargets(),
}
cdef.t.baseLevel = 1
require.NoError(t, db.lc.runCompactDef(-1, 0, cdef))
// foo version 3 (both) should be dropped after compaction.
getAllAndCheck(t, db, []keyValVersion{{"foo", "bar", 4, 0}, {"fooz", "baz", 1, 0}})
})
})
t.Run("level 0 to level 1 with lower overlap", func(t *testing.T) {
runBadgerTest(t, &opt, func(t *testing.T, db *DB) {
l0 := []keyValVersion{{"foo", "bar", 3, 0}, {"fooz", "baz", 1, 0}}
l01 := []keyValVersion{{"foo", "bar", 2, 0}}
l1 := []keyValVersion{{"foo", "bar", 1, 0}}
l2 := []keyValVersion{{"foo", "bar", 0, 0}}
// Level 0 has table l0 and l01.
createAndOpen(db, l0, 0)
createAndOpen(db, l01, 0)
// Level 1 has table l1.
createAndOpen(db, l1, 1)
// Level 2 has table l2.
createAndOpen(db, l2, 2)
// Set a high discard timestamp so that all the keys are below the discard timestamp.
db.SetDiscardTs(10)
getAllAndCheck(t, db, []keyValVersion{
{"foo", "bar", 3, 0}, {"foo", "bar", 2, 0}, {"foo", "bar", 1, 0},
{"foo", "bar", 0, 0}, {"fooz", "baz", 1, 0},
})
cdef := compactDef{
thisLevel: db.lc.levels[0],
nextLevel: db.lc.levels[1],
top: db.lc.levels[0].tables,
bot: db.lc.levels[1].tables,
t: db.lc.levelTargets(),
}
cdef.t.baseLevel = 1
require.NoError(t, db.lc.runCompactDef(-1, 0, cdef))
// foo version 2 and version 1 should be dropped after compaction.
getAllAndCheck(t, db, []keyValVersion{
{"foo", "bar", 3, 0}, {"foo", "bar", 0, 0}, {"fooz", "baz", 1, 0},
})
})
})
t.Run("level 1 to level 2", func(t *testing.T) {
runBadgerTest(t, &opt, func(t *testing.T, db *DB) {
l1 := []keyValVersion{{"foo", "bar", 3, 0}, {"fooz", "baz", 1, 0}}
l2 := []keyValVersion{{"foo", "bar", 2, 0}}
createAndOpen(db, l1, 1)
createAndOpen(db, l2, 2)
// Set a high discard timestamp so that all the keys are below the discard timestamp.
db.SetDiscardTs(10)
getAllAndCheck(t, db, []keyValVersion{
{"foo", "bar", 3, 0}, {"foo", "bar", 2, 0}, {"fooz", "baz", 1, 0},
})
cdef := compactDef{
thisLevel: db.lc.levels[1],
nextLevel: db.lc.levels[2],
top: db.lc.levels[1].tables,
bot: db.lc.levels[2].tables,
t: db.lc.levelTargets(),
}
cdef.t.baseLevel = 2
require.NoError(t, db.lc.runCompactDef(-1, 1, cdef))
// foo version 2 should be dropped after compaction.
getAllAndCheck(t, db, []keyValVersion{{"foo", "bar", 3, 0}, {"fooz", "baz", 1, 0}})
})
})
t.Run("level 1 to level 2 with delete", func(t *testing.T) {
t.Run("with overlap", func(t *testing.T) {
runBadgerTest(t, &opt, func(t *testing.T, db *DB) {
l1 := []keyValVersion{{"foo", "bar", 3, bitDelete}, {"fooz", "baz", 1, bitDelete}}
l2 := []keyValVersion{{"foo", "bar", 2, 0}}
l3 := []keyValVersion{{"foo", "bar", 1, 0}}
createAndOpen(db, l1, 1)
createAndOpen(db, l2, 2)
createAndOpen(db, l3, 3)
// Set a high discard timestamp so that all the keys are below the discard timestamp.
db.SetDiscardTs(10)
getAllAndCheck(t, db, []keyValVersion{
{"foo", "bar", 3, 1},
{"foo", "bar", 2, 0},
{"foo", "bar", 1, 0},
{"fooz", "baz", 1, 1},
})
cdef := compactDef{
thisLevel: db.lc.levels[1],
nextLevel: db.lc.levels[2],
top: db.lc.levels[1].tables,
bot: db.lc.levels[2].tables,
t: db.lc.levelTargets(),
}
cdef.t.baseLevel = 2
require.NoError(t, db.lc.runCompactDef(-1, 1, cdef))
// foo bar version 2 should be dropped after compaction. fooz
// baz version 1 will remain because overlap exists, which is
// expected because `hasOverlap` is only checked once at the
// beginning of `compactBuildTables` method.
// everything from level 1 is now in level 2.
getAllAndCheck(t, db, []keyValVersion{
{"foo", "bar", 3, bitDelete},
{"foo", "bar", 1, 0},
{"fooz", "baz", 1, 1},
})
cdef = compactDef{
thisLevel: db.lc.levels[2],
nextLevel: db.lc.levels[3],
top: db.lc.levels[2].tables,
bot: db.lc.levels[3].tables,
t: db.lc.levelTargets(),
}
cdef.t.baseLevel = 3
require.NoError(t, db.lc.runCompactDef(-1, 2, cdef))
// everything should be removed now
getAllAndCheck(t, db, []keyValVersion{})
})
})
t.Run("with bottom overlap", func(t *testing.T) {
runBadgerTest(t, &opt, func(t *testing.T, db *DB) {
l1 := []keyValVersion{{"foo", "bar", 3, bitDelete}}
l2 := []keyValVersion{{"foo", "bar", 2, 0}, {"fooz", "baz", 2, bitDelete}}
l3 := []keyValVersion{{"fooz", "baz", 1, 0}}
createAndOpen(db, l1, 1)
createAndOpen(db, l2, 2)
createAndOpen(db, l3, 3)
// Set a high discard timestamp so that all the keys are below the discard timestamp.
db.SetDiscardTs(10)
getAllAndCheck(t, db, []keyValVersion{
{"foo", "bar", 3, bitDelete},
{"foo", "bar", 2, 0},
{"fooz", "baz", 2, bitDelete},
{"fooz", "baz", 1, 0},
})
cdef := compactDef{
thisLevel: db.lc.levels[1],
nextLevel: db.lc.levels[2],
top: db.lc.levels[1].tables,
bot: db.lc.levels[2].tables,
t: db.lc.levelTargets(),
}
cdef.t.baseLevel = 2
require.NoError(t, db.lc.runCompactDef(-1, 1, cdef))
// the top table at L1 doesn't overlap L3, but the bottom table at L2
// does, delete keys should not be removed.
getAllAndCheck(t, db, []keyValVersion{
{"foo", "bar", 3, bitDelete},
{"fooz", "baz", 2, bitDelete},
{"fooz", "baz", 1, 0},
})
})
})
t.Run("without overlap", func(t *testing.T) {
runBadgerTest(t, &opt, func(t *testing.T, db *DB) {
l1 := []keyValVersion{{"foo", "bar", 3, bitDelete}, {"fooz", "baz", 1, bitDelete}}
l2 := []keyValVersion{{"fooo", "barr", 2, 0}}
createAndOpen(db, l1, 1)
createAndOpen(db, l2, 2)
// Set a high discard timestamp so that all the keys are below the discard timestamp.
db.SetDiscardTs(10)
getAllAndCheck(t, db, []keyValVersion{
{"foo", "bar", 3, 1}, {"fooo", "barr", 2, 0}, {"fooz", "baz", 1, 1},
})
cdef := compactDef{
thisLevel: db.lc.levels[1],
nextLevel: db.lc.levels[2],
top: db.lc.levels[1].tables,
bot: db.lc.levels[2].tables,
t: db.lc.levelTargets(),
}
cdef.t.baseLevel = 2
require.NoError(t, db.lc.runCompactDef(-1, 1, cdef))
// foo version 2 should be dropped after compaction.
getAllAndCheck(t, db, []keyValVersion{{"fooo", "barr", 2, 0}})
})
})
t.Run("with splits", func(t *testing.T) {
runBadgerTest(t, &opt, func(t *testing.T, db *DB) {
l1 := []keyValVersion{{"C", "bar", 3, bitDelete}}
l21 := []keyValVersion{{"A", "bar", 2, 0}}
l22 := []keyValVersion{{"B", "bar", 2, 0}}
l23 := []keyValVersion{{"C", "bar", 2, 0}}
l24 := []keyValVersion{{"D", "bar", 2, 0}}
l3 := []keyValVersion{{"fooz", "baz", 1, 0}}
createAndOpen(db, l1, 1)
createAndOpen(db, l21, 2)
createAndOpen(db, l22, 2)
createAndOpen(db, l23, 2)
createAndOpen(db, l24, 2)
createAndOpen(db, l3, 3)
// Set a high discard timestamp so that all the keys are below the discard timestamp.
db.SetDiscardTs(10)
getAllAndCheck(t, db, []keyValVersion{
{"A", "bar", 2, 0},
{"B", "bar", 2, 0},
{"C", "bar", 3, bitDelete},
{"C", "bar", 2, 0},
{"D", "bar", 2, 0},
{"fooz", "baz", 1, 0},
})
cdef := compactDef{
thisLevel: db.lc.levels[1],
nextLevel: db.lc.levels[2],
top: db.lc.levels[1].tables,
bot: db.lc.levels[2].tables,
t: db.lc.levelTargets(),
}
cdef.t.baseLevel = 2
require.NoError(t, db.lc.runCompactDef(-1, 1, cdef))
getAllAndCheck(t, db, []keyValVersion{
{"A", "bar", 2, 0},
{"B", "bar", 2, 0},
{"D", "bar", 2, 0},
{"fooz", "baz", 1, 0},
})
})
})
})
}
func TestCompactionTwoVersions(t *testing.T) {
// Disable compactions and keep two versions of each key.
opt := DefaultOptions("").WithNumCompactors(0).WithNumVersionsToKeep(2)
opt.managedTxns = true
t.Run("with overlap", func(t *testing.T) {
runBadgerTest(t, &opt, func(t *testing.T, db *DB) {
l1 := []keyValVersion{{"foo", "bar", 3, 0}, {"fooz", "baz", 1, bitDelete}}
l2 := []keyValVersion{{"foo", "bar", 2, 0}}
l3 := []keyValVersion{{"foo", "bar", 1, 0}}
createAndOpen(db, l1, 1)
createAndOpen(db, l2, 2)
createAndOpen(db, l3, 3)
// Set a high discard timestamp so that all the keys are below the discard timestamp.
db.SetDiscardTs(10)
getAllAndCheck(t, db, []keyValVersion{
{"foo", "bar", 3, 0},
{"foo", "bar", 2, 0},
{"foo", "bar", 1, 0},
{"fooz", "baz", 1, 1},
})
cdef := compactDef{
thisLevel: db.lc.levels[1],
nextLevel: db.lc.levels[2],
top: db.lc.levels[1].tables,
bot: db.lc.levels[2].tables,
t: db.lc.levelTargets(),
}
cdef.t.baseLevel = 2
require.NoError(t, db.lc.runCompactDef(-1, 1, cdef))
// Nothing should be dropped after compaction because number of
// versions to keep is 2.
getAllAndCheck(t, db, []keyValVersion{
{"foo", "bar", 3, 0},
{"foo", "bar", 2, 0},
{"foo", "bar", 1, 0},
{"fooz", "baz", 1, 1},
})
cdef = compactDef{
thisLevel: db.lc.levels[2],
nextLevel: db.lc.levels[3],
top: db.lc.levels[2].tables,
bot: db.lc.levels[3].tables,
t: db.lc.levelTargets(),
}
cdef.t.baseLevel = 3
require.NoError(t, db.lc.runCompactDef(-1, 2, cdef))
getAllAndCheck(t, db, []keyValVersion{
{"foo", "bar", 3, 0},
{"foo", "bar", 2, 0},
})
})
})
}
func TestCompactionAllVersions(t *testing.T) {
// Disable compactions and keep all versions of the each key.
opt := DefaultOptions("").WithNumCompactors(0).WithNumVersionsToKeep(math.MaxInt32)
opt.managedTxns = true
t.Run("without overlap", func(t *testing.T) {
runBadgerTest(t, &opt, func(t *testing.T, db *DB) {
l1 := []keyValVersion{{"foo", "bar", 3, 0}, {"fooz", "baz", 1, bitDelete}}
l2 := []keyValVersion{{"foo", "bar", 2, 0}}
l3 := []keyValVersion{{"foo", "bar", 1, 0}}
createAndOpen(db, l1, 1)
createAndOpen(db, l2, 2)
createAndOpen(db, l3, 3)
// Set a high discard timestamp so that all the keys are below the discard timestamp.
db.SetDiscardTs(10)
getAllAndCheck(t, db, []keyValVersion{
{"foo", "bar", 3, 0},
{"foo", "bar", 2, 0},
{"foo", "bar", 1, 0},
{"fooz", "baz", 1, 1},
})
cdef := compactDef{
thisLevel: db.lc.levels[1],
nextLevel: db.lc.levels[2],
top: db.lc.levels[1].tables,
bot: db.lc.levels[2].tables,
t: db.lc.levelTargets(),
}
cdef.t.baseLevel = 2
require.NoError(t, db.lc.runCompactDef(-1, 1, cdef))
// Nothing should be dropped after compaction because all versions
// should be kept.
getAllAndCheck(t, db, []keyValVersion{
{"foo", "bar", 3, 0},
{"foo", "bar", 2, 0},
{"foo", "bar", 1, 0},
{"fooz", "baz", 1, 1},
})
cdef = compactDef{
thisLevel: db.lc.levels[2],
nextLevel: db.lc.levels[3],
top: db.lc.levels[2].tables,
bot: db.lc.levels[3].tables,
t: db.lc.levelTargets(),
}
cdef.t.baseLevel = 3
require.NoError(t, db.lc.runCompactDef(-1, 2, cdef))
getAllAndCheck(t, db, []keyValVersion{
{"foo", "bar", 3, 0},
{"foo", "bar", 2, 0},
{"foo", "bar", 1, 0},
})
})
})
t.Run("without overlap", func(t *testing.T) {
runBadgerTest(t, &opt, func(t *testing.T, db *DB) {
l1 := []keyValVersion{{"foo", "bar", 3, bitDelete}, {"fooz", "baz", 1, bitDelete}}
l2 := []keyValVersion{{"fooo", "barr", 2, 0}}
createAndOpen(db, l1, 1)
createAndOpen(db, l2, 2)
// Set a high discard timestamp so that all the keys are below the discard timestamp.
db.SetDiscardTs(10)
getAllAndCheck(t, db, []keyValVersion{
{"foo", "bar", 3, 1}, {"fooo", "barr", 2, 0}, {"fooz", "baz", 1, 1},
})
cdef := compactDef{
thisLevel: db.lc.levels[1],
nextLevel: db.lc.levels[2],
top: db.lc.levels[1].tables,
bot: db.lc.levels[2].tables,
t: db.lc.levelTargets(),
}
cdef.t.baseLevel = 2
require.NoError(t, db.lc.runCompactDef(-1, 1, cdef))
// foo version 2 should be dropped after compaction.
getAllAndCheck(t, db, []keyValVersion{{"fooo", "barr", 2, 0}})
})
})
}
func TestDiscardTs(t *testing.T) {
// Disable compactions and keep single version of each key.
opt := DefaultOptions("").WithNumCompactors(0).WithNumVersionsToKeep(1)
opt.managedTxns = true
t.Run("all keys above discardTs", func(t *testing.T) {
runBadgerTest(t, &opt, func(t *testing.T, db *DB) {
l0 := []keyValVersion{{"foo", "bar", 4, 0}, {"fooz", "baz", 3, 0}}
l01 := []keyValVersion{{"foo", "bar", 3, 0}}
l1 := []keyValVersion{{"foo", "bar", 2, 0}}
// Level 0 has table l0 and l01.
createAndOpen(db, l0, 0)
createAndOpen(db, l01, 0)
// Level 1 has table l1.
createAndOpen(db, l1, 1)
// Set dicardTs to 1. All the keys are above discardTs.
db.SetDiscardTs(1)
getAllAndCheck(t, db, []keyValVersion{
{"foo", "bar", 4, 0}, {"foo", "bar", 3, 0},
{"foo", "bar", 2, 0}, {"fooz", "baz", 3, 0},
})
cdef := compactDef{
thisLevel: db.lc.levels[0],
nextLevel: db.lc.levels[1],
top: db.lc.levels[0].tables,
bot: db.lc.levels[1].tables,
t: db.lc.levelTargets(),
}
cdef.t.baseLevel = 1
require.NoError(t, db.lc.runCompactDef(-1, 0, cdef))
// No keys should be dropped.
getAllAndCheck(t, db, []keyValVersion{
{"foo", "bar", 4, 0}, {"foo", "bar", 3, 0},
{"foo", "bar", 2, 0}, {"fooz", "baz", 3, 0},
})
})
})
t.Run("some keys above discardTs", func(t *testing.T) {
runBadgerTest(t, &opt, func(t *testing.T, db *DB) {
l0 := []keyValVersion{
{"foo", "bar", 4, 0}, {"foo", "bar", 3, 0},
{"foo", "bar", 2, 0}, {"fooz", "baz", 2, 0},
}
l1 := []keyValVersion{{"foo", "bbb", 1, 0}}
createAndOpen(db, l0, 0)
createAndOpen(db, l1, 1)
// Set dicardTs to 3. foo2 and foo1 should be dropped.
db.SetDiscardTs(3)
getAllAndCheck(t, db, []keyValVersion{
{"foo", "bar", 4, 0}, {"foo", "bar", 3, 0}, {"foo", "bar", 2, 0},
{"foo", "bbb", 1, 0}, {"fooz", "baz", 2, 0},
})
cdef := compactDef{
thisLevel: db.lc.levels[0],
nextLevel: db.lc.levels[1],
top: db.lc.levels[0].tables,
bot: db.lc.levels[1].tables,
t: db.lc.levelTargets(),
}
cdef.t.baseLevel = 1
require.NoError(t, db.lc.runCompactDef(-1, 0, cdef))
// foo1 and foo2 should be dropped.
getAllAndCheck(t, db, []keyValVersion{
{"foo", "bar", 4, 0}, {"foo", "bar", 3, 0}, {"fooz", "baz", 2, 0},
})
})
})
t.Run("all keys below discardTs", func(t *testing.T) {
runBadgerTest(t, &opt, func(t *testing.T, db *DB) {
l0 := []keyValVersion{{"foo", "bar", 4, 0}, {"fooz", "baz", 3, 0}}
l01 := []keyValVersion{{"foo", "bar", 3, 0}}
l1 := []keyValVersion{{"foo", "bar", 2, 0}}
// Level 0 has table l0 and l01.
createAndOpen(db, l0, 0)
createAndOpen(db, l01, 0)
// Level 1 has table l1.
createAndOpen(db, l1, 1)
// Set dicardTs to 10. All the keys are below discardTs.
db.SetDiscardTs(10)
getAllAndCheck(t, db, []keyValVersion{
{"foo", "bar", 4, 0}, {"foo", "bar", 3, 0},
{"foo", "bar", 2, 0}, {"fooz", "baz", 3, 0},
})
cdef := compactDef{
thisLevel: db.lc.levels[0],
nextLevel: db.lc.levels[1],
top: db.lc.levels[0].tables,
bot: db.lc.levels[1].tables,
t: db.lc.levelTargets(),
}
cdef.t.baseLevel = 1
require.NoError(t, db.lc.runCompactDef(-1, 0, cdef))
// Only one version of every key should be left.
getAllAndCheck(t, db, []keyValVersion{{"foo", "bar", 4, 0}, {"fooz", "baz", 3, 0}})
})
})
}
// This is a test to ensure that the first entry with DiscardEarlierversion bit < DiscardTs
// is kept around (when numversionstokeep is infinite).
func TestDiscardFirstVersion(t *testing.T) {
opt := DefaultOptions("")
opt.NumCompactors = 0
opt.NumVersionsToKeep = math.MaxInt32
opt.managedTxns = true
runBadgerTest(t, &opt, func(t *testing.T, db *DB) {
l0 := []keyValVersion{{"foo", "bar", 1, 0}}
l01 := []keyValVersion{{"foo", "bar", 2, bitDiscardEarlierVersions}}
l02 := []keyValVersion{{"foo", "bar", 3, 0}}
l03 := []keyValVersion{{"foo", "bar", 4, 0}}
l04 := []keyValVersion{{"foo", "bar", 9, 0}}
l05 := []keyValVersion{{"foo", "bar", 10, bitDiscardEarlierVersions}}
// Level 0 has all the tables.
createAndOpen(db, l0, 0)
createAndOpen(db, l01, 0)
createAndOpen(db, l02, 0)
createAndOpen(db, l03, 0)
createAndOpen(db, l04, 0)
createAndOpen(db, l05, 0)
// Discard Time stamp is set to 7.
db.SetDiscardTs(7)
// Compact L0 to L1
cdef := compactDef{
thisLevel: db.lc.levels[0],
nextLevel: db.lc.levels[1],
top: db.lc.levels[0].tables,
bot: db.lc.levels[1].tables,
t: db.lc.levelTargets(),
}
cdef.t.baseLevel = 1
require.NoError(t, db.lc.runCompactDef(-1, 0, cdef))
// - Version 10, 9 lie above version 7 so they should be there.
// - Version 4, 3, 2 lie below the discardTs but they don't have the
// "bitDiscardEarlierVersions" versions set so they should not be removed because number
// of versions to keep is set to infinite.
// - Version 1 is below DiscardTS and below the first "bitDiscardEarlierVersions"
// marker so IT WILL BE REMOVED.
ExpectedKeys := []keyValVersion{
{"foo", "bar", 10, bitDiscardEarlierVersions},
{"foo", "bar", 9, 0},
{"foo", "bar", 4, 0},
{"foo", "bar", 3, 0},
{"foo", "bar", 2, bitDiscardEarlierVersions}}
getAllAndCheck(t, db, ExpectedKeys)
})
}
// This test ensures we don't stall when L1's size is greater than opt.LevelOneSize.
// We should stall only when L0 tables more than the opt.NumLevelZeroTableStall.
func TestL1Stall(t *testing.T) {
// TODO(ibrahim): Is this test still valid?
t.Skip()
opt := DefaultOptions("")
// Disable all compactions.
opt.NumCompactors = 0
// Number of level zero tables.
opt.NumLevelZeroTables = 3
// Addition of new tables will stall if there are 4 or more L0 tables.
opt.NumLevelZeroTablesStall = 4
// Level 1 size is 10 bytes.
opt.BaseLevelSize = 10
runBadgerTest(t, &opt, func(t *testing.T, db *DB) {
// Level 0 has 4 tables.
db.lc.levels[0].Lock()
db.lc.levels[0].tables = []*table.Table{createEmptyTable(db), createEmptyTable(db),
createEmptyTable(db), createEmptyTable(db)}
db.lc.levels[0].Unlock()
timeout := time.After(5 * time.Second)
done := make(chan bool)
// This is important. Set level 1 size more than the opt.LevelOneSize (we've set it to 10).
db.lc.levels[1].totalSize = 100
go func() {
tab := createEmptyTable(db)
require.NoError(t, db.lc.addLevel0Table(tab))
tab.DecrRef()
done <- true
}()
time.Sleep(time.Second)
db.lc.levels[0].Lock()
// Drop two tables from Level 0 so that addLevel0Table can make progress. Earlier table
// count was 4 which is equal to L0 stall count.
toDrop := db.lc.levels[0].tables[:2]
decrRefs(toDrop)
db.lc.levels[0].tables = db.lc.levels[0].tables[2:]
db.lc.levels[0].Unlock()
select {
case <-timeout:
t.Fatal("Test didn't finish in time")
case <-done:
}
})
}
func createEmptyTable(db *DB) *table.Table {
opts := table.Options{
BloomFalsePositive: db.opt.BloomFalsePositive,
ChkMode: options.NoVerification,
}
b := table.NewTableBuilder(opts)
defer b.Close()
// Add one key so that we can open this table.
b.Add(y.KeyWithTs([]byte("foo"), 1), y.ValueStruct{}, 0)
// Open table in memory to avoid adding changes to manifest file.
tab, err := table.OpenInMemoryTable(b.Finish(), db.lc.reserveFileID(), &opts)
if err != nil {
panic(err)
}
return tab
}
func TestL0Stall(t *testing.T) {
// TODO(ibrahim): Is this test still valid?
t.Skip()
opt := DefaultOptions("")
// Disable all compactions.
opt.NumCompactors = 0
// Number of level zero tables.
opt.NumLevelZeroTables = 3
// Addition of new tables will stall if there are 4 or more L0 tables.
opt.NumLevelZeroTablesStall = 4
runBadgerTest(t, &opt, func(t *testing.T, db *DB) {
db.lc.levels[0].Lock()
// Add NumLevelZeroTableStall+1 number of tables to level 0. This would fill up level
// zero and all new additions are expected to stall if L0 is in memory.
for i := 0; i < opt.NumLevelZeroTablesStall+1; i++ {
db.lc.levels[0].tables = append(db.lc.levels[0].tables, createEmptyTable(db))
}
db.lc.levels[0].Unlock()
timeout := time.After(5 * time.Second)
done := make(chan bool)
go func() {
tab := createEmptyTable(db)
require.NoError(t, db.lc.addLevel0Table(tab))
tab.DecrRef()
done <- true
}()
// Let it stall for a second.
time.Sleep(time.Second)
select {
case <-timeout:
t.Log("Timeout triggered")
// Mark this test as successful since L0 is in memory and the
// addition of new table to L0 is supposed to stall.
// Remove tables from level 0 so that the stalled
// compaction can make progress. This does not have any
// effect on the test. This is done so that the goroutine
// stuck on addLevel0Table can make progress and end.
db.lc.levels[0].Lock()
db.lc.levels[0].tables = nil
db.lc.levels[0].Unlock()
<-done
case <-done:
// The test completed before 5 second timeout. Mark it as successful.
t.Fatal("Test did not stall")
}
})
}
func TestLevelGet(t *testing.T) {
createLevel := func(db *DB, level int, data [][]keyValVersion) {
for _, v := range data {
createAndOpen(db, v, level)
}
}
type testData struct {
name string
// Keys on each level. keyValVersion[0] is the first table and so on.
levelData map[int][][]keyValVersion
expect []keyValVersion
}
test := func(t *testing.T, ti testData, db *DB) {
for level, data := range ti.levelData {
createLevel(db, level, data)
}
for _, item := range ti.expect {
key := y.KeyWithTs([]byte(item.key), uint64(item.version))
vs, err := db.get(key)
require.NoError(t, err)
require.Equal(t, item.val, string(vs.Value), "key:%s ver:%d", item.key, item.version)
}
}
tt := []testData{
{
"Normal",
map[int][][]keyValVersion{
0: { // Level 0 has 2 tables and each table has single key.
{{"foo", "bar10", 10, 0}},
{{"foo", "barSeven", 7, 0}},
},
1: { // Level 1 has 1 table with a single key.
{{"foo", "bar", 1, 0}},
},
},
[]keyValVersion{
{"foo", "bar", 1, 0},
{"foo", "barSeven", 7, 0},
{"foo", "bar10", 10, 0},
{"foo", "bar10", 11, 0}, // ver 11 doesn't exist so we should get bar10.
{"foo", "barSeven", 9, 0}, // ver 9 doesn't exist so we should get barSeven.
{"foo", "bar10", 100000, 0}, // ver doesn't exist so we should get bar10.
},
},
{"after gc",
map[int][][]keyValVersion{
0: { // Level 0 has 3 tables and each table has single key.
{{"foo", "barNew", 1, 0}}, // foo1 is above foo10 because of the GC.
{{"foo", "bar10", 10, 0}},
{{"foo", "barSeven", 7, 0}},
},
1: { // Level 1 has 1 table with a single key.
{{"foo", "bar", 1, 0}},
},
},
[]keyValVersion{
{"foo", "barNew", 1, 0},
{"foo", "barSeven", 7, 0},
{"foo", "bar10", 10, 0},
{"foo", "bar10", 11, 0}, // Should return biggest version.
},
},
{"after two gc",
map[int][][]keyValVersion{
0: { // Level 0 has 4 tables and each table has single key.
{{"foo", "barL0", 1, 0}}, // foo1 is above foo10 because of the GC.
{{"foo", "bar10", 10, 0}},
{{"foo", "barSeven", 7, 0}},
},
1: { // Level 1 has 1 table with a single key.
// Level 1 also has a foo because it was moved twice during GC.
{{"foo", "barL1", 1, 0}},
},
2: { // Level 1 has 1 table with a single key.
{{"foo", "bar", 1, 0}},
},
},
[]keyValVersion{
{"foo", "barL0", 1, 0},
{"foo", "barSeven", 7, 0},
{"foo", "bar10", 10, 0},
{"foo", "bar10", 11, 0}, // Should return biggest version.
},
},
}
for _, ti := range tt {
t.Run(ti.name, func(t *testing.T) {
runBadgerTest(t, nil, func(t *testing.T, db *DB) {
test(t, ti, db)
})
})
}
}
func TestKeyVersions(t *testing.T) {
inMemoryOpt := DefaultOptions("").
WithSyncWrites(false).
WithInMemory(true).
WithMemTableSize(4 << 20)
t.Run("disk", func(t *testing.T) {
t.Run("small table", func(t *testing.T) {
runBadgerTest(t, nil, func(t *testing.T, db *DB) {
l0 := make([]keyValVersion, 0)
for i := 0; i < 10; i++ {
l0 = append(l0, keyValVersion{fmt.Sprintf("%05d", i), "foo", 1, 0})
}
createAndOpen(db, l0, 0)
require.Equal(t, 1, len(db.KeySplits(nil)))
})
})
t.Run("medium table", func(t *testing.T) {
runBadgerTest(t, nil, func(t *testing.T, db *DB) {
l0 := make([]keyValVersion, 0)
for i := 0; i < 1000; i++ {
l0 = append(l0, keyValVersion{fmt.Sprintf("%05d", i), "foo", 1, 0})
}
createAndOpen(db, l0, 0)
require.Equal(t, 7, len(db.KeySplits(nil)))
})
})
t.Run("large table", func(t *testing.T) {
runBadgerTest(t, nil, func(t *testing.T, db *DB) {
l0 := make([]keyValVersion, 0)
for i := 0; i < 10000; i++ {