Notes: a save writes its file while history records it (#298) #392

Closed
kayg wants to merge 0 commits from issue/298-save-overlap-2 into dev
Owner

Part of #298 (storage latency). Replaces #378. Independent of the other #298 PRs (its numbers come from their storage-bench).

What
A note save made four flushes one after another: the pending record in history, the new file, the folder, then the "done" record. Now:

  • the new file is written and flushed under its temporary name while history records the pending save, and swapped in only once that record is durable, so "history before the file" (ADR 0020) still holds;
  • the "done" record (promote) is still waited for. (An earlier version, #378, queued it without waiting; CI's edit_while_closed_is_merged_on_open showed why that's wrong: if "done" is lost and the file is then edited outside while the server is down, the next start takes our save for one that never happened, and text doubles.)

fsio::write_unless_changed is split into Prepared::new (write and flush the temporary file) and Prepared::place (check the file, swap in, flush the folder); write_unless_changed is the two in a row, unchanged for its callers. A Prepared that is dropped unplaced removes its temporary file. History gets set_pending_queued.

Checked with strace on the VM: per save, the history flush and the temporary file's flush now run at the same time, then the folder, then "done": three waits instead of four.

Numbers (perf VM, storage-bench save, median of 3 runs, #298): with all #298 PRs together vs dev a305ee2, saves on NVMe went p50 / p90 / p99 40 / 71 / 354 → 20 / 32 / 210 ms, a 300 KB note 44 / 68 / 117 → 31 / 52 / 88 ms, on HDD 77 / 124 / 216 → 50 / 79 / 145 ms. Most of the p50 gain is this PR: on its own (the earlier version that didn't wait for "done") it measured 40 → 16 ms; this one waits for one flush more.

Tests: all notes tests (crash before and after the write, a write error after the rename, #222 swaps, an outside edit while closed) and notes_crash_typing (kill -9), and a new fsio test that an unplaced write leaves the file and no temp file behind. 16 full notes-suite runs in a row clean, 8 of them alongside another suite for load.

🤖 Generated with Claude Code

https://claude.ai/code/session_018Yyy8fQGxPLkLa8v8NXz7T

Part of #298 (storage latency). Replaces #378. Independent of the other #298 PRs (its numbers come from their `storage-bench`). **What** A note save made four flushes one after another: the pending record in history, the new file, the folder, then the "done" record. Now: - the new file is written and flushed under its temporary name **while** history records the pending save, and swapped in only once that record is durable, so "history before the file" (ADR 0020) still holds; - the "done" record (`promote`) is still waited for. (An earlier version, #378, queued it without waiting; CI's `edit_while_closed_is_merged_on_open` showed why that's wrong: if "done" is lost and the file is then edited outside while the server is down, the next start takes our save for one that never happened, and text doubles.) `fsio::write_unless_changed` is split into `Prepared::new` (write and flush the temporary file) and `Prepared::place` (check the file, swap in, flush the folder); `write_unless_changed` is the two in a row, unchanged for its callers. A `Prepared` that is dropped unplaced removes its temporary file. `History` gets `set_pending_queued`. Checked with strace on the VM: per save, the history flush and the temporary file's flush now run at the same time, then the folder, then "done": three waits instead of four. **Numbers** (perf VM, `storage-bench save`, median of 3 runs, #298): with all #298 PRs together vs dev a305ee2, saves on NVMe went p50 / p90 / p99 40 / 71 / 354 → **20 / 32 / 210 ms**, a 300 KB note 44 / 68 / 117 → **31 / 52 / 88 ms**, on HDD 77 / 124 / 216 → **50 / 79 / 145 ms**. Most of the p50 gain is this PR: on its own (the earlier version that didn't wait for "done") it measured 40 → 16 ms; this one waits for one flush more. **Tests**: all notes tests (crash before and after the write, a write error after the rename, #222 swaps, an outside edit while closed) and `notes_crash_typing` (kill -9), and a new fsio test that an unplaced write leaves the file and no temp file behind. 16 full notes-suite runs in a row clean, 8 of them alongside another suite for load. 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_018Yyy8fQGxPLkLa8v8NXz7T
Notes: a save writes its file while history records it (#298)
Some checks failed
CI / server (pull_request) Successful in 22s
CI / rust (pull_request) Failing after 1m13s
CI / web (pull_request) Failing after 44m28s
2fb4583df5
A note save made four flushes one after another: the pending record in
history, the new file, the folder, and the "done" record. Now the new
file is written and flushed under its temporary name while history
records the pending save, and swapped in once that is durable (history
before the file still holds, ADR 0020). Three waits instead of four.

The "done" record is still waited for: if it were lost in a crash and
the file then changed outside, the next start could no longer tell our
save from an outside edit (tests::outside::edit_while_closed_is_merged_on_open
catches exactly that).

fsio's write_unless_changed is now Prepared::new (write and flush the
temporary file) plus Prepared::place (check and swap in).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018Yyy8fQGxPLkLa8v8NXz7T
Author
Owner

CI note: the rust job failed on calternal-engine's an_idle_user_is_released_and_comes_back_with_nothing_lost ("writer thread still running"). That test counts every store-* thread in the whole test process, so other engine tests running at the same time can keep the count up; this PR doesn't touch the engine or the store. fmt, clippy and the tests before it passed (including notes_crash_typing); cargo stops at the first failing crate, so notes and fsio didn't run there. They pass locally on this branch (full workspace, plus 16 notes-suite runs in a row). The web job fails on dev too.

CI note: the rust job failed on `calternal-engine`'s `an_idle_user_is_released_and_comes_back_with_nothing_lost` ("writer thread still running"). That test counts every `store-*` thread in the whole test process, so other engine tests running at the same time can keep the count up; this PR doesn't touch the engine or the store. fmt, clippy and the tests before it passed (including `notes_crash_typing`); cargo stops at the first failing crate, so notes and fsio didn't run there. They pass locally on this branch (full workspace, plus 16 notes-suite runs in a row). The web job fails on dev too.
Author
Owner

Landed in dev via train k (#441).

Landed in dev via train k (#441).
kayg closed this pull request 2026-10-15 14:34:27 +00:00
Some checks failed
CI / server (pull_request) Successful in 22s
CI / rust (pull_request) Failing after 1m13s
CI / web (pull_request) Failing after 44m28s

Pull request closed

Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
kayg/calternal!392
No description provided.