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/09/25 19:27:49 UTC
[PR] [improve][broker] Apply Shared and Key_Shared Flow permits without the dispatcher monitor [pulsar]
lhotari opened a new pull request, #26716:
URL: https://github.com/apache/pulsar/pull/26716
### Motivation
Each Flow command on a Shared or Key_Shared subscription schedules a task on the broker executor. The task takes
the dispatcher monitor only to add the permits to the dispatcher's total and request a read, and
`readMoreEntriesAsync` already coalesces read requests. While a dispatch or a read completion holds the monitor,
these tasks block broker executor threads.
Off-CPU profiling of 500 producers and a 20-member Key_Shared subscription shows these tasks as the largest blocked
time with an application frame in the broker: executor threads waiting for the dispatcher monitor in
`PersistentDispatcherMultipleConsumers.internalConsumerFlow` (2.5 s observed, about 10.7 s estimated per run).
### Modifications
- `Consumer` tracks whether the dispatcher accounts its Flow updates, from `addConsumer` until removal, under its
existing flow permit accounting lock.
- `PersistentDispatcherMultipleConsumers.consumerFlow` completes the pending Flow update on the calling thread.
Under the consumer's accounting lock it adds the permits to the total atomically while the consumer is
accounted, so the update is ordered with the consumer's removal, and then requests a coalesced read.
`internalConsumerFlow`, its executor task and the membership check under the dispatcher monitor are removed.
- Removal stops the consumer's accounting and debits its balance with an atomic update, as the other writers of
the total already do.
- `SharedDispatcherPermitAccountingTest` defers Flow updates through a test subscription instead of holding the
dispatcher monitor, and checks that a Flow no longer waits for the monitor.
### Measurements
The Key_Shared 500×20 performance scenario (`iot-key-shared-500x20.yaml`), run with the performance launcher on
one host (Intel i9-9980HK, 8 cores, 16 hardware threads) running the broker, 3 bookies and the clients. Baseline
is the parent of this change (#26715, `9f14092d208a`), candidate is this change (`3ea1de258a68`); the runs were
interleaved, baseline first, three unprofiled runs each and one profiled run each (`iot-key-shared-500x20-profile.yaml`).
| Setting | Value |
|---|---|
| Cluster | 1 broker, 3 bookies |
| Ledger replication | E=1, W=1, A=1 |
| Producers | 500 gateways × 1 topic |
| Applications | 1 × 20 consumers, Key_Shared |
| Messages | 4,000,000 measured, 1,000,000 warmup |
| Payload | 128 bytes, batching off |
| Rate limit | none |
Every run delivered all 5,000,000 messages to `iot-application-0` with no duplicates, no ordering violations and no
invalid messages.
| | Baseline (#26715) | This change |
|---|---|---|
| Producer throughput, unprofiled (msg/s) | 100,344 · 102,724 · 101,774 (mean 101,614) | 99,900 · 99,595 · 100,420 (mean 99,972) |
| Publish latency p50, unprofiled (ms) | 970.8 · 945.7 · 960.0 | 989.7 · 976.4 · 980.5 |
| End-to-end latency p50, unprofiled (ms) | 1,232.9 · 1,167.4 · 1,209.3 | 1,254.4 · 1,258.5 · 1,241.1 |
| End-to-end latency p99, unprofiled (ms) | 1,759.2 · 1,782.8 · 1,708.0 | 1,714.2 · 1,818.6 · 1,740.8 |
| Broker blocked time with an application frame (profiled, observed) | 4.63 s | 1.18 s |
| `internalConsumerFlow` monitor waits (profiled, observed / estimated) | 3.22 s / 12.21 s | none |
| Broker blocked time without idle waits (profiled) | 9.9 s | 5.4 s |
In the baseline, executor threads waiting for the dispatcher monitor in `internalConsumerFlow` are 70 % of the
broker's blocked time with an application frame; with this change that wait is gone, and what remains is led by
`ServerCnx.handleSend` (0.85 s, the managed-ledger executor queue and deduplication), as in the baseline (1.01 s).
The contention is gone, but on its own the change lowers producer throughput by about 1.6 % in this CPU-saturated
benchmark, and end-to-end p50 rises by about 50 ms. In the profiled runs, which moved the same 5,000,000 messages,
the broker's CPU samples in `ManagedCursorImpl.readEntriesWithSkip` rise by 7.5 % (1,344 → 1,445) and in the
Key_Shared send path `trySendMessagesToConsumers` by 19 % (686 → 819): permits reach the dispatcher sooner, so
tailing reads return smaller batches. The queued Flow tasks were acting as accidental read batching.
The managed-ledger thread is the serial stage in this workload, and tailing reads and waiting-cursor wake-ups also
run on it. Together with a follow-up change that hands adds off to the managed-ledger executor through an MPSC queue,
the same scenario reached 114.6k msg/s with an end-to-end p50 of 877 ms (one run in an earlier session).
Charts of the median unprofiled run of each side (baseline 101,774 msg/s, this change 99,900 msg/s), as the run
report renders them:
| Baseline (#26715) | This change |
|---|---|
|  |  |
|  |  |
|  |  |
### Verifying this change
- [ ] Make sure that the change passes the CI checks.
This change added tests and can be verified as follows:
- `SharedDispatcherPermitAccountingTest` (16 cases) covers Flow updates racing consumer removal, a replacement
consumer with the same identity, blocked-consumer permits, negative removal balances, and a new test that a Flow
command updates the dispatcher total while another thread holds the dispatcher monitor.
### Does this pull request potentially affect one of the following parts:
*If the box was checked, please highlight the changes*
- [ ] Dependencies (add or upgrade a dependency)
- [ ] The public API
- [ ] The schema
- [ ] The default values of configurations
- [x] The threading model
- [ ] The binary protocol
- [ ] The REST endpoints
- [ ] The admin CLI options
- [ ] The metrics
- [ ] Anything that affects deployment
Threading model: Shared and Key_Shared Flow permits are applied on the connection thread that received the Flow
command, without the dispatcher monitor and without a broker executor task. Reads are still scheduled through the
existing coalescing `readMoreEntriesAsync`.
This PR was prepared with AI assistance (Claude Code) and reviewed by a human contributor.
--
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