Commit d23cf69
authored
fix(adapter/sqs): standard SendMessageBatch double-send under leader churn (#923)
## Summary
Fixes a **double-send** bug in the SQS standard-queue `SendMessageBatch`
path — the same `:duplicate-elements` class fixed for DynamoDB in PR
#920, found by an adversarial S3/SQS audit prompted by the "extend dedup
to S3/SQS" follow-up.
## The bug
`sendMessageBatchWithRetry` → `trySendMessageBatchOnce` →
`sendBatchStandardOnce` runs an **in-process OCC retry loop** that, on
every attempt, re-ran `buildBatchSendRecord` → `buildSendRecord` →
`newMessageIDHex()`, minting a **fresh random MessageID (and send
timestamp) per entry per attempt**, and derived the `data`/`vis`/`byage`
storage keys from them.
Under a Raft leader-election storm, attempt 1 can **commit** its writes
yet surface a self-inflicted `WriteConflict` to the adapter (the
mechanism PR #920 documented). The retry then mints **new** MessageIDs
and writes a **second copy of every entry** at new keys:
1. Attempt 1: mints `{M1,M2,M3}`, commits, but returns `WriteConflict`
under churn.
2. Retry: mints `{M1',M2',M3'}`, commits cleanly, returns
`{M1',M2',M3'}` + HTTP 200.
3. Queue holds **both** sets → every batch entry duplicated; the client
only learns the second set.
Standard queues carry no dedup identity, and the OCC `ReadKeys` are only
`[meta, gen]` (the random data keys never self-conflict), so nothing
fenced this.
**Why only batch:** single `SendMessage` has **no** in-process retry (a
conflict becomes a normal at-least-once SDK retry), and FIFO send is
fenced by the `MessageDeduplicationId` dedup record. The batch path
added an in-process retry without either fence.
## The fix
Pre-generate **one stable `sqsSendIdentity`** (MessageID + receipt token
+ send timestamp) **per entry, before the retry loop**, and reuse it on
every attempt (`buildSendRecordWithIdentity`). The storage keys then
stay constant across retries, so a committed-but-conflicted attempt is
**overwritten idempotently** instead of duplicated.
No FSM probe / `PrevCommitTS` / gate is needed — unlike DynamoDB's
`list_append` (which re-read a *growing* value), the batch write is a
whole-value PUT at a now-stable key, so re-applying is a plain
idempotent overwrite. The record's `QueueGeneration` is still re-derived
per attempt, so a concurrent `DeleteQueue`/`PurgeQueue` generation bump
is followed correctly (the stale-gen attempt-1 write is
orphaned/unreachable).
## S3/SQS audit outcome (recorded in the design doc)
A handler-by-handler adversarial sweep found the original "S3/SQS share
the same latent gap" note was over-broad:
- **S3 — safe.** Whole-value PUT at a stable key; `PutObject` is
OCC-fenced by a caller-supplied `StartTS` (a leader-move retry that
races an interleaved write **fails the fence rather than clobbering**),
and has no in-process re-read-recompute. `CompleteMultipartUpload`'s
manifest is re-assembled deterministically from the **immutable**
uploaded parts (computed once before its retry loop), not from a re-read
of the object value → no duplicate.
- **SQS single `SendMessage` / FIFO — safe** (random-UUID key + no
in-process retry; FIFO dedup record).
- **SQS standard `SendMessageBatch` — fixed here.**
## Tests (`adapter/sqs_batch_send_dedup_test.go`)
- `TestSendMessageBatchStandard_RetryReusesStableKeys` — first dispatch
commits-then-conflicts; asserts the retry dispatches the **same**
storage keys (fresh MessageIDs would fail it → reproduces the bug
pre-fix).
- `TestSendMessageBatchStandard_ReturnedMessageIdsStableAcrossRetry` —
the returned MessageId is the one actually written on both attempts
(client id == stored id).
## Validation
- `go test ./adapter/ -run 'SendMessage|Batch|FIFO|Send'` pass; new
dedup tests pass.
- `golangci-lint run ./adapter/...` 0 issues; `go build ./...` clean.
## Self-review (5 passes)
1. **Data loss** — the fix prevents a double-send (extra messages), and
keeps the generation re-derivation so a concurrent queue delete/recreate
still routes to the live generation; no committed message is dropped.
2. **Concurrency / distributed** — stable keys make the leader-churn
retry an idempotent overwrite; the `[meta,gen]` ReadKeys fence against
`DeleteQueue`/`PurgeQueue` is unchanged.
3. **Performance** — identities are minted once instead of
once-per-attempt (strictly fewer `crypto/rand` calls on the retry path);
hot path (no retry) is one mint per entry, same as before.
4. **Data consistency** — eliminates the duplicate-element anomaly for
standard batch send; FIFO/exactly-once and single-send semantics
unchanged.
5. **Test coverage** — new co-located tests pin key-stability across the
retry (the regression), the load-bearing property of the fix.4 files changed
Lines changed: 347 additions & 34 deletions
| 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 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
653 | 653 | | |
654 | 654 | | |
655 | 655 | | |
656 | | - | |
| 656 | + | |
| 657 | + | |
| 658 | + | |
| 659 | + | |
| 660 | + | |
| 661 | + | |
| 662 | + | |
| 663 | + | |
| 664 | + | |
| 665 | + | |
| 666 | + | |
| 667 | + | |
| 668 | + | |
| 669 | + | |
| 670 | + | |
| 671 | + | |
| 672 | + | |
| 673 | + | |
| 674 | + | |
| 675 | + | |
| 676 | + | |
| 677 | + | |
| 678 | + | |
| 679 | + | |
| 680 | + | |
| 681 | + | |
| 682 | + | |
| 683 | + | |
657 | 684 | | |
658 | 685 | | |
659 | | - | |
| 686 | + | |
660 | 687 | | |
661 | 688 | | |
662 | 689 | | |
663 | | - | |
| 690 | + | |
664 | 691 | | |
665 | 692 | | |
666 | | - | |
| 693 | + | |
| 694 | + | |
| 695 | + | |
| 696 | + | |
| 697 | + | |
| 698 | + | |
| 699 | + | |
| 700 | + | |
| 701 | + | |
| 702 | + | |
| 703 | + | |
| 704 | + | |
| 705 | + | |
| 706 | + | |
| 707 | + | |
| 708 | + | |
| 709 | + | |
| 710 | + | |
| 711 | + | |
| 712 | + | |
| 713 | + | |
| 714 | + | |
| 715 | + | |
| 716 | + | |
667 | 717 | | |
668 | 718 | | |
669 | | - | |
| 719 | + | |
670 | 720 | | |
671 | 721 | | |
672 | 722 | | |
673 | 723 | | |
674 | | - | |
675 | | - | |
676 | | - | |
677 | | - | |
| 724 | + | |
| 725 | + | |
| 726 | + | |
| 727 | + | |
678 | 728 | | |
679 | 729 | | |
680 | 730 | | |
| |||
0 commit comments