Commit 3f21f73
authored
backup: Phase 0b M5-2 - SQS side-record derivation (vis/byage/dedup) (#892)
## Summary
Phase 0b M5-2 implementation per the merged design doc
[`docs/design/2026_05_30_proposed_sqs_side_record_derivation.md`](https://github.com/bootjp/elastickv/blob/main/docs/design/2026_05_30_proposed_sqs_side_record_derivation.md)
(#885).
Resolves the SQS side-record decision gate the M5-1 encoder (#846)
explicitly deferred. Without this PR, restoring a backup silently
breaks: FIFO dedup gating (the next send after restore replays an
already-acked message), the by-age reaper (retention is silently
ignored, queues grow unboundedly), and visibility (ReceiveMessage finds
nothing).
## What lands
The encoder now emits three derived side-record families inline during
M5-1's per-message walk:
| Family | Always? | Value | Why required |
|---|---|---|---|
| `!sqs\|msg\|vis\|...` | yes | `[]byte(messageID)` | ReceiveMessage
scans this prefix |
| `!sqs\|msg\|byage\|...` | yes | `[]byte(messageID)` | reaper honors
`MessageRetentionPeriod` |
| `!sqs\|msg\|dedup\|...` | FIFO + non-empty `MessageDedupID` |
`sqsFifoDedupRecord` JSON (4 fields) | gates FIFO sends within 5-minute
window |
`!sqs|msg|group|` rows are **never** emitted: the live
`loadFifoGroupLock` treats key presence alone as "lock held" with no
graceful empty-owner decode, so any value (even zeroed) would
permanently block every group post-restore (gemini critical #885). Test
`TestSQSEncodeSideRecordsNoGroupRows` pins this.
Scope is classic queues only (`partition_count = 1`). M5-1's existing
`ErrSQSEncodeUnsupportedPartitioned` gate at `encodeQueue` means the
side-record walk is never reached for partitioned queues.
Partitioned-FIFO support requires coordinated decoder + encoder lift
(deferred to M5-3 per the design doc's "Deferred (partitioned-FIFO)"
section).
## Test plan
`go test -race -count=1 ./internal/backup/` — all green, including:
- `TestSQSEncodeSideRecordsCrossCheckClassic` — byte-identical key +
value cross-check against the live adapter constructors (`sqsMsgVisKey`,
`sqsMsgByAgeKey`, `sqsMsgDedupKey`, `sqsFifoDedupRecord`) for a
2-message FIFO fixture. The gold-standard pattern the parent encoder
design (§"Encoder cross-check") mandates.
- `TestSQSEncodeSideRecordsStandardQueueOmitsFIFOFamilies` — non-FIFO
queue: vis + byage emitted, dedup NOT.
- `TestSQSEncodeSideRecordsEmptyDedupOmitsDedupRow` — FIFO + empty
`message_deduplication_id`: dedup row NOT emitted.
- `TestSQSEncodeSideRecordsNoGroupRows` — FIFO queue across 3 distinct
`message_group_id` values: 0 `!sqs|msg|group|` rows emitted (regression
pin for gemini critical #885).
The two restore round-trip tests listed in the design doc test plan
(`TestSQSEncodeFifoRestoreRoundTrip`,
`TestSQSEncodeReaperFindsRestoredMessage`) are deferred to the M6 CLI /
Jepsen integration layer rather than this package — they require
single-node cluster boot that is heavier infra than this package's
existing pattern. The cross-check + three negative tests above pin the
contract M5-2 is responsible for at the encoder layer; cluster-level
restore validation belongs at the integration level.
`make lint` clean (verified via golangci-lint --fix on the touched
files).
## Self-review (5 passes)
1. **Data loss** — Full reconstruction eliminates the silent-redeliver /
invisible-message / retention-leak classes M5-1-only restores would
produce. `addSideRecords` surfaces every `b.Add` error and is wired to
the existing per-message walk's error path; no swallowed errors.
2. **Concurrency / distributed failures** — Pure offline derivation. No
Raft, no HLC, no locks; restored cluster's first sends/receives go
through the normal OCC path against the restored state.
3. **Performance** — One `b.Add` per emitted side row, no extra disk
reads (records already loaded by M5-1). Snapshot grows ~2-3× message
count, all small fixed-width keys. `addMessage`'s existing loop body
inflates by ~5 lines, no extra iterations.
4. **Data consistency** — Group-row prohibition pinned by
`TestSQSEncodeSideRecordsNoGroupRows`. Dedup gating on `(FIFO &&
non-empty MessageDedupID)` pinned by two tests. `visibleAt =
available_at_millis` matches the parent design's "SQS: vis zeroed by
default" contract. Cross-check test validates byte-identical output
against the live writer for a shared fixture.
5. **Test coverage** — One cross-check (classic; partitioned variant
deferred to M5-3) plus three negative tests cover the three
conditional-emission rules the design enumerates. Round-trip tests
deferred to M6/Jepsen as discussed above.
## Risk
Low. The encoder is offline; restoration is non-destructive (new
keyspace on a fresh cluster). The change is additive to M5-1's existing
per-message walk and gated on conditions verified by the negative tests;
existing M5-1 round-trip tests continue to pass (verified locally). The
single semantic-changing behavior is "M5-1-only restores silently break
FIFO/reaper/receive" → "restore now works"; no caller of M5-1 depends on
the prior broken state.
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit
* **New Features**
* SQS message backups now include preservation of visibility metadata,
age-based attributes, and FIFO deduplication records for complete
message restoration.
* **Tests**
* Added comprehensive test coverage validating SQS side record encoding
across standard and FIFO queue scenarios.
<!-- review_stack_entry_start -->
[](https://app.coderabbit.ai/change-stack/bootjp/elastickv/pull/892?utm_source=github_walkthrough&utm_medium=github&utm_campaign=change_stack)
<!-- review_stack_entry_end -->
<!-- end of auto-generated comment: release notes by coderabbit.ai -->3 files changed
Lines changed: 439 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
211 | 211 | | |
212 | 212 | | |
213 | 213 | | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
214 | 217 | | |
215 | 218 | | |
216 | 219 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
0 commit comments