Repository navigation
Fix StorageReplicatedMergeTree startup/shutdown races - #121672
Conversation
A `DETACH` racing an in-flight async `startup` of a `ReplicatedMergeTree` table could: - leave the periodic refresh tasks and the outdated-parts loading task armed on a shut-down table (the repeated `shutdown` returned early, and `startup` did not re-check `shutdown_called`); - hang `waitFirstTry` forever, when `ReplicatedMergeTreeAttachThread::shutdown` cancelled the scheduled first attempt before it ran, so `first_try_done` was never set; or re-arm the attach task after `shutdown` deactivated it; - tear the interserver parts exchange endpoint down under a concurrent full shutdown that still needs it for `waitForUniquePartsToBeFetchedByOtherReplicas`. `shutdown` is now serialized by a mutex taken before consulting `shutdown_called`, deactivates the refresh tasks on every call, and on a repeated call stops whatever a racing `startup` could have re-armed. `startup` stops right away when it observes `shutdown_called` after arming. `ReplicatedMergeTreeAttachThread` re-checks the flag after arming, always deactivates in `shutdown`, and always publishes `first_try_done`. Split out of #96130 Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
|
Workflow [PR], commit [0e4ef7f] Summary: ✅
AI ReviewSummaryThis PR hardens Final Verdict
LLVM Coverage ReportMeasured on commit 0e4ef7f.
Changed lines: Changed C/C++ lines covered: 66/84 (78.57%) · Uncovered code |
…e-tree-startup-shutdown-race
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units242 translation units recompiled, 2652 s compile time in total, 242 of them have a recent master baseline. |
When the attach thread's `startupImpl` fails because the table is already being shut down, its cleanup removed the interserver parts exchange endpoint, while the full `shutdown` still needs it to serve peer fetches in `waitForUniquePartsToBeFetchedByOtherReplicas`. Leave the endpoint to the full `shutdown` in that case, and make the attach thread stop retrying once the storage is being shut down, so it never tries to register the endpoint again. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…s_task` `startStatisticsCache` re-assigns the `refresh_stats_task` holder (on `startup` and on `ALTER` of `refresh_statistics_interval`), while a concurrent `shutdown` reads and deactivates the same holder. Guard the holder with a dedicated mutex and route every deactivation through the new `stopStatisticsCache`. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
🕵 @groeneai, investigate the failure: Other failures from the previous run: the stress test server start failure ( |
|
The fix is in progress: #123790 (it includes the change from my #123774, closed in its favor). In its own run on The failure on |
…h-thread startup cleanup The previous fix decided whether to keep `data_parts_exchange_endpoint` from plain loads of `shutdown_prepared_called` / `shutdown_called`, which a concurrent full `shutdown` publishes later, so the cleanup could still remove the endpoint that the full `shutdown` then needs in `waitForUniquePartsToBeFetchedByOtherReplicas`. Taking `shutdown_mutex` there is not possible: the full `shutdown` holds it while waiting for the attach thread. Now the failure cleanup of `startupImpl` in the attach thread leaves the endpoint registered, and its teardown belongs to `shutdown` only. A retry of the attach thread reuses the registered endpoint. The endpoint is published in `data_parts_exchange_endpoint` only after `addEndpoint` succeeds, so a retry never skips the registration, and a duplicate-endpoint failure no longer removes the endpoint of another table with the same name. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
🕵 The |
|
🕵 The only failing check was the bug-fix gate in |
…tup` During server or database shutdown, `flushAndPrepareForShutdown` runs for all tables before `shutdown`. It sets `shutdown_prepared_called` and stops the attach and restarting threads, but not `shutdown_called`. A late async `startup` must not re-arm background work after that, so stop it on either flag. Co-Authored-By: Claude Opus 5.5 <[email protected]>
alexey-milovidov
left a comment
There was a problem hiding this comment.
I found this good. Even verbosity is justified.
Resolve conflicts with #121672 (the `StorageReplicatedMergeTree` half), which already added `refresh_stats_task_mutex` and `stopStatisticsCache`: keep the `refresh_stats_stopped` latch from this PR on top of it, and take master's `StorageReplicatedMergeTree::shutdown`, which already stops the statistics task. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Resolve the cherry-pick of #125165 to 26.9. The conflicts come from #121671 and #121672, which are not on 26.9: on master the statistics refresh task is stopped through `stopStatisticsCache` under `refresh_stats_task_mutex`, and the `refreshStatistics` loop no longer takes the parts lock per part. On 26.9 the task is deactivated directly in `startStatisticsCache`, `~MergeTreeData` and the `shutdown` of `StorageMergeTree` and `StorageReplicatedMergeTree`, so `refresh_stats_cancelled` is set before each of those four `deactivate` calls, reset when `startStatisticsCache` arms a new task, and checked before each part in `refreshStatistics`. Neither the mutex nor `stopStatisticsCache` is ported, and the `LoadedStatistics` profile event that master's loop also increments is left out. The `05361_statistics_refresh_does_not_block_drop` test is ported unchanged. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Resolve the cherry-pick of #125165 to 26.8. The conflicts come from #121671 and #121672, which are not on 26.8: on master the statistics refresh task is stopped through `stopStatisticsCache` under `refresh_stats_task_mutex`, and the `refreshStatistics` loop no longer takes the parts lock per part. On 26.8 the task is deactivated directly in `startStatisticsCache`, `~MergeTreeData` and the `shutdown` of `StorageMergeTree` and `StorageReplicatedMergeTree`, so `refresh_stats_cancelled` is set before each of those four `deactivate` calls, reset when `startStatisticsCache` arms a new task, and checked before each part in `refreshStatistics`. Neither the mutex nor `stopStatisticsCache` is ported, and the `LoadedStatistics` profile event that master's loop also increments is left out. The `05361_statistics_refresh_does_not_block_drop` test is ported unchanged. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Several things could go wrong when a
DETACHraced an in-flight asyncstartupof aReplicatedMergeTreetable:shutdownreturned early, andstartupdid not re-checkshutdown_called.waitFirstTrycould hang forever.ReplicatedMergeTreeAttachThread::shutdowncould cancel the scheduled first attempt before it ran, sofirst_try_donewas never set. A racingstartcould also re-arm the attach task aftershutdownhad deactivated it.waitForUniquePartsToBeFetchedByOtherReplicas.What changes:
shutdownis now serialized by a mutex, taken beforeshutdown_calledis read.shutdowndeactivates the refresh tasks on every call, and on a repeated call stops whatever a racingstartupcould have re-armed.startupstops right away when it seesshutdown_calledafter arming.ReplicatedMergeTreeAttachThreadre-checks the flag after arming, always deactivates inshutdown, and always sets and notifiesfirst_try_done.Split out of #96130, where the random
DETACH/ATTACHtesting hook exposed these races.Related: #96130
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Make the asynchronous startup of a
ReplicatedMergeTreetable robust against a concurrentDETACH, which could previously hang the startup forever or leave background tasks running on a table that was already shut down.🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/121672]
Version info
26.10.1.1753-master(included in26.10and later)