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 — [fix][compaction] Size rebatch buffers from uncompressed batches by Asthenia0412 · Pull Request #26732 · apache/pulsar

Conversation

@Asthenia0412

@Asthenia0412 Asthenia0412 commented Sep 28, 2026 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Contributor

Motivation

RawBatchConverter.rebatchMessage creates its output buffer using the compressed input capacity, then writes the decompressed batch back into it. With highly compressible batches and long partition keys, the buffer can be smaller than the serialized batch. The report in #26290 documents deterministic compaction failures on Pulsar 4.0.x and 4.1.x. Newer generated metadata writers grow the buffer, but the allocation still relies on that implementation detail.

Modifications

Size the rebatch buffer from the uncompressed batch size plus per-message serialization headroom, preserving the input capacity when it is larger. Add a regression test that re-batches a ZSTD-compressed batch containing long partition keys and verifies all retained keys.

Also fixes the buffer-lifetime issue raised in review: the previous code allocated batchBuffer before the codec.decode() call, so a decode failure leaked it. The buffer is now allocated inside the try block (after decode) and released behind a null check, so both a decode failure and a failed allocation release everything they own.

Checks

  • ./gradlew :pulsar-broker:test --tests 'org.apache.pulsar.client.impl.RawReaderTest.testBatchingRebatchWithHighlyCompressedPartitionKeys' -PtestRetryCount=0 (passed)
  • ./gradlew quickCheck (passed)

The only red check on the previous head was CI - Integration - Kubernetes, which failed in the Restore build outputs from build job step before any test ran (artifact-restore flake, not a test failure); this push re-triggers it.

Fixes #26290

Sorry, something went wrong.

Uh oh!

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

All reactions
@Asthenia0412 Trae Bot
[fix][compaction] Size rebatch buffers for uncompressed messages
05089cb
Fixes apache#26290
@Asthenia0412
Asthenia0412 force-pushed the fix-26290-rebatch-buffer-capacity branch from b624fac to 05089cb Compare October 17, 2026 01:07
@merlimat merlimat mentioned this pull request Oct 18, 2026
fix: allocate rebatch buffer with uncompressed size (#26290) #26292
Closed
merlimat
merlimat approved these changes Oct 18, 2026
View reviewed changes
@merlimat merlimat added this to the 5.1.0 milestone Oct 18, 2026
Denovo1998
Denovo1998 approved these changes Oct 25, 2026
View reviewed changes

@Denovo1998 Denovo1998 left a comment

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM overall. The buffer sizing change makes sense, especially with the extra headroom for compacted-out messages.

One thing I noticed is that batchBuffer isn't released if decoding fails before entering the try/finally block. This was a pre-existing issue, but the larger allocation could make the leak more significant. It might be worth addressing as well.

Also, since the original issue affects 4.0.x and 4.1.x, we should make sure the fix gets cherry-pick to the relevant supported branches? @merlimat

Sorry, something went wrong.

Uh oh!

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

All reactions
yancy
[fix][compaction] Release rebatch buffer when decoding or allocation …
0cfc949
…fails

Addresses the review note on apache#26732: when codec.decode() threw, the
rebatch buffer allocated before the try/finally block was leaked. Move
the allocation after decode and inside the try block, and guard the
release with a null check so a failed allocation still releases the
decoded payload.
lhotari
lhotari reviewed Nov 18, 2026
View reviewed changes

@lhotari lhotari left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for digging into the rebatch failure in #26290 and for tightening the buffer cleanup on the decode path.

The cleanup part looks right to me. At the merge base batchBuffer was allocated before codec.decode() and leaked when decode threw. Allocating it after decode inside the try, with the null-guarded release, fixes that.

I'm less sure about the sizing change on master. master (and branch-4.2) are no longer affected by the original failure: since the LightProto upgrade in #25332, the generated SingleMessageMetadata.writeTo() reserves its space with ensureWritable before writing. branch-4.0 was fixed separately in #27051 by reserving each record in Commands.serializeSingleMessageInBatchWithPayload. I checked this by reverting RawBatchConverter.java to the merge base (745b216) and running RawReaderTest.testBatchingRebatchWithHighlyCompressedPartitionKeys. It passes without the production change. Meanwhile, the up-front sizing adds some behavior of its own, described inline. I'd suggest keeping the cleanup fix and the test, and dropping the metadata-based capacity (or sizing from the decoded buffer instead).

Sorry, something went wrong.

Uh oh!

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

All reactions
Comment thread pulsar-broker/src/main/java/org/apache/pulsar/client/impl/RawBatchConverter.java
int batchSize = metadata.getNumMessagesInBatch();
int uncompressedSize = metadata.getUncompressedSize();
long rebatchBufferCapacity = (long) uncompressedSize + 8L * batchSize;
int batchBufferCapacity = Math.toIntExact(Math.max((long) payload.capacity(), rebatchBufferCapacity));

@lhotari lhotari Nov 18, 2026

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[RISK] the new capacity trusts uncompressed_size from the metadata, which uncompressed batches never check

For CompressionType.NONE, CompressionCodecNone.decode() ignores uncompressedSize and returns the payload as it is, so for uncompressed batches the field is never checked against the actual data. Before this change such a batch rebatched from payload.capacity() whatever the field said. Now, if the metadata value is larger than the real payload, two things can happen:

  • The buffer is sized from the declared value rather than from the data.
  • When the sum exceeds Integer.MAX_VALUE, Math.toIntExact throws ArithmeticException before decode. phaseTwoLoop falls back to keeping the whole batch only on IOException, so the runtime exception fails the compaction run, and every retry fails on the same entry:

pulsar/pulsar-broker/src/main/java/org/apache/pulsar/compaction/AbstractTwoPhaseCompactor.java

Lines 307 to 317 in 0cfc949

} else if (RawBatchConverter.isReadableBatch(metadata)) {
try {
messageToAdd = rebatchMessage(reader.getTopic(),
m, metadata, (key, subid) -> subid.equals(latestForKey.get(key)),
topicCompactionRetainNullKey);
} catch (IOException ioe) {
log.info()
.attr("message", id)
.exception(ioe)
.log("Error decoding batch for message . Whole batch will be included in output");
messageToAdd = Optional.of(m);

For valid compressed batches, the full-size output buffer is also reserved before any key is filtered. With a highly compressible batch where most keys are dropped, peak memory is about twice the decoded size, even when the result is empty. The old buffer started small and grew with what was written.

On master the buffer already grows on demand (LightProto 0.9.3 writeTo() calls ensureWritable). I'd suggest keeping payload.capacity() as the initial capacity. If you want a pre-size anyway, use the actual decoded length now that allocation happens after decode, e.g. uncompressedPayload.readableBytes(). For a defensive guard against older generated writers, reserving per record as #27051 did on branch-4.0 is bounded by real data:

batchBuffer.ensureWritable(Integer.BYTES + singleMessageMetadata.getSerializedSize() + payload.readableBytes());

Sorry, something went wrong.

Uh oh!

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

All reactions
Comment thread pulsar-broker/src/test/java/org/apache/pulsar/client/impl/RawReaderTest.java
.messageRoutingMode(MessageRoutingMode.SinglePartition)
.maxPendingMessages(batchSize * 5)
.create()) {
for (int i = 0; i < batchSize * 5; i++) {

@lhotari lhotari Nov 18, 2026

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[NIT] the new test passes on master without the production change

With RawBatchConverter.java reverted to the merge base, testBatchingRebatchWithHighlyCompressedPartitionKeys still passes, because the current generated writer grows the buffer itself. The test is still useful coverage for long partition keys in compressed batches. It might be worth saying in the PR description that on master it is regression coverage, not a reproducer.

A small cleanup: the loop sends batchSize * 5 messages, but only the first batch is read, so a single batch (batchSize messages, maxPendingMessages(batchSize)) keeps every assertion and drops 4,000 sends.

Sorry, something went wrong.

Uh oh!

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

All reactions
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@lhotari lhotari lhotari left review comments
@merlimat merlimat merlimat approved these changes
@Denovo1998 Denovo1998 Denovo1998 approved these changes

Uh oh!

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

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

5.1.0

Development

Successfully merging this pull request may close these issues.

[Bug] Topic compaction fails permanently with IndexOutOfBoundsException when rebatching compressed batch messages

Uh oh!

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

4 participants

@Asthenia0412 @merlimat @lhotari @Denovo1998
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.