Skip to content

tsdb/wlog: Remove any temproary checkpoints when creating a Checkpoint#16780

Closed
kgeckhart wants to merge 4 commits into
prometheus:mainfrom
kgeckhart:kgeckhart/wlog-cleanup-old-tmp-checkpoints
Closed

tsdb/wlog: Remove any temproary checkpoints when creating a Checkpoint#16780
kgeckhart wants to merge 4 commits into
prometheus:mainfrom
kgeckhart:kgeckhart/wlog-cleanup-old-tmp-checkpoints

Conversation

@kgeckhart

@kgeckhart kgeckhart commented Jun 25, 2025

Copy link
Copy Markdown
Contributor

This PR ensure Checkpointwill remove any temporary checkpoint that might exist before creating a new one. This can help prevent accumulation of temporary checkpoints in the event a OOM/other shutdown case causes the process to shutdown while creating the checkpoint.

Resolves: #16779

Note: Based on a suggestion I refactored the code used in tsdb/db.go to cleanup temporary files in to a utility function shared between the two.

@kgeckhart
kgeckhart requested a review from jesusvazquez as a code owner June 25, 2025 19:11

@bboreham bboreham left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks; this seems a valid idea though I am concerned that the logic seems incorrect.

Comment thread tsdb/wlog/checkpoint.go Outdated
Comment thread tsdb/wlog/checkpoint.go Outdated
Comment thread tsdb/wlog/checkpoint.go Outdated
@kgeckhart
kgeckhart force-pushed the kgeckhart/wlog-cleanup-old-tmp-checkpoints branch from 2cdd761 to c42d8ac Compare June 27, 2025 18:31
Comment thread tsdb/wlog/checkpoint.go Outdated
if checkpoint.index >= maxIndex && !isTempCheckpoint(checkpoint) {
continue
}
// It's possible the latest checkpoint is a temporary checkpoint currently being created, so ignore the latest

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this.

To avoid adding this logic and have more/easier control on the tmps creation/deletion, don't you think it's more appropriate to add the cleaning here

if err := os.RemoveAll(cpdirtmp); err != nil {
return nil, fmt.Errorf("remove previous temporary checkpoint dir: %w", err)
}
; have Checkpoint() itself clean leftovers from its previous calls.

An extra iteration over the files shouldn't be that problematic. Also it'd be great if we could reuse removeBestEffortTmpDirs (we could move it under tsdbutil/ or sth)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To avoid adding this logic and have more/easier control on the tmps creation/deletion, don't you think it's more appropriate to add the cleaning here

That makes sense, for some reason I thought DeleteCheckpoints was being called during initialization which could allow leftover checkpoints to be removed earlier but looks like it's only called after calling Checkpoint. It would be nice to try to delete them as early as possible since it's unlikely a call to Checkpoint happens on startup. LastCheckpoint is the closest to an "init" but it having a side effect of deleting files doesn't feel great. I could add another function to do it but that feels unnecessary as well.

Also it'd be great if we could reuse removeBestEffortTmpDirs (we could move it under tsdbutil/ or sth)

👍 I'll see what I can do here

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having the logic in Checkpoint() where tmp things are already cleaned up + adding an exported method that only cleans tmp stuff for library users works for me as well.

@kgeckhart
kgeckhart force-pushed the kgeckhart/wlog-cleanup-old-tmp-checkpoints branch from c42d8ac to d26c3bb Compare July 7, 2025 19:42
@kgeckhart
kgeckhart force-pushed the kgeckhart/wlog-cleanup-old-tmp-checkpoints branch from d26c3bb to 3975a38 Compare July 7, 2025 19:44
@kgeckhart kgeckhart changed the title tsdb/wlog: Remove old temproary checkpoints in DeleteCheckpoints tsdb/wlog: Remove any temproary checkpoints in Checkpoint Jul 8, 2025
@kgeckhart kgeckhart changed the title tsdb/wlog: Remove any temproary checkpoints in Checkpoint tsdb/wlog: Remove any temproary checkpoints when creating a Checkpoint Jul 8, 2025
@kgeckhart

Copy link
Copy Markdown
Contributor Author

I made a bad assumption on the refactor and need to do some more changes. I'll close this PR and re-open when I'm ready again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

tsdb/wlog should have the ability to cleanup old temproary checkpoints

3 participants