Repository navigation
[improve][broker] Keep the Key_Shared consistent hashing ring in arrays - #26863
Merged
Merged
Conversation
ConsistentHashingStickyKeyConsumerSelector kept its hash ring three times: a TreeMap with an entry, a boxed Integer and a HashRingPointEntry per ring point, the published lookup arrays, and with draining hashes tracking a ConsumerHashAssignmentsSnapshot with two objects per ring point. That cost about 25 KB of heap per Key_Shared consumer with the default 100 ring points, and every membership change rebuilt and diffed the full snapshot. - Keep the ring only in the published arrays, rebuilt in one pass on a membership change. - Resolve the hash ranges that moved between consumers by walking the previous and the new ring's points together, instead of building and diffing snapshots. - Keep each consumer's sorted points while it's in the ring, and colliding consumers in a map by point, created at the first collision. - Build the stats' hash ranges and the assignments snapshot from the ring on request. The heap cost becomes about 1.2 KB per consumer with 100 ring points. Fixes #26862 Assisted-by: Claude Code (claude-opus-5-5)
This was referenced Oct 12, 2026
dao-jun
approved these changes
Oct 12, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #26862
Motivation
ConsistentHashingStickyKeyConsumerSelector, the default selector of Key_Shared subscriptions, kept three copies of its hash ring:TreeMap<Integer, HashRingPointEntry>: aTreeMap.Entry, a boxedIntegerand aHashRingPointEntryper ring point;AUTO_SPLITmode without out-of-order delivery uses, aConsumerHashAssignmentsSnapshot: aRangeand aHashRangeAssignmentper ring point, kept to diff with the next snapshot on every membership change.With the default 100 ring points per consumer (
subscriptionKeySharedConsistentHashingReplicaPoints), that cost about 25 KB of heap per consumer. In #26862, consumers whose connections disappeared without the broker noticing reconnected while the broker kept their old consumers. The broker held 11,971 consumers for a workload of 6,000, and its 512 MB heap ran out: about 297 MB of the 571 MB heap dump was the Key_Shared dispatchers and selectors.Every membership change also rebuilt the full snapshot and diffed it with the previous one. When all consumers of many subscriptions reconnect at once, this work runs on the I/O threads that also serve the producers.
Modifications
getConsumerKeyHashRanges(), which the subscription stats use, straight from the ring in one pass, andgetConsumerHashAssignmentsSnapshot()from the ring on request, instead of keeping a snapshot.ReentrantLock; the lookups and the stats read the published ring without locking.The heap cost becomes 12 bytes per ring point, the ring's two arrays and the consumer's points, about 1.2 KB per consumer with 100 points plus the arrays' headers and a map entry per consumer (computed, not measured).
Heap saving
The selectors' heap drops from about 25 KB to about 1.2 KB per Key_Shared consumer, about 20 times less: for 10,000 Key_Shared consumers, from about 250 MB to about 12 MB. The broker's heap then has room for situations where the consumers' count or their pending messages grow:
network/silent-client-disconnect, a 512 MB broker ran out of heap in 2 runs out of 2 on master, with a heap dump of 593 MB. With this change, it didn't in 3 runs out of 3, and the heap dump at the highest heap usage was 306 MB.network/constrained-consumerslimits the broker's traffic to the consumers to 5 Mbit/s, about half of their traffic, for 60 s. On master, the 512 MB broker ran out of heap 14 s after the limit ended, in 2 runs out of 2: the messages dispatched to the slow consumers and not yet acknowledged, 105,000–243,000 of them, on top of the Key_Shared selectors' state. The restart then stalled every producer for 20–40 s. With this change, the broker didn't run out of heap in 2 runs out of 2. With a 4 GB broker, master didn't either.Verifying this change
This change added tests and can be verified as follows:
ConsistentHashingStickyKeyConsumerSelectorTest.testImpactedConsumersMatchAssignmentsDiffmakes 4,000 random membership changes, with 1–30 ring points on a hash range of 63, so that points collide often, also among one consumer's own points. After each change, it compares the impacted consumers that the selector returns with the diff of the assignment snapshots before and after, the computation that the selector used before, comparesgetConsumerKeyHashRanges()with the snapshot's ranges, and checks every hash's lookup against the assignments.Integer.MAX_VALUE.ConsistentHashingStickyKeyConsumerSelectorTest,ConsumerHashAssignmentsSnapshotTest,PersistentStickyKeyDispatcherMultipleConsumersTest,NonPersistentStickyKeyDispatcherMultipleConsumersTest,KeySharedSubscriptionTest,DrainingHashesTrackerTestand the other selector tests.KeySelectorLookupBenchmark.reconnectAllConsumersis a new benchmark: every consumer of a subscription reconnects, so the selector removes all of them and adds them back. Results on Linux x86_64 (i9-9980HK, with the performance environment of the Pulsar Performance Testing Framework: fixed CPU frequency, no turbo), 2 forks × 5 iterations of 2 s, with the time and the allocation per operation:reconnectAllConsumersreconnectAllConsumersreconnectAllConsumerschangeMembershipchangeMembershipselectThe network-condition scenarios of the Pulsar Performance Testing Framework in #26859, on
iot-telemetry-small: 6,000 Key_Shared consumers in 600 subscriptions on one broker, with the performance environment on, master and this change on the same base. The image with this change also had two fixes for a broker that loses its metadata session, one of them #26861, which act only when the session is lost, which doesn't happen in these scenarios:network/silent-client-disconnect, broker heap 512 MBOutOfMemoryError15 s after the consumers' silent disconnection, in 2 runs out of 2; heap dump 593 MBOutOfMemoryErrorin 3 runs; the heap dump at the highest heap usage was 306 MBnetwork/consumer-reconnect-storm, a reset during the warmup and one 30 s into the measurement, publish latency maxnetwork/constrained-consumers, broker heap 512 MBOutOfMemoryErrorin 2 runs out of 2OutOfMemoryErrorin 2 runsAbout the reconnect storm: a broker profile of the reset with this change showed that the selector's share of the stall is small. The broker used 9–10 cores for about 3 s, half of them the JIT recompiling the dispatch paths, while the host's 16 hardware threads were 14–15 busy with the broker and the 200 reconnecting clients on the same host. The
pulsar-iothreads also blocked on the log appender's lock for the 18,000 INFO lines of the subscriptions, but the same runs with the broker's log level at WARN stalled as long (2.03 s and 1.39 s), so the logging isn't what limits it. This change mitigates the storm without fixing it.About the ordering violations: with a 512 MB heap, one of the three runs with this change had 3 ordering violations, and with 768 MB, master had them in both runs. They come from an existing problem of Key_Shared dispatching while the old consumers stay registered after their connections vanished, which this change makes rarer by leaving more heap, not one this change introduces: a read in flight when the keep-alive removes an old consumer can dispatch a newer entry of a moved hash before the removed consumer's older pending entry. It will be reported and fixed separately.
Does this pull request potentially affect one of the following parts:
If the box was checked, please highlight the changes