synapse
A component of the AienOS ecosystem.
Brought by drakestapleton
synapse / docs / A2_SEGMENT_STORE.md
| 1 | # A2: Segment Store Replacing File per Blob |
| 2 | |
| 3 | ADR 0003 section 9 step A2 (issue #23), design in section 5.9. Group-commit stores (WAL v2) now keep |
| 4 | every blob inside append-only segment files for good. Before A2, WAL v2 already wrote blobs inline in |
| 5 | `wal.v2.log`, but every `checkpoint_and_compact` turned the retained inline blobs back into one file |
| 6 | each (`blobs/<2-hex>/<64-hex>`, temporary file, rename, directory flush) and emptied the log. A2 removes |
| 7 | that: no blob file is written for a segment store, and acknowledged segment bytes are never rewritten |
| 8 | or removed. |
| 9 | |
| 10 | ## 1. Design |
| 11 | |
| 12 | - **Segments.** Segment 1 is `wal.v2.log` (so a pre-A2 single-file WAL v2 store is simply segment 1); |
| 13 | segment n >= 2 is `wal.v2.{n:06}.log`. Every segment uses the WAL v2 batch format unchanged |
| 14 | (`crates/synapse-store/src/wal2.rs`): 4 KiB-aligned batches of frames with the blobs inline, |
| 15 | written into pre-written zero space. A batch is one `pwrite` plus one `fdatasync`. |
| 16 | - **Rotation.** When the active segment holds data and the next batch would end past |
| 17 | `StoreLimits::segment_bytes` (default 256 MiB), the writer creates the next file, zero-fills it, |
| 18 | `sync_all`s it, and flushes the store directory, and only then writes the batch into it. A batch |
| 19 | larger than a segment goes into an empty segment on its own. Reopening a store also syncs the active |
| 20 | segment and flushes the directory before the first write (a file left by a crash before its |
| 21 | directory flush is made durable first). |
| 22 | - **Index.** In memory: `digest -> (segment, offset, length)` for every blob, filled as batches become |
| 23 | durable. On disk, next to each checkpoint: a segment index file `wal.v2.index.<16 hex>` (format `SSX1`, |
| 24 | documented in `crates/synapse-store/src/segment.rs`) holding the replay position (segment, offset) |
| 25 | and the locations of the blobs the checkpoint retains. It is bound to the checkpoint by |
| 26 | `SHA-256("SYNAPSE_SEGMENT_CHECKPOINT_BINDING_V1\0" || checkpoint file name || checkpoint bytes)` and |
| 27 | sealed by a SHA-256 trailer; a missing, damaged, or mismatched index refuses to open |
| 28 | (`CheckpointCorrupt`). Recovery seeds the in-memory index from it and rebuilds the rest by replaying |
| 29 | the segments from the replay position. |
| 30 | - **Checkpoint.** Order: every frame durable; segment index written atomically (temporary file, |
| 31 | rename, directory flush); `checkpoint.v2` renamed into place; `wal.log` replaced by the marker; older |
| 32 | index files removed. Only index metadata is ever removed: segment data is not swept (no garbage |
| 33 | collection before step K1, ADR section 9). Blob reads verify the digest of the bytes read, so a |
| 34 | damaged segment byte under a retained blob is refused on read (`BlobDigestMismatch`). |
| 35 | - **Layout marker.** `wal.log` ends with an 88-byte marker frame (kind `0xFF`); version 3 means the |
| 36 | segment layout, version 2 the single-file WAL v2 layout. A pre-A2 binary refuses version 3 |
| 37 | (`WalVersionUnsupported`), so it can never misread a segment store as single-file. |
| 38 | - **Upgrade.** Opening a v1 store or a single-file WAL v2 store with group commit writes the index |
| 39 | (replay from segment 1, offset 0) and then atomically rewrites `wal.log` with the version 3 marker. |
| 40 | Old WAL logs still replay; v1 blob files stay where they are and stay readable. |
| 41 | |
| 42 | ## 2. Durability Contract |
| 43 | |
| 44 | A change is acknowledged only after the `fdatasync` of its batch in its segment returned success, and, |
| 45 | when the batch is the first in a new segment file, after that file was synced and the store directory |
| 46 | flushed. Whether a flush survives power loss (the drive's volatile write cache) is UNVERIFIED; the |
| 47 | guarantee is "survives process crash and kernel crash" (ADR section 5.9). |
| 48 | |
| 49 | Recovery rules for segments: |
| 50 | |
| 51 | - Every segment id from 1 to the last must be present (a missing middle segment is `WalSequenceGap`). |
| 52 | - A torn tail (the last batch written, nothing valid after it, its lost end reading back as zeros) is |
| 53 | accepted only in the last segment that holds data; every later segment must be all zero or empty. |
| 54 | Strict recovery refuses it (`WalTornFrame`); truncating recovery zeroes it. The same damage in an |
| 55 | earlier segment fails closed under both policies. |
| 56 | - A last segment file that is all zero, empty, or missing (crash after its creation, before or after its |
| 57 | directory flush) is not data; the next rotation truncates and pre-writes it again. |
| 58 | |
| 59 | ## 3. Acceptance and Tests |
| 60 | |
| 61 | Tests are in `crates/synapse-store/src/segment_tests.rs`, named after the acceptance items. Stores use |
| 62 | 64 KiB segments so a few 6 KB changesets cross several segment files. Process crashes rerun the test |
| 63 | binary as a child that terminates itself at the injected fault (exit code 42) and logs every |
| 64 | acknowledgement the moment `ingest_changeset` returns it; the parent then checks that every logged |
| 65 | acknowledgement survives. |
| 66 | |
| 67 | | Acceptance item | Test | What it shows | |
| 68 | | :--- | :--- | :--- | |
| 69 | | Torn segment | `a2_torn_segment_crash_mid_batch_keeps_every_ack` | Crash 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 | |
| 70 | | Torn segment (fail closed) | `a2_torn_segment_with_data_after_it_fails_closed_under_both_policies` | The 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) | |
| 71 | | Crash between payload and index | `a2_crash_between_payload_and_index_keeps_every_ack` | Crash 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` | |
| 72 | | Crash between payload and index (checkpoint) | `a2_crash_after_segment_index_before_checkpoint_keeps_every_ack` | Crash 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 | |
| 73 | | Flush failure | `a2_flush_failure_directory_flush_of_new_segment_acks_nothing`, `a2_flush_failure_batch_sync_in_new_segment_acks_nothing` | A 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 | |
| 74 | | Crash after a new segment file before its directory flush | `a2_crash_after_new_segment_file_before_directory_flush_keeps_every_ack` | Crash 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 | |
| 75 | | Durability order | `a2_directory_flush_precedes_every_batch_in_a_new_segment` | A 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 | |
| 76 | | File per blob replaced | `a2_checkpoint_keeps_blobs_in_segments_across_reopen` | After checkpoints no file exists under `blobs/`, segment bytes are unchanged, blobs read back across four segments after reopen | |
| 77 | | Old stores readable | `a2_pre_a2_stores_replay_and_switch_to_segments` | The 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 | |
| 78 | | Index decoder | `a2_segment_index_round_trips_and_refuses_damage` | Round trip; a flipped bit, truncation, an entry past the replay position, or unsorted entries are refused | |
| 79 | |
| 80 | Three existing tests changed with the layout, keeping their intent: two A1 migration tests read the |
| 81 | frames after the checkpoint's replay position instead of the whole `wal.v2.log` (a checkpoint no |
| 82 | longer empties it), and `test_v2_checkpoint_crash_window_reopens_and_skips_covered_frames` now covers |
| 83 | the A2 checkpoint window (checkpoint renamed, `wal.log` not yet replaced), checks a damaged covered |
| 84 | blob is refused on read, and checks a missing segment index refuses to open. |
| 85 | |
| 86 | ### 3.1 Red before green |
| 87 | |
| 88 | Evidence in `evidence/a2-segment-store/red-green/`: |
| 89 | |
| 90 | - `red-main-compile.txt`: the suite against main `e863c87` cannot build (no segment store, none of the |
| 91 | four new fault injections). |
| 92 | - `red-main-behaviour.txt`: a probe with the core assertion of |
| 93 | `a2_checkpoint_keeps_blobs_in_segments_across_reopen`, written against main's API: main's checkpoint |
| 94 | writes 20 blob files for 20 changesets and empties `wal.v2.log`. Fails on main, passes here. |
| 95 | - `mutation-M1.txt` to `mutation-M7.txt`: each acceptance test fails when the defect it guards is put |
| 96 | back into this branch, and passes again without it (`green.txt`): |
| 97 | |
| 98 | | Mutation | Defect put back | Tests that fail | |
| 99 | | :--- | :--- | :--- | |
| 100 | | M1 | Rotation skips the directory flush of the new segment | the three new-segment tests and the durability-order test | |
| 101 | | M2 | Reopen does not flush the directory before writing | durability order | |
| 102 | | M3 | A torn batch is accepted with data in a later segment | torn segment (fail closed) | |
| 103 | | M4 | The segment index is written without blob locations | both crash-between-payload-and-index tests, checkpoint, old stores | |
| 104 | | M5 | Replay does not rebuild blob locations from the segments | 8 of 10 | |
| 105 | | M6 | A failed directory flush is ignored (the batch is acknowledged) | flush failure (directory) | |
| 106 | | M7 | Strict recovery truncates a torn tail like `TruncateTornTail` | both torn segment tests | |
| 107 | |
| 108 | The crash tests kill a process; they cannot see a missing flush on their own (the page cache survives a |
| 109 | process crash). The durability-order trace is what makes M1 and M2 fail. |
| 110 | |
| 111 | ## 4. Seconds and Joules per Accepted Change |
| 112 | |
| 113 | This section is a performance report. It is not acceptance: acceptance is the crash suite in section 3. |
| 114 | |
| 115 | **Method.** `synapse-bench segments` (`crates/synapse-bench/src/segments.rs`) opens a group-commit |
| 116 | store (`GROUP_COMMIT_DEFAULT`, 16 MiB segments) and runs N agent threads that ingest 4,000 changesets |
| 117 | of one 11,949-byte blob each, with `checkpoint_and_compact` after every 1,000 accepted changes. One |
| 118 | cell is one (agents, repetition); its time runs from the first ingest to the end of its last |
| 119 | checkpoint, so checkpoint cost is inside the figure. Seconds per accepted change = cell time / 4,000. |
| 120 | Each acknowledgement's latency is recorded (p50, p99, max). Energy is the `aien_spbm` SoC package |
| 121 | counter read before and after each cell; idle power is measured once per run over a 30 s window before |
| 122 | the first cell. Agents 1, 4, 8, 32; 3 repetitions; medians reported with the range of the three. |
| 123 | |
| 124 | **Bounds (ADR section 6).** The flush bound is the cheapest `overwrite_*_fdatasync` p50 measured the same |
| 125 | morning by `synapse-bench floor`: 1.006 ms (64 KiB overwrite; 4 KiB and 1 MiB were 1.099 ms). The |
| 126 | interval bound per accepted change is the largest of: flush bound / agents (one flush can carry every |
| 127 | waiting agent), 11,949 B at the 15.754 GB/s measured write floor, 11,949 B at 273 GB/s memory |
| 128 | bandwidth, and the SHA-256 (39.46 GB/s) plus Ed25519 (397,560 verifies/s) cost of one change. The |
| 129 | latency bound is flush bound + bytes at 273 GB/s. The energy bound is idle package power times the |
| 130 | interval bound (the least energy the machine spends being on for that long). Fraction of bound = |
| 131 | bound / measured, so 1.0 is at the bound. |
| 132 | |
| 133 | **Results** (run 2026-10-16T08:13:12Z to 08:21:08Z, branch commit `0da8183`; raw samples in |
| 134 | `evidence/a2-segment-store/measurement/raw-a2.json`, summary in `report-a2.json`): |
| 135 | |
| 136 | | Agents | Interval per change (ms) median [range] | Interval bound (ms) | Fraction | Latency p50 / p99 / max (ms) | Latency fraction (p50) | Package energy per change (mJ) median [range] | Energy bound (mJ) | Energy fraction | Changes per flush | |
| 137 | | ---: | :--- | ---: | ---: | :--- | ---: | :--- | ---: | ---: | ---: | |
| 138 | | 1 | 1.3003 [1.2865, 1.3015] | 1.0060 | 0.774 | 1.197 / 2.200 / 12.208 | 0.841 | 27.767 [26.896, 34.392] | 20.34 | 0.733 | 1.00 | |
| 139 | | 4 | 0.6000 [0.5950, 0.6075] | 0.2515 | 0.419 | 2.150 / 4.287 / 41.618 | | 17.043 [14.128, 26.639] | 5.086 | 0.298 | 2.17 | |
| 140 | | 8 | 0.3347 [0.3345, 0.3398] | 0.1258 | 0.376 | 2.303 / 9.642 / 43.637 | | 7.970 [6.915, 14.857] | 2.543 | 0.319 | 4.01 | |
| 141 | | 32 | 0.1284 [0.1241, 0.1862] | 0.0314 | 0.245 | 2.573 / 39.732 / 45.933 | | 3.405 [2.866, 13.833] | 0.636 | 0.187 | 15.62 | |
| 142 | |
| 143 | Idle package power over the 30 s window was 20.222 W; average package power during the cells was |
| 144 | 21.6 to 28.4 W. Each cell wrote 4 segment files and 0 blob files; the four checkpoints of a cell took |
| 145 | 0.14 to 0.21 s in total. Latency fraction is given only for 1 agent: with more agents an |
| 146 | acknowledgement waits for the batch ahead of it by design (group commit), so p50 above the single-flush |
| 147 | bound is queueing, not a missed flush. |
| 148 | |
| 149 | **Comparison with main (pre-A2).** The same harness built on main `e863c87` |
| 150 | (`measurement/main-comparison.patch` adds only the benchmark module; `raw-main.json`, |
| 151 | `report-main.json`), same host, same morning: |
| 152 | |
| 153 | | Agents | Main interval per change (ms) | Main fraction | A2 interval per change (ms) | Main package energy per change (mJ) | A2 package energy per change (mJ) | |
| 154 | | ---: | ---: | ---: | ---: | ---: | ---: | |
| 155 | | 1 | 7.8461 | 0.128 | 1.3003 | 253.973 | 27.767 | |
| 156 | | 4 | 7.0774 | 0.036 | 0.6000 | 255.014 | 17.043 | |
| 157 | | 8 | 6.7881 | 0.019 | 0.3347 | 222.001 | 7.970 | |
| 158 | | 32 | 6.6204 | 0.005 | 0.1284 | 216.313 | 3.405 | |
| 159 | |
| 160 | On main each 1,000-change checkpoint writes 1,000 blob files (file, rename, directory flush each): |
| 161 | about 26 s of checkpoint per 4,000-change cell, with acknowledgements stalled up to 7.6 s behind it. |
| 162 | Between checkpoints both layouts write the same batches; the difference is the checkpoint. |
| 163 | |
| 164 | **What these figures do not show.** |
| 165 | |
| 166 | - The host was shared with other agent sessions (load average 2.2 at start, 5.8 at end). Energy |
| 167 | above idle is small against this noise and some cells read below idle; the figures above are |
| 168 | totals, not energy above idle, for that reason. Main's idle window read 33.0 W against A2's 20.2 W. |
| 169 | - The package meter is uncalibrated and covers the SoC only; NVMe energy is UNMEASURED. |
| 170 | - One host, one filesystem (ext4 on NVMe), one blob size, one segment size; no power-loss run. |
| 171 | - The remaining gap at 1 agent (0.774 of the flush bound) is per-batch work outside the flush (4 KiB |
| 172 | padding, encoding, signature verification, index update); it was not broken down further here. |
| 173 | |
| 174 | **Reproduce.** |
| 175 | |
| 176 | ```sh |
| 177 | cargo build --release -p synapse-bench |
| 178 | target/release/synapse-bench floor --dir <scratch-dir> --out floor.json --iterations 300 |
| 179 | target/release/synapse-bench segments --dir <scratch-dir> --out raw.json |
| 180 | target/release/synapse-bench segments-report --raw raw.json --floor floor.json > report.json |
| 181 | ``` |
| 182 | |
| 183 | `measurement/manifest.json` records the binaries' SHA-256, the host, and the run window; |
| 184 | `measurement/SHA256SUMS` seals the files. |
| 185 | |
| 186 | ## 5. Deviations from ADR Section 5.9 |
| 187 | |
| 188 | - **Padding per batch.** Batches stay 4 KiB-aligned (the WAL v2 format), so each batch, not each |
| 189 | object, ends in a partly filled block. The alignment is what lets recovery tell a torn tail from |
| 190 | corruption (`torn_evidence`). |
| 191 | - **Index rebuilt from checkpoint plus segments.** The ADR says the index "is rebuilt from |
| 192 | checkpoints". Here the checkpoint carries a bound segment index for the blobs it retains, and the |
| 193 | locations of later blobs come from replaying the segments after the replay position. Covered segment |
| 194 | bytes before the replay position are not re-verified at open (the bound index vouches for them); |
| 195 | their blobs are verified on every read. |
| 196 | - **WAL records and content chunks share segments, content is not chunked.** Content-defined chunking |
| 197 | is step G2 (after X6); a blob is stored whole inside its frame. |
| 198 | - **No reclamation.** Segments only grow until K1 (garbage collection) lands; superseded blob bytes |
| 199 | stay on disk. The pre-written zero tail of each finished segment (at most one pre-write chunk) is |
| 200 | also not reclaimed. |
| 201 | |
| 202 | ## 6. Limits |
| 203 | |
| 204 | - Power loss was not tested; flush durability on power loss is UNVERIFIED (drive write cache). |
| 205 | - 512-byte sector write atomicity on this drive is UNVERIFIED; the torn-tail rule assumes it (as W1). |
| 206 | - Energy is SoC package only, from the uncalibrated `aien_spbm` counters; NVMe energy is UNMEASURED. |
| 207 | - A v1 store opened with group commit keeps its existing blob files (they stay readable and are swept |
| 208 | only by the existing unreferenced-blob sweep). |
| 209 | - A pre-A2 binary refuses a segment store (marker version 3); there is no downgrade path. |