Skip to content

Fix usage of seekAsync in MessageImpl.hasMessageAvailable and flaky-test - #10190

Merged
eolivelli merged 6 commits into
apache:masterfrom
315157973:Flaky-test-1
Apr 23, 2021
Merged

eolivelli merged 6 commits into
apache:masterfrom
315157973:Flaky-test-1

Conversation

@315157973

Copy link
Copy Markdown
Contributor

Fixes #10152

Motivation

1

When the connection is reset, message received by reader may be duplicated, so I remove the judgment of whether the key is unique
Repeated scene:

start to read -> 1255:0 threadId:68
after read,update read position -> 1255:1 threadId:68
start to read -> 1255:1 threadId:68

[pulsar-external-listener-39-1] WARN org.apache.pulsar.client.impl.ConsumerImpl - [persistent://my-property/my-ns/my-reader-topic-with-batching-inclusivebdcf4a3e-6c2e-4d36-b362-52c066d14da0-partition-2] [d975f7e2-7b8f-467a-a56b-b3c1e96ec75f] Could not get connection while getLastMessageId -- Will try again in 190 ms

[pulsar-external-listener-39-1] INFO org.apache.pulsar.client.impl.ConsumerImpl - [persistent://my-property/my-ns/my-reader-topic-with-batching-inclusivebdcf4a3e-6c2e-4d36-b362-52c066d14da0-partition-2][d975f7e2-7b8f-467a-a56b-b3c1e96ec75f] Get topic last message Id

[pulsar-client-io-38-1] INFO org.apache.pulsar.client.impl.ConsumerImpl - [persistent://my-property/my-ns/
my-reader-topic-with-batching-inclusivebdcf4a3e-6c2e-4d36-b362-52c066d14da0-partition-2][d975f7e2-7b8f-467a-a56b-b3c1e96ec75f] Successfully getLastMessageId 1255:0

[pulsar-client-io-38-1] INFO org.apache.pulsar.client.impl.ConsumerImpl - [persistent://my-property/my-ns/my-reader-topic-with-batching-inclusivebdcf4a3e-6c2e-4d36-b362-52c066d14da0-partition-2][d975f7e2-7b8f-467a-a56b-b3c1e96ec75f] Seek subscription to the message 1255:0:2:4

2

When I fixed bug 1, I found bug 2
normal situation:

  1. ConsumerImpl.hasMessageAvailableAsync triggers seek, and then seek triggers reconnection
  2. Reconnection triggers connectionOpened, at this time startMessageId = clearReceiverQueue() = seekMessageId
  3. ConsumerImpl reconnects. BrokerlastDequeuedMessageId=earliest, startMessageId=seekMessageId
  4. hasMessageAvailableAsync return true
  5. The broker returns a message
  6. Consumer receive

abnormal situation:

  1. ConsumerImpl.hasMessageAvailableAsync triggers seek, and then seek triggers reconnection
  2. Reconnection triggers connectionOpened, at this time startMessageId = clearReceiverQueue() = seekMessageId
  3. ConsumerImpl reconnects. BrokerlastDequeuedMessageId=earliest, startMessageId=seekMessageId
  4. hasMessageAvailableAsync return true
  5. The broker returns a message
  6. Due to other reasons, the connection is disconnected again, and step 2, 3, and 4 are triggered again.
    When 2 is triggered, the message in incomeQueue is cleaned, and startMessageId is no longer equal to latest, so seek will not be triggered anymore, resulting in the cleaned message cannot be read

The only time to trigger seek now is:
lastDequeuedMessageId == MessageId.earliest
and
startMessageId.equals(MessageId.latest)
and
resetIncludeHead == true

Modifications

1)remove ·assertTrue(set.remove(key)) in uniteTest, because message may be duplicated
2)add seek in hasMessageAvailableAsync

@315157973 315157973 self-assigned this Apr 12, 2021
@eolivelli

Copy link
Copy Markdown
Contributor

What about changing the Title and explicitly point out that we are modifying the behaviour of "hasMessageAvailableAsync" ?

The title reads about fixing a flaky test but we are touching ConsumerImpl

@315157973

Copy link
Copy Markdown
Contributor Author

What about changing the Title and explicitly point out that we are modifying the behaviour of "hasMessageAvailableAsync" ?

The title reads about fixing a flaky test but we are touching ConsumerImpl

OK, do you have any good suggestions ?

@eolivelli

Copy link
Copy Markdown
Contributor

What about "Fix usage of seekAsync in MessageImpl.hasMessageAvailable" ?

@315157973

Copy link
Copy Markdown
Contributor Author

What about "Fix usage of seekAsync in MessageImpl.hasMessageAvailable" ?

Em...But I fixed 2 bugs in total, not only hasMessageAvailable but also including flaky-test. If I submit flaky-test separately, there will be other problems.

# Conflicts:
#	pulsar-broker/src/test/java/org/apache/pulsar/client/impl/MultiTopicsReaderTest.java
@315157973 315157973 changed the title Fix flaky-test and reader cannot read data Fix usage of seekAsync in MessageImpl.hasMessageAvailable and flaky-test Apr 13, 2021

@codelipenghui codelipenghui left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems the root cause is the reader uses a incorrect start message ID to subscribe to a topic during the reconnection. I think the right way to fix this problem is fix the start message ID that the reader used for subscribe to the topic and the topic always apply the reader's start message ID since the reader always uses the exclusive non-durable subscription. And the case of the reader never consume any messages due to the reconnection and users uses the latest position as the initial start message ID, this might introduce uncertain behavior during the reconnection. My suggestion is if uses the latest positioin as the nitial start message ID for a reader, we can get the last message ID from the broker first and then use this message ID as the start message ID to subscribe to the topic, so that as long as the consumer does not crashes, it will always uses the certain message ID to subscribe to the topic when it never receives any messages.

@codelipenghui codelipenghui added type/bug The PR fixed a bug or issue reported a bug release/2.7.2 labels Apr 15, 2021
@codelipenghui codelipenghui added this to the 2.8.0 milestone Apr 15, 2021
@315157973

Copy link
Copy Markdown
Contributor Author

I tried to handle this situation in connectionOpen, but failed:
1 The first time call hasMessageAvailable, it will get the last msg in broker, and then trigger seek lastMsgIdInBroker
2 seek will trigger disconnect and reconnect, clearReceiverQueue() will be called when connectionOpen, and then startMessageId will be assigned to seekId (lastMsgIdInBroker)
3 Then connection is disconnected again(For other unknown reasons), and the startMessageId subscribed at this time is lastMsgIdInBroker, and the broker will not return this message.

Because the sub command does not have an inclusive option, it can only be read from the next item of startMessageId. And I can't set entryId or ledger -1, otherwise the client may read forward after multiple reconnections. Unless I change the proto, add the inclusive option.

@codelipenghui

@315157973

315157973 commented Apr 21, 2021 •

Copy link
Copy Markdown
Contributor Author

Record the process:

  1. I modified the proto so that the subscription supports the inclusive option. I found that hasMessageAvailableAsync and messageProcessed are not in the same thread, which has thread safety issues with seek. Time line:
messageProcessed 1131:0:0:4 topic 0  (lastDequeuedMessageId become 1131:0:0:4)
Successfully getLastMessageId 1131:0
Seek subscription to the message 1131:0:0:4 (lastDequeuedMessageId become -1:-1:-1)
connectionOpened 1131:0:0:4 topic 0
hasMoreMessages 1131:0:0:4 lastMessageIdInBroker 1131:0:0:4 lastDequeuedMessageId-1:-1:-1 topic 0 (return true)
hasMoreMessages 1131:0:0:4 lastMessageIdInBroker 1131:0:0:4 lastDequeuedMessageId-1:-1:-1 topic 0  (return true)
message received [1132:0:1:4, 1131:0:0:4, 1133:0:2:4, 1133:0:2:4]
  1. Then I implemented the first seek in ConnectionOpen, and found that when there is no subscription, the lastMessageIdInBroker cannot be obtained.

Finally, I can only temporarily use seek to solve this problem. If anyone wants to use subscription to solve this problem, they can open another issue.

@315157973

Copy link
Copy Markdown
Contributor Author

@eolivelli @merlimat @codelipenghui PTAL

@eolivelli eolivelli left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

I believe this fix does not fix 2.7.2, it is better to keep it only on master branch and give time to settle

@eolivelli
eolivelli requested a review from codelipenghui April 23, 2021 08:23
@eolivelli

Copy link
Copy Markdown
Contributor

@codelipenghui can you please take a final look ?

@315157973

315157973 commented Apr 23, 2021 •

Copy link
Copy Markdown
Contributor Author

This PR can also fix these problems:

  1. hasMessageAvailable returns true, but the reader cannot read any message
  2. After the broker restarts, the reader will not continue to receive messages

Although it is not a perfect solution, it is solved

sijie pushed a commit that referenced this pull request May 4, 2021
### Motivation

I temporarily fixed this problem in PR #10190.
Now we have found a better way, this way can avoid the seek, then avoid trigger another reconnection.
Thank you @codelipenghui  to troubleshoot this issue with me all night.

We have added a lot of log and found that this issue is caused by some race condition problems. Here is the first reason:
https://github.com/apache/pulsar/blob/f2d72c9fc13a33df584ec1bd96a4c147774b858d/pulsar-client/src/main/java/org/apache/pulsar/client/impl/ConsumerImpl.java#L1808-L1818
Now we have an acknowledgmentsGroupingTracker to filter duplicate messages, and this Tracker will be cleaned up after seek.

However, it is possible that the connection is ready and Broker has pushed message, but `acknowledgmentsGroupingTracker.flushAndClean(); ` has not been executed yet. 

Finally hasMessageAvailableAsync returns true, but the message cannot be read because it is filtered by the acknowledgmentsGroupingTracker


### Modifications
clean the tracker when connection was open

### Verifying this change
@315157973
315157973 deleted the Flaky-test-1 branch May 11, 2021 09:02
eolivelli pushed a commit to eolivelli/pulsar that referenced this pull request May 11, 2021
)

### Motivation

I temporarily fixed this problem in PR apache#10190.
Now we have found a better way, this way can avoid the seek, then avoid trigger another reconnection.
Thank you @codelipenghui  to troubleshoot this issue with me all night.

We have added a lot of log and found that this issue is caused by some race condition problems. Here is the first reason:
https://github.com/apache/pulsar/blob/f2d72c9fc13a33df584ec1bd96a4c147774b858d/pulsar-client/src/main/java/org/apache/pulsar/client/impl/ConsumerImpl.java#L1808-L1818
Now we have an acknowledgmentsGroupingTracker to filter duplicate messages, and this Tracker will be cleaned up after seek.

However, it is possible that the connection is ready and Broker has pushed message, but `acknowledgmentsGroupingTracker.flushAndClean(); ` has not been executed yet. 

Finally hasMessageAvailableAsync returns true, but the message cannot be read because it is filtered by the acknowledgmentsGroupingTracker


### Modifications
clean the tracker when connection was open

### Verifying this change
eolivelli pushed a commit to datastax/pulsar that referenced this pull request May 24, 2021
codelipenghui pushed a commit that referenced this pull request Jun 26, 2021
### Motivation

I temporarily fixed this problem in PR #10190.
Now we have found a better way, this way can avoid the seek, then avoid trigger another reconnection.
Thank you @codelipenghui  to troubleshoot this issue with me all night.

We have added a lot of log and found that this issue is caused by some race condition problems. Here is the first reason:
https://github.com/apache/pulsar/blob/f2d72c9fc13a33df584ec1bd96a4c147774b858d/pulsar-client/src/main/java/org/apache/pulsar/client/impl/ConsumerImpl.java#L1808-L1818
Now we have an acknowledgmentsGroupingTracker to filter duplicate messages, and this Tracker will be cleaned up after seek.

However, it is possible that the connection is ready and Broker has pushed message, but `acknowledgmentsGroupingTracker.flushAndClean(); ` has not been executed yet. 

Finally hasMessageAvailableAsync returns true, but the message cannot be read because it is filtered by the acknowledgmentsGroupingTracker


### Modifications
clean the tracker when connection was open

### Verifying this change


(cherry picked from commit f69a03b)
nicoloboschi pushed a commit to datastax/pulsar that referenced this pull request Oct 19, 2021
nicoloboschi pushed a commit to datastax/pulsar that referenced this pull request Nov 24, 2021
)

I temporarily fixed this problem in PR apache#10190.
Now we have found a better way, this way can avoid the seek, then avoid trigger another reconnection.
Thank you @codelipenghui  to troubleshoot this issue with me all night.

We have added a lot of log and found that this issue is caused by some race condition problems. Here is the first reason:
https://github.com/apache/pulsar/blob/f2d72c9fc13a33df584ec1bd96a4c147774b858d/pulsar-client/src/main/java/org/apache/pulsar/client/impl/ConsumerImpl.java#L1808-L1818
Now we have an acknowledgmentsGroupingTracker to filter duplicate messages, and this Tracker will be cleaned up after seek.

However, it is possible that the connection is ready and Broker has pushed message, but `acknowledgmentsGroupingTracker.flushAndClean(); ` has not been executed yet.

Finally hasMessageAvailableAsync returns true, but the message cannot be read because it is filtered by the acknowledgmentsGroupingTracker

clean the tracker when connection was open

(cherry picked from commit f69a03b)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type/bug The PR fixed a bug or issue reported a bug

Projects

None yet

4 participants