Skip to content

Fix flaky test_trace_log_memory_context: force a new global memory peak - #113688

Merged
alexey-milovidov merged 3 commits into
masterfrom
fix/test-trace-log-memory-context-global-peak
Aug 9, 2026
Merged

alexey-milovidov merged 3 commits into
masterfrom
fix/test-trace-log-memory-context-global-peak

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Aug 6, 2026 •

Copy link
Copy Markdown
Member

test_trace_log_memory_context::test_memory_context_in_trace_log asserts that MemoryPeak events with memory_context = 'Global' appear in system.trace_log. Such events are sent only when the server-wide memory usage grows past its previous peak by at least total_memory_profiler_step (4 MiB). When the peak reached during server startup is higher than anything the test's small queries allocate, no MemoryPeak event can ever be produced and the existing retry loop cannot help — the test fails with assert 0 > 0 (labelled retry_ok). Fix by forcing a new global peak: allocate an increasing amount of memory (~100 MB more per attempt, max_threads = 1) inside the retry loop — only on attempts where the global Memory/MemoryPeak events are still missing, so retries that only miss a probabilistic MemorySample/JemallocSample do not escalate the allocation.

Seen in https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=102402&sha=f8b38f834a2e63df6820ad1e750205d84276799a&name_0=PR&name_1=Integration%20tests%20%28arm_binary%2C%20distributed%20plan%2C%202%2F4%29

Related: #102402

Changelog category (leave one):

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

Version info

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

The test asserts that `MemoryPeak` events with `memory_context = 'Global'`
appear in `system.trace_log`, but such events are sent only when the
server-wide memory usage grows past its previous peak by at least
`total_memory_profiler_step` (4 MiB). When the peak reached during server
startup is higher than anything the test's small queries allocate, no
`MemoryPeak` event can ever be produced and the existing retry loop cannot
help - the test fails with `assert 0 > 0`. Force a new global peak by
allocating an increasing amount of memory (~100 MB more per attempt) inside
the retry loop.

Seen in https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=102402&sha=f8b38f834a2e63df6820ad1e750205d84276799a&name_0=PR&name_1=Integration%20tests%20%28arm_binary%2C%20distributed%20plan%2C%202%2F4%29
of #102402

Co-Authored-By: Claude Fable 5 <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Aug 6, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [574e83e]

Summary: ✅


AI Review

Summary

This PR stabilizes test_memory_context_in_trace_log by forcing a new server-wide memory peak only when the Global/Max Memory or MemoryPeak rows are still missing, while keeping the extra allocation bounded with max_threads = 1. The earlier concern about escalating on unrelated probabilistic sampling misses is fixed in the current code, and I did not find any remaining correctness, coverage, or PR-metadata issues in the reviewed diff.

Final Verdict

✅ No remaining review findings.

@clickhouse-gh clickhouse-gh Bot added the pr-ci label Aug 6, 2026
alexey-milovidov and others added 2 commits August 7, 2026 10:34
Address review: run and escalate the `groupArray` peak-forcing query only
while the `Global`/`Max` `Memory`/`MemoryPeak` events are still missing, so
retries that only miss a probabilistic `MemorySample`/`JemallocSample` do not
keep increasing the allocation size. Pin `max_threads = 1` to keep the growth
per attempt close to ~100 MB.

Co-Authored-By: Claude Fable 5 <[email protected]>

@alexey-milovidov alexey-milovidov left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice.

@alexey-milovidov alexey-milovidov self-assigned this Aug 9, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Aug 9, 2026
Merged via the queue into master with commit ae4daa4 Aug 9, 2026
180 checks passed
@alexey-milovidov
alexey-milovidov deleted the fix/test-trace-log-memory-context-global-peak branch August 9, 2026 20:01
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-synced-to-cloud The PR is synced to the cloud repo label Aug 9, 2026
pull Bot pushed a commit to Mu-L/ClickHouse that referenced this pull request Aug 28, 2026
…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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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.

2 participants