Skip to content

[fix] [ml] incorrect non-durable cursor's backlog due to concurrently trimming ledger and non-durable cursor creation - #23951

Merged
poorbarcode merged 3 commits into
apache:masterfrom
poorbarcode:fix/non_durable_backlog
Feb 11, 2025
Merged

poorbarcode merged 3 commits into
apache:masterfrom
poorbarcode:fix/non_durable_backlog

Conversation

@poorbarcode

@poorbarcode poorbarcode commented Feb 8, 2025 •

Copy link
Copy Markdown
Contributor

Motivation

Background: the logic of calculating backlog

  • There is a variable named manegedledger.entriesAddedCounter, which means how many entries were added after the topic was loaded up at the latest time.
  • there is a variable named cursor.messagesConsumedCounter, which means how many entries were acked after the cursor was loaded up at the latest time, it can be a negative value.
  • Backlog calculating mechanism: manegedledger.entriesAddedCounter - cursor.messagesConsumedCounter

Steps to reproduce the issue

  • ledgers: [ 1, 2, 3 ], 100 entries per ledger.
  • durable-cursor: {mark-deleted-position: 3:0}
  • manegedledger.entriesAddedCounter: 0, no entries were added after the topic was loaded up.
  • concurrently trimming ledger and non-durable cursor creation
time trim ledgers non-durable cursor
1 Init the variable messagesConsumedCounter by managedLegder.ledgers: -300
2 Calculate ledgers to delete
3 Move non-durable cursors' mark deleted position
4 Skip the non-durable cursor because it has not been added to the list cursors
5 Delete ledgers
6 added to the managed ledger via addCursors

After that, if the non-durable cursor receives messages 3:0~3:99 and acknowledges them, the messagesConsumedCounter will be -200 (-300 + 100). Then backlog will be 200, but there is no message to consume|

The issue we encountered

Screenshot 2025-02-08 at 17 35 40 Screenshot 2025-02-08 at 17 35 18

Modifications

  • fix the race condition: since trimming ledger is executed in a lock block ml.synchronized, add a lock when creating a non-durable cursor
  • Since it is hard to add a test to reproduce the issue, I did not add a test.

Documentation

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

Matching PR in forked repository

PR in forked repository: x

…y trimming legder and non-durable cursor creation
@poorbarcode poorbarcode added this to the 4.1.0 milestone Feb 8, 2025
@poorbarcode poorbarcode self-assigned this Feb 8, 2025
@github-actions github-actions Bot added the doc-not-needed Your PR changes do not impact docs label Feb 8, 2025
@poorbarcode poorbarcode added the type/bug The PR fixed a bug or issue reported a bug label Feb 8, 2025
@poorbarcode

Copy link
Copy Markdown
Contributor Author

/pulsarbot rerun-failure-checks

@BewareMyPower

Copy link
Copy Markdown
Contributor

Receive messages 3:0~3:99 and ack them, cursor.messagesConsumedCounter += 100 (it will be -200)

Could you explain which method does it happen? From the table in the PR description, I can only see:

  • 2, 3, 4, 5 happen in ManagedLedgerImpl#internalTrimLedgers
  • 1 happens in ManagedLedgerImpl#initializeCursorPosition

but I'm not sure where does 6 happen.

@poorbarcode

Copy link
Copy Markdown
Contributor Author

@codecov-commenter

Copy link
Copy Markdown

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 74.23%. Comparing base (bbc6224) to head (3c816fd).
Report is 890 commits behind head on master.

Additional details and impacted files

Impacted file tree graph

@@             Coverage Diff              @@
##             master   #23951      +/-   ##
============================================
+ Coverage     73.57%   74.23%   +0.65%     
+ Complexity    32624    32280     -344     
============================================
  Files          1877     1853      -24     
  Lines        139502   143849    +4347     
  Branches      15299    16355    +1056     
============================================
+ Hits         102638   106782    +4144     
+ Misses        28908    28671     -237     
- Partials       7956     8396     +440     
Flag Coverage Δ
inttests 26.74% <100.00%> (+2.16%) ⬆️
systests 23.19% <100.00%> (-1.14%) ⬇️
unittests 73.75% <100.00%> (+0.91%) ⬆️

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

Files with missing lines Coverage Δ
...che/bookkeeper/mledger/impl/ManagedLedgerImpl.java 82.10% <100.00%> (+1.44%) ⬆️

... and 1039 files with indirect coverage changes

@BewareMyPower BewareMyPower 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.

Synchronizing the creation of NonDurableCursorImpl does not fix the race condition you mentioned. From your table in the PR description.

  • 1 happens in ManagedLedgerImpl#initializeCursorPosition, which is called in NonDurableCursorImpl's constructor
  • 2, 3, 4, 5 happen in ManagedLedgerImpl#internalTrimLedgers
  • 6 happens in ManagedCursorImpl#setAcknowledgedPosition, which is called in asyncMarkDelete or asyncDelete.

The change in this PR just synchronizes NonDurableCursorImpl's construction and internalTrimLedgers. The order 1 -> 2 -> 3 -> 4 -> 5 -> 6 could still be possible after that.

@poorbarcode
poorbarcode merged commit cc7b381 into apache:master Feb 11, 2025
poorbarcode added a commit that referenced this pull request Feb 11, 2025
… trimming ledger and non-durable cursor creation (#23951)

Co-authored-by: Yunze Xu <[email protected]>
(cherry picked from commit cc7b381)
poorbarcode added a commit that referenced this pull request Feb 11, 2025
… trimming ledger and non-durable cursor creation (#23951)

Co-authored-by: Yunze Xu <[email protected]>
(cherry picked from commit cc7b381)
poorbarcode added a commit that referenced this pull request Feb 11, 2025
… trimming ledger and non-durable cursor creation (#23951)

Co-authored-by: Yunze Xu <[email protected]>
(cherry picked from commit cc7b381)
nodece pushed a commit to ascentstream/pulsar that referenced this pull request Feb 11, 2025
… trimming ledger and non-durable cursor creation (apache#23951)

Co-authored-by: Yunze Xu <[email protected]>
(cherry picked from commit cc7b381)
hanmz pushed a commit to hanmz/pulsar that referenced this pull request Feb 12, 2025
… trimming ledger and non-durable cursor creation (apache#23951)

Co-authored-by: Yunze Xu <[email protected]>
nikhil-ctds pushed a commit to datastax/pulsar that referenced this pull request Feb 19, 2025
… trimming ledger and non-durable cursor creation (apache#23951)

Co-authored-by: Yunze Xu <[email protected]>
(cherry picked from commit cc7b381)
(cherry picked from commit 16bfb1a)
mukesh-ctds pushed a commit to datastax/pulsar that referenced this pull request Feb 20, 2025
… trimming ledger and non-durable cursor creation (apache#23951)

Co-authored-by: Yunze Xu <[email protected]>
(cherry picked from commit cc7b381)
(cherry picked from commit 05797ac)
srinath-ctds pushed a commit to datastax/pulsar that referenced this pull request Feb 24, 2025
… trimming ledger and non-durable cursor creation (apache#23951)

Co-authored-by: Yunze Xu <[email protected]>
(cherry picked from commit cc7b381)
(cherry picked from commit 16bfb1a)
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