Skip to content

Make 02981_insert_select_resize_to_max_insert_threads deterministic under free-memory limiter - #104449

Merged
alexey-milovidov merged 2 commits into
ClickHouse:masterfrom
groeneai:fix-flaky-02981-insert-select-resize
Jun 26, 2026
Merged

alexey-milovidov merged 2 commits into
ClickHouse:masterfrom
groeneai:fix-flaky-02981-insert-select-resize

Conversation

@groeneai

@groeneai groeneai commented May 9, 2026 •

Copy link
Copy Markdown
Collaborator

The test exercises EXPLAIN PIPELINE INSERT INTO ... SELECT with max_insert_threads=8 and asserts that the resulting pipeline contains exactly 8 sinks. Since #100383 (Limit max_threads and max_insert_threads based on available free memory), max_insert_threads is trimmed when the server has less than max_insert_threads_min_free_memory_per_thread (default 4 GiB) of free memory per requested thread.

In Stateless tests (amd_llvm_coverage_per_test, per_test_coverage, 8/8) the per-test coverage instrumentation reduces available memory, so the limiter clamps the requested 8 threads to 5–7 and the test fails:

- inserting into a remote table from local with concurrency equal to max_insert_threads
- 8
+ 5

Pass --max_threads_min_free_memory_per_thread=0 and --max_insert_threads_min_free_memory_per_thread=0 on every clickhouse-client invocation in the test so the requested max_insert_threads=8 is preserved regardless of concurrent memory pressure. Same fix pattern applied to 02343_aggregation_pipeline.sql in the original PR.

The test had no master failures before #100383 was merged on 2026-05-01 23:22 UTC; the first failure landed on 2026-05-02 03:55 UTC, ~4 hours later. CIDB shows 7 master hits in the 14-day window since, all on per_test_coverage. There is no flaky test GitHub issue for it yet.

Reproduction (deterministic):

I injected the limiter manually into CLICKHOUSE_CLIENT_OPT to simulate the per-test-coverage memory pressure:

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 02981_insert_select_resize_to_max_insert_threads

Before the fix the test fails with 4 sinks instead of 8 (the same failure shape as CI):

- inserting into a remote table from local with concurrency equal to max_insert_threads
- 8
+ 4

After the fix the test passes 50/50 runs both with and without the injected throttle.

CI failure example: https://s3.amazonaws.com/clickhouse-test-reports/json.html?REF=master&sha=8eaaf96ca45fffc4f228cc8892660bd20e9f4b99&name_0=NightlyCoverage&name_1=Stateless%20tests%20%28amd_llvm_coverage_per_test%2C%20per_test_coverage%2C%208%2F8%29

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

...

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

Version info

  • Merged into: 26.7.1.125

… under free-memory limiter

The test exercises `EXPLAIN PIPELINE INSERT INTO ... SELECT` with
`max_insert_threads=8` and asserts that the resulting pipeline contains
exactly 8 sinks. Since ClickHouse#100383 (`Limit max_threads and max_insert_threads
based on available free memory`), `max_insert_threads` is trimmed when
the server has less than `max_insert_threads_min_free_memory_per_thread`
(default 4 GiB) of free memory per requested thread.

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

```
- inserting into a remote table from local with concurrency equal to max_insert_threads
- 8
+ 5
```

Pass `--max_threads_min_free_memory_per_thread=0` and
`--max_insert_threads_min_free_memory_per_thread=0` on every
`clickhouse-client` invocation in the test so the requested
`max_insert_threads=8` is preserved regardless of concurrent memory
pressure. Same fix pattern applied to `02343_aggregation_pipeline.sql`
in the original PR.

Reproduced locally by injecting
`--max_threads_min_free_memory_per_thread=24000000000`
`--max_insert_threads_min_free_memory_per_thread=24000000000`
into `CLICKHOUSE_CLIENT_OPT`: before the fix the test fails with 4 sinks
instead of 8; after the fix it passes 50/50 runs both with and without
the injected throttle.

CI failure example: https://s3.amazonaws.com/clickhouse-test-reports/json.html?REF=master&sha=8eaaf96ca45fffc4f228cc8892660bd20e9f4b99&name_0=NightlyCoverage&name_1=Stateless%20tests%20%28amd_llvm_coverage_per_test%2C%20per_test_coverage%2C%208%2F8%29
@groeneai

groeneai commented May 9, 2026

Copy link
Copy Markdown
Collaborator Author

Pre-PR validation gate (TASK.md Phase 4 Step 9):

a) Deterministic repro? Yes. Inject --max_threads_min_free_memory_per_thread=24000000000 --max_insert_threads_min_free_memory_per_thread=24000000000 into CLICKHOUSE_CLIENT_OPT and the test fails with 4 sinks instead of 8 on every run.

b) Root cause explained? Yes. PR #100383 added a free-memory based limiter to max_threads/max_insert_threads (getMaxThreadsForAvailableMemory in src/Common/MemoryTrackerUtils.cpp). Default max_insert_threads_min_free_memory_per_thread is 4 GiB; on per_test_coverage the per-test coverage instrumentation reduces available memory enough that the requested 8 threads get clamped to 5–7 (matches the observed 5/6/7 in CI).

c) Fix matches root cause? Yes. Setting both *_min_free_memory_per_thread settings to 0 disables the limiter for these client invocations only — same pattern Alexey used for 02343_aggregation_pipeline.sql (commit de505d131eb).

d) Test intent preserved? Yes. The test's purpose is to verify that the resize stage produces exactly max_insert_threads sinks. Disabling the memory limiter does not weaken that assertion — it ensures the requested thread count is honored so the assertion can be checked.

e) Both directions demonstrated? Yes.

  • Without fix + injected throttle → FAIL (4 sinks instead of 8) — same diff shape as CI.
  • With fix + injected throttle → PASS, 50/50 runs.
  • With fix + default settings → PASS, 50/50 runs.

f) Fix is general? Test-only fix. The underlying behavior (memory limiter trimming threads) is intentional per #100383 and not a bug. A sibling test 02493_max_streams_for_merge_tree_reading shows the same failure pattern under the limiter and will be addressed in a follow-up PR.

Session: cron:clickhouse-ci-task-worker:20260509-091500

@groeneai

groeneai commented May 9, 2026

Copy link
Copy Markdown
Collaborator Author

cc @alexey-milovidov — could you review this? Same fix pattern as your de505d131eb for 02343_aggregation_pipeline, applied to the sibling test 02981_insert_select_resize_to_max_insert_threads which started failing on per_test_coverage ~4h after #100383 was merged.

@tiandiwonder tiandiwonder self-assigned this May 9, 2026
@alexey-milovidov alexey-milovidov added the can be tested Allows running workflows for external contributors label May 9, 2026
@clickhouse-gh

clickhouse-gh Bot commented May 9, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [1016567]

Summary: ✅


AI Review

Summary

This PR makes 02981_insert_select_resize_to_max_insert_threads deterministic by disabling max_threads_min_free_memory_per_thread and max_insert_threads_min_free_memory_per_thread for the three clickhouse-client EXPLAIN PIPELINE INSERT INTO ... SELECT checks whose expected output depends on exactly eight sinks. The change is narrowly scoped to the flaky test, matches the pattern already used by sibling pipeline tests, and I found no code-review blockers or major issues.

Missing context / blind spots
  • ⚠️ The latest PR workflow was still pending at review time (Fast test and the aggregate PR status). A completed PR run, especially the affected stateless test, would close this runtime validation gap.
Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-ci label May 9, 2026
@tiandiwonder

Copy link
Copy Markdown
Contributor

@groeneai please fix the ci failure in a new PR: Logical error: 'std::exception. Code: 1001, type: std::out_of_range, e.what() = unordered_map::at: key not found (version 26.5.1.465), Stack trace: (STID: 2508-34fb)

@groeneai

Copy link
Copy Markdown
Collaborator Author

@tiandiwonder — STID 2508-34fb (and sibling stack-frame variants 2508-3132, 2508-3307, 2508-3cd3) are the chronic InsertDependenciesBuilder::createSelect unordered_map::at: key not found family. Tracking under our umbrella P0 task 2026-04-29-ci-p0-stid-2508-34fb-logical-error-std.

⚠️ Status update on the in-flight fix:

We've publicly committed to opening our own follow-up fix PR targeting the residual code path. Investigation in progress: identifying which .at() site in createSelect (lines ~1320-1382) still throws after the #98779 fix to createPostSink (line 1500). Most likely candidate: a stale-key scenario in the multi-MV-shared-target diamond pattern (2 MVs both TO the same target table) under concurrent DDL — see also #104448.

Tracking PR will be opened separately; this PR (#104449) is not blocked by the family bug.

@clickhouse-gh

clickhouse-gh Bot commented Jun 9, 2026

Copy link
Copy Markdown
Contributor

Dear @tiandiwonder, you haven't been active on this PR for 30 days. You will be unassigned. Will you continue working on it? If so, please feel free to reassign yourself.

@tiandiwonder

Copy link
Copy Markdown
Contributor

@groeneai please merge the PR with latest master and then check ci result.

@groeneai

Copy link
Copy Markdown
Collaborator Author

@tiandiwonder done. Merged latest master into the branch (clean, zero conflicts; master had not touched the test file). New HEAD: 10165671644. The only diff vs master is still the approved fix to 02981_insert_select_resize_to_max_insert_threads.sh. CI re-running now.

@clickhouse-gh

clickhouse-gh Bot commented Jun 10, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.60% 84.70% +0.10%
Functions 92.30% 92.30% +0.00%
Branches 77.20% 77.30% +0.10%

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

Newly covered by added/modified tests: 922 line(s), 82 function(s) across 116 file(s) · Details

Top files
  • src/Storages/ObjectStorage/DataLakes/Iceberg/ComplexTypeSchemaProcessorFunctions.cpp: 284 line(s), 19 function(s)
  • src/Server/ACME/Client.cpp: 169 line(s), 8 function(s)
  • src/Server/ACME/API.cpp: 99 line(s), 9 function(s)
  • src/Common/Crypto/KeyPair.cpp: 28 line(s), 5 function(s)
  • src/Server/CertificateReloader.cpp: 24 line(s), 2 function(s)

Full report

@alexey-milovidov
alexey-milovidov merged commit 60659b3 into ClickHouse:master Jun 26, 2026
165 of 166 checks passed
@robot-clickhouse robot-clickhouse added the pr-synced-to-cloud The PR is synced to the cloud repo label Jun 26, 2026
@filimonov filimonov mentioned this pull request Sep 7, 2026
2 of 30 tasks
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
…sert-select-resize

Make `02981_insert_select_resize_to_max_insert_threads` deterministic under free-memory limiter
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