Skip to content

Parallel replicas followers follow the initiator whatever their own automatic_parallel_replicas_mode - #125801

Merged
nickitat merged 3 commits into
ClickHouse:masterfrom
groeneai:groeneai/pr-follower-ignores-automatic-mode
Oct 15, 2026
Merged

nickitat merged 3 commits into
ClickHouse:masterfrom
groeneai:groeneai/pr-follower-ignores-automatic-mode

Conversation

@groeneai

@groeneai groeneai commented Oct 14, 2026 •

Copy link
Copy Markdown
Collaborator

Closes: #125757
Related: #121734

Changelog category (leave one):

  • Improvement

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

A replica that takes part in a parallel replicas read now follows the initiator even when its own automatic_parallel_replicas_mode is not 0, for example when allow_feature_tier drops the value the initiator sends, or when INSERT ... SETTINGS automatic_parallel_replicas_mode = 2 SELECT re-applies it on the replica. Previously such a replica read its whole table on its own, so a query could count rows more than once and a distributed INSERT SELECT wrote them once per replica.

Description

With allow_feature_tier = 1 and automatic_parallel_replicas_mode = 1 in the users profile, a query switched to parallel replicas was read in full by every replica: only the initiator registered at the coordinator, and each follower ran the shipped query uncoordinated over its whole table. Its result was merged into the answer whenever it arrived before the initiator cancelled it, and a distributed INSERT SELECT has no such cancel, so every follower inserted the whole source again.

The initiator ships automatic_parallel_replicas_mode = 0 to its followers, but under allow_feature_tier = 1 a replica drops the shipped change of this EXPERIMENTAL setting (clampToSettingsConstraints on a secondary query), and #121734's INSERT ... SETTINGS clause re-applies a non-zero value on the follower. Context::canUseTaskBasedParallelReplicas requires the mode to be 0, so the follower refused to collaborate.

The automatic mode is a decision of the initiator. On a follower (collaborate_with_initiator) canUseTaskBasedParallelReplicas and isParallelReplicasEnabled now ignore it, so the follower answers exactly as it does when the shipped 0 arrives. Initiators are unchanged; no setting or protocol change.

Validation on a 3-node cluster (20M rows): base read 50.6M rows on the initiator with followers ParallelReplicasReadMarks = 0; with the fix 20M rows, 3 replicas used. INSERT SELECT of 1M rows wrote 3M on base and 1M with the fix, for both the tier vector and #121734's vector. The new integration test fails on base and passes 10/10 with the fix. #121734's second shape (the probe declining for the setting in the SELECT's trailing SETTINGS) is not touched here.

🤖 Generated with Claude Code


Workflow [PR]
Sync PR [sync-upstream/pr/125801]

Version info

  • Merged into: 26.10.1.2509-master (included in 26.10 and later)
  • Backported to: 26.10.2.3

groeneai and others added 3 commits October 14, 2026 12:34
…utomatic_parallel_replicas_mode

`Context::canUseTaskBasedParallelReplicas` requires `automatic_parallel_replicas_mode == 0`,
and `canUseParallelReplicasOnFollower` is that predicate plus `collaborate_with_initiator`.
The initiator ships `automatic_parallel_replicas_mode = 0` to its followers, but the value
does not always arrive:

- under `allow_feature_tier = 1`, `TCPHandler::receiveQuery` applies a secondary query's
  settings through `clampToSettingsConstraints`, which silently drops the change of this
  EXPERIMENTAL setting, so the follower keeps the mode from its profile;
- the INSERT's own `SETTINGS automatic_parallel_replicas_mode = 2` is part of the shipped
  query text and is re-applied on the follower (ClickHouse#121734).

Such a follower refused to collaborate and read its whole table. A SELECT is saved by the
coordinator cancelling unused replicas once every range is assigned, at the cost of N times
the work; a distributed INSERT SELECT has no such cancel, so every follower inserted its
whole source table again (3x rows on a 3-replica cluster).

The automatic mode is a decision of the initiator. On a follower `canUseTaskBasedParallelReplicas`
now ignores it (the private impl's flag already exists for cluster engines), and so does
`isParallelReplicasEnabled`, so every follower-side answer equals the answer for mode 0, i.e.
what it already was when the shipped value arrived. Changing the base predicate, not only
`canUseParallelReplicasOnFollower`, keeps the follower-side direct callers (planner checks,
view metadata propagation) consistent within one query. Initiator contexts are unchanged.

New integration test `test_parallel_replicas_feature_tier`: a distributed INSERT SELECT
(300000 rows instead of 100000 before the change) and the heuristic SELECT with unused
replicas kept alive by `parallel_replicas_wait_for_unused_replicas` (every group counted 3
times before the change).

Closes ClickHouse#125757

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ture_tier

The row count alone is also satisfied when the INSERT SELECT falls back to the
local pipeline (`buildInsertSelectPipelineParallelReplicas` returning nothing),
so the test would keep passing without any follower having run. Count the
non-initial `Insert` queries of the INSERT in `system.query_log` on all replicas:
node1 runs its share in the local plan, node2 and node3 run the shipped INSERT.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Condense the follower comment in Context::canUseTaskBasedParallelReplicas to one line.

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

Copy link
Copy Markdown
Collaborator Author
Internal second-model review: adjudication log (click to expand)

Pre-publication review by an independent model (engine: codex; 2 rounds, 2 findings) plus my own cold review (2 nits).

# Sev Finding Verdict Evidence / action
1 ⚠️ test_insert_select checked only the row count, which a local fallback of the INSERT (buildInsertSelectPipelineParallelReplicas declining) would also satisfy AGREE, fixed @ 6e19c57 asserts the two other replicas ran the shipped INSERT (system.query_log, non-initial Insert entries for the query id); a one-off run with the parallel path disabled fails on it
2 💡 Category should be Bug Fix DISAGREE a follower holds a non-zero automatic_parallel_replicas_mode only when that EXPERIMENTAL setting is set (default 0); with it unset the followers collaborate, so the misbehavior is reachable only through an experimental feature
3 💡 Followers of cluster table functions (s3Cluster and friends) also carry collaborate_with_initiator and now get the same exemption AGREE, no change they answer as with automatic_parallel_replicas_mode = 0, the existing follower behavior; a MergeTree read not chosen for coordinated reading stays a plain read
4 💡 The INSERT ... SETTINGS automatic_parallel_replicas_mode = 2 vector (#121734) has no fixture of its own AGREE, no change it reaches the same single predicate as the tested allow_feature_tier vector; measured on a 3-node cluster: 3000000 rows before, 1000000 after

Severity: ❌ blocker / ⚠️ major / 💡 nit. DISAGREE verdicts carry recorded evidence and are
terminal per finding. Findings on hunks unchanged by the fix round are auto-dropped.

Session id: cron:clickhouse-review-slot-9:20261014-131600

@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. 3-process cluster, allow_feature_tier = 1, profile automatic_parallel_replicas_mode = 1: a distributed INSERT SELECT of 1M rows writes 3M rows every time; the new integration test fails on the parent commit in every run.
b Root cause explained? The follower keeps a non-zero automatic_parallel_replicas_mode (the tier clamp of a secondary query drops the shipped 0, or the INSERT's own SETTINGS clause re-applies it), so canUseTaskBasedParallelReplicas and hence canUseParallelReplicasOnFollower are false and it reads its whole table without the coordinator. A SELECT is saved by the cancel of unused replicas; an INSERT SELECT is not.
c Fix matches root cause? Yes. A follower no longer consults the automatic mode, which only the initiator's heuristic decides. No change to settings constraints or the protocol.
d Test intent preserved / new tests added? New integration test test_parallel_replicas_feature_tier (INSERT SELECT row count plus a check that the two other replicas ran the shipped INSERT, and the heuristic SELECT with unused replicas kept alive plus a ParallelReplicasUsedCount positive control).
e Both directions demonstrated? Yes. Parent commit: 300000 rows instead of 100000, every group counted 3 times. Fix: 10/10 in two config flavours. Cluster run of the reported query: followers read 0 coordinated marks before, 760 and 484 of 2443 after.
f Fix is general across code paths? The base predicate is changed, so every follower-side caller (planner checks, view metadata propagation, nested remote reads) answers as for mode 0; isParallelReplicasEnabled gets the same exemption; initiator predicates are unchanged.
g Fix generalizes across inputs (params/datatypes/wrappers)? Both trigger vectors (tier clamp with mode 1, INSERT ... SETTINGS with mode 2), SELECT and INSERT SELECT, tier 0 and 1; test_parallel_replicas_insert_select (local plan and plan-based matrix) still 30/30. Data types are not involved.
h Backward compatible? (maintainer-approved exception only) Yes. No setting, default or protocol change; initiators still ship 0 for older followers.
i Invariants and contracts preserved? For a context with collaborate_with_initiator, every parallel replicas predicate answers as it would with automatic_parallel_replicas_mode = 0; initiator contexts are unchanged. The flag comes from ClientInfo, which per-subquery context copies keep.

Session id: cron:clickhouse-impl-slot-5:20261014-111300

@clickhouse-gh

clickhouse-gh Bot commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [2be9c18]

Summary: ✅


AI Review

Summary

PR #125801, reviewed at 2be9c181413533c82301584eafde3eb840ae75c1, makes coordinated followers ignore their own automatic_parallel_replicas_mode. This prevents uncoordinated full-table reads and duplicated results or INSERT SELECT rows when the initiator's setting is clamped or overridden. No actionable findings remain.

Missing context / blind spots
Final Verdict

✅ Approve. No code changes requested; CI validation remains pending.

@clickhouse-gh clickhouse-gh Bot added the pr-improvement Pull request with some product improvements label Oct 14, 2026
@groeneai

groeneai commented Oct 14, 2026 •

Copy link
Copy Markdown
Collaborator Author

cc @nickitat @devcrafter, could you review this? A follower whose own automatic_parallel_replicas_mode is not 0 (for example because allow_feature_tier drops the shipped 0) refused to collaborate and read its whole table, so on a follower canUseTaskBasedParallelReplicas and isParallelReplicasEnabled now ignore the automatic mode. I can no longer add labels here, so CI needs the can be tested label from one of you.

@clickhouse-gh clickhouse-gh Bot added the comp-parallel-replicas Parallel reading from replicas for query speed (not data replication consistency). label Oct 14, 2026
@nickitat nickitat self-assigned this Oct 14, 2026
@nickitat nickitat added the can be tested Allows running workflows for external contributors label Oct 14, 2026
@nickitat

Copy link
Copy Markdown
Member

@groeneai, would this change be necessary when we promote automatic_parallel_replicas_mode to the production tier?

@groeneai

Copy link
Copy Markdown
Collaborator Author

Only partly. The promotion removes the allow_feature_tier trigger, but a follower can still end up with a non-zero mode of its own and then reads its whole table:

  1. The INSERT's own SETTINGS (Distributed INSERT SELECT duplicates rows: automatic mode leaks to followers #121734): INSERT INTO t SETTINGS automatic_parallel_replicas_mode = 2 SELECT ... ships that clause in the query text (executeInsertSelectWithParallelReplicas strips only the initiator-only names and the leaf timeouts), and the follower applies it on top of the shipped 0. With allow_feature_tier = 0 on 3 replicas: 3M rows written for a 1M-row source, 1M with this PR.
  2. A const constraint on the setting in the follower's profile: clamping the secondary query's settings drops the shipped 0 the same way the tier check does. On current master with allow_feature_tier = 0 and automatic_parallel_replicas_mode = 1 in the profile: 3M rows with the constraint, 1M without.

A narrower alternative after the promotion is to strip automatic_parallel_replicas_mode from the shipped INSERT text, as buildQueryPlanForAutomaticParallelReplicas already does for SELECT, but that does not cover (2). Not reading the follower's own mode covers both.

@nickitat
nickitat added this pull request to the merge queue Oct 15, 2026
Merged via the queue into ClickHouse:master with commit c0db9e8 Oct 15, 2026
520 of 522 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 Oct 15, 2026
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Oct 15, 2026
@robot-clickhouse robot-clickhouse added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Oct 15, 2026
clickhouse-gh Bot added a commit that referenced this pull request Oct 15, 2026
Backport #125801 to 26.10: Parallel replicas followers follow the initiator whatever their own `automatic_parallel_replicas_mode`
nickitat added a commit that referenced this pull request Oct 16, 2026
…LECT

The parser copies the SELECT's trailing `SETTINGS` into the INSERT's own `settings_ast`, and
`executeInsertSelectWithParallelReplicas` shipped that copy unchanged. Stripping only `query.select`
left `automatic_parallel_replicas_mode = 2` in the query text every follower receives. A follower
that predates #125801 re-applies it over the shipped 0,
reads the whole source table instead of its share, and inserts every row again, so in a mixed-version
cluster the previous commit would turn a local but correct insert into row duplication.

Remove `automatic_parallel_replicas_mode` and `parallel_replicas_plan_based` from the top-level
`SETTINGS` carriers of the cloned query before formatting it. The clone is deep, so the user's INSERT
is not changed. This also covers the setting written in the INSERT's own `SETTINGS` clause.

The test now checks that no follower receives the setting in its query text.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
clickgapai pushed a commit to clickgapai/ClickHouse that referenced this pull request Oct 18, 2026
… SELECT settings

`buildInsertSelectPipelineParallelReplicas` pins `automatic_parallel_replicas_mode = 0` on its
context so that a distributed INSERT SELECT uses parallel replicas regardless of the automatic
mode. The suitability probe (`isSuitableForInsertSelectWithParallelReplicas`) builds the SELECT with
`InterpreterSelectQueryAnalyzer`, which re-applies the SELECT's own `SETTINGS` clause on top of that
context, so `INSERT INTO t SELECT ... SETTINGS automatic_parallel_replicas_mode = 2` brought the mode
back, the probe found no parallel replicas read and the insert silently ran on the initiator alone.

Strip the setting from the SELECT's top-level `SETTINGS` together with `parallel_replicas_plan_based`,
which had the same problem and was fixed the same way. `execute` restores `query.select` afterwards.

The new test also covers the setting in the INSERT's own `SETTINGS` clause, which made every replica
insert the whole source table until ClickHouse#125801.

Closes: ClickHouse#121734

Co-Authored-By: Claude Opus 5.5 <[email protected]>
opensource-fork-sync Bot pushed a commit to cla7aye15I4nd/opensource-ClickHouse__ClickHouse that referenced this pull request Oct 19, 2026
…n followers too

A `*Cluster` worker executing a forwarded INSERT SELECT has `collaborate_with_initiator`
set, so after ClickHouse#125801 `canUseParallelReplicasOnFollower` is true even when the initiator
runs with `automatic_parallel_replicas_mode = 1`. The insert part kept parallel replicas on
because it was disabled only under `canUseParallelReplicasOnInitiator`, so a materialized
view that joins a MergeTree table started a coordinated read, and the initiator threw
`Coordinator for parallel reading from replicas is not initialized`. With mode 0 the same
query failed before ClickHouse#125801 as well.

Gate on `canUseTaskBasedParallelReplicas` in `addInsertToSelectPipeline` and
`buildInsertPipeline`, so the insert part never uses parallel replicas, wherever it runs.

ClickHouse#126127

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@nickitat nickitat added v26.6-must-backport v26.8-must-backport and removed pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore labels Nov 4, 2026
@nickitat nickitat removed the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Nov 4, 2026
@robot-ch-test-poll robot-ch-test-poll added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Nov 4, 2026
robot-clickhouse-ci-2 added a commit that referenced this pull request Nov 11, 2026
Cherry pick #125801 to 26.9: Parallel replicas followers follow the initiator whatever their own `automatic_parallel_replicas_mode`
robot-clickhouse added a commit that referenced this pull request Nov 11, 2026
…iator whatever their own `automatic_parallel_replicas_mode`
clickhouse-gh Bot added a commit that referenced this pull request Nov 12, 2026
Backport #125801 to 26.9: Parallel replicas followers follow the initiator whatever their own `automatic_parallel_replicas_mode`
leshikus pushed a commit that referenced this pull request Nov 13, 2026
…iator whatever their own `automatic_parallel_replicas_mode`
@robot-clickhouse robot-clickhouse added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Nov 18, 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 comp-parallel-replicas Parallel reading from replicas for query speed (not data replication consistency). pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-improvement Pull request with some product improvements pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR pr-synced-to-cloud The PR is synced to the cloud repo v26.6-must-backport v26.8-must-backport v26.10-must-backport

Projects

None yet

5 participants