Repository navigation
Do not wait for a running statistics refresh when a MergeTree table is shut down - #125165
Conversation
`refreshStatistics` walks every active part and reads its statistics, one or more object storage reads per part, with no stop check. Every deactivation of `refresh_stats_task` (table shutdown, `DROP`/`DETACH`, the destructor, and `ALTER ... MODIFY SETTING refresh_statistics_interval`) joins the running walk in `BackgroundSchedulePoolTaskInfo::deactivate`, so on object storage with thousands of parts each of them waited minutes, and a server restart with many such tables did not finish. `refresh_stats_stopped` cannot be used for this: it is guarded by the mutex the deactivating thread holds while it waits, and the `ALTER` path must not set it. Add an atomic `refresh_stats_cancelled`, set before both `deactivate` calls and reset only when `startStatisticsCache` arms a new task (under the same mutex, after the old task has been joined). The walk checks it before each part and returns without publishing the partial estimator, so a deactivation waits for at most one part. Closes ClickHouse#125136 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Shorten the refresh_stats_cancelled member comment to its one-line contract. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Internal second-model review (click to expand)Two rounds: own cold review + Gate B (codex) on
|
|
Workflow [PR], commit [b3dc9e0] Summary: ✅
AI ReviewSummaryThis PR adds a cancellation flag around PR Metadata
Findings
Final VerdictChanges required before merge: the production-side cancellation logic looks reasonable, but the new regression test still admits a success path that does not validate the contract it is supposed to protect. |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units246 translation units recompiled, 2601 s compile time in total, 246 of them have a recent master baseline. |
…e GET limit The CI test profile (`tests/config/users.d/enable_blobs_check.xml`) enables `s3_check_objects_after_upload`, which sends two HEAD requests per uploaded part. They count against the disk's `s3_max_get_rps = 2`, so each 120-part INSERT took about 120 s and every CI run 250 to 310 s (over the 180 s limit). The disk access check at CREATE adds a few more HEAD/GET requests, and it runs under `Context::getOrCreateDisk`'s server-wide `storage_policies_mutex`, which in the flaky check serialized the parallel runs (CREATE up to 15 s) and delayed their DROPs. Disable `s3_check_objects_after_upload` for the inserts and skip the access check of the ad hoc disks. With the CI user profile installed locally (debug build): a run takes 8 to 11 s instead of 250 s, INSERTs make no throttled requests, the 50 randomized runs (half at -j 8) pass with ALTER <= 0.9 s and DROP <= 1.2 s, master still waits 58 s for both, and the four deletion mutants still fail. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The flaky checks on release and debug builds still ran the test for 150-200 s at 3e2d7fa: with 8 copies in parallel, every one-row part cost about 650 ms of S3 uploads in CI, so each 120-part INSERT took 70-97 s, and one DROP of 120 parts waited 110 s in the shared parts cleanup pool. A statistics refresh reads every statistic of a part with its own GET request, so 24 parts with 5 statistics each keep the throttled walk at 120 requests or more, like 120 parts with one, while the number of parts written and removed is 5 times smaller. Local, debug build, CI user profile: master ALTER/DROP 57.8-69.9 s (5 runs), fix 83/83 OK with ALTER max 2.5 s and DROP max 1.9 s (25 sequential, 25 at -j 8, 32 at -j 16, 9-15 s per run); the four deletion mutants still fail. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…r-part Conflicts: - `src/Storages/MergeTree/MergeTreeData.cpp`, `MergeTreeData.h`: master changed the old statistics refresh task (`startStatisticsCache` / `stopStatisticsCache` / `refreshStatistics`, #125165) so that shutdown does not wait for a running refresh. This PR removes that task entirely in favor of the per-part cache, so the removal is kept. Its test `05361_statistics_refresh_does_not_block_drop` exercises only the removed task and `refresh_statistics_interval` (now obsolete), so it is deleted. - `src/Core/ServerSettings.cpp`: kept the `statistics_cache_*` settings and took master's new description of `iceberg_metadata_files_cache_policy`; the description of `statistics_cache_policy` now lists the possible values like the other cache policies after #123435.
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]>
Cherry pick #125165 to 26.8: Do not wait for a running statistics refresh when a MergeTree table is shut down
…h when a MergeTree table is shut down
Cherry pick #125165 to 26.9: Do not wait for a running statistics refresh when a MergeTree table is shut down
…h when a MergeTree table is shut down
Backport #125165 to 26.9: Do not wait for a running statistics refresh when a MergeTree table is shut down
Backport #125165 to 26.8: Do not wait for a running statistics refresh when a MergeTree table is shut down
|
A question about the release branches. The test this PR added, Under stress the test's The test is still on 26.8, 26.9 and 26.10 (#120758 removed it from master), and I don't open PRs against release branches. It is rare so far: 1 of about 2,000 release-branch stress jobs since 2026-10-13, and none of 60 sampled green restarts had the table left over. A lighter variant would not remove the hazard: loading a part reads about 8 objects and a refresh reads 5, so the restart always costs more than the refresh the test needs. Should |
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
DROP TABLE,DETACH TABLE, server shutdown andALTER TABLE ... MODIFY SETTING refresh_statistics_intervalno longer wait until a running background refresh of the statistics cache of aMergeTreetable has read the statistics of every part. On object storage with thousands of parts this wait could take minutes per table and delay a server restart. Closes #125136.Description
MergeTreeData::refreshStatistics, the background task behindrefresh_statistics_interval(300 s by default), reads the statistics of every active part, at least one read from the part's disk per part, with no stop check. Every place that stops the task (tableshutdown, soDROP,DETACHand server shutdown; the destructor;ALTER ... MODIFY SETTING refresh_statistics_interval) callsdeactivate, which waits for the running callback. On object storage with thousands of parts that is minutes per table, as in #125136, where a restart did not finish.The task now has an atomic cancellation flag, set before each
deactivateand reset only when a new task is armed. The walk checks it before each part and returns without publishing the partial estimator, so stopping the task waits for at most one part's statistics reads.refresh_stats_stoppedcould not be reused: it is guarded by the mutex the stopping thread holds while it waits, and theALTERpath must not set it.Validation (debug build, local MinIO, disk throttled to 2 GET/s):
DROP TABLE ... SYNCand theALTERduring a refresh take 58 to 70 s on master and at most 3.3 s with the change (over 300 randomized runs, more than half of them in parallel); a server with two tables of 120 parts mid-refresh shuts down in 121.9 s on master and 4.2 s with the change. The new test reproduces it through the disk-levels3_max_get_rpsthrottle, without failpoints; it fails on master and on each of four deletion mutants of the change.Workflow [PR]
Sync PR [sync-upstream/pr/125165]
Version info
26.10.1.2228-master(included in26.10and later)26.9.18.13,26.8.26.15