lib/mergeset: close and open the table before making snapshots at TestTableCreateSnapshotAt()

This gives guarantees that all the in-memory data is written to disk at the snapshot time.

See https://github.com/VictoriaMetrics/VictoriaMetrics/issues/4272
See https://github.com/VictoriaMetrics/VictoriaMetrics/pull/4316
This commit is contained in:
Aliaksandr Valialkin 2023-05-16 14:55:09 -07:00
parent 036ab185bf
commit 4a5b5c5020
No known key found for this signature in database
GPG key ID: A72BEC6CD3D0DED1

View file

@ -95,13 +95,9 @@ func TestTableCreateSnapshotAt(t *testing.T) {
if err := os.RemoveAll(path); err != nil {
t.Fatalf("cannot remove %q: %s", path, err)
}
defer func() {
_ = os.RemoveAll(path)
}()
var isReadOnly uint32
tb := MustOpenTable(path, nil, nil, &isReadOnly)
defer tb.MustClose()
// Write a lot of items into the table, so background merges would start.
const itemsCount = 3e5
@ -109,7 +105,11 @@ func TestTableCreateSnapshotAt(t *testing.T) {
item := []byte(fmt.Sprintf("item %d", i))
tb.AddItems([][]byte{item})
}
tb.DebugFlush()
// Close and open the table in order to flush all the data to disk before creating snapshots.
// See https://github.com/VictoriaMetrics/VictoriaMetrics/issues/4272#issuecomment-1550221840
tb.MustClose()
tb = MustOpenTable(path, nil, nil, &isReadOnly)
// Create multiple snapshots.
snapshot1 := path + "-test-snapshot1"
@ -120,24 +120,15 @@ func TestTableCreateSnapshotAt(t *testing.T) {
if err := tb.CreateSnapshotAt(snapshot2, 0); err != nil {
t.Fatalf("cannot create snapshot2: %s", err)
}
defer func() {
_ = os.RemoveAll(snapshot1)
_ = os.RemoveAll(snapshot2)
}()
// Verify snapshots contain all the data.
tb1 := MustOpenTable(snapshot1, nil, nil, &isReadOnly)
defer tb1.MustClose()
tb2 := MustOpenTable(snapshot2, nil, nil, &isReadOnly)
defer tb2.MustClose()
var ts, ts1, ts2 TableSearch
ts.Init(tb)
ts1.Init(tb1)
defer ts1.MustClose()
ts2.Init(tb2)
defer ts2.MustClose()
for i := 0; i < itemsCount; i++ {
key := []byte(fmt.Sprintf("item %d", i))
if err := ts.FirstItemWithPrefix(key); err != nil {
@ -159,6 +150,17 @@ func TestTableCreateSnapshotAt(t *testing.T) {
t.Fatalf("unexpected item found for key=%q in snapshot2; got %q", key, ts2.Item)
}
}
ts1.MustClose()
ts2.MustClose()
// Close and remove tables.
tb2.MustClose()
tb1.MustClose()
tb.MustClose()
_ = os.RemoveAll(snapshot2)
_ = os.RemoveAll(snapshot1)
_ = os.RemoveAll(path)
}
func TestTableAddItemsConcurrent(t *testing.T) {