Skip to content

[fold] One generation swap for data-log and journal compaction #191

Closed

Description

@saworbit

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_with in crates/nvram-sim/src/lib.rs, with compaction_file, rename_into_place and reopen_after_failed_rename
  • Journal::compact_with in crates/capsule-registry/src/journal.rs, with compaction_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_file as one helper, for example record_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 NvramLog has now, so both callers can be tested through it.
  • Add one shared compaction_path(path). Both files already use the <path>.compact naming.
  • Both compact_with functions call the helper and set their own lost flag from its result. The journal also checks lost at the top of compact_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_with and Journal::compact_with both 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_journal are 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

  1. self-assigned this
    on Nov 14, 2026
  2. added
    enhancement
    P3
    on Nov 14, 2026
  3. saworbit commented on Nov 14, 2026

    @saworbit
    OwnerAuthor

    Cross-ref: #194 (a missing space.journal opens as an empty store, and the next reclaim erases every segment) and #188 (after journal compaction, a rebuilt space.db has 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.

  4. saworbit commented on Nov 14, 2026

    @saworbit
    OwnerAuthor

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

P3coreenhancement

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions