You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by "codelipenghui (via GitHub)" <gi...@apache.org> on 2025/10/15 04:00:24 UTC

[PR] [fix][ml] Fix ledger trimming race causing cursor to point to deleted ledgers [pulsar]

codelipenghui opened a new pull request, #24855:
URL: https://github.com/apache/pulsar/pull/24855

   ## Summary
   
   Fixes a critical race condition where ledger trimming could delete ledgers while cursors still pointed to them, causing:
   - Negative backlog values from `getNumberOfEntriesInBacklog(isPrecise=false)`
   - Cursors pointing to non-existent ledgers after topic reload
   - `messagesConsumedCounter > entriesAddedCounter` inconsistency
   
   ## Root Cause
   
   The issue occurs when:
   1. Messages are acknowledged → cursor position advances in memory immediately
   2. Cursor state persists to BookKeeper asynchronously (can be slow)
   3. Ledger trimming runs during the persistence delay, using the in-memory cursor position
   4. Ledgers get deleted before cursor state is durably saved
   5. On topic reload, cursor reverts to old persistent position pointing to deleted ledgers
   
   ## The Fix
   
   Changed `maybeUpdateCursorBeforeTrimmingConsumedLedger()` in `ManagedLedgerImpl.java:2704-2705` to use the persistent cursor position instead of the in-memory position:
   
   ```java
   // Before:
   Position lastAckedPosition = cursor.getMarkDeletedPosition();
   
   // After:
   Position lastAckedPosition = cursor.getPersistentMarkDeletedPosition() != null
           ? cursor.getPersistentMarkDeletedPosition() : cursor.getMarkDeletedPosition();
   ```
   
   This ensures ledgers are only deleted after cursor advancement has been durably persisted to BookKeeper.
   
   ## Test Coverage
   
   Added `testCursorPointsToDeletedLedgerAfterTrim()` in `ManagedLedgerTest.java` which:
   - Simulates BookKeeper persistence delay (30 seconds)
   - Acknowledges messages asynchronously during the delay
   - Triggers ledger trimming
   - Verifies ledgers are NOT trimmed when persistent position hasn't advanced yet
   
   ## Verification
   
   Without the fix, the test fails because:
   - First ledger gets trimmed even though persistent cursor still points to it
   - Creates the exact race condition seen in production
   
   With the fix, the test passes because:
   - Trimming respects the persistent cursor position
   - Ledgers are only deleted after cursor state is durably saved
   
   🤖 Generated with [Claude Code](https://claude.com/claude-code)


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [fix][ml] Fix ledger trimming race causing cursor to point to deleted ledgers [pulsar]

Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #24855:
URL: https://github.com/apache/pulsar/pull/24855#issuecomment-3404426971

   @codelipenghui Please add the following content to your PR description and select a checkbox:
   ```
   - [ ] `doc` <!-- Your PR contains doc changes -->
   - [ ] `doc-required` <!-- Your PR changes impact docs and you will update later -->
   - [ ] `doc-not-needed` <!-- Your PR changes do not impact docs -->
   - [ ] `doc-complete` <!-- Docs have been already added -->
   ```


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [fix][ml] Fix ledger trimming race causing cursor to point to deleted ledgers [pulsar]

Posted by "codelipenghui (via GitHub)" <gi...@apache.org>.
codelipenghui commented on PR #24855:
URL: https://github.com/apache/pulsar/pull/24855#issuecomment-3406667240

   @lhotari Yes, I think so.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [fix][ml] Fix ledger trimming race causing cursor to point to deleted ledgers [pulsar]

Posted by "codecov-commenter (via GitHub)" <gi...@apache.org>.
codecov-commenter commented on PR #24855:
URL: https://github.com/apache/pulsar/pull/24855#issuecomment-3404795693

   ## [Codecov](https://app.codecov.io/gh/apache/pulsar/pull/24855?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) Report
   :x: Patch coverage is `0%` with `2 lines` in your changes missing coverage. Please review.
   :white_check_mark: Project coverage is 74.30%. Comparing base ([`cc5e479`](https://app.codecov.io/gh/apache/pulsar/commit/cc5e479d63103f81e3af833e8b06227d1a6563e1?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)) to head ([`c3c24de`](https://app.codecov.io/gh/apache/pulsar/commit/c3c24def31c6e198cb87842a9cd172c03ef74efe?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)).
   :warning: Report is 6 commits behind head on master.
   
   | [Files with missing lines](https://app.codecov.io/gh/apache/pulsar/pull/24855?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | Patch % | Lines |
   |---|---|---|
   | [...che/bookkeeper/mledger/impl/ManagedLedgerImpl.java](https://app.codecov.io/gh/apache/pulsar/pull/24855?src=pr&el=tree&filepath=managed-ledger%2Fsrc%2Fmain%2Fjava%2Forg%2Fapache%2Fbookkeeper%2Fmledger%2Fimpl%2FManagedLedgerImpl.java&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-bWFuYWdlZC1sZWRnZXIvc3JjL21haW4vamF2YS9vcmcvYXBhY2hlL2Jvb2trZWVwZXIvbWxlZGdlci9pbXBsL01hbmFnZWRMZWRnZXJJbXBsLmphdmE=) | 0.00% | [2 Missing :warning: ](https://app.codecov.io/gh/apache/pulsar/pull/24855?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) |
   
   <details><summary>Additional details and impacted files</summary>
   
   
   
   [![Impacted file tree graph](https://app.codecov.io/gh/apache/pulsar/pull/24855/graphs/tree.svg?width=650&height=150&src=pr&token=acYqCpsK9J&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)](https://app.codecov.io/gh/apache/pulsar/pull/24855?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)
   
   ```diff
   @@              Coverage Diff              @@
   ##             master   #24855       +/-   ##
   =============================================
   + Coverage     38.36%   74.30%   +35.93%     
   - Complexity    13123    33451    +20328     
   =============================================
     Files          1856     1913       +57     
     Lines        145070   149281     +4211     
     Branches      16836    17325      +489     
   =============================================
   + Hits          55662   110923    +55261     
   + Misses        81843    29521    -52322     
   - Partials       7565     8837     +1272     
   ```
   
   | [Flag](https://app.codecov.io/gh/apache/pulsar/pull/24855/flags?src=pr&el=flags&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | Coverage Δ | |
   |---|---|---|
   | [inttests](https://app.codecov.io/gh/apache/pulsar/pull/24855/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | `26.47% <0.00%> (+0.09%)` | :arrow_up: |
   | [systests](https://app.codecov.io/gh/apache/pulsar/pull/24855/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | `22.74% <0.00%> (-0.03%)` | :arrow_down: |
   | [unittests](https://app.codecov.io/gh/apache/pulsar/pull/24855/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | `73.81% <0.00%> (+39.28%)` | :arrow_up: |
   
   Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#carryforward-flags-in-the-pull-request-comment) to find out more.
   
   | [Files with missing lines](https://app.codecov.io/gh/apache/pulsar/pull/24855?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | Coverage Δ | |
   |---|---|---|
   | [...che/bookkeeper/mledger/impl/ManagedLedgerImpl.java](https://app.codecov.io/gh/apache/pulsar/pull/24855?src=pr&el=tree&filepath=managed-ledger%2Fsrc%2Fmain%2Fjava%2Forg%2Fapache%2Fbookkeeper%2Fmledger%2Fimpl%2FManagedLedgerImpl.java&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-bWFuYWdlZC1sZWRnZXIvc3JjL21haW4vamF2YS9vcmcvYXBhY2hlL2Jvb2trZWVwZXIvbWxlZGdlci9pbXBsL01hbmFnZWRMZWRnZXJJbXBsLmphdmE=) | `81.23% <0.00%> (+30.90%)` | :arrow_up: |
   
   ... and [1417 files with indirect coverage changes](https://app.codecov.io/gh/apache/pulsar/pull/24855/indirect-changes?src=pr&el=tree-more&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)
   </details>
   <details><summary> :rocket: New features to boost your workflow: </summary>
   
   - :snowflake: [Test Analytics](https://docs.codecov.com/docs/test-analytics): Detect flaky tests, report on failures, and find test suite problems.
   - :package: [JS Bundle Analysis](https://docs.codecov.com/docs/javascript-bundle-analysis): Save yourself from yourself by tracking and limiting bundle sizes in JS merges.
   </details>


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [fix][ml] Fix ledger trimming race causing cursor to point to deleted ledgers [pulsar]

Posted by "lhotari (via GitHub)" <gi...@apache.org>.
lhotari commented on PR #24855:
URL: https://github.com/apache/pulsar/pull/24855#issuecomment-3405255685

   @codelipenghui Does this problem apply to 3.0.x and 3.3.x?


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [fix][ml] Fix ledger trimming race causing cursor to point to deleted ledgers [pulsar]

Posted by "lhotari (via GitHub)" <gi...@apache.org>.
lhotari merged PR #24855:
URL: https://github.com/apache/pulsar/pull/24855


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org