[fix][broker] Fix non-batched null-value messages not removed during topic compaction#25817
Open
grishaf wants to merge 1 commit into
Open
[fix][broker] Fix non-batched null-value messages not removed during topic compaction#25817grishaf wants to merge 1 commit into
grishaf wants to merge 1 commit into
Conversation
…topic compaction When a non-Java producer (C++, Python, Go) sends a null-value message (tombstone) on a compacted topic, the key is not removed during topic compaction. The tombstone is retained in the compacted view instead of being deleted. Root cause: `AbstractTwoPhaseCompactor.extractKeyAndSize()` computes payload size using `headersAndPayload.readableBytes()`, which returns the combined size of the serialized MessageMetadata + payload. For null-value messages the payload is empty, but the metadata is always present, so readableBytes() is always > 0 (e.g. 32 bytes of metadata). This prevents the tombstone path (`size <= 0 -> latestForKey.remove(key)`) from being reached in phase one of compaction. The batch code path is not affected because it extracts per-message payload sizes from `SingleMessageMetadata.payloadSize`, which correctly returns 0 for null-value messages. The Java client always sets `numMessagesInBatch` in the metadata (even with `enableBatching(false)`), so all Java-produced messages go through the batch path -- which is why this bug was never caught by existing tests. Fix: Check `msgMetadata.hasNullValue() && msgMetadata.isNullValue()` in `extractKeyAndSize()` and return size 0, so the compaction phase one correctly removes the key from `latestForKey`. Also refactored `EventTimeOrderCompactor.extractMessageCompactionData()` to reuse `extractKeyAndSize()` instead of duplicating the size logic. Verification (testNonBatchedMessageWithNullValue): Writes raw non-batch entries (no numMessagesInBatch) to the managed ledger, triggers compaction, and reads the compacted view. | Test | Without fix | With fix | |-------------------------------------|------------------------------------|----------| | testNonBatchedMessageWithNullValue | FAILED: expected [2] but found [4] | PASSED | Co-authored-by: Cursor <cursoragent@cursor.com>
lhotari
reviewed
May 19, 2026
Comment on lines
+476
to
+478
| ByteBuf headersAndPayload = m.getHeadersAndPayload(); | ||
| Commands.skipMessageMetadata(headersAndPayload); | ||
| payloadSize = headersAndPayload.readableBytes(); |
Member
There was a problem hiding this comment.
It's better to call ByteBuf.duplicate() so that the original headerAndPayload buffer's readIndex doesn't advance:
Suggested change
| ByteBuf headersAndPayload = m.getHeadersAndPayload(); | |
| Commands.skipMessageMetadata(headersAndPayload); | |
| payloadSize = headersAndPayload.readableBytes(); | |
| ByteBuf headersAndPayload = m.getHeadersAndPayload().duplicate(); | |
| Commands.skipMessageMetadata(headersAndPayload); | |
| payloadSize = headersAndPayload.readableBytes(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Motivation
When a non-Java producer (C++, Python, Go) sends a null-value message (tombstone) on a compacted topic, the key is not removed during topic compaction. The tombstone is retained in the compacted view instead of being deleted.
Root cause:
AbstractTwoPhaseCompactor.extractKeyAndSize()computed payload size usingheadersAndPayload.readableBytes(), which returns the combined size of the serialized MessageMetadata + payload. For null-value messages the payload is empty, but the metadata is always present, soreadableBytes()is always > 0 (e.g. 32 bytes of metadata). This prevents the tombstone path (size <= 0 → latestForKey.remove(key)) from being reached in phase one of compaction.The batch code path is not affected because it extracts per-message payload sizes from
SingleMessageMetadata.payloadSize, which correctly returns 0 for null-value messages. The Java client always setsnumMessagesInBatchin the metadata (even withenableBatching(false)), so all Java-produced messages go through the batch path — which is why this bug was never caught by existing tests.Modifications
AbstractTwoPhaseCompactor.extractKeyAndSize(): Fixed to compute the correct payload-only size by usingCommands.skipMessageMetadata()to skip past the metadata before readingreadableBytes(). For compressed messages,getUncompressedSize()was already correct.EventTimeOrderCompactor.extractMessageCompactionData(): Refactored to reuseextractKeyAndSize()instead of duplicating the (buggy) size calculation.Verifying this change
This change added tests and can be verified as follows:
CompactionTest.testNonBatchedMessageWithNullValue— writes raw non-batch entries (nonumMessagesInBatchin metadata, simulating C++/Python producers) directly to the managed ledger, triggers compaction, and verifies tombstoned keys are removed from the compacted view.| Test | Without fix | With fix |
|-------------------------------------|------------------------------------|----------|
| testNonBatchedMessageWithNullValue | FAILED: expected [2] but found [4] | PASSED |
Does this pull request potentially affect one of the following parts: