Skip to content

Commit 253cc74

Browse files
author
Ibrahim Jarif
committed
fix discards stats tests
1 parent 8030243 commit 253cc74

File tree

1 file changed

+14
-31
lines changed

1 file changed

+14
-31
lines changed

value_test.go

Lines changed: 14 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1014,26 +1014,28 @@ func TestDiscardStatsMove(t *testing.T) {
10141014
for i := uint32(0); i < uint32(ops.ValueThreshold+10); i++ {
10151015
stat[i] = 0
10161016
}
1017+
1018+
db.vlog.lfDiscardStats.Lock()
10171019
db.vlog.lfDiscardStats.m = stat
10181020
encodedDS, _ := json.Marshal(db.vlog.lfDiscardStats.m)
1021+
db.vlog.lfDiscardStats.Unlock()
1022+
10191023
entries := []*Entry{{
10201024
Key: y.KeyWithTs(lfDiscardStatsKey, 1),
10211025
// The discard stat value is more than value threshold.
1022-
10231026
Value: encodedDS,
10241027
}}
10251028
// Push discard stats entry to the write channel.
10261029
req, err := db.sendToWriteCh(entries)
10271030
require.NoError(t, err)
10281031
req.Wait()
10291032

1030-
// pushed again on DB close.
1031-
db.vlog.lfDiscardStats.m = nil
1032-
10331033
// Unset discard stats. We've already pushed the stats. If we don't unset it then it will be
10341034
// pushed again on DB close. Also, the first insertion was in vlog file 1, this insertion would
10351035
// be in value log file 3.
1036+
db.vlog.lfDiscardStats.Lock()
10361037
db.vlog.lfDiscardStats.m = nil
1038+
db.vlog.lfDiscardStats.Unlock()
10371039

10381040
// Push more entries so that we get more than 1 value log files.
10391041
require.NoError(t, db.Update(func(txn *Txn) error {
@@ -1054,7 +1056,13 @@ func TestDiscardStatsMove(t *testing.T) {
10541056

10551057
db, err = Open(ops)
10561058
require.NoError(t, err)
1059+
// discardStats will be populate using vlog.populateDiscardStats(), which pushes discard stats
1060+
// to vlog.lfDiscardStats.flushChan. Hence wait for some time, for discard stats to be updated.
1061+
time.Sleep(1 * time.Second)
1062+
require.NoError(t, err)
1063+
db.vlog.lfDiscardStats.RLock()
10571064
require.Equal(t, stat, db.vlog.lfDiscardStats.m)
1065+
db.vlog.lfDiscardStats.RUnlock()
10581066
require.NoError(t, db.Close())
10591067
}
10601068

@@ -1071,51 +1079,26 @@ func TestTruncatedDiscardStat(t *testing.T) {
10711079
stat[i] = 0
10721080
}
10731081

1074-
db.vlog.lfDiscardStats.Lock()
10751082
db.vlog.lfDiscardStats.m = stat
10761083
encodedDS, _ := json.Marshal(db.vlog.lfDiscardStats.m)
1077-
db.vlog.lfDiscardStats.Unlock()
10781084
entries := []*Entry{{
10791085
Key: y.KeyWithTs(lfDiscardStatsKey, 1),
10801086
// Insert truncated discard stats. This is important.
1081-
Value: encodedDS[:10],
1087+
Value: encodedDS[:13],
10821088
}}
10831089
// Push discard stats entry to the write channel.
10841090
req, err := db.sendToWriteCh(entries)
10851091
require.NoError(t, err)
10861092
req.Wait()
10871093

10881094
// Unset discard stats. We've already pushed the stats. If we don't unset it then it will be
1089-
// pushed again on DB close. Also, the first insertion was in vlog file 1, this insertion would
1090-
// be in value log file 3.
1091-
db.vlog.lfDiscardStats.Lock()
1095+
// pushed again on DB close.
10921096
db.vlog.lfDiscardStats.m = nil
1093-
db.vlog.lfDiscardStats.Unlock()
1094-
1095-
// Push more entries so that we get more than 1 value log files.
1096-
require.NoError(t, db.Update(func(txn *Txn) error {
1097-
e := NewEntry([]byte("f"), []byte("1"))
1098-
return txn.SetEntry(e)
1099-
}))
1100-
require.NoError(t, db.Update(func(txn *Txn) error {
1101-
e := NewEntry([]byte("ff"), []byte("1"))
1102-
return txn.SetEntry(e)
1103-
}))
11041097

1105-
tr := trace.New("Badger.ValueLog", "GC")
1106-
// Use first value log file for GC. This value log file contains the discard stats.
1107-
lf := db.vlog.filesMap[0]
1108-
require.NoError(t, db.vlog.rewrite(lf, tr))
11091098
require.NoError(t, db.Close())
11101099

11111100
db, err = Open(ops)
1112-
// discardStats will be populate using vlog.populateDiscardStats(), which pushes discard stats
1113-
// to vlog.lfDiscardStats.flushChan. Hence wait for some time, for discard stats to be updated.
1114-
time.Sleep(1 * time.Second)
11151101
require.NoError(t, err)
1116-
db.vlog.lfDiscardStats.RLock()
1117-
require.Equal(t, stat, db.vlog.lfDiscardStats.m)
1118-
db.vlog.lfDiscardStats.RUnlock()
11191102
require.NoError(t, db.Close())
11201103
}
11211104

0 commit comments

Comments
 (0)