interplane.Get Omega
Read in your language

synapse

A component of the AienOS ecosystem.

Brought by drakestapleton

branch main Branches Tags History

synapse / docs / A2_SEGMENT_STORE.md

209 lines · 16667 bytes Raw Blame History Source

A2: Segment Store Replacing File per Blob

ADR 0003 section 9 step A2 (issue #23), design in section 5.9. Group-commit stores (WAL v2) now keep every blob inside append-only segment files for good. Before A2, WAL v2 already wrote blobs inline in wal.v2.log, but every checkpoint_and_compact turned the retained inline blobs back into one file each (blobs/<2-hex>/<64-hex>, temporary file, rename, directory flush) and emptied the log. A2 removes that: no blob file is written for a segment store, and acknowledged segment bytes are never rewritten or removed.

1. Design

2. Durability Contract

A change is acknowledged only after the fdatasync of its batch in its segment returned success, and, when the batch is the first in a new segment file, after that file was synced and the store directory flushed. Whether a flush survives power loss (the drive's volatile write cache) is UNVERIFIED; the guarantee is "survives process crash and kernel crash" (ADR section 5.9).

Recovery rules for segments:

3. Acceptance and Tests

Tests are in crates/synapse-store/src/segment_tests.rs, named after the acceptance items. Stores use 64 KiB segments so a few 6 KB changesets cross several segment files. Process crashes rerun the test binary as a child that terminates itself at the injected fault (exit code 42) and logs every acknowledgement the moment ingest_changeset returns it; the parent then checks that every logged acknowledgement survives.

Acceptance itemTestWhat it shows
Torn segmenta2_torn_segment_crash_mid_batch_keeps_every_ackCrash after 3,000 bytes of a batch in segment 2: strict refuses WalTornFrame, truncating recovery keeps all 12 acknowledged changes and nothing else, then new commits and a strict reopen work
Torn segment (fail closed)a2_torn_segment_with_data_after_it_fails_closed_under_both_policiesThe same torn-looking damage in segment 2 with data in segment 3 refuses to open under both policies; in the last segment it is a torn tail (control)
Crash between payload and indexa2_crash_between_payload_and_index_keeps_every_ackCrash after a batch's fdatasync, before the in-memory index update and the acknowledgement: all 10 acknowledged changes survive, the durable unacknowledged change is present with a readable blob, and its retry returns IdempotentDuplicate
Crash between payload and index (checkpoint)a2_crash_after_segment_index_before_checkpoint_keeps_every_ackCrash after the new segment index is written, before the checkpoint rename: the old checkpoint and its own index still describe the store; all acknowledged changes survive; the next checkpoint removes the orphan index
Flush failurea2_flush_failure_directory_flush_of_new_segment_acks_nothing, a2_flush_failure_batch_sync_in_new_segment_acks_nothingA failed directory flush of a new segment, or a failed fdatasync of the first batch in it: nothing acknowledged, state root unchanged, store not poisoned; reopen shows nothing of the attempt; the retry commits once
Crash after a new segment file before its directory flusha2_crash_after_new_segment_file_before_directory_flush_keeps_every_ackCrash after segment 2 is created and pre-written, before the directory flush; reopened with the file kept, emptied, and deleted (lost directory entry): all 8 acknowledged changes survive strictly, and the next commit flushes the directory before writing
Durability ordera2_directory_flush_precedes_every_batch_in_a_new_segmentA test-only trace of the writer: in every new segment, create, then file sync, then directory flush, then the first batch; a reopened store syncs its active segment and the directory before writing
File per blob replaceda2_checkpoint_keeps_blobs_in_segments_across_reopenAfter checkpoints no file exists under blobs/, segment bytes are unchanged, blobs read back across four segments after reopen
Old stores readablea2_pre_a2_stores_replay_and_switch_to_segmentsThe A1 fixtures written by the pre-A2 binary (v1 file-per-blob store, single-file WAL v2 store) replay to their recorded state root, switch to the segment layout, and keep working across new segments
Index decodera2_segment_index_round_trips_and_refuses_damageRound trip; a flipped bit, truncation, an entry past the replay position, or unsorted entries are refused

Three existing tests changed with the layout, keeping their intent: two A1 migration tests read the frames after the checkpoint's replay position instead of the whole wal.v2.log (a checkpoint no longer empties it), and test_v2_checkpoint_crash_window_reopens_and_skips_covered_frames now covers the A2 checkpoint window (checkpoint renamed, wal.log not yet replaced), checks a damaged covered blob is refused on read, and checks a missing segment index refuses to open.

3.1 Red before green

Evidence in evidence/a2-segment-store/red-green/:

MutationDefect put backTests that fail
M1Rotation skips the directory flush of the new segmentthe three new-segment tests and the durability-order test
M2Reopen does not flush the directory before writingdurability order
M3A torn batch is accepted with data in a later segmenttorn segment (fail closed)
M4The segment index is written without blob locationsboth crash-between-payload-and-index tests, checkpoint, old stores
M5Replay does not rebuild blob locations from the segments8 of 10
M6A failed directory flush is ignored (the batch is acknowledged)flush failure (directory)
M7Strict recovery truncates a torn tail like TruncateTornTailboth torn segment tests

The crash tests kill a process; they cannot see a missing flush on their own (the page cache survives a process crash). The durability-order trace is what makes M1 and M2 fail.

4. Seconds and Joules per Accepted Change

This section is a performance report. It is not acceptance: acceptance is the crash suite in section 3.

Method. synapse-bench segments (crates/synapse-bench/src/segments.rs) opens a group-commit store (GROUP_COMMIT_DEFAULT, 16 MiB segments) and runs N agent threads that ingest 4,000 changesets of one 11,949-byte blob each, with checkpoint_and_compact after every 1,000 accepted changes. One cell is one (agents, repetition); its time runs from the first ingest to the end of its last checkpoint, so checkpoint cost is inside the figure. Seconds per accepted change = cell time / 4,000. Each acknowledgement's latency is recorded (p50, p99, max). Energy is the aien_spbm SoC package counter read before and after each cell; idle power is measured once per run over a 30 s window before the first cell. Agents 1, 4, 8, 32; 3 repetitions; medians reported with the range of the three.

Bounds (ADR section 6). The flush bound is the cheapest overwrite_*_fdatasync p50 measured the same morning by synapse-bench floor: 1.006 ms (64 KiB overwrite; 4 KiB and 1 MiB were 1.099 ms). The interval bound per accepted change is the largest of: flush bound / agents (one flush can carry every waiting agent), 11,949 B at the 15.754 GB/s measured write floor, 11,949 B at 273 GB/s memory bandwidth, and the SHA-256 (39.46 GB/s) plus Ed25519 (397,560 verifies/s) cost of one change. The latency bound is flush bound + bytes at 273 GB/s. The energy bound is idle package power times the interval bound (the least energy the machine spends being on for that long). Fraction of bound = bound / measured, so 1.0 is at the bound.

Results (run 2026-10-16T08:13:12Z to 08:21:08Z, branch commit 0da8183; raw samples in evidence/a2-segment-store/measurement/raw-a2.json, summary in report-a2.json):

AgentsInterval per change (ms) median [range]Interval bound (ms)FractionLatency p50 / p99 / max (ms)Latency fraction (p50)Package energy per change (mJ) median [range]Energy bound (mJ)Energy fractionChanges per flush
11.3003 [1.2865, 1.3015]1.00600.7741.197 / 2.200 / 12.2080.84127.767 [26.896, 34.392]20.340.7331.00
40.6000 [0.5950, 0.6075]0.25150.4192.150 / 4.287 / 41.61817.043 [14.128, 26.639]5.0860.2982.17
80.3347 [0.3345, 0.3398]0.12580.3762.303 / 9.642 / 43.6377.970 [6.915, 14.857]2.5430.3194.01
320.1284 [0.1241, 0.1862]0.03140.2452.573 / 39.732 / 45.9333.405 [2.866, 13.833]0.6360.18715.62

Idle package power over the 30 s window was 20.222 W; average package power during the cells was 21.6 to 28.4 W. Each cell wrote 4 segment files and 0 blob files; the four checkpoints of a cell took 0.14 to 0.21 s in total. Latency fraction is given only for 1 agent: with more agents an acknowledgement waits for the batch ahead of it by design (group commit), so p50 above the single-flush bound is queueing, not a missed flush.

Comparison with main (pre-A2). The same harness built on main e863c87 (measurement/main-comparison.patch adds only the benchmark module; raw-main.json, report-main.json), same host, same morning:

AgentsMain interval per change (ms)Main fractionA2 interval per change (ms)Main package energy per change (mJ)A2 package energy per change (mJ)
17.84610.1281.3003253.97327.767
47.07740.0360.6000255.01417.043
86.78810.0190.3347222.0017.970
326.62040.0050.1284216.3133.405

On main each 1,000-change checkpoint writes 1,000 blob files (file, rename, directory flush each): about 26 s of checkpoint per 4,000-change cell, with acknowledgements stalled up to 7.6 s behind it. Between checkpoints both layouts write the same batches; the difference is the checkpoint.

What these figures do not show.

Reproduce.

cargo build --release -p synapse-bench
target/release/synapse-bench floor --dir <scratch-dir> --out floor.json --iterations 300
target/release/synapse-bench segments --dir <scratch-dir> --out raw.json
target/release/synapse-bench segments-report --raw raw.json --floor floor.json > report.json

measurement/manifest.json records the binaries' SHA-256, the host, and the run window; measurement/SHA256SUMS seals the files.

5. Deviations from ADR Section 5.9

6. Limits