Repository navigation
fix(core): disk cache byte accounting is exact around its scans - #2183
Conversation
The cache sizes its directory with a walk that runs outside the state lock (off-thread for the first sizing since #2022, inline in evict_until), and writes and evictions account their bytes after their file operation. A scan landing between the two counted the operation twice or not at all: a write whose file the walk saw was added again by its note_write, an eviction the walk missed was subtracted again, and evict_until's total overwrote whatever landed during its rescan. Two writes of one entry could also both rename and both count. Each miss drifted the total by one entry until the next evict_until rescanned. Writes and evictions, evict_until's own removals included, now register before their file operation, at most one per path, and account after it. Every scan lands through one reconciliation: a path an operation changed while the scan walked holds what that operation left, and an operation still under way corrects the total by what it leaves when it lands. A write registers in the lock it already took to read the size, so it takes no extra lock; reads take none. A disk-full write no longer leaves its partial temporary file behind, where no accounting saw it and no eviction removed it.
|
Heads-up on One fix worth making here: a no-op write blocks an eviction.
if self.budget_bytes == 0 || target.exists() {
return;
}keeping |
|
Smaller items, none blocking:
|
bplatz
left a comment
There was a problem hiding this comment.
Approving. The reconciliation holds across the interleavings I traced — operations landing before, during and after a walk, operations spanning a land, overlapping sizing and evict_until scans, and NotFound from an in-cache vs an outside removal — since each land sets tracked_bytes and every in-flight counted together.
Some feedback in the comments above: a note on porting this to next, one suggested fix (a no-op write blocking an eviction), and smaller items. None blocking.
A paused operation waited on its release for good, and a test waited for an operation to reach a pause for good. A test that failed in between left the scope joining a parked thread. Both sides now time out, the release can make the operation panic, and the pauses are armed inside the scope so a failing test drops them and fails the parked thread at once.
It summed the walk for its eviction loop, then summed it again building the path map it lands with, and dropped the second total.
A scan opened under the state lock and closed only on its landing or error paths. A panic between the two left it open for good: every later write and eviction was recorded for it without bound, and if it was the background sizing, the size never landed and the budget was never enforced again. Nothing in that span panics today. A scan is now a guard that closes on drop, and ends the sizing when it is the sizing scan. The background sizing opens its scan on its own thread, just before the walk: anything that lands before then is on disk for the walk to see, so it needs no record.
A write registered its path before anything checked whether the entry was already on disk, so a write that would change nothing still held the path through its budget check and any rescan, and an eviction of that entry in the window left it in place. Seeding writes without checking the cache first, so it could do this. A write of an entry already on disk now returns before registering. One that races another write to create the entry is still caught by write_atomic's own check.
The walk read each listed entry's size with a ? on the stat, so an entry removed between the listing and the stat, by the cache's own eviction or from outside it, failed the whole walk with NotFound. A rescan for the budget then skipped the write it was making room for, and a release loop evicting while a writer rescanned could hit this repeatedly. Such an entry is gone, and the walk now passes over it.
|
Folded all of these into this PR (head One fix worth making here: a no-op write blocks an eviction Thanks for tracing this one. You're right, and it reproduced exactly as you described: with Folded in as 8b906c7, with your check at the top of One thing I want to be honest about: this narrows the window rather than closing it. A write On cost: a write that creates its entry now pays one extra
Agreed, and I went with the guard rather than 498e742 replaces A walk fails when an entry vanishes mid-walk Same family as this PR, so I folded it in here rather than a follow-up: 2d94b33. Removing a A failing pause-point test hangs instead of failing Yep, confirmed. I injected a failing assert into Nit: Good catch, thank you. The second total came from Heads-up on Thanks for flagging it. I checked
Happy to take the port onto The two notes that need no change Both check out, and I agree neither needs a change. On the double decrement in On N walks for N concurrent over-budget writers: also right, and it's pre-existing. Each walk |
Brings #2247, #2232, #2246, #2248, #2245, #2230, #2205, #2231, #2255, #2219, #2209, #2207, #2190, #2216, #2186, #2215, #2213, #2184, #2210, #2206, #2198, #2187, #2183, #2200, #2193, #2199, #2196 and #2145 into next. Resolutions: - Novelty: main's fact-state trim (a cutoff plus a bounded sweep) and the dictionary's coverage watermark sit on next's graph slots and per-graph OrdMaps. A commit's reification links count toward coverage. Planner stats from segment sums decline under annotations, whose link deltas only the walk counts, and otherwise carry the indexed link counts. - Stats-view identity: the index root's id reaches the snapshot through next's root decoder (`decode` takes the id, as `take_snapshot_metadata` does). `LedgerState::load_record` has a decoding-root form beside `load_decoding_root`. - Transact: #2184's parallel-relationship check runs in next's link cascade, counting the `rdf:reifies` links on the retracted edge that the transaction does not retract itself. Main's bundle `edge_reifiers` is dropped, and `subjects_with_prior_rows` and `enforce_single_target_reifiers` stay deleted. The upsert pre-check takes main's dictionary probe and bounded seek. - Query: geo search resolves the graph's POST tree once at open (pooled graphs keep their filter), an empty manifest when there is none. `branch_or_none` reads the dedicated branch, and the batched joins keep next's pooled lanes. The stats-view key hashes the root id. - Indexer: `apply_dict_novelty` and `compact_forward_streams` take the cycle's compaction budget, so the term dictionary keeps its reserved share. `Prefetched` holds `ContentBytes`. `relocalize_sorted_commit` rewrites next's 20-byte types records. - Disk cache: main's in-flight write registration, with an empty entry still read as crash debris that a write replaces. The process-wide eviction walk stays removed. - Graph import (#2232): fenced CAS and publishes, the seed carries the record's namespace and fence, 32-bit graph ids and next's loaders. An import into a graph of an existing ledger refuses triple terms and edge annotations: the splice merges no triple-term dictionary yet. - Iceberg: `create_iceberg_graph_source` goes with main (a source needs a mapping); the R2RML path keeps next's `model_instance`. - CLI: main's shared import flags without the leaflet flags; `graph import` routes through next's process-wide server routing, and `upload_source_import` serves next's `Destination`. Doc sources read a ledger through the command's open instance. - Tests: sibling ledger ids become distinct names, `genesis_ledger` is awaited, Cypher state dumps include each relationship's `rdf:reifies` link, and the graph-import arena test checks the refusal instead.
The disk cache's byte total could drift from what its directory actually holds. The cache sizes the directory with a walk that runs outside the state lock: the first sizing on a background thread (since #2022), and
evict_until's rescan inline. Meanwhile writes and evictions account their bytes after their file operation. A scan that landed between the two miscounted:evict_until's total overwrote anything that landed during its rescan, so that write or eviction was lost.Two writes of one entry at once (a seed and a fetch of the same object) could also both rename and both count. The first of these is what made
disk_cache::tests::tracked_bytes_updated_on_writefail about 1 run in 40 (400for300).Each slip moved the total by one entry's size, and only while a scan was walking (the first write's sizing, or an eviction pass). The next
evict_untilrescanned and reset it, so it was bounded and self-correcting. An over-count evicted a little early; an under-count let the directory run past its budget until the next pass.Now the total is exact. Writes and evictions, including
evict_until's own removals, register before their file operation (at most one per path) and account after it. Both kinds of scan land through one reconciliation:Two small things came along. An eviction that finds a write of the same entry under way now leaves it to that write;
evict_cached_cid's doc says so. And a disk-full write no longer leaves its partial temporary file behind: scans skip those, so it took space that nothing counted and nothing evicted.Tests. Test-only pause points drive each window deterministically:
Each fails with its piece of the mechanism taken out. On
main, a scan landing between a write's rename and its accounting counts a 100-byte write as 200, every time. The flaky test now passes 40/40 run alone.Cost. A write takes the same two lock acquisitions as before (the registration rides in the lock that reads the size), plus one path allocation and a scan of the in-flight list, which holds one entry per concurrent write or eviction. Reads take no lock, as before. Evicting an entry that exists takes one more lock; evicting one that doesn't (the usual case for a released object) takes none.
evict_untiltakes two locks per removal and now builds a path-to-size map of its rescan (one entry per cached file) to land it, which it didn't before; the background sizing builds the same map outside the lock instead of under it. On its own, a write's accounting goes from about 4 ns to about 21 ns, most of it the path allocation, against roughly 120 µs for a 1 KiB write itself on this machine's disk. Since review, a write first checks whether its entry is already on disk and returns if so: about 1 µs more for a write that creates its entry, and a write of an entry that's already cached now costs that onestatinstead of the locks, the allocation and any rescan.Review follow-ups (in this PR):
8b906c741).498e74282).stat(2d94b338b).34f4fa31f).evict_untiltotals its walk once (8dff87670).Each of the first four has a test that fails without it.
Gates (all
--all-features):cargo fmt --all -- --check, clippy-D warningson fluree-db-core, the CI wasm32 clippy step, andcargo check --workspace --all-targets: pass.cargo test -p fluree-db-core: 1,089 passed at the head (1,078 lib, 3util_bytes, 8 doc); the disk-cache suite 20 of 20 runs.3e65938db, before the review follow-ups (which touch onlydisk_cache.rs): binary-index and iceberg lib tests (the remote-store cache paths), 765 passed; and the api tests that go through the remote-storage cache: 56 passed (disk-cache locality, residency retry, encrypted storage, store-load failure, rebuild fetch counts and staging cleanup, indexing stats, memory-storage reads, reindex pre-build).tracked_bytes_updated_on_writealone, 40 runs: 40 passed.