Repository navigation
Overlord becomeLeader() blocks for 3 × druid.indexer.runner.syncRequestTimeout and fails leadership acquisition if a MiddleManager's discovery entry flaps during startup worker sync #20339
Description
Affected Version
Observed on 36.x; the relevant code paths are unchanged on master (as of commit 2bf1643cdd). Uses the HTTP-based task runner (druid.indexer.runner.type=httpRemote).
Summary
If a MiddleManager's node-discovery entry flaps (removed then re-added) during the Overlord's startup worker-sync, the Overlord's becomeLeader() blocks for the full 3 × druid.indexer.runner.syncRequestTimeout (default 3 × PT3M = 9 minutes) awaiting a stale WorkerHolder whose initialization latch can never fire, then throws and fails leadership acquisition. During that window the Overlord is effectively leaderless (no supervisor management), and the failure triggers a further leadership transition.
Description
Cluster / config
httpRemotetask runner; combined Coordinator/Overlord process.- ~12 MiddleManagers, ~40 Kafka streaming supervisors.
druid.indexer.runner.syncRequestTimeoutleft at its defaultPT3M.- ZooKeeper-based discovery (
CuratorDruidNodeDiscoveryProvider).
Trigger
The ZooKeeper ensemble leader restarted, causing a brief re-election and Curator SUSPENDED → RECONNECTED on the Druid processes. This forced an Overlord leadership change. As the new leader ran becomeLeader(), one MiddleManager's ZK session had also reset, so its ephemeral discovery node was deleted and immediately re-created — a remove+add flap occurring within ~1s of the Overlord starting its worker sync.
Observed behavior (timestamps relative)
T+0.000 New leader: HttpRemoteTaskRunner - "[12] Workers are discovered."
T+0.000 ChangeRequestHttpSyncer - "Starting sync for server[...MM-X...]" (x12)
T+0.000 HttpRemoteTaskRunner - "Waiting for worker[MM-0] to sync state..."
T+1.017 HttpRemoteTaskRunner - "Kaboom! Worker[MM-X] removed!" # old ZK node gone
T+1.350 HttpRemoteTaskRunner - "Worker[MM-X] reportin' for duty!" # new ZK node -> new holder, syncs fine
T+1.359 HttpRemoteTaskRunner - "Task[...] location changed on worker[MM-X]" # new holder healthy
...
T+555.4 ERROR CuratorDruidLeaderSelector - "listener becomeLeader() failed. Unable to become leader"
Stack trace (host scrubbed):
java.lang.RuntimeException: java.lang.RuntimeException: org.apache.druid.java.util.common.RE:
Failed to sync with worker[<middlemanager-host>:8088].
at org.apache.druid.indexing.overlord.DruidOverlord$1.becomeLeader(DruidOverlord.java)
...
The block lasted ~555s ≈ 3 × PT3M (540s) plus the time to await the earlier, healthy workers. The re-added MiddleManager was healthy the entire time (its new WorkerHolder synced within ~1s and streamed task snapshots); only the stale, stopped holder the startup loop was awaiting never initialized.
Root cause
Leadership callbacks run on a single-threaded executor and becomeLeader() is invoked synchronously, so the whole node cannot process any further leadership transition until it returns:
CuratorDruidLeaderSelector#createNewLeaderLatchWithListener—isLeader()callslistener.becomeLeader()inline on theLeaderSelector[...]single-thread executor; on any thrown exception it alerts and callsnotLeader().
becomeLeader() starts the task runner (a managed lifecycle instance) before the supervisor manager, and HttpRemoteTaskRunner.start() blocks until it has synced with every discovered worker:
DruidOverlord$1.becomeLeader()builds the"task-master"Lifecycle, addstaskRunner(HttpRemoteTaskRunner) as a managed instance, thensupervisorManager, thenleaderLifecycle.start().HttpRemoteTaskRunner#startWorkersHandling()— after worker discovery, iteratesfor (WorkerHolder worker : workers.values()) { worker.waitForInitialization(); }. The loop holdsWorkerHolderreferences obtained at loop start.
The per-worker wait can never complete for a flapped worker:
WorkerHolder#waitForInitialization()→syncer.awaitInitialization(); returnsfalse→throw new RE("Failed to sync with worker[%s]").ChangeRequestHttpSyncer:maxDurationToWaitForSync = 3 * serverHttpTimeoutandawaitInitialization()awaitsinitializationLatchup to that duration.serverHttpTimeoutisHttpRemoteTaskRunnerConfig#getSyncRequestTimeout()(defaultPT3M).initializationLatchis only counted down on the first successful full/delta sync.- When the worker's discovery node is removed,
HttpRemoteTaskRunner#removeWorker()doesworkers.remove(host)thenworkerHolder.stop()→syncer.stop(). Stopping the syncer does not count downinitializationLatch.
So when the node flaps mid-startup: the old holder the startup loop is awaiting gets stop()ped (latch stuck at 1 forever), while a brand-new holder is created via addWorker() and syncs normally. The loop keeps awaiting the dead holder, blocks the full 3 × syncRequestTimeout, then throws — failing becomeLeader().
Impact
- Overlord leadership acquisition is delayed/failed for up to
3 × druid.indexer.runner.syncRequestTimeout(default 9 minutes) whenever a worker's discovery entry flaps during that startup window — likely during the very ZK instability that caused the leadership change in the first place. - During the gap the Overlord never reaches
SupervisorManager.start(), so streaming supervisors are unmanaged: Kafka indexing tasks that reach theirtaskDurationand exit are not replaced, and ingestion falls behind. When leadership finally settles on another node, all supervisors are (re)started at once, producing a synchronized ingestion-lag spike across every datasource on the cluster. - The
becomeLeader()failure callsnotLeader(), adding another leadership transition to an already-unstable moment.
Suggested fixes (for discussion)
- In
startWorkersHandling(), don't await a capturedWorkerHolderreference — re-check the current holder inworkers(skip/replace ones that have been removed/stopped), or await by host and drop the wait if the entry disappears. - Have
WorkerHolder#stop()/ChangeRequestHttpSyncer#stop()count down (or otherwise release)initializationLatchsoawaitInitialization()returns promptly for a stopped syncer instead of blocking the full timeout. - Make the initial worker sync best-effort: log-and-continue when a single worker can't sync at startup (it will sync via the normal
addWorkerpath) rather than failing the entirebecomeLeader()— optionally bound the aggregate wait rather than paying3 × syncRequestTimeoutper worker.
Debugging already done
Correlated Overlord logs (becomeLeader start → becomeLeader() failed after ~555s with Failed to sync with worker[...]), the affected MiddleManager's logs (process healthy throughout; ZK node re-announced; successfully POSTing to the new Overlord), and the discovery flap on the Overlord (Kaboom! Worker[...] removed! then reportin' for duty! within ~1s), against the code paths above. Timing matches 3 × PT3M.
Activity
aho135 commentedon Sep 14, 2026
Adding a diagram to make the race easier to follow. The crux is that two threads touch the same
WorkerHolder: the single-threadedLeaderSelectorrunningbecomeLeader(), and the Curator discovery-listener thread runningremoveWorker()/addWorker().flowchart TD A["ZooKeeper leader restarts<br/>Curator SUSPENDED → RECONNECTED<br/>Overlord leadership moves to new node"] --> B subgraph T1["LeaderSelector thread (single-threaded, synchronous)"] direction TB B["isLeader() → listener.becomeLeader()"] --> C["DruidOverlord.becomeLeader()<br/>build 'task-master' Lifecycle<br/>start HttpRemoteTaskRunner FIRST (before SupervisorManager)"] C --> D["HttpRemoteTaskRunner.startWorkersHandling()<br/>discover workers → create WorkerHolder A for MM-X<br/>start all syncers"] D --> E["loop: for (WorkerHolder w : workers.values())<br/>w.waitForInitialization()<br/>(holds reference to holder A)"] E --> F["reach MM-X → holderA.awaitInitialization()<br/>await latch up to 3 × syncRequestTimeout"] F --> G{"holder A latch<br/>counts down?"} G -->|"NO — A was stopped pre-init (see race)"| H["block full 3 × PT3M = 9 min"] H --> I["awaitInitialization() = false<br/>throw RE 'Failed to sync with worker[MM-X]'"] I --> J["becomeLeader() throws"] end subgraph T2["Curator discovery-listener thread (concurrent, ~1s later)"] direction TB K["MM-X ZK session reset →<br/>ephemeral node removed"] --> L["nodesRemoved → removeWorker(MM-X)<br/>workers.remove(MM-X)<br/>holderA.stop() → syncer.stop()"] L --> M["stop() does NOT count down<br/>holderA.initializationLatch<br/>(stuck at 1 forever)"] M --> N["MM-X re-announces node →<br/>nodesAdded → addWorker(MM-X)"] N --> O["create holder B → holderB.start()<br/>workers.put(MM-X, B)"] O --> P["holder B syncs fine<br/>(latch B counts down, streams tasks)"] end D -. "loop captured ref to A" .-> E M == "A's latch never fires ⇒ this is why G = NO" ==> G P -. "B is healthy but the loop is awaiting A, not B" .-> F J --> Q["CuratorDruidLeaderSelector.isLeader() catch:<br/>'becomeLeader() failed' → notLeader()"] Q --> R["leadership released → another node becomes leader<br/>→ mass supervisor/task restart → ingestion-lag spike"] classDef bug fill:#ffdddd,stroke:#cc0000,stroke-width:2px,color:#000; classDef ok fill:#ddffdd,stroke:#22aa22,color:#000; class M,H,I bug; class P ok;In one sentence: the startup loop grabs a reference to
WorkerHolder Afor MM-X, but a split second later the discovery threadremoveWorkers MM-X — callingholderA.stop(), which stops the syncer without counting down itsinitializationLatch— and creates a fresh healthyholder B; the loop keeps awaiting the now-dead A, whose latch can never fire, so it blocks the full3 × syncRequestTimeout(9 min) and failsbecomeLeader().Two conditions must coincide for the hang:
startWorkersHandling()captured holder A but while the loop still awaits it.WorkerHolder.stop()/ChangeRequestHttpSyncer.stop()never releaseinitializationLatch, so a stopped-pre-init holder is indistinguishable from a merely-slow one and the awaiter waits the full timeout.jianjindream commentedon Oct 2, 2026
I investigated this on
masterat3e9365196f9893ff9b1cefcfbcb57d49e4080b4aand reproduced the stale-holder initialization wait.Releasing the syncer's initialization latch on stop, while tracking successful initialization separately, addresses the wait. However, skipping the removed holder during startup exposes a task-recovery ordering that needs clarification:
The replacement is correctly excluded from eligible workers until initialized; that does not prevent reassignment to C. In a control case where B completes fullSync before task recovery, the original ownership is restored without assignment or shutdown requests.
This is a controlled local test using the production discovery, syncer, TaskQueue recovery and assignment paths, with mocked HTTP transport and storage. TaskQueue management is invoked directly; no live cluster was tested.
What startup synchronization contract should this fix preserve? In particular, should startup follow the current holder for an initially discovered host and wait for its first fullSync? How should that handle a remove/readd gap or repeated replacement within a bounded wait?
I have paused the production changes rather than introduce an unconfirmed recovery policy. I can share the focused reproduction and candidate diff if useful.