Skip to content

Spread small reads evenly across parallel replicas - #129356

Draft
nickitat wants to merge 11 commits into
masterfrom
mark-segment-size
Draft

nickitat wants to merge 11 commits into
masterfrom
mark-segment-size

Conversation

@nickitat

@nickitat nickitat commented Nov 4, 2026 •

Copy link
Copy Markdown
Member

Two changes to how parallel replicas spread a read, both found while investigating why automatic parallel replicas were slower than manual parallel replicas on a customer workload.

1. The stealer is one of the other replicas

An idle replica may take a segment from a busy replica's queue only if a second consistent hash names it as the segment's stealer. That hash was computed over all replicas, so for 1/N of the segments (half of them with 2 replicas) the stealer was the owner itself. While the owner is alive and announced, nobody else may take such segments (Don't steal segments with alive owner that sees them), so a slow owner had to read all of them.

The stealer is now chosen by hash among the other N-1 replicas (with 2 replicas, always the other one). Owners do not change, and only the coordinator on the initiator computes the hash, so nothing changes on the wire.

2. A replica takes at most sum_marks / replicas^2 at once

A replica asks the coordinator for one task per thread at once (threads * min_marks_per_task marks). With the defaults (merge_tree_min_rows_for_concurrent_read, merge_tree_min_bytes_for_concurrent_read, or the remote-filesystem settings set to the same values) and 16 threads one batch can exceed the whole read: e.g. 16 * 48 = 765 marks against a 753-mark read. The replica that starts first - the initiator with parallel_replicas_local_plan = 1, ~15 ms ahead - then takes everything before the others ask. Change 1 makes this worse on its own: the first requester can now steal all of the other replica's segments, even without a local plan.

min_marks_per_task (and min_marks_for_concurrent_read, which the pool takes the maximum with) is now at most max(merge_tree_min_read_task_size, max(sum_marks / replicas^2, 128) / threads), so one batch is at most sum_marks / replicas^2, which also leaves something to steal for every other replica.

128 marks is the smallest segment the coordinator distributes. A smaller read is not worth balancing, and capping it would only add coordinator requests: without the floor, 03581_parallel_replicas_task_size (41 marks on S3, 3 replicas) made 20 requests with 1-mark tasks instead of 14. With the floor such reads behave as before.

This applies only to the default (not in-order) parallel replicas read with more than one replica. The automatic segment size follows: it is chosen from max(threads * min_marks_per_task, sum_marks / replicas^2), so now mostly from the second term (test_parallel_replicas_protocol needed more granules to get a segment above 128).

Results

Local reproduction, 2 replicas with 7 CPUs each, reads of ~753 marks almost all in one big part, 36 reads per row (automatic and 128 segment size), quiet machine:

median time smaller replica's median share one replica read nothing
master, no local plan 711-752 ms 19% 7 of 36
master, local plan 794-796 ms 0% 30 of 36
both changes, either mode 572-579 ms 48% 0 of 36

Change 1 alone: 0% share, one replica read nothing in 36 of 36.

Bigger reads (5-9k marks) with one replica pinned to 2 CPUs and the other to 12, change 1 only: on master the slow replica read 41-45% of the marks and got more than 2 requests in 5-15 of 18 queries; with the change it read 36-37% and never more than 2 requests. Not yet re-measured with change 2.

The new gtest gtest_parallel_replicas_coordinator_stealing.cpp has two announced, available replicas where one takes a task smaller than a segment and never asks again; the other must read every remaining mark exactly once. Without change 1 both variants (stalled owner, stalled follower) leave marks unread.

Changelog category (leave one):

  • Performance Improvement

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

Parallel replicas spread small reads more evenly: an idle replica can take over any part of another replica's remaining work, and a replica no longer takes more marks at once than sum_marks / replicas^2 (but at least 128), so the replica that starts first can't take the whole read.

🤖 Generated with Claude Code


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

nickitat and others added 2 commits November 4, 2026 17:09
The replica allowed to steal a segment was chosen by a consistent hash over
all replicas, so for 1/N of the segments (half of them with 2 replicas) it was
the owner itself. Such segments could not be stolen while the owner was alive,
so a slow owner had to read them all. Choose the stealer among the other N-1
replicas.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Two announced, available replicas: one takes its first task, which covers only
part of a segment, and never asks again; the other must read every remaining
mark exactly once. Without the previous commit the segments whose stealer was
their own owner stay unread. Also covers the single-replica case.

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

clickhouse-gh Bot commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [9600902]

Summary: ✅


AI Review

Summary

Reviewed PR #129356 at 9600902f391223e82e4b1bf458aafb15ae3b5bb9. The PR chooses a segment's stealer from replicas other than its owner and limits coordinator batches to improve distribution of small reads. The reported two-replica measurements support that improvement, but one existing performance finding remains unresolved: the quadratic batch cap counts inactive replicas. Request changes for that case. The author's documented decision to limit one batch, rather than buffered plus active work, is accepted.

Findings

⚠️ Majors

  • The batch cap must use the participating replica count. ReadFromMergeTree.cpp:742: pool_settings.total_query_nodes uses the configured cluster size, bounded by max_parallel_replicas. However, prepareConnectionPoolsForParallelReplicas removes inactive registrations and sizes the coordinator for the smaller active count without carrying that count into this calculation. After scale-down, batches therefore shrink for replicas that will never participate. With 32 registered replicas, two active replicas, max_parallel_replicas = 32, 65,536 selected marks, 16 threads, and an existing task size of 64 marks, the new cap reduces requests from 1,024 to 128 marks, adding roughly eight times as many assignment requests without improving balance. Using the two participating replicas would preserve the existing task size. The cap also applies when only one replica participates. Carry the coordinator's selected replica count into the read-pool calculation and its single-replica guard. This remains covered by the existing inline finding; no duplicate comment was posted.
Tests
  • ⚠️ Add focused coverage with inactive replica registrations and two participating replicas, verifying that request sizes depend on the participating count. Include the single-participant boundary to prove that the cap is skipped there. The added coordinator tests use fixed request sizes, and the modified protocol test uses a fully active cluster; neither exercises this divergence.
Missing context / blind spots
  • ⚠️ No local build or live cluster was available, so the inactive-replica example was traced through the current code rather than reproduced end to end. Running the focused regression above would close this verification gap.
Final Verdict

⚠️ Request changes. Use the coordinator's selected replica count for batch sizing and add regression coverage for inactive registrations, including a single participating replica.

@clickhouse-gh clickhouse-gh Bot added the pr-performance Pull request with some performance improvements label Nov 4, 2026
@nickitat
nickitat marked this pull request as ready for review November 4, 2026 18:56
@clickhouse-gh clickhouse-gh Bot added the comp-parallel-replicas Parallel reading from replicas for query speed (not data replication consistency). label Nov 4, 2026
@clickhouse-gh

clickhouse-gh Bot commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 9600902f3 with master 360ab5255 (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

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

Job report

…plicas^2)

A replica asks the coordinator for more marks as soon as its buffer is empty,
so it holds up to one task per thread at once. When that exceeds a replica's
share (e.g. 16 threads x 48 marks against a 753-mark read), the replica that
starts first - the initiator with a local plan - takes the whole read before
the others ask, and the stealer change makes this happen even without a local
plan. Bound min_marks_per_task (and min_marks_for_concurrent_read, which the
pool takes the maximum with) so that all threads of a replica hold at most
sum_marks / replicas^2, but not below merge_tree_min_read_task_size.

Local repro, 2 replicas, ~753-mark reads in one big part, 18 reads each with
automatic and 128 segment size: the smaller replica's median share went from
0% (master with local plan, and the stealer change in either mode) to 48%.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@nickitat nickitat changed the title Pick the parallel replicas stealer among replicas other than the segment owner Spread small reads evenly across parallel replicas Nov 5, 2026
@nickitat
nickitat marked this pull request as draft November 5, 2026 14:56
nickitat and others added 5 commits November 5, 2026 15:15
…cas task cap

A replica asks the coordinator for `threads` tasks as soon as its buffer is empty, while
`threads - 1` workers still read their tasks. With 1024 marks, 16 threads and 2 replicas the
cap gave 16-mark tasks and 256-mark requests, so a replica could hold 496 marks instead of the
intended `sum_marks / replicas^2` = 256. Halve the per-task bound so that both batches together
fit into it.

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

The task size a replica cuts from its assignments is the one the initiator computed for its own
number of threads, and the coordinator uses the initiator's batch size for every replica. A
follower with more threads than the initiator then holds far more than one batch: with a
2-thread initiator, 47-mark tasks and 94-mark batches, a 16-thread follower could hold
15 * 47 + 94 = 799 unread marks of a 753-mark read. Bound the task size by
`ceil(batch / threads)` (but not below `merge_tree_min_read_task_size`), so that the threads of
any replica together hold at most one batch, as the cap on the initiator assumes.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The cap on what a replica holds (`sum_marks / replicas^2`) needs about `2 * replicas^2` coordinator
requests whatever the size of the read. For tiny reads this made tasks of a single mark: in
`03581_parallel_replicas_task_size` (about 41 marks on S3, 3 threads, 3 replicas) the requests
went from 6 to 3 marks and the query made more than the 15 requests the test allows. A read
that does not fill the smallest coordinator segment (128 marks) is not worth balancing, so let a
replica always hold that much; the remote reading heuristic then decides as before.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
/// a read too small for that is not worth balancing, and smaller tasks would only add requests to the coordinator.
if (pool_settings.total_query_nodes > 1)
{
const size_t replicas = pool_settings.total_query_nodes;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] Use the participating replica count for the quadratic cap

pool_settings.total_query_nodes is the minimum of the configured node count and max_parallel_replicas (see read below), but prepareConnectionPoolsForParallelReplicas excludes inactive replicas and creates the coordinator for the smaller active count without updating that setting. A Replicated database or Cloud cluster with inactive registrations after scale-down therefore gets a cap based on replicas that will never read anything.

For example, with 32 registered replicas, two active replicas, max_parallel_replicas = 32, 65,536 selected marks, 16 threads, and an existing task size of 64 marks, this calculates an 8-mark task cap and 128-mark requests instead of the previous 1,024-mark requests. Using the two participating replicas would calculate a 512-mark cap and preserve the existing task size. This adds roughly eight times as many coordinator round trips without a balancing benefit; the cap can even apply when the coordinator has only one participating replica.

Please carry the coordinator's selected replica count into this calculation, including its single-replica guard, and cover the inactive-replica case.

nickitat and others added 3 commits November 5, 2026 20:36
…local thread"

This reverts commit 9e10b1d. Replicas of one query are expected to run with the same number
of threads, so the initiator's task size fits the followers too.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Counting the buffered batch together with the one being read halved the task size without a
measurable effect: on 753-mark reads with 2 replicas the split and the timings were the same.
Keep the bound as the size of one batch.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The automatic segment size is the larger of `threads * min_marks_per_task` and
`sum_marks / replicas^2`. With the task size now bounded by the latter, 1000 tiny granules gave
the smallest segment (128), which the test avoids. Use 10000 granules: `10000 / 3^2` gives 1024.

https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=129356&sha=latest&name_0=PR
#129356

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

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ci-force-all Do not skip any jobs when running comp-parallel-replicas Parallel reading from replicas for query speed (not data replication consistency). pr-performance Pull request with some performance improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant