Fix incorrect last sequence id when sending messages in batch#546
Open
zhanglistar wants to merge 2 commits intoapache:mainfrom
Open
Fix incorrect last sequence id when sending messages in batch#546zhanglistar wants to merge 2 commits intoapache:mainfrom
zhanglistar wants to merge 2 commits intoapache:mainfrom
Conversation
added 2 commits
March 4, 2026 16:52
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.
Fixes #531
Problem
When sending messages with
sendAsyncand thenflush(),getLastSequenceId()returns a wrong value when batching is enabled.The sequence id is effectively doubled per message in the batch.
Root cause
Batch metadata
Commands::serializeSingleMessagesToBatchPayloadused the last message'ssequence_id(messages.back()) for the batch metadata. The client then assumed the broker ack referred to the first id and computedlastSequenceIdPublished_ = sequenceId + messagesCount - 1, which is wrong when the broker echoes or uses the last id.Ack handling
In
ProducerImpl::ackReceived, the code assumed the broker always sends the first sequence id of the batch and setlastSequenceIdPublished_ = sequenceId + messagesCount - 1. When the broker sends the last sequence id (common for batch acks), this formula double-counts and yields the wrong last sequence id.Solution
lib/Commands.cc
In
serializeSingleMessagesToBatchPayload, return the first message'ssequence_id(messages.front()) for the batch metadata so the batch is identified by the first message's id.lib/ProducerImpl.cc
In
ackReceived:sequenceIdis in[expectedFirstSequenceId, expectedLastSequenceId].lastSequenceIdPublished_ = expectedLastSequenceId.lastSequenceIdPublished_ = sequenceId.tests/ProducerTest.cc
Add
testGetLastSequenceIdAfterBatchFlushto verify:flush(),getLastSequenceId()is 2.getLastSequenceId()is 4.How to verify
./tests/pulsar_tests --gtest_filter="ProducerTest.testGetLastSequenceIdAfterBatchFlush"