Skip to content

Fix flaky test_trace_log_memory_context: attribute the forced global peak to its own query - #116855

Merged
alexey-milovidov merged 1 commit into
ClickHouse:masterfrom
groeneai:attribute-forced-global-peak-to-its-own-query
Aug 28, 2026
Merged

alexey-milovidov merged 1 commit into
ClickHouse:masterfrom
groeneai:attribute-forced-global-peak-to-its-own-query

Conversation

@groeneai

@groeneai groeneai commented Aug 28, 2026 •

Copy link
Copy Markdown
Collaborator

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

Requested in #56335 (comment)
Related: #113688, #87995
Failing run: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=56335&sha=a3c5c38948ecdf544994b54043184abfa82c5e60&name_0=PR&name_1=Integration%20tests%20%28amd_binary_excluded_from_llvm%29

The test required Global/MemoryPeak events with empty(query_id), that is, emitted by a thread
with no query attached. 8 PRs in 120 days, 0 on master.

Root cause: MemoryPeak is emitted only when a single commitAllocation on total_memory_tracker
both crosses total_memory_profiler_step and sets a new global peak, and TraceSender stamps the
event with the emitting thread's query id. Both watermarks only ever rise, so once a query has
raised them a query-less thread must out-allocate that query to emit again. #113688 forces the peak
with a query, so its own events carry that query's id and were excluded, and each escalation raised
the watermarks further, making the next attempt less likely to help.

The change counts the Global Memory/MemoryPeak events of the query that forces the peak, in
the break condition and the assertion together so they cannot diverge. Nothing is relaxed: both rows
are still required in the Global context with memory_blocked_context = 'Max'. What changes is
which thread emits them, from any query-less thread to the one the test issues for that purpose,
which also pins query_id where empty(query_id) could not.
01092_memory_profiler.sql already scopes MemoryPeak this way.

Validation: I rebuilt the failing run's own system.trace_log from its artifacts. The old predicate
reads 0 there; the new one reads 6 over 5 query ids, every one an escalation query, so the events
existed and only the query_id filter hid them. 30 runs of the real job config pass, converging on
the first forcing query in 29 of 30; the retry loop absorbs the rest. Shrinking that query to
numbers(1) makes the new assertion fail, so it still catches regressions. I used the job's own
amd_binary artifact because the MALLOC_CONF this test sets hangs a debug binary at startup.


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

Version info

  • Merged into: 26.9.1.266 (included in 26.9 and later)

…peak to its own query

test_memory_context_in_trace_log required `Global`/`Memory` and `Global`/`MemoryPeak` trace
events with `empty(query_id)`, that is, emitted by a thread with no query attached. It failed
on 8 unrelated PRs in 120 days, 0 on master, on both `amd_binary_excluded_from_llvm` and
`arm_binary distributed plan`.

`MemoryTracker::commitAllocation` emits a `Global` `MemoryPeak` event only when one and the
same allocation both crosses `total_memory_profiler_step` on `total_memory_tracker` and sets a
new global peak, and `TraceSender::send` stamps the event with the emitting thread's query id.
Both watermarks only ever grow for the life of the server, so the assertion depended on a race
that a query-less background thread had to win. ClickHouse#113688 forces the peak with a query: its own
events carry that query's id and were therefore excluded, and each escalation raised both
watermarks further, so retrying made the next attempt less likely to help rather than more.

Count the events of the query that forces the peak instead, in the loop's break condition and
in the assertion together so the two cannot diverge. That is stricter than before rather than
weaker: a `Global`-context `Memory` and `MemoryPeak` row with `memory_blocked_context = 'Max'`
are both still required, and they are now tied to a known query id.
`01092_memory_profiler.sql` already scopes `MemoryPeak` to the allocating query.

Rebuilding the failing run's own `system.trace_log` from its artifacts shows the old predicate
reading 0 and the new one reading 6 over 5 query ids, every one of them an escalation query.
30 runs of the same CI job config pass, converging on the first forcing query in 29 of 30; the
bounded retry loop absorbs the rest. Shrinking the forcing query to `numbers(1)` makes the new
assertion fail, so it still catches a regression.
@groeneai groeneai added can be tested Allows running workflows for external contributors groeneai-origin-request PR origin: a maintainer pinged or directed groeneai labels Aug 28, 2026
@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; 0 findings), plus a separate cold
read of the diff by the reviewing session before it saw the author's evidence (2 nits, 1 below).

# Sev Finding Verdict Evidence / action
1 💡 The old empty(query_id) predicate also implied the emitting thread had no query attached, so a per-query tracker whose level was mislabelled Global could not have satisfied it. The new predicate is satisfiable by the forcing query's own rows. AGREE, noted, no code change memory_context is written at one site, memory_context = level, thread-agnostically (src/Common/MemoryTracker.cpp:531-566), so no code path stops being covered; query-less emission stays asserted for JemallocSample. The new predicate also pins something the old one could not: if trace events stopped carrying a query id, the old predicate would stay green and this one reddens. I state the delta as a fact in the description rather than calling the change "stricter".

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

Session id: cron:clickhouse-review-slot-10:20260828-083300

@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes, by replay. The failure event is ~1 in 2300 runs and is not locally reproducible, so I rebuilt the linked run's own system.trace_log from its artifacts: the old predicate reads 0 there and the new one reads 6, on demand and repeatably.
b Root cause explained? Yes. MemoryPeak needs one commitAllocation on total_memory_tracker to both cross total_memory_profiler_step and set a new global peak; TraceSender stamps the emitting thread's query id. Both watermarks only rise, so the old empty(query_id) form depended on a query-less thread out-allocating the peak-forcing query.
c Fix matches root cause? Yes. It counts the events of the query that forces the peak. No widened bound, no reduced dataset, no no-parallel or no-random-* tag, no timeout bump, no deleted assertion.
d Test intent preserved / new tests added? Yes. A Global-context Memory and MemoryPeak row with memory_blocked_context = 'Max' are both still required; what changes is which thread has to emit them, from any query-less thread to the one the test issues for that purpose. That also pins query_id on global-context rows, which empty(query_id) could not. No new test: this is a change to an existing regression test.
e Both directions demonstrated? Yes. On the failing run's own recovered trace_log: 0 with the old predicate, 6 over 5 query ids with the new one, every id an escalation query. Shrinking the forcing query to numbers(1) makes the new assertion fail, so it can still redden.
f Fix is general across code paths? Yes. grep -rl 'memory_context' tests/ returns only this test; MemoryPeak returns only this test and 01092_memory_profiler.sql, which already scopes MemoryPeak to the allocating query and is stable. The loop's break condition and the assertion were changed together so they cannot diverge. The four query-less JemallocSample call sites are deliberately unchanged: they want background-logging events and get ~45000 per run.
g Fix generalizes across inputs (params/datatypes/wrappers)? N/A for type wrappers and settings matrices: this is a Python integration test that issues no typed input. Both trace types are covered by the loop, both affected lanes (amd_binary_excluded_from_llvm, arm_binary distributed plan) are covered since the change carries no timing or thread-count assumption, and the escalation ladder still spans 12.5M to 187.5M rows.
h Backward compatible? N/A, test-only. No setting, no SettingsChangesHistory.cpp, no serialization format, no experimental gate, no default change.
i Invariants and contracts preserved? Yes. The escalation if short-circuits on peak_forcing_query_id is None, so a forcing query always runs on the first iteration and the id is never None at the assertion. A fresh id per attempt is deliberate: only the newest forcing query exceeds every previous one, and it avoids reusing a query_id. The escalation stays decoupled from the probabilistic sample checks, so a retry that only misses a jemalloc sample does not grow the allocation.

Validation runs, real job config (Integration tests (amd_binary_excluded_from_llvm)), on the
linked run's own amd_binary artifact: 30/30 pass, converging on the first forcing query in 29 of
30 runs, with the bounded retry loop absorbing the rest. trace_log overflow windows dropped from
11 (628444 discarded entries) in the failing run to 0. For honesty: the unmodified test also passes
30/30 locally, so that arm is a no-regression check and the proof is the replay in (a) and (e).

Session id: cron:clickhouse-impl-slot-4:20260828-071800

@clickhouse-gh

clickhouse-gh Bot commented Aug 28, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [df42abc]

Summary: ✅


AI Review

Summary

This PR rewires test_memory_context_in_trace_log so the Global Memory/MemoryPeak assertion follows the query that intentionally forces the new global peak, instead of requiring those rows to come from empty(query_id). Given MemoryTracker::commitAllocation emits MemoryPeak only when the same allocation both crosses the profiler watermark and raises the peak, and TraceSender::send stamps the row with the emitting thread's current query_id, that change matches the producer contract and removes the observed flake without relaxing the memory_context = 'Global' / memory_blocked_context = 'Max' coverage. I did not find a code-level regression in the current diff.

Missing context / blind spots
  • ⚠️ Full PR CI is still pending, so I could not confirm the fix in the actual Integration tests (amd_binary_excluded_from_llvm) / arm lanes from this run. A green Praktika result for those lanes would close the remaining execution-side gap.
Final Verdict

✅ No review findings at current head.

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

Copy link
Copy Markdown
Collaborator Author

cc @antonio2368, could you review this? The test counted Global/MemoryPeak trace events with empty(query_id), but the peak and profiler watermarks on total_memory_tracker only ever rise, so the peak-forcing query added in #113688 pushed them out of reach of the query-less threads whose events were counted while its own events were excluded by the filter. It now counts the forcing query's own events.

@alexey-milovidov alexey-milovidov self-assigned this Aug 28, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Aug 28, 2026
Merged via the queue into ClickHouse:master with commit f7a09dd Aug 28, 2026
187 of 188 checks passed
@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-synced-to-cloud The PR is synced to the cloud repo label Aug 28, 2026
pull Bot pushed a commit to admariner/ClickHouse that referenced this pull request Sep 22, 2026
…ueue fills

The test's configs/overrides.xml lowers trace_log/max_size_rows to 100000 but
leaves buffer_size_rows_flush_threshold at the 524288 it inherits from
programs/server/config.xml; SystemLog.cpp derives max_size_rows / 2 only when the
key is absent from every config, and it is not absent here. SystemLogQueue::push
wakes the flush thread on queue.size() == threshold and caps the queue at
max_size_rows, so a threshold above the cap makes the size-based flush
unreachable: the queue drains only on the 7500 ms interval timer and on an
explicit SYSTEM FLUSH LOGS, and discards everything in between.

In the failing job
(https://github.com/ClickHouse/ClickHouse/actions/runs/35678160277/job/106631465373)
the server logged 0 "Queue is half full", 10 "Queue had been full" every one of
them at accepted 100000, and 1081202 ignored rows, 546830 of those in the window
holding the query the assertion read. Recovering that run's own system.trace_log
from the job artifacts shows the query whose thread the log proves raised the
global peak to 2.20 GiB has 0 Global/Max/Memory rows, while six earlier
escalation ids kept theirs: the events were produced and then discarded.

Setting the companion key to max_size_rows / 2 keeps the ratio that every one of
the 18 log blocks in programs/server/config.xml uses. Measured on the failing
job's own binary, 30 runs per arm: without the line "Queue is half full" is 0/30
and a single flush reaches exactly the 100000 cap (6466 rows dropped); with it
every flush is 50438 rows or below, 0 rows are dropped, and 30/30 pass. That
removes the row loss, not the probabilistic MemoryPeak emission which the
existing 15-attempt retry loop already covers.

Related: ClickHouse#116855

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-request PR origin: a maintainer pinged or directed groeneai 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