Skip to content

Size parallel replicas mark segments by the active replica count capped by max_parallel_replicas - #133260

Merged
alexey-milovidov merged 2 commits into
ClickHouse:masterfrom
groeneai:groeneai/parallel-replicas-segment-size-active-count
Nov 20, 2026
Merged

alexey-milovidov merged 2 commits into
ClickHouse:masterfrom
groeneai:groeneai/parallel-replicas-segment-size-active-count

Conversation

@groeneai

@groeneai groeneai commented Nov 20, 2026 •

Copy link
Copy Markdown
Collaborator

Related: #107805
Related: #109377

Changelog category (leave one):

  • Improvement

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

The automatic choice of parallel_replicas_mark_segment_size now counts only the replicas that take part in the query: the active replicas of cluster_for_parallel_replicas, at most max_parallel_replicas, the same count the reading coordinator uses. Before, stale replicas of a Replicated database and replicas beyond max_parallel_replicas were counted too, which could choose a smaller segment.

Description

Replaces #109377 with the minimal version agreed there.

Since #107805 the parallel replicas reading coordinator is sized by the active replicas of cluster_for_parallel_replicas, at most max_parallel_replicas. ReadFromMergeTree still passed the registered replica count as total_nodes_count, and chooseSegmentSize divides sum_marks by its square to pick the automatic segment size. So stale registrations of a Replicated database, or a cluster larger than max_parallel_replicas, counted replicas that take no part in the query.

The liveness part of prepareConnectionPoolsForParallelReplicas (liveness lookup, size check, local replica forced active) moves into getParallelReplicasLiveness, and the new getReplicasCountForParallelReplicas counts with it by the coordinator's rules. ReadFromMergeTree computes it once per step (the liveness can be a Keeper read) and uses it for every ParallelReadingExtension. A follower counts from its own liveness; the coordinator keeps the segment size of the announcement that initializes it, so a different follower value changes nothing.

Example, 3 replicas, max_parallel_replicas = 2, a 6250-mark read with one thread and small read tasks: master divides by 3 and picks segment 128, this PR divides by 2 and picks 1024; the coordinator has 2 replicas in both. With the AutoPR CI config (11 replicas, max_parallel_replicas = 3) the count goes from 11 to 3.

Validation: the existing test_parallel_replicas_skip_inactive_replicas now checks number_of_replicas (added to the chooseSegmentSize trace line) with one of 3 replicas stopped and with max_parallel_replicas = 2. It fails on master, each case fails when its half of the rule (liveness, cap) is removed, and it passed 5/5 with the fix. 8 parallel replicas stateless tests x10 with random settings: 80/80.


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

Version info

  • Merged into: 26.12.1.102-master (included in 26.12 and later)

groeneai and others added 2 commits November 20, 2026 15:02
The reading coordinator is sized by the active replicas of
`cluster_for_parallel_replicas`, at most `max_parallel_replicas` (ClickHouse#107805),
but `ReadFromMergeTree` still passed the registered replica count of the
cluster as `total_nodes_count`, which `chooseSegmentSize` uses to pick the
automatic `mark_segment_size`. With stale registrations of a `Replicated`
database, or a cluster larger than `max_parallel_replicas`, the heuristic
divided by replicas that take no part in the query.

Extract the liveness half of `prepareConnectionPoolsForParallelReplicas`
into `getParallelReplicasLiveness` and add
`getReplicasCountForParallelReplicas`, which counts replicas by the
coordinator's rules. `ReadFromMergeTree` computes it once per step (the
liveness may be a Keeper read, and a read in order builds an extension per
split) and uses it for every `ParallelReadingExtension`. The
`chooseSegmentSize` trace line now logs the count, and the existing
integration test checks it with one of three replicas stopped and with
`max_parallel_replicas = 2`.

Related: ClickHouse#107805

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Shorten the liveness helper and cache docs to one line; drop a fallback comment the header already states.

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; 3 findings), plus my own review (3 nits, not listed).

# Sev Finding Verdict Evidence / action
1 ⚠️ total_query_nodes (ReadFromMergeTree.cpp, read and the in-order read) still counts registered replicas DISAGREE Out of scope as agreed in #109377: this PR changes total_nodes_count only. total_query_nodes is already capped by max_parallel_replicas and is reworked by #129356.
2 ⚠️ The count ignores the _shard_num shard scope of a multi-shard cluster DISAGREE Out of scope as agreed in #109377. Master already reads shard 0 of the unscoped cluster; there the liveness size does not match, so the count falls back to shard 0's node count, now capped. Only the segment size heuristic and a tracing span attribute read it, and the coordinator keeps the segment size of its first announcement.
3 ⚠️ The test asserts number_of_replicas, not the chosen segment size DISAGREE chooseSegmentSize logs the parameter it computes with, and this PR does not change that computation. The test fails when the count is the registered one or uncapped (number_of_replicas=3). Measured separately: 6250 marks, one thread, max_parallel_replicas = 2 picks 1024 instead of 128.

Severity: ❌ blocker / ⚠️ major / 💡 nit. DISAGREE verdicts carry recorded evidence and are terminal per finding.

Session id: cron:clickhouse-review-slot-8:20261120-152900

@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. test_parallel_replicas_skip_inactive_replicas fails on master every run; on a plain 3-replica cluster with max_parallel_replicas = 2 the chooseSegmentSize trace on master shows sum_marks/number_of_replicas^2=173 for 1563 marks (divides by 3) while the coordinator has 2 replicas.
b Root cause explained? The coordinator counts active replicas capped by max_parallel_replicas (#107805), but every ParallelReadingExtension in ReadFromMergeTree took the registered replica count, which chooseSegmentSize divides sum_marks by (squared).
c Fix matches root cause? Yes. The read side now counts with the coordinator's own liveness rules, through a helper extracted from prepareConnectionPoolsForParallelReplicas, not a copy.
d Test intent preserved / new tests added? The existing integration test is extended (inactive replica case, max_parallel_replicas cap case), assertions scoped by query_id; its existing coordinator assertions are unchanged.
e Both directions demonstrated? Master: fails. Fix: 5/5 pass. Counting registered replicas: fails at the inactive case (number_of_replicas=3). Dropping the cap: fails at the cap case (number_of_replicas=3).
f Fix is general across code paths? All four ParallelReadingExtension sites use the new count. The coordinator path is a pure extraction; both test failpoints keep their semantics.
g Fix generalizes across inputs (params/datatypes/wrappers)? Liveness known with an inactive replica (2), all active with a cap (2), liveness unknown (2/3/3 for max_parallel_replicas 2/3/10); no active or mismatched liveness falls back to the registered count, never 0. A user-set parallel_replicas_mark_segment_size is kept. Type wrappers: N/A.
h Backward compatible? (maintainer-approved exception only) Yes. No setting, protocol or on-disk change; only the input of the segment size heuristic and one trace field.
i Invariants and contracts preserved? number_of_replicas >= 1 (no division by zero). DefaultCoordinator keeps the segment size of the announcement that initializes it, so a follower computing another count changes no result.

Session id: cron:clickhouse-impl-slot-41:20261120-145000

@clickhouse-gh

clickhouse-gh Bot commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [dc2a328]

Summary: ✅


AI Review

Summary

Reviewed PR #133260 at dc2a32890f16c0f573d1f3c7be3f134c47a52268, including the full diff, current code, and all prior discussion. The change sizes automatic parallel-replica mark segments using the active replica count capped by max_parallel_replicas, sharing the coordinator's liveness rules and caching the count per read step. No actionable findings.

Missing context / blind spots
  • ⚠️ On 2026-11-20, CI for the reviewed head is still running: Fast test passed, while the targeted integration jobs are pending. The PR includes regression assertions for inactive replicas and the replica cap, and reports successful local validation. I did not rerun these tests because this checkout has no build directory or installed binary. Completed integration jobs would close this validation gap.
Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added pr-improvement Pull request with some product improvements can be tested Allows running workflows for external contributors comp-parallel-replicas Parallel reading from replicas for query speed (not data replication consistency). labels Nov 20, 2026
@clickhouse-gh

clickhouse-gh Bot commented Nov 20, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing dc2a32890 with master 8930b7fa5 (stripped binary size, per-symbol sizes and ThinLTO time; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Compile time of recompiled translation units

164 translation units recompiled, 1477 s compile time in total, 164 of them have a recent master baseline.

Median compile-time ratio to the baselines is ×0.94 (machine-speed difference or a change affecting every TU); per-TU deltas below are relative to that ratio.
The matched translation units cost -96.4 s (-6%) in total before that adjustment.

Job report

@alexey-milovidov alexey-milovidov self-assigned this Nov 20, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Nov 20, 2026
Merged via the queue into ClickHouse:master with commit 361db3a Nov 20, 2026
389 of 390 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 Nov 20, 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-improvement Pull request with some product improvements 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.

3 participants