Repository navigation
Fix StorageKafka2 per-consumer partition lock quota for kafka_num_consumers>1 - #116843
Conversation
|
Workflow [PR], commit [c49fc43] Summary: ✅
AI ReviewSummaryThis PR changes Missing context / blind spots
Findings
Tests
Final VerdictLLVM Coverage ReportMeasured on commit c49fc43.
Changed lines: Changed C/C++ lines covered: 32/32 (100.00%) · Uncovered code |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units12 translation units recompiled, 44 s compile time in total, 12 of them have a recent master baseline. |
c33696c to
883d30c
Compare
- Make num_consumers const (set at construction, never changes) - Tighten test bounds to match exact formula expectations (6/6, 2/2, 3/4, 2/2) - Use parameterized test for all quota cases
Keeper lock znodes include both permanent and temporary locks, making exact bound assertions unreliable. Parse the server trace log instead, which prints the computed permanent-lock quota for each consumer with all formula inputs (node_quota, active_replicas, num_consumers, idx).
4688ed5 to
073637e
Compare
The log-based assertions check what the quota code *computed*; they never check what it actually holds. Add wait_for_consumer_assignments(), which reads per-consumer partition counts from system.kafka_consumers, so a regression where the quota is computed correctly but not enforced is caught. - acceptable_consumer_counts added to the matrix: [2,2,2], [0,1,1], [1,1,1]/[1,1,2], [2]. The [0,1,1] row is the valuable one -- it proves the third consumer correctly idles and no single consumer hoards the replica's share, which nothing previously verified. - Two multisets are accepted where P mod R != 0, because getStat() merges permanent and temporary locks, so a consumer may transiently hold the floating partition. - Restore test_multi_consumer_with_partition_affinity, dropped in the Keeper->log rewrite. Affinity shrinks P without shrinking N, which is what drives a cluster into the P < R*N regime, so it is the case most worth covering. kafka_partition_shard_num is a KafkaSettingsString: passing an int makes create_settings_string emit it unquoted and CREATE TABLE fails with "Code: 170. Bad get: has UInt64, requested String" -- hence str(). - 240s timeout on the assignment poll. Unlike the log assertions this waits for the distribution to CONVERGE: a replica registering first sees active_replicas=1, takes the whole node_quota, and sheds the surplus only on a later refresh round. That exceeded 90s under asan/ubsan -- see the 9/3-instead-of-6/6 failure in the flaky check on ef41915. - Fix F541 (f-string without placeholders) reported by the style check. Verified locally against a branch-built binary: 5 passed in 693.85s.
test_multi_consumer_with_partition_affinity failed 2 of 3 runs in the flaky check on 5a0a15a: Per-consumer assignments for kafka_aff_s1_r1 never matched any of [[0, 0, 1, 1]]; last seen: [0,0,1,2] [0,0,1,2] sums to 3 against a node_quota of 2. The extra partition is a temporary lock: getStat() merges permanent_locks and tmp_locks into one assignments list, and while the replica that registered first sheds its surplus (it saw active_replicas=1 and took node_quota=4), the partitions it releases are briefly picked up as temporary locks by its shard peer. The exact multiset was the wrong assertion for this test. The parametrized cases already prove enforcement -- they now pass 12/12 -- and the log assertions above already prove the whole affinity contract: node_quota=2 (P_shard 4 / R_shard 2), active_replicas=2 (only same-shard replicas counted), and the per-consumer split summing to node_quota. What is left to check is affinity's own contract, which no other test covers: a replica must only ever hold partitions of its own shard. That holds from the moment a partition is locked, because the affinity filter runs before locking, so it does not depend on catching a converged instant. Verified locally against a branch-built binary: 1 passed in 124.81s, down from ~4 minutes since it no longer polls for a converged count.
test_permanent_lock_quota[even] failed on arm_binary (distributed plan, 3/4):
Per-consumer assignments for kafka_quota_12p_2r_3c_r2 never matched any of
[[2, 2, 2]]; last seen: [1,2,3]
[1,2,3] sums to 6, so the replica held exactly its node_quota -- only the split
across its three consumers had not settled. One consumer held 2 permanent locks
plus a temporary one; its starved sibling could not advance because all 12
partitions were locked cluster-wide, so it had to wait for the temporary lock to
be released on a later refresh round. That is a per-round race which does not
reliably resolve inside the timeout.
The exact multiset asserted a stronger property than the code guarantees. The
node-level contract this PR fixes was satisfied throughout. Replace it with the
bounds that hold immediately rather than only at a quiet instant:
node_quota <= sum(counts) <= node_quota + P % R node holds its own share
max(counts) <= ceil(node_quota / N) + 1 no consumer exceeds its share
plus one temporary lock
count(c > 0) >= min(N, node_quota) the share is spread, which is
what rules out "one consumer
keeps all of the replica's
assignments"
The third bound matters: `max` alone cannot reject a hoarding [0,0,2] when
node_quota is 2 and the ceiling is 1, because that is indistinguishable from a
legitimate temporary lock. Requiring the share to be spread rejects it.
Verified offline that the bounds accept the ideal split and the observed [1,2,3],
and reject a hoarding [0,0,node_quota] on every multi-consumer row. Per-consumer
quotas are still asserted exactly from the trace log, so computation remains
covered; this check now covers enforcement only.
Verified locally against a branch-built binary: 5 passed in 517.94s.
|
@alexey-milovidov @antaljanosbenjamin Can you help review this PR please ? |
Co-authored-by: János Benjamin Antal <[email protected]>
Co-authored-by: János Benjamin Antal <[email protected]>
Co-authored-by: János Benjamin Antal <[email protected]>
7472b51 to
a8f4869
Compare
a8f4869 to
c49fc43
Compare
|
|
||
| # End-to-end: the quota was enforced, not just computed. Every replica gets work | ||
| # and the topic is fully claimed -- on master one replica takes everything. | ||
| held = wait_for_partitions_claimed( |
There was a problem hiding this comment.
The two-phase setup removes the exact rebalance path the PR description says is broken. Because every replica is registered before any consumer polls, this test never drives updatePermanentLocksLocked() from “one replica already owns too many permanent locks” to can_lock_partitions < permanent_locks.size() after a late peer appears. The wait_for_partitions_claimed check then only proves that every replica eventually gets some work, not that an early replica actually sheds its surplus down to node_quota.
Please keep one focused staggered-start regression here: start one replica streaming first, wait until it hoards the topic, then attach the peer and verify that the first replica releases locks once active_replica_count grows.
The test failed at ~0.06% in CI with "never acquired any partition within 240s" (ClickHouse#119632). Every genuine failure was this test alone in an otherwise healthy job, on sanitizer builds. The assertion was a liveness property, not an invariant. A replica can legitimately hold nothing for a long time: the fairness guard in lockTemporaryLocksLocked only restrains a consumer that already holds a temporary lock (tmp_locks_quota > 0 && has_replica_without_locks), so a sibling consumer on the same replica holding none skips the guard and re-takes the partition its peer just released. With 8 partitions, shard count 2 and 4 consumers, one replica could hold its whole shard (2 permanent + 2 temporary locks against a shard of 4) and keep it, leaving the peer empty for the length of the test. Drop both lower bounds -- the "holds no partitions" assert and the pytest.fail in wait_for_shard_partitions -- and assert an upper bound instead: a replica holds at most node_quota permanent locks plus one temporary lock per consumer. For that ceiling to be meaningful it has to sit below the shard size, so the topic grows to 16 partitions with 2 consumers: node_quota becomes 4 and the ceiling 4 + 2 = 6 against a shard of 8. Before ClickHouse#116843 a single replica takes all 8 and trips it. At the old sizing the ceiling would have been 6 against a shard of 4, which no distribution can exceed. What remains asserted -- shard membership, the ceiling, and the quota values read from the log -- holds at any instant, so a slow build cannot fail the test.
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes to CHANGELOG.md):
Fixed uneven partition distribution in
StorageKafka2whenkafka_num_consumersis greater than 1. A node running N consumers could claim N times its fair share of the topic's partitions and hold them indefinitely, leaving other replicas idle.Description
We ran a 256-partition topic across 16 nodes with
kafka_num_consumers = 16. Two or three nodes were doing all the ingestion and the rest were idle. Partition counts per node looked like 106 / 50 / 100 / 0 / 0 / …. Restarting the busy nodes just moved the problem to whichever node came back first.active_replica_countcounts nodes, but permanent_locks and early return below it are per consumer. Each consumer on a node can fills up to node's entire quota and the node lands on N * (P / R).StorageKafka2's Keeper-based partition lock quota is computed per consumer but divided only by the replica count.
Nothing corrects it afterwards. Permanent locks are released on consumer shutdown not on topology change, so first node up wins and keeps winning. The losers fall through to he temporary-lock path, which hands out at most one partition per round , enough took alive in the logs, nowhere near enough to catch up. That's why this reads as a
throughput problem rather than an obvious failure, and why it survived in production for as long as it did.
This is evaluated per consumer, so each consumer sees itself as satisfied and never releases the surplus. Once a node wins the initial race it keeps those partitions indefinitely.
Example from production
256-partition topic, 16 nodes, kafka_num_consumers = 16:
Observed impact
Proposal
Compute the node's quota first, then split it across the node's consumers, distributing the remainder by consumer index:
Two properties:
idx is already a member, so each consumer derives its slice with no cross-consumer coordination and the split is stable across rounds.
Behaviour when N = 1
Unchanged : node_quota / 1 == node_quota, node_quota % 1 == 0, and idx == 0, so can_lock_partitions == max(P/R, 1), identical to the previous code. No existing single-consumer test is affected.
Tests
The affinity case is included because affinity divides P by shard_count while leaving N unchanged, which can push a cluster into the P < R × N .
Workflow [PR]
Sync PR [sync-upstream/pr/116843]
Version info
26.9.1.892(included in26.9and later)