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 Reader.hasMessageAvailable might return true after seeking to latest #22201

Conversation

BewareMyPower
Copy link
Contributor

@BewareMyPower BewareMyPower commented Mar 5, 2024

Motivation

Java client has the same issue with
apache/pulsar-client-python#199

After a seek operation is done, the startMessageId will be updated until the reconnection due to the seek is done in connectionOpened. So before it's updated, hasMessageAvailable could compare with an outdated startMessageId and return a wrong value.

Modifications

Replace duringSeek with a SeekStatus field:

  • NOT_STARTED: initial, or a seek operation is done. seek could only succeed in this status.
  • IN_PROGRESS: A seek operation has started but the client does not receive the response from broker.
  • COMPLETED: The client has received the seek response but the seek future is not done.

After the status becomes COMPLETED, if the connection is not ready, next time the connection is established, the status will change from COMPLETED to NOT_STARTED and then seek future will be completed in the internal executor.

Add testHasMessageAvailableAfterSeek to cover this change.

Documentation

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

Matching PR in forked repository

PR in forked repository: BewareMyPower#30

@BewareMyPower BewareMyPower self-assigned this Mar 5, 2024
@BewareMyPower BewareMyPower added the type/bug The PR fixed a bug or issue reported a bug label Mar 5, 2024
@github-actions github-actions bot added the doc-not-needed Your PR changes do not impact docs label Mar 5, 2024
@BewareMyPower BewareMyPower added this to the 3.3.0 milestone Mar 5, 2024
@BewareMyPower BewareMyPower marked this pull request as draft March 5, 2024 07:33
…seeking to latest

### Motivation

Java client has the same issue with
apache/pulsar-client-python#199

After a seek operation is done, the `startMessageId` will be updated
until the reconnection due to the seek is done in `connectionOpened`. So
before it's updated, `hasMessageAvailable` could compare with an
outdated `startMessageId` and return a wrong value.

### Modifications

Replace `duringSeek` with a `SeekStatus` field:
- `NOT_STARTED`: initial, or a seek operation is done. `seek` could only
  succeed in this status.
- `IN_PROGRESS`: A seek operation has started but the client does not
  receive the response from broker.
- `COMPLETED`: The client has received the seek response but the seek
  future is not done.

After the status becomes `COMPLETED`, if the connection is not ready,
next time the connection is established, the status will change from
`COMPLETED` to `NOT_STARTED` and then seek future will be completed in
the internal executor.

Add `testHasMessageAvailableAfterSeek` to cover this change.
@BewareMyPower BewareMyPower force-pushed the bewaremypower/fix-seek-has-msg-available branch from 79ad518 to 5f09ec7 Compare March 5, 2024 07:41
@BewareMyPower BewareMyPower marked this pull request as ready for review March 5, 2024 07:42
Copy link
Member

@lhotari lhotari left a comment

Choose a reason for hiding this comment

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

LGTM, awesome job @BewareMyPower

@BewareMyPower BewareMyPower marked this pull request as draft March 5, 2024 11:05
@BewareMyPower
Copy link
Contributor Author

There seems to be something wrong for the multi-topics consumer seek. I'm investigating

@BewareMyPower BewareMyPower marked this pull request as ready for review March 5, 2024 14:09
@codecov-commenter
Copy link

Codecov Report

Attention: Patch coverage is 80.00000% with 9 lines in your changes are missing coverage. Please review.

Project coverage is 73.56%. Comparing base (bbc6224) to head (901c6a9).
Report is 23 commits behind head on master.

Additional details and impacted files

Impacted file tree graph

@@             Coverage Diff              @@
##             master   #22201      +/-   ##
============================================
- Coverage     73.57%   73.56%   -0.01%     
+ Complexity    32624    32071     -553     
============================================
  Files          1877     1878       +1     
  Lines        139502   139663     +161     
  Branches      15299    15335      +36     
============================================
+ Hits         102638   102750     +112     
- Misses        28908    28931      +23     
- Partials       7956     7982      +26     
Flag Coverage Δ
inttests 26.69% <48.88%> (+2.11%) ⬆️
systests 24.30% <48.88%> (-0.03%) ⬇️
unittests 72.82% <80.00%> (-0.02%) ⬇️

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

Files Coverage Δ
...va/org/apache/pulsar/client/impl/ConsumerImpl.java 77.50% <80.00%> (-0.07%) ⬇️

... and 68 files with indirect coverage changes

@codelipenghui codelipenghui merged commit 95a53f3 into apache:master Mar 7, 2024
50 checks passed
@Technoboy- Technoboy- changed the title [fix][client] fix Reader.hasMessageAvailable might return true after seeking to latest [fix][client] Fix Reader.hasMessageAvailable might return true after seeking to latest Mar 11, 2024
Technoboy- pushed a commit that referenced this pull request Mar 13, 2024
@BewareMyPower BewareMyPower deleted the bewaremypower/fix-seek-has-msg-available branch March 26, 2024 02:34
BewareMyPower added a commit to BewareMyPower/pulsar that referenced this pull request Mar 26, 2024
…fter seeking by timestamp

### Motivation

After seeking by timestamp, `hasMessageAvailable()` and `readNext()`
could return wrong values.

The 1st bug was a regression introduced by
apache#22201, which modifies
`startMessageId` to `seekMessageId` before a seek operation is done.
However, the previous behavior is also a bug but accidentally works in
this case. When seeking by timestamp, the `seekMessageId` is modified to
`earliest`, which should not be compared with `lastMessageIdInBroker`
because the actual start position is determined by the seek timestamp,
not the `earliest` message id.

The 2nd bug was caused by apache#9652,
when `startMessageIdInclusive()` is configured to create a reader, it
could seek to the position of the latest message when
`lastDequeuedMessageId` is `earliest` and `startMessageId` is `latest`.

### Modifications

Add a boolean flag `hasSoughtByTimestamp` to represent whether the last
seek call accepts a timestamp. If it's true, don't take `startMessageId`
into comparison with `lastMessageIdInBroker`, just compare the
mark-delete position and last position in the GetLastMessageId response.

Add `testHasMessageAvailableAfterSeekTimestamp` to verify the change.

For the `readNext` call, if the reader has sought by timestamp, don't
seek to the latest position in `hasMessageAvailable`. Modify
`testReadMessageWithBatchingWithMessageInclusive` to verify the fix.
However, this patch does not modify the existing behavior when `seek` is
not called because the inclusive reader relies on the implicit seek
operation in `hasMessageAvailable`.
@lhotari
Copy link
Member

lhotari commented Mar 27, 2024

It's necessary to cherry-pick #20963 so that this fix could be applied cleanly.

lhotari pushed a commit that referenced this pull request Mar 27, 2024
mukesh-ctds pushed a commit to datastax/pulsar that referenced this pull request Apr 15, 2024
…seeking to latest (apache#22201)

(cherry picked from commit 95a53f3)
(cherry picked from commit 318ff33)
mukesh-ctds pushed a commit to datastax/pulsar that referenced this pull request Apr 17, 2024
…seeking to latest (apache#22201)

(cherry picked from commit 95a53f3)
(cherry picked from commit 318ff33)
mukesh-ctds pushed a commit to datastax/pulsar that referenced this pull request Apr 17, 2024
…seeking to latest (apache#22201)

(cherry picked from commit 95a53f3)
(cherry picked from commit 318ff33)
mukesh-ctds pushed a commit to datastax/pulsar that referenced this pull request Apr 19, 2024
…seeking to latest (apache#22201)

(cherry picked from commit 95a53f3)
(cherry picked from commit 318ff33)
srinath-ctds pushed a commit to datastax/pulsar that referenced this pull request Apr 23, 2024
…seeking to latest (apache#22201)

(cherry picked from commit 95a53f3)
(cherry picked from commit 318ff33)
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.

6 participants