From 7f2fe8551e6819ad209c86c40c3d3d52e9d60a70 Mon Sep 17 00:00:00 2001 From: Nicolas Takashi Date: Fri, 20 Dec 2024 23:43:00 +0000 Subject: [PATCH] [CHORE] deleting blocks on startup Signed-off-by: Nicolas Takashi --- cmd/promtool/backfill_test.go | 1 + tsdb/db.go | 29 +++++++++++++---------------- 2 files changed, 14 insertions(+), 16 deletions(-) diff --git a/cmd/promtool/backfill_test.go b/cmd/promtool/backfill_test.go index da64b5dca1..e972770954 100644 --- a/cmd/promtool/backfill_test.go +++ b/cmd/promtool/backfill_test.go @@ -745,6 +745,7 @@ after_eof 1 2 require.NoError(t, err) options := tsdb.DefaultOptions() options.RetentionDuration = int64(10 * 365 * 24 * time.Hour / time.Millisecond) // maximum duration tests require a long retention + options.StartupMinRetentionTime = test.Expected.MaxTime * 2 db, err := tsdb.Open(outputDir, nil, nil, options, nil) require.NoError(t, err) defer func() { diff --git a/tsdb/db.go b/tsdb/db.go index 615ab8fea8..15c7eeb282 100644 --- a/tsdb/db.go +++ b/tsdb/db.go @@ -933,21 +933,6 @@ func open(dir string, l *slog.Logger, r prometheus.Registerer, opts *Options, rn db.blockChunkQuerierFunc = opts.BlockChunkQuerierFunc } - // store the current blocksToDeleteFunc and replace it with a startup specific one. - originalBlocksToDelete := db.blocksToDelete - db.blocksToDelete = func(blocks []*Block) map[ulid.ULID]struct{} { - return deletableBlocks(db, blocks, beyondStartupTimeRetention, BeyondSizeRetention) - } - // Reload blocks so the retention policy is applied to the blocks. - // based on the current blocksToDeleteFunc. - err = db.reloadBlocks() - if err != nil { - return nil, err - } - - // Restore the original blocksToDeleteFunc. - db.blocksToDelete = originalBlocksToDelete - var wal, wbl *wlog.WL segmentSize := wlog.DefaultSegmentSize // Wal is enabled. @@ -1017,9 +1002,21 @@ func open(dir string, l *slog.Logger, r prometheus.Registerer, opts *Options, rn db.metrics.maxBytes.Set(float64(maxBytes)) db.metrics.retentionDuration.Set((time.Duration(opts.RetentionDuration) * time.Millisecond).Seconds()) + // store the current blocksToDeleteFunc and replace it with a startup specific one. + originalBlocksToDelete := db.blocksToDelete + + // Using a custom deletableBlocks so we can delete blocks that are beyond the startup time retention. + db.blocksToDelete = func(blocks []*Block) map[ulid.ULID]struct{} { + return deletableBlocks(db, blocks, beyondStartupTimeRetention, BeyondSizeRetention) + } + if err := db.reload(); err != nil { return nil, err } + + // Restore the original blocksToDeleteFunc after applying the startup specific one. + db.blocksToDelete = originalBlocksToDelete + // Set the min valid time for the ingested samples // to be no lower than the maxt of the last block. minValidTime := int64(math.MinInt64) @@ -1771,7 +1768,7 @@ func beyondStartupTimeRetention(db *DB, blocks []*Block) (deletable map[ulid.ULI deletable = make(map[ulid.ULID]struct{}) for _, block := range blocks { - if block.Meta().MaxTime >= int64(db.opts.StartupMinRetentionTime) { + if block.Meta().MaxTime >= db.opts.StartupMinRetentionTime { for _, b := range blocks { deletable[b.meta.ULID] = struct{}{} }