Repository navigation
Conversation
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]>
|
Workflow [PR], commit [9600902] Summary: ✅
AI ReviewSummaryReviewed PR #129356 at Findings
Tests
Missing context / blind spots
Final Verdict |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units137 translation units recompiled, 1632 s compile time in total, 137 of them have a recent master baseline. |
…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]>
…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]>
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; |
There was a problem hiding this comment.
[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.
…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]>
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^2at onceA replica asks the coordinator for one task per thread at once (
threads * min_marks_per_taskmarks). 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 withparallel_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(andmin_marks_for_concurrent_read, which the pool takes the maximum with) is now at mostmax(merge_tree_min_read_task_size, max(sum_marks / replicas^2, 128) / threads), so one batch is at mostsum_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_protocolneeded 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:
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.cpphas 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):
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]