Skip to content

Open a store to read it without repairing it - #9

Merged
orelvis15 merged 1 commit into
mainfrom
feat/open-a-store-without-repairing-it
Sep 8, 2026
Merged

orelvis15 merged 1 commit into
mainfrom
feat/open-a-store-without-repairing-it

Conversation

@orelvis15

Copy link
Copy Markdown
Collaborator

GlobalStore::open is not a read: it runs recovery. It drops a staged index.bin.tmp, sweeps .part packs and half-staged record packs, cuts a torn journal tail back to the last sealed record, restores quarantined packs, and writes a fresh snapshot when there is no ledger at all.

Every one of those is right after a crash, and every one of them assumes this process is the only one here. Beside a live writer they are not repairs: index.bin.tmp is not a leftover but the snapshot being staged between its two renames, and a reader that removes it makes the writer's own rename fail with ENOENT.

That is not hypothetical. It is what a concurrent test did the first time a second handle was allowed to open a store somebody was writing — the writer failed with store: io error: No such file or directory and the reader with disk I/O error downstream of it.

What this adds

GlobalStore::open_read_only reads the ledger and touches nothing: no create, no delete, no rename, no truncate. save_index — the one place every durable mutation reaches the disk — refuses such a handle with StoreError::ReadOnly, which makes read-only a property of the handle rather than a promise about how the caller uses it. Refused and not ignored: a dropped save would leave a caller believing it wrote.

What it gives up: a store left mid-crash reads as it stood before the interrupted save. That is a state a reader could have seen a moment earlier rather than a wrong one, and the next writer to open still performs every repair.

Nothing about the on-disk format changes, and open is untouched.

Why it is safe to read a store somebody is writing

Not a new invariant — the crash-safety already there:

  • a publish is invisible until its sealing commit;
  • index.bin is replaced by rename, and the open path already falls back to index.bin.prev for the window where the live snapshot does not exist;
  • a torn journal append is cut back to the last record boundary by the writer, and next_journal_record verifies a BLAKE3 seal and stops a replay at the first incomplete record either way.

Tested

cargo test -p cavs-store green (46 + 3), clippy clean. Two new tests:

  • a_read_only_open_does_not_repair_anything stages the three files each repair targets — a .tmp snapshot, a .part pack, a torn journal tail — asserts a read-only open leaves all three exactly where they are while still reading the store, and then asserts a writer still repairs all three, so nothing is lost, only deferred to the process allowed to do it.
  • a_read_only_store_refuses_to_save checks the refusal is an error and that nothing landed.

Consumed by cavsdb, where the end-to-end effect is measured: a chi overview during a full index went from 6.9 s to 291 ms on grafana, and on linux from failing outright to 113 of 113 reads answered.

`GlobalStore::open` is not a read: it runs recovery. It drops a staged
`index.bin.tmp`, sweeps `.part` packs and half-staged record packs, cuts a torn
journal tail back to the last sealed record, restores quarantined packs, and
writes a fresh snapshot when there is no ledger at all. Every one of those is
right after a crash, and every one of them assumes this process is the only one
here.

Beside a live writer they are not repairs, they are sabotage: `index.bin.tmp` is
not a leftover but the snapshot being staged between its two renames, and a
reader that removes it makes the writer's own `rename` fail with `ENOENT`. That
is not hypothetical — it is what a concurrent test did the first time a second
handle was allowed to open a store somebody was writing.

So `open_read_only` reads the ledger and touches nothing: no create, no delete,
no rename, no truncate. What it gives up is that a store left mid-crash reads as
it stood before the interrupted save, which is a state a reader could have seen
a moment earlier rather than a wrong one — and the next writer to open still
performs every repair.

`save_index` refuses such a handle, which makes read-only a property of the
handle rather than a promise about how the caller uses it. Refused and not
ignored: a dropped save would leave a caller believing it had written.
@orelvis15
orelvis15 merged commit 7bd2a21 into main Sep 8, 2026
5 checks passed
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.

1 participant