Skip to content

[fix][broker] Preserve compaction state on ledger close failure - #26665

Merged
nodece merged 2 commits into
apache:masterfrom
Denovo1998:close-compacted-ledger-before-publish
Sep 22, 2026
Merged

nodece merged 2 commits into
apache:masterfrom
Denovo1998:close-compacted-ledger-before-publish

Conversation

@Denovo1998

@Denovo1998 Denovo1998 commented Sep 20, 2026 •

Copy link
Copy Markdown
Contributor

Motivation

StrategicTwoPhaseCompactor acknowledges the compaction subscription with the new ledger ID before closing that ledger. If closing fails after the broker has processed the acknowledgment, the failure cleanup deletes a ledger that the subscription already references.

This follows up on #26576 by fixing the publication order.

Modifications

  • Close the compacted ledger before acknowledging the compaction subscription, matching AbstractTwoPhaseCompactor.
  • If closing fails, skip the acknowledgment, attempt to delete the new ledger, and fail the compaction. The previously published ledger and cursor position remain unchanged.
  • Add a regression test to the existing CompactionConcurrencyTest. It uses a real broker and BookKeeper, persists conflicting ledger metadata to trigger a real close failure, and verifies preservation of the previous compaction state, cleanup of the failed ledger, and successful retry.
  • Use AssertJ future assertions for bounded waits and failure diagnostics, and define the compaction strategy locally in the test.

Verifying this change

  • Make sure that the change passes the CI checks.

(Please pick either of the following options)

This change is a trivial rework / code cleanup without any test coverage.

(or)

This change is already covered by existing tests, such as (please describe tests).

(or)

This change added tests and can be verified as follows:

(example:)

  • Added integration tests for end-to-end deployment with large payloads (10MB)
  • Extended integration test for recovery after broker failure

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

If the box was checked, please highlight the changes

  • 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

@nodece nodece left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified the ordering fix — this checks out from my side:

  • The bug is real: with ack-before-close, a close failure routes into the whenComplete cleanup which deletes a ledger the compaction subscription already references. CompactedTopicImpl's no-such-ledger fallback (lines 79-97) is evidence this failure shape is expected in practice.
  • The new order matches AbstractTwoPhaseCompactor (close at line 212, ack at line 213 there); the deviation dates back to #18195, where the phase-two chain was copied with the two steps inverted.
  • Failure-path enumeration: close failure -> unpublished ledger, delete is safe; ack failure -> closed-but-unpublished, delete is safe; flush failure path unchanged.
  • I ran CompactionConcurrencyTest locally on this branch: 2/2 pass, including the new regression test. The metadata-conflict injection through the real ledger manager is a nicely realistic way to trigger an actual BookKeeper close failure.

Two known trade-offs that predate this change (also present in AbstractTwoPhaseCompactor, so not blockers — just noting): a broker crash between close and ack leaves an orphaned closed ledger with no GC, and an ack that persists on the broker but fails the caller can still trigger deleting a published ledger. Both windows existed before; this change strictly narrows them.

Non-code nit: the PR description still has template residue (unchecked verification options, example bullets).

Review assisted by Claude Code (AI-assisted review), posted by @nodece.

@Denovo1998

Copy link
Copy Markdown
Contributor Author

@nodece Okay, it has already been processed.

@Denovo1998
Denovo1998 requested a review from nodece September 21, 2026 11:08
@nodece
nodece merged commit b642e8e into apache:master Sep 22, 2026
43 checks passed
@Denovo1998
Denovo1998 deleted the close-compacted-ledger-before-publish branch September 22, 2026 06:40
@lhotari lhotari added this to the 5.0.0 milestone Sep 23, 2026
lhotari pushed a commit that referenced this pull request Sep 23, 2026
lhotari pushed a commit that referenced this pull request Sep 23, 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.

3 participants