Skip to content

Fix flaky test 04023_rollup_totals_memory_limit - #106679

Merged
alexey-milovidov merged 1 commit into
ClickHouse:masterfrom
groeneai:groeneai/fix-flaky-04023-rollup-totals-memory-limit
Jun 7, 2026
Merged

alexey-milovidov merged 1 commit into
ClickHouse:masterfrom
groeneai:groeneai/fix-flaky-04023-rollup-totals-memory-limit

Conversation

@groeneai

@groeneai groeneai commented Jun 7, 2026

Copy link
Copy Markdown
Collaborator

Lower max_memory_usage from 10 MB to 1 MB so the expected MEMORY_LIMIT_EXCEEDED exception is hit deterministically across all randomized settings combinations.

The previous limit (10 MB) sat just below the observed aggregator footprint of ~12 MiB. The 1.2x margin is too thin: certain randomized settings on Stateless tests (amd_debug, distributed plan, s3 storage, parallel) and Fast test runs shave off 2-3 MiB and let the query slip under the limit, so MEMORY_LIMIT_EXCEEDED is never thrown and the test fails with "query succeeded but server error 241 was expected" (22/88 reruns failed on PR #106187).

With a 1 MB limit the aggregator footprint (5-6 MiB observed locally across 50 random setting combinations) is firmly above the threshold, so the exception fires deterministically.

The regression target from issue #93465 (server crash inside ROLLUP+TOTALS memory-limit handling) is still exercised: any MEMORY_LIMIT_EXCEEDED thrown cleanly proves the crash path is not reached.

Requested by @ alexey-milovidov on #106187

Related: #93465

CI report (the run that prompted this fix): #106187

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):

...

Lower `max_memory_usage` from 10 MB to 1 MB so the expected
`MEMORY_LIMIT_EXCEEDED` exception is hit deterministically.

The previous limit (10 MB) sat just below the observed aggregator
footprint of ~12 MiB. The 1.2x margin is too thin: certain
randomized settings (in `Stateless tests (amd_debug, distributed
plan, s3 storage, parallel)` and `Fast test`) shave off 2-3 MiB
and let the query slip under the limit, so `MEMORY_LIMIT_EXCEEDED`
is never thrown and the test fails with "query succeeded but
server error 241 was expected".

With a 1 MB limit the aggregator footprint (5-6 MiB observed
locally across 50 random setting combinations) is firmly above
the threshold, so the exception fires deterministically.

The regression target from issue ClickHouse#93465 (server crash inside
ROLLUP+TOTALS memory-limit handling) is still exercised: any
`MEMORY_LIMIT_EXCEEDED` thrown cleanly proves the crash path is
not reached.

Requested by @ alexey-milovidov on
ClickHouse#106187

Related: ClickHouse#93465

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

groeneai commented Jun 7, 2026

Copy link
Copy Markdown
Collaborator Author

Pre-PR validation gate

# Question Answer
a Deterministic repro? Partial. The CI flake reproduces deterministically per-environment: 22/88 reruns failed on PR #106187 with the same randomized settings. Locally I cannot reproduce the WRONG_PASS shape (the local aggregator footprint is rock-stable at 12.14 MiB across all setting combinations I tested), but the flake is well-attested in CI history (7 unrelated PRs in 30d on Stateless tests (amd_debug, distributed plan, s3 storage, parallel) and Fast test). The local FAIL direction is fully deterministic at both the old (10 MB) and new (1 MB) limits.
b Root cause explained? Yes. The test relies on the aggregator memory footprint exceeding max_memory_usage. At 10 MB limit, observed footprint is ~12 MiB - a 1.2x margin. Several randomized settings (most likely the multi-stage execution paths under distributed plan, s3 storage, parallel plus aggregator-internal optimizations) shave 2-3 MiB off this, dropping below 10 MB. The query then completes and returns 3 rows (one per ROLLUP level + TOTALS), and MEMORY_LIMIT_EXCEEDED is never thrown, so the assertion fails.
c Fix matches root cause? Yes. Lowering the limit to 1 MB widens the margin to 5-6x (1 MB vs ~6 MiB observed locally). This is the correct lever: the test's purpose is to verify the server handles memory-limit-during-ROLLUP-TOTALS cleanly, which only requires that the limit is hit somewhere in the query, not at a specific transform.
d Test intent preserved / new tests added? Yes. The test's purpose - verify that ROLLUP WITH TOTALS over countDistinct(Int128) raises a clean MEMORY_LIMIT_EXCEEDED rather than crashing the server (regression for issue #93465) - is fully preserved. Note: at 10 MB limit the exception originates in RollupTransform; at 1 MB limit it originates earlier in AggregatingTransform. The original crash from issue #93465 was in Aggregator::mergeBlocks reached from RollupTransform, but any clean memory-limit exception in this query class proves the crash path is not taken.
e Both directions demonstrated? Yes. (i) Without the fix at 10 MB limit, MEMORY_LIMIT_EXCEEDED thrown reliably locally, but flaky in CI (22/88). (ii) With the fix at 1 MB limit, MEMORY_LIMIT_EXCEEDED thrown 50/50 times in a local stress loop with randomized values of max_threads, optimize_aggregation_in_order, count_distinct_optimization, compile_aggregate_expressions, enable_memory_bound_merging_of_aggregation_results.
f Fix is general, not a narrow patch? N/A (flaky test setting fix, not a code bug). Addresses the root cause of test flakiness (insufficient margin between memory usage and limit) rather than band-aiding the symptom (e.g. no-random-merge-tree-settings would silence randomization without targeting the cause and hide unrelated regressions).

Session id: cron:clickhouse-ci-task-worker:20260607-142300

@groeneai

groeneai commented Jun 7, 2026

Copy link
Copy Markdown
Collaborator Author

cc @KochetovNicolai - could you review this? Test-only fix for the flaky 04023_rollup_totals_memory_limit: lowers max_memory_usage from 10 MB to 1 MB so the expected MEMORY_LIMIT_EXCEEDED is reached deterministically (current 1.2x margin between aggregator footprint and limit is too thin for some randomized settings combinations to satisfy).

@nikitamikhaylov nikitamikhaylov added the can be tested Allows running workflows for external contributors label Jun 7, 2026
@clickhouse-gh

clickhouse-gh Bot commented Jun 7, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [d94acd3]

Summary: ✅


AI Review

Summary

This PR tries to make 04023_rollup_totals_memory_limit deterministic by lowering max_memory_usage from 10000000 to 1000000. The flake fix is plausible for forcing MEMORY_LIMIT_EXCEEDED, but it weakens the regression contract: the linked issue failed in the ROLLUP merge path, while the new limit can raise the exception during initial aggregation before that path is exercised.

Findings

⚠️ Majors

  • [tests/queries/0_stateless/04023_rollup_totals_memory_limit.sql:8] The test no longer reliably covers the regression from issue #93465. That issue's stack goes through RollupTransform::generate and Aggregator::mergeBlocks, but the new 1 MB limit can fail while building the initial countDistinct aggregate state, before ROLLUP/TOTALS processing. A future regression in the rollup merge path could pass this test as long as aggregation fails earlier.
    Suggested fix: keep the test deterministic while ensuring base aggregation completes and MEMORY_LIMIT_EXCEEDED is raised from the ROLLUP merge path, or add focused evidence in the test that the exception still reaches that path.
Tests
  • ⚠️ Current CI is green, but it only proves the query throws MEMORY_LIMIT_EXCEEDED somewhere. The missing proof is a focused run/assertion showing the exception still originates after RollupTransform starts merging aggregated blocks.
Final Verdict

Status: ⚠️ Request changes

Minimum required action: adjust the test so it remains deterministic without moving the failure before the ROLLUP/TOTALS merge path it is meant to protect.

@clickhouse-gh clickhouse-gh Bot added the pr-ci label Jun 7, 2026
CREATE TABLE t0 (c0 Int, c1 Int128) ENGINE = MergeTree ORDER BY tuple();
INSERT INTO t0 SELECT 1, number FROM numbers(40000);
SELECT countDistinct(c1) FROM t0 GROUP BY c0 WITH ROLLUP WITH TOTALS SETTINGS max_memory_usage = 10000000, max_bytes_before_external_group_by = 0, max_bytes_ratio_before_external_group_by = 0; -- { serverError MEMORY_LIMIT_EXCEEDED }
SELECT countDistinct(c1) FROM t0 GROUP BY c0 WITH ROLLUP WITH TOTALS SETTINGS max_memory_usage = 1000000, max_bytes_before_external_group_by = 0, max_bytes_ratio_before_external_group_by = 0; -- { serverError MEMORY_LIMIT_EXCEEDED }

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This lowers the limit enough that the query can fail while building the initial aggregate state, before RollupTransform::generate calls Aggregator::mergeBlocks. That stops the test from covering the regression it links to: issue #93465 failed in the ROLLUP merge path (RollupTransform -> Aggregator::mergeBlocks -> AggregateFunctionUniq::merge), not merely anywhere in this query shape.

Please keep the test deterministic without moving the exception before ROLLUP/TOTALS processing. For example, tune the input/limit so the base aggregation completes and the MEMORY_LIMIT_EXCEEDED is raised from the rollup merge path, or add evidence in the test that the exception still comes from that path.

@clickhouse-gh

clickhouse-gh Bot commented Jun 7, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.50% 84.50% +0.00%
Functions 92.30% 92.30% +0.00%
Branches 77.20% 77.20% +0.00%

Changed lines: No C/C++ source files changed — skipping uncovered code analysis.

Newly covered by added/modified tests: 401 line(s), 55 function(s) across 120 file(s) · Details

Top files
  • src/Storages/StorageReplicatedMergeTree.cpp: 24 line(s)
  • src/AggregateFunctions/AggregateFunctionIntervalLengthSum.cpp: 18 line(s), 40 function(s)
  • src/Common/ZooKeeper/ZooKeeperImpl.cpp: 16 line(s)
  • src/Functions/FunctionsCodingIP.cpp: 12 line(s)
  • src/Storages/ObjectStorageQueue/ObjectStorageQueueSource.cpp: 12 line(s)

Full report

@groeneai

groeneai commented Jun 7, 2026

Copy link
Copy Markdown
Collaborator Author

CI ledger — covered SHA: d94acd3

CI fully finished (Finish Workflow = SUCCESS at 22:11:44Z, all 166 checks completed, no in-progress/queued, >20 min ingestion buffer elapsed).

Zero test failures in CIDB on this commit. mergeStateStatus = CLEAN. AI Code Review check passed. Ball is now with @KochetovNicolai for review.

@alexey-milovidov alexey-milovidov self-assigned this Jun 7, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Jun 7, 2026
Merged via the queue into ClickHouse:master with commit 5c935a2 Jun 7, 2026
166 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 Jun 8, 2026
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 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.

4 participants