Repository navigation
Pause the statistics refresh while replica metadata restore and RENAME TABLE rewrite part paths - #125927
Pause the statistics refresh while replica metadata restore and RENAME TABLE rewrite part paths#125927groeneai wants to merge 4 commits into
RENAME TABLE rewrite part paths#125927Conversation
The background statistics refresh (`MergeTreeData::refreshStatistics`) reads the storage path of every part without any table lock, and since ClickHouse#124420 without the parts lock. Two writers rewrite those paths for every part: `restoreMetadataInZooKeeper`, which renames all parts to `detached/` in place (`forcefullyMovePartToDetachedAndRemoveFromMemory`, documented as unsafe for parts others hold), and `MergeTreeData::rename` (`changeRootPath`, under the exclusive lock of the catalog object only). TSan reported both in CI: the convert-to-replicated startup job in test_modify_engine_on_restart/test_lazy_load_tables.py and RENAME TABLE of a non-Atomic table (STID 4967-4b32). Add `MergeTreeData::pauseStatisticsCache`, a guard that cancels and deactivates the refresh task (waiting for a running pass, which stops at its next part), keeps it disarmed while any guard lives, and re-arms it at the end if it was armed or `startStatisticsCache` was called meanwhile. Shutdown wins. Pausing through the task works for `lazy_load_tables` stand-ins too, whose table locks belong to the proxy, not to the nested storage running the task. Both writers take the guard around their rename loops. `MergeTreeData::rename` releases it after `renameInMemory`, so the re-armed task gets the new storage id. The runtime `SYSTEM RESTORE REPLICA` additionally takes the exclusive lock of the catalog object (the one queries lock) around the snapshot and the move loop, because queries read the same paths (TSan reported a race and a heap-use-after-free on the marks loader there). It is passed as a callback; the attach thread and the convert-to-replicated job cannot take it (they run inside the table's startup) and need not (queries wait for the startup job). Cost: a non-Atomic RENAME with a refresh pass in flight waits for one part's statistics load (1.4 s on a disk throttled to 2 GET/s, 0.1 s before), and the runtime restore waits for running queries up to `lock_acquire_timeout` (then DEADLOCK_AVOIDED, which the client may retry). Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ully Three follow-ups to the statistics refresh pause: - `ReplicatedMergeTreeRestartingThread::removeFailedQuorumParts` moves parts with a failed quorum to `detached/` with `forcefullyMovePartToDetachedAndRemoveFromMemory`, which renames them in place, and it runs on every restarting-thread startup while the refresh is armed. It now pauses the refresh around its loop when there is anything to move. - For a `lazy_load_tables` stand-in, the runtime `SYSTEM RESTORE REPLICA` locked only the stand-in, while `system.parts` locks the nested storage and reads part paths. The `lock_out_readers` callback now returns a vector of locks, and `restoreReplica` takes the stand-in's lock and then the nested storage's lock when it is a different object. - A test checks that the refresh task is armed again after `RENAME TABLE` of a table in an Ordinary database, under the new table name. Co-Authored-By: Claude Opus 5.5 <[email protected]>
`deactivate` cancels only a delayed task: a task still waiting in the pool queue, or one that is executing, stays in `system.background_schedule_pool` with `deactivated = 1` until a worker is done with it. The first refresh pass of the table is queued at CREATE TABLE and can wait there when the per-type cap is saturated by other tables' refresh passes, so the query after RENAME could list the old task next to the new one. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Drop the three call-site comments on why the statistics refresh is paused, keep that once on pauseStatisticsCache, shorten the lock_out_readers doc, and leave the refresh_stats_task_mutex comment as on master. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Internal second-model review: adjudication log (click to expand)Pre-publication review by an independent model (engine: codex) plus my own review, three rounds.
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-8:20261015-024400 |
|
Workflow [PR], commit [3b17102] Summary: ❌
AI ReviewSummaryReviewed commit Final VerdictStatus: ✅ Approve. |
|
cc @tiandiwonder @alesapin, could you review this? The background statistics refresh reads part paths without any lock since #124420, so |
CI finish ledger - 3b17102Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
The only other Session id: cron:our-pr-ci-monitor:20261015-190014 |
|
@PedroTadim this label will not produce a
The open cherry-picks of #124420 to 26.7, 26.8 and 26.9 (#125184, #125185, #125186) would bring the same race to those branches if merged. |
|
close it because #120758 is merged |
|
Agreed for master: 0
Could you put back |
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Description
TSan data races in CI, both with the background statistics refresh (
MergeTreeData::refreshStatistics) reading a part'sroot_path/part_dirwhile another thread rewrites it:Integration tests (amd_tsan, 1/8),test_modify_engine_on_restart/test_lazy_load_tables.py: the convert-to-replicated startup job runsrestoreMetadataInZooKeeper, which renames every part todetached/in place (report).Stateless tests (amd_tsan, flaky check), STID 4967-4b32:RENAME TABLEof a non-Atomic table (MergeTreeData::rename->changeRootPath) (report).The refresh holds no table lock, and since #124420 no parts lock either. Instead of locking the reader, both writers now pause the refresh task (new
MergeTreeData::pauseStatisticsCache, a guard that waits for a running pass and re-arms the task afterwards), which also works forlazy_load_tablesstand-ins. So does the cleanup of parts with a failed quorum, which renames single parts the same way. The runtimeSYSTEM RESTORE REPLICAadditionally takes the exclusive lock of the table (for a lazily loaded table, also the lock of its nested storage, whichsystem.partstakes) while it moves the parts, because queries read the same paths (TSan also reported a heap-use-after-free there). So it now waits for running queries, up tolock_acquire_timeout.Validated on TSan builds of this branch and its base with the CI test's flow (convert-to-replicated in a lazy database, restart) and with loops of
SYSTEM RESTORE REPLICAandRENAME TABLEwhile the refresh and queries run: about 100 reports on base, none with the fix. Related stateless tests show no new failures. Only TSan observes the race; a new test checks that the refresh task is armed again afterRENAME TABLE. A non-AtomicRENAMEwith a refresh in flight waits for one part's statistics load (1.4 s on a disk throttled to 2 GET/s).If #120758 removes the background refresh first, only the
SYSTEM RESTORE REPLICAlock remains needed.Workflow [PR]
Sync PR [sync-upstream/pr/125927]