Skip to content

Make test_server_overload deterministic under the metrics-reset window - #115905

Merged
alexey-milovidov merged 1 commit into
ClickHouse:masterfrom
groeneai:groeneai/test-server-overload-cpu-busy-gate
Aug 23, 2026
Merged

alexey-milovidov merged 1 commit into
ClickHouse:masterfrom
groeneai:groeneai/test-server-overload-cpu-busy-gate

Conversation

@groeneai

@groeneai groeneai commented Aug 22, 2026 •

Copy link
Copy Markdown
Collaborator

Related: #113529

Changelog category (leave one):

  • CI Fix or Improvement (changelog entry is not required)

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

...

Description

test_server_overload::test_overload still fails intermittently with #113529 merged
(run).
That PR removed the Bernoulli lottery; a sampling one remained.

checkCPUOverload reads getCPUOverload(os_cpu_busy_time_threshold) with reset=false
(ProfileEvents.cpp:1972), which returns 0 at :2039 while busy time accrued since the last reset is
below the threshold; on 0 the ratio, the ramp and the draw are never evaluated. The only resetting caller
is AsynchronousMetrics.cpp:2734, so every window opens at zero busy time and its first
threshold / busy_rate cannot throw - ~0.43 s on the failing runner. The probe loop samples that grid at
~0.99 s (a fresh clickhouse-client per probe, not the 0.3 s sleep), drifting only ~9 ms per iteration,
so a run starting inside that window stays there.

Setting the fixture's os_cpu_busy_time_threshold to 0 makes the measured ratio decide, not the
probe's arrival time. 0 is not a tuned constant: it degenerates to the liveness precondition the ratio
needs (no zero denominator, no ratio from an idle window). What excludes an unloaded server is
min_os_cpu_wait_time_ratio_to_throw, not this budget - on an idle node at threshold 0 the ratios
reaching the ramp are 0.137-0.470, all probability 0. Both probability ... 1. assertions from #113529
are kept, and the test now throws on probe 1.

Validated both directions: with the fix test_overload passes 10/10 debug, 10/10 asan+ubsan and 50/50,
test_drop_connections 10/10; with checkCPUOverload returning early, the ramp forced to 0, or the
threshold unreachably high it fails 3/3 each with CI's assertion.

Left alone: node2's early_drop.xml keeps 300000 - it shares the blind window but is green, and its
config also carries the drop-connection ratios. helpers/min_cpu_busy_time.xml holds 300000 too, but
nothing references it. The product default 1'000'000 is unchanged - two getCPUOverload consumers
sharing one prev_* pair gives any server a short blind window each period, a product question rather
than a test fix.


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

Version info

  • Merged into: 26.8.1.1931 (included in 26.8 and later)

test_server_overload::test_overload still failed intermittently after
ClickHouse#113529, which removed the Bernoulli lottery but left a sampling lottery.

checkCPUOverload calls getCPUOverload(os_cpu_busy_time_threshold) with
reset defaulted false (ProfileEvents.cpp:1972). getCPUOverload returns 0
at :2039 while the busy time accrued since the last reset is below the
threshold, and on 0 the caller's `cpu_load > DBL_EPSILON` test fails, so
the ratio, the probability ramp and the draw are never evaluated. The
only reset=true caller is AsynchronousMetrics.cpp:2734, so both
consumers share one prev_* pair: every reset window opens at zero busy
time and its first threshold/busy_rate is a window in which an
overloaded server provably cannot throw.

The probe loop samples that grid at about 0.99 s per iteration, because
each node1.query() spawns a fresh clickhouse-client process against a
one-core-pinned server, so the 0.3 s sleep is not the period. Against
the reset cadence that is a near-resonance: the phase drifts only about
9 ms per iteration, so a run whose starting phase lands inside the blind
window stays inside it for every probe that overlaps the 30 s load, and
the loop falls through to the assertion. On the failing CI runner the
per-probe busy time was around 8000 us against the 300000 us budget.

Setting the fixture's threshold to 0 removes the blind window, so the
verdict follows the measured wait/busy ratio rather than the probe's
arrival time. 0 is the degenerate in-domain value rather than another
calibrated constant: at :2039 the test becomes busy_delta <= 0 ||
wait_delta <= 0, exactly the liveness precondition the ratio needs to be
defined. Anything larger has to encode a busy rate and a reset cadence
that the setting cannot observe, which is why a smaller nonzero value
was measured to change no outcome at all.

This does not weaken the test. What excludes an unloaded server is
min_os_cpu_wait_time_ratio_to_throw, not the busy-time budget: measured
on an idle node at threshold 0, the ratios that now reach the ramp are
0.14 to 0.47, all giving probability 0 and no throw, while 5 of 9 idle
windows are still refused on the wait_delta clause. Both
"probability ... 1." assertions from ClickHouse#113529 are kept, so the saturated
ramp remains the thing under test, and the test now throws on its first
probe instead of iterating.

Validated in both directions. With the fix, test_overload passes 10/10
on debug, 10/10 on asan+ubsan and 50/50. With the throttle mutated it
still reddens: checkCPUOverload returning early fails 3/3, the
probability ramp forced to 0 fails 3/3, and the threshold set
unreachably high fails 3/3, each with the assertion CI reported. The
pre-fix config plus a disabled throttle also fails 3/3, so the
sensitivity was not created by this change. test_drop_connections passes
10/10 with its own config untouched.

node2's early_drop.xml keeps the same budget: it shares the blind window
but is green today, and its config also carries the drop-connection
ratios. The product default os_cpu_busy_time_threshold = 1'000'000 is
also unchanged; the shared prev_* pair gives any server a short blind
window each period, which is a product question rather than one to
settle from a test fix.
@groeneai groeneai added can be tested Allows running workflows for external contributors groeneai-origin-ci-own-pr PR origin: CI on a groeneai PR exposed a separate defect labels Aug 22, 2026
@groeneai

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

Two independent reviews of this diff: a cold read of the resulting code, and a separate
second-model pass. 9 findings, adjudicated below. Nothing changed in the diff as a result -
every accepted finding was in the PR text, not the change.

⚠️ Corrections to my own earlier text, in case an earlier draft was read:

  1. I had described node1's connection-drop path as "armed but inert on the 15.0/30.0
    defaults". That is wrong. Context.cpp:737-738 are member initializers only;
    Server.cpp:2819 overwrites them unconditionally from ServerSettings, whose defaults
    for both drop ratios are 0 (ServerSettings.cpp:1439, :1443). node1 sets neither, so
    max_ratio is 0 and checkCPUOverload returns at its first guard
    (max_ratio <= 0.0, ProfileEvents.cpp:1970) before getCPUOverload runs. The
    conclusion is unchanged and in fact stronger: node1 cannot drop connections at any
    threshold, by an unconditional early return rather than a numeric margin.
  2. The commit message says the failing runner's per-probe busy time was "around 8000 us".
    That figure is a per-query-group counter from query_log
    (ProcessList.cpp:892-893), whereas the gate reads global_counters
    (ProfileEvents.cpp:1972) - different roots, not comparable. The like-for-like global
    rate is ~705724 us/s, at which the gate opened ~0.43 s into each window. The description
    above states it correctly; the commit message does not.
# Finding Verdict
❌ node1's drop path described via the wrong mechanism (15.0/30.0 rather than max_ratio == 0 early return) AGREED - text corrected above; conclusion unchanged and stronger
❌ commit message reuses a per-query-group busy figure against a global-counter budget AGREED - corrected in the description; recorded here for the commit message
❌ helpers/min_cpu_busy_time.xml also holds 300000 and was absent from the carrier list AGREED - now disclosed in the description; deliberately not deleted (unreferenced file, out of scope here)
❌ the 60-probe oracle cannot distinguish this fix from 300000 under favourable timing, so a revert could stay green DISAGREED - the revert is not a hypothesis, it was run. It is red on the failing runner (this PR's own CI row, with #113529 already an ancestor) and green on my host, which the evidence reports and explains: that host's reset cadence is a clean 3.000 s and its windows accrue 2.5-4M us against the 300000 budget, so it is not phase-locked. The suggested remedy (assert first-probe behaviour, or synchronise a probe inside the blind window) would encode a reset cadence the test cannot observe - 3.000 s measured versus 1 s nominal - which is the host-calibration this fix exists to remove. Anti-vacuity is carried by mutation instead: throttle early-return 3/3 red, ramp forced to 0 3/3 red, threshold unreachable 3/3 red, pre-fix config + disabled throttle 3/3 red, each with this test's own assertion
⚠️ node2 (early_drop.xml) keeps 300000 and inherits the same blind window AGREED on the mechanism, fix deliberately out of scope. It is the stronger of the two second-model findings and genuinely differs from node1: node2 sets max_os_cpu_wait_time_ratio_to_drop_connection = 1.5, so it does not short-circuit at :1970 and really does reach getCPUOverload. Not fixed here because (i) since #113529 merged, CIDB shows test_drop_connections 0 failures in 27131 runs versus the 1-in-27133 test_overload failure this PR addresses, so there is no signal to validate a change against; (ii) it is not a one-line analogue - node2 is the armed path, so 0 there would newly admit low-ratio windows into a live drop ramp, which needs its own measurement rather than a copied constant; (iii) its earlier failure was already tracked and closed by #113529. Named in the description so the decision is visible
💡 Related: sat inside the non-rendering template comment AGREED - moved onto its own rendering line
💡 idle-ratio range quoted as 0.14-0.47 where the measurement is 0.137-0.470 AGREED - corrected
💡 description was 311 words against the 300-word budget AGREED - condensed to 300
💡 comment hygiene: 1 comment line, 1 code line, states a standing invariant, no src/ comments Passed, no action

Also verified: template headers byte-identical to current upstream, one changelog category,
category parses correctly through pr_labels_and_category, no em-dash anywhere, single
commit, no src/ change so no SettingsChangesHistory.cpp entry is owed.

@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. Threshold set unreachably high (100000000) reddens 3/3 with CI's exact assertion in ~2 min, no intermittency. The mechanism itself is observed directly at the decision point in an instrumented build: gate_closed=true -> getCPUOverload returns 0 -> the ratio is never evaluated.
b Root cause explained? Yes. checkCPUOverload reads getCPUOverload(threshold) with reset=false (ProfileEvents.cpp:1972), which returns 0 at :2039 while busy time accrued since the last reset is below the threshold. The only resetting caller is AsynchronousMetrics.cpp:2734, so each window opens at zero busy time and its first threshold / busy_rate cannot throw. The probe loop samples that grid at ~0.99 s, drifting ~9 ms/iteration, so a run starting in the blind window stays there.
c Fix matches root cause? Yes. The blind window is removed at its source (the fixture's busy-time budget), not worked around. No loop widening, no timeout bump, no no-random-* tag, no dataset reduction, no defensive guard. 0 is the degenerate in-domain value, deliberately not a new calibrated constant.
d Test intent preserved / new tests added? Preserved and strengthened. Both probability ... 1. assertions from #113529 kept verbatim; no iteration, sleep or timeout change; nothing weakened. The subject is a test, so the mutation arms below are the coverage proving it still catches a broken throttle.
e Both directions demonstrated? Yes. With the fix: 10/10 debug, 10/10 asan+ubsan, 50/50. Against a mutated throttle: checkCPUOverload returning early 3/3 FAIL; probability ramp forced to 0 3/3 FAIL; threshold unreachable 3/3 FAIL; pre-fix config + disabled throttle 3/3 FAIL. Each mutant proven landed by source grep plus a changed Build ID, and reverting reproduced the pristine Build ID bit-identically.
f Fix is general across code paths? Yes, carriers enumerated. Fixed: the THROW path (executeQuery.cpp:3611). Unchanged and unaffected: the second THROW site (:4005, HTTP, not exercised here); node1's accept path, which cannot fire at all - node1 sets no *_to_drop_connection, so max_ratio is the ServerSettings default 0 (ServerSettings.cpp:1443, applied unconditionally at Server.cpp:2819) and checkCPUOverload returns at its first guard max_ratio <= 0.0 (ProfileEvents.cpp:1970) before getCPUOverload is reached; measured to log 0 drop decisions across 70 runs. early_drop.xml (node2) and the product default are deliberately out of scope, disclosed in the description.
g Fix generalizes across inputs (params/datatypes/wrappers)? N/A for type wrappers (no expression code). The relevant axis is the setting value, and it was swept: 300000 baseline, 10000 (measured inert), 1800000, 100000000 (permanently blind), 0 (this fix), plus asynchronous_metrics_update_period_s default vs pinned.
h Backward compatible? (maintainer-approved exception only) Yes. One integration-test fixture config. No product default, no setting semantics, no serialization format, so no SettingsChangesHistory.cpp entry and no changelog entry.
i Invariants and contracts preserved? Yes. 0 is in-domain for the UInt64 server setting and keeps getCPUOverload's own precondition: at :2039 the test degenerates to busy_delta <= 0 || wait_delta <= 0, so no ratio is computed from a zero denominator or an idle window (measured: 5 of 9 idle windows still refused on exactly that clause, and the 4 admitted ones gave probability 0). No concurrency, lock-ordering or error-path change; the fixture's wait_for_queries teardown is untouched.

Session id: cron:clickhouse-impl-slot-40:20260822-081400

@clickhouse-gh

clickhouse-gh Bot commented Aug 22, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [30927aa]

Summary: ✅


AI Review

Summary

This PR makes test_server_overload::test_overload stop depending on the asynchronous-metrics reset window by overriding os_cpu_busy_time_threshold to 0 for the node that exercises query rejection. I reviewed the current diff, the test_server_overload harness, the checkCPUOverload / getCPUOverload logic, and the prior PR discussion, and I did not find a correctness or coverage regression in the current patch.

Final Verdict

✅ No blocking issues found.

@clickhouse-gh clickhouse-gh Bot added the pr-ci label Aug 22, 2026
@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @alexkats, could you review this? The fixture's os_cpu_busy_time_threshold made checkCPUOverload return 0 for the first part of every asynchronous-metrics reset window, so whether test_overload saw a rejection depended on which phase of that window its probes landed in; setting the threshold to 0 lets the measured wait/busy ratio decide instead.

@clickhouse-gh

clickhouse-gh Bot commented Aug 22, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 30927aa1f with master ecc01ef26 (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
Binary Master PR Δ
programs/clickhouse-stripped 705.37 MiB 702.35 MiB -3.02 MiB (-0.43%)

Only the stripped binary is compared: the official master build keeps debug symbols while PR builds strip them, so the other binaries differ by construction.

Compile time of recompiled translation units

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

Job report

@alexey-milovidov alexey-milovidov self-assigned this Aug 23, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Aug 23, 2026
Merged via the queue into ClickHouse:master with commit f252e70 Aug 23, 2026
184 of 186 checks passed
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Aug 23, 2026
rorylshanks pushed a commit to rorylshanks/ClickHouse that referenced this pull request Sep 12, 2026
… test_server_overload

test_overload asserted the rendered value of the throttle's rejection
probability ("probability used to decide whether to discard the query 1."),
i.e. p == 1. Both setting pairs document p as a linear interpolation between
min and max ratio, and ProfileEvents::checkCPUOverload implements exactly
that: p = (clamp(cpu_load, min, max) - min) / (max - min), followed by one
Bernoulli draw. With the test's min=1, max=1.5, p reaches 1 only when the
runner's wait/busy ratio reaches 1.5. The CI runners that run this test
produce 1.287 - 1.393, so p is fractional and the second assertion fails
while the rejection itself is correct: the observed 1.2870989304812834 gives
0.5741978609625669, matching the failure log exactly. The assertion was a
statement about the runner, not about ClickHouse.

Drop the asserted value on both carriers and keep the message check. The
query-path assertion is rewritten value-free rather than deleted, because
SERVER_OVERLOADED is also raised by the workload scheduler (FifoQueue,
AllocationQueue), so asserting the error code alone would no longer pin the
rejection to the CPU throttle. On the connection path the identical assertion
is deleted outright, since the preceding line already asserts the same string
without its value.

max = 1.5 is kept on both paths: at the ratios CI produces it puts the
per-probe rejection probability at 0.57 - 0.79, so a rejection within the
test's 60 probes is effectively certain. The two comments stating the refuted
premise (that max is below the ratio the load produces, so the ramp
saturates) are rewritten to state the ramp semantics instead.

Verified by constructing the CI condition locally. This box reaches ratios of
3.5 - 7.1, above max, which is why the stock test passes here, so max was
raised above that range to put the ratio inside the band. On the unfixed tree
test_overload then fails 5/5 with the exact CI assertion and fractional
probabilities; with this change, on the same binary and configuration, it
passes 5/5, and 50/50 on the shipping config. Removing only the probability
clause from the server message reddens exactly the two rewritten assertions
while the error-code and log-line assertions still pass; zeroing the ramp,
disabling the check, and removing the synthetic load each still redden the
behavioural oracle.

Follow-up to ClickHouse#113529 and ClickHouse#115905.

Co-Authored-By: Claude Opus 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 groeneai-origin-ci-own-pr PR origin: CI on a groeneai PR exposed a separate defect pr-ci 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.

3 participants