Skip to content

fix(consensus): a commit worker outlives its branch's drained queue - #2279

Merged
bplatz merged 3 commits into
nextfrom
fix/commit-worker-drained-queue
Nov 11, 2026
Merged

bplatz merged 3 commits into
nextfrom
fix/commit-worker-drained-queue

Conversation

@bplatz

@bplatz bplatz commented Nov 8, 2026

Copy link
Copy Markdown
Contributor

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: false about once per four writes: a proposal's response receiver was dropped before the response arrived.

The cause is worker churn. Applying the ApplyHead that pops a branch's last queued entry removes that branch's queue (drop_queue_if_empty). Every node's WorkerSupervisor wakes 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:

  • on the leader, its own client_write;
  • on a follower, the HTTP forward to the leader, and aborting the forward drops the leader's apply_staged_commit handler.

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

  • The supervisor keeps a running worker while its branch is still owned by this node, even with no queue. It tears a worker down only when ownership moves elsewhere, and reaps workers that have finished.
  • A worker whose branch has had no queue for 30 s returns on its own, between entries.
  • The supervisor, which runs on every applied entry, wakes each owned branch's idle worker, so a new entry is taken at once rather than at the worker's next 50 ms poll. Without the wake, write p50 rose from 62 to 83 ms.

Measurements

Three voters, 4 writers and 4 readers over 4 ledgers, 5-minute runs alternating base, fix, base, fix:

base fix base fix
Dropped proposal responses 7,666 0 7,467 0
OneshotConsumer warnings 4,157 0 4,070 0
Writes/s 61.3 60.5 60.5 59.3
Write p50 / p99 (ms) 63.7 / 93.6 64.4 / 95.4 64.4 / 95.6 65.7 / 98.5
Reads/s 706 685 714 687
Read p99 (ms) 38.4 41.0 38.6 41.7

Write 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

  • Unit (commit_worker): a_drained_branch_keeps_its_running_worker_while_owned_here, teardown_spares_a_live_worker_kept_for_a_drained_branch, and a_worker_returns_once_its_branch_stays_without_a_queue (paused clock).
  • it_embedded_node: a_branch_keeps_its_worker_across_drained_queues makes four writes spaced past the supervisor's poll, so each drains the queue, and counts the supervisor's commit worker spawned events through a tracing layer: 1 with the fix, 4 without it.

@bplatz bplatz added bug Something isn't working as expected area:transact Staging, commit, import/bulk-import, novelty, retraction semantics labels Nov 8, 2026
@bplatz
bplatz requested a review from a team November 8, 2026 12:49
@bplatz
bplatz added this pull request to stack #2281 November 8, 2026 13:02
@bplatz
bplatz removed this pull request from stack #2281 November 8, 2026 13:02
@bplatz
bplatz added this pull request to stack #2282 November 8, 2026 13:02

@aaj3f aaj3f left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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 (ownership generalizes desired_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-consensus runs 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, plus it_embedded_node::a_branch_keeps_its_worker_across_drained_queues through 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-subscriber added 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) => {}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in 8dced1e

Base automatically changed from fix/raft-read-catch-up to next November 11, 2026 01:26
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.
@bplatz
bplatz force-pushed the fix/commit-worker-drained-queue branch from 8dced1e to a1d369a Compare November 11, 2026 01:26
@bplatz
bplatz merged commit 17dac64 into next Nov 11, 2026
19 checks passed
@bplatz
bplatz deleted the fix/commit-worker-drained-queue branch November 11, 2026 01:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:transact Staging, commit, import/bulk-import, novelty, retraction semantics bug Something isn't working as expected

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants