Repository navigation
fix(api): Raft reads catch up instead of reloading, and load waiters survive stale evictions - #2278
Conversation
…ewer entry Under concurrent reads and writes about 0.3% of requests failed with a 500 "load cancelled". A reader that found its cached handle stale evicted the entry by name once its staleness check returned, and that check waits on the handle's state lock, which a commit holds. Meanwhile another reader could have evicted the same entry and begun a load (or reload) that a third had joined: the removal dropped that load's waiters' senders. On Raft every applied commit raises the head watermark on every node, so concurrent readers take this path after each write. The stale entry is now removed only while it is still the same Ready handle; otherwise the reader joins the load or reload under way, or takes the newer handle. And a waiter whose load or reload ended without a result (its leader cancelled, or its entry evicted) goes round again and joins or leads the next, since its own request is still live; at shutdown waiters still fail. A peer query re-resolves its handle after a reload, so one evicted mid-reload cannot leave it serving the state it had.
…ssing commits rather than loading it again A Raft member raises a ledger's head watermark when it applies a commit, before its cache listener applies that commit to the cached state. A read in between found the cached handle behind, evicted it and loaded the ledger from scratch (index plus all unindexed commits), and the listener's one-commit catch-up then found nothing cached. Now the read takes the watermark it found on arrival as its target and waits for the catch-up in flight on the handle, or applies the missing commits itself through notify's plan (up to MAX_INCREMENTAL_COMMITS; past that the handle reloads in place). A reload in flight no longer serves a reader behind its target the state it replaces. Only when the handle still falls short (dropped or evicted meanwhile) does the read evict and load as before. Without a watermark (single node) nothing changes.
A reader whose cached handle could not be caught up to the head it arrived at went on to evict it and load, but when the entry had become a reload meanwhile, it took the handle being reloaded: the state the reload replaces, behind the commits the reader had to see. It now joins the reload, as the eviction fix meant it to, and checks the reloaded handle against its target again.
…che listener The instance refreshing may still be handling its own earlier events when the other drops the ledger and creates it again. Finding the name absent, or taken by the new ledger, its cache listener evicts or reloads the old ledger first, and the refresh then reports NotLoaded or Current, or, with a min_t, that nothing cached reaches it. The tests asserted the refresh did the reload; they now assert what they guard, that the old ledger is no longer served.
aaj3f
left a comment
There was a problem hiding this comment.
@bplatz this is a really nice fix, and at a design level no pushback from me: having the behind reader catch up under the same per-handle mutex the listener takes, through notify's own planner, is what I'd have wanted here, rather than parking readers on the listener (which is one task for every ledger). Two headline notes: (1) the "served at least the head this node applied when the read arrived" bound now holds on the fast path and for reload waiters, but a reader that joins a load or finds a freshly published Ready in the slow path still returns without the check, and on Raft that's exactly where read-your-writes on the node a client wrote through can lapse (details inline; I think it's a two-line continue) (2) this is the second round of a class we've fixed in this file before (#1261 for orphaned Loading/Reloading slots, and b11df4265/b07ada6d5 here): a single-flight slot changed after an await on an observation the await made stale. We now guard it two ways, generations on Loading/Reloading and Arc::ptr_eq on Ready, and perhaps at some point a slot token on every LoadState variant with one compare-and-remove helper would make the next one hard to write, but to be clear, not for this PR — the point fixes are right here.
Full review below:
✅ Approve with nits.
The is_same guard on the stale eviction and the waiter rejoin loop are clean fixes for the load cancelled 500s, and the reader-side catch-up is a really nice way to stop Raft reads from cold-loading after every write. The design reuses notify's plan (incremental up to MAX_INCREMENTAL_COMMITS, reload in place past it, the activation check) rather than growing a second catch-up, the catch_up mutex plus re-check means the listener and N behind readers do the work once, and the lock order is documented and holds at every call site Claude traced (catch_up is never taken while holding entries or state; reload's leader never takes it). The race tests drive each interleaving deterministically and each one goes red when its fix is reverted. The one note I'd fold in is the load-waiter / slow-path Ready arms that skip the target check (inline), which is where read-your-writes on the node a client wrote through still lapses.
Adherence to repo commitments:
- Patterns/abstractions: ✔ extends the existing single-flight
LoadStatemachine andnotify'sUpdatePlan(split intonotify+notify_handle) instead of adding a parallel catch-up path. - Performance (speed first, memory second): ✔ no performance-degradation risk; the single-node cache hit no longer takes the
stateread lock at all (holdsshort-circuits on no watermark), a behind Raft read trades a cold load for at most five commit reads under one per-handle mutex, and the only added memory is oneMutex<()>per cached handle. - Deployment targets: ✔ shared
fluree-db-apicode, but the new behavior engages only with a watermark (only the Raft state machine sets one); elsewhere it reduces to the waiter rejoin, which spawns nothing and holds nothing across invocations; wasm32 and wasm-smoke green. - Testing: ✔ seven deterministic race tests plus two catch-up handshake tests in
ledger_manager::tests, and three end-to-end catch-up tests init_notify_incremental(wired viagrp_index), all run in CI on this head; mutation checks confirm they guard the fixes. - Conventions: ✔ multi-line commit bodies explaining mechanism and consequence; fmt/clippy green; the
it_refreshrelaxation is explained in its own commit and still asserts the old ledger is no longer served.
Verified at branch HEAD 727cf824a: CI run 42521441351 green (test, clippy, fmt, wasm32, wasm-smoke, testsuite-sparql; all new tests ran by name); on a clean EC2 box Claude re-ran the ledger_manager, it_notify_incremental and it_refresh tests (all pass), ran the eight race tests 50 times (0 failures), and reverted each of the four fixes in turn: every revert turns its tests red.
Approving so you can merge when ready, but maybe worth folding in the load-waiter check first.
| // no matching leader publish is the orphaned-Loading signature. | ||
| tracing::debug!(alias = %canonical_alias, "ledger.load.waiter.park"); | ||
| match rx.await { | ||
| Ok(published) => return published.map_err(shared_load_error), |
There was a problem hiding this comment.
Optional. This is more of a suggestion than a must, but I think it's worth folding in. The arrival bound ("a read is served at least the head this node had applied when it arrived") is checked on the fast path, and since b07ada6d5 a reader that joins a reload goes back round the loop and gets checked again (:1909). But two arms still return a handle straight through: a reader that joins a load (Wait::Load, here) and a reader that finds a freshly published Ready in the slow path (:1855-1858, "Another task loaded while we waited for write lock").
The mechanism: a load samples the nameservice record when it starts, and its leader sends to its waiters under the entries lock before it runs its own absorb_missed_events notify (:2036-2071). So if commit t applies on this node while the load is in flight, a reader that arrives after the apply (target t) joins the load and is handed the handle at t - 1; the leader itself catches up afterwards, but the waiter has already returned. The slow-path Ready arm is the same thing one step later (the leader published between this reader's fast-path read and its write lock).
Why I think it matters on Raft specifically: a client's write response on node X is resolved by the Effect::Waiter in phase two of NameServiceApp::publish, after phase one already raised X's watermark (fluree-db-consensus/src/raft/app.rs:342-382). So the watermark target is what gives read-your-writes on the node a client wrote through, and these two arms are where it lapses: X's cache for the ledger was cold (idle or capacity eviction, or this PR's own fallback eviction) and a load was in flight across the write. Claude reproduced the arm on the box with the PR's own harness (begin_load → note_head_advance(1) → a joining reader → publish_load of a handle at t 0): the reader returns that same handle at t 0 with the watermark at 1. It isn't new (on next both arms returned the same way), but this is the PR that makes target explicit.
The fix I'd suggest is to send both arms back round the loop, the way the reload arm already goes, so the fast path runs catch_up on whatever is cached. A joiner then waits on the catch_up mutex behind the leader's absorb notify and usually finds the handle current; it terminates because a still-behind handle is evicted and this caller leads the next load, which returns from the leader path.
Some(LoadState::Ready(_)) => {
// Another task loaded while we waited for the write lock: check it
// against the target like any cached handle.
continue;
}match rx.await {
// Loaded: the handle is checked against the target again.
Ok(published) => {
published.map_err(shared_load_error)?;
}
// …
}Without a watermark holds short-circuits, so the single-node path costs one more entries read on these two arms and nothing else. I recognize 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.
There was a problem hiding this comment.
Addressed in 90ec5d0. One departure: a joined load whose handle already holds the target is returned directly rather than sent back round, so a waiter of a load that finishes during shutdown still gets that handle instead of loading again.
A read is served at least the head this node had applied when it arrived. The cached-handle path checked that, and so did a reader that joined a reload, but two arms returned a handle unchecked: a reader that joined a load, and one that went to load and found another caller's load already published. A load samples the nameservice record when it begins, so a commit applied while it runs, before the reader arrived, is one its handle lacks. On Raft that is where a client could miss its own write on the node it wrote through, when that node's cache for the ledger was cold and a load was in flight across the write. Both arms now go back round, where the handle is checked like any cached one: the reader waits on the catch-up bringing it up, or evicts it and leads the next load. A joined load whose handle already holds the target is returned as before, and without a watermark every handle does.
Two ledger-manager fixes for reads that overlap writes. Both show up most on a Raft cluster, where every applied commit raises the ledger's head watermark on every node and so marks cached handles as behind.
load cancelled500sUnder concurrent reads and writes about 0.3% of requests failed with a 500
load cancelled. A reader that found its cached handle stale evicted the entry by name once its staleness check returned, and that check waits on the handle's state lock, which a commit holds. In the meantime another reader could have evicted the same entry and started a load (or reload) that a third had joined; the late removal dropped that load's waiters.Readyhandle. Otherwise the reader joins the load or reload under way, or takes the newer handle.A Raft read cold-loaded the ledger after every write
A member raises the head watermark when it applies a commit, before its cache listener applies that commit to the cached state. A read in between found the handle behind, evicted it and loaded the ledger from scratch (index plus every unindexed commit); the listener's catch-up then found nothing cached.
Now the read takes the watermark it saw on arrival as its target. It waits for the catch-up already in flight on the handle, or applies the missing commits itself through
notify's plan (up toMAX_INCREMENTAL_COMMITS; past that the handle reloads in place and other readers wait for it). Only a handle that still falls short, because it was dropped or evicted meanwhile, is evicted and loaded as before. Without a watermark (a single node) nothing changes, and freshness is the same.Measured on a three-voter cluster with 4 writers and 4 readers over 4 ledgers, 10-minute runs alternating base and head: cold loads fell from about 20,000 per run (5.5–5.8% of reads) to 12, each node's first load of each ledger. Read and write throughput and latency were unchanged at that ledger size; the saving grows with what a load has to read.
Tests
ledger_manager:a_stale_eviction_leaves_a_newer_load_to_its_waiters,a_stale_eviction_leaves_a_reload_to_its_waiters,a_load_waiter_outlives_its_cancelled_leader,a_load_waiter_outlives_an_eviction_of_its_load,a_reload_waiter_outlives_its_cancelled_leader,a_reload_waiting_on_a_load_outlives_its_cancelled_leaderandwaiters_fail_when_the_manager_shuts_downdrive the races deterministically; each fails without its fix.the_head_watermark_marks_a_cached_handle_behindanda_reader_behind_the_head_waits_for_the_catch_up_in_flightcover the catch-up's handshake with the listener.it_notify_incremental: a reader one commit, a few commits and far behind the head applies the missing commits, or reloads its handle in place, instead of loading again; each fails without the catch-up.it_refresh: the two tests of a ledger dropped and created again by another instance asserted that the refresh did the reload. This instance's cache listener, still handling its own earlier events, can evict or reload the old ledger first (they failed that way onnexttoo); they now assert that the old ledger is no longer served.