Repository navigation
Description
While profiling sustained continuous-transact workloads (many small commits into one ledger over a long window) I hit the fluree::cow_probe warning firing on essentially every commit — with zero concurrent readers — and chased it down. I think what we have is a real per-commit perf bug on the server's normal write path, and one that gets worse exactly when we can least afford it (when the indexer falls behind). Writing it up with the mechanism because the fix needs to be made carefully.
The short version: the cached-handle transact path (ledger_cached → stage(&handle)…execute(), which is what every server transact route uses — fluree-db-server/src/routes/transact.rs:1519, :1872, :2002, :2069) can never satisfy the probe's unique-ownership precondition, so Arc::make_mut deep-clones DictNovelty (and RuntimeSmallDicts) on every commit. The regression that 34592f3 fixed for the owned/threaded path was never actually fixed for the cached-handle path, and no test guards either probe site.
Mechanism (Claude traced and measured this; pasting its findings essentially as-is, fwiw):
- The probe at
fluree-db-transact/src/commit.rs:994-1000readsArc::strong_counton the dict Arcs moved out of the commit's base state. Baseline1is only reachable when the baseLedgerStateis uniquely owned — i.e. the owned path (stage_owned), which is what the original fix was measured against. - On the cached-handle path the LedgerManager's cached
LedgerState(fluree-db-api/src/ledger_manager.rs:177) is always a second holder: the optimistic path Arc-clones every field viasnapshot()/to_ledger_state()(fluree-db-api/src/tx_builder.rs:1627-1630,ledger_view.rs:114-119), and the fast path usesclone_state(). Measuredstrong_count == 2from commit #0, index or no index. - Once an index is attached it's 3: the commit path itself rebuilds a
BinaryRangeProviderevery commit (commit.rs:1034-1039) whose clones live inside the cached snapshot, and the detach atcommit.rs:979-981can't release it —base.snapshotis anArc<LedgerSnapshot>shared with the cache, soArc::make_mut(...).range_provider = Nonedeep-copies the snapshot and nulls the private copy while the cache's copy keeps its provider. The probe confirms this directly (had_binary_provider=trueanddict=3after the detach has already run). apply_index_v2itself is clean, for the record — this is not an index-install leak.
Cost shape: the clone is ~15ns per accumulated dictionary entry (the expensive part is NsVecBiDict.reverse's one heap allocation per subject), i.e. O(entries since the last index) per commit → O(commits²) across an unindexed window. Measured end-to-end (release, fsync off, indexing off to open the window): cached-handle went 0.82 → 2.63 ms/commit over 1,500 commits (total 2.55s vs 1.09s for the owned control); at 2,000×50-node commits the per-commit delta hit 6.8ms, matching the microbench within 15%. With the default reindex_min_bytes = 100 the dicts stay trimmed and the tax is only ~0.35–0.55 ms/commit — so this mostly hides in steady state, and then compounds precisely when the indexer lags (bulk ingest, slow storage, indexer contention), feeding back into ingest slowdown right when the system is already struggling. That coupling is what I think makes this worth fixing rather than shrugging at.
Fix direction (emulated and verified, not yet implemented): take-instead-of-clone. Under the write guard in commit_and_finalize (tx_builder.rs:1325), after the no-op early return, std::mem::replace the cached state with a cheap placeholder and drop it — the staged base is provably identical at that point (the optimistic loop verified t and head_commit_id at :1644-1648; the fast path held the lock throughout), and finalize_commit already replaces the state on success. An emulation of exactly this via the public API eliminates the growth entirely (probe reads 1 on every commit, latency flat 0.76–0.81 ms/commit on the same workload). Three constraints the emulation surfaced the hard way, for whoever picks this up (probably me):
- The write guard must be held across the whole commit — a released guard let the cache event listener reconcile against the placeholder and produced a spurious
CommitConflict. - The error path must evict (
disconnect, precedent atfluree-db-api/src/tx.rs:2710-2719), never park-and-restore the old Arcs — restoring keeps the count at 2 and defeats the fix. - tokio's
RwLockdoesn't poison, so the take-to-replace window needs a drop guard that evicts on unwind rather than leaving a placeholder serving reads.
Two honest caveats: this removes only the cache's holder — a concurrent query holding a LedgerView across the commit window still pins the Arcs and the COW returns (inherent to the current design); and the durable fix is probably to commit into the cached state in place the way apply_single_commit already does (fluree-db-ledger/src/lib.rs:896-898), which would kill the class of bug instead of this instance. I'd suggest we take the minimal fix now and treat in-place commit as the follow-on design question, since the latter needs a decision beyond a bugfix PR.
I'll put up a PR with the minimal fix plus a regression test asserting unique ownership at the probe sites (there is none today). Happy to talk through any of this — particularly if someone remembers a reason the cached state must survive the commit window that I'm not seeing.
Activity