Repository navigation
Description
User job
Compact a store file (space.nvram or space.journal) by writing its next generation and renaming it into place. If the rename fails, leave the store either unchanged or clearly refused, never half-used.
Current behavior
The swap is written out twice, line for line:
NvramLog::compact_withincrates/nvram-sim/src/lib.rs, withcompaction_file,rename_into_placeandreopen_after_failed_renameJournal::compact_withincrates/capsule-registry/src/journal.rs, withcompaction_path
Both write a <file>.compact with record_file::write_new_file, copy the live records, catch up the tail with record_file::copy_range, and fsync. Both close the old handle first "because Windows won't rename over a file that's open", then rename. If the rename fails, both try to reopen the old file, and if that also fails, both mark the file lost. At open, both remove a leftover .compact.
The handling of the "lost" state has drifted. #150, fixed in #180, says the journal "has the same swap", and the fix was then made by hand in the data log only:
Data log (NvramLog) |
Journal (Journal) |
|
|---|---|---|
| Appends after lost | refused (ensure_not_lost) |
refused (JournalWriter::append) |
| Reads and record checks after lost | refused (read, verify_record) |
not checked |
| A second compaction after lost | refused (ensure_writable at the top of compact_with) |
runs. compact_with never looks at inner.lost |
| Rename and reopen failures injectable in tests | yes (fail_rename and fail_reopen flags; tests a_failed_rename_leaves_the_log_usable and a_swap_that_cant_recover_refuses_reads_and_appends) |
no. Nothing in the workspace exercises the journal's lost branch |
Drift: code-read, not reproduced, because the journal has no hook for injecting the failure. Getting there takes a failed rename followed by a failed reopen, so today this is unlikely, untested divergence rather than a known wrong result.
Proposed fold
- Move the swap into
nvram_sim::record_fileas one helper, for examplerecord_file::swap_in_next_generation(path, &mut current_handle, next_handle, next_path) -> Result<Swap, Lost>. It does the Windows close-then-rename, the reopen when the rename fails, and the removal of.compact, and it reports the three outcomes (swapped, unchanged, lost) as a value. - Give the helper the test-only rename and reopen failure flags that
NvramLoghas now, so both callers can be tested through it. - Add one shared
compaction_path(path). Both files already use the<path>.compactnaming. - Both
compact_withfunctions call the helper and set their ownlostflag from its result. The journal also checkslostat the top ofcompact_with, as the data log does.
Invariant
There is one implementation of the next-generation swap. A file marked lost refuses appends and further compactions until the store is reopened, whichever file it is.
Acceptance
-
NvramLog::compact_withandJournal::compact_withboth go through the one swap helper. No second close-rename-reopen block is left. - A journal test injects a failed rename alone and checks the journal is still usable.
- A journal test injects a failed rename and a failed reopen, and checks that later commits and a second
compact_journalare refused until the store is reopened. - The existing data-log rename-failure tests pass unchanged.
Non-goals
- Changing what compaction keeps, its thresholds (
worth_compacting,journal_worth_compacting) or the on-disk format. - Merging the two live-record selection steps, which really do differ (segments by ref count, journal records by
Live).
Activity
saworbit commentedon Nov 14, 2026
Cross-ref: #194 (a missing
space.journalopens as an empty store, and the next reclaim erases every segment) and #188 (after journal compaction, a rebuiltspace.dbhas dedup entries the live index dropped) are both on the compaction and lost-state path this consolidates. Worth checking both against whatever the shared swap ends up doing.saworbit commentedon Nov 14, 2026
Done in 42184b4, in #214. Both compactions now swap through
record_file::swap_in_next_generation, which returns swapped, unchanged or lost. A lost journal refuses compactions and replays as well as commits. Journal tests cover a failed rename on its own, and a failed rename followed by a failed reopen. The data log's swap tests pass, with only the path helper renamed. CHANGELOG and on-disk-format.md updated. This closes when #214 merges.