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
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.
#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
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
…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.
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.
Uh oh!
There was an error while loading. Please reload this page.
…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.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
This was referenced Nov 21, 2026
Uh oh!
There was an error while loading. Please reload this page.
Assignees
No one assigned
Milestone
No milestone
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.
|