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][client] Fix consumer discarding all messages after redelivering during a reconnection by congbobo184 · Pull Request #20032 · apache/pulsar

Conversation

@congbobo184

@congbobo184 congbobo184 commented Apr 6, 2023 •
edited by merlimat
Loading

Uh oh!

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

Copy link
Copy Markdown
Contributor

Motivation

With consumer epochs (PIP-84), a Failover or Exclusive consumer discards messages that carry an epoch older than
its own. A consumer that calls redeliverUnacknowledgedMessages() while it reconnects can end up with an epoch the
broker never learns:

  1. The reconnection sends the subscribe with the current epoch, say 0, and the consumer stays in Connecting
    until the response.
  2. redeliverUnacknowledgedMessages() bumps the epoch to 1, but doesn't send the command because the consumer
    isn't Ready:
    if (cnx != null && isConnected(cnx)) {   // isConnected requires State.Ready
  3. The broker creates the consumer with epoch 0 and dispatches with it. The client discards every message
    (0 < 1 in ConsumerBase.isValidConsumerEpoch), and the consumer looks stuck until another redeliver or
    reconnection.

Sending the redeliver while Connecting would not be enough. The broker drops a redeliver for a consumer it
hasn't created yet. The redeliver can even reach the broker before the subscribe: when the subscribe write is
queued from the lookup thread and the redeliver is called on the connection's event loop, the redeliver is
written first.

Modifications

ConsumerImpl.connectionOpened() keeps the epoch it subscribes with. When the subscribe succeeds and the epoch has
changed, it sends a redeliver with the current epoch, before consumerIsReconnectedToBroker() and the initial flow
permits:

long consumerEpoch = CONSUMER_EPOCH.get(ConsumerImpl.this);
if (consumerEpoch != subscribeEpoch) {
    cnx.ctx().writeAndFlush(Commands.newRedeliverUnacknowledgedMessages(
            consumerId, consumerEpoch), cnx.ctx().voidPromise());
}
consumerIsReconnectedToBroker(cnx, currentSize);

This runs under the consumer lock that redeliverUnacknowledgedMessages() takes. A redeliver either runs before
the check, which then sends its epoch, or after the consumer is Ready, and sends the epoch itself. The command is
written directly, without bumping the epoch again or clearing the queue: the redeliver call already did both, and
no messages arrive before the flow permits.

This adopts the original change by @congbobo184 and is now client-only. The broker changes are dropped, along with
the tests that set the consumer's state and epoch directly. The fix works with any broker version.

Verifying this change

MessageRedeliveryTest.testRedeliveryWhileReconnecting creates the race for real. It holds the topic future in
BrokerService, so the reconnection's subscribe stays pending on the broker. It then calls redeliver while the
subscribe is in flight, releases the topic, and expects the message. The test fails without the fix.
MessageRedeliveryTest, ConsumerImplTest, ConsumerRedeliveryTest, ZeroQueueSizeTest and quickCheck pass.

Sorry, something went wrong.

Uh oh!

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

All reactions
congbobo184
[fix][client] Fix client redeliver epoch bigger than broker consumer …
c24bb3b
…epoch
@congbobo184
congbobo184 requested review from Technoboy-, gaoran10 and shibd April 6, 2023 15:07
@github-actions github-actions Bot added the doc-not-needed Your PR changes do not impact docs label Apr 6, 2023
@congbobo184
congbobo184 requested review from liangyepianzhou and poorbarcode April 6, 2023 15:08
congbobo184
fix log
5673942
@congbobo184 congbobo184 self-assigned this Apr 7, 2023
@github-actions

github-actions Bot commented May 7, 2023

Copy link
Copy Markdown

The pr had no activity for 30 days, mark with Stale label.

All reactions

Sorry, something went wrong.

Uh oh!

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

@github-actions github-actions Bot added the Stale label May 7, 2023
congbobo184
Merge remote-tracking branch 'apache/master' into congbo/fix/redelive…
ec4be0d
…r-epoch-consume-stuck-problem
@lhotari
lhotari requested review from BewareMyPower, codelipenghui and michaeljmarshall May 10, 2023 10:19
lhotari
lhotari reviewed May 10, 2023
View reviewed changes
Comment thread pulsar-broker/src/main/java/org/apache/pulsar/broker/service/ServerCnx.java Resolved

Uh oh!

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

@Technoboy- Technoboy- added this to the 3.2.0 milestone Jul 31, 2023
@Technoboy- Technoboy- modified the milestones: 3.2.0, 3.3.0 Dec 22, 2023
@coderzc coderzc modified the milestones: 3.3.0, 3.4.0 May 8, 2024
@lhotari
Merge branch 'master' into congbo/fix/redeliver-epoch-consume-stuck-p…
9e7e1af
…roblem
@lhotari lhotari added category/reliability The function does not work properly in certain specific environments or failures. e.g. data lost release/blocker Indicate the PR or issue that should block the release until it gets resolved labels Oct 9, 2024
lhotari
lhotari approved these changes Oct 14, 2024
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.

LGTM

Sorry, something went wrong.

Uh oh!

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

All reactions
@lhotari

lhotari commented Oct 14, 2024

Copy link
Copy Markdown
Member

closing and reopening to trigger CI

All reactions

Sorry, something went wrong.

Uh oh!

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

@lhotari lhotari closed this Oct 14, 2024
@lhotari lhotari reopened this Oct 14, 2024
lhotari added 2 commits October 14, 2024 09:50
@lhotari
Merge remote-tracking branch 'origin/master' into congbo/fix/redelive…
fe8677a
…r-epoch-consume-stuck-problem
@lhotari
Fix import
ef862ae
@lhotari lhotari closed this Nov 29, 2024
@lhotari lhotari reopened this Nov 29, 2024
@coderzc coderzc modified the milestones: 4.1.0, 4.2.0 Sep 1, 2025
merlimat added 2 commits October 22, 2026 20:28
@merlimat
Merge remote-tracking branch 'apache/master' into mmerli/adopt-20032-…
ae3cf3e
…redeliver-epoch

# Conflicts:
#	pulsar-broker/src/main/java/org/apache/pulsar/broker/service/ServerCnx.java
#	pulsar-broker/src/main/java/org/apache/pulsar/broker/service/persistent/PersistentDispatcherSingleActiveConsumer.java
#	pulsar-broker/src/test/java/org/apache/pulsar/client/impl/MessageRedeliveryTest.java
#	pulsar-client/src/main/java/org/apache/pulsar/client/impl/ConsumerImpl.java
@merlimat
[fix][client] Send the redeliver while the consumer reconnects so the…
c4e688c
… broker learns the new epoch

A Failover/Exclusive consumer that calls redeliverUnacknowledgedMessages()
while its subscribe is in flight bumped its epoch but skipped the command,
and the broker also dropped a redeliver that arrived before the consumer
was created. The broker then kept the subscribe's older epoch and the
client discarded every message it dispatched.

The client now sends the redeliver whenever it has a connection (the
subscribe success resets the permits, so none are added while
reconnecting), and the broker applies a redeliver that arrives while the
consumer is being created once it is.

Supersedes the trimmed-down original change by congbobo184.
@merlimat merlimat changed the title [fix][client] Fix client redeliver epoch bigger than broker consumer epoch [fix][client] Send the redeliver while the consumer reconnects so the broker learns the new epoch Oct 23, 2026
@merlimat

merlimat commented Oct 23, 2026

Copy link
Copy Markdown
Contributor

@congbobo184 this race is still on master, so I've taken the PR over: merged master and kept just the client send
plus the broker applying the redeliver once the consumer is created. I dropped the dispatcher change and the
ServerCnx refactor. The new test holds the topic so the subscribe stays pending and the race really happens
(lhotari's point); it fails without either half. While reconnecting, the client no longer adds permits for the
redeliver, since the subscribe success resets them. Thanks for finding this!

congbobo184 reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

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

merlimat
merlimat approved these changes Oct 23, 2026
View reviewed changes
@merlimat merlimat modified the milestones: 4.2.0, 5.1.0 Oct 23, 2026
void-ptr974
void-ptr974 reviewed Oct 23, 2026
View reviewed changes

@void-ptr974 void-ptr974 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.

Two comments on reconnect ordering and test coverage.

Sorry, something went wrong.

Uh oh!

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

All reactions
Comment thread pulsar-client/src/main/java/org/apache/pulsar/client/impl/ConsumerImpl.java Resolved

Uh oh!

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

Comment thread pulsar-broker/src/test/java/org/apache/pulsar/client/impl/MessageRedeliveryTest.java Resolved

Uh oh!

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

@merlimat
[fix][client] Send the new epoch once subscribed instead of while rec…
47f09e9
…onnecting

The redeliver sent while the consumer is Connecting can still reach the
broker before the subscribe: when the subscribe write is queued from the
lookup thread and the redeliver is called on the connection's event loop,
the redeliver is written first and the broker drops it.

The consumer now keeps the epoch it subscribed with and, once the
subscribe succeeds, sends the current epoch if a redeliver changed it,
before any flow permits. That covers every ordering and works with any
broker version, so the early send and the broker-side deferral are
reverted.
@merlimat merlimat changed the title [fix][client] Send the redeliver while the consumer reconnects so the broker learns the new epoch [fix][client] Fix consumer discarding all messages after redelivering during a reconnection Oct 23, 2026
void-ptr974
void-ptr974 approved these changes Oct 23, 2026
View reviewed changes
@merlimat
merlimat dismissed lhotari’s stale review October 23, 2026 18:52

addressed the comments

@merlimat
merlimat merged commit 4fc01a6 into apache:master Oct 23, 2026
82 of 84 checks passed

Uh oh!

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

@merlimat merlimat mentioned this pull request Oct 23, 2026
[fix][client] Flush the pending acks before the zero-queue permits on reconnect #27087
Merged
11 tasks
lhotari added a commit that referenced this pull request Oct 26, 2026
@congbobo184 congbobo184
@lhotari @merlimat
[fix][client] Fix consumer discarding all messages after redelivering…
1311566
… during a reconnection (#20032)

Co-authored-by: congbobo184 <[email protected]>
Co-authored-by: Lari Hotari <[email protected]>
Co-authored-by: Lari Hotari <[email protected]>
Co-authored-by: Matteo Merli <[email protected]>
(cherry picked from commit 4fc01a6)
@lhotari lhotari added release/5.0.2 cherry-picked/branch-5.0 labels Oct 26, 2026
This was referenced Nov 21, 2026
[improve][ci] Run CI for the 4.2.6 backports to branch-4.2 (do not merge) #27680
Closed
[improve][ci] Run CI for the 4.2.6 backports to branch-4.2 (do not merge) #27681
Closed
[improve][ci] Run CI for the 4.2.6 backports to branch-4.2 (do not merge) #27684
Closed
@lhotari lhotari added cherry-picked/branch-4.2 release/4.2.6 cherry-picked/branch-4.0 release/4.0.15 labels Nov 21, 2026
poorbarcode pushed a commit to poorbarcode/pulsar that referenced this pull request Nov 26, 2026
@congbobo184 congbobo184
@lhotari @merlimat
[fix][client] Fix consumer discarding all messages after redelivering…
f0e7f12
… during a reconnection (apache#20032)

Co-authored-by: congbobo184 <[email protected]>
Co-authored-by: Lari Hotari <[email protected]>
Co-authored-by: Lari Hotari <[email protected]>
Co-authored-by: Matteo Merli <[email protected]>
(cherry picked from commit 4fc01a6)
poorbarcode pushed a commit to poorbarcode/pulsar that referenced this pull request Nov 26, 2026
@congbobo184 congbobo184
@lhotari @merlimat
[fix][client] Fix consumer discarding all messages after redelivering…
3233350
… during a reconnection (apache#20032)

Co-authored-by: congbobo184 <[email protected]>
Co-authored-by: Lari Hotari <[email protected]>
Co-authored-by: Lari Hotari <[email protected]>
Co-authored-by: Matteo Merli <[email protected]>
(cherry picked from commit 4fc01a6)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@merlimat merlimat merlimat approved these changes
@lhotari lhotari lhotari left review comments
@Technoboy- Technoboy- Awaiting requested review from Technoboy-
@shibd shibd Awaiting requested review from shibd
@gaoran10 gaoran10 Awaiting requested review from gaoran10
@poorbarcode poorbarcode Awaiting requested review from poorbarcode
@liangyepianzhou liangyepianzhou Awaiting requested review from liangyepianzhou
@codelipenghui codelipenghui Awaiting requested review from codelipenghui
@BewareMyPower BewareMyPower Awaiting requested review from BewareMyPower
@michaeljmarshall michaeljmarshall Awaiting requested review from michaeljmarshall
+1 more reviewer
@void-ptr974 void-ptr974 void-ptr974 approved these changes
Reviewers whose approvals may not affect merge requirements

Uh oh!

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

Assignees

@congbobo184 congbobo184

Labels

category/reliability The function does not work properly in certain specific environments or failures. e.g. data lost cherry-picked/branch-4.0 cherry-picked/branch-4.2 cherry-picked/branch-5.0 doc-not-needed Your PR changes do not impact docs ready-to-test release/4.0.15 release/4.2.6 release/5.0.2 Stale triage/lhotari/important lhotari's triaging label for important issues or PRs

Projects

None yet

Milestone

5.1.0

Development

Successfully merging this pull request may close these issues.

Uh oh!

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

6 participants

@congbobo184 @lhotari @merlimat @void-ptr974 @Technoboy- @coderzc
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.