GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — [improve][misc] Use Netty's adaptive allocator for Pulsar's default allocator by lhotari · Pull Request #26720 · apache/pulsar

Conversation

@lhotari

@lhotari lhotari commented Sep 25, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

Motivation

Netty's AdaptiveByteBufAllocator sizes its pooled chunks and size classes to the allocation pattern it sees, and Netty 4.2 already uses it as its own default. Pulsar's default allocator, which the broker, the bookies and the clients in the Pulsar distribution use for their buffers, is still Netty's PooledByteBufAllocator. Pulsar already supports the adaptive allocator (-Dpulsar.allocator.type=adaptive) and uses it by default for the managed-ledger cache copies.

Modifications

  • conf/pulsar_env.sh and conf/bkenv.sh add -Dpulsar.allocator.default.type=adaptive, so that Pulsar's default allocator is the adaptive allocator, and -Dio.netty.allocator.type=adaptive, which pins Netty's own default allocator to the one Netty 4.2 already defaults to.
  • The options are prepended to OPTS, so the configured options (PULSAR_EXTRA_OPTS, BOOKIE_EXTRA_OPTS) come after them and override them. Since the named pulsar.allocator.default.type setting takes precedence over pulsar.allocator.type, the pooled allocator is selected again with -Dpulsar.allocator.default.type=pooled; the comments in both files say so.

Measurements

The Key_Shared 500×20 (iot-key-shared-500x20.yaml) and IoT telemetry high-rate (iot-telemetry-high-rate.yaml) performance scenarios of tests/performance, run with the performance launcher on one host (Intel i9-9980HK, 8 cores, 16 hardware threads) running the broker, 3 bookies and the clients, on JDK 25.0.4 with ZGC. Both sides used the same Pulsar build (master with the stack of #26714–#26717 applied, 1e80d28e8956), without and with this change. The measurements come from one rotation over the stack and its follow-up experiments, with the builds interleaved run by run: three unprofiled Key_Shared runs, two unprofiled high-rate runs and one profiled Key_Shared run (iot-key-shared-500x20-profile.yaml) of each build. thermald was stopped, and the launcher let the CPU package cool down to 55 °C before each run and again before each measurement (-Pperformance.cooldownTemperature=55), so every run started at 50–55 °C. None of the Key_Shared runs throttled thermally; in the high-rate scenario the host throttled thermally during both of the two runs without the change and both of the two runs with it.

Setting Key_Shared 500×20 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 none

Every run delivered all messages to every application with no duplicates, no ordering violations and no invalid messages.

Key_Shared 500×20, unprofiled:

Without With Change
Producer throughput (msg/s) 103,273 · 105,937 · 106,469 (mean 105,226) 113,610 · 114,881 · 113,446 (mean 113,979) +8.3 %
Publish latency p50 (ms) 965.6 · 938.5 · 940.5 877.1 · 864.8 · 877.6 −7.9 %
End-to-end latency p50 (ms) 980.5 · 948.2 · 954.4 894.5 · 897.0 · 892.4 −6.9 %
End-to-end latency p99 (ms) 1,122.3 · 1,073.2 · 1,096.7 1,039.4 · 1,382.4 · 1,073.2 +6.2 %
Sampled maximum backlog 25,875 · 26,765 · 49,855 43,953 · 90,782 · 56,146 +86.2 %

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):

Without With Change
Producer throughput (msg/s) 61,371 · 61,680 (mean 61,526) 62,469 · 64,190 (mean 63,330) +2.9 %
Publish latency p50 (ms) 1,626.1 · 1,648.6 1,607.7 · 1,599.5 −2.1 %
End-to-end latency p50 (ms) 2,062–2,109 · 2,076–2,107 2,022–2,062 · 2,016–2,044 −2.6 %
End-to-end latency p99 (ms) 2,896–3,031 · 2,873–2,916 2,636–2,851 · 2,533–2,630 −9.4 %
Sampled maximum backlog per application 60,383–68,605 · 82,522–85,356 67,733–71,975 · 72,689–83,762 −1.2 %

Broker, profiled Key_Shared run (JFR recording and flame graphs of the measurement period):

Without With Change
Producer throughput (msg/s) 101,116 108,700 +7.5 %
Broker CPU samples per million messages 6,389 5,756 −9.9 %
Managed-ledger thread CPU samples per million messages 913 845 −7.4 %
Java heap allocation per message (allocation flame graph) 1,814 bytes 1,968 bytes +8.5 %
Blocked time with an application frame (off-CPU) 0.34 s 0.46 s

With the adaptive allocator the broker uses less CPU per message, including on the managed-ledger thread, which is the serial stage in the Key_Shared scenario, and every Key_Shared run with it had a higher throughput than every run without it; the high-rate runs with it were also faster. The p50 latencies drop with the throughput gain. In the Key_Shared scenario the p99 is within the run-to-run spread apart from one run with it (1,382 ms), and the sampled backlog peaks are higher (43,953–90,782 against 25,875–49,855); in the high-rate scenario the p99 is lower. The broker allocates 8.5 % more on the Java heap per message with the adaptive allocator (+154 bytes), which ZGC handles within this run without a visible cost. The allocation flame graphs of the first measurement of this change, where the difference was 13 %, show where it comes from:

Heap allocation per message Pooled Adaptive
UnpooledSlicedByteBuf from AbstractByteBuf.slice 0 bytes 262 bytes
java.nio.DirectByteBuffer from PlatformDependent0.offsetSlice 0 bytes 149 bytes
UnpooledDuplicatedByteBuf from AbstractByteBuf.duplicate 0 bytes 62 bytes
Recycled pooled derived buffers and other pooled allocator objects 86 bytes 0 bytes
ResourceLeakDetector trace records 144 bytes 41 bytes
  • Derived buffers are allocated instead of recycled. A pooled buffer's retainedSlice() and retainedDuplicate() return a PooledSlicedByteBuf or PooledDuplicatedByteBuf from Netty's Recycler. AdaptivePoolingAllocator.AdaptiveByteBuf doesn't override them, so they fall back to AbstractByteBuf.slice() and duplicate(), which create an UnpooledSlicedByteBuf or UnpooledDuplicatedByteBuf on every call. A message's buffer is sliced or duplicated several times on its way through the broker: LengthFieldBasedFrameDecoder.extractFrame for each incoming frame (75 bytes per message), EntryImpl.create, createWithRetainedDuplicate and initializeMessageMetadataIfNeeded (115), OpAddEntry.initiate (34), ByteBufPair.Encoder.write (65) and BookKeeper's PerChannelBookieClient.addEntry (32).
  • Copying into an adaptive buffer creates a java.nio.DirectByteBuffer. AdaptiveByteBuf.setBytes(int, ByteBuf, int, int) copies through nioBuffer() and internalNioBuffer() of the underlying UnpooledDirectByteBuf, which create a new DirectByteBuffer view (PlatformDependent0.offsetSlice) for each call, mostly in BookKeeper's DigestManager.computeDigestAndPackageForSendingV2 (69 bytes per message) and in copies from sliced buffers (78). A pooled unsafe buffer copies by memory address without a view object.

These objects are small and short-lived, the cheapest garbage for ZGC's young generation, and the broker still uses 9.9 % less CPU per message. The difference comes from Netty's adaptive allocator implementation (Netty 4.2.18) rather than from Pulsar: if AdaptiveByteBuf recycled its derived buffers and copied by memory address, most of it would go away.

Charts of the median unprofiled Key_Shared run of each side (without 105,937 msg/s, with 113,610 msg/s), as the run report renders them:

Without With
Latency by percentile without the change Latency by percentile with the change
Throughput over time without the change Throughput over time with the change
Backlog over time without the change Backlog over time with the change

Verifying this change

  • Make sure that the change passes the CI checks.

This change is a trivial rework / code cleanup without any test coverage.

Both scripts pass bash -n. The allocator selection itself is covered by the existing PulsarByteBufAllocatorTest and PulsarByteBufAllocatorTypeTest, which include the adaptive type and the named pulsar.allocator.default.type setting.

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
  • The threading model
  • The binary protocol
  • The REST endpoints
  • The admin CLI options
  • The metrics
  • Anything that affects deployment

Default values of configurations: the scripts select the adaptive allocator for Pulsar's default allocator; -Dpulsar.allocator.default.type=pooled in PULSAR_EXTRA_OPTS or BOOKIE_EXTRA_OPTS restores the pooled allocator.

This PR was prepared with AI assistance (Claude Code) and reviewed by a human contributor.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@lhotari
[improve][misc] Use Netty's adaptive allocator for Pulsar's default a…
510208c
…llocator

Netty's AdaptiveByteBufAllocator sizes its pooled chunks to the allocation pattern it sees, and Netty 4.2 already
uses it as its own default. Pulsar's default allocator still uses PooledByteBufAllocator.

Make the adaptive allocator the default for Pulsar's general operations in conf/pulsar_env.sh and conf/bkenv.sh,
with -Dpulsar.allocator.default.type=adaptive, and pin Netty's own default with -Dio.netty.allocator.type=adaptive.
The options are prepended to OPTS, so that the configured options override them.

Assisted-by: Claude Code (claude-opus-5-5)
@lhotari
lhotari requested review from Technoboy-, dao-jun, merlimat and nodece September 25, 2026 22:53
dao-jun
dao-jun approved these changes Sep 26, 2026
View reviewed changes
@lhotari
lhotari merged commit d649aac into apache:master Sep 26, 2026
46 checks passed

Uh oh!

There was an error while loading. Please reload this page.

@lhotari lhotari mentioned this pull request Sep 30, 2026
[improve] Preload mimalloc in the Alpine-based Pulsar image #26791
Merged
1 of 11 tasks
@lhotari lhotari added this to the 5.0.0 milestone Oct 1, 2026
@dao-jun dao-jun mentioned this pull request Oct 5, 2026
[feat][ml] Port batch read and managed-ledger cache improvements (PIP-430) on BK 4.17.4.1 ascentstream/pulsar#105
Closed
dao-jun pushed a commit to ascentstream/pulsar that referenced this pull request Oct 5, 2026
@lhotari @dao-jun
[improve][misc] Use Netty's adaptive allocator for Pulsar's default a…
4b43f41
…llocator (apache#26720)

(cherry picked from commit d649aac)
Radiancebobo pushed a commit to Radiancebobo/pulsar that referenced this pull request Oct 8, 2026
@lhotari hechengbo
[improve][misc] Use Netty's adaptive allocator for Pulsar's default a…
f8ba100
…llocator (apache#26720)
@nodece nodece mentioned this pull request Oct 13, 2026
[feat][ml] Port batch read and managed-ledger cache improvements (PIP-430) on BK 4.17.4.1 (ml-only subset) ascentstream/pulsar#106
Closed
nodece pushed a commit to ascentstream/pulsar that referenced this pull request Oct 13, 2026
@lhotari @nodece
[improve][misc] Use Netty's adaptive allocator for Pulsar's default a…
4648e2a
…llocator (apache#26720)

(cherry picked from commit d649aac)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@dao-jun dao-jun dao-jun approved these changes
@merlimat merlimat Awaiting requested review from merlimat
@nodece nodece Awaiting requested review from nodece
@Technoboy- Technoboy- Awaiting requested review from Technoboy-

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

5.0.0

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

2 participants

@lhotari @dao-jun
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.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.