interplane.Get Omega
Read in your language

synapse

A component of the AienOS ecosystem.

Brought by drakestapleton

branch main Branches Tags History

synapse / docs / A1_BINARY_CODEC.md

163 lines · 11921 bytes Raw Blame History Rendered
1# ADR 0003 step A1: binary canonical codec v2 (WAL records and checkpoints)
2
3Issue #22. ADR 0003 section 9, row A1. Code: `crates/synapse-store/src/codec.rs`, integration in
4`crates/synapse-store/src/{lib.rs,wal2.rs,shared.rs}`. Evidence: `evidence/a1-binary-codec/`.
5
6## What changes
7
8* WAL record payloads (in `wal.log` and `wal.v2.log`) and the checkpoint move from JSON to one
9 canonical binary encoding. The changeset envelope, its signature bytes and every identity
10 (`ChangesetId`, `BlobDigest`, frame digests) are unchanged; envelope v2 is step B1.
11* Frame header byte 7 (`flags`) declares the record codec: `0` = v1 JSON, `1` = binary v2.
12 Any other value is refused (`WalFlagsNonZero`).
13* `checkpoint.v2` (binary) replaces `checkpoint.v1`; `checkpoint.v1` becomes a `version: 2`
14 tombstone so a binary that only knows v1 refuses the store instead of opening it empty.
15
16## Record layout
17
18`CODEC_V2 (u8 = 2) | kind (u8, WalRecord::kind_byte) | body`. Little-endian fixed-width
19integers; `usize` as `u64`; `bool` one byte 0 or 1; `Option` tag 0 or 1 then the value;
20strings and byte strings `u32 len | bytes` (strings UTF-8); sequences `u32 count | items`;
21fixed arrays raw. Field order follows the struct definitions in `synapse-core`. Exactly one
22encoding per record: any other tag, trailing byte, invalid UTF-8, invalid `RepoId` or
23out-of-order checkpoint key is refused. Every length and count is checked against the bytes
24that remain and against the cap before anything is allocated.
25
26## checkpoint.v2 layout (216-byte header, then content)
27
28| Bytes | Field |
29| :--- | :--- |
30| 0..4 | magic `SCK2` |
31| 4..6 | version `u16 = 2` |
32| 6 | codec `2` |
33| 7 | reserved `0` |
34| 8..16 | `checkpoint_seq` |
35| 16..48 | `anchor_frame_sha256` |
36| 48..80 | `v1_state_root` (last v1 `state_root_digest`; empty-store root for a store that never wrote v1) |
37| 80..112 | `prev_checkpoint_root` (`v1_state_root` for the first v2 checkpoint, else the previous `checkpoint_root`) |
38| 112..144 | `state_root` (`state_root_digest` of the content) |
39| 144..176 | `content_sha256` = SHA-256(`SYNAPSE_CHECKPOINT_CONTENT_V2\0` ‖ content) |
40| 176..208 | `checkpoint_root` = SHA-256(`SYNAPSE_CHECKPOINT_ROOT_V2\0` ‖ v1_state_root ‖ prev_checkpoint_root ‖ seq ‖ anchor ‖ state_root ‖ content_sha256) |
41| 208..216 | `content_len u64` |
42
43Content: `CODEC_V2`, then changesets, idempotency index, blob visibility, projections and
44canonical heads, each `u32 count | items` in strictly increasing key order. Decoding verifies
45header, length, content digest, root, canonical content, and recomputes the state root. Every
46failure is `CheckpointCorrupt`. The writer decodes its own bytes back before writing.
47
48## Migration rules
49
50* A store with v1 history (a v1 checkpoint or replayed JSON records) keeps writing JSON until
51 its first `checkpoint_and_compact`, which writes the first `checkpoint.v2` (the migration
52 checkpoint). The v1 root is taken before eviction. Write order: `checkpoint.v2` (temp,
53 sync, rename, directory sync), then the tombstone, then log compaction. A crash between
54 the two leaves an older `checkpoint.v1`, which the next open replaces with the tombstone.
55* Crash window. Between the `checkpoint.v2` rename and the tombstone write, the store still
56 holds the old `checkpoint.v1` and the JSON log, so a v1-only binary (main `b04ad31`) can
57 still open it. If that binary appends JSON records after the checkpoint's seq, the new code
58 refuses the store at replay with `WalVersionUnsupported`, because `checkpoint.v2` makes the
59 store binary (`a1_v1_records_appended_in_the_crash_window_are_refused`). If it checkpoints,
60 its `checkpoint.v1` is now ahead of `checkpoint.v2`, and the new code refuses the store with
61 `CheckpointCorrupt`
62 (`a1_v1_checkpoint_ahead_of_checkpoint_v2_is_refused`). Either way the store fails closed;
63 nothing is silently merged. Recovery from this window without a v1 writer in between is
64 tested on the main-written fixtures: `a1_crash_before_tombstone_recovers_wal_log`,
65 `..._wal_v2_log` (stale `checkpoint.v1`, `checkpoint.v2` present, no tombstone),
66 `a1_covered_json_frames_then_binary_frames_recover_wal_log`, `..._wal_v2_log` (tombstone
67 written, log not yet compacted, then binary frames appended: a mixed log), and
68 `a1_stale_checkpoint_v1_beside_checkpoint_v2_recovers_wal_log`. Each reopens with the same
69 state root and chain, commits a new binary record, and reopens again with the same root.
70* A fresh store writes binary from its first record.
71* Replay refuses a JSON record after a binary one, and a binary record in a v1 store before
72 its migration checkpoint (`WalVersionUnsupported`). Records a checkpoint covers are exempt.
73* A record prepared outside the lock before the migration is re-encoded at commit.
74* `StoreLimits::max_wal_record_bytes` (default 128 MiB) caps a frame's declared payload. A
75 larger declaration is refused before allocation and never taken for a torn tail
76 (`QuotaPayloadTooLarge`); the writer refuses such a record before writing any blob.
77* A malformed binary record with valid checksums is refused with the new
78 `RefusalCode::WalRecordMalformed`.
79
80## Acceptance (issue #22)
81
82| Acceptance | Test | Result |
83| :--- | :--- | :--- |
84| Every record kind decodes to the same logical record as its v1 JSON form, both directions | `a1_every_record_kind_matches_its_v1_json_form_in_both_directions` (16 records written by main `b04ad31`, all 6 kinds), `a1_edge_records_match_their_json_form_in_both_directions`, `a1_checkpoint_state_matches_its_v1_json_form_in_both_directions`, `a1_documented_layout_decodes_and_every_non_canonical_form_is_refused` | pass |
85| First v2 checkpoint records the last v1 `state_root_digest` and chains its root to it | `a1_first_v2_checkpoint_records_last_v1_root_and_chains_wal_log`, `..._wal_v2_log`, `a1_fresh_store_writes_binary_frames_and_records_the_empty_v1_root`, `a1_v1_readers_refuse_the_tombstone`, `a1_damaged_checkpoint_v2_is_refused` | pass |
86| WAL conformance 11 of 11 | `wal_suite_cases_pass` (11 cases), `wal_suite_stores_write_binary_codec_frames` (suite stores write binary frames) | pass |
87| Truncated, bit-flipped and oversized-length corpus refused typed, no panic, no allocation above the declared cap | `tests/a1_malformed_corpus.rs`: 30,690 cases at cap 2,048 bytes, 0 panics, 0 accepted, largest single allocation 136 bytes; resealed mode: 31,881 cases with checksums and digests recomputed, all reaching the decoders, 0 panics, every accept canonical, largest allocation 1,224 bytes (records, cap 2,048) and 3,976 bytes (checkpoint, the undamaged decode peak) | pass |
88| Replay seconds per changeset against the replay bound | below | reported |
89
90Corpus tally (`evidence/a1-binary-codec/red-green/green.txt`): truncated 1,502 (`WalTornFrame`);
91bit-flipped 12,016 (CRC, chain, magic, version, flags, cap, torn); oversized frame length 18
92(`QuotaPayloadTooLarge`); oversized inner length with valid checksums 18
93(`WalRecordMalformed`); checkpoint truncated 1,904 and bit-flipped 15,232 (`CheckpointCorrupt`).
94
95Most of those stop at a checksum or digest, before the decoder. The resealed mode
96(`a1_resealed_corpus_reaches_the_decoder_and_is_refused_or_canonical`) applies the same
97kinds of damage (every truncation, every bit flip, and a false `u32` length of remaining + 1,
98cap + 1 and `u32::MAX` at every byte offset), then recomputes the frame CRC, payload SHA-256,
99`checkpoint.v2` content digest and checkpoint root, so `codec::decode_record` and the
100checkpoint content decoder see the garbage. A case passes if the decoder refuses it
101(`WalRecordMalformed` for records, `CheckpointCorrupt` naming the content or state root for
102checkpoints) or if it decodes to a value whose encoding is exactly the damaged bytes (a flip
103that lands inside a hash or a name is still a valid record). Counts reaching the decoder:
104record truncated 974 (974 refused), bit-flipped 7,792 (1,225 refused, 6,567 canonical),
105false length 2,868 (877 refused, 1,991 canonical); checkpoint truncated 1,688 (1,688
106refused), bit-flipped 13,504 (3,094 refused, 10,410 canonical), false length 5,055 (1,983
107refused, 3,072 canonical). Total 31,881.
108
109Fixture provenance: `crates/synapse-store/tests/fixtures/a1-v1` is written by
110`evidence/a1-binary-codec/fixture-generator`, whose `Cargo.toml` pins `synapse-core` and
111`synapse-store` to git rev `b04ad31d2f57725f7fa971d26d6b51062f3b53a6` (main) with its
112`Cargo.lock` committed. Regenerating from that pin gives byte-identical fixtures.
113
114## Replay against the bound
115
116Reference used: the J3 all-core SHA-256 reference, 39.46 GB/s (`docs/J3_CRYPTO_REFERENCE.md`,
117which supersedes A0's 22.25 GB/s). Bound (ADR 0003 section 6.2): max(bytes / 273 GB/s memory
118bandwidth, bytes / 39.46 GB/s) = bytes / 39.46 GB/s, bytes = the store's apparent bytes
119(replay re-hashes every committed blob); at 10,000 the memory term is 3.942e-4 s
120(calc:bb71a8df8eabb86f50db709efa4104b9f1af6edcfb36f8c86a10e42aa52e97e6). Fraction = bound /
121measured, at most 1. Gate 4 workload (seed 7673641184369446932, fixture rev `1b6e340`),
122`SynapseStore::open` replaying the full `wal.log` (EveryCommit), 7 repetitions, median; raw
123samples in `replay/a1-11c48e5/trace.jsonl`. Release binary built from `11c48e5` (this branch
124merged with main `b3473fc`, which includes J3's `sha2` `asm` feature).
125
126| Changesets | Store bytes (apparent) | WAL bytes | Measured median s (min to max) | Bound s | Fraction | Achieved s per changeset |
127| :--- | ---: | ---: | :--- | ---: | ---: | ---: |
128| 100 | 1,212,536 | 64,212 | 0.001077 (0.001051 to 0.001381) | 3.0728e-5 | 0.0285 | 1.077e-5 |
129| 1,000 | 9,887,858 | 620,587 | 0.010559 (0.010369 to 0.011613) | 2.5058e-4 | 0.0237 | 1.056e-5 |
130| 10,000 | 107,605,504 | 6,288,321 | 0.120637 (0.119610 to 0.125275) | 2.7270e-3 | 0.0226 | 1.206e-5 |
131
132Calc receipts: bounds calc:382fb7ec, calc:ac060435, calc:c4a298b5; fractions calc:08e61cce,
133calc:60712e80, calc:a538f7aa (full ids:
134calc:382fb7ec606e6212301994c15cead2ba14695accd07dd0d0f66fb60124fcf415,
135calc:ac0604357b52cb16cc5fa32e35fe195e66673be4655f70fad1b1dcdd14a991f3,
136calc:c4a298b579a51d980cb40087dcc58b8e3c70ff3b53acd2769cc6e3e8e06b2418,
137calc:08e61ccef0c49ba20638f9434369ba13a83fde3438106cc4f91d8995a57cbffa,
138calc:60712e80b63bd195c0ee124a3c00c92385e483c1c183e0b3460d835136f5c97c,
139calc:a538f7aa5d19fd16a26d3056dc9c565f49c301d63044ea0360c14c6b9d6469dd).
140Per changeset at 10,000: achieved 1.206e-5 s against a bound of 2.727e-7 s (fraction 0.0226;
141calc:3dd430011f038777914dfe5ce84a5931e3e91f2d3b166767dfe16c09a4ea372c).
142
143Same-host JSON reference, same day, main `b3473fc` (`replay/main-b3473fc-json/`): 10,000
144changesets, 117,196,369 bytes, WAL 15,879,186 bytes, 0.156076 s median, bound 2.970e-3 s
145(calc:4dbd8acc297504ee2a5cec05d809482e134c1d21643b17ce83193fcf748b45a3), fraction 0.0190
146(calc:766eb64a258a43f515d920ce80f7cbf3476a3d2b37058ee48f8eb938bbd8babb). Context only, not an
147acceptance. Open after checkpoint at 10,000: 0.008945 s (binary, checkpoint 6,768,446 bytes)
148and 0.043907 s (JSON, 17,645,939 bytes).
149
150Superseded: the earlier run (`replay/a1/`, binary from `b11bd8c` before J3, and
151`replay/main-b04ad31-json/`) measured 0.261571 s and 0.30807 s at 10,000 and used A0's
15222.25 GB/s reference (bound 4.836e-3 s, fractions 0.0185 and 0.0171). Between the two runs the
153only change on the store path is J3's workspace dependency change (`sha2` with its `asm`
154feature); the drop in measured time is attributed to it, not isolated further.
155
156## Limits
157
158* Energy (J) was not measured: no power instrument was read during these runs.
159* Replay is far from the bound (fraction about 0.02 at the J3 reference); the time is
160 dominated by re-reading and re-hashing blob files and per-record work, which A1 does not
161 change. Not an acceptance.
162* `RecordCodec` and the frame API are new public items of `synapse-store`;
163 `SynapseStore::prepare_ingest` now takes `Option<RecordCodec>` instead of `bool`.