Skip to content

fix: db() reflects owned-state commits (read-your-writes) - #1385

Merged
bplatz merged 2 commits into
mainfrom
fix/owned-state-transact-cache-staleness
Jun 29, 2026
Merged

bplatz merged 2 commits into
mainfrom
fix/owned-state-transact-cache-staleness

Conversation

@bplatz

@bplatz bplatz commented Jun 27, 2026

Copy link
Copy Markdown
Contributor

Problem

Closes #1330

Calling fluree.db(ledger_id) after committing through the owned-state API could return a previously cached GraphDb view that predates those commits — a read-your-writes violation.

The owned-state API (insert / update / transact(ledger, ...) and the transact(...).execute() builder) consumes a caller-supplied LedgerState, commits, and publishes to the nameservice, then returns a new LedgerState — but it never wrote that state back into the LedgerManager cache. A subsequent db() / ledger_cached() on a warm cache therefore served the pre-commit view.

The guarded builder path (fluree.stage(&handle).execute()) was never affected: it holds the ledger write lock across stage + commit and replaces the cached state via finalize_commit. The server transaction path (LocalCommitter) uses that guarded path, so server-side read-your-writes was already correct — this only affected the embedded owned-state API.

Fix

  • Add Fluree::finalize_owned_commit, a shared post-commit tail for the owned-state methods that writes the committed state back into the cache (via sync_owned_commit_to_cache) before computing the indexing status and triggering background indexing. This also consolidates six duplicated post-commit tails into one.
  • sync_owned_commit_to_cache is monotonic (skips when the cache has advanced past the new state) and only touches an already-cached handle — an uncached ledger loads fresh from the nameservice head on next access. On a refresh_index failure it evicts the entry so the next access reloads rather than serving stale state.
  • Add LedgerManager::get_loaded_handle to fetch a cached handle without forcing a load.

Tests

  • Drop the db_at_t workarounds in it_cyclic_bgp_probe and it_join_batched_overlay (the sites the issue called out).
  • Add it_read_your_writes as a direct regression test.
  • it_notify_incremental: in-process commits no longer leave the writer's own cache stale, so these now create a genuine cross-writer gap via a peer LedgerManager (the realistic scenario notify catch-up exists for).
  • it_query_sparql_indexed: head-with-novelty views now take the production overlay fast path, which renders a decimal AVG as a precision-preserving string.

Verification

  • Full fluree-db-api suite (2253 tests) + fluree-db-server / fluree-db-consensus tests pass.
  • cargo fmt --all --check and cargo clippy --all --all-features --all-targets -- -D warnings clean.

Docs

Updated docs/getting-started/rust-api.md and docs/concepts/time-travel.md to state that a writer reads its own writes in-process, with eventual consistency remaining a cross-process concern.

The owned-state API (`insert`/`update`/`transact(ledger, ...)` and the
`transact(...).execute()` builder) committed and published to the
nameservice but returned a new `LedgerState` without writing it back to
the `LedgerManager` cache. A subsequent `fluree.db()` / `ledger_cached()`
on a warm cache then served the pre-commit view — a read-your-writes
violation (issue #1330).

Add `Fluree::finalize_owned_commit`, a shared tail for the owned-state
commit methods that writes the committed state back into the cache
(monotonic, only when already cached) before computing indexing status
and triggering background indexing. This also consolidates six
duplicated post-commit tails. The guarded builder path is unaffected —
it already updates the cache via `finalize_commit` under the write lock,
which is why the server transaction path was never affected.

- `LedgerManager::get_loaded_handle`: fetch a cached handle without
  forcing a load from the nameservice.
- Drop the `db_at_t` workarounds in `it_cyclic_bgp_probe` and
  `it_join_batched_overlay`; add `it_read_your_writes` regression test.
- `it_notify_incremental` now creates a genuine cross-writer gap via a
  peer `LedgerManager` (in-process commits no longer leave the writer's
  own cache stale — that's the fix).
- `it_query_sparql_indexed`: head-with-novelty views now take the
  production overlay fast path, which renders a decimal AVG as a string.

Closes #1330
@bplatz
bplatz requested review from aaj3f and zonotope June 27, 2026 01:51

@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 seems to be a very good / correct fix for #1330. Only one note below that's only test-coverage related

Comment on lines +183 to +201
@@ -195,18 +189,16 @@ async fn cyclic_bgp_bounded_probe_matches_fallback() {

clear_cyclic_env();
std::env::set_var("FLUREE_CYCLIC_BGP_PROBE_SCAN_RATIO", "1");
let (spans, guard) = span_capture::init_test_tracing();
let probed = run_query(&fluree, &view, query).await;
drop(guard);
assert_eq!(
probed, expected,
"{name}: probed cascade under novelty != fallback"
);
if name == "directed-triangle" {
assert!(
spans.has_event("cyclic cascade: probing edge per-subject"),
"{name}: bounded probes should engage under novelty"
);
// At head the view is the binary index plus a separate
// novelty overlay; the bounded probe declines on that fast
// path (the overlay-merging fallback serves these rows), so
// we assert the merged result rather than probe engagement.

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.

This is a claude-code-discovered item re: test coverage and I'm just going to paste-in as-is:

  — The switch from `db_at_t(novelty_t)` to `db()` is correct for the
  read-your-writes story, but it changes which lane serves the query: at head the
  view is the binary index plus a separate novelty overlay, so the bounded probe
  declines and the overlay-merging fallback serves the rows. To accommodate this
  the test **dropped its only assertion that the bounded cyclic probe actually
  engages** (the former `spans.has_event("cyclic cascade: probing edge
  per-subject")` check). Verified repo-wide there is no other coverage of probe
  activation: the event is emitted only at `fluree-db-query/src/cyclic_bgp.rs:1340`
  and `:1442`, and `span_capture`/`has_event` no longer appear anywhere in this
  test file. Bounded-probe activation in `cyclic_bgp.rs` is now uncovered;
  only result-equivalence remains. Suggest retaining the probe-engagement
  assertion on a separate case that still engages the probe (e.g. a time-pinned /
  merged view) while keeping the new `db()` result comparison.

The read-your-writes switch from db_at_t(novelty_t) to db() moved the
Phase 2 novelty query onto the head-with-separate-overlay lane, where the
bounded probe declines (overlay unmergeable), so the probe-engagement
assertion was dropped — leaving only fallback-equivalence checks that a
silently always-declining probe would still pass.

Restore activation coverage in Phase 1 instead: at the indexed HEAD with
no novelty the overlay lane is Clean and probing is allowed, so a
probe-eligible query emits the per-subject probe event. Capture spans
across the probed lane and assert the bounded probe engaged at least once.
@bplatz

bplatz commented Jun 29, 2026

Copy link
Copy Markdown
Contributor Author

Addressed in 21ce914

Note on approach: I restored the probe-engagement assertion in Phase 1 (indexed HEAD, no novelty) rather than on a time-pinned/merged view as suggested — post-fix that view no longer engages the probe. compute_probe_overlay_state disables bounded probes off-HEAD (ctx.to_t < store.max_t()), and now that db() correctly reflects the commit, db_at_t(novelty_t) returns the same head-with-separate-overlay view where the probe declines. At the clean indexed HEAD the overlay lane is Clean and probing is allowed, so a probe-eligible query emits the per-subject event; the test now captures spans across the probed lane and asserts the bounded probe engaged at least once (fails if it silently stops).

@bplatz
bplatz merged commit d4f5414 into main Jun 29, 2026
12 checks passed
@bplatz
bplatz deleted the fix/owned-state-transact-cache-staleness branch June 29, 2026 14:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fluree.db() can serve a stale cached view after subsequent commits (read-your-writes violation)

2 participants