Repository navigation
fix(consensus): a commit worker outlives its branch's drained queue - #2279
Conversation
aaj3f
left a comment
There was a problem hiding this comment.
@bplatz nice find, and yes to the fix: keying a worker's lifetime to ownership rather than to its queue is the right key, since the queue empties as a side effect of the worker's own success. One headline note: with the wake from 4e0e1ed in place, the 50 ms idle poll is mostly redundant, and it's newly a steady state (every branch written in the last 30 s keeps a worker taking the shared_state read lock 20 times a second), which is invisible at four ledgers but is background load on the apply path's lock on a host with a lot of recently written ledgers, so I think the idle sleep can lengthen (details inline). On the read dip, I agree with your read that whether readers should wait on an in-flight publish is a separate question: it's StagedStateInstall holding the state write lock across the consensus round trip, which predates this PR, and this PR only stops cutting it short. Not for this PR.
Full review below:
✅ Approve with nits.
This is a clean fix for a real churn: the ApplyHead that drains a branch's queue was getting its own worker aborted mid-await on every node, which skipped the local install and dropped the leader's response. ownership now splits desired from kept, teardown follows ownership moves and idle exits only, the idle exit is checked only between entries so it can't land mid-publish, and the wake keeps write latency where it was. Claude checked the two things that keeping a worker could have broken (a kept worker's last_committed against a recreated queue, and an idle exit racing a new enqueue) and both hold: queue ids never repeat, and drain_dead_workers respawns a finished worker for a desired branch on the next tick. The one note is the idle poll (inline).
Adherence to repo commitments:
- Patterns/abstractions: ✔ extends the existing supervisor (
ownershipgeneralizesdesired_owners, teardown stays in one helper) instead of adding a second lifecycle mechanism. - Performance (speed first, memory second):
⚠️ no regression at the measured scale; the kept worker's 50 ms idle poll is a new steady-state read-lock load per recently written branch, redundant with the wake (inline). - Deployment targets: ✔ n/a —
fluree-db-consensusruns only in a long-lived Raft host; shutdown still aborts and awaits every worker, kept ones included. - Testing: ✔ three unit tests on
ownership, teardown and the idle exit, plusit_embedded_node::a_branch_keeps_its_worker_across_drained_queuesthrough a real single-node Raft, all run in CI on this head; the integration test goes red when the old teardown rule is restored. - Conventions: ✔ multi-line commit bodies with the mechanism and the measured effect; fmt/clippy green;
tracing-subscriberadded as a dev-dependency only.
Verified at branch HEAD 70bc9c9c4: CI run 42521440957 green (test, clippy, fmt, wasm32; all four new tests ran by name); on a clean EC2 box Claude re-ran the commit_worker and it_embedded_node tests with --features raft (all pass, the keep-worker test 10 of 10), and restoring the old teardown rule fails the integration test (4 spawns, not 1) and the teardown unit test.
Approving so you can merge when ready, but maybe worth considering the idle poll first.
| } | ||
| tokio::select! { | ||
| () = self.wake.notified() => {} | ||
| () = tokio::time::sleep(POLL_INTERVAL) => {} |
There was a problem hiding this comment.
Optional. This is more of a suggestion than a must. Now that 4e0e1ed wakes the worker from the supervisor, I think the 50 ms idle poll here is mostly redundant, and this PR is what makes it a steady state. Before, a worker only existed while its branch had a queue, so it barely idled; now every branch written in the last IDLE_EXIT keeps a worker that calls snapshot_front 20 times a second for up to 30 s, about 600 shared_state read-lock acquisitions per branch per idle window, on the lock the state machine's apply takes for writing.
At four ledgers that's invisible, which matches your run. On a host with a lot of recently written ledgers it's background load on the apply path's lock with nothing to show for it, since the supervisor already runs on every applied entry (and every SUPERVISOR_POLL_INTERVAL) and signals every desired branch's wake each time, and notify_one keeps a permit if the worker isn't parked yet.
Minimally the idle sleep could use the supervisor's interval; more fully it could sleep out the rest of IDLE_EXIT and rely on the wake, keeping the 50 ms poll only for the post-publish last_committed wait, where it actually buys latency:
let Some(entry) = self.snapshot_front().await else {
let since = *idle_since.get_or_insert_with(tokio::time::Instant::now);
if since.elapsed() >= IDLE_EXIT {
return;
}
// The supervisor wakes this worker whenever its branch queues;
// the sleep only bounds the idle exit.
let left = IDLE_EXIT.saturating_sub(since.elapsed());
tokio::select! {
() = self.wake.notified() => {}
() = tokio::time::sleep(left) => {}
}
continue;
};
Claude tried exactly this on a box: commit_worker's unit tests (including a_worker_returns_once_its_branch_stays_without_a_queue, on its paused clock) and all of it_embedded_node pass unchanged with it. This is minor and non-blocking — but if you agree it's right, I'd rather see it folded in now than lost in the backlog.
Applying the ApplyHead that pops a branch's last entry removes its queue, and every node's supervisor then aborted that branch's worker while it still awaited the response to that propose: the leader's own client_write, or a follower's forward, whose abort dropped the leader's handler. openraft logged each dropped response (about one per four writes under load), the worker skipped installing the commit it had just published, and the next write respawned it. The supervisor now tears a worker down only when its branch's ownership moves elsewhere, and reaps finished ones. A worker whose branch has had no queue for 30 s returns on its own, between entries.
A worker kept across a drained queue polled for new entries every 50 ms, so the next write to its branch waited about half that before staging (write p50 62 -> 83 ms under a mixed cluster load). The supervisor runs on every applied entry and now signals each owned branch's worker, which takes the entry at once. Write throughput and latency are back to what they were when a drained queue respawned the worker.
…olling A worker kept across a drained queue looked for a new entry every 50 ms, taking the replicated state's read lock 20 times a second for up to 30 s per recently written branch, on the lock every apply takes for writing. The supervisor already signals each owned branch's worker on every applied entry and every poll of its own, and the signal is kept if the worker is not yet waiting, so the idle worker now sleeps until woken or until its idle exit. The 50 ms poll stays where it buys latency: waiting for local state to apply the pop of the entry just published. The wake now carries the next write's latency, so the kept-worker test bounds each write: left asleep, a write waits out the idle exit.
8dced1e to
a1d369a
Compare
Stops a Raft commit worker from being torn down while it is still publishing.
The problem
Under steady writes the leader logged openraft's
OneshotConsumer.tx.send: is_ok: falseabout once per four writes: a proposal's response receiver was dropped before the response arrived.The cause is worker churn. Applying the
ApplyHeadthat pops a branch's last queued entry removes that branch's queue (drop_queue_if_empty). Every node'sWorkerSupervisorwakes on the apply, no longer finds the branch among those it should serve, and aborts its worker. That worker is still awaiting the response to the very proposal that drained the queue:client_write;apply_staged_commithandler.The commit itself is safe, since an abort mid-publish was already anticipated. But each drained queue skipped
finalize_after_publish, the local install of the state just staged, so the node's cache caught up from storage through the commit listener instead. The next write to the branch then spawned a fresh worker.The fix
Measurements
Three voters, 4 writers and 4 readers over 4 ledgers, 5-minute runs alternating base, fix, base, fix:
OneshotConsumerwarningsWrite throughput and latency track run order, not the binary. Reads are about 3% lower with the fix, with read p50 unchanged at 3.1 ms. The likely cause is that a worker now holds the ledger's write guard through the response and the local install on the writes where it used to be aborted early. That is the publish completing as designed. Whether reads should wait on an in-flight publish at all is a separate question.
Tests
commit_worker):a_drained_branch_keeps_its_running_worker_while_owned_here,teardown_spares_a_live_worker_kept_for_a_drained_branch, anda_worker_returns_once_its_branch_stays_without_a_queue(paused clock).it_embedded_node:a_branch_keeps_its_worker_across_drained_queuesmakes four writes spaced past the supervisor's poll, so each drains the queue, and counts the supervisor'scommit worker spawnedevents through a tracing layer: 1 with the fix, 4 without it.