MCPcopy
hub / github.com/dgraph-io/badger / TestL0GCBug

Function TestL0GCBug

db2_test.go:610–690  ·  view source on GitHub ↗

The test ensures we don't lose data when badger is opened with KeepL0InMemory and GC is being done.

(t *testing.T)

Source from the content-addressed store, hash-verified

608// The test ensures we don't lose data when badger is opened with KeepL0InMemory and GC is being
609// done.
610func TestL0GCBug(t *testing.T) {
611 dir, err := ioutil.TempDir("", "badger-test")
612 require.NoError(t, err)
613 defer removeDir(dir)
614
615 // Do not change any of the options below unless it's necessary.
616 opts := getTestOptions(dir)
617 opts.NumLevelZeroTables = 50
618 opts.NumLevelZeroTablesStall = 51
619 opts.ValueLogMaxEntries = 2
620 opts.ValueThreshold = 2
621 opts.KeepL0InMemory = true
622 // Setting LoadingMode to mmap seems to cause segmentation fault while closing DB.
623 opts.ValueLogLoadingMode = options.FileIO
624 opts.TableLoadingMode = options.FileIO
625
626 db1, err := Open(opts)
627 require.NoError(t, err)
628 key := func(i int) []byte {
629 return []byte(fmt.Sprintf("%10d", i))
630 }
631 val := []byte{1, 1, 1, 1, 1, 1, 1, 1}
632 // Insert 100 entries. This will create about 50*3 vlog files and 6 SST files.
633 for i := 0; i < 3; i++ {
634 for j := 0; j < 100; j++ {
635 err = db1.Update(func(txn *Txn) error {
636 return txn.SetEntry(NewEntry(key(j), val))
637 })
638 require.NoError(t, err)
639 }
640 }
641 // Run value log GC multiple times. This would ensure at least
642 // one value log file is garbage collected.
643 success := 0
644 for i := 0; i < 10; i++ {
645 err := db1.RunValueLogGC(0.01)
646 if err == nil {
647 success++
648 }
649 if err != nil && err != ErrNoRewrite {
650 t.Fatalf(err.Error())
651 }
652 }
653 // Ensure alteast one GC call was successful.
654 require.NotZero(t, success)
655 // CheckKeys reads all the keys previously stored.
656 checkKeys := func(db *DB) {
657 for i := 0; i < 100; i++ {
658 err := db.View(func(txn *Txn) error {
659 item, err := txn.Get(key(i))
660 require.NoError(t, err)
661 val1 := getItemValue(t, item)
662 require.Equal(t, val, val1)
663 return nil
664 })
665 require.NoError(t, err)
666 }
667 }

Callers

nothing calls this directly

Calls 15

removeDirFunction · 0.85
getTestOptionsFunction · 0.85
OpenFunction · 0.85
NewEntryFunction · 0.85
getItemValueFunction · 0.85
checkKeysFunction · 0.85
RunValueLogGCMethod · 0.80
ViewMethod · 0.80
keyFunction · 0.70
CloseMethod · 0.65
UpdateMethod · 0.45
SetEntryMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…