GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
GeezerVison is showing github.com — [improve][ml] Batch managed-ledger adds across the thread boundary to the ledger executor by lhotari · Pull Request #26717 · apache/pulsar Conversation
|
| Setting | IoT telemetry max rate | IoT telemetry high rate |
|---|---|---|
| Cluster | 1 broker, 3 bookies | 1 broker, 3 bookies |
| Ledger replication | E=1, W=1, A=1 | broker defaults |
| Producers | 500 gateways × 1 topic | 500 gateways × 1 topic |
| Applications | 1 × 20 consumers, Key_Shared | 5 × 10 consumers, Key_Shared |
| Messages | 4,000,000 measured, 1,000,000 warmup | 5,000,000 measured, 1,000,000 warmup |
| Payload | 128 bytes, batching off | 64 bytes, batching on |
| Rate limit | none | 30,000 msg/s |
Every run delivered all messages to every application with no duplicates, no ordering violations and no invalid messages.
IoT telemetry max rate, unprofiled (runs in the order they ran):
| Baseline (#26715) | This change | Change | |
|---|---|---|---|
| Producer throughput (msg/s) | 91,127 · 90,509 · 88,483 (mean 90,040) | 99,923 · 100,805 · 99,012 (mean 99,913) | +11.0 % |
| Publish latency p50 (ms) | 1,070.1 · 1,078.3 · 1,094.7 | 993.3 · 987.6 · 1,004.5 | −7.9 % |
| Publish latency p99 (ms) | 1,442.8 · 1,421.3 · 1,520.6 | 1,152.0 · 1,091.6 · 1,123.3 | −23.2 % |
| End-to-end latency p50 (ms) | 1,352.7 · 1,382.4 · 1,379.3 | 1,000.4 · 1,003.0 · 1,019.4 | −26.5 % |
| End-to-end latency p99 (ms) | 1,909.8 · 1,956.9 · 2,105.3 | 1,161.2 · 1,104.9 · 1,140.7 | −43.0 % |
| Sampled maximum backlog | 61,892 · 59,365 · 57,182 | 36,333 · 52,369 · 47,169 | −23.9 % |
IoT telemetry high rate, unprofiled (end-to-end latency and backlog are the ranges over the 5 applications, and their change is that of the mean over the applications):
| Baseline (#26715) | This change | Change | |
|---|---|---|---|
| Producer throughput (msg/s) | 30,000 · 30,000 (mean 30,000) | 30,000 · 30,000 (mean 30,000) | +0.0 % |
| Publish latency p50 (ms) | 2.1 · 2.2 | 2.1 · 2.1 | −2.3 % |
| End-to-end latency p50 (ms) | 8 · 8 | 8 · 8 | +0.0 % |
| End-to-end latency p99 (ms) | 23 · 19–20 | 20–21 · 24–25 | +7.1 % |
| Sampled maximum backlog per application | 2,625–2,902 · 2,212–2,466 | 3,530–5,242 · 2,380–2,727 | +43.1 % |
Broker, profiled max-rate run (JFR recording, flame graphs and off-CPU profile of the measurement period, 4,000,000 messages on each side):
| Baseline (#26715) | This change | Change | |
|---|---|---|---|
| Producer throughput (msg/s) | 88,192 | 95,001 | +7.7 % |
Blocked in GrowableBatchedArrayBlockingQueue.offer (off-CPU, observed) |
1.835 s | 0.002 s | |
| Blocked time with an application frame (off-CPU, observed) | 5.96 s | 3.19 s | |
| Ledger-thread CPU samples per million messages | 1,037 | 958 | −7.6 % |
| Broker CPU samples per million messages | 6,774 | 6,237 | −7.9 % |
In the baseline, the connection threads blocking on the ledger executor's queue lock from ServerCnx.handleSend are 30.8 % of the broker's blocked time with an application frame, the largest single wait, next to the dispatcher monitor waits in internalConsumerFlow (56 % over its two rows) that #26716 removes; with this change that wait is gone.
The managed-ledger thread (BookKeeperClientWorker-OrderedExecutor-12-0) stays the serial stage, so throughput follows its cost per message, which drops with fewer executor tasks. The max-rate throughput improves by 11.0 %, with every run of this change faster than every baseline run. End-to-end latency now follows the publish latency closely (the max-rate end-to-end p50 is on average 13 ms above the publish p50, against 290 ms in the baseline), most likely because dispatch work on the ledger executor no longer queues behind one task per published message.
The high-rate scenario publishes at a fixed 30,000 messages per second, which this host sustains on both sides, so it shows the latency at a rate below saturation rather than the maximum throughput. Both sides deliver every message at that rate with a publish p50 of 2.1–2.2 ms and an end-to-end p50 of 8 ms. The end-to-end p99 of 19–25 ms and the backlogs of at most 5,300 messages vary between the runs of each side by more than the difference between the sides: the first run of this change had the highest backlogs, with a p99 within the baseline's range, and its second run the highest p99, with backlogs within the baseline's range.
In an earlier session, an eagerly created queue with 256-entry chunks and the final lazily created queue with 512-entry chunks were compared in 3 interleaved unprofiled runs each (means 110.7k and 108.7k msg/s, within the run-to-run spread).
Charts of the median unprofiled max-rate run of each side (baseline 90,509 msg/s, this change 99,923 msg/s), as the run report renders them:
| Baseline (#26715) | This change |
|---|---|
This change added tests and can be verified as follows:
BatchingExecutorWrapperTest (unit tests with a hand-driven delegate):
BatchingExecutorWrapperTest stress tests: 8 threads submitting 10,000 tasks each keep their per-thread order (with item limits 2 and 1024, and with a weight limit); against a delegate that always rejects, every task is rejected and none runs; against a delegate that rejects every third batch, every task runs or is rejected exactly once.ManagedLedgerTest:
testAddEntryHandoverLimitsAreCapturedWhenOpened, testAddEntryHandoverWithoutByteLimit and testAddEntryHandoverBatchingDisabledUsesTheExecutor: a ledger passes its limits to the wrapper, a byte limit of 0 removes the weight limit, max batch items of 0 or 1 hands adds straight to the executor, and setConfig does not change the limits of an open ledger.testAddEntryWithAddEntryHandoverBatchingDisabled and testAsyncAddEntriesWithSmallAddEntryHandoverMaxBatchBytesSize: adds complete in order with batching disabled, and with a byte limit that gives every add a batch of its own.testRejectedAddEntryReleasesTheRetainedBuffer: with batching enabled and disabled, an add rejected by a shut down executor fails its caller and releases the buffer it retained.testAddEntryHandoverMaxBatchLimitsRejectNegativeValues: defaults and validation of the configuration.BrokerServiceTest.testManagedLedgerAddEntryHandoverMaxBatchItemsConfiguration, testManagedLedgerAddEntryHandoverMaxBatchBytesSizeConfiguration and testManagedLedgerAddEntryHandoverMaxBatchItemsDynamicUpdate: the broker settings reach the managed ledger configuration, and dynamic updates are validated and applied.ManagedLedgerErrorsTest (22 cases) passes locally; it exercises add failures and recovery through the changed hand-off.If the box was checked, please highlight the changes
Threading model: adds reach the managed ledger's executor in batches bounded by a number of adds and a total entry size, through an MPSC queue run by one batch task at a time, instead of one executor task per add. The adds still run on the same executor thread, in the same per-thread order.
This PR was prepared with AI assistance (Claude Code) and reviewed by a human contributor.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
09e083c to
a7036c2
Compare
a7036c2 to
1e80d28
Compare
1e80d28 to
9d0081d
Compare
9d0081d to
77dcc9c
Compare
77dcc9c to
ec23e49
Compare
3754c95 to
cf3ff5a
Compare
0ed9907 to
c369e43
Compare
8784fd5 to
e319d7d
Compare
… MPSC queue Motivation ManagedLedgerImpl.asyncAddEntry submitted one executor task per entry. With hundreds of producer connections on one topic, the connection threads contend on the ledger executor's queue lock (BookKeeper GrowableBatchedArrayBlockingQueue) once per message: off-CPU profiling with jonoffcpu shows it as the largest remaining blocked time with an application frame in a 500-producer Key_Shared load, and 4.9 s of blocked time in the IoT high-rate scenario. Modifications - asyncAddEntry offers the add to a JCTools MPSC queue and schedules a drain task only when none is scheduled, so concurrent publishers contend on the executor's queue once per batch of adds. - The drain task processes at most 1024 adds and reschedules itself for the rest, so add completions and other executor tasks keep running under load. A failing add is logged and does not stop the drain; a rejected drain task clears the scheduled flag and fails the caller as before. - The add is still created and processed on the executor, so the ledger's threading and each thread's add order are unchanged. Assisted-by: Claude Code (claude-opus-5-5)
…it does Batch adds across the thread boundary under names that say so: the add batch queue, scheduleAddBatch and runAddBatch replace the hand-off and drain names, and the comments describe the batching. Create the queue on the first add with a compare-and-set on a volatile field, so managed ledgers that are never written to do not allocate it. The queue is never replaced, so racing first adds all use the queue that won the compare-and-set. With that, use 512-entry chunks instead of 256, so that a batch backing up during a burst links new chunks less often: an idle ledger costs nothing and a ledger that has been written to about 2.7 KB. Assisted-by: Claude Code (claude-opus-5-5)
…edLedgerMaxAddBatchSize The number of adds that the managed ledger's executor thread processes in one batch was fixed at 1024. A larger batch reduces scheduling overhead and contention between publishing threads, but occupies the executor thread for longer, which can delay add completions, reads and cursor notifications for the ledgers that share the thread. Make it tunable so that the trade-off can be adapted to the workload. - New dynamic broker setting managedLedgerMaxAddBatchSize (default 1024), passed to ManagedLedgerConfig as maxAddBatchSize. A managed ledger captures the value when it opens, so the add path reads no shared state; updates apply to managed ledgers opened afterwards. - 0 disables batching: each add is submitted to the executor as a task of its own, as before the batching. - Negative values are rejected by the dynamic configuration validator and by ManagedLedgerConfig. Assisted-by: Claude Code (claude-opus-5-5)
…AddEntryHandoverBatchSize Name the setting after what it limits, the adds handed over to the managed ledger's executor thread in one batch, and rename the related ManagedLedgerConfig property, fields, methods and tests to match: the add batch queue becomes the add entry handover queue. Assisted-by: Claude Code (claude-opus-5-5)
…agedLedgerAddEntryHandoverMaxBatchSize Lead with the add entry handover it configures, so that the setting reads as the maximum batch size of the handover, and rename the ManagedLedgerConfig property, fields, methods and tests to match. Assisted-by: Claude Code (claude-opus-5-5)
…efore running it Take up to addEntryHandoverMaxBatchSize adds out of the handover queue with a single drain call and then run them, instead of polling the queue once per add. The list is local to the batch run on the ledger's executor thread. An add that is still being offered when the drain stops is picked up by the next batch, which the existing emptiness check schedules. Assisted-by: Claude Code (claude-opus-5-5)
e319d7d to
74b3d1f
Compare
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
… tests Move the add entry handover tests from ManagedLedgerTest to BatchingExecutorWrapperTest as unit tests, and remove the ManagedLedgerTest ones that relied on accessors removed by the extraction. Assisted-by: Claude Code (claude-opus-5-5)
…h size is greater than 1 BatchingExecutorWrapper now requires a max batch size greater than 1, since a batch of one gives nothing over handing each add over on its own. ManagedLedgerImpl still wrapped the executor for a batch size of 1, so opening a ledger with that valid configuration failed. Treat 0 and 1 alike as disabling batching, document it, and test that ledgers accept adds with either value. Assisted-by: Claude Code (claude-opus-5-5)
…heir entries A handover batch ran up to managedLedgerAddEntryHandoverMaxBatchSize adds regardless of their size, so on a loaded broker a ledger with very large entries could hold the executor thread for a long time, delaying add completions, reads and cursor notifications for the ledgers that share the thread. BatchingExecutorWrapper now takes a maxWeight in addition to maxItems: a batch stops taking tasks once the weights of those it has run add up to maxWeight, and always runs at least one task. Tasks implementing BatchingExecutorWrapper.WeightedRunnable carry a weight; other tasks weigh 0. ManagedLedgerImpl hands adds over as an AddEntryHandover that weighs the readable bytes of its entry, in place of a lambda capturing the same fields. The new limit is configured with managedLedgerAddEntryHandoverMaxBatchBytesSize (default 5 MB, 0 for no limit), and managedLedgerAddEntryHandoverMaxBatchSize is renamed to managedLedgerAddEntryHandoverMaxBatchItems. Both are dynamic and apply to managed ledgers opened after a change. Assisted-by: Claude Code (claude-opus-5-5)
…d by a rejected handover batch BatchingExecutorWrapper allocated its queue when it was created, so every managed ledger paid for it even when it was never written to. The queue is now created by the first submitted task. When the delegate rejected a handover batch, the task of the rejected caller stayed queued and could still run with a later batch, although its caller had been failed, and tasks queued meanwhile by other threads were left in the queue with no batch to run them. The thread that sets the handover flag is now the only consumer of the queue until it clears it, including while a batch runs, so a rejected thread can take the queued tasks: its own task is dropped and the rejection is thrown as before, and every other task is passed to a rejected task handler. ManagedLedgerImpl fails such adds through their callback and releases their buffer. Assisted-by: Claude Code (claude-opus-5-5)
… delegate Assisted-by: Claude Code (claude-opus-5-5)
…er limits in tests Address review findings: - asyncAddEntry retained the entry buffer before handing the add over, and leaked that reference when the executor rejected the add and the exception was thrown to the caller. Release it before rethrowing. - Test that a ledger passes its handover limits to BatchingExecutorWrapper, that 0 bytes removes the weight limit, that 0 or 1 items hands adds straight to the executor, and that setConfig does not change the limits of an open ledger. - Stress BatchingExecutorWrapper with a delegate that rejects some handover batches, checking that every task runs or is rejected exactly once. Assisted-by: Claude Code (claude-opus-5-5)
There was an error while loading. Please reload this page.
… the ledger executor (apache#26717)
There was an error while loading. Please reload this page.
Successfully merging this pull request may close these issues.
There was an error while loading. Please reload this page.