Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fix][client] Fix RawReader hasMessageAvailable returns true when no messages #21032

Merged
merged 14 commits into from
Aug 20, 2023

Conversation

Technoboy-
Copy link
Contributor

@Technoboy- Technoboy- commented Aug 19, 2023

Motivation

Related #16443

When runs testHasMessageAvailableWithBatch, there is a race condition between test-thread and pulsar-client-internal-thread

time test-thread pulsar-client-internal-4082-1
1 reader.hasMessageAvailableAsync() = true
2 reader.readNextAsync() and print below msg from 3:0:-1:0 to 3:0:-1:9
3 update lastDequeuedMessageId: 3:0:-1:9
4 reader.hasMessageAvailableAsync() = true
5 reader.readNextAsync() and print below msg from 3:1:-1:0 to 3:1:-1:9
6 continue reader.hasMessageAvailableAsync() = true because lastDequeuedMessageId: 3:0:-1:9
7 update lastDequeuedMessageId: 3:1:-1:9
8 blocked

So we should update lastDequeuedMessageId before future.complete:

if (!future.complete(messageAndCnx.msg)) {
messageAndCnx.msg.close();
closeAsync();
}
MessageIdData messageId = messageAndCnx.msg.getMessageIdData();
lastDequeuedMessageId = new BatchMessageIdImpl(messageId.getLedgerId(), messageId.getEntryId(),
messageId.getPartition(), numMsg - 1);

Add related codes and logs:

2023-08-19T12:20:59,710 - INFO  - [TestNG-method=testHasMessageAvailableWithBatch-1:RawReaderImpl$RawConsumerImpl@161] - update lastDequeuedMessageId: {}, lastDequeuedMessageId
2023-08-19T12:20:59,710 - INFO  - [TestNG-method=testHasMessageAvailableWithBatch-1:RawReaderTest@162] - ids : [3:0:-1:0], keys2 : [key0]
2023-08-19T12:20:59,710 - INFO  - [TestNG-method=testHasMessageAvailableWithBatch-1:RawReaderTest@162] - ids : [3:0:-1:0, 3:0:-1:1], keys2 : [key0, key1]
2023-08-19T12:20:59,710 - INFO  - [TestNG-method=testHasMessageAvailableWithBatch-1:RawReaderTest@162] - ids : [3:0:-1:0, 3:0:-1:1, 3:0:-1:2], keys2 : [key0, key1, key2]
2023-08-19T12:20:59,710 - INFO  - [TestNG-method=testHasMessageAvailableWithBatch-1:RawReaderTest@162] - ids : [3:0:-1:0, 3:0:-1:1, 3:0:-1:2, 3:0:-1:3], keys2 : [key0, key1, key2, key3]
2023-08-19T12:20:59,710 - INFO  - [TestNG-method=testHasMessageAvailableWithBatch-1:RawReaderTest@162] - ids : [3:0:-1:0, 3:0:-1:1, 3:0:-1:2, 3:0:-1:3, 3:0:-1:4], keys2 : [key0, key1, key2, key3, key4]
2023-08-19T12:20:59,710 - INFO  - [TestNG-method=testHasMessageAvailableWithBatch-1:RawReaderTest@162] - ids : [3:0:-1:0, 3:0:-1:1, 3:0:-1:2, 3:0:-1:3, 3:0:-1:4, 3:0:-1:5], keys2 : [key0, key1, key2, key3, key4, key5]
2023-08-19T12:20:59,710 - INFO  - [TestNG-method=testHasMessageAvailableWithBatch-1:RawReaderTest@162] - ids : [3:0:-1:0, 3:0:-1:1, 3:0:-1:2, 3:0:-1:3, 3:0:-1:4, 3:0:-1:5, 3:0:-1:6], keys2 : [key0, key1, key2, key3, key4, key5, key6]
2023-08-19T12:20:59,710 - INFO  - [TestNG-method=testHasMessageAvailableWithBatch-1:RawReaderTest@162] - ids : [3:0:-1:0, 3:0:-1:1, 3:0:-1:2, 3:0:-1:3, 3:0:-1:4, 3:0:-1:5, 3:0:-1:6, 3:0:-1:7], keys2 : [key0, key1, key2, key3, key4, key5, key6, key7]
2023-08-19T12:20:59,710 - INFO  - [TestNG-method=testHasMessageAvailableWithBatch-1:RawReaderTest@162] - ids : [3:0:-1:0, 3:0:-1:1, 3:0:-1:2, 3:0:-1:3, 3:0:-1:4, 3:0:-1:5, 3:0:-1:6, 3:0:-1:7, 3:0:-1:8], keys2 : [key0, key1, key2, key3, key4, key5, key6, key7, key8]
2023-08-19T12:20:59,710 - INFO  - [TestNG-method=testHasMessageAvailableWithBatch-1:RawReaderTest@162] - ids : [3:0:-1:0, 3:0:-1:1, 3:0:-1:2, 3:0:-1:3, 3:0:-1:4, 3:0:-1:5, 3:0:-1:6, 3:0:-1:7, 3:0:-1:8, 3:0:-1:9], keys2 : [key0, key1, key2, key3, key4, key5, key6, key7, key8, key9]
2023-08-19T12:20:59,711 - INFO  - [TestNG-method=testHasMessageAvailableWithBatch-1:ConsumerImpl@2322] - hasMoreMessages,  lastMessageIdInBroker: 3:1:-1:9, lastDequeuedMessageId: 3:0:-1:9
2023-08-19T12:20:59,714 - INFO  - [TestNG-method=testHasMessageAvailableWithBatch-1:RawReaderTest@162] - ids : [3:0:-1:0, 3:0:-1:1, 3:0:-1:2, 3:0:-1:3, 3:0:-1:4, 3:0:-1:5, 3:0:-1:6, 3:0:-1:7, 3:0:-1:8, 3:0:-1:9, 3:1:-1:0], keys2 : [key0, key1, key2, key3, key4, key5, key6, key7, key8, key9, key10]
2023-08-19T12:20:59,714 - INFO  - [TestNG-method=testHasMessageAvailableWithBatch-1:RawReaderTest@162] - ids : [3:0:-1:0, 3:0:-1:1, 3:0:-1:2, 3:0:-1:3, 3:0:-1:4, 3:0:-1:5, 3:0:-1:6, 3:0:-1:7, 3:0:-1:8, 3:0:-1:9, 3:1:-1:0, 3:1:-1:1], keys2 : [key0, key1, key2, key3, key4, key5, key6, key7, key8, key9, key10, key11]
2023-08-19T12:20:59,714 - INFO  - [TestNG-method=testHasMessageAvailableWithBatch-1:RawReaderTest@162] - ids : [3:0:-1:0, 3:0:-1:1, 3:0:-1:2, 3:0:-1:3, 3:0:-1:4, 3:0:-1:5, 3:0:-1:6, 3:0:-1:7, 3:0:-1:8, 3:0:-1:9, 3:1:-1:0, 3:1:-1:1, 3:1:-1:2], keys2 : [key0, key1, key2, key3, key4, key5, key6, key7, key8, key9, key10, key11, key12]
2023-08-19T12:20:59,714 - INFO  - [TestNG-method=testHasMessageAvailableWithBatch-1:RawReaderTest@162] - ids : [3:0:-1:0, 3:0:-1:1, 3:0:-1:2, 3:0:-1:3, 3:0:-1:4, 3:0:-1:5, 3:0:-1:6, 3:0:-1:7, 3:0:-1:8, 3:0:-1:9, 3:1:-1:0, 3:1:-1:1, 3:1:-1:2, 3:1:-1:3], keys2 : [key0, key1, key2, key3, key4, key5, key6, key7, key8, key9, key10, key11, key12, key13]
2023-08-19T12:20:59,714 - INFO  - [TestNG-method=testHasMessageAvailableWithBatch-1:RawReaderTest@162] - ids : [3:0:-1:0, 3:0:-1:1, 3:0:-1:2, 3:0:-1:3, 3:0:-1:4, 3:0:-1:5, 3:0:-1:6, 3:0:-1:7, 3:0:-1:8, 3:0:-1:9, 3:1:-1:0, 3:1:-1:1, 3:1:-1:2, 3:1:-1:3, 3:1:-1:4], keys2 : [key0, key1, key2, key3, key4, key5, key6, key7, key8, key9, key10, key11, key12, key13, key14]
2023-08-19T12:20:59,714 - INFO  - [TestNG-method=testHasMessageAvailableWithBatch-1:RawReaderTest@162] - ids : [3:0:-1:0, 3:0:-1:1, 3:0:-1:2, 3:0:-1:3, 3:0:-1:4, 3:0:-1:5, 3:0:-1:6, 3:0:-1:7, 3:0:-1:8, 3:0:-1:9, 3:1:-1:0, 3:1:-1:1, 3:1:-1:2, 3:1:-1:3, 3:1:-1:4, 3:1:-1:5], keys2 : [key0, key1, key2, key3, key4, key5, key6, key7, key8, key9, key10, key11, key12, key13, key14, key15]
2023-08-19T12:20:59,714 - INFO  - [TestNG-method=testHasMessageAvailableWithBatch-1:RawReaderTest@162] - ids : [3:0:-1:0, 3:0:-1:1, 3:0:-1:2, 3:0:-1:3, 3:0:-1:4, 3:0:-1:5, 3:0:-1:6, 3:0:-1:7, 3:0:-1:8, 3:0:-1:9, 3:1:-1:0, 3:1:-1:1, 3:1:-1:2, 3:1:-1:3, 3:1:-1:4, 3:1:-1:5, 3:1:-1:6], keys2 : [key0, key1, key2, key3, key4, key5, key6, key7, key8, key9, key10, key11, key12, key13, key14, key15, key16]
2023-08-19T12:20:59,714 - INFO  - [TestNG-method=testHasMessageAvailableWithBatch-1:RawReaderTest@162] - ids : [3:0:-1:0, 3:0:-1:1, 3:0:-1:2, 3:0:-1:3, 3:0:-1:4, 3:0:-1:5, 3:0:-1:6, 3:0:-1:7, 3:0:-1:8, 3:0:-1:9, 3:1:-1:0, 3:1:-1:1, 3:1:-1:2, 3:1:-1:3, 3:1:-1:4, 3:1:-1:5, 3:1:-1:6, 3:1:-1:7], keys2 : [key0, key1, key2, key3, key4, key5, key6, key7, key8, key9, key10, key11, key12, key13, key14, key15, key16, key17]
2023-08-19T12:20:59,714 - INFO  - [TestNG-method=testHasMessageAvailableWithBatch-1:RawReaderTest@162] - ids : [3:0:-1:0, 3:0:-1:1, 3:0:-1:2, 3:0:-1:3, 3:0:-1:4, 3:0:-1:5, 3:0:-1:6, 3:0:-1:7, 3:0:-1:8, 3:0:-1:9, 3:1:-1:0, 3:1:-1:1, 3:1:-1:2, 3:1:-1:3, 3:1:-1:4, 3:1:-1:5, 3:1:-1:6, 3:1:-1:7, 3:1:-1:8], keys2 : [key0, key1, key2, key3, key4, key5, key6, key7, key8, key9, key10, key11, key12, key13, key14, key15, key16, key17, key18]
2023-08-19T12:20:59,714 - INFO  - [TestNG-method=testHasMessageAvailableWithBatch-1:RawReaderTest@162] - ids : [3:0:-1:0, 3:0:-1:1, 3:0:-1:2, 3:0:-1:3, 3:0:-1:4, 3:0:-1:5, 3:0:-1:6, 3:0:-1:7, 3:0:-1:8, 3:0:-1:9, 3:1:-1:0, 3:1:-1:1, 3:1:-1:2, 3:1:-1:3, 3:1:-1:4, 3:1:-1:5, 3:1:-1:6, 3:1:-1:7, 3:1:-1:8, 3:1:-1:9], keys2 : [key0, key1, key2, key3, key4, key5, key6, key7, key8, key9, key10, key11, key12, key13, key14, key15, key16, key17, key18, key19]
2023-08-19T12:20:59,714 - INFO  - [TestNG-method=testHasMessageAvailableWithBatch-1:ConsumerImpl@2322] - hasMoreMessages,  lastMessageIdInBroker: 3:1:-1:9, lastDequeuedMessageId: 3:0:-1:9
2023-08-19T12:20:59,714 - INFO  - [pulsar-client-internal-4082-1:RawReaderImpl$RawConsumerImpl@161] - update lastDequeuedMessageId: {}, lastDequeuedMessageId

This also fix #20998

Documentation

  • doc
  • doc-required
  • doc-not-needed
  • doc-complete

@github-actions github-actions bot added the doc-not-needed Your PR changes do not impact docs label Aug 19, 2023
@Technoboy- Technoboy- closed this Aug 19, 2023
@Technoboy- Technoboy- reopened this Aug 19, 2023
@Technoboy- Technoboy- closed this Aug 19, 2023
@Technoboy- Technoboy- reopened this Aug 19, 2023
@codecov-commenter
Copy link

codecov-commenter commented Aug 19, 2023

Codecov Report

Merging #21032 (f490be5) into master (0cb1c78) will increase coverage by 39.54%.
Report is 5 commits behind head on master.
The diff coverage is 100.00%.

Impacted file tree graph

@@              Coverage Diff              @@
##             master   #21032       +/-   ##
=============================================
+ Coverage     33.56%   73.11%   +39.54%     
- Complexity    12198    32262    +20064     
=============================================
  Files          1621     1875      +254     
  Lines        126970   139505    +12535     
  Branches      13857    15344     +1487     
=============================================
+ Hits          42618   101999    +59381     
+ Misses        78748    29436    -49312     
- Partials       5604     8070     +2466     
Flag Coverage Δ
inttests 24.24% <100.00%> (+<0.01%) ⬆️
systests 25.15% <100.00%> (?)
unittests 72.40% <100.00%> (+40.30%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files Changed Coverage Δ
...a/org/apache/pulsar/client/impl/RawReaderImpl.java 83.65% <100.00%> (+0.96%) ⬆️

... and 1518 files with indirect coverage changes

@Technoboy- Technoboy- self-assigned this Aug 19, 2023
@Technoboy- Technoboy- added this to the 3.2.0 milestone Aug 19, 2023
@Technoboy- Technoboy- changed the title [fix][test] Fix flaky test RawReaderTest [fix][test] Fix RawReader hasMessageAvailable returns true when no messages Aug 19, 2023
@Technoboy- Technoboy- changed the title [fix][test] Fix RawReader hasMessageAvailable returns true when no messages [fix][client] Fix RawReader hasMessageAvailable returns true when no messages Aug 19, 2023
@Technoboy- Technoboy- merged commit 00ca6aa into apache:master Aug 20, 2023
59 checks passed
Set<String> keys = publishMessages(topic, numKeys, true);
RawReader reader = RawReader.create(pulsarClient, topic, subscription).get();
int messageCount = 0;
while (true) {
boolean hasMsg = reader.hasMessageAvailableAsync().get();
if (hasMsg && (messageCount == numKeys)) {
Copy link
Member

@zymap zymap Aug 21, 2023

Choose a reason for hiding this comment

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

Why remove the original checks? It changes the original testing purpose, no?

Copy link
Member

@zymap zymap Aug 21, 2023

Choose a reason for hiding this comment

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

It may be blocked at the readNextAsync without any information until the test timeout.

@shibd
Copy link
Member

shibd commented Oct 22, 2023

@Technoboy- Can you help create a PR to cherry-pick this change to branch-2.11?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Flaky-test: RawReaderTest.testHasMessageAvailableWithBatch
9 participants