Skip to content

Compactor: let an in-flight compaction finish on shutdown - #7973

Open
alexqyle wants to merge 4 commits into
cortexproject:masterfrom
alexqyle:compactor-finish-compaction-on-shutdown
Open

alexqyle wants to merge 4 commits into
cortexproject:masterfrom
alexqyle:compactor-finish-compaction-on-shutdown

Conversation

@alexqyle

Copy link
Copy Markdown
Contributor

What this PR does:

Adds -compactor.compaction-drain-timeout, which lets a compaction that is already running when shutdown is requested finish instead of being cancelled. A large compaction can take hours, and today any restart, whether a rollout or a node replacement, throws that work away and the next owner starts the merge again from scratch.

When the flag is set, compactions run under a context detached from the service context, and that context is cancelled once the drain timeout has passed after shutdown is requested. The compactor stops picking up further tenants as soon as shutdown is requested, regardless of the flag. It defaults to 0, which keeps the current behaviour of cancelling the running compaction immediately.

A detached context on its own is not enough. Thanos's BucketCompactor.Compact keeps taking groups off its queue and repeats whole compaction passes until no work is left, and only sees cancellation through the calls it makes, so without anything else a draining compactor would work through the tenant's backlog until the timeout cut it mid-merge. Two gates stop that once shutdown is requested:

  • The planner returns an empty plan for any group not yet planned. Thanos treats that as a group with nothing to do and does not ask for another pass, so groups already queued in the current pass are skipped.
  • The grouper returns no groups on later passes. Under the partitioning strategy, building a group writes a visit marker claiming its partition, so without this gate a draining compactor would claim partitions it never compacts, and other compactors would skip them until -compactor.compaction-visit-marker-timeout expired.

The process still has to be given long enough to drain: the timeout should be set below the deployment's termination grace period, otherwise the process is killed before the timeout fires.

Which issue(s) this PR fixes:
N/A

Checklist

  • Tests updated
  • Documentation added
  • CHANGELOG.md updated - the order of entries should be [CHANGE], [FEATURE], [ENHANCEMENT], [BUGFIX]
  • docs/configuration/v1-guarantees.md updated if this PR introduces experimental flags - the new flag is not experimental

Disclosure per GENAI_POLICY.md: this change was drafted with the help of an AI coding assistant.

Add -compactor.compaction-drain-timeout. When set, shutdown no longer
cancels a compaction that is already running. Compactions run under a
context detached from the service context, cancelled once the drain
timeout has passed after shutdown is requested. The compactor stops
picking up further tenants as soon as shutdown is requested.

Thanos's BucketCompactor keeps taking groups off its queue and repeats
whole compaction passes until no work is left, and only sees
cancellation through the calls it makes. A detached context alone would
therefore keep compacting the tenant's backlog until the timeout cut it
mid-merge. Two gates stop that. The planner returns an empty plan for
every group not yet planned when shutdown is requested, which thanos
treats as a group with nothing to do. The grouper returns no groups on
later passes. Under the partitioning strategy, building a group writes
a visit marker claiming its partition, so without the grouper gate a
draining compactor would claim partitions it never compacts and other
compactors would skip them until the marker timed out.

Defaults to 0, which keeps the current behaviour of cancelling the
running compaction immediately.

Signed-off-by: Alex Le <leqiyue@amazon.com>
@alexqyle
alexqyle requested a review from a team as a code owner November 26, 2026 21:13
@alexqyle
alexqyle requested a review from SungJin1212 November 26, 2026 21:13
Signed-off-by: Alex Le <leqiyue@amazon.com>
Signed-off-by: Alex Le <leqiyue@amazon.com>
Comment thread pkg/compactor/compactor.go Outdated

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.

Shouldn't the drain context also be passed to blocksCompactorFactory here?
The current tests don't seem to catch this because the mocked compactor ignores the factory ctx.

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.

Good catch. Updated the change

…context

The TSDB compactor keeps the context it is built with and checks it
while merging, so building it in starting() with the service context
aborted the merge at shutdown even with a drain timeout set. Only the
block download and upload, which use the context thanos passes down,
were protected.

Create the drain context in starting() and pass it to both the blocks
compactor factory and the bucket client factory. The GCS bucket client
builds its token source with that context, and for credential types
other than service account keys a token refresh sends its request with
it, so a refresh during the drain would otherwise fail.

The test mock now records the context its factory was given, so a test
can assert the merge context survives shutdown.

Signed-off-by: Alex Le <leqiyue@amazon.com>

This branch has not been deployed

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants