console: a held platform promise fails only its own ref
Task 8 fix round 2 (ruling C). finishPendingMints passes over a held promise, logging its operator line, so the gate and every write door go on. The promise keeps its slot and ref; only a create for its ref answers create.failed, writing nothing. A create whose own fresh promise is refused before the mint wrote anything withdraws that promise, so a refused create leaves nothing behind and one held id cannot fill the cap with stuck promises.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
3 files changed,
+170
−48
internal/console/apiplatformcustomers.go+62 −19internal/console/apiplatformcustomers_test.go+99 −29internal/console/platform_store.go+9 −0
diff --git a/internal/console/apiplatformcustomers.go b/internal/console/apiplatformcustomers.go| index c2792b5b0..32f67ab50 100644 |
| --- a/internal/console/apiplatformcustomers.go |
| +++ b/internal/console/apiplatformcustomers.go |
| @@ -181,10 +181,11 @@ func (s *Server) platformAuthorize(w http.ResponseWriter, r *http.Request, failR |
| // platformGateFor is the gate every platform door but create runs: |
| // platformAuthorize, then, when the platform has pending mints, they are |
| // finished under the platform lifecycle lock, released before the handler |
| -// runs (the lock is not re-entrant). failRow is the caller's door's own |
| -// failure row (pre-flight C10): on a write door a finish that fails is 502 |
| -// with it; with failRow "" (a read door) it is logged and the request goes |
| -// on. One indexed query when there are none. |
| +// runs (the lock is not re-entrant). A held promise is passed over and the |
| +// door goes on (fix round 2). failRow is the caller's door's own failure |
| +// row (pre-flight C10): on a write door a finish that fails for any other |
| +// reason is 502 with it; with failRow "" (a read door) it is logged and the |
| +// request goes on. One indexed query when there are none. |
| // |
| // Create runs platformAuthorize alone and finishes under its own hold of |
| // the lock, after it has validated its body, so a refused create writes |
| @@ -444,12 +445,29 @@ func (s *Server) handlePlatformCustomerCreate(w http.ResponseWriter, r *http.Req |
| return |
| } |
| - // Step 3. |
| + // Step 3. A held promise is passed over (Task 8 fix round 2), so it |
| + // fails only a create for its own ref, here: that create writes nothing. |
| if err := s.finishPendingMints(ctx, platform); err != nil { |
| log.Printf("console: platform %s: create customer: finish pending creates: %v", platform.Sqid, err) |
| s.platformDoorFailed(w, c) |
| return |
| } |
| + if req.Ref != "" { |
| + pending, err := s.store.PendingPlatformMints(ctx, platform.Sqid) |
| + if err != nil { |
| + log.Printf("console: platform %s: create customer: pending creates: %v", platform.Sqid, err) |
| + s.platformDoorFailed(w, c) |
| + return |
| + } |
| + for _, m := range pending { |
| + if m.Ref == req.Ref { |
| + log.Printf("console: platform %s: create customer for ref %q refused: pending create %s holds the ref "+ |
| + "and is held until an operator repairs it", platform.Sqid, req.Ref, m.MintID) |
| + s.platformDoorFailed(w, c) |
| + return |
| + } |
| + } |
| + } |
| // Step 4. |
| if req.Ref != "" { |
| @@ -500,6 +518,14 @@ func (s *Server) handlePlatformCustomerCreate(w http.ResponseWriter, r *http.Req |
| // Step 7. |
| cust, err := s.finishPlatformMint(ctx, platform, m) |
| if err != nil { |
| + if errors.Is(err, errMintNotWritten) { |
| + // Refused before anything named it in the bucket: this |
| + // request's own promise is withdrawn, so a refused create |
| + // leaves nothing behind and holds no slot. |
| + if uerr := s.store.unreservePlatformMint(ctx, platform.Sqid, m.MintID); uerr != nil { |
| + log.Printf("console: platform %s: create customer: withdraw pending create %s: %v", platform.Sqid, m.MintID, uerr) |
| + } |
| + } |
| s.platformDoorFailed(w, c) |
| return |
| } |
| @@ -556,21 +582,31 @@ var errPlatformMintEnded = errors.New("console: the pending customer create ende |
| // errPlatformMintHeld is a pending mint this console refuses to finish |
| // because the bucket and the console disagree about it: the id the bucket |
| // would give it is held by another account, or its ref by another live |
| -// customer. Nothing is written; the promise stays, holding its slot and |
| -// ref, until an operator repairs the bucket or the database. |
| +// customer. The promise stays, holding its slot and ref, until an operator |
| +// repairs the bucket or the database. It fails only requests about itself |
| +// (Task 8 fix round 2): finishPendingMints passes over it, and only a |
| +// create for its ref is refused. |
| var errPlatformMintHeld = errors.New("console: the pending customer create is held until an operator repairs the bucket") |
| +// errMintNotWritten marks a held refusal made before the mint wrote |
| +// anything to the bucket, so nothing outside the database names the |
| +// promise. Create withdraws its own fresh promise on it. |
| +var errMintNotWritten = errors.New("console: nothing was written for the pending customer create") |
| + |
| // finishPendingMints finishes every pending mint of the platform, oldest |
| // first. The caller holds s.lockPlatformLifecycle(platform.Sqid). A mint |
| -// that ended without an account (errPlatformMintEnded) is past and the next |
| -// is finished; any other failure stops it, and is returned (Astra r1-1). |
| +// that ended without an account (errPlatformMintEnded) is past, and a held |
| +// one (errPlatformMintHeld, its operator line already logged) is passed |
| +// over: one bad record must not stop the platform's other writes (Task 8 |
| +// fix round 2). Any other failure stops it, and is returned (Astra r1-1). |
| func (s *Server) finishPendingMints(ctx context.Context, platform *Account) error { |
| pending, err := s.store.PendingPlatformMints(ctx, platform.Sqid) |
| if err != nil { |
| return err |
| } |
| for _, m := range pending { |
| - if _, err := s.finishPlatformMint(ctx, platform, m); err != nil && !errors.Is(err, errPlatformMintEnded) { |
| + if _, err := s.finishPlatformMint(ctx, platform, m); err != nil && |
| + !errors.Is(err, errPlatformMintEnded) && !errors.Is(err, errPlatformMintHeld) { |
| return err |
| } |
| } |
| @@ -596,7 +632,11 @@ func (s *Server) finishPendingMints(ctx context.Context, platform *Account) erro |
| // no manifest and no row are written, one line for an operator names the |
| // mint id, the account id and the reason, and the promise stays, holding |
| // its slot and ref. Repairing silently is how an identity, a deletion or a |
| -// ref gets lost. The cases: |
| +// ref gets lost. A held promise fails only requests about itself: |
| +// finishPendingMints passes over it, and create refuses only a create for |
| +// its ref (fix round 2). A refusal made before the mint wrote anything is |
| +// also errMintNotWritten, on which create withdraws its own fresh promise, |
| +// so a refused create leaves nothing behind. The cases: |
| // |
| // - the id the bucket would give the mint is held by an account in this |
| // console (live or deleted, any platform): MintLinkedAccountIfFree asks |
| @@ -613,11 +653,14 @@ func (s *Server) finishPlatformMint(ctx context.Context, platform *Account, m Pe |
| if s.cfg.Releases == nil { |
| return nil, fmt.Errorf("pending customer create %s: no deployment bucket", m.MintID) |
| } |
| - held := func(id int64, reason string) error { |
| + held := func(id int64, written bool, reason string) error { |
| log.Printf("console: platform %s: pending customer create %s held: account %d %s; it stays pending, "+ |
| - "and the platform's writes fail, until an operator repairs the bucket or the console", platform.Sqid, |
| - m.MintID, id, reason) |
| - return fmt.Errorf("%w: mint %s, account %d: %s", errPlatformMintHeld, m.MintID, id, reason) |
| + "holding its ref, until an operator repairs the bucket or the console", platform.Sqid, m.MintID, id, reason) |
| + err := fmt.Errorf("%w: mint %s, account %d: %s", errPlatformMintHeld, m.MintID, id, reason) |
| + if !written { |
| + err = fmt.Errorf("%w (%w)", err, errMintNotWritten) |
| + } |
| + return err |
| } |
| if m.Ref != "" { |
| holder, holderMint, ok, err := s.store.refHolder(ctx, platform.Sqid, m.Ref) |
| @@ -626,7 +669,7 @@ func (s *Server) finishPlatformMint(ctx context.Context, platform *Account, m Pe |
| return nil, err |
| } |
| if ok && holderMint != m.MintID { |
| - return nil, held(holder, fmt.Sprintf("holds ref %q and was not made by this create", m.Ref)) |
| + return nil, held(holder, false, fmt.Sprintf("holds ref %q and was not made by this create", m.Ref)) |
| } |
| } |
| if err := s.platformStep("mint"); err != nil { |
| @@ -647,7 +690,7 @@ func (s *Server) finishPlatformMint(ctx context.Context, platform *Account, m Pe |
| return nil |
| }) |
| if errors.Is(err, errLinkedIDTaken) { |
| - return nil, held(heldID, "is in this console with no manifest in the bucket, and the bucket gives its id to this create") |
| + return nil, held(heldID, false, "is in this console with no manifest in the bucket, and the bucket gives its id to this create") |
| } |
| if err != nil { |
| log.Printf("console: platform %s: pending customer create %s (%q): mint: %v", platform.Sqid, m.MintID, m.Name, err) |
| @@ -673,9 +716,9 @@ func (s *Server) finishPlatformMint(ctx context.Context, platform *Account, m Pe |
| platform.Sqid, m.MintID, sqid) |
| return nil, fmt.Errorf("%w: %w", errPlatformMintEnded, err) |
| case errors.Is(err, errLinkedIDTaken): |
| - return nil, held(id, "is another account in this console, and the manifest carrying this create sits at its id") |
| + return nil, held(id, true, "is another account in this console, and the manifest carrying this create sits at its id") |
| case errors.Is(err, ErrPlatformRefTaken): |
| - return nil, held(id, fmt.Sprintf("would take ref %q, which another live customer holds", m.Ref)) |
| + return nil, held(id, true, fmt.Sprintf("would take ref %q, which another live customer holds", m.Ref)) |
| default: |
| log.Printf("console: platform %s: minted customer %s (id %d, pending create %s) into the bucket but "+ |
| "could not record it: %v; the next platform request finishes it", platform.Sqid, sqid, id, m.MintID, err) |
diff --git a/internal/console/apiplatformcustomers_test.go b/internal/console/apiplatformcustomers_test.go| index 510959ad9..0832374ce 100644 |
| --- a/internal/console/apiplatformcustomers_test.go |
| +++ b/internal/console/apiplatformcustomers_test.go |
| @@ -954,21 +954,34 @@ func wantNoManifest(t *testing.T, fx *consoleFixture, id int64) { |
| } |
| } |
| +// wantNothingFor fails if a refused create left a promise, a manifest at |
| +// the held id, or anything naming the platform in the bucket. |
| +func wantNothingFor(t *testing.T, fx *consoleFixture, platform string, id int64, pending int) { |
| + t.Helper() |
| + if p := pendingMints(t, fx.st, platform); len(p) != pending { |
| + t.Fatalf("pending mints %+v, want %d", p, pending) |
| + } |
| + wantNoManifest(t, fx, id) |
| + if ms := platformManifests(t, fx.rel, platform); len(ms) != 0 { |
| + t.Fatalf("manifests naming the platform %+v, want none", ms) |
| + } |
| +} |
| + |
| // TestCustomerFinishRefusesAnIDHeldByAnotherAccount: the bucket would give |
| // the create an id this console holds for an unlinked account whose |
| -// manifest the bucket lacks. Nothing is written, the promise stays, and |
| -// every retry answers create.failed until an operator repairs it (Task 8 |
| -// fix round 1, ruling A). |
| +// manifest the bucket lacks. Nothing is written (ruling A): no manifest, no |
| +// row, and the create's own fresh promise is withdrawn, so a refused create |
| +// leaves nothing behind (fix round 2). Every retry answers create.failed |
| +// until an operator repairs it. |
| func TestCustomerFinishRefusesAnIDHeldByAnotherAccount(t *testing.T) { |
| fx, acct, tok, next := heldIDFixture(t) |
| if err := fx.st.CreateAccount(next, domains.Sqid(next), "other", "other@example.com", time.Now()); err != nil { |
| t.Fatal(err) |
| } |
| - for range 2 { |
| - status, out, _ := createCustomer(t, fx.ts.URL, acct.Sqid, tok, map[string]any{"name": "shop", "ref": "4812"}) |
| + for _, ref := range []string{"4812", "4812", "4813", ""} { |
| + status, out, _ := createCustomer(t, fx.ts.URL, acct.Sqid, tok, map[string]any{"name": "shop", "ref": ref}) |
| wantPlatformError(t, "create over a held id", status, out, http.StatusBadGateway, "create.failed") |
| - wantHeldPromise(t, fx, acct.Sqid, "4812", 1) |
| - wantNoManifest(t, fx, next) |
| + wantNothingFor(t, fx, acct.Sqid, next, 0) |
| } |
| other, err := fx.st.AccountBySqid(domains.Sqid(next)) |
| if err != nil || other == nil || other.Name != "other" { |
| @@ -977,10 +990,9 @@ func TestCustomerFinishRefusesAnIDHeldByAnotherAccount(t *testing.T) { |
| if linked, _, err := fx.st.AccountPlatform(context.Background(), next); err != nil || linked != "" { |
| t.Fatalf("the other account's link %q %v, want none", linked, err) |
| } |
| - // A read door goes on. |
| if status, out, raw := getPlatform(t, platformCustomersURL(fx.ts.URL, acct.Sqid), tok); status != http.StatusOK || |
| - len(customersIn(t, out)) != 0 || out["count"] != float64(1) { |
| - t.Fatalf("list = %d %s, want no customers and the promise counted", status, raw) |
| + len(customersIn(t, out)) != 0 || out["count"] != float64(0) { |
| + t.Fatalf("list = %d %s, want no customers and nothing counted", status, raw) |
| } |
| } |
| @@ -1001,8 +1013,8 @@ func TestCustomerFinishRefusesAnIDHeldBySamePlatformCustomer(t *testing.T) { |
| t.Fatalf("create over a same-platform customer's id = %d %s, want no customer answered", status, raw) |
| } |
| wantPlatformError(t, "create over a same-platform customer's id", status, out, http.StatusBadGateway, "create.failed") |
| - if p := pendingMints(t, fx.st, acct.Sqid); len(p) != 1 || p[0].Ref != "4812" { |
| - t.Fatalf("pending mints %+v, want the promise for 4812 kept", p) |
| + if p := pendingMints(t, fx.st, acct.Sqid); len(p) != 0 { |
| + t.Fatalf("pending mints %+v, want the refused create's withdrawn", p) |
| } |
| wantNoManifest(t, fx, next) |
| if old, err := fx.st.LinkedCustomer(ctx, acct.Sqid, domains.Sqid(next)); err != nil || old == nil || old.Ref != "4700" || old.Name != "old" { |
| @@ -1015,19 +1027,49 @@ func TestCustomerFinishRefusesAnIDHeldBySamePlatformCustomer(t *testing.T) { |
| // The store holds the same rule: a row at the id finishes the mint only |
| // when it carries the mint's own id. |
| err := fx.st.CreateLinkedAccount(ctx, NewLinkedAccount{ID: next, Sqid: domains.Sqid(next), Name: "shop", |
| - Platform: acct.Sqid, Ref: "4812", MintID: pendingMints(t, fx.st, acct.Sqid)[0].MintID}, time.Now()) |
| + Platform: acct.Sqid, Ref: "4812", MintID: strings.Repeat("cd", 16)}, time.Now()) |
| if !errors.Is(err, errLinkedIDTaken) { |
| t.Fatalf("CreateLinkedAccount over a same-platform customer with another mint id = %v, want errLinkedIDTaken", err) |
| } |
| } |
| -// TestCustomerFinishRefusesARefHeldByAnotherCustomer: a pending mint whose |
| -// ref a live customer took meanwhile (only a manifest written outside this |
| -// console can do that) is not finished, and nothing is written. |
| -func TestCustomerFinishRefusesARefHeldByAnotherCustomer(t *testing.T) { |
| +// TestCustomerHeldIDKeepsAnAcceptedPromise: a promise accepted earlier |
| +// (its first finish failed before writing) that then meets a held id stays |
| +// pending, keeping its slot and ref. It fails only a create for its own ref; |
| +// the platform's reads go on, and a create for another ref is refused only |
| +// because it meets the same held id, and leaves nothing behind. |
| +func TestCustomerHeldIDKeepsAnAcceptedPromise(t *testing.T) { |
| + fx, acct, tok, next := heldIDFixture(t) |
| + fx.srv.platformMintStep = failStep("mint", true) |
| + status, out, _ := createCustomer(t, fx.ts.URL, acct.Sqid, tok, map[string]any{"name": "shop", "ref": "4812"}) |
| + wantPlatformError(t, "create with a failed mint", status, out, http.StatusBadGateway, "create.failed") |
| + if err := fx.st.CreateAccount(next, domains.Sqid(next), "other", "other@example.com", time.Now()); err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := fx.srv.finishPendingMints(context.Background(), acct); err != nil { |
| + t.Fatalf("finishing with a held promise = %v, want nil: the gate goes on", err) |
| + } |
| + if status, out, raw := getPlatform(t, platformCustomersURL(fx.ts.URL, acct.Sqid), tok); status != http.StatusOK || out["count"] != float64(1) { |
| + t.Fatalf("list = %d %s, want the held promise counted", status, raw) |
| + } |
| + for _, ref := range []string{"4812", "4813"} { |
| + status, out, _ = createCustomer(t, fx.ts.URL, acct.Sqid, tok, map[string]any{"name": "shop", "ref": ref}) |
| + wantPlatformError(t, "create "+ref+" behind a held id", status, out, http.StatusBadGateway, "create.failed") |
| + if p := pendingMints(t, fx.st, acct.Sqid); len(p) != 1 || p[0].Ref != "4812" { |
| + t.Fatalf("pending mints %+v, want only the accepted 4812 promise", p) |
| + } |
| + wantNoManifest(t, fx, next) |
| + } |
| +} |
| + |
| +// TestCustomerHeldPromiseFailsOnlyItsOwnRef is fix round 2 (ruling C): a |
| +// promise held because a live customer took its ref fails only a create for |
| +// that ref. The gate goes on, a create for another ref succeeds, and the |
| +// held promise keeps counting against capacity. |
| +func TestCustomerHeldPromiseFailsOnlyItsOwnRef(t *testing.T) { |
| fx := newConsoleFixture(t) |
| acct := planAccount(t, fx, 501) |
| - grantPlatformAccount(t, fx, acct.Sqid, 5) |
| + grantPlatformAccount(t, fx, acct.Sqid, 3) |
| tok := platformToken(t, fx, acct.ID, "cms") |
| ctx := context.Background() |
| if err := fx.st.ReservePlatformMint(ctx, PendingMint{MintID: strings.Repeat("ab", 16), Platform: acct.Sqid, |
| @@ -1039,23 +1081,51 @@ func TestCustomerFinishRefusesARefHeldByAnotherCustomer(t *testing.T) { |
| strings.Repeat("cd", 16), time.Now()); err != nil { |
| t.Fatal(err) |
| } |
| - before, err := fx.rel.List(ctx, "accounts/") |
| + // The gate on every door goes on past it. |
| + if err := fx.srv.finishPendingMints(ctx, acct); err != nil { |
| + t.Fatalf("finishing with a held promise = %v, want nil", err) |
| + } |
| + |
| + // A create for another ref succeeds. |
| + status, out, raw := createCustomer(t, fx.ts.URL, acct.Sqid, tok, map[string]any{"name": "cafe", "ref": "4813"}) |
| + if status != http.StatusCreated || customerIn(t, out)["ref"] != "4813" { |
| + t.Fatalf("create 4813 beside a held promise = %d %s, want 201", status, raw) |
| + } |
| + |
| + // A create for the held ref is refused, writing nothing. |
| + keys := func() []string { |
| + t.Helper() |
| + k, err := fx.rel.List(ctx, "accounts/") |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + return k |
| + } |
| + before := keys() |
| + customersBefore, err := fx.st.LinkedCustomers(ctx, acct.Sqid) |
| if err != nil { |
| t.Fatal(err) |
| } |
| - |
| - // The read door goes on; the write door answers its own row. |
| - status, out, raw := getPlatform(t, platformCustomersURL(fx.ts.URL, acct.Sqid), tok) |
| - if cs := customersIn(t, out); status != http.StatusOK || len(cs) != 1 || cs[0]["name"] != "held" || out["count"] != float64(2) { |
| - t.Fatalf("list = %d %s, want held alone, and the promise counted", status, raw) |
| + status, out, _ = createCustomer(t, fx.ts.URL, acct.Sqid, tok, map[string]any{"name": "shop", "ref": "4812"}) |
| + wantPlatformError(t, "create for the held ref", status, out, http.StatusBadGateway, "create.failed") |
| + if after := keys(); !slices.Equal(after, before) { |
| + t.Fatalf("bucket accounts/ %v, was %v: want nothing written", after, before) |
| + } |
| + if after, err := fx.st.LinkedCustomers(ctx, acct.Sqid); err != nil || len(after) != len(customersBefore) { |
| + t.Fatalf("customers %+v %v, was %+v", after, err, customersBefore) |
| } |
| - status, out, _ = createCustomer(t, fx.ts.URL, acct.Sqid, tok, map[string]any{"name": "cafe", "ref": "4813"}) |
| - wantPlatformError(t, "create behind a held promise", status, out, http.StatusBadGateway, "create.failed") |
| if p := pendingMints(t, fx.st, acct.Sqid); len(p) != 1 || p[0].Ref != "4812" { |
| - t.Fatalf("pending mints %+v, want the 4812 promise kept and no new one", p) |
| + t.Fatalf("pending mints %+v, want the held 4812 promise alone", p) |
| + } |
| + |
| + // The held promise counts: held + its promise + 4813 fill max 3. |
| + status, out, raw = getPlatform(t, platformCustomersURL(fx.ts.URL, acct.Sqid), tok) |
| + if status != http.StatusOK || len(customersIn(t, out)) != 2 || out["count"] != float64(3) { |
| + t.Fatalf("list = %d %s, want two customers and a count of 3", status, raw) |
| } |
| - if after, err := fx.rel.List(ctx, "accounts/"); err != nil || !slices.Equal(after, before) { |
| - t.Fatalf("bucket accounts/ %v %v, was %v: want nothing written", after, err, before) |
| + status, out, _ = createCustomer(t, fx.ts.URL, acct.Sqid, tok, map[string]any{"name": "bakery", "ref": "4814"}) |
| + if status != http.StatusConflict || out["error"] != fmt.Sprintf(platformCopy["create.cap"], 3) { |
| + t.Fatalf("create past the cap = %d %v, want 409 create.cap counting the held promise", status, out) |
| } |
| } |
diff --git a/internal/console/platform_store.go b/internal/console/platform_store.go| index 807742e34..219dc9acf 100644 |
| --- a/internal/console/platform_store.go |
| +++ b/internal/console/platform_store.go |
| @@ -486,6 +486,15 @@ func (s *Store) accountIDHeld(ctx context.Context, id int64) (bool, error) { |
| return n > 0, err |
| } |
| +// unreservePlatformMint withdraws a promise its own create just made and |
| +// could not start: nothing in the bucket names it. Only that create calls |
| +// it (errMintNotWritten); an accepted promise is never withdrawn. |
| +func (s *Store) unreservePlatformMint(ctx context.Context, platformSqid, mintID string) error { |
| + _, err := s.db.ExecContext(ctx, `DELETE FROM platform_pending_mints WHERE platform = ? AND mint_id = ?`, |
| + platformSqid, mintID) |
| + return err |
| +} |
| + |
| // refHolder is the live customer of the platform holding ref: its id and |
| // the mint id its row carries. ok is false when none does. |
| func (s *Store) refHolder(ctx context.Context, platformSqid, ref string) (id int64, mintID string, ok bool, err error) { |