Skip to content

Saturate the deadline of cluster_function_wait_for_replica_ms instead of overflowing it - #130812

Merged
alexey-milovidov merged 1 commit into
ClickHouse:masterfrom
groeneai:saturate-cluster-function-wait-for-replica-ms-126101
Nov 12, 2026
Merged

alexey-milovidov merged 1 commit into
ClickHouse:masterfrom
groeneai:saturate-cluster-function-wait-for-replica-ms-126101

Conversation

@groeneai

@groeneai groeneai commented Nov 11, 2026 •

Copy link
Copy Markdown
Collaborator

Changelog category (leave one):

  • Improvement

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

A value of cluster_function_wait_for_replica_ms too large for the deadline arithmetic (about 292 years or more) is now honoured as the longest possible wait, instead of overflowing, which skipped the wait. Closes #126101.

Description

Closes: #126101

StorageObjectStorageStableTaskDistributor::getAnyUnprocessedFile computed steady_clock::now() + wait_for_owner. The setting accepts up to INT64_MAX / 1000 ms, and the sum overflows Int64 nanoseconds in two places: the ms to ns conversion (above ~9.2e12 ms) and the addition to now() (a bound that moves with uptime, so no clamp constant is exact). UBSan builds abort; other builds wrap the deadline into the past, so the wait is skipped. Results stay correct, so this is an Improvement, not a Bug Fix.

The deadline is now steady_clock::time_point::max() when now + wait is not representable, and now + wait (unchanged) otherwise. Its readers are a time point comparison and wait_until, which is defined for max() (as in QuerySlot.cpp). I did not use the one-year saturatedMilliseconds clamp the issue mentions: it would shorten every configured value between one year and ~292 years, which works today.

Validation:

  • New gtest RendezvousHashing.WaitsForOwnerWithHugeWait (the setting's maximum, and the largest value whose conversion fits but whose addition overflows): fails on master (the replica takes the owner's file at once), passes with the fix, 50/50 repeated.
  • New 05620_cluster_function_wait_for_replica_ms_overflow: aborts a master ASan+UBSan server (signed integer overflow: 9223372036854775 * 1000000 at StorageObjectStorageStableTaskDistributor.cpp:218); passes with the fix (10/10 ASan+UBSan, 50/50 debug, randomized). The issue's own query returns 3 6 with no UBSan report.

🤖 Generated with Claude Code


Workflow [PR]
Sync PR [sync-upstream/pr/130812]

Version info

  • Merged into: 26.11.1.2020-master (included in 26.11 and later)

…ad of overflowing it

`StorageObjectStorageStableTaskDistributor::getAnyUnprocessedFile` computed
`steady_clock::now() + wait_for_owner` on the first request of every replica that
had no own files left. The setting accepts up to INT64_MAX / 1000 ms, and the sum
overflows Int64 nanoseconds at two places: the ms -> ns conversion (for more than
INT64_MAX / 1e6 ms) and the addition to `now()` (a bound that moves with uptime).
UBSan builds abort; other builds wrap the deadline into the past, so a wait of
"practically forever" became no wait.

The deadline is now `steady_clock::time_point::max()` whenever `now + wait` is not
representable, and `now + wait` (bit-identical to before) otherwise. Its readers
are a time point comparison and `wait_until`, which is defined for `max()`.

Closes ClickHouse#126101

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai

Copy link
Copy Markdown
Collaborator Author
Internal second-model review: adjudication log (click to expand)

Pre-publication review by an independent model (engine: codex; 2 findings; 1 full pass).

# Sev Finding Verdict Evidence / action
1 ⚠️ The gtest only checks that replica 0 still waits after 100 ms, so it does not pin time_point::max() and could pass reverted code if the worker thread starts late DISAGREE With the fix reverted the test fails (master debug: replica 0 takes the owner's file at once). A late worker still makes the first getAnyUnprocessedFile call, so Unit tests (asan_ubsan) (-fno-sanitize-recover=all) aborts on the overflow whatever the timing. Pinning max() exactly would need a test-only seam in src/.
2 💡 < saturates the one representable boundary value instead of adding it AGREE, not changed The deadline is then later by less than 1 ms, at a single value of a ~292-year wait; it is never earlier than requested.

Severity: ❌ blocker / ⚠️ major / 💡 nit. DISAGREE verdicts carry recorded evidence and are terminal per finding.

Session id: cron:clickhouse-review-slot-48:20261111-140503

@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. RendezvousHashing.WaitsForOwnerWithHugeWait fails on master every run (the wrapped deadline is in the past, no timing involved); 05620_cluster_function_wait_for_replica_ms_overflow aborts a master ASan+UBSan server on the first run.
b Root cause explained? getAnyUnprocessedFile adds the setting's milliseconds to steady_clock::now() in Int64 nanoseconds. Above ~9.2e12 ms the ms to ns conversion overflows; above (INT64_MAX - uptime_ns) / 1e6 ms the addition does. UBSan aborts; other builds get a deadline in the past and skip the wait.
c Fix matches root cause? Yes. The deadline computation saturates at steady_clock::time_point::max(), never earlier than requested, covering both overflow sites. The user's value is not clamped.
d Test intent preserved / new tests added? New gtest (both overflow sites) and new stateless test; no existing test changed.
e Both directions demonstrated? Gtest: FAIL on master (debug), PASS with the fix, 50/50 repeated. Stateless test: UBSan abort at StorageObjectStorageStableTaskDistributor.cpp:218 on master; with the fix 10/10 ASan+UBSan and 50/50 debug, randomized.
f Fix is general across code paths? It is the only writer of the deadline. Its readers (a time point comparison and wait_until) do no arithmetic, and the call site in StorageObjectStorageCluster.cpp does no ns conversion. No other now() + <setting> in the cluster read-task code.
g Fix generalizes across inputs (params/datatypes/wrappers)? Checked 0, 200 ms and 1 h (existing gtests), 9223372036854 (addition site), 9223372036854775 (setting maximum, conversion site), 10000000000000 (the issue). Negative values cannot come from SQL (the setting is unsigned). No type wrappers involved.
h Backward compatible? (maintainer-approved exception only) Yes. No setting, default or format change. Representable deadlines are bit-identical; only a non-representable one changes from "no wait" to "the longest wait".
i Invariants and contracts preserved? The deadline is never earlier than now + wait and is computed without signed overflow; it is written once, under the existing lock, as before.

Session id: cron:clickhouse-impl-slot-41:20261111-130900

@clickhouse-gh

clickhouse-gh Bot commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [5e43219]

Summary: ✅


AI Review

Summary

PR #130812 saturates an unrepresentable cluster_function_wait_for_replica_ms deadline at steady_clock::time_point::max(), preventing overflow during duration conversion and deadline addition. Reviewed commit 5e43219cbf559c4f0204edd46791e2b72c158389, the current code, regression tests, and all prior discussion. No actionable findings.

Missing context / blind spots
  • ⚠️ The relevant unit and stateless CI results were not yet available in the Praktika report. This checkout has no configured build or ClickHouse binary, so the author's reported regression runs were not independently repeated. Completed sanitizer CI runs covering the new tests would close this validation gap.
Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added pr-improvement Pull request with some product improvements can be tested Allows running workflows for external contributors comp-object-storage Object storage connectivity (S3/GCS/Azure) including credentials, retries, multipart, etc. labels Nov 11, 2026
@clickhouse-gh

clickhouse-gh Bot commented Nov 11, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 5e43219cb with master 1e5f1a9a8 (stripped binary size, per-symbol sizes and ThinLTO time; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Slowest function optimization changes (ThinLTO)

Median per-function time ratio to the master baseline: programs/clickhouse ×0.80, programs/clickhouse-keeper ×0.69 (each binary is linked separately, on a different machine and with different build flags); deltas below are relative to it.

Binary Function Master PR Δ vs median
programs/clickhouse DB::registerFunctionConversion(DB::FunctionFactory&) 6.7 s 14.2 s +8.9 s (+166%)
Compile time of recompiled translation units

7 translation units recompiled, 12 s compile time in total, 7 of them have a recent master baseline.

Job report

@alexey-milovidov alexey-milovidov self-assigned this Nov 12, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Nov 12, 2026
Merged via the queue into ClickHouse:master with commit 9b1b6b0 Nov 12, 2026
599 of 601 checks passed
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-synced-to-cloud The PR is synced to the cloud repo label Nov 12, 2026
m7kss1 pushed a commit to m7kss1/ClickHouse that referenced this pull request Nov 22, 2026
…able

When the file iterator is exhausted, `StorageObjectStorageStableTaskDistributor::getAnyUnprocessedFile`
waits on `replica_seen_cv` until `cluster_function_wait_for_replica_ms` passes for a replica that has not
asked for tasks yet. The wait runs inside the read-task callback, on an initiator pipeline thread that
holds `RemoteQueryExecutor::was_cancelled_mutex`, and nothing checked the query or notified the cv on
cancellation: `QueryStatus::cancelQuery` blocks in `RemoteQueryExecutor::cancel` on that same mutex.
So `KILL QUERY` and `max_execution_time` waited for the whole interval, and since the deadline saturates
for huge values (ClickHouse#130812) such a query never ended. This hung the Stress test (arm_release) on PR ClickHouse#115722
for a fuzzed `icebergLocalCluster('test_cluster_two_shards', ...)` query with `max_threads = 1`.

The wait now polls `QueryStatus::checkTimeLimit()` every 100 ms, as the other waits on a query's behalf
do (ZooKeeperWaitLimit, DatabaseReplicated, the worker side ReadTaskIterator::next). `cancelQuery` sets
`is_killed` before cancelling the executors, so the waiter sees it while the canceller is blocked, throws,
and the unwinding releases `was_cancelled_mutex`. With `timeout_overflow_mode = 'break'` the replica gets
an empty task. The query status comes from `getProcessListElementSafe()` of the query context; the
gtests construct the distributor without one and keep their behaviour.

With fewer initiator threads than replicas the owner's request can still sit behind the wait, so the
query can still wait until `cluster_function_wait_for_replica_ms`, but that wait now ends on
`KILL QUERY` and at `max_execution_time`.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors comp-object-storage Object storage connectivity (S3/GCS/Azure) including credentials, retries, multipart, etc. pr-improvement Pull request with some product improvements pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

cluster_function_wait_for_replica_ms >= ~9.2e12 overflows now() + wait (UB)

3 participants