You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by "lhotari (via GitHub)" <gi...@apache.org> on 2026/10/16 21:31:40 UTC
[PR] [improve][broker] Keep each ledger's pending acks in sorted blocks instead of an AVL tree [pulsar]
lhotari opened a new pull request, #26955:
URL: https://github.com/apache/pulsar/pull/26955
### Motivation
`PendingAcksMap` keeps each consumer's pending acks per ledger in a `Long2LongAVLTreeMap`. Every entry dispatched to a consumer inserts a node, and every ack looks the entry up and removes it. With many pending acks per consumer, as when consumers fall behind, the tree's pointer chasing and rebalancing cost much of the broker's CPU. In a broker profile of a Shared subscription fan-out at the maximum rate (one topic, 50 Shared subscriptions with 4 consumers each), the tree's methods were the largest cluster of the broker's self time, 8.9 % of its CPU samples.
An individual ack also looks the acked entry up twice: once when the ack arrives, for the entry's owner and batch size, and once to remove it after the ack is persisted.
### Modifications
- `PendingAcksLedgerMap` replaces the per-ledger AVL tree. A ledger's entries are in a list of blocks of up to 128 entries, each a sorted array window.
- A consumer is sent a ledger's entries mostly in increasing order and acks them mostly in the same order. Adding an entry appends it to the last block, and removing one advances the first block's window.
- A lookup is a binary search over the blocks' first entry IDs, kept in one `long[]` in step with the blocks, and over one block's consecutive memory.
- `get` and `remove` check whether the entry is the ledger's first pending entry before searching. Removals happen in the order the acks are persisted, which is mostly the order of the entries, so the removed entry is usually the first.
- An entry added out of order, as redeliveries add them, shifts the entries of one block at most, on the shorter side, and a full block splits. Removals shift the shorter side of their block. The last block is compacted only when that frees a quarter of it at least; otherwise it grows or a new block starts.
- Memory follows the number of entries:
- Empty blocks are dropped, a block with fewer than 32 entries merges with a neighbor when the two fit in half a block, and the list of blocks releases its capacity once a quarter of it is in use.
- A lone block starts with 16 entries, doubles up to 128 as entries are added, and halves when its entries fall below a quarter of it. A map with a few pending acks, such as the ack holes a consumer leaves behind in older ledgers, keeps 0.3 to 1.2 KB per ledger (the AVL tree 0.2 to 0.9 KB).
- While a map has more than one block, the last emptied block is kept for the next one, so a window of pending acks that moves through a ledger doesn't allocate.
- `PendingAcksMap` reuses a ledger map that became empty, with its small block, so a consumer whose pending acks drop to none between messages, as on a topic with a low rate, doesn't allocate a map for each message.
- `PendingAcksMap` uses `PendingAcksLedgerMap` with the same semantics for its callers. Two cases that no production callback causes behave differently, and correctly now:
- A callback that changes the map during a traversal: `forEach` (used by `forEachAndClear` and `forEachAndClose`) and `removeAllUpTo` continue with the first entry after the current one as the map is after the callback (`higherEntryId`). They visit the entries added after the current one, not those added before it, and never an entry that was removed; the AVL tree's iterator could visit an entry that the callback removed. `forEach` keeps its index-based loop until a callback adds or removes an entry, which a modification count detects.
- A `removeAllUpTo` callback that removes the current entry: the size counts it once; with the AVL tree it was counted twice.
- `PendingAcksLedgerMapBenchmark` compares the AVL tree and `PendingAcksLedgerMap` in the patterns of dispatch and acks, and `PendingAcksMapOneEntryBenchmark` measures `PendingAcksMap` for a consumer whose pending acks drop to none between messages.
### Verifying this change
- `PendingAcksLedgerMapTest` (15), with an internal consistency check (block order, the first-entry-ID index, the size, the block capacities and the spare block) after every operation of the randomized tests:
- ordered contents with appends, out-of-order inserts, updates, removals and prefix removal;
- a randomized comparison with `TreeMap` over 50 rounds of 5,000 operations in the patterns of dispatch and acks;
- removals anywhere in a window of 1,000 entries, followed by appends and out-of-order inserts, which reuse spare blocks from merges, in 20 rounds of 4,000 operations;
- re-adding a removed half of 200,000 entries in ascending order;
- block splits in both halves, and an insert into the upper half of a full block;
- memory released as entries are removed, and a lone block that shrinks to 16 entries when 7 of 640 remain;
- a small first block that grows for appends and for entries inserted into it, and blocks after it of the full capacity;
- a moving window of 512 entries that removes and appends 12,800 entries allocates at most one block more, and an emptied map starts again with its small block without allocating;
- traversals whose consumer removes the current entry and adds or removes others before and after it (20 seeded rounds, compared with `TreeMap.higherKey`), or drains the map;
- `higherEntryId` across blocks.
- `PendingAcksMapTest` (34), including the size when a `removeAllUpTo` callback throws, adds an entry, or removes the current entry; `forEachAndClear` when its callback removes the current or a later entry; `removeAllUpTo` when its callback adds entries before and after the current one; and a ledger map reused across ledgers.
- `ConsumerTest` (19) passes.
- A differential fuzzer against `TreeMap` (6,000 seeds, 36 million operations, with the consistency check after every operation and inside traversal callbacks) found no difference.
### Measurements
JMH on an Intel Core i9-9980HK (Linux, turbo off with the performance test environment), 2 forks, 3 × 1 s warm-up, 5 × 1 s measurement, `-prof gc`.
`PendingAcksLedgerMapBenchmark`, the per-ledger structure alone, ns per entry (per traversal for `traverse`), with the allocation per operation:
| Pattern | Window | AVL tree | `PendingAcksLedgerMap` | Change |
|---|---|---|---|---|
| `ackInOrder`: lookup, removal, new entry | 1,024 | 163.4 ns, 40 B | 32.5 ns, 0 B | −80 % |
| | 65,536 | 348.2 ns, 40 B | 34.1 ns, 0 B | −90 % |
| `ackGroupInOrder`: 64 lookups, then removals, then new entries | 1,024 | 163.6 ns, 40 B | 50.5 ns, 0 B | −69 % |
| | 65,536 | 341.8 ns, 40 B | 57.5 ns, 0 B | −83 % |
| `ackShuffled`: a random entry of the window | 1,024 | 385.7 ns, 40 B | 246.0 ns, 5 B | −36 % |
| | 65,536 | 718.9 ns, 40 B | 415.9 ns, 6 B | −42 % |
| `ackInOrderWithRedeliveries`: 1 in 16 redelivered out of order | 1,024 | 208.2 ns, 43 B | 85.6 ns, 0 B | −59 % |
| | 65,536 | 346.7 ns, 43 B | 105.8 ns, 0 B | −69 % |
| `removeUpToMarkDelete`: 64 oldest entries | 1,024 | 136.6 ns, 41 B | 30.4 ns, 0 B | −78 % |
| | 65,536 | 281.4 ns, 41 B | 33.2 ns, 0 B | −88 % |
| `traverse`: the whole window | 1,024 | 5.37 µs | 0.51 µs | −90 % |
| | 65,536 | 458 µs | 51 µs | −89 % |
| `tinyMaps`: 8,192 maps of 8 entries | – | 117.5 ns, 40 B | 53.6 ns, 0 B | −54 % |
| `oneEntryAtATime`: a map that empties between entries | – | 37.8 ns, 168 B | 45.7 ns, 0 B | +21 % |
With `PendingAcksMap`'s lock and outer ledger map, before (the branch's merge base) and after:
| Benchmark | Before | After | Change |
|---|---|---|---|
| `PendingAckMapCycleBenchmark.rotateWindow`, window 1,024 | 252.7 ns, 40 B | 121.6 ns, 0 B | −52 % |
| same, window 65,536 | 391.5 ns, 40 B | 122.4 ns, 0 B | −69 % |
| `PendingAcksMapOneEntryBenchmark.oneEntryAtATime` | 167.1 ns, 208 B | 149.2 ns, 40 B | −11 % |
- The lock accounts for most of the remaining cost of `PendingAcksMap`'s operations: about 85 ns of the 122 ns cycle.
- `PendingAckMapCycleBenchmark`'s shuffled variant acks in order after the first window (its permutation repeats and each slot is refilled with the next entry ID), so it is left out above; `ackShuffled` acks random entries.
- A map that empties between entries costs 8 ns more as a structure (the AVL tree allocates a new map instead), and 18 ns less with `PendingAcksMap`, which reuses the emptied map and allocates only the outer map's node.
### Does this pull request potentially affect one of the following parts:
- [ ] Dependencies (add or upgrade a dependency)
- [ ] The public API
- [ ] The schema
- [ ] The default values of configurations
- [ ] The threading model
- [ ] The binary protocol
- [ ] The REST endpoints
- [ ] The admin CLI options
- [ ] The metrics
- [ ] Anything that affects deployment
This change was developed with the assistance of Claude Code.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [improve][broker] Keep each ledger's pending acks in sorted blocks instead of an AVL tree [pulsar]
Posted by "lhotari (via GitHub)" <gi...@apache.org>.
lhotari commented on code in PR #26955:
URL: https://github.com/apache/pulsar/pull/26955#discussion_r4300992356
##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/service/PendingAcksMap.java:
##########
@@ -484,15 +482,14 @@ private void internalRemoveAllUpTo(long markDeleteLedgerId, long markDeleteEntry
if (removedEntryCallback != null) {
removedEntryCallback.accept(ledgerId, entryId, batchSize, stickyKeyHash);
}
- entryMapIterator.remove();
- size--;
+ if (!PendingAckValues.isNotFound(ledgerMap.remove(entryId))) {
Review Comment:
Thanks, fixed in fd761ff945af. While `removeAllUpTo` or `forEachAndClear` runs (the traversals whose callbacks may change the map), an emptied ledger map is no longer kept for reuse, so a callback that adds to another ledger gets a new one. Added `removeAllUpTo_KeepsAnEntryThatTheCallbackAddsToAnotherLedgerAfterEmptyingTheCurrentOne` (your sequence) and `forEachAndClear_DoesntVisitAnEntryThatTheCallbackAddsToAnotherLedgerAfterEmptyingTheCurrentOne`; both fail without the change.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [improve][broker] Keep each ledger's pending acks in sorted blocks instead of an AVL tree [pulsar]
Posted by "codelipenghui (via GitHub)" <gi...@apache.org>.
codelipenghui commented on code in PR #26955:
URL: https://github.com/apache/pulsar/pull/26955#discussion_r4300325949
##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/service/PendingAcksMap.java:
##########
@@ -484,15 +482,14 @@ private void internalRemoveAllUpTo(long markDeleteLedgerId, long markDeleteEntry
if (removedEntryCallback != null) {
removedEntryCallback.accept(ledgerId, entryId, batchSize, stickyKeyHash);
}
- entryMapIterator.remove();
- size--;
+ if (!PendingAckValues.isNotFound(ledgerMap.remove(entryId))) {
Review Comment:
[P3, non-blocking] Please defer recycling an inner map while a traversal still references it. During `removeAllUpTo(1, 1)`, a callback that removes `(ledger=1, entry=1)` and adds `(ledger=2, entry=1)` reuses this object, causing this removal to delete ledger 2's ACK outside the cutoff. Reproduced locally: the new ACK exists inside the callback but disappears afterward. Current production callbacks do not perform this sequence.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [improve][broker] Keep each ledger's pending acks in sorted blocks instead of an AVL tree [pulsar]
Posted by "dao-jun (via GitHub)" <gi...@apache.org>.
dao-jun merged PR #26955:
URL: https://github.com/apache/pulsar/pull/26955
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org