[fix] [client] Messages lost when consumer reconnect - #20695
Merged
codelipenghui merged 6 commits intoJul 4, 2023
Merged
Conversation
poorbarcode
requested review from
BewareMyPower,
Technoboy-,
codelipenghui,
mattisonchao and
tisonkun
June 30, 2023 09:20
JooHyukKim
reviewed
Jun 30, 2023
| // responsible for reconnection. And the variable "duringConnect" will prevent the concurrent execution. | ||
| if (getState() == State.Ready) { | ||
| return CompletableFuture.completedFuture(null); | ||
| } |
Contributor
There was a problem hiding this comment.
By duringConnect, did you mean duringSeek in the current method at line 785? I am assuming this because I couldn't find the word duringConnect within the method or the class itself.
If duringConnect is outside, maybe providing JavaDoc with {@link } tag will help readers to follow.
JooHyukKim
approved these changes
Jun 30, 2023
BewareMyPower
requested changes
Jun 30, 2023
mattisonchao
approved these changes
Jul 4, 2023
Member
|
Good catch! |
BewareMyPower
approved these changes
Jul 4, 2023
codelipenghui
approved these changes
Jul 4, 2023
Technoboy-
pushed a commit
that referenced
this pull request
Jul 4, 2023
liangyepianzhou
pushed a commit
that referenced
this pull request
Jul 8, 2023
(cherry picked from commit 09c89cd)
Technoboy-
pushed a commit
that referenced
this pull request
Aug 17, 2023
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.
Reopen #20591
Motivation
Background of consumer reconnects
CMD-subscribeto the brokerflow permitsto broker to incrementavailablePermitsBackground of scenarios that could trigger reconnection:
cmd-close_consumer, such asunload topic,reset clusters, and so on.Background of the response of broker received subscribe request
The broker only response
successif it receives a secondsubscriberequest of the same consumerhttps://github.com/apache/pulsar/blob/master/pulsar-broker/src/main/java/org/apache/pulsar/broker/service/ServerCnx.java#L1197
I wanted to prevent this by adding a validation below to prevent the messages which in memory is cleared if the method
grabCnxexecute after the subscribe is finished, which result in messages being lost.But I notice that there is a check
consumer.cnx == nullin the methodgrabCnx, because if thestateof the consumer isReady, the variablecnxof the consumer must not be null, so this test can be effective as the checkconsumer.state != Ready.And I notice another issue below:
Issue-1
If the method
grab connectionis executed multi times, it will lose some messages due to a race condition, for example:grab connection 1grab connection 2cnxof the consumer is nullcnxof the consumer is nullduringConnecttotrueconsumer.connectionOpened()consumer.cnxavailablePermitsduringConnecttofalseduringConnecttotrueconsumer.connectionOpened()consumer.cnxavailablePermitsduringConnecttofalseWe should make the check
consumer.cnx == nullexecute after the checkcompare and set duringConnect.Issue-2
After we fixed the
issue-1, the checkconsumer.cnx == nulland the checkduringConnect == falseswitch the order, the new issue occurs:Since we use the variableduringConnectto prevent multigrabCnxrunning at the same time, we should makeset consumer.cnx to nullbeing executed beforeset duringConnect to true` when the subscribe request fails. This can avoid the issue below:reconnect latergrab connectionduringConnecttofalseduringConnecttotruecnxof the consumer is nullset consumer.cnx to nullModifications
set consumer.cnx to nullbeing executed beforeset duringConnect to truewhen the subscribe request failsDocumentation
docdoc-requireddoc-not-neededdoc-completeMatching PR in forked repository
PR in forked repository: x