Skip to content

[fix][broker] Don't continue loading a topic after its load has timed out - #20540

Merged
merlimat merged 1 commit into
apache:masterfrom
lhotari:lh-fix-topic-unloading-loading-raceconditions
Oct 22, 2026
Merged

merlimat merged 1 commit into
apache:masterfrom
lhotari:lh-fix-topic-unloading-loading-raceconditions

Conversation

@lhotari

@lhotari lhotari commented Jun 8, 2023 •

Copy link
Copy Markdown
Member

Motivation

A persistent topic load that times out (topicLoadTimeoutSeconds) keeps going in the background. The timed-out
future intentionally stays in the topics cache until the pending load actions finish, so that a new load can't
open the same managed ledger concurrently. But the load then still opens, and so recovers, the managed ledger,
only to close it again because the future was already completed. Until that is done, every lookup of the topic
gets the timed-out failure ("Failed to load topic within timeout" / "Topic is temporarily unavailable").

The same happens to the loads waiting in the pending topic load queue, which is used when
maxConcurrentTopicLoadRequest is reached: loads that timed out while queued are still started, one after
another, each opening and closing its managed ledger. A backed up queue makes this worse exactly when the broker
is already overloaded.

At shutdown, the graceful bundle unload waits for every topic future cached for the bundle, including the ones
of queued loads. Those only complete when the topic load times out, so the shutdown waits for that timeout (or
the bundle unloading timeout).

This PR was originally opened in 2023 with a broader set of changes. Since then, master has gained most of them
in other forms: closing a topic whose future was completed before its load finished, removing a cache entry only
when it matches the exact create future (getCreateFuture(), snapshot-based cleanUnloadedTopicFromCache),
re-checking the ownership right before opening the managed ledger, and TopicLoadingContext timeout tracing.
The PR has been rewritten on top of current master to keep only what is still missing.

Modifications

  • createPersistentTopic0: if the topic future is already completed right before opening the managed ledger,
    skip opening it and remove the timed-out future from the cache, so that a new load can start.
  • createPendingLoadTopic: drop queued loads whose topic future is already completed, removing their cache
    entries, instead of starting them.
  • Fail queued loads with ServiceUnitNotReadyException("Broker is shutting down") when the graceful bundle
    unload of a shutdown starts (unloadNamespaceBundlesGracefully, which is only used on shutdown), and also
    loads that are queued after that. Clients handle ServiceNotReady by looking the topic up again.

Verifying this change

  • Make sure that the change passes the CI checks.

This change added tests to BrokerServiceTest and can be verified as follows:

  • testTimedOutTopicLoadDoesNotOpenTheManagedLedger: a load that times out before the managed ledger is opened
    doesn't call asyncOpen and its future is removed from the cache.
  • testPendingTopicLoadThatTimedOutIsNotStarted: a queued load that timed out is not started when the queue is
    processed, and its future is removed from the cache.
  • testPendingTopicLoadsFailWhenTheBrokerShutsDown: the graceful bundle unload fails a queued load and doesn't
    wait for its bundle unloading timeout.

Each test fails when the corresponding change is reverted.

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

Documentation

  • doc
  • doc-required
  • doc-not-needed
  • doc-complete

@poorbarcode

Copy link
Copy Markdown
Contributor

@lhotari A quick update, I think these are helpful for these issues

@lhotari

lhotari commented Jul 5, 2023

Copy link
Copy Markdown
Member Author

@lhotari A quick update, I think these are helpful for these issues

@poorbarcode the PRs you referenced don't seem to make this current PR #20540 obsolete. Please review

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

Nice work @lhotari. I left a few comments.

@codecov-commenter

codecov-commenter commented Jul 16, 2023 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 35.44304% with 51 lines in your changes missing coverage. Please review.
✅ Project coverage is 36.36%. Comparing base (99d06b9) to head (744581f).
⚠️ Report is 1894 commits behind head on master.

Files with missing lines Patch % Lines
...rg/apache/pulsar/broker/service/BrokerService.java 31.88% 37 Missing and 10 partials ⚠️
...rg/apache/pulsar/broker/service/AbstractTopic.java 66.66% 1 Missing and 2 partials ⚠️
...oker/service/nonpersistent/NonPersistentTopic.java 0.00% 1 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@              Coverage Diff              @@
##             master   #20540       +/-   ##
=============================================
- Coverage     73.58%   36.36%   -37.23%     
+ Complexity    32254    12289    -19965     
=============================================
  Files          1858     1722      -136     
  Lines        138021   131190     -6831     
  Branches      15111    14338      -773     
=============================================
- Hits         101558    47702    -53856     
- Misses        28606    77113    +48507     
+ Partials       7857     6375     -1482     
Flag Coverage Δ
inttests 24.17% <29.11%> (-0.02%) ⬇️
systests 23.64% <24.05%> (-0.14%) ⬇️
unittests 31.88% <31.64%> (-41.00%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
...n/java/org/apache/pulsar/broker/service/Topic.java 9.09% <ø> (-27.28%) ⬇️
...sar/broker/service/persistent/PersistentTopic.java 51.78% <ø> (-26.90%) ⬇️
...oker/service/nonpersistent/NonPersistentTopic.java 46.14% <0.00%> (-23.78%) ⬇️
...rg/apache/pulsar/broker/service/AbstractTopic.java 62.53% <66.66%> (-25.36%) ⬇️
...rg/apache/pulsar/broker/service/BrokerService.java 56.20% <31.88%> (-24.84%) ⬇️

... and 1430 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@poorbarcode

poorbarcode commented Jul 21, 2023 •

Copy link
Copy Markdown
Contributor

the PRs you referenced don't seem to make this current PR #20540 obsolete.

Yes

… out

A topic load that timed out kept going: it opened, and so recovered, the
managed ledger only to close it again, and the timed-out future stayed in
the topic cache until then, so the topic could not be loaded meanwhile.
The pending topic load queue, used when maxConcurrentTopicLoadRequest is
reached, likewise started loads that had already timed out while queued.
At shutdown, unloading a bundle waited for the futures of the queued
loads, which only completed when the topic load timed out.

- Skip opening the managed ledger when the topic future is already
  completed, and remove the timed-out future from the cache.
- Drop queued loads whose topic future is already completed.
- Fail queued loads, and loads queued later, with
  ServiceUnitNotReadyException when the graceful bundle unload of a
  shutdown starts.

Assisted-by: Claude Code (claude-opus-5-5)
@lhotari
lhotari force-pushed the lh-fix-topic-unloading-loading-raceconditions branch from 744581f to 766863a Compare October 20, 2026 14:38
@lhotari lhotari changed the title [fix][broker] Fix multiple race conditions in topic unloading and loading [fix][broker] Don't continue loading a topic after its load has timed out Oct 20, 2026
@lhotari
lhotari requested review from dao-jun and nodece October 20, 2026 14:48

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

LGTM.

This seems to also cover the expired queued loading scenario discussed in #26729, while maintaining the existing cleanup order for loading that has already opened its ledger.

There may be a separate follow-up around the queue wake-up: after tryAcquire() fails, the last active load can release its permit and drain an empty queue before the new request is enqueued. With no later load completion, that request stays queued, and retries keep seeing its cached timeout. This predates the changes here, so I wouldn't block this PR on it.

@merlimat merlimat added this to the 5.1.0 milestone Oct 22, 2026
@merlimat
merlimat merged commit aec5da3 into apache:master Oct 22, 2026
82 of 84 checks passed
lhotari added a commit that referenced this pull request Oct 22, 2026
poorbarcode pushed a commit to poorbarcode/pulsar that referenced this pull request Nov 26, 2026
poorbarcode pushed a commit to poorbarcode/pulsar that referenced this pull request Nov 26, 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.