Skip to content

Fix flaky test 02493_max_streams_for_merge_tree_reading - #102818

Merged
murphy-4o merged 1 commit into
ClickHouse:masterfrom
groeneai:fix/02493-read-in-order-threshold-flaky
Apr 16, 2026
Merged

murphy-4o merged 1 commit into
ClickHouse:masterfrom
groeneai:fix/02493-read-in-order-threshold-flaky

Conversation

@groeneai

@groeneai groeneai commented Apr 15, 2026 •

Copy link
Copy Markdown
Collaborator

Pin read_in_order_two_level_merge_threshold to prevent the randomized value (0–100) from triggering a two-level merge pipeline when the number of parts exceeds the threshold.

Root cause

INSERT with max_insert_threads=8 creates ~8 parts. When read_in_order_two_level_merge_threshold is randomized below this count (e.g. 6), the read-in-order pipeline switches to a two-level merge structure. Combined with max_streams_to_max_threads_ratio=8 (used in the last test query), the aggregation collapses to 1 thread, eliminating the expected Resize 1 → 4 step from the pipeline output.

The previous fix (adding no-parallel-replicas tag) addressed a different failure mode but this one persisted — 66+ failures across 30+ unrelated PRs in the last 30 days.

Reproduction

-- Create table with 8+ unmerged parts
CREATE TABLE t (x UInt64) ENGINE = MergeTree ORDER BY x
  SETTINGS max_bytes_to_merge_at_max_space_in_pool=1;
INSERT INTO t SELECT number FROM numbers_mt(10000000) SETTINGS max_insert_threads=8;

-- This returns empty (no Resize) — the failure:
SET query_plan_remove_redundant_sorting=0;
SET read_in_order_two_level_merge_threshold=6;  -- randomized, can be < 8
EXPLAIN PIPELINE SELECT sum(x) FROM (SELECT x FROM t ORDER BY x)
  SETTINGS max_threads=4, max_streams_for_merge_tree_reading=16,
    allow_asynchronous_read_from_io_pool_for_merge_tree=1,
    max_streams_to_max_threads_ratio=8,
    optimize_read_in_order=1, query_plan_read_in_order=1;

-- With the fix (threshold=100), Resize 1 → 4 appears correctly

Fix

Pin read_in_order_two_level_merge_threshold = 100 before the echo block. This is a targeted fix — only the specific setting that causes the issue is pinned, and the test's intent (verifying max_streams_for_merge_tree_reading behavior) is fully preserved.

Verified: 100/100 passes with full randomization locally.

Closes #101657

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 to CHANGELOG.md):

  • no changes

Version info

  • Merged into: 26.4.1.983

Pin read_in_order_two_level_merge_threshold to prevent the randomized
value (0-100) from triggering a two-level merge pipeline when the
number of parts exceeds the threshold.

Root cause: INSERT with max_insert_threads=8 creates ~8 parts. When
read_in_order_two_level_merge_threshold is randomized below this
count (e.g. 6), the read-in-order pipeline switches to a two-level
merge structure. Combined with max_streams_to_max_threads_ratio=8
(used in the last test query), the aggregation collapses to 1 thread,
eliminating the expected Resize 1 → 4 step from the pipeline output.

Closes ClickHouse#101657

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

Copy link
Copy Markdown
Collaborator Author

Pre-PR Validation Gate (Session: cron:clickhouse-ci-task-worker:20260415-134500)

a) Deterministic repro? ✅ Yes. Create a table with 8+ unmerged parts, set read_in_order_two_level_merge_threshold=6, and run EXPLAIN PIPELINE with max_streams_to_max_threads_ratio=8 + read-in-order settings. The Resize 1 → 4 step disappears deterministically.

b) Root cause explained? ✅ read_in_order_two_level_merge_threshold is randomized 0–100 by the CI test runner. The INSERT with max_insert_threads=8 creates ~8 parts. When the threshold falls below the part count, the pipeline activates a two-level merge structure. Combined with max_streams_to_max_threads_ratio=8 (the last test query's distinguishing setting), the aggregation collapses to a single thread, removing the Resize 1 → 4 step that the reference file expects.

c) Fix matches root cause? ✅ Pins read_in_order_two_level_merge_threshold = 100 — exactly the setting whose randomization triggers the failure. No broad tags used.

d) Test intent preserved? ✅ The test verifies max_streams_for_merge_tree_reading behavior across different read modes (sync, async, read-in-order). The pinned setting only prevents the irrelevant two-level merge optimization from altering the pipeline structure; all max_streams_for_merge_tree_reading behavior is still exercised.

e) Both directions demonstrated? ✅ With 9 parts and threshold=6: empty output (failure). With threshold=100: Resize 1 → 4 (pass). 100/100 passes with full randomization after fix.

@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @vdimir — could you review this? It pins read_in_order_two_level_merge_threshold to fix the chronic flaky test — the randomized value (0–100) triggers a two-level merge pipeline when max_insert_threads=8 creates more parts than the threshold, changing the read-in-order pipeline structure and eliminating the expected Resize 1 → 4 step.

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

clickhouse-gh Bot commented Apr 15, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [2625f0d]

Summary: ✅


AI Review

Summary

This PR stabilizes tests/queries/0_stateless/02493_max_streams_for_merge_tree_reading.sql by pinning read_in_order_two_level_merge_threshold to avoid randomized two-level merge-plan shape changes. The change is narrow, test-only, and directly aligned with the reported flaky behavior. I did not find correctness, safety, compatibility, or performance issues in the patch.

ClickHouse Rules
Item Status Notes
Deletion logging ➖
Serialization versioning ➖
Core-area scrutiny ➖
No test removal ✅
Experimental gate ➖
No magic constants ➖
Backward compatibility ✅
SettingsChangesHistory.cpp ➖
PR metadata quality ✅
Safe rollout ✅
Compilation time ✅
No large/binary files ✅
Final Verdict
  • Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-ci label Apr 15, 2026
@murphy-4o murphy-4o self-assigned this Apr 16, 2026
@murphy-4o
murphy-4o enabled auto-merge April 16, 2026 03:33
@murphy-4o
murphy-4o added this pull request to the merge queue Apr 16, 2026
Merged via the queue into ClickHouse:master with commit 2f268ac Apr 16, 2026
161 checks passed
@robot-ch-test-poll robot-ch-test-poll added the pr-synced-to-cloud The PR is synced to the cloud repo label Apr 16, 2026
huldarchen pushed a commit to huldarchen/ClickHouse that referenced this pull request May 12, 2026
…ree-memory limiter

The test exercises `EXPLAIN PIPELINE` with `max_threads=32` and asserts the
resulting pipeline contains `Resize 16 → 32`. Since ClickHouse#100383 (`Limit max_threads
and max_insert_threads based on available free memory`), `max_threads` is
trimmed when free memory is below `max_threads_min_free_memory_per_thread`
(default 1 GiB) per requested thread.

On `Stateless tests (amd_llvm_coverage_per_test, per_test_coverage, 7/8)`
the per-test coverage instrumentation reduces available memory, so the
limiter clamps the requested 32 threads to ~24 and the test fails:

```
- Resize 16 → 32
- MergeTreeSelect(pool: ReadPool, algorithm: Thread) × 16 0 → 1
+ Resize 16 → 24
+ MergeTreeSelect(pool: ReadPool, algorithm: Thread) × 16 0 → 1
```

Disable both `max_threads_min_free_memory_per_thread` and
`max_insert_threads_min_free_memory_per_thread` in the test SET block so
the requested thread count is preserved regardless of concurrent memory
pressure. Same fix pattern applied to `02343_aggregation_pipeline.sql` in
the original PR (commit de505d1).

The test had its last master failure on the wave introduced by ClickHouse#100383 on
2026-05-02 03:55 UTC, ~4 hours after ClickHouse#100383 merged on 2026-05-01 23:22 UTC.
CIDB shows 10 master hits in the 14-day window since, all on
`per_test_coverage`. The related auto-generated `Flaky test` issue ClickHouse#101657
covers an older failure mode that was fixed by ClickHouse#102818; this PR addresses
a separate failure mode introduced by ClickHouse#100383.

**Reproduction (deterministic):**

```bash
PATH=build/programs:$PATH \
  CLICKHOUSE_CLIENT_OPT='--max_threads_min_free_memory_per_thread=24000000000 --max_insert_threads_min_free_memory_per_thread=24000000000' \
  tests/clickhouse-test 02493_max_streams_for_merge_tree_reading
```

Without the fix the test fails with `Resize 4 → 4` instead of
`Resize 16 → 32` (same failure shape as CI). With the fix it passes 10/10
runs with the limiter injected and 50/50 runs without it.

CI failure example: https://s3.amazonaws.com/clickhouse-test-reports/json.html?REF=master&sha=bbf2861532b8871fbc0e3d7d3362e1f0e3fabf3f&name_0=NightlyCoverage&name_1=Stateless%20tests%20%28amd_llvm_coverage_per_test%2C%20per_test_coverage%2C%207%2F8%29
PR introducing the regression: ClickHouse#100383

Co-Authored-By: Claude Opus 4.7 <[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 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.

Flaky test: 02493_max_streams_for_merge_tree_reading

4 participants