diff --git a/CHANGELOG.md b/CHANGELOG.md index 0b0350004..da1979def 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## unreleased + +* [CHANGE] TSDB: Fix the predicate checking for blocks which are beyond the retention period to include the ones right at the retention boundary. #9633 + ## 2.50.1 / 2024-02-26 * [BUGFIX] API: Fix metadata API using wrong field names. #13633 diff --git a/tsdb/db.go b/tsdb/db.go index 4998da6aa..eaba50855 100644 --- a/tsdb/db.go +++ b/tsdb/db.go @@ -1615,7 +1615,7 @@ func BeyondTimeRetention(db *DB, blocks []*Block) (deletable map[ulid.ULID]struc for i, block := range blocks { // The difference between the first block and this block is larger than // the retention period so any blocks after that are added as deletable. - if i > 0 && blocks[0].Meta().MaxTime-block.Meta().MaxTime > db.opts.RetentionDuration { + if i > 0 && blocks[0].Meta().MaxTime-block.Meta().MaxTime >= db.opts.RetentionDuration { for _, b := range blocks[i:] { deletable[b.meta.ULID] = struct{}{} } diff --git a/tsdb/db_test.go b/tsdb/db_test.go index 45a16b8ba..9e8887789 100644 --- a/tsdb/db_test.go +++ b/tsdb/db_test.go @@ -681,6 +681,34 @@ 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 the retention duration. + metas := []BlockMeta{ + {MinTime: 300, MaxTime: 500}, + {MinTime: 200, MaxTime: 300}, + {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