Fix incorrect last sequence id when sending messages in batch#546
Merged
BewareMyPower merged 5 commits intoapache:mainfrom Mar 5, 2026
Merged
Fix incorrect last sequence id when sending messages in batch#546BewareMyPower merged 5 commits intoapache:mainfrom
BewareMyPower merged 5 commits intoapache:mainfrom
Conversation
Contributor
|
added 2 commits
March 5, 2026 18:52
Comment on lines
+867
to
+868
| // After seek-to-end the broker may close the consumer and trigger reconnect; allow a short | ||
| // delay for hasMessageAvailable to become false (avoids flakiness when reconnect completes). |
Contributor
There was a problem hiding this comment.
It's okay to reduce flakiness to avoid blocking PRs, but it should be noted that these changes don't fix the bug of hasMessageAvailable.
Contributor
Author
There was a problem hiding this comment.
Yeah. That's a little tricky here.
BewareMyPower
approved these changes
Mar 5, 2026
2 tasks
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"