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] Don't read the metadata store while holding the broker-selection lock in ModularLoadManagerImpl by subodhwandile · Pull Request #27610 · apache/pulsar

Conversation

@subodhwandile

@subodhwandile subodhwandile commented Nov 13, 2026 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Contributor

Fixes #27609

Motivation

ModularLoadManagerImpl#selectBrokerForAssignment holds synchronized (brokerCandidateCache) while it waits on the metadata store: getAvailableBrokers() (up to metadataStoreOperationTimeoutSeconds) and getBundleDataOrDefaultAsync(bundle).join(). It is called synchronously from NamespaceService.selectLeastLoadedBroker on pulsar.getExecutor(). When the cached broker list has been invalidated (a broker joins or leaves, a ZooKeeper session reconnects) and the store is slow, one lookup blocks inside the lock. Every other lookup that needs a broker selection then blocks a pulsar executor thread on the lock, until the executor runs out of threads. Clients see Lookup request timeout for new producers and consumers. This is the same thread dump as #20738; #20189 removed only the deadlock trigger reported there.

Modifications

  • Add ModularLoadManager#selectBrokerForAssignmentAsync and LoadManager#getLeastLoadedAsync. They are default methods that wrap the existing sync methods, so other load managers are unchanged.
  • ModularLoadManagerImpl#selectBrokerForAssignmentAsync fetches the available brokers (same timeout and loadData fallback as getAvailableBrokers()) and the bundle's data. The bundle data is always prefetched, even when the bundle is already in loadData, so selection never has to fall back to a metadata read under the lock if the bundle leaves loadData before the lock is taken. The extra read happens outside the lock and is normally served from the metadata cache. It then runs the unchanged selection logic under the lock on pulsar.getExecutor(), using the prefetched values.
  • The selection always continues on pulsar.getExecutor() (thenCombineAsync), even when both reads are served from cache, so the lock is never taken on a metadata-store callback thread. A bundle that is already preallocated, with no pending affinity, is answered first under a short lock on pulsar.getExecutor() without any metadata read; only otherwise are the broker list and bundle data prefetched, and the selection under the lock re-checks affinity and preallocation. The bundle data is still prefetched for the affinity path, where it may not be needed.
  • The sync selectBrokerForAssignment(ServiceUnitId) and selectBroker(ServiceUnitId) (load shedding) read before taking the lock as well.
  • ModularLoadManagerWrapper#getLeastLoadedAsync delegates to selectBrokerForAssignmentAsync.
  • NamespaceService.selectLeastLoadedBroker chains on getLeastLoadedAsync.
  • Tests that stubbed LoadManager#getLeastLoaded to steer lookups now stub getLeastLoadedAsync.

Not changed here, still possible blocking reads under the lock (follow-ups):

  • LoadManagerShared.applyNamespacePolicies calls SimpleResourceAllocationPolicies.getIsolationPolicies → BaseResources.get → getAsync(path).get(30s). This is normally served from cache, but after a cache invalidation it also blocks under the lock. It is not prefetched here because the result is used for per-broker filtering and changing that path is larger in scope.
  • The same applies to LoadManagerShared.filterAntiAffinityGroupOwnedBrokers when anti-affinity groups are configured, which waits up to 30 s on getAntiAffinityNamespaceOwnedBrokers(...).

Verifying this change

  • This change added tests and can be verified as follows:
    • LookupWithSlowBrokerListTest delays every broker-list read by 5 s while a flag is on, sends 32 lookups for unowned bundles, and asserts that a delayed read happened and that lookups were still pending when the executor was probed. It also checks that the pulsar executor still runs a task within 1 s and that all lookups resolve, and waits for the Deleted notification of the fake broker instead of sleeping. On master it fails ("the executor was blocked for 29021 ms"); with this change it passes (3/3 runs, each 1 test with 0 failures, errors, or skips). Verified on JDK 21 (Corretto 21.0.13) and JDK 25.
    • ModularLoadManagerImplTest passes (28 tests with 0 failures, errors, or skips), including two new tests: testPreallocatedBundleLookupDoesNotWaitForMetadata holds the broker-list and bundle-data reads pending and asserts that an already-preallocated bundle is still returned within 1 s (fails without the preallocation fast path), and testBrokerAffinityTakesPrecedenceOverPreallocationAsync asserts that a pending affinity still wins over an existing preallocation. NamespaceServiceTest (28 tests) and BrokerServiceLookupTest (22 tests, 1 skipped) also pass.

Relation to #27340

#27340 (open) changes the preallocation lookup inside the same synchronized block of selectBrokerForAssignment. It reads preallocatedBundleToBroker once instead of containsKey + get, and changes when preallocations are removed. This PR moves that block unchanged into a private selectBrokerForAssignment(serviceUnit, availableBrokers, bundleData) overload. The two changes are independent, but whichever lands second needs a small textual rebase.

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

  • Dependencies
  • The public API
  • The schema
  • The default values of configurations
  • The threading model: broker selection for a lookup no longer blocks a pulsar executor thread on metadata reads. NamespaceService.selectLeastLoadedBroker now chains on a future, and the selection itself runs on pulsar.getExecutor() once the reads complete.
  • The binary protocol
  • The REST endpoints
  • The admin CLI options
  • The metrics: the selectBrokerForAssignment summary now measures only the selection under the lock; it no longer includes the time spent waiting for the broker-list and bundle-data reads.
  • Anything that affects deployment

(LoadManager/ModularLoadManager are broker-internal plugin interfaces; the new methods are defaults.)

Documentation

  • doc-not-needed

Sorry, something went wrong.

Uh oh!

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

All reactions
@subodhwandile
subodhwandile marked this pull request as draft November 13, 2026 08:12
@subodhwandile
subodhwandile force-pushed the fix/broker-selection-no-metadata-read-under-lock branch from 9f8c3f4 to 96f6949 Compare November 13, 2026 08:23
@subodhwandile subodhwandile mentioned this pull request Nov 13, 2026
[fix][broker] Don't read the metadata store while holding the broker-selection lock in ModularLoadManagerImpl subodhwandile/pulsar#2
Open
@subodhwandile

subodhwandile commented Nov 13, 2026 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Contributor Author

Fork CI passed: https://github.com/subodhwandile/pulsar/actions/runs/43407861526

Could someone please add the ready-to-test label to trigger CI? cc @lhotari

All reactions

Sorry, something went wrong.

Uh oh!

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

@subodhwandile
subodhwandile marked this pull request as ready for review November 13, 2026 10:58
@subodhwandile subodhwandile-toast
[fix][broker] Don't read the metadata store while holding the broker-…
b1fb5b9
…selection lock in ModularLoadManagerImpl

selectBrokerForAssignment holds synchronized(brokerCandidateCache) while
calling getAvailableBrokers() and getBundleDataOrDefaultAsync().join().
When the cached broker list is invalidated and the metadata store is slow,
one lookup blocks inside the lock and every other lookup's broker selection
queues behind it, one pulsar executor thread each. The executor drains and
clients see Lookup request timeout for all new producer/consumer creations.

Add selectBrokerForAssignmentAsync / getLeastLoadedAsync that fetch the
broker list and bundle data before taking the lock, then run the unchanged
selection logic under the lock on pulsar.getExecutor() using the prefetched
values. NamespaceService.selectLeastLoadedBroker now chains on the async
path. The sync selectBrokerForAssignment and selectBroker (load-shedding)
also move their reads before the lock.

Regression test: LookupWithSlowBrokerListTest delays every broker-list read
by 5s after a broker join/leave invalidates the cache, fans out 32 lookups,
and asserts the pulsar executor can still run a task within 1s.

Verified on JDK 21 (Corretto 21.0.13) and JDK 25.
@subodhwandile
subodhwandile force-pushed the fix/broker-selection-no-metadata-read-under-lock branch from e676ed4 to b1fb5b9 Compare November 13, 2026 11:08
codelipenghui
codelipenghui requested changes Nov 13, 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.

The async boundary fixes the demonstrated executor-starvation problem, but introduces a confirmed availability regression: an existing preallocation now waits for unused bundle metadata. Preserve the metadata-free fast path before merging. The committed slow-list test, 26 existing load-manager tests, and eight API-test executions passed; an added regression test fails at this head and passes with the parent selector.

Sorry, something went wrong.

Uh oh!

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

All reactions
Comment thread ...r-broker/src/main/java/org/apache/pulsar/broker/loadbalance/impl/ModularLoadManagerImpl.java Resolved

Uh oh!

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

@subodhwandile subodhwandile-toast
[fix][broker] Return existing preallocations without waiting for meta…
013d4da
…data reads

Return existing bundle placements without waiting for broker-list or bundle-data reads.

Pending affinity still takes precedence and is rechecked under the selection lock.
@subodhwandile
subodhwandile requested a review from codelipenghui November 13, 2026 17:24
codelipenghui
codelipenghui approved these changes Nov 13, 2026
View reviewed changes
@subodhwandile

subodhwandile commented Nov 14, 2026

Copy link
Copy Markdown
Contributor Author

Fork CI passed on the latest head (013d4da): https://github.com/subodhwandile/pulsar/actions/runs/43471013186

All reactions

Sorry, something went wrong.

Uh oh!

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

codelipenghui
codelipenghui approved these changes Nov 14, 2026
View reviewed changes
@subodhwandile
Merge branch 'master' into fix/broker-selection-no-metadata-read-unde…
cc81eca
…r-lock
codelipenghui
codelipenghui approved these changes Nov 14, 2026
View reviewed changes
@codelipenghui
codelipenghui merged commit 75527d8 into apache:master Nov 15, 2026
43 checks passed

Uh oh!

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

lhotari pushed a commit that referenced this pull request Nov 17, 2026
@subodhwandile @lhotari
[fix][broker] Don't read the metadata store while holding the broker-…
e9c75a4
…selection lock in ModularLoadManagerImpl (#27610)

(cherry picked from commit 75527d8)
@lhotari lhotari added release/5.0.2 cherry-picked/branch-5.0 labels Nov 17, 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
@subodhwandile @lhotari
[fix][broker] Don't read the metadata store while holding the broker-…
b5140e0
…selection lock in ModularLoadManagerImpl (apache#27610)

(cherry picked from commit 75527d8)
poorbarcode pushed a commit to poorbarcode/pulsar that referenced this pull request Nov 26, 2026
@subodhwandile @lhotari
[fix][broker] Don't read the metadata store while holding the broker-…
09b2f2d
…selection lock in ModularLoadManagerImpl (apache#27610)

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

Reviewers

@codelipenghui codelipenghui codelipenghui approved these changes

Uh oh!

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

Assignees

No one assigned

Labels

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.

[Bug][broker] One slow metadata-store read stalls all broker selections and blocks the broker executor (ModularLoadManagerImpl holds brokerCandidateCache while reading the metadata store)

Uh oh!

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

3 participants

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