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][test] Fix flaky TransactionBufferClientTest.testTransactionBufferLookUp by merlimat · Pull Request #27349 · apache/pulsar

Conversation

@merlimat

@merlimat merlimat commented Nov 4, 2026

Copy link
Copy Markdown
Contributor

Motivation

Problem. testTransactionBufferLookUp fails its first attempt in 18 of the 30 scheduled CI runs of Broker Group 1
covered by a scan of 2026-10-15 .. 11-03 (the scan goes no further back) and passes on the TestNG retry, so the
failure shows nowhere. The first attempts recorded in failed jobs' reports failed in
admin.topics().createSubscription(topic, "test", MessageId.earliest):

PulsarAdminException$PreconditionFailedException: Unable to find position for position specified: unable to persist readPosition for cursor reset 48:-1

or in the tbClient calls:

PulsarClientException$ServiceNotReadyException: Handle end txn on subscription failed: org.apache.bookkeeper.mledger.ManagedLedgerException: ManagedLedger public/test/persistent/testTransactionBufferLookUp_abort_sub has already been closed
PulsarClientException$ServiceNotReadyException: Handle end txn on subscription failed: PendingAckHandle have been closed!

The broker closes the test's topic while the test uses it, because two earlier tests of the class leave state behind:

  1. testTransactionBufferClientTimeout, testTransactionBufferChannelUnActive and
    testTransactionBufferConnectionClosedBeforeHandshake stub the first broker's getClient() to return a mocked
    PulsarClientImpl, so that the TransactionBufferHandlerImpl they create picks it up. Nothing removes the stub, so
    that broker returns the mock for the rest of the class:

    PulsarService pulsarService = pulsarServiceList.get(0);
    ...
    when(pulsarService.getClient()).thenAnswer(new Answer<PulsarClient>(){
        @Override
        public PulsarClient answer(InvocationOnMock invocation) throws Throwable {
            return mockClient;
        }
    });
  2. testRecoveryTransactionBufferWhenCommonTopicAndSystemTopicAtDifferentBroker turns on segmented transaction
    buffer snapshots and never turns them off. The three brokers share one ServiceConfiguration
    (TransactionTestBase.conf), which they reuse when they restart, so the setting stays on for the rest of the class.

With segmented snapshots, a topic recovers its transaction buffer with SnapshotSegmentAbortedTxnProcessorImpl, which
reads the broker's client for its operation timeout when the topic loads:

private long getSystemClientOperationTimeoutMs() throws Exception {
    PulsarClientImpl pulsarClient = (PulsarClientImpl) topic.getBrokerService().getPulsar().getClient();
    return pulsarClient.getConfiguration().getOperationTimeoutMs();
}

On the first broker, the mock's getConfiguration() returns null, the recovery fails with a NullPointerException,
and TopicTransactionBuffer closes the topic, as it does after any failed recovery:

ERROR TopicTransactionBuffer - Transaction buffer failed to recover snapshot {state=Initializing, topic=persistent://public/test/testTransactionBufferLookUp_commit_sub}
java.lang.NullPointerException: Cannot invoke "org.apache.pulsar.client.impl.conf.ClientConfigurationData.getOperationTimeoutMs()" because the return value of "org.apache.pulsar.client.impl.PulsarClientImpl.getConfiguration()" is null
	at org.apache.pulsar.broker.transaction.buffer.impl.SnapshotSegmentAbortedTxnProcessorImpl.getSystemClientOperationTimeoutMs(SnapshotSegmentAbortedTxnProcessorImpl.java:445)
WARN  TopicTransactionBuffer - Closing topic due to read transaction buffer snapshot while recovering the transaction buffer throw exception

The next request loads the topic again, and the recovery fails again a few milliseconds later. TestNG runs the
methods in alphabetical order, so testTransactionBufferLookUp comes right after the three handler tests. It fails
whenever one of its two topics is placed on the first broker, which happens in about 5 runs in 9 if each lands on a
random one of the three brokers. The failing call is the one the close interrupts. The retry passes because
TestRetrySupport restarts the brokers after a failed attempt, which drops the stub.

Example. The first attempt in the scheduled run 41443581285 (2 Nov, Broker Group 1). The admin client talks to
the first broker, which owned testTransactionBufferLookUp_commit_sub:

time event
12:49:02.603 the topic is loaded; createNonPartitionedTopic returns
02.617 createSubscription(..., earliest) arrives
02.620 the transaction buffer recovery fails with the NPE; the topic is closed at 02.624
02.636 createSubscription loads the topic again (loaded at 02.681)
02.683 the recovery fails again, and the topic starts closing
02.688 the cursor reset finds the cursor Closing: "unable to persist readPosition for cursor reset 48:-1", HTTP 412

The cursor reset is fine: it fails because the topic is being closed under it. The broker is right to close a topic
whose transaction buffer can't recover, so that the next load tries again. It can't recover here only because the test
gave the broker a mocked client.

Modifications

Change. The tests leave nothing behind for the later tests:

  • The three handler tests give TransactionBufferHandlerImpl a mocked PulsarService that returns the mocked client
    and the first broker's NamespaceService (the only two things the handler takes from it), instead of stubbing the
    broker's own getClient(). TransactionBufferHandlerImplTest builds its handlers the same way.

    private PulsarService mockPulsarService(PulsarClientImpl client) throws PulsarServerException {
        NamespaceService namespaceService = pulsarServiceList.get(0).getNamespaceService();
        PulsarService pulsarService = mock(PulsarService.class);
        when(pulsarService.getNamespaceService()).thenReturn(namespaceService);
        when(pulsarService.getClient()).thenReturn(client);
        return pulsarService;
    }

    This also stops stubbing the spy of a running broker, which can race with the broker's own threads calling it.

  • testRecoveryTransactionBufferWhenCommonTopicAndSystemTopicAtDifferentBroker turns segmented snapshots off again
    when it ends, through an @Cleanup AutoCloseable, so also when it fails:

    @Cleanup
    AutoCloseable restoreSnapshotConfig = () -> {
        for (int i = 0; i < getPulsarServiceList().size(); i++) {
            getPulsarServiceList().get(i).getConfig().setTransactionBufferSegmentedSnapshotEnabled(false);
        }
    };

Either change alone removes the failure (see below). Both are kept so that no test of the class depends on the
order of the others.

Verifying this change

  • Make sure that the change passes the CI checks.

This change is already covered by existing tests: it changes TransactionBufferClientTest itself.

Each run below is a fresh ./gradlew :pulsar-broker:test --tests '<filter>' --rerun -PtestFailFast=false -PtestRetryCount=0 on an Apple M5 Pro.

run before after
the whole class (15 tests) 7 of 10 failed, only testTransactionBufferLookUp, with the CI messages 30 of 30 passed
a scratch test that loads a topic on the first broker after testRecovery... and testTransactionBufferClientTimeout (not committed) 5 of 5 failed 5 of 5 passed
the same, with only the first change 5 of 5 passed
the same, with only the second change 5 of 5 passed

./gradlew quickCheck passes.

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
@merlimat
[fix][test] Fix flaky TransactionBufferClientTest.testTransactionBuff…
9b186d6
…erLookUp

testTransactionBufferLookUp failed its first attempt in 18 of 30 scheduled CI
runs and passed on the retry, because two earlier tests of the class left state
behind:

- testTransactionBufferClientTimeout, testTransactionBufferChannelUnActive and
  testTransactionBufferConnectionClosedBeforeHandshake stubbed the first
  broker's getClient() to return a mocked PulsarClientImpl and never removed
  the stub.
- testRecoveryTransactionBufferWhenCommonTopicAndSystemTopicAtDifferentBroker
  turned on segmented transaction buffer snapshots in the configuration that
  the three brokers share, which outlives their restarts, and never turned
  them off.

With segmented snapshots, a topic's transaction buffer recovery reads the
broker's client for its operation timeout. On the first broker that is the
mock, so the recovery failed with a NullPointerException, and the broker
closed the topic while the test was creating its subscription or ending a
transaction on it.

The handler tests now give TransactionBufferHandlerImpl a mocked
PulsarService instead of stubbing the broker's, and the recovery test turns
segmented snapshots off again when it ends.
@merlimat merlimat added the area/test label Nov 4, 2026
codelipenghui
codelipenghui approved these changes Nov 4, 2026
View reviewed changes

@codelipenghui codelipenghui left a comment

Copy link
Copy Markdown
Contributor

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.

Code Review approval for commit 9b186d6.

Summary:

No actionable findings identified within the reviewed scope. Both fixture-isolation fixes are sound and leave production behavior unchanged. All 15 tests passed with retries disabled; temporarily reinstating both leaks reproduced the reported recovery NPE and topic-closure failure. All temporary edits were restored.

Sorry, something went wrong.

Uh oh!

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

All reactions
lhotari
lhotari approved these changes Nov 4, 2026
View reviewed changes
@lhotari
lhotari merged commit fd603ba into apache:master Nov 4, 2026
43 checks passed

Uh oh!

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

@merlimat merlimat mentioned this pull request Nov 4, 2026
[fix][txn] Give the transaction buffer request credit back before completing the request #27355
Merged
11 tasks
lhotari pushed a commit that referenced this pull request Nov 6, 2026
@merlimat @lhotari
[fix][test] Fix flaky TransactionBufferClientTest.testTransactionBuff…
bfd60fd
…erLookUp (#27349)

(cherry picked from commit fd603ba)
@lhotari lhotari added release/5.0.2 cherry-picked/branch-5.0 labels Nov 6, 2026
This was referenced Nov 21, 2026
[improve][ci] Run CI for the 4.2.6 backports to branch-4.2 (do not merge) #27680
Closed
[improve][ci] Run CI for the 4.2.6 backports to branch-4.2 (do not merge) #27681
Closed
[improve][ci] Run CI for the 4.2.6 backports to branch-4.2 (do not merge) #27684
Closed
@lhotari lhotari added cherry-picked/branch-4.2 release/4.2.6 cherry-picked/branch-4.0 release/4.0.15 labels Nov 21, 2026
poorbarcode pushed a commit to poorbarcode/pulsar that referenced this pull request Nov 26, 2026
@merlimat @lhotari
[fix][test] Fix flaky TransactionBufferClientTest.testTransactionBuff…
1c9eea6
…erLookUp (apache#27349)

(cherry picked from commit fd603ba)
poorbarcode pushed a commit to poorbarcode/pulsar that referenced this pull request Nov 26, 2026
@merlimat @lhotari
[fix][test] Fix flaky TransactionBufferClientTest.testTransactionBuff…
fae6106
…erLookUp (apache#27349)

(cherry picked from commit fd603ba)
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
@codelipenghui codelipenghui codelipenghui approved these changes

Uh oh!

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

Assignees

No one assigned

Labels

area/test cherry-picked/branch-4.0 cherry-picked/branch-4.2 cherry-picked/branch-5.0 release/4.0.15 release/4.2.6 release/5.0.2

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

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

3 participants

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