[fix] functions: Run worker leader-election off the consumer event-listener thread#26059
Open
merlimat wants to merge 1 commit into
Open
Conversation
…stener thread LeaderService implements ConsumerEventListener for the failover-subscription leader election. Its becameActive/becameInactive callbacks ran the full blocking leader-election routine inline: functionMetaDataManager/functionRuntimeManager getIsInitialized().get(), schedulerManager/functionMetaDataManager acquireExclusiveWrite (a retry loop doing createAsync().get(10s) + Thread.sleep while still leader), functionAssignmentTailer.triggerReadToTheEndAndExit().get(), and on becameInactive schedulerManager.close() (which blocks on schedulerLock for an in-flight schedule). These callbacks are dispatched by the Pulsar client on its shared consumer-listener executor (ConsumerImpl.activeConsumerChanged -> externalPinnedExecutor), so this potentially-unbounded leader handshake blocked a thread shared by all consumers/readers on that client, including the worker's own tailers. Run becameActive/becameInactive on a dedicated single-threaded executor and return from the listener callbacks immediately. The single thread preserves the becameActive -> becameInactive ordering. The callback bodies are unchanged, moved into becameActiveInternal/becameInactiveInternal. close() shuts the executor down. A @VisibleForTesting joinPendingEventTasks() barrier keeps LeaderServiceTest deterministic.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Motivation
LeaderServiceimplementsConsumerEventListenerfor the failover-subscription leader election. ItsbecameActive/becameInactivecallbacks ran the full blocking leader-election routine inline:functionMetaDataManager/functionRuntimeManagergetIsInitialized().get()(unbounded),schedulerManager/functionMetaDataManageracquireExclusiveWrite(...)— a retry loop doingcreateAsync().get(10s)+Thread.sleepwhile still leader,functionAssignmentTailer.triggerReadToTheEndAndExit().get(),becameInactive,schedulerManager.close()(which blocks onschedulerLockfor an in-flight schedule).These callbacks are dispatched by the Pulsar client on its shared consumer-listener executor (
ConsumerImpl.activeConsumerChanged→externalPinnedExecutor), so this potentially-unbounded leader handshake blocked a thread shared by all consumers/readers on that client — including the worker's own tailers.Modifications
Run
becameActive/becameInactiveon a dedicated single-threaded executor and return from the listener callbacks immediately. The single thread preserves thebecameActive→becameInactiveordering. The callback bodies are unchanged, moved intobecameActiveInternal/becameInactiveInternal.close()shuts the executor down. A@VisibleForTesting joinPendingEventTasks()barrier keepsLeaderServiceTestdeterministic.Verifying this change
Covered by the existing
LeaderServiceTest(updated to await the offloaded tasks via the barrier) — 4/4 pass.Does this pull request potentially affect one of the following parts:
If the box was checked, please highlight the changes