Regression test for https://github.com/dgraph-io/badger/issues/926
(t *testing.T)
| 1069 | |
| 1070 | // Regression test for https://github.com/dgraph-io/badger/issues/926 |
| 1071 | func TestDiscardStatsMove(t *testing.T) { |
| 1072 | dir, err := ioutil.TempDir("", "badger-test") |
| 1073 | require.NoError(t, err) |
| 1074 | ops := getTestOptions(dir) |
| 1075 | ops.ValueLogMaxEntries = 1 |
| 1076 | db, err := Open(ops) |
| 1077 | require.NoError(t, err) |
| 1078 | |
| 1079 | stat := make(map[uint32]int64, ops.ValueThreshold+10) |
| 1080 | for i := uint32(0); i < uint32(ops.ValueThreshold+10); i++ { |
| 1081 | stat[i] = 0 |
| 1082 | } |
| 1083 | |
| 1084 | db.vlog.lfDiscardStats.Lock() |
| 1085 | db.vlog.lfDiscardStats.m = stat |
| 1086 | encodedDS, _ := json.Marshal(db.vlog.lfDiscardStats.m) |
| 1087 | db.vlog.lfDiscardStats.Unlock() |
| 1088 | entries := []*Entry{{ |
| 1089 | Key: y.KeyWithTs(lfDiscardStatsKey, 1), |
| 1090 | // The discard stat value is more than value threshold. |
| 1091 | Value: encodedDS, |
| 1092 | }} |
| 1093 | // Push discard stats entry to the write channel. |
| 1094 | req, err := db.sendToWriteCh(entries) |
| 1095 | require.NoError(t, err) |
| 1096 | req.Wait() |
| 1097 | |
| 1098 | // Unset discard stats. We've already pushed the stats. If we don't unset it then it will be |
| 1099 | // pushed again on DB close. Also, the first insertion was in vlog file 1, this insertion would |
| 1100 | // be in value log file 3. |
| 1101 | db.vlog.lfDiscardStats.Lock() |
| 1102 | db.vlog.lfDiscardStats.m = nil |
| 1103 | db.vlog.lfDiscardStats.Unlock() |
| 1104 | |
| 1105 | // Push more entries so that we get more than 1 value log files. |
| 1106 | require.NoError(t, db.Update(func(txn *Txn) error { |
| 1107 | e := NewEntry([]byte("f"), []byte("1")) |
| 1108 | return txn.SetEntry(e) |
| 1109 | })) |
| 1110 | require.NoError(t, db.Update(func(txn *Txn) error { |
| 1111 | e := NewEntry([]byte("ff"), []byte("1")) |
| 1112 | return txn.SetEntry(e) |
| 1113 | })) |
| 1114 | |
| 1115 | tr := trace.New("Badger.ValueLog", "GC") |
| 1116 | // Use first value log file for GC. This value log file contains the discard stats. |
| 1117 | lf := db.vlog.filesMap[0] |
| 1118 | require.NoError(t, db.vlog.rewrite(lf, tr)) |
| 1119 | require.NoError(t, db.Close()) |
| 1120 | |
| 1121 | db, err = Open(ops) |
| 1122 | // discardStats will be populate using vlog.populateDiscardStats(), which pushes discard stats |
| 1123 | // to vlog.lfDiscardStats.flushChan. Hence wait for some time, for discard stats to be updated. |
| 1124 | time.Sleep(1 * time.Second) |
| 1125 | require.NoError(t, err) |
| 1126 | db.vlog.lfDiscardStats.RLock() |
| 1127 | require.Equal(t, stat, db.vlog.lfDiscardStats.m) |
| 1128 | db.vlog.lfDiscardStats.RUnlock() |
nothing calls this directly
no test coverage detected
searching dependent graphs…