Skip to content

Cherry pick #125165 to 26.8: Do not wait for a running statistics refresh when a MergeTree table is shut down - #125477

Merged
robot-ch-test-poll3 merged 6 commits into
backport/26.8/125165from
cherrypick/26.8/125165
Oct 13, 2026
Merged

robot-ch-test-poll3 merged 6 commits into
backport/26.8/125165from
cherrypick/26.8/125165

Conversation

@robot-ch-test-poll2

Copy link
Copy Markdown
Contributor

Original pull-request #125165

Do not merge this PR manually

This pull-request is a first step of an automated backporting.
It contains changes similar to calling git cherry-pick locally.
If you intend to continue backporting the changes, then resolve all conflicts if any.
Otherwise, if you do not want to backport them, then just close this pull-request.

The check results does not matter at this step - you can safely ignore them.

Before you resolve anything

Conflicts are often caused by a prerequisite change that has not been backported yet, rather than by a real divergence. The bot re-tries this cherry-pick against the release branch on every run, so if that is the case here it will merge itself as soon as the prerequisite lands, and you will see a comment saying so. Manual resolution is only needed while the conflict persists.

Troubleshooting

If the conflicts were resolved in a wrong way

If this cherry-pick PR is completely screwed by a wrong conflicts resolution, and you want to recreate it:

  • delete the pr-cherrypick label from the PR
  • delete this branch from the repository

You also need to check the Original pull-request for pr-backports-created label, and delete if it's presented there

The PR source

The PR is created in the CI job

groeneai and others added 5 commits October 11, 2026 07:16
`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 #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]>
…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]>
…does-not-block-shutdown

Do not wait for a running statistics refresh when a MergeTree table is shut down
@robot-ch-test-poll2 robot-ch-test-poll2 added pr-cherrypick Cherry-pick of merge-commit before backporting. Do not use manually - automated use only! do not test disable testing on pull request labels Oct 12, 2026
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]>
@groeneai

Copy link
Copy Markdown
Collaborator

I pushed a resolution of this cherry-pick to my fork, branch resolve-26.8-cherrypick-125165 (4508947f3104). Its parents are b38b68cb5065 (this PR's head) and 5e3cd565a61e (backport/26.8/125165), so applying it is a fast-forward.

The conflicts come from #121671 and #121672, which are not on 26.8: there is no refresh_stats_task_mutex and no stopStatisticsCache. On 26.8 the refresh task is deactivated directly in startStatisticsCache, ~MergeTreeData, StorageMergeTree::shutdown and StorageReplicatedMergeTree::shutdown. The resolution therefore sets refresh_stats_cancelled before each of those four deactivate calls, resets it when startStatisticsCache arms a new task, and checks it before each part in refreshStatistics. Neither the mutex nor stopStatisticsCache is ported. The 05361_statistics_refresh_does_not_block_drop test is unchanged.

The cherry-pick of #124420 (#125185) changes the next line of the same loop: it removes readLockParts and adds LoadedStatistics. Whichever of the two lands second gets a one-line conflict there. Resolve it by keeping the refresh_stats_cancelled check as the first statement of the loop, as on master.

I tested a Debug build of the resolved tree with clang-22, running the 26.8 clickhouse-test:

  • 05361 passes 50/50 (25 sequential, 25 at -j 8). The ALTER takes at most 2.4 s and the DROP at most 0.9 s.
  • The existing statistics refresh tests pass.
  • On the official 26.8.26.4, built from the first parent of backport/26.8/125165, 05361 fails: the ALTER takes 57.6 s and the DROP 57.8 s.
  • Dropping a ReplicatedMergeTree table in the middle of a refresh takes 70.0 s on 26.8.26.4 and about 0.4 s with the resolution.

@alexey-milovidov, I have triage but not push on this repository, so I cannot update cherrypick/26.8/125165 myself. Applying it takes two commands:

git fetch https://github.com/groeneai/ClickHouse resolve-26.8-cherrypick-125165
git push origin FETCH_HEAD:cherrypick/26.8/125165

After that the robot merges this PR and opens the 26.8 backport PR itself.

@alexey-milovidov

Copy link
Copy Markdown
Member

🕵 Pushed the groeneai resolution (4508947f3104, a fast-forward of the previous head b38b68cb5065) to cherrypick/26.8/125165. I checked it before pushing. It sets refresh_stats_cancelled before all four refresh_stats_task->deactivate calls on 26.8 (startStatisticsCache, ~MergeTreeData, StorageMergeTree::shutdown, StorageReplicatedMergeTree::shutdown). It resets the flag only when a new task is armed, and the scheduled task is the only caller of refreshStatistics, so the early return cannot leave the estimator unbuilt on any other path. Thanks @groeneai.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

do not test disable testing on pull request pr-cherrypick Cherry-pick of merge-commit before backporting. Do not use manually - automated use only!

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants