Repository navigation
Conversation
SungJin1212
reviewed
Nov 27, 2026
sandy2008
reviewed
Nov 29, 2026
sandy2008
left a comment
Contributor
There was a problem hiding this comment.
Two issues on the shutdown drain path, both reproduced with unit-test probes.
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]>
Signed-off-by: Alex Le <[email protected]>
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
force-pushed
the
compactor-finish-compaction-on-shutdown
branch
from
November 30, 2026 17:32
23edecf to
14e8368
Compare
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.Compactkeeps 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:-compactor.compaction-visit-marker-timeoutexpired.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
CHANGELOG.mdupdated - the order of entries should be[CHANGE],[FEATURE],[ENHANCEMENT],[BUGFIX]docs/configuration/v1-guarantees.mdupdated if this PR introduces experimental flags - the new flag is not experimentalDisclosure per GENAI_POLICY.md: this change was drafted with the help of an AI coding assistant.