Repository navigation
Fix race condition in MergeTreeRestartingThread - #68513
Merged
Merged
Conversation
Contributor
|
This is an automated comment for commit a258b4f with description of existing statuses. It's updated for the latest CI running ✅ Click here to open a full report in a separate page Successful checks
|
groeneai
added a commit
to groeneai/ClickHouse
that referenced
this pull request
Aug 7, 2026
The previous commit made the attach-path call `restarting_thread.start(true)`, that is `BackgroundSchedulePoolTaskInfo::activateAndSchedule`. That helper only tests `scheduled`, so when the inline `run()` had already armed a delay it went on to `scheduleImpl`, which cancels the pending delayed entry and enqueues the task for immediate execution instead. Recovering the deactivated task does not need that. When the task is deactivated, `run()`'s own `scheduleAfter` is refused and nothing is armed, so there is no delay to keep. When it is not deactivated, `run()` has already chosen the interval: `zookeeper_session_expiration_check_period` after a successful attempt, or `min(10000, 100 * (n + 1) * (n + 2) / 2)` ms after a failed one. Cancelling it retried `tryStartup()` immediately on every attach-path startup, not only in the window the previous commit fixes. Replace the call with `ensureArmed`, which activates the task and then arms a retry through `scheduleAfter(0, /*overwrite=*/false)`. With `overwrite` unset that call returns false while a delay is already armed, so an interval `run()` chose is kept and only a refused re-arm is replaced. `DistributedAsyncInsertDirectoryQueue` and `BackgroundJobsAssignee` already use the same argument for the same reason. The call still happens after the inline `run()` returns, so the ordering from ClickHouse#68513 is unchanged. Measured on a failing attach-path startup whose task was never deactivated, with `zookeeper_session_expiration_check_period` left at its default: the interval between the inline attempt and the next one is 22.5 to 114.4 ms over five runs with the previous commit, against 121.6 to 149.4 ms over five runs with this one, which is the 100 ms first-failure backoff `run()` selected. The subsequent intervals are 300, 600, 1000 and 1500 ms in both arms. Both integration tests added by the previous commits still pass, and still fail when the fix is reverted.
pull Bot
pushed a commit
to Mu-L/ClickHouse
that referenced
this pull request
Aug 9, 2026
…h startup A ReplicatedMergeTree table whose startup goes through ReplicatedMergeTreeAttachThread could stay in readonly mode indefinitely, rejecting every write with TABLE_IS_READ_ONLY. Recovery then depended on an unrelated ZooKeeper session loss, a SYSTEM RESTART REPLICA, or a server restart, rather than on the table's own retry. BackgroundSchedulePoolTaskInfo::schedule and ::scheduleAfter both refuse a deactivated task and return false, and ReplicatedMergeTreeRestartingThread ignores that return value, so a rejected re-arm is silent. When a startup attempt on the attach path throws after startBeingLeader, startupImpl's catch calls restarting_thread.shutdown(false), which deactivates the restarting task. ReplicatedMergeTreeAttachThread::run sees a hardware error and retries, and the next attempt gets past startBeingLeader and enters the from_attach_thread branch with the task still deactivated. The inline restarting_thread.run() there therefore cannot re-arm itself on either outcome, neither scheduleAfter(check_period_ms) on success nor scheduleAfter(next_failure_retry_ms) on failure, and the following restarting_thread.start(false) only cleared the deactivated flag without enqueuing anything. The task was left activated but neither scheduled nor delayed, that is, owned by no pool collection, so nothing retried the activation and nothing armed the zookeeper_session_expiration_check_period timer. A replica that did activate through this path silently lost its periodic session-expiration check. The ZSESSIONEXPIRED callback installed later in startupImpl does call start(true), so an unrelated session loss can rescue the table, but only if one happens to occur after this table subscribed. In the reported failure the only session-expiry notify in the affected server process fired 91 ms before that subscription, and belonged to a sibling table. The wedged table's restarting task then logged nothing at all for the remaining 417 seconds of the run, while the sibling's task logged its periodic scheduling line seven times at exact 60 second intervals. Call start(true), that is activateAndSchedule(), which clears the flag and enqueues under a single schedule_mutex hold. It runs strictly after the inline run() returns, so the ordering introduced in ClickHouse#68513 to keep a pool thread from entering run() concurrently is preserved. On a healthy first attempt the extra scheduled run takes runImpl's early-return fast path and re-arms the periodic check, measured on both a patched and an unpatched binary. Two failpoints make the two-attempt sequence deterministic in test_restarting_task_rearmed_after_failed_attach_startup, which asserts that the task is owned by the pool while the replica is still readonly and that the replica recovers once the injected failure is disabled. With the re-arm reverted to start(false) it fails on that ownership assertion; with the fix it passed every countable repetition, 60 of 60. test_startup_with_small_bg_pool_partitioned, which reported this failure, is unchanged. Co-Authored-By: Claude Opus 5 <[email protected]>
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.
Changelog category (leave one):
This closes #68512
CI Settings (Only check the boxes if you know what you are doing):