Skip to content

Fix 01666_merge_tree_max_query_limit under parallel replicas - #112385

Merged
alexey-milovidov merged 1 commit into
ClickHouse:masterfrom
groeneai:fix-01666-parallel-replicas-throttle
Jul 29, 2026
Merged

alexey-milovidov merged 1 commit into
ClickHouse:masterfrom
groeneai:fix-01666-parallel-replicas-throttle

Conversation

@groeneai

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

Related: #111230. Found by CI triage on #108091.

01666_merge_tree_max_query_limit fails on Stateless tests (amd_llvm_coverage, ParallelReplicas, s3 storage, parallel), including on master (86d92d6b4119f54a, 71ec9afe6fed1be8, both Failed 3 out of 3 reruns), with Expected error code: 202 but got: 0. It became reachable on that lane when #111230 removed its entry from tests/parallel_replicas_blacklist.txt; the entry dated from January 2025 with the reason !!!Reason: Timeout!.

Root cause: the throttle the test asserts is a per-table counter keyed on query id (checkLimits -> getQueryIdHolder -> insertQueryIdOrThrowNoLock). ReadFromParallelRemoteReplicasStep::createPipeForSingeReplica builds each secondary RemoteQueryExecutor without calling setQueryId, so the query id is sent empty and Context::setCurrentQueryId turns it into a random UUID. With max_concurrent_queries = 1 the long running query that should hold the single slot becomes several differently-keyed reads of the same table on the same server, so it competes against itself and is rejected instead of holding the slot, and the last assertion gets code 0. system.query_log shows two secondary reads of the holder's own statement rejected with 202, then the holder itself, within 204 ms. If that lands before read_rows is visible, wait_for_query_to_start instead spins on sum(read_rows) over zero rows, which is the recorded timeout mode. ReadFromParallelReplicasStep omits setQueryId the same way.

The fix pins parallel replicas off for the test's client, so all three 202 assertions keep running on every lane. It neither restores the blacklist entry nor undoes #111230, which was right for the other 13 tests it removed. A parallel-replicas-coherent engine throttle would be a separate change.

Validation, one server, both directions: unpinned 0/30 runs match the reference (4 202 but got: 0, 21 hang, 5 other); pinned 30/30. With randomization on, 50/50 pass on local disk and 50/50 on S3, 0 skipped in either. --enable_parallel_replicas 0 alone suffices; --automatic_parallel_replicas_mode 0 is redundant here, kept to match 03164_selects_with_pk_usage_profile_event.sh and 03518_key_condition_binary_search.sh.

The test asserts a per-table concurrency throttle that is keyed on query id:
checkLimits -> MergeTreeData::getQueryIdHolder -> insertQueryIdOrThrowNoLock
throws TOO_MANY_SIMULTANEOUS_QUERIES (202) once query_id_set.size() reaches
max_concurrent_queries.

ReadFromParallelReplicas never calls RemoteQueryExecutor::setQueryId, so every
secondary read is sent with an empty query id, and Context::setCurrentQueryId
generates a random UUID when the received id is empty. With
max_concurrent_queries = 1 the long running query that is supposed to occupy the
single slot therefore becomes several reads of the same table on the same server,
each with a different query id, so it competes against itself and is rejected
instead of holding the slot. Nothing holds the slot when the final assertion
runs, so it returns code 0 and the test fails with
"Expected error code: 202 but got: 0". When the rejection lands before read_rows
becomes visible, wait_for_query_to_start instead spins on sum(read_rows) over
zero rows, which is the timeout mode recorded in the 2025 blacklist entry.

The test was excluded from the ParallelReplicas lane by
tests/parallel_replicas_blacklist.txt from January 2025 until ClickHouse#111230 removed the
entry, which is what made this reachable. This change pins parallel replicas off
for the test's client instead of restoring the entry, so all three 202
assertions keep executing on every lane.

Validated on one server in both directions: without the pin 0 of 30 runs match
the reference, with the pin 30 of 30 match. On ordinary lanes with randomization
enabled, 50/50 pass on local disk and 50/50 on S3 storage with nothing skipped.
--enable_parallel_replicas 0 alone is sufficient here;
--automatic_parallel_replicas_mode 0 is redundant and is kept only to match the
two existing precedents in tests/queries/0_stateless.

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

Copy link
Copy Markdown
Collaborator Author
Internal second-model review (2 findings, both addressed; second-model gate: 0 findings)

An independent reviewer read the change cold, then a second model re-reviewed the diff.
Verdicts and the evidence behind each are below.

⚠️ citation-1 (major) - AGREED, fixed in the description. The draft attributed the
missing setQueryId call to ReadFromParallelReplicas. No class has that name: it is the
getName string of ReadFromParallelReplicasStep, while the step actually exercised on
this lane is ReadFromRemoteParallelReplicas, i.e. ReadFromParallelRemoteReplicasStep
(src/Processors/QueryPlan/ReadFromRemote.h:89). The conclusion is unchanged, because both
steps build their RemoteQueryExecutor without ever setting a query id
(ReadFromRemote.cpp:1174-1189 and ReadFromParallelReplicas.cpp:211-225 call only
setLogger, setMainTable, setDistributedFanout), and RemoteQueryExecutor::setQueryId
has no call site in src/ at all. The description now names the exercised class and notes
the sibling shares the omission. The test file itself cites no class name, so the change is
unaffected.

💡 evidence-lineno-1 (nit) - AGREED, noted. An internal note cited
ReadFromMergeTree.cpp:4150 for the checkLimits call; it is at :4156, inside
ReadFromMergeTree::initializePipeline. The substantive claim, that the call is
unconditional with no parallel-replicas guard, is correct.

Second-model gate: 0 findings. It read the changed test, the reference, the two sibling
tests that pin the same settings, the harness option-precedence path, the throttle and
query-id paths, and both parallel-replica implementations.

Checks the reviewer ran independently rather than taking from the author:

  • RemoteQueryExecutor::setQueryId has zero call sites in src/, and
    git log -S setQueryId -- ReadFromRemote.cpp is empty, so this is a long-standing
    omission rather than a recent regression.
  • TCPHandler.cpp:2728 -> Context.cpp:3576-3591: an empty received id becomes a random
    UUID, so each secondary read is keyed differently in the per-table query_id_set
    (MergeTreeData.cpp:11592-11604).
  • The lane really enables parallel replicas: ci/defs/job_configs.py:744 ->
    ci/jobs/functional_tests.py:163 -> tests/config/install.sh:32,484-485, which installs
    enable_parallel_replicas 1 with a three-replica cluster.
  • Blacklist provenance: exactly two commits touch this entry, added 2026-01-08 under
    !!!Reason: Timeout! and removed by Remove stale entries from parallel_replicas_blacklist.txt #111230, which removed 14 names in a single-file diff.
  • All the cited failing master commits are ancestors of master, none carries the blacklist
    entry, and each already carries the earlier max_threads = 1 pin, so that pin is correctly
    not offered as the fix for this failure.
  • Line 12 precedes all 15 later uses of $CLICKHOUSE_CLIENT; the only other statement is the
    system.processes poll, which never reads the table, so coverage is complete.
  • Both quoted precedents exist verbatim on master.
  • The redundancy of --automatic_parallel_replicas_mode 0 is real:
    InterpreterSelectQueryAnalyzer.cpp:138-152 clears the automatic mode when parallel
    replicas are off. The description says so rather than claiming a second mechanism.
  • Carrier sweep: min_marks_to_honor_max_concurrent_queries appears in no other test, and the
    other tests mentioning max_concurrent_queries use the unrelated server-level setting, so no
    sibling change is owed.

No blocker was raised. The three 202 assertions and the reference file are untouched, and the
test is skipped on no lane.

@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? YES. Local server with the lane's profile (tests/config/{users.d,config.d}/enable_parallel_replicas.xml with the replica ports rewritten to the local port, plus config.d/listen.xml): the unmodified test fails 30/30 runs. Not a low-rate flake.
b Root cause explained? YES. The asserted throttle is a per-table counter keyed on query id (checkLimits -> getQueryIdHolder -> insertQueryIdOrThrowNoLock, which throws 202 once query_id_set.size() >= max_queries). ReadFromParallelRemoteReplicasStep::createPipeForSingeReplica builds each secondary RemoteQueryExecutor without calling setQueryId (and ReadFromParallelReplicasStep omits it the same way), so every secondary read is sent with an empty query id and Context::setCurrentQueryId assigns a random UUID to an empty id. With max_concurrent_queries = 1 the long running query that should hold the single slot becomes several differently-keyed reads of the same table on the same server, competes against itself, and is rejected instead of holding the slot, so the final assertion sees code 0.
c Fix matches root cause? YES. The cause is that parallel replicas multiplies one statement into several differently-keyed reads of the counted table; the fix turns that multiplication off for this test's client. Not a widened bound, a reduced dataset, a blanket no-random-* tag, or a skip.
d Test intent preserved / new tests added? YES. All three 202 assertions still execute on every lane, stdout is byte-identical to the unchanged .reference, and both 50-run sweeps report 0 tests skipped. No new test: the deliverable is making an existing test valid on a lane where it could not pass.
e Both directions demonstrated? YES, same server and config. Unpinned: 0/30 runs match the reference (4 Expected error code: 202 but got: 0, 21 hang, 5 other mismatch). Pinned: 30/30 match. system.query_log confirms the mechanism both ways: unpinned, two secondary reads of the holder's own statement and then the holder itself are rejected with 202 within 204 ms; pinned, zero secondary reads and zero 202s for that statement, and the holder ends only via the test's own KILL QUERY.
f Fix is general across code paths? YES. grep -rl min_marks_to_honor_max_concurrent_queries tests/queries/0_stateless/ --include='*.sh' --include='*.sql' returns only this test, so there is no second carrier of the same assertion. Within the file every statement that reads the table goes through ${CLICKHOUSE_CLIENT}, so one appended pin covers all of them; the only non-client statement reads system.processes, which never reaches the throttle. The other 13 tests removed by #111230 were swept and show 0 failures each, so no sibling fix is owed.
g Fix generalizes across inputs (params/datatypes/wrappers)? N/A for type wrappers (test-only change; no source, no data types). The settings dimension is covered: randomization on in both 50-run sweeps, plus a 3-arm flag matrix (whole pin removed -> FAIL; --enable_parallel_replicas 0 alone -> PASS; --automatic_parallel_replicas_mode 0 alone -> FAIL).
h Backward compatible? (maintainer-approved exception only) N/A. Test-only: no setting defaults, no SettingsChangesHistory.cpp, no serialization format, no experimental gate.
i Invariants and contracts preserved? YES. The pin appends to CLICKHOUSE_CLIENT rather than reassigning it, so the harness's own --database, --log_comment, --send_logs_level and --client_logs_file flags survive; it sits after shell_config.sh is sourced so it is not overwritten; $CLICKHOUSE_CURL is untouched; and the existing max_block_size = 1, max_threads = 1 pin on the long running query is unchanged. Both flags were confirmed accepted by the client rather than assumed.

Session id: cron:clickhouse-review-slot-49:20260729-015200

@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @devcrafter — could you review this? It pins parallel replicas off for 01666_merge_tree_max_query_limit, which asserts a per-table concurrency throttle keyed on query id: under parallel replicas the holder query becomes several differently-keyed reads of the same table, so it competes against itself for the single slot instead of holding it. The test had been on the blacklist since January 2025 for the timeout form of the same problem, and the harness rewrites a blacklisted FAIL to SKIPPED, so only its passes were visible when the entry was reviewed. The other 13 entries removed in #111230 are clean.

This was referenced Jul 29, 2026
@PedroTadim PedroTadim added the can be tested Allows running workflows for external contributors label Jul 29, 2026
groeneai added a commit to groeneai/ClickHouse that referenced this pull request Jul 29, 2026
Picks up merged ClickHouse#112385, which owns the 01666_merge_tree_max_query_limit
flakiness this PR's last CI run reported (ledger rule 3c).

All 19 conflicts git reported were spurious. The history is criss-cross:
two merge bases (5853d88 and e0b3304) exist because master
was merged into this branch twice by the maintainer, so the recursive
strategy synthesized a virtual base and diffed against that instead of
the real one. Verified for every conflicted path that this branch is
byte-identical to 5853d88 (the base that is an ancestor of
origin/master), so master's content is the only correct resolution and
was taken wholesale. This is also why git merge-tree reported a clean
tree for the same pair.

Master has not touched any of the three files this branch owns, so the
unique diff is unchanged: patch-id cd69a0d9107a29 before and after.
groeneai added a commit to groeneai/ClickHouse that referenced this pull request Jul 29, 2026
alexey-milovidov added a commit that referenced this pull request Jul 30, 2026
…arity

Pulls in the fix for `01666_merge_tree_max_query_limit` under parallel
replicas (#112385), which was
the only CI failure in this pull request and was failing across hundreds of
unrelated pull requests in the same job.
alexey-milovidov added a commit that referenced this pull request Jul 30, 2026
The only red on 91d1694 was 01666_merge_tree_max_query_limit
(amd_llvm_coverage), whose fix #112385 merged into master after the
previous merge; this pulls it in. Master did not touch play.html.
@alexey-milovidov alexey-milovidov mentioned this pull request Jul 31, 2026
1 task done
groeneai added a commit to groeneai/ClickHouse that referenced this pull request Aug 3, 2026
@alexey-milovidov alexey-milovidov mentioned this pull request Aug 5, 2026
1 task done
groeneai added a commit to groeneai/ClickHouse that referenced this pull request Aug 28, 2026
The re-run of this PR's workflow (attempt 2 of run 30397395779, created
2026-07-28) checks out refs/pull/112265/merge as it stood then, so the tested
tree was this head merged with master at 71ec9af (2026-07-28 19:52 UTC). That
tree predates c5b976c (PR ClickHouse#112385, merged 2026-07-29 14:35 UTC), which pins
--enable_parallel_replicas 0 in 01666_merge_tree_max_query_limit.sh. Without
that pin the holder query becomes several reads of the same table on the same
server and is rejected by its own max_concurrent_queries = 1 slot, so the last
throttle assertion sees no holder. Merging master brings the pin onto the
branch, so the outcome no longer depends on which merge base CI picks.

Pinned master: 6764095
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