Merge TestDB_BeyondTimeRetention into TestTimeRetention

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>
This commit is contained in:
Arve Knudsen 2024-03-20 09:07:16 +01:00
parent 9c7a734063
commit af694dc295

View file

@ -681,34 +681,6 @@ func TestDB_Snapshot(t *testing.T) {
require.Equal(t, 1000.0, sum)
}
func TestDB_BeyondTimeRetention(t *testing.T) {
opts := DefaultOptions()
opts.RetentionDuration = 100
db := openTestDB(t, opts, nil)
defer func() {
require.NoError(t, db.Close())
}()
// We have 4 blocks, 3 of which are beyond or at the retention duration.
metas := []BlockMeta{
{MinTime: 300, MaxTime: 500},
{MinTime: 200, MaxTime: 400},
{MinTime: 100, MaxTime: 200},
{MinTime: 0, MaxTime: 100},
}
for _, m := range metas {
createBlock(t, db.Dir(), genSeries(1, 1, m.MinTime, m.MaxTime))
}
// Reloading should truncate the 3 blocks which are >= the retention period.
require.NoError(t, db.reloadBlocks())
blocks := db.Blocks()
require.Len(t, blocks, 1)
require.Equal(t, metas[0].MinTime, blocks[0].Meta().MinTime)
require.Equal(t, metas[0].MaxTime, blocks[0].Meta().MaxTime)
}
// TestDB_Snapshot_ChunksOutsideOfCompactedRange ensures that a snapshot removes chunks samples
// that are outside the set block time range.
// See https://github.com/prometheus/prometheus/issues/5105
@ -1497,7 +1469,8 @@ func TestTimeRetention(t *testing.T) {
}()
blocks := []*BlockMeta{
{MinTime: 500, MaxTime: 900}, // Oldest block
{MinTime: 500, MaxTime: 900}, // Oldest block
{MinTime: 500, MaxTime: 1000}, // Coinciding exactly with the retention duration.
{MinTime: 1000, MaxTime: 1500},
{MinTime: 1500, MaxTime: 2000}, // Newest Block
}
@ -1509,14 +1482,17 @@ func TestTimeRetention(t *testing.T) {
require.NoError(t, db.reloadBlocks()) // Reload the db to register the new blocks.
require.Equal(t, len(blocks), len(db.Blocks())) // Ensure all blocks are registered.
db.opts.RetentionDuration = blocks[2].MaxTime - blocks[1].MinTime
// By setting retention duration as follows, we verify that also blocks[1],
// coinciding exactly with the boundary, is deleted.
db.opts.RetentionDuration = blocks[3].MaxTime - blocks[1].MaxTime
// Reloading should truncate the blocks which are >= the retention duration.
require.NoError(t, db.reloadBlocks())
expBlocks := blocks[1:]
expBlocks := blocks[2:]
actBlocks := db.Blocks()
require.Equal(t, 1, int(prom_testutil.ToFloat64(db.metrics.timeRetentionCount)), "metric retention count mismatch")
require.Equal(t, len(expBlocks), len(actBlocks))
require.Len(t, actBlocks, len(expBlocks))
require.Equal(t, expBlocks[0].MaxTime, actBlocks[0].meta.MaxTime)
require.Equal(t, expBlocks[len(expBlocks)-1].MaxTime, actBlocks[len(actBlocks)-1].meta.MaxTime)
}