Skip to content

[fix][ml] Preserve individual batch ACKs when merging cumulative ACKs - #26998

Merged
codelipenghui merged 1 commit into
apache:masterfrom
Denovo1998:preserve_individual_batch_acks_when_merging_cumulative_acks
Oct 20, 2026
Merged

codelipenghui merged 1 commit into
apache:masterfrom
Denovo1998:preserve_individual_batch_acks_when_merging_cumulative_acks

Conversation

@Denovo1998

@Denovo1998 Denovo1998 commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

Follow-up to #26765.

Motivation

With batch-index acknowledgment enabled, a cumulative ACK can replace the cursor's bitmap and restore indexes already acknowledged individually. For example, merging pending masks 7 and 12 stores 12 instead of 4. The cumulative path also updates mutable bitmaps before acquiring the cursor write lock, allowing it to race with individual ACK processing. Lost ACK state can cause acknowledged messages to be redelivered, including after cursor recovery.

Modifications

  • Protect cumulative batch bitmap updates and cleanup with the same cursor write lock used by individual ACKs.
  • Merge pending-message bitmaps with AND to preserve acknowledgments from both paths and prevent older snapshots from restoring acknowledged indexes.
  • Remove an empty merged bitmap and advance mark-delete through the fully acknowledged entry.
  • Add deterministic interleaving tests for partial and complete batch acknowledgment, plus recovery coverage for BookKeeper and MetadataStore persistence.

Verifying this change

  • Make sure that the change passes the CI checks.

This change added tests in ManagedCursorBatchAckRecoveryTest. All four new test invocations fail on the unpatched code. On the final code, all 19 selected test invocations passed with retries disabled. quickCheck and git diff --check also passed.

These results are from local validation; CI has not run for this change.

./gradlew :managed-ledger:test \
  --tests '*ManagedCursorBatchAckRecoveryTest' \
  --tests '*ManagedCursorTest.testBatchIndexMarkdelete' \
  --tests '*ManagedCursorTest.testBatchIndexDelete' \
  --tests '*ManagedCursorTest.testBatchIndexesDeletionPersistAndRecover' \
  --tests '*ManagedCursorTest.testDeleteBatchedMessageWithEmptyAckSet' \
  --tests '*ManagedCursorTest.testFlushCursorAfterIndividualDeleteInactivity' \
  -PtestRetryCount=0 -PtestFailFast=false \
  -PtestMaxParallelForks=1 --max-workers=1 --no-daemon

./gradlew quickCheck --max-workers=1 --no-daemon
git diff --check

Does this pull request potentially affect one of the following parts:

  • Dependencies (add or upgrade a dependency)
  • The public API
  • The schema
  • The default values of configurations
  • The threading model
  • The binary protocol
  • The REST endpoints
  • The admin CLI options
  • The metrics
  • Anything that affects deployment

Cumulative batch bitmap updates now acquire the existing cursor write lock used by individual ACK processing.

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

Reviewed at 67db263. No actionable findings identified within the reviewed scope.

The AND merge preserves both individual and cumulative batch ACKs, and the shared cursor lock correctly protects the mutable bitmap updates.

Validation with retries disabled: all 19 selected test invocations passed. Temporarily restoring the pre-fix helper reproduced the overlap regression (mask 12 instead of 8); after restoring the PR source, all four new regression cases passed again. The baseline stopped at its first expected failure.

LGTM, subject to required CI passing.

@codelipenghui codelipenghui added this to the 5.1.0 milestone Oct 20, 2026
@codelipenghui
codelipenghui merged commit 7d8107e into apache:master Oct 20, 2026
44 of 45 checks passed
lhotari pushed a commit that referenced this pull request Oct 21, 2026
@lhotari

lhotari commented Nov 24, 2026

Copy link
Copy Markdown
Member

@Denovo1998 There's some remaining gaps in this area. Here are some findings from Claude. Would you like to pick this up?

  1. A stale or late cumulative ACK can put back an orphan bitmap (ManagedCursorImpl.java:2841). This predates the PR. If the batch is already fully acked, computeIfAbsent creates a fresh bitmap from the ACK's bits. This happens in two cases:

    • The batch was moved into individualDeletedMessages. The orphan is then trimmed after the next persisted mark-delete.
    • The mark-delete is already at the batch entry, so setAcknowledgedPosition throws MarkDeletingMarkedPosition, the ACK fails, and the orphan stays until a later mark-delete passes it. Until then it can be persisted.

    The old compute() had the same behaviour. Checking internalIsMessageDeleted(position) under the lock and treating such an entry as fully acked would close the gap. That's a possible follow-up, not a defect introduced here.

  2. getBatchPositionAckSet reads the bitmap with no lock (ManagedCursorImpl.java:4716, used by PendingAckHandleImpl). This also predates the PR. It now races with in-place and() calls from both ACK paths instead of only asyncDelete. The worst case is reading a half-updated set of words. It can't crash, because and() never reallocates the words array.

  3. The batch bitmap merge is duplicated, and the asyncDelete copy has steps that are no longer needed (ManagedCursorImpl.java:3164-3190 and :2839-2853). asyncDelete (rewritten in [fix][managed-ledger] Persist partial batch ACKs on flush #26765) and ackBatchPosition (from [fix][ml] Preserve individual batch ACKs when merging cumulative ACKs #26998) both merge under the write lock with the same computeIfAbsent, in-place and() and remove-when-empty sequence. The copies have already diverged: only asyncDelete skips the merge for an entry that is already deleted (internalIsMessageDeleted, :3157), and that missing check is what allows the orphan bitmap in finding 1. A shared helper under the write lock would fix both. It would check whether the entry is already deleted, merge, and return whether the bitmap changed and whether it is now empty; each caller then handles its own "empty" case (add the entry to individualDeletedMessages, or move the mark-delete onto it).

    Since [fix][ml] Preserve individual batch ACKs when merging cumulative ACKs #26998, every write to batchDeletedIndexes happens under the write lock (:295, :939-947, :1857-1861, both merge sites). That leaves three steps in the asyncDelete copy with no purpose:

    • The comment at :3170-3172 ("another thread may have inserted a bitmap between a separate get() and computeIfAbsent()") and the containsKey + computeIfAbsent pair. No other writer can get in between, so a plain get and then a put when it's missing is enough.
    • BitSet.valueOf(givenBitSet.toByteArray()). givenBitSet is already a fresh copy made by BitSet.valueOf(long[]), so this allocates a second copy for nothing.
    • clone() then equals() to detect a change, which allocates on every partial individual ACK. AND can only clear bits, so cardinality() before and after tells you whether it changed, with no allocation. A new bitmap still counts as a change.

    This isn't a bug on master; it's a cleanup follow-up.

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.

3 participants