Skip to content

[fix][managed-ledger] Persist partial batch ACKs on flush - #26765

Merged
codelipenghui merged 3 commits into
apache:masterfrom
Asthenia0412:fix/26498-persist-partial-batch-acks
Oct 15, 2026
Merged

codelipenghui merged 3 commits into
apache:masterfrom
Asthenia0412:fix/26498-persist-partial-batch-acks

Conversation

@Asthenia0412

Copy link
Copy Markdown
Contributor

Fixes #26498

Motivation

asyncDelete updates the batch ACK bitmap for a partial acknowledgment. When this does not create an individual-delete range, the method completes without marking the cursor dirty, so the periodic flush skips the changed bitmap. A broker restart can then redeliver a message that was already acknowledged.

Modifications

Track whether a batch ACK bitmap actually changes. When the no-range path completes after such a change, mark the cursor dirty and update its latest mark-delete entry so the existing flush persists the bitmap without advancing the cursor. Add a regression test that reads the persisted cursor-ledger entry after a flush and verifies the bitmap while the individual-delete range set remains empty.

Verification

  • ./gradlew :managed-ledger:test --tests org.apache.bookkeeper.mledger.impl.ManagedCursorBatchAckRecoveryTest.testFlushPersistsPartialBatchAckWithoutIndividualRanges -PtestRetryCount=0 --no-daemon
  • ./gradlew :managed-ledger:test --tests org.apache.bookkeeper.mledger.impl.ManagedCursorBatchAckRecoveryTest --tests org.apache.bookkeeper.mledger.impl.ManagedCursorTest.testDeleteBatchedMessageWithEmptyAckSet --tests org.apache.bookkeeper.mledger.impl.ManagedCursorTest.testFlushCursorAfterIndividualDeleteInactivity -PtestRetryCount=0 --no-daemon
  • ./gradlew quickCheck --no-daemon
  • git diff --check

A cumulative ACK can insert a bitmap between the separate get() and
computeIfAbsent(), causing the existingBitSet==null branch to skip
the required bitmap merge. Always merge against whatever bitmap
computeIfAbsent returns, and supply a fresh BitSet when creating.

Signed-off-by: Asthenia0412 <[email protected]>
@Asthenia0412

Copy link
Copy Markdown
Contributor Author

@codelipenghui Addressed your feedback — pushed 8 commit updates:

  • Reworked get()+computeIfAbsent() racy merge into a single atomic path — always merge against what computeIfAbsent returns, supply fresh BitSet when creating.
  • Resolved the inline thread. Thanks for the sharp catch!

@Asthenia0412

Copy link
Copy Markdown
Contributor Author

@codelipenghui Fix pushed (26db1d8): removed the racy get() before computeIfAbsent() — the branch now always merges against whatever bitmap computeIfAbsent returns, supplying a fresh BitSet when creating. Deterministic test and your suggested 6 & 5 case both covered. Inline thread resolved. Let me know if you want me to rebase onto latest broker.

@Asthenia0412

Copy link
Copy Markdown
Contributor Author

@codelipenghui Good catch on the race condition — fixed and pushed. The original separate get()+computeIfAbsent() pattern could miss a concurrent bitmap update; now we always merge against whatever bitmap computeIfAbsent returns, creating a fresh BitSet for new entries so the stored value is independent. Please re-review when you have time.

@Asthenia0412

Copy link
Copy Markdown
Contributor Author

@codelipenghui Fixed the race you pointed out — removed the racy separate get() and always merge against whatever bitmap computeIfAbsent returns. See latest push. Ready for re-review.

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

Thanks @Asthenia0412—the earlier separate get()/computeIfAbsent() merge race is resolved in 26db1d8c47.

One issue remains: [P1] newly inserted batch bitmaps are not counted as changed, as explained in the latest inline review.

For a previously unseen batch, computeIfAbsent initializes the stored bitmap with the incoming value. Cloning it and ANDing with that same value changes nothing (2 & 2 == 2), leaving batchAckChanged false. When a clean cursor has no individual-delete ranges, it therefore never becomes dirty and flush() still skips the first partial ACK. The original duplicate-redelivery risk after a broker crash remains.

The added testFlushPersistsPartialBatchAckWithoutIndividualRanges fails on 26db1d8c47 with a five-second timeout waiting for a cursor-ledger write. The focused recovery/empty-ACK/inactivity-flush run completed with 11 passing tests and this one failure, with retries disabled.

Please count insertion as a state change while preserving the merge against the actual computeIfAbsent result, without reintroducing the separate lookup, and rerun the regression. This is distinct from the already-resolved merge race.

When computeIfAbsent creates a new entry for a previously unseen batch,
the subsequent clone-and-AND produces no delta (newBitSet AND givenBitSet ==
givenBitSet), leaving batchAckChanged false. This caused the cursor dirty
state to be incorrect when a partial batch ack created state from nothing.

Fix: check containsKey before computeIfAbsent. If the batch didn't exist
before, set batchAckChanged=true unconditionally. If it existed, fall back
to the clone/AND/compare merge to detect changes.

Signed-off-by: Trae Agent <[email protected]>
@Asthenia0412
Asthenia0412 force-pushed the fix/26498-persist-partial-batch-acks branch from 075fe57 to 6e43231 Compare October 13, 2026 21:19

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

Re-reviewed the complete change at 6e43231fc0. Both previous findings are resolved: newly inserted batch bitmaps now mark the cursor dirty, and ACK merging still uses the actual computeIfAbsent result.

Forced a fresh focused test run with retries disabled: all 12 tests passed, including the previously failing partial-ACK flush regression, recovery tests, empty-ACK deletion, and inactivity flushing. git diff --check also passed.

No actionable findings identified within the reviewed scope. LGTM, subject to required CI passing.

@Asthenia0412

Copy link
Copy Markdown
Contributor Author

@dao-jun Hello I think the pr could be merged

@codelipenghui
codelipenghui merged commit fa28230 into apache:master Oct 15, 2026
43 checks passed
lhotari pushed a commit that referenced this pull request Oct 16, 2026
lhotari pushed a commit that referenced this pull request Oct 16, 2026
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.

[Bug] Partial batch ACKs are not scheduled for persistence when individual-delete ranges are empty

4 participants