synapse
A component of the AienOS ecosystem.
Brought by drakestapleton
synapse / docs / PHASE5_GROUP_COMMIT.md
| 1 | # Phase 5: Durable Group Commit |
| 2 | |
| 3 | Status: implemented on branch `feat/phase5-group-commit`; measured on the Spark (GB10) host on |
| 4 | 2026-10-14 from commit `6826610` (the code with the review fixes). Evidence: `evidence/phase5-group-commit/` (manifest, report, floor, raw traces, |
| 5 | red/green test logs, `SHA256SUMS`). |
| 6 | |
| 7 | ## 1. Design in Two Lines |
| 8 | |
| 9 | Many submitters hand prepared changes (signature, digests and encoding done in parallel on their own |
| 10 | threads) to one leader, which writes the whole batch with one positioned write into WAL v2 space that |
| 11 | was written and synced in advance, then calls `fdatasync` once and only then acknowledges every member. |
| 12 | |
| 13 | A failed write or sync rolls the batch back (zeroes it) and acknowledges nobody; if the rollback |
| 14 | itself fails the store is poisoned until reopened, and recovery accepts a batch all-or-nothing. |
| 15 | |
| 16 | ## 2. Durability Rule |
| 17 | |
| 18 | No acknowledgement before the blobs, directories and WAL bytes that make the change readable are |
| 19 | synced. Concretely (`crates/synapse-store/src/shared.rs`, `wal2.rs`): |
| 20 | |
| 21 | - Blobs travel inline in the WAL v2 batch, so the batch `fdatasync` covers them. |
| 22 | - WAL v2 space is extended by writing zeros and calling `sync_all` (file size and directory entry |
| 23 | synced) before any batch lands in it; the file is created with a directory `fsync`. |
| 24 | - Waiters are released only after `v2_finish` has advanced the durable watermark. The test-only |
| 25 | `ack_log` records release order so tests can check it. |
| 26 | |
| 27 | On-disk format: `wal.v2.log`, 4 KiB aligned batches with a 64-byte `SWB2` header, CRC-32C per frame, |
| 28 | and an 8-byte trailer `SWB2END!` (no zero byte) closing every batch. A version marker frame added to |
| 29 | the legacy `wal.log` makes older fsync policies refuse the store (`WalVersionUnsupported`) instead of |
| 30 | silently ignoring v2 data. The upgrade writes the old log plus the marker to `wal.log.tmp`, syncs it, |
| 31 | renames it over `wal.log` and syncs the directory, so a crash leaves either the v1 store or the |
| 32 | complete marker. |
| 33 | |
| 34 | Torn tail rule: an invalid batch is a torn tail only when no valid batch follows it, the sector |
| 35 | holding the end of its declared extent reads back all zero, and every byte after it is zero (or its |
| 36 | header's whole sector is zero). The space was zero before the write and the trailer has no zero |
| 37 | byte, so a complete batch never ends in zeros, however few bytes of it sit in the last sector. |
| 38 | Strict open refuses a torn tail; `TruncateTornTail` drops that whole batch. A batch that still ends |
| 39 | in its trailer but fails validation is corruption, and both policies refuse it, even when its blobs |
| 40 | contain whole zero sectors. |
| 41 | |
| 42 | Checkpoint crash window: `checkpoint_and_compact` syncs the WAL, renames `checkpoint.v1` into place, |
| 43 | then empties the logs. A crash between the rename and the emptying leaves frames the checkpoint |
| 44 | already covers. Recovery verifies them (CRC, frame digest, one unbroken hash chain that must end at |
| 45 | the checkpoint's sequence and anchor digest) and skips them, for `wal.log` (v1 frames and the v2 |
| 46 | marker) and `wal.v2.log` alike. Skipping is allowed only before the first frame is applied. |
| 47 | |
| 48 | Unknown outcome: a refusal is not proof of non-commit. When a batch's rollback fails (the store is |
| 49 | then poisoned) or the leader dies after its write, callers get `IoFailure` although the batch may be |
| 50 | durable. Clients must reopen and retry with the same idempotency key; the retry returns |
| 51 | `IdempotentDuplicate` if the first attempt landed and commits exactly once otherwise |
| 52 | (`SharedStore::submit` documents this, and the error message says so). |
| 53 | |
| 54 | API edit: `SynapseService::submit` was split into `admit_submit` (authorization and checks) and |
| 55 | `index_submitted` (index update) with unchanged behaviour, so the bench can run admission, then |
| 56 | `SharedStore::submit` outside the service lock. No root `Cargo.toml` changes. |
| 57 | |
| 58 | ## 3. Tests and Red-Before-Green Evidence |
| 59 | |
| 60 | `crates/synapse-store/src/group_tests.rs` (15 tests; the store crate runs 21, all pass): |
| 61 | |
| 62 | | Test | What it proves | |
| 63 | | :--- | :--- | |
| 64 | | `test_crc32c_hardware_matches_portable` | Arm `crc32c` instructions equal the portable table | |
| 65 | | `test_group_commit_roundtrip_reopen_and_legacy_policy_refusal` | Data survives reopen; legacy policy refuses a v2 store | |
| 66 | | `test_v1_store_upgrades_to_v2_with_continuous_chain` | v1 history stays readable, sequence continues | |
| 67 | | `test_concurrent_submitters_acks_unique_contiguous_and_durable_before_ack` | 16 threads x 8 changes behind a Barrier; acks unique, contiguous 1..=128, never ahead of the durable watermark | |
| 68 | | `test_duplicate_idempotency_keys_inside_one_batch` | Same key twice in one batch commits once | |
| 69 | | `test_injected_sync_failure_acks_nothing_and_retry_commits_once` | Failed `fdatasync`: no ack, nothing visible after reopen, retry commits once | |
| 70 | | `test_injected_write_failure_acks_nothing_and_retry_commits_once` | Partial write (1500 bytes): same as above | |
| 71 | | `test_rollback_failure_poisons_store_until_reopen` | Sync and rollback both fail: store refuses work until reopen; after reopen the refused record is there and the retry is `IdempotentDuplicate` | |
| 72 | | `test_shutdown_drains_queued_submissions_and_refuses_new_ones` | Queued work finishes, new work refused | |
| 73 | | `test_subprocess_crash_mid_batch_no_lost_ack_no_phantom` | Child process exits mid-batch write; every ack survives, no unacked change appears | |
| 74 | | `test_v2_checkpoint_crash_window_reopens_and_skips_covered_frames` | Crash after the checkpoint rename, before `wal.log` is replaced and before `wal.v2.log` is reset: reopens with the same state, new commits follow; a damaged covered frame or a covered chain short of the anchor is refused | |
| 75 | | `test_v1_checkpoint_crash_window_reopens_and_skips_covered_frames` | Same window for a v1 store (before `wal.log` is truncated) | |
| 76 | | `test_torn_last_sector_with_short_tail_is_truncatable` | Last batch ends 9 to 63 bytes into its last sector and that sector is lost: strict refuses (`WalTornFrame`), `TruncateTornTail` drops only that batch | |
| 77 | | `test_corrupt_last_batch_with_zero_sector_in_blob_is_not_dropped` | One flipped bit in an acknowledged last batch whose blob holds whole zero sectors: both policies refuse, nothing truncated | |
| 78 | | `test_v2_upgrade_marker_is_atomic_and_survives_a_stray_temp_file` | A crash mid-upgrade leaves the v1 store intact; the upgrade then completes | |
| 79 | |
| 80 | Red runs (code deliberately broken, logs in `evidence/phase5-group-commit/red-green/`; each mutation |
| 81 | was reverted from a saved copy): |
| 82 | |
| 83 | 1. Crash: recovery made to keep the valid frames of a torn batch. Crash test fails: "strict recovery |
| 84 | accepted the torn batch: committed_seq wal:5 with 4 acknowledged". |
| 85 | 2. Failed flush: rollback zeroing removed. Sync test fails "failed batch visible after reopen"; write |
| 86 | test fails on reopen with a CRC mismatch (`WalTornFrame`). |
| 87 | 3. Ack ordering: waiters released before the batch write. Four tests fail, including "acknowledged |
| 88 | wal:8 while the durable watermark was wal:1" and "no waiter of a failed batch is acknowledged". |
| 89 | |
| 90 | Green: all three restored; full store suite 16/16, three consecutive runs. |
| 91 | |
| 92 | Review fixes (independent review of PR #18), red on the unfixed code, then green (`red-green/review-red.txt`, `review-green.txt`): |
| 93 | |
| 94 | 4. Checkpoint crash window: both crash-window tests fail before the fix with `WalSequenceGap` ("expected wal:6, found wal:1" for v2, "expected wal:4, found wal:1" for v1): the store could never be opened again. |
| 95 | 5. Torn tail rule: before the fix the short-tail test fails because the lost sector is not recognised as torn (strict open returns `WalCrcMismatch` instead of `WalTornFrame`, so `TruncateTornTail` refuses to open too), and the bit-rot test fails because the corrupt acknowledged batch is classified as torn (`WalTornFrame`), which `TruncateTornTail` would drop. |
| 96 | 6. Atomic upgrade: before the fix the upgrade appended in place (the stray-temp-file test fails on its `wal.log.tmp` check). |
| 97 | |
| 98 | Green: store crate 21/21, three consecutive runs. |
| 99 | |
| 100 | ## 4. Measurement Method |
| 101 | |
| 102 | Command (release binary built from commit `6826610e7cd2`, clean tree; `manifest.json` records the |
| 103 | revision and the `git archive` source digest): |
| 104 | |
| 105 | ``` |
| 106 | synapse-bench run --fixture-repo ~/workspace/aien-sovereign-core \ |
| 107 | --fixture-rev 1b6e340d48417dfbcfe2df56d48a0696a3a9b250 --write-only --arms S1,S3 \ |
| 108 | --tiers 1,4,8,32 --reps 3 --ops-floor 4000 --idle-window-s 30 |
| 109 | synapse-bench floor # hardware floor, floor.json |
| 110 | ``` |
| 111 | |
| 112 | - S1 is Gate 4's arm: Synapse with `EveryCommit` (one WAL append plus fsync per change). |
| 113 | - S3 is the same service admission (auth, checks) followed by `SharedStore` group commit |
| 114 | (`GROUP_COMMIT_DEFAULT`: up to 256 records, no added delay). |
| 115 | - Agent counts 1, 4, 8, 32 are an observed sweep, not targets. 4000 changes per cell, 3 repetitions, |
| 116 | medians reported. Storage: `/dev/nvme0n1p2`, ext4. |
| 117 | |
| 118 | Energy sensor: the `aien_spbm` hwmon device, which reads the GB10 SPBM firmware energy counters. It |
| 119 | covers the SoC package only (CPU clusters plus the rest of the package); the NVMe drive is |
| 120 | UNMEASURED. 1 mJ step, the counter updates about every 0.1 s, sampled at 10 Hz. There is no published |
| 121 | accuracy and it has never been compared with an external meter: treat all joules as uncalibrated. |
| 122 | A 30 s idle window (22.731 W) is the counterfactual baseline for "above idle" values. |
| 123 | |
| 124 | ## 5. Hardware Floor |
| 125 | |
| 126 | From `floor.json` (300 iterations each, same filesystem): |
| 127 | |
| 128 | | Floor item | Measured p50 | |
| 129 | | :--- | :--- | |
| 130 | | Overwrite already-synced space + `fdatasync`, 4 KiB / 64 KiB / 1 MiB | 1.106 / 0.903 / 0.805 ms | |
| 131 | | Append + fsync or fdatasync (4 KiB to 1 MiB) | 3.5 to 3.8 ms | |
| 132 | | `fallocate`d space + `fdatasync` | 3.715 ms | |
| 133 | | New blob file + fsync + rename + directory fsync | 6.401 ms | |
| 134 | | Sequential write, O_DIRECT / buffered | 0.138 / 0.263 s per GiB | |
| 135 | | Ed25519 verify / sign | 0.023 / 0.010 ms | |
| 136 | | Blob digest (12 KiB) / CRC-32C (request sized) | 0.021 / 0.001 ms | |
| 137 | |
| 138 | Floor per change at N agents (assumptions: ext4 file, not raw block device; the cheapest sync |
| 139 | measured; all N changes share one sync; the essential CPU work runs on 20 cores in parallel): |
| 140 | |
| 141 | - t_floor(N) = max(0.805 ms, 0.055 ms x ceil(N/20)) / N + 11 500 B x 0.138 s/GiB |
| 142 | - E_floor(N) = 22.731 W x t_floor(N) + 0.2766 mJ (above-idle energy of verify, sign, blob digest and CRC) |
| 143 | |
| 144 | One idle baseline throughout: the sweep's 30 s idle window, 22.731 W. `floor.json` stores |
| 145 | per-operation above-idle energy against its own 5 s idle window (23.19 W), which gives 0.2504 mJ for |
| 146 | the same four operations; re-based to 22.731 W (per operation: package joules minus 22.731 W times |
| 147 | the operation's share of its window) they sum to 0.2766 mJ: verify 0.0919, sign 0.0165, blob digest |
| 148 | 0.1655, CRC 0.0027 mJ. `floor.json` itself is unchanged; it was measured with the `floor` |
| 149 | subcommand from the earlier build `de17a0a` (the floor is hardware and file system behaviour, and |
| 150 | that subcommand did not change). |
| 151 | |
| 152 | "Fraction of floor" = floor / measured. 1.0 would mean running at the floor. |
| 153 | |
| 154 | ## 6. Results (medians of 3) |
| 155 | |
| 156 | | Arm | Agents | s per change | floor | fraction | J per change (pkg) | floor | fraction | above idle | mean W | |
| 157 | | :--- | ---: | ---: | ---: | ---: | ---: | ---: | ---: | ---: | ---: | |
| 158 | | S1 | 1 | 14.217 ms | 0.806 ms | 0.057 | 305.2 mJ | 18.61 mJ | 0.061 | -18.0 mJ | 21.5 | |
| 159 | | S3 | 1 | 2.048 ms | 0.806 ms | 0.394 | 51.7 mJ | 18.61 mJ | 0.360 | 4.9 mJ | 24.9 | |
| 160 | | S1 | 4 | 14.030 ms | 0.203 ms | 0.014 | 316.3 mJ | 4.88 mJ | 0.015 | -3.0 mJ | 22.5 | |
| 161 | | S3 | 4 | 0.726 ms | 0.203 ms | 0.279 | 17.2 mJ | 4.88 mJ | 0.283 | 0.7 mJ | 23.8 | |
| 162 | | S1 | 8 | 13.623 ms | 0.102 ms | 0.007 | 376.9 mJ | 2.60 mJ | 0.007 | 67.3 mJ | 27.7 | |
| 163 | | S3 | 8 | 0.411 ms | 0.102 ms | 0.248 | 12.9 mJ | 2.60 mJ | 0.202 | 4.0 mJ | 33.1 | |
| 164 | | S1 | 32 | 14.038 ms | 0.027 ms | 0.002 | 305.2 mJ | 0.88 mJ | 0.003 | -13.9 mJ | 21.7 | |
| 165 | | S3 | 32 | 0.349 ms | 0.027 ms | 0.076 | 8.5 mJ | 0.88 mJ | 0.104 | 0.6 mJ | 24.6 | |
| 166 | |
| 167 | - Gate 4 S1 for reference (`docs/GATE4_QUALIFICATION.md`): 13.79, 14.33, 13.58, 14.38 ms per change at |
| 168 | 1, 4, 8, 32 agents. This run's S1 agrees within about 0.5 ms. |
| 169 | - S3 mean batch size: 1.0, about 1.8, about 3.1, about 3.6 records at 1, 4, 8, 32 agents (max 1, 4, |
| 170 | 7, 16). No failed batches. |
| 171 | - S3 latency p50 / p99: 1.86 / 5.09, 2.71 / 5.61, 2.99 / 6.69, 9.95 / 28.3 ms. S1 p99 at 32 agents is |
| 172 | 1372 ms. |
| 173 | - Above-idle energy varies widely between repetitions (for example S1 at 1 agent: -42.6 to 11.0 mJ), |
| 174 | and medians can be negative: S1 often drew less package power than the idle window (21.5 W |
| 175 | against 22.7 W), so other work on the host moved the baseline. Do not read S1 above-idle values |
| 176 | as S1's own cost. The package totals are steadier. |
| 177 | - An earlier run on `de17a0a` (before the review fixes) gave S3 2.010, 0.718, 0.398, 0.348 ms per |
| 178 | change; it is replaced by this run and kept only in git history. |
| 179 | |
| 180 | ## 7. What Still Sits Between the Agent and the Floor |
| 181 | |
| 182 | - Admission is serialized by the service lock (auth payload digest about 0.108 ms, signature |
| 183 | verification, JSON). This limits how many changes reach a batch (about 3.6 at 32 agents) and holds |
| 184 | per-change time near 0.35 ms. |
| 185 | - JSON encode and decode on the wire (the bench approximates the transport). |
| 186 | - The signature is verified twice (admission and `prepare_ingest`). |
| 187 | - The ext4 journal and page cache: the floor itself is an ext4 overwrite plus `fdatasync`, not a raw |
| 188 | block write. |
| 189 | - A single agent waits a full write-and-sync round per change: about 2.0 ms against the 0.8 ms sync. |
| 190 | |
| 191 | ## 8. Limits |
| 192 | |
| 193 | - Energy is SoC package only and uncalibrated; NVMe energy is UNMEASURED. Above-idle values use an |
| 194 | idle window as a counterfactual and can be noisy or negative. |
| 195 | - Other work was running on the host (load average about 2 during the idle window). |
| 196 | - One run, one host, one filesystem. No confidence intervals beyond min and max of 3 repetitions. |
| 197 | - 512-byte sector write atomicity on this drive is UNVERIFIED; the torn-tail rule assumes it. |
| 198 | - A torn write that lost a middle sector of the last batch but persisted its final sector is not |
| 199 | recognised as torn: recovery then refuses to open under both policies (fails closed; an operator |
| 200 | must inspect). A corrupt last batch whose final sector became all zero would be taken as torn and |
| 201 | dropped; that needs the whole final sector zeroed, not bit rot. |
| 202 | - A refused submit (`IoFailure`) can still be durable after a failed rollback or a leader failure; |
| 203 | only a retry with the same idempotency key settles it. |
| 204 | - The crash test kills a process; it cannot detect a missing fsync. Power loss was not tested. |
| 205 | - In S3 the service's own store is opened but unused; `SharedStore` is not yet wired into |
| 206 | `SynapseService`'s read path. The 64 MiB WAL pre-write happens during setup, outside the timed window. |
| 207 | - The measured binary is `6826610`. Later commits on the branch rebase onto main and change docs |
| 208 | and evidence; the store code is unchanged since the measurement. |
| 209 | - A stray `wal.log.tmp` left by a crash during the v1 to v2 upgrade is not removed on open; it is |
| 210 | overwritten when the upgrade runs again, and it is harmless but stays on disk otherwise. |
| 211 | - The floor model is a lower bound under the stated assumptions, not a proven physical limit. |
| 212 | |
| 213 | ## 9. Reproduction |
| 214 | |
| 215 | `cargo build --release -p synapse-bench`, then the two commands in section 4 with `--out` and |
| 216 | `--work-dir` set. Verify the evidence with `cd evidence/phase5-group-commit && sha256sum -c SHA256SUMS`. |