Repository navigation
[fix][broker] Move newPosition to nextValidLedger:-1 to avoid cursor position and ledger inconsistency in ManagedCursorImpl - #25117
Conversation
There was a problem hiding this comment.
Pull request overview
This PR fixes cursor position and ledger inconsistency in ManagedCursorImpl by moving the mark delete position to nextValidLedger:-1 when all entries in the current ledger are consumed. This addresses potential inconsistencies where the cursor position and ledger state could become misaligned.
Key Changes
- Modified
ManagedCursorImpl.asyncMarkDelete()to intelligently move cursor position tonextValidLedger:-1when appropriate - Added snapshot positions to local variables to prevent race conditions
- Added four comprehensive test cases to verify the new mark delete behavior
- Updated existing tests to reflect the new cursor positioning behavior
Reviewed changes
Copilot reviewed 8 out of 8 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/ManagedCursorImpl.java |
Core logic change to move cursor position to nextValidLedger:-1 when ledger entries are fully consumed; snapshots positions to avoid race conditions |
managed-ledger/src/test/java/org/apache/bookkeeper/mledger/impl/ManagedCursorTest.java |
Added four new test cases (testAsyncMarkDeleteMoveToNextLedgerInNonRolloverScenario, testAsyncMarkDeleteMayMoveToNextLedgerInRolloverScenario, testAsyncMarkDeleteMoveToNextLedgerOneByOne, testAsyncMarkDeleteNextLedgerMinusOneEntryIdPosition) and updated existing tests to use relaxed assertions |
managed-ledger/src/test/java/org/apache/bookkeeper/mledger/impl/ManagedLedgerTest.java |
Updated testNoRetention test to use Awaitility for proper async handling and updated assertions for cursor position validation; fixed flaky tests |
pulsar-broker/src/test/java/org/apache/pulsar/compaction/CompactedTopicTest.java |
Updated test assertions to expect cursor position at currentLedgerId:-1 after compaction |
pulsar-broker/src/test/java/org/apache/pulsar/broker/service/persistent/PersistentTopicProtectedMethodsTest.java |
Changed exact equality assertions to greater-than-or-equal assertions for mark delete position |
pulsar-broker/src/test/java/org/apache/pulsar/broker/service/PersistentMessageFinderTest.java |
Updated message expiry tests to expect mark delete position at nextLedgerId:-1 |
pulsar-broker/src/test/java/org/apache/pulsar/broker/service/MessageTTLTest.java |
Fixed spelling from "exacly" to "exactly" and updated test to expect cursor at currentLedgerId:-1 |
pulsar-broker/src/test/java/org/apache/pulsar/broker/service/BacklogQuotaManagerTest.java |
Updated assertions to reflect that mark deleting moves cursor to nextLedgerId:-1 |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #25117 +/- ##
============================================
+ Coverage 74.44% 74.56% +0.12%
- Complexity 34046 34077 +31
============================================
Files 1899 1899
Lines 149655 149711 +56
Branches 17393 17405 +12
============================================
+ Hits 111412 111636 +224
+ Misses 29368 29199 -169
- Partials 8875 8876 +1
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
BewareMyPower
left a comment
There was a problem hiding this comment.
avoid cursor position and ledger inconsistency in ManagedCursorImpl whenever possible.
Could you share which test can show the inconsistency? I see some tests are added to ManagedCursorTest, which only verify the new behavior of this PR. But it's still confusing to me that what's wrong with the current behavior.
|
This PR is inspired by #25087. If a cursor consumes the last entry of previous ledger, I think we should move the
But, unfortunately, we can not ensure happens-before between
I think current behavior is not wrong, they all worked well besides the following code(see PR description). |
Could there be any real issue caused by this inconsistency? It would be helpful if you can share a test to show the impacts. For example, generally computing I think it's a bit different from #25087. From what I understand, #25087 mainly fixes the bug that |
|
I think at least we should snapshot immutable positions into local variables to avoid race condition, or phenomena and logs will not match. The volatile positions may be updated by other threads when retrieving it again or logging. |
In continuous consuming, this should rarely happens. I was intened to do this thing: make sure Now, I intend to consolidate all the logic for moving the |
|
After this PR, if we want to move |
ad55d0b to
ffa7d8f
Compare
|
Wrong behaviours in previous code that could cause test failures: |
526e841 to
039a822
Compare
91202bb to
667d3c8
Compare
667d3c8 to
5d285ee
Compare
|
Another inconsistent test case is: If we don't move Without this PR, the assert should be |
|
Squashed commits and rebased master, ready for review. |
lhotari
left a comment
There was a problem hiding this comment.
Thanks for the detailed reply. You are right about getNextValidLedger, and I have no further concern about that point. I found one rollover race in the new check and I would also like a test for partially acked batches. Details are inline and in the thread.
Signed-off-by: Matteo Merli <[email protected]> # Conflicts: # managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/ManagedCursorImpl.java # managed-ledger/src/test/java/org/apache/bookkeeper/mledger/impl/ManagedCursorTest.java # managed-ledger/src/test/java/org/apache/bookkeeper/mledger/impl/ManagedLedgerTest.java # pulsar-broker/src/test/java/org/apache/pulsar/broker/service/MessageTTLTest.java # pulsar-broker/src/test/java/org/apache/pulsar/broker/service/persistent/PersistentTopicProtectedMethodsTest.java
… mark-delete position While a ledger is open its LedgerInfo has entries == 0, and the closed info is stored before the next ledger is added. Reading the ledger info first could pair the open ledger's placeholder with the next ledger of a concurrent rollover, so the cursor jumped to nextLedger:-1 and skipped unconsumed entries. Also add a test for a partly and a fully acked batch on the last entry of a rolled-over ledger, and adapt two tests added on master since this PR was opened to the new mark-delete behavior. Assisted-by: Claude Code (claude-opus-5-5)
…stimator test testEstimatedUnackedSizeRejectsUnconfirmedEntryInEmptyLedger relied on asyncMarkDelete accepting an entry of the next ledger beyond the last confirmed entry. That is now rejected, so assert it and set the invalid position directly to keep testing the estimator guard. Assisted-by: Claude Code (claude-opus-5-5)
|
Hi @oneby-wang, I picked this up as part of the open-PR backlog pass (dev@ thread) and pushed a few commits on top of yours. Your authorship of the change is unchanged:
@lhotari, could you take another look? |
lhotari
left a comment
There was a problem hiding this comment.
Thanks for picking this up and addressing both follow-ups. Reading the next ledger before the ledger info closes the rollover race, and the new batch test covers the partly and fully acked last entry. I have one small question about acknowledging an entry just past the end of a rolled-over ledger.
|
Thanks for picking this up. I looked at the new commits: the swapped read order closes the rollover race, and the batch test pins the partly and fully acked cases. The adapted tests look sound. I left one small question inline about accepting an entry just past the end of a rolled-over ledger. |
…_trim_ledger # Conflicts: # pulsar-broker/src/test/java/org/apache/pulsar/broker/service/BacklogQuotaManagerTest.java
|
@merlimat @lhotari Thanks for picking this up. I merged master into the PR branch and resolved the conflict in BacklogQuotaManagerTest. I kept the explicit quota check from master and the PR’s assertions that eviction leaves one empty ledger and clears the backlog. All tests passed in my personal CI. |
lhotari
left a comment
There was a problem hiding this comment.
Thanks for merging master and for explaining the reasoning behind >=. The merge leaves the PR's own changes as they were; the BacklogQuotaManagerTest conflict keeps master's explicit backlogQuotaCheck() call and the PR's assertions that eviction trims down to one empty ledger, which looks right.
The one open question is the >= normalization, answered in that thread.
lhotari
left a comment
There was a problem hiding this comment.
LGTM. Thanks for adding the test and the comment. testMarkDeleteOnePastEndWithEmptyNextLedger (ManagedCursorTest.java:7957) covers the exact case: LAC stays at A:1, the next ledger is empty, and markDelete(A:2) ends at B:-1. It also pins the >=, because with == that call would be rejected as ahead of LAC. The note at ManagedCursorImpl.java:2690-2691 explains why the condition is intentional, and the comment in testMarkDeletePositionNeverExceedLacOrNextLedgerMinusOneEntryIdPos now describes the older-ledger case it actually covers.
Motivation
Since
nextValidLedger:-1is a validmarkDeletePosition, we should movenewPositiontonextValidLedger:-1to avoid cursor position and ledger inconsistency inManagedCursorImplwhenever possible.PR #25087 also made this change.
Modifications
ManagedCursorImpl.asyncMarkDelete()method, movenewPositiontonextValidLedger:-1if we reach the following conditions:a. if
lastConfirmedEntry >= newPosition, and next ledger exists, and current ledger entries are all consumed.b. if
lastConfirmedEntry < newPosition, next ledger exists, andnewPosition == nextValidLedger:-1.I think the previous code might have a little problem.
a. If
newPosition == nextValidLedger:n, n is an non-negative number, we might set newnewPositiontonextValidLedger:nwhich is greater thanlastConfirmedEntry, which is the biggest problem.b. We can not mark delete
lacmany times if we already moved markDeletePosition tonextValidLedger:-1.c. We can not mark delete
nextValidLedger:-1directly if markDeletePosition is less thanlac.pulsar/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/ManagedCursorImpl.java
Lines 2184 to 2195 in ab65faa
And snapshot positions into a local variable to avoid race condition.
Add
testAsyncMarkDeleteMoveToNextLedgerInNonRolloverScenario,testAsyncMarkDeleteMayMoveToNextLedgerInRolloverScenario,testAsyncMarkDeleteMoveToNextLedgerOneByOne,testAsyncMarkDeleteNextLedgerMinusOneEntryIdPositiontests inManagedCursorTestto verify the code change.Fix tests due to this PR's code change.
Fix some flaky tests introduced by PR [fix][broker] Fix cursor position persistence in ledger trimming #25087.
Verifying this change
Does this pull request potentially affect one of the following parts:
If the box was checked, please highlight the changes
Documentation
docdoc-requireddoc-not-neededdoc-completeMatching PR in forked repository
PR in forked repository: oneby-wang#19