Skip to content

feat(sqs): seedable create-then-lookup consistency window (#413) - #427

Merged
scttfrdmn merged 3 commits into
mainfrom
feat/413b-sqs-consistency-seed
Aug 1, 2026
Merged

scttfrdmn merged 3 commits into
mainfrom
feat/413b-sqs-consistency-seed

Conversation

@scttfrdmn

Copy link
Copy Markdown
Owner

Part B of #413 — the seedable consistency window. Part A (#426) corrected the error
code these tests assert on and had to land first.

AWS documents that a caller "must wait at least one second after the queue is
created to be able to use the queue", so a real CreateQueue → GetQueueUrl →
retry loop can legitimately observe QueueDoesNotExist for a queue that exists.
Substrate resolved a new queue instantly, which made that retry path unreachable
— the consumer could not exercise it offline at all, so a suite asserting on it
passed without ever running the loop.

The seed

POST   /v1/sqs/consistency   {"queueName":"run-q","getUrlMisses":2}
DELETE /v1/sqs/consistency?queueName=run-q     # or no query to clear all

Keyed by queue name or the "*" wildcard. GetQueueUrl and GetQueueAttributes
have independent counters, both defaulting to 0, so unseeded behavior is
unchanged. An exhausted name-scoped seed falls through to the wildcard, so an empty
named seed cannot mask a wildcard that still has budget.

End to end, matching how the reporter will use it:

POST /v1/sqs/consistency  {"queueName":"run-q","getUrlMisses":2}
CreateQueue run-q          → 200
GetQueueUrl run-q ×2       → 400 QueueDoesNotExist
GetQueueUrl run-q          → 200 + URL

Counts, not durations

The issue explicitly offered "or a clock-duration form"; this is counted in misses
deliberately. TimeController.Now advances with wall time from its baseline —
it is not a frozen, manually-stepped clock — so a duration seed would expire partway
through a test and make "still missing" assertions wall-clock dependent, which
CLAUDE.md forbids outright. A miss counter is exactly reproducible.

emulator/consistency.go is not reusable here for that same reason plus three
others: it is evaluated in server middleware, returns HTTP 409
InconsistentStateException (a different error entirely), and its primaryParam
list has no QueueName candidate.

Two ordering rules, each pinned by a test

  • A miss is consumed only after the queue is known to exist. Seeding before
    CreateQueue is the natural harness order, and consuming budget on a genuinely
    absent queue would silently spend what the test meant to spend on the window.
  • A seed counts down the next N misses rather than re-arming on CreateQueue.
    createQueue is unconditionally idempotent (it early-returns the existing URL),
    so "after its CreateQueue" is ambiguous when create runs twice, and re-arming
    would make the data path write control-plane state.

Concurrency

Consuming a miss is a read-modify-write and StateManager offers no
compare-and-swap (MemoryStateManager is last-write-wins), so concurrent lookups
could each read misses=1 and each write 0, consuming one miss where several were
seeded — a test asserting "exactly N calls fail" would flake.

A striped per-queue mutex closes that window. TestSQS_Consistency_ConcurrentGetQueueUrl
drives 32 goroutines through one barrier and asserts exactly N misses; it fails
3/3 runs with the lock removed
and passes -count=3 with it restored.

sqsQueueMutex deliberately mirrors s3KeyMutex rather than generalizing it:
that type's lock(bucket, key) is two-part by design and its documentation is
written entirely in S3 terms, so sharing would mean changing a signature used at
every S3 conditional-write call site from inside a PR about SQS. Extract a common
type on the third caller — the doc comment says so.

Why the seed goes through StateManager

PluginConfig carries only State, Logger and Options, so a plugin cannot reach
ServerOptions controllers. The side benefit is that POST /v1/state/reset wipes
seeds, which is what a harness expects between cases;
TestSQS_Consistency_ResetClearsSeeds pins it.

Tests

New emulator/sqs_consistency_test.go — 11 tests, the protocol-sensitive ones run
under both query and JSON (SQS is dual-protocol and the reporter reaches it
through an SDK): the two window tests, counter independence, unseeded-is-instant,
absent-queue-does-not-consume, no-re-arm-on-create, wildcard,
name-over-wildcard precedence, clear (by name and all), negative-count rejection,
the concurrency test, and reset.

Verification

gofmt, go build ./..., go vet ./..., golangci-lint run ./... (0 issues),
make test (race), cd test/e2e && go test ./..., make docs-reference-check,
make docs-versions — all clean.

Closes #413.

AWS documents that a caller "must wait at least one second after the queue is
created to be able to use the queue", so a real CreateQueue -> GetQueueUrl ->
retry loop can legitimately observe QueueDoesNotExist for a queue that exists.
Substrate resolved a new queue instantly, which made that retry path unreachable:
the consumer could not exercise it offline at all, so a suite asserting on it
passed without ever running the loop.

POST/DELETE /v1/sqs/consistency seeds how many subsequent lookups report
QueueDoesNotExist for a queue that does exist, keyed by queue name or the "*"
wildcard. GetQueueUrl and GetQueueAttributes have independent counters, both
defaulting to 0, so unseeded behavior is unchanged.

Counted in misses, not measured as a duration. TimeController.Now advances with
wall time from its baseline, so a duration seed would expire partway through a
test and make "still missing" assertions wall-clock dependent, which CLAUDE.md
forbids. A miss counter is exactly reproducible. emulator/consistency.go is not
reusable here for the same reason plus three others: it is evaluated in server
middleware, returns 409 InconsistentStateException, and its primaryParam list has
no QueueName candidate.

Two ordering rules, each pinned by a test:

- A miss is consumed only after the queue is known to exist. Seeding before
  CreateQueue is the natural harness order, and consuming budget on a genuinely
  absent queue would silently spend what the test meant to spend on the window.
- A seed counts down the next N misses rather than re-arming on CreateQueue.
  createQueue is unconditionally idempotent, so "after its CreateQueue" is
  ambiguous when create runs twice, and re-arming would make the data path write
  control-plane state.

An exhausted name-scoped seed falls through to the wildcard, so an empty named
seed cannot mask a wildcard that still has budget.

Consuming a miss is a read-modify-write and StateManager offers no
compare-and-swap (MemoryStateManager is last-write-wins), so concurrent lookups
could each read misses=1 and each write 0, consuming one miss where several were
seeded. A striped per-queue mutex closes that window.
TestSQS_Consistency_ConcurrentGetQueueUrl drives 32 goroutines through one
barrier and asserts exactly N misses; it fails 3/3 runs with the lock removed.
sqsQueueMutex deliberately mirrors s3KeyMutex rather than generalizing it: that
type's lock(bucket, key) is two-part by design and its documentation is written
entirely in S3 terms, so sharing would mean changing a signature used at every S3
conditional-write call site from inside a PR about SQS. Extract a common type on
the third caller.

Seeds go through StateManager because PluginConfig carries only State, Logger and
Options, so a plugin cannot reach ServerOptions controllers. The side benefit is
that POST /v1/state/reset wipes them, which is what a harness expects between
cases; TestSQS_Consistency_ResetClearsSeeds pins it.

Closes #413.
@codecov

codecov Bot commented Aug 1, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 91.56627% with 7 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
emulator/sqs_control.go 90.41% 4 Missing and 3 partials ⚠️

📢 Thoughts on this report? Let us know!

Raised by codecov/patch on #427 at 60.24% against a 74.09% target. The uncovered
lines were the state-failure branches, and they turn out to be worth testing on
their own merits rather than as a coverage exercise.

QueueDoesNotExist is a 400 telling a caller the queue is gone and to stop
retrying; a store failure is transient and should be retried. Collapsing the
latter into the former sends a consumer down a permanent-failure path over a
blip. That distinction matters more here than usual, because the seed added in
this PR exists precisely so consumers can test their retry loops — getting it
backwards would undercut the feature. Same argument as
TestS3_StateFailureIsNotReportedAsNoSuchBucket (#392), and the failing-store
double follows errAfterGetsStateManager from that work.

Verified load-bearing: collapsing consumeQueueMiss's Get error into
"(false, nil)" makes both subtests fail with 200 where 500 is expected.

Also covers the control plane's own error paths. A seed that silently fails to
persist is worse than no seed: the harness proceeds believing the window is
armed, every lookup succeeds, and the retry assertion passes vacuously — exactly
the failure mode #413 was filed about. Four rows: seed Put, clear-one Delete,
clear-all List, clear-all Delete. Plus a malformed request body, a corrupt stored
seed (which must not read as "no misses left"), and a table over
sqsQueueNameFromURL's degenerate inputs.

newSQSTestServer now delegates to newSQSTestServerWithState so a test can
substitute a failing store; the no-argument signature is unchanged, so the ~40
existing call sites are untouched. Two export_test.go helpers write a seed
directly into state, which is needed to arrange one behind a store whose Put or
Delete is rigged to fail.

Patch coverage 60.24% -> 96.92%. The four statements still uncovered are
json.Marshal on a struct of two ints and a string, which cannot fail.
@scttfrdmn
scttfrdmn merged commit 54a917b into main Aug 1, 2026
15 checks passed
@scttfrdmn scttfrdmn mentioned this pull request Aug 1, 2026
@scttfrdmn
scttfrdmn deleted the feat/413b-sqs-consistency-seed branch August 2, 2026 22:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SQS: seedable eventual-consistency delay on GetQueueUrl after CreateQueue

1 participant