GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — [fix][broker] Cancel queued transaction snapshot recovery on topic close by Demogorgon314 · Pull Request #26335 · apache/pulsar

Conversation

@Demogorgon314

@Demogorgon314 Demogorgon314 commented Aug 15, 2026

Copy link
Copy Markdown
Member

Motivation

Transaction snapshot recovery tasks can remain queued after a topic is closed.
The queued task retains the snapshot processor, topic, and managed ledger,
causing closed topics to accumulate when topic loading is repeatedly retried.

Modifications

  • Cancel queued snapshot recovery tasks when the transaction buffer closes.
  • Wait for running recovery to stop before releasing snapshot resources.
  • Prevent recovery from succeeding after close has started.
  • Stop segmented recovery between I/O operations after close.
  • Handle recovery task submission rejection asynchronously.
  • Add tests for queued, running, completed, retried, and rejected recovery.

Topic close may now wait for an in-flight recovery I/O operation to finish before releasing snapshot resources.

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

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@Demogorgon314 Demogorgon314 self-assigned this Aug 15, 2026
Denovo1998
Denovo1998 requested changes Aug 15, 2026
View reviewed changes
Comment thread ...va/org/apache/pulsar/broker/transaction/buffer/impl/AbstractSnapshotAbortedTxnProcessor.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread ...org/apache/pulsar/broker/transaction/buffer/impl/SnapshotSegmentAbortedTxnProcessorImpl.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread ...org/apache/pulsar/broker/transaction/buffer/impl/SnapshotSegmentAbortedTxnProcessorImpl.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

@Demogorgon314
Demogorgon314 requested review from BewareMyPower, RobertIndie, codelipenghui, coderzc, gaoran10, lhotari, merlimat and poorbarcode August 26, 2026 11:14
lhotari
lhotari requested changes Aug 28, 2026
View reviewed changes

@lhotari lhotari left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Thanks for taking this on — the underlying problem is real, and the shape of the fix (a per-attempt state machine plus a recoveryStoppedFuture barrier) is the right one. The three points from the earlier review look genuinely addressed: the stopped-future barrier does cover synchronous continuations, recoveryIndexUpdateFuture is safely published (written on the recovery thread before stoppedFuture.complete, read only in a dependent of it), and the isClosed() check is now at readSegmentEntries entry ahead of openReadOnlyManagedLedger. The new tests are deterministic — latch-driven, no sleeps, no reflection — and SnapshotSegmentAbortedTxnProcessorCloseTest genuinely pins the fix rather than passing vacuously.

My concern is not the barrier itself but what ended up behind it. Because recoverFromSnapshot() hands back future.copy() and TopicTransactionBufferRecover attaches a non-async thenAccept, the continuation that runs inside future.complete(...) is not a small callback — it is the entire transaction-buffer replay: open a non-durable cursor and read every entry through to LAC. Waiting for that before releasing resources is correct for safety, but it puts the whole replay inside topic close, and the two most common close types gate the managed-ledger close on it. Two consequences follow that I think need addressing before merge (the first two inline comments); a third is a residual gap in the barrier itself.

One unrelated, pre-existing bug I noticed while reading PersistentWorker, mentioned only so it does not stay buried — it is not something I think you should fix here. In the Clear branch, taskQueue.forEach(pair -> pair.getRight().getRight().get().completeExceptionally(...)): pair.getRight().getRight() is the Supplier, so .get() starts every queued WriteSegment/DeleteSegment instead of cancelling it, and then completes the newly started task's future rather than the stored taskExecutedResult (which stays incomplete forever). Compare executeTask(), which correctly uses pair.getRight().getKey() for the result future. The effect is that topic deletion launches the writes it means to cancel and then deletes the segments concurrently. Worth a separate issue/PR.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread ...va/org/apache/pulsar/broker/transaction/buffer/impl/AbstractSnapshotAbortedTxnProcessor.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread ...va/org/apache/pulsar/broker/transaction/buffer/impl/AbstractSnapshotAbortedTxnProcessor.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread ...va/org/apache/pulsar/broker/transaction/buffer/impl/AbstractSnapshotAbortedTxnProcessor.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread ...va/org/apache/pulsar/broker/transaction/buffer/impl/AbstractSnapshotAbortedTxnProcessor.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread ...va/org/apache/pulsar/broker/transaction/buffer/impl/AbstractSnapshotAbortedTxnProcessor.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread ...org/apache/pulsar/broker/transaction/buffer/impl/SnapshotSegmentAbortedTxnProcessorImpl.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

@Demogorgon314
Demogorgon314 requested a review from lhotari September 2, 2026 16:43
Demogorgon314 added 10 commits September 7, 2026 10:57
@Demogorgon314
[fix][broker] Cancel queued transaction snapshot recovery on topic close
3f02c57
Cancel queued snapshot recovery tasks when the transaction buffer closes and wait for running recovery before releasing snapshot resources.

This prevents the recovery executor queue from retaining closed topics and managed ledgers.
@Demogorgon314
Replace ScheduledExecutorService with ListeningScheduledExecutorServi…
b1c9a23
…ce in TransactionTest
@Demogorgon314
Ensure recovery completion is awaited during close operation
7b519ac
@Demogorgon314
Prevent reading segment entries if the topic is closed
0f9e489
@Demogorgon314
Ensure recovery index update is awaited during resource closure
4120c6e
@Demogorgon314
Refactor recovery handling to ensure closure waits for processor-owne…
7b6e2aa
…d recovery work to finish
@Demogorgon314
Cancel queued recovery replay on topic close
effa191
@Demogorgon314
[fix][broker] Isolate transaction buffer replay and preserve recovery…
f107954
… after failed deletion

Run blocking replay on the snapshot recovery executor and cancel recovery only when the buffer is closed. Gate cursor setup before starting recovery and cover transaction executor responsiveness and failed topic deletion.
@Demogorgon314
[fix][broker] Update recovery state management to prevent queued reco…
0c1f970
…very on topic close
@Demogorgon314
[improve][test] Simplify transaction snapshot recovery lifecycle tests
29d2141
Remove same-instance recovery retry coverage and consolidate post-recovery close assertions. Test late recovery completion with a standard future instead of intercepting continuation registration.

Assisted-by: Codex
@Demogorgon314
Demogorgon314 force-pushed the Demogorgon314/Cancel-queued-transaction-snapshot-recovery-on-topic-close branch from 7259653 to 29d2141 Compare September 7, 2026 03:09
lhotari
lhotari requested changes Sep 7, 2026
View reviewed changes

@lhotari lhotari left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

All six points from my last review are addressed, and the way they are addressed is better than what I asked for. Rather than widening the close barrier you narrowed it to processor-owned work and made the replay itself a separately cancellable task that observes close.

One new problem, and it is the only thing I would hold on. Taking the transaction buffer's monitor in closeAsync() introduces a lock-ordering inversion against a monitor the buffer already acquires in the other direction, so a replay racing a fenced publish can deadlock two shared broker threads. Detail in the inline comment.

Two smaller notes, neither blocking:

  • PersistentWorker is unchanged, so the residual from my earlier comment is still open: putAbortedTxnAndPosition from the replay can enqueue a WriteSegment right up to the moment close takes the buffer monitor, and closeResources() then releases the writers without draining the queue. Pre-existing, still not something I am asking you to widen the scope for — just recording that the new design does not close it either.
  • recoverFromSnapshot() and closeAsync() are the state machine, but neither is final (AbstractSnapshotAbortedTxnProcessor.java:57 and :143). Marking them final would stop a subclass from half-overriding the protocol; isClosed() already is.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread ...r/src/main/java/org/apache/pulsar/broker/transaction/buffer/impl/TopicTransactionBuffer.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread ...va/org/apache/pulsar/broker/transaction/buffer/impl/AbstractSnapshotAbortedTxnProcessor.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

@Demogorgon314
address comments
39485b7
lhotari
lhotari requested changes Sep 7, 2026
View reviewed changes

@lhotari lhotari left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

The lock-order fix landed in the shape I suggested and it holds up on inspection: removeTxnAndUpdateMaxReadPosition no longer touches the topic, all three of its callers invalidate lastDispatchablePosition after leaving the buffer monitor, and testTxnCompletionUpdatesTopicOutsideBufferLock pins that with a Thread.holdsLock assertion across all four replay/commit/abort combinations. The final nit is done as well, and moving the abort path's complete(null) out of the monitor is a bonus improvement.

One edge of the same cycle is still open, and it is one I missed last round rather than anything new here: recoverComplete() and noNeedToRecover() complete transactionBufferFuture while holding the buffer monitor, checkIfTBRecoverCompletely() hands that same future to handleGetLastMessageId, and that continuation can take the topic monitor inline. Detail and a suggested fix are on TopicTransactionBuffer.java:159 — it is the discipline closeAsync() already applies to itself.

Nothing else in the delta needs attention; it is exactly the requested edits plus the test.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread ...r/src/main/java/org/apache/pulsar/broker/transaction/buffer/impl/TopicTransactionBuffer.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

Demogorgon314 added 2 commits September 7, 2026 18:18
@Demogorgon314
[fix][broker] Ensure recovery completion notifications are handled ou…
4c36756
…tside buffer lock
@Demogorgon314
[fix][broker] Move low-water-mark handling outside buffer monitor for…
a839501
… transaction aborts
lhotari
lhotari approved these changes Sep 7, 2026
View reviewed changes

@lhotari lhotari left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Both open points are closed, and I checked the code rather than taking the reply for it.

recoverComplete() now decides the state transition under the monitor and completes transactionBufferFuture after leaving it, on the failure branch as well as the success one:

pulsar/pulsar-broker/src/main/java/org/apache/pulsar/broker/transaction/buffer/impl/TopicTransactionBuffer.java

Lines 179 to 183 in a839501

if (recoveryFailure != null) {
getTransactionBufferFuture().completeExceptionally(recoveryFailure);
} else {
getTransactionBufferFuture().complete(null);
}

noNeedToRecover() does the same at TopicTransactionBuffer.java:188-199. The other two completions were already outside a monitor — recoverExceptionally at TopicTransactionBuffer.java:241-244 and closeAsync() at TopicTransactionBuffer.java:756 — and nothing outside this class completes that future, so no path is left that completes it while the buffer monitor is held. Putting the rule on the field itself at TopicTransactionBuffer.java:97-98 is the right place for it.

testRecoveryNotifiesLastPositionQueryOutsideBufferLock pins it rather than leaving it to argument, and both data-provider values earn their place: null drives noNeedToRecover(), a position drives recoverComplete().

One non-blocking flag on the second commit, detail inline: moving handleLowWaterMark past completableFuture.complete(null) is not only a lock move — it changes how far a single low-water-mark trigger drains the backlog. It looks right and it does not reopen the cycle, but nothing pins it and the commit path was left as it was.

The PersistentWorker residual on the segment processor is unchanged and still deliberately out of scope, exactly as I said in my last review.

Otherwise this looks good to me.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread ...r/src/main/java/org/apache/pulsar/broker/transaction/buffer/impl/TopicTransactionBuffer.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

Denovo1998
Denovo1998 approved these changes Sep 8, 2026
View reviewed changes
@Demogorgon314
Demogorgon314 merged commit d2bc207 into apache:master Sep 9, 2026
43 checks passed

Uh oh!

There was an error while loading. Please reload this page.

@lhotari lhotari added release/4.2.5 release/4.0.14 labels Sep 9, 2026
lhotari pushed a commit that referenced this pull request Sep 9, 2026
@Demogorgon314 @lhotari
[fix][broker] Cancel queued transaction snapshot recovery on topic cl…
4d59e8d
…ose (#26335)

(cherry picked from commit d2bc207)
@lhotari lhotari added the cherry-picked/branch-4.2 label Sep 9, 2026
lhotari pushed a commit that referenced this pull request Sep 9, 2026
@Demogorgon314 @lhotari
[fix][broker] Cancel queued transaction snapshot recovery on topic cl…
fb093d2
…ose (#26335)

(cherry picked from commit d2bc207)
lhotari added a commit that referenced this pull request Sep 9, 2026
@lhotari
Revert "[fix][broker] Cancel queued transaction snapshot recovery on …
862ed3f
…topic close (#26335)"

This reverts commit 4d59e8d.
lhotari pushed a commit that referenced this pull request Sep 9, 2026
@Demogorgon314 @lhotari
[fix][broker] Cancel queued transaction snapshot recovery on topic cl…
001c89c
…ose (#26335)

(cherry picked from commit d2bc207)
@lhotari lhotari added the cherry-picked/branch-4.0 label Sep 9, 2026
@lhotari lhotari mentioned this pull request Sep 11, 2026
[fix][txn] Stop the pending ack replay loop from spinning forever #26369
Merged
9 of 10 tasks
lhotari added a commit that referenced this pull request Sep 12, 2026
@lhotari
[fix][test][branch-4.0] Correct transaction replay marker fixture and…
a4a0726
… release assertion

Fix the replay fixture introduced by
fb093d2
([fix][broker] Cancel queued transaction snapshot recovery on topic
close, #26335), cherry-picked from
d2bc207.

Upstream and branch-4.2 mock Entry.getMessageMetadata(), so the fixture
needs neither serialized metadata nor a real reference-counted entry.
Branch-4.0 lacks that API and parses metadata from the entry buffer.
The backport therefore replaced the mock with a serialized EntryImpl,
but omitted required producerName, sequenceId and publishTime fields
and retained Mockito.verify(marker).release() on the real entry.
It also retained the buffer's original reference after EntryImpl.create.

Build valid commit/abort markers with the production marker helpers,
release the original buffer reference, and assert that recovery releases
the remaining reference instead of verifying a non-mock object.

Validation: all 8 TopicTransactionBufferCloseTest cases pass;
Checkstyle passes.
@lhotari lhotari added this to the 5.0.0-M2 milestone Sep 12, 2026
Radiancebobo pushed a commit to Radiancebobo/pulsar that referenced this pull request Oct 8, 2026
@Demogorgon314 hechengbo
[fix][broker] Cancel queued transaction snapshot recovery on topic cl…
d767632
…ose (apache#26335)
This was referenced Oct 22, 2026
[fix][txn] Fix SnapshotSegmentAbortedTxnProcessorImpl thread safety issue #22384
Closed
[Bug] SnapshotSegmentAbortedTxnProcessorImpl contains thread safety issues #22116
Open
nodece pushed a commit to ascentstream/pulsar that referenced this pull request Nov 6, 2026
@Demogorgon314 @nodece
[fix][broker] Cancel queued transaction snapshot recovery on topic cl…
e7c0e63
…ose (apache#26335)

(cherry picked from commit d2bc207)
nodece added a commit to ascentstream/pulsar that referenced this pull request Nov 6, 2026
@nodece
fix: adapt apache#26335 TopicTransactionBufferCloseTest to fork (requ…
cdbe4e8
…ired metadata fields, real EntryImpl instead of mock verify, snapshot stub)
nodece pushed a commit to ascentstream/pulsar that referenced this pull request Nov 16, 2026
@Demogorgon314 @nodece
[fix][broker] Cancel queued transaction snapshot recovery on topic cl…
b01d8b7
…ose (apache#26335)

(cherry picked from commit d2bc207)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@lhotari lhotari lhotari approved these changes
@Denovo1998 Denovo1998 Denovo1998 approved these changes
@merlimat merlimat Awaiting requested review from merlimat
@codelipenghui codelipenghui Awaiting requested review from codelipenghui
@gaoran10 gaoran10 Awaiting requested review from gaoran10
@RobertIndie RobertIndie Awaiting requested review from RobertIndie
@BewareMyPower BewareMyPower Awaiting requested review from BewareMyPower
@poorbarcode poorbarcode Awaiting requested review from poorbarcode
@coderzc coderzc Awaiting requested review from coderzc

Uh oh!

There was an error while loading. Please reload this page.

Assignees

@Demogorgon314 Demogorgon314

Labels

cherry-picked/branch-4.0 cherry-picked/branch-4.2 release/4.0.14 release/4.2.5

Projects

None yet

Milestone

5.0.0-M2

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

3 participants

@Demogorgon314 @lhotari @Denovo1998
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.