GeezerVison is showing github.com — [fix][compaction] Size rebatch buffers from uncompressed batches by Asthenia0412 · Pull Request #26732 · apache/pulsar
Conversation
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.
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.
…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.
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.
|
|
int batchSize = metadata.getNumMessagesInBatch();
|
|
|
int uncompressedSize = metadata.getUncompressedSize();
|
|
|
long rebatchBufferCapacity = (long) uncompressedSize + 8L * batchSize;
|
|
|
int batchBufferCapacity = Math.toIntExact(Math.max((long) payload.capacity(), rebatchBufferCapacity));
|
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:
|
} 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.
|
|
.messageRoutingMode(MessageRoutingMode.SinglePartition)
|
|
|
.maxPendingMessages(batchSize * 5)
|
|
|
.create()) {
|
|
|
for (int i = 0; i < batchSize * 5; i++) {
|
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.
Reviewers
lhotari
lhotari left review comments
merlimat
merlimat approved these changes
Uh oh!
There was an error while loading. Please reload this page.
Assignees
No one assigned
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.
|