Repository navigation
feat(s3): a seedable 409 ConditionalRequestConflict on conditional writes (#540) - #583
Merged
Merged
Conversation
…ites (#540) S3 conditional writes could reach two of the three outcomes AWS documents — 412 PreconditionFailed and 404 NoSuchKey — but never the 409 returned when a concurrent operation interferes. The three are not interchangeable: a 412 means re-read, recompute and retry the compare-and-swap; a 409 on PutObject means retry the request as-is; a 409 on CompleteMultipartUpload means the upload ID is finished and the caller must re-do CreateMultipartUpload and every UploadPart. A CAS loop that answers the third case like the first spins until it gives up, and against Substrate that loop looked correct. POST /v1/s3/conditional-conflict seeds how many subsequent conditional writes to a key report the code, keyed bucket/key or by the "*" wildcard, with independent counters per operation; DELETE clears one or all. Follows the established seed pattern: key-scoped consulted before the wildcard, falling through when exhausted, counted in occurrences rather than measured as a duration. Consuming a seeded multipart conflict invalidates the upload, so a same-ID retry gets NoSuchUpload and ListParts on that ID is gone. That is why this is a plugin-level seed and not a FaultController rule as the issue proposed: faults are evaluated at step 4.5, before a request reaches its plugin, so a faulted Complete writes no state and its upload ID stays completable — reproducing the code but not the consequence, and passing the very loop the code exists to catch. Nothing changes for an unseeded caller. A conflict is consumed only after the preconditions pass, so a 412 or 404 is still reported as itself and does not spend the budget, and an unconditional write to a seeded key is untouched, since AWS documents the code only on the If-Match and If-None-Match members. Consumption takes a dedicated mutex rather than the per-key stripe the caller already holds: the wildcard seed is shared across keys, so two writes to different keys hold different stripes and could double-consume one conflict. Lock order is always key-stripe then seed mutex.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The gap
S3 conditional writes in Substrate could produce two of the three outcomes AWS
documents —
412 PreconditionFailedand404 NoSuchKey— but never the409 ConditionalRequestConflictS3 returns when a concurrent operation interferes witha conditional write between its evaluation and its completion.
The three are not interchangeable, and the recovery each demands differs:
412 PreconditionFailed404 NoSuchKeyonIf-Match409onPutObject/CopyObjectIf-Match, fetch the current ETag first)409onCompleteMultipartUploadCreateMultipartUploadand everyUploadPartA CAS loop that answers the last case like the first re-sends Complete with an
upload ID that can never complete again and spins until it gives up. Against
Substrate that loop looked correct, because only 412 was reachable.
Why a seed, and not the fault rule the issue proposed
#540 prefers making the code fault-injectable. Measured: that produces the code
but not its consequence.
InjectFaultruns at step 4.5 (server.go:636),before
registry.RouteRequest, so a fault never reaches the plugin and writes nostate. With a rule armed on
CompleteMultipartUpload, the first Complete returnedthe injected 409 and the immediate retry with the same upload ID returned 200
with
ETag "0b5160c2adf21a2967b0bb17bb704b12-1"— passing the exact loop the codeexists to catch. (Control: a genuinely-successful Complete retried gives
NoSuchUpload.) Full evidence in#540 (comment).
So this is a plugin-level seed, which reaches the plugin and can therefore call
abortUploadState. The issue's option 1 (a per-key recently-deleted window) isrejected for the reason the issue itself gives: it makes Substrate keep state to
model a timing accident.
What this adds
POST /v1/s3/conditional-conflictseeds how many subsequent conditional writes toa key report the code;
DELETEclears one (?bucket=&key=) or all.curl -X POST localhost:4566/v1/s3/conditional-conflict \ -d '{"bucket":"cond","key":"big","completeConflicts":1}'Keyed
bucket/keyor by the*wildcard, with independent counters per operation(
putConflicts,copyConflicts,completeConflicts) so a fixture can seed themultipart arm — the one with the expensive recovery — without perturbing plain
writes. It follows the established seed pattern (
sqs_control.go) down to itsreasoning: key-scoped consulted before the wildcard, falling through when
exhausted so a spent key-scoped seed cannot mask a funded wildcard, and counted
in occurrences rather than measured as a duration, because
TimeController.Nowadvances with wall time and a duration window would make assertions wall-clock
dependent.
Consuming a seeded multipart conflict invalidates the upload. A same-ID retry
gets
NoSuchUpload, andListPartson that ID is gone. This deliberatelycontradicts the 412 arm, which leaves the upload open, and the comment at the call
site explains why: AWS documents the 409 recovery as re-initiating and re-uploading
every part, advice only meaningful if the old ID cannot complete. The inference from
that advice to "the ID is dead" is Substrate's, and the comment says so.
Nothing changes for an unseeded caller
evaluateWritePreconditionspasses. A 412or 404 is a determinate observation of the destination's current state, is still
reported as itself, and does not spend the budget — otherwise a request that
was going to fail anyway silently consumes what the test armed for the conflict.
documents this code only on the two conditional headers.
bucketandkeymust be given together (a400otherwise), because such aseed would be stored under a key no write can match: it would look armed, never
fire, and the fixture would pass vacuously.
Provenance
The code and the
409status are documented — they appear in theIfMatchandIfNoneMatchmember docs for all three operations, and in the user guide'sconditional-writes page. No message text is documented anywhere: the model
carries no
ConditionalRequestConflictshape and lists the code in no operation'serrors. Perdocs/fidelity.mdthe message is Substrate's own and the commentbeside the literal says so. Assert on the code and the status.
Concurrency
Consumption takes a dedicated mutex rather than the per-key stripe the caller
already holds. The stripe is chosen by hashing bucket/key, but the wildcard seed is
shared across every key, so two concurrent writes to different keys hold
different stripes and could both read a budget of 1 and both decrement — the
read-modify-write hazard
s3KeyMutexexists to close, sinceStateManagerhas nocompare-and-swap. Lock order is always key-stripe → seed mutex.
The same window exists in
consumeQueueMisson the SQS side; filed separately as#582 rather than fixed here.
Tests
Twelve tests in
emulator/s3_conditional_conflict_test.go. The headline gate seedsthe multipart arm, asserts 409, then asserts the same-ID retry is
NoSuchUploadwith
ListPartsand the upload listing gone — plus its control, that a 412'dComplete still leaves the upload open. Also: both headers on the Put arm, Copy, the
counters' independence, key-scoped-beats-wildcard and the fall-through, an
unconditional write untouched, a failed precondition keeping its budget, a refused
write leaving the object byte-identical, the bare
<Error>wire shape (a wrappeddocument would hide the code from an SDK), and the endpoint's validation.
Mutation tested, 7 mutants, all CAUGHT:
abortUploadStatedropped from theComplete arm; the seed consulted before the preconditions;
continue→returnonan exhausted counter; the
cond.any()gate removed; the decrement dropped;StatusConflict→StatusPreconditionFailed; the bucket-XOR-key validationdropped.
Verification
gofmt -l .,go build ./...,go vet ./...,golangci-lint run ./...(0issues),
make test(race),make docs-reference docs-reference-check docs-versions,npm --prefix docs run build,go vet -C test/e2e ./...,go test -C test/e2e ./...— all clean.End to end against
substrate server --address :4621with the real AWS CLI:The plain arm (409 then 200), the copy arm, an unconditional write to a seeded key
(200, budget unspent), and both
400validations were checked the same way.Docs
docs/services.md's "Conditional writes" section gains the outcome, the recoverytable, the endpoint with
curlexamples, the precedence andcounted-in-occurrences rules, and the upload-invalidation asymmetry. The paragraph
claiming 409 is "deliberately not emulated" is gone — it is now false. The three
operation rows cross-reference it.
Closes #540