console: media repeat maps store refusals only in the race window
The repeat path answers 402 store_writes_denied or 409 store_changed only when the row failed at or after this request read the store record. A job that failed before the request began answers 200 with the failed job, its own error saying why, so a poll never meets "then try again" for good (controller ruling after fix round 2).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2 files changed,
+72
−6
internal/console/apimedia.go+12 −6internal/console/apimedia_test.go+60 −0
CI failed — run details
diff --git a/internal/console/apimedia.go b/internal/console/apimedia.go| index 6b28ac589..b5fdb74b4 100644 |
| --- a/internal/console/apimedia.go |
| +++ b/internal/console/apimedia.go |
| @@ -193,6 +193,9 @@ func (s *Server) handleAPIMediaSubmit(w http.ResponseWriter, r *http.Request) { |
| return |
| } |
| ctx := r.Context() |
| + // readAt is when mediaRepeat reads the store record, to the millisecond |
| + // ended_at is stamped in. |
| + readAt := s.now().Truncate(time.Millisecond) |
| job, found, src, err := s.mediaRepeat(ctx, acct.Sqid, app, body.Key, preset, body.Class) |
| if err != nil { |
| mediaInternal(w, "repeat lookup "+acct.Sqid+"/"+app, err) |
| @@ -200,14 +203,17 @@ func (s *Server) handleAPIMediaSubmit(w http.ResponseWriter, r *http.Request) { |
| } |
| if found { |
| // The repeat path read the store record once, without the lock. A |
| - // dispatch that failed the row on a store refusal since then saw a |
| - // newer store than that read: answer the refusal the ordered path |
| - // would give, not 200 (Astra fix round 2). Any other row answers |
| - // as it is. |
| + // dispatch that failed the row on a store refusal at or after that |
| + // read saw a newer store than it: answer the refusal the ordered |
| + // path would give, not 200 (Astra fix round 2). A row that had |
| + // already failed before this request began answers 200 as it is, |
| + // its own error saying why, so a poll never meets "try again" |
| + // forever. Any other row answers as it is. |
| + raced := job.State == "failed" && !job.EndedAt.Before(readAt) |
| switch { |
| - case job.State == "failed" && job.ErrorCode == media.CodeStoreWritesDenied: |
| + case raced && job.ErrorCode == media.CodeStoreWritesDenied: |
| mediaRefuse(w, http.StatusPaymentRequired, media.CodeStoreWritesDenied, mediaText("media.store_writes_denied")) |
| - case job.State == "failed" && job.ErrorCode == media.CodeStoreChanged: |
| + case raced && job.ErrorCode == media.CodeStoreChanged: |
| mediaRefuse(w, http.StatusConflict, media.CodeStoreChanged, mediaText("media.job.store_changed")) |
| default: |
| writeJSON(w, http.StatusOK, s.mediaAnswer(job)) |
diff --git a/internal/console/apimedia_test.go b/internal/console/apimedia_test.go| index 81c92bcf5..ffb4c08a9 100644 |
| --- a/internal/console/apimedia_test.go |
| +++ b/internal/console/apimedia_test.go |
| @@ -1028,3 +1028,63 @@ func TestRepeatPathAnswersAStoreRefusalDispatchMadeMeanwhile(t *testing.T) { |
| }) |
| } |
| } |
| + |
| +// TestRepeatOfAJobThatFailedEarlierAnswersTheJob (controller ruling after |
| +// fix round 2): a job dispatch failed on a store refusal before the |
| +// request began is answered 200 with the failed job, its own error saying |
| +// why, on every later poll; the refusal mapping is for the race window |
| +// only. |
| +func TestRepeatOfAJobThatFailedEarlierAnswersTheJob(t *testing.T) { |
| + for _, tc := range []struct { |
| + name string |
| + change func(f *mediaDoorFixture) |
| + undo func(f *mediaDoorFixture) |
| + code string |
| + msg string |
| + }{ |
| + {"store_writes_denied", func(f *mediaDoorFixture) { |
| + if _, err := objstore.SetWritesDenied(context.Background(), f.rel, f.sqid, "titogo", "", |
| + &objstore.WritesDenied{Reason: "storage grace ended", At: f.clock}); err != nil { |
| + f.t.Fatal(err) |
| + } |
| + }, func(f *mediaDoorFixture) { |
| + if _, err := objstore.SetWritesDenied(context.Background(), f.rel, f.sqid, "titogo", "", nil); err != nil { |
| + f.t.Fatal(err) |
| + } |
| + }, "store_writes_denied", mediaText("media.store_writes_denied")}, |
| + // The tag is gone and the bucket no longer verifies: dispatch fails |
| + // the row store_changed and the record keeps its incarnation. The |
| + // bucket is then put right and tagged as it was. |
| + {"store_changed", func(f *mediaDoorFixture) { |
| + f.buckets.Tags[f.bucket] = map[string]string{} |
| + facts := goodBucket() |
| + facts.Encryption = "aws:kms" |
| + f.buckets.Facts[f.bucket] = facts |
| + }, func(f *mediaDoorFixture) { |
| + f.buckets.Facts[f.bucket] = goodBucket() |
| + f.buckets.Tags[f.bucket] = map[string]string{ |
| + storecfg.IncarnationTag: storedRecord(f.t, f.consoleFixture, "titogo").Conversion.Incarnation} |
| + }, "store_changed", mediaText("media.job.store_changed")}, |
| + } { |
| + t.Run(tc.name, func(t *testing.T) { |
| + f := newMediaDoor(t, "code-media-earlier-"+tc.name) |
| + sub := f.submit(mediaTestKey) |
| + if sub.status != http.StatusAccepted { |
| + t.Fatalf("submit %d %s", sub.status, sub.raw) |
| + } |
| + tc.change(f) |
| + if err := f.srv.media.Pass(context.Background(), f.clock); err != nil { |
| + t.Fatal(err) |
| + } |
| + tc.undo(f) |
| + for i := 0; i < 3; i++ { |
| + f.clock = f.clock.Add(time.Minute) |
| + r := f.submit(mediaTestKey) |
| + if r.status != http.StatusOK || r.job.ID != sub.job.ID || r.job.State != "failed" || r.job.Error == nil || |
| + *r.job.Error != (mediaJobError{Code: tc.code, Message: tc.msg}) { |
| + t.Fatalf("poll %d: %d %s", i+1, r.status, r.raw) |
| + } |
| + } |
| + }) |
| + } |
| +} |