Skip to content

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

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

alexqyle wants to merge 5 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.

@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
Comment thread pkg/compactor/compactor.go Resolved

@sandy2008 sandy2008 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Two issues on the shutdown drain path, both reproduced with unit-test probes.

Comment thread pkg/compactor/compactor.go Resolved
Comment thread pkg/compactor/compactor.go Resolved
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 <[email protected]>
…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 <[email protected]>
The ring subservices were started with the service context, so the
lifecycler left the ring as soon as shutdown started, while the
compaction was still draining. With the default sharding strategy,
which has no visit markers, another compactor then took over the
tenant and compacted the same blocks. Start them with the compaction
context instead. stopping() already stops them explicitly, so the
instance now leaves the ring once the drain ends.

A compaction that failed with a retriable error during the drain was
retried on the compaction context. The retry built no groups because of
the shutdown gate and returned nil, so the failure was reported as a
success. Stop retrying once shutdown has been requested and handle the
last error as when retries run out.

Signed-off-by: Alex Le <[email protected]>
@alexqyle
alexqyle force-pushed the compactor-finish-compaction-on-shutdown branch from 23edecf to 14e8368 Compare November 30, 2026 17:32

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.

3 participants