carlos / platform Public

console: fleet seeds door review fixes

Task 15's review (READY AFTER FIXES):

- P2-1: tests for the in-lock ownership re-check (404, record unchanged)
  and the ownership-bound CAS (409), each failing without its guard.
- P3-1: an aborted seed takes only copy-in-storage, and only over no
  reason or FS-30; any other failed report is 409, nothing written.
- P3-4: a file seed's record with a replica assignment is refused with
  the same 404; the begin and upload are pinned never to write one.
- P3-3: the in-request finish's overlap with a box retry is documented
  (no-op transition, idempotent CAS-guarded finish).
- P3-2: the door fixture runs with no data bucket.
- The rolled-back seed's snapshot is deleted by finishSeedOnce alone;
  the door keeps its delete on placed.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Paul Campbell pushed by paul@keymail.dev c4b775e012971747f600deb0e0f00f307f972406 parent 64e6430
4 files changed, +211 −17
  • internal/console/apifleetseeds.go +42 −9
  • internal/console/apifleetseeds_test.go +156 −3
  • internal/console/seedfilefleet_test.go +8 −2
  • internal/provision/fleet_seeded_create_3_census_test.go +5 −3

CI failed — run details

diff --git a/internal/console/apifleetseeds.go b/internal/console/apifleetseeds.go
index e55f117c0..3fc4f157c 100644
--- a/internal/console/apifleetseeds.go
+++ b/internal/console/apifleetseeds.go
@@ -44,8 +44,11 @@ import (
// on that fleet ("fleet/<fleet>") and not retired. Then the seed is a file
// seed, and the caller owns the record: FleetBox names it, or there is no
// FleetBox and the caller is the fleet's sole active box (a file seed's
-// record is created unlabelled, plan G1). A replica assignment grants
-// nothing here: a file seed is placed by its one owning box.
+// record is created unlabelled, plan G1). A record that carries a replica
+// assignment is refused outright: the box grants itself ownership by
+// assignment (provision ownsRecord), but no create path writes one on a
+// file seed's record, so one there is not a record this door knows how to
+// own, and it is answered like any other caller's.
//
// Any failure is false, which every caller answers with the byte-identical
// http.NotFound before anything about the record is revealed. It answers
@@ -69,7 +72,7 @@ func (s *Server) fleetSeedRecord(r *http.Request) (*provision.Record, string, bo
if err != nil || rec == nil || rec.Retired || rec.Region != registry.PlacementTarget(fleet) {
return nil, "", false
}
- if rec.Seed == nil || rec.Seed.Kind != seeds.KindFile {
+ if rec.Seed == nil || rec.Seed.Kind != seeds.KindFile || rec.ReplicaAssignment != nil {
return nil, "", false
}
switch rec.FleetBox {
@@ -110,6 +113,18 @@ func fleetSeedReason(code string) (string, bool) {
return "", false
}
+// failRefreshesAborted is the one failed report an already-aborted fleet
+// file seed takes (decision D6, review P3-1): copy-in-storage, from the
+// rollback that found a copy, refreshing FS-30 and where it is held. It
+// lands only over no reason (a member's or the sweep's abort, which says
+// nothing) or over FS-30 itself, so it never overwrites a reason the
+// console set (FS-17, an upload's failure) or the box set before, and no
+// other code ever clears FS-30. Anything else is the record saying
+// otherwise: 409, nothing written.
+func failRefreshesAborted(seed *provision.SeedSource, code string) bool {
+ return code == provision.SeedCodeCopyInStorage && (seed.Reason == "" || seed.Reason == fileSeedCopyInStorageCopy)
+}
+
// fleetSeedReportValid is the body's grammar for op (plan Task 15): a seed
// id that passes seeds.Check; for failed, a known code, with held exactly
// when the code is copy-in-storage and then one of the three places; for
@@ -195,7 +210,19 @@ func (s *Server) fleetSeedDoor(w http.ResponseWriter, r *http.Request, op string
// record through withAppHost, which takes the same non-reentrant
// mutex. finishSeed checks the seed id and state itself, and its
// CAS protects the record; a finish that fails is the seeds sweep's
- // to retry, and the transition the box reported stands.
+ // to retry, and the transition the box reported stands. The finish
+ // deletes the seed's fleet snapshot too (finishSeedOnce): one place
+ // owns that delete.
+ //
+ // Overlapping finishes are safe (review P3-3). The finish runs in
+ // the request, and a store teardown can take storeTeardownTimeout
+ // (60s) while the box's client and the hub give up at 30s. The
+ // box's retry then finds the record rolled-back: the transition is
+ // a no-op, and a second finishSeed runs beside the first (or beside
+ // the sweep's). Every finish step is idempotent, the store teardown
+ // is under its walk lock, and the record goes only by a CAS on the
+ // etag of a read that saw this seed rolled-back, so the second
+ // either finds work done or loses that CAS and stops.
if err := s.finishSeed(context.WithoutCancel(r.Context()), acct, app, host, body.ID); err != nil {
log.Printf("console: fleet seeds door: %s/%s/%s: finish seed %s: %v (the seeds sweep retries)", acct, app, host, body.ID, err)
}
@@ -227,7 +254,13 @@ func (s *Server) fleetSeedTransition(ctx context.Context, rec *provision.Record,
if seed.State == seeds.StateAborted && seed.Reason == reason && seed.Held == b.Held {
return false, nil
}
- mut = provision.SeedFail(b.ID, reason, b.Held)
+ fail := provision.SeedFail(b.ID, reason, b.Held)
+ mut = func(cur *provision.Record) error {
+ if cur.Seed != nil && cur.Seed.State == seeds.StateAborted && !failRefreshesAborted(cur.Seed, b.Code) {
+ return provision.ErrSeedConflict
+ }
+ return fail(cur)
+ }
case provision.FleetSeedRolledBack:
if seed.State == seeds.StateRolledBack {
return false, nil
@@ -249,10 +282,10 @@ func (s *Server) fleetSeedTransition(ctx context.Context, rec *provision.Record,
if _, err := provision.SetRecordSeed(ctx, s.cfg.Releases, rec.Account, rec.App, rec.Host, owned); err != nil {
return false, err
}
- if op != provision.FleetSeedFailed {
- // Placed or rolled back, the seed's snapshot is spent. Absent is
- // fine; a delete that fails is the seeds sweep's to retry, which
- // deletes a placed or abandoned seed's snapshot.
+ if op == provision.FleetSeedPlaced {
+ // Placed, the seed's snapshot is spent. Absent is fine; a delete
+ // that fails is the seeds sweep's to retry, which deletes a placed
+ // seed's snapshot. A rolled-back seed's goes with its finish.
key := seeds.FleetSnapshotKey(rec.Account, rec.App, rec.Host, b.ID)
if err := s.cfg.Releases.Delete(ctx, key); err != nil && !errors.Is(err, os.ErrNotExist) {
log.Printf("console: fleet seeds door: %s: delete the snapshot of seed %s: %v (the seeds sweep retries)", rec.Host, b.ID, err)
diff --git a/internal/console/apifleetseeds_test.go b/internal/console/apifleetseeds_test.go
index 4bda6a5eb..98bd8f5ef 100644
--- a/internal/console/apifleetseeds_test.go
+++ b/internal/console/apifleetseeds_test.go
@@ -9,11 +9,13 @@ import (
"net/http"
"os"
"strings"
+ "sync/atomic"
"testing"
"time"
"github.com/carlosframework/platform/internal/provision"
"github.com/carlosframework/platform/internal/release"
+ "github.com/carlosframework/platform/internal/replicas"
"github.com/carlosframework/platform/internal/seeds"
)
@@ -24,8 +26,8 @@ import (
const fsHost = "seeded.shop.example.com"
// fleetSeedFx is the interactive world's fleets (home: b1 active, b2
-// detached; away: a1), with a data bucket for the finish, and lifetimes
-// on or off.
+// detached; away: a1), with no data bucket (a fleet file seed's finish
+// needs none, review P3-2), and lifetimes on or off.
type fleetSeedFx struct {
fx *interactiveFx
b1, b2, a1 string
@@ -37,7 +39,7 @@ func newFleetSeedFx(t *testing.T, lifetimesOn bool) fleetSeedFx {
clock := useLifetimesClock(t)
fx := newIxFx(t, "code-fleet-seeds")
b1, b2, a1 := ixFleetWorld(t, fx, fx.acct)
- fx.srv.cfg.SeedData = release.NewDirStore(t.TempDir())
+ fx.srv.cfg.SeedData = nil
if lifetimesOn {
turnLifetimesOn(t, fx.srv, clock)
}
@@ -465,3 +467,154 @@ func TestFleetSeedDoorRolledBackOnlyFromAborted(t *testing.T) {
}
}
}
+
+// ---- review fixes ---------------------------------------------------------
+
+// P2-1: ownership is checked again under the record lock, and the CAS is
+// bound to it. reads counts Gets of the record's key: the first is the
+// door's look before the lock, the second its look under it, the third
+// the CAS's own read.
+func TestFleetSeedDoorRechecksOwnershipUnderLock(t *testing.T) {
+ for _, c := range []struct {
+ name string
+ at int32
+ code int
+ }{
+ {"a second active box before the locked look", 2, http.StatusNotFound},
+ {"relabelled before the CAS", 3, http.StatusConflict},
+ } {
+ t.Run(c.name, func(t *testing.T) {
+ f := newFleetSeedFx(t, false)
+ id := f.seed(t, fsHost, "fleet/home", "", seeds.KindFile, seeds.StateReady)
+ before := f.raw(t, fsHost)
+ ds := f.fx.rel.(*deletableStore)
+ key := provision.RecordKey(f.fx.acct, "shop", fsHost)
+ reg, err := openRegistry(f.fx.regPath)
+ if err != nil {
+ t.Fatal(err)
+ }
+ var reads atomic.Int32
+ var after []byte
+ ds.setGetHook(func(ctx context.Context, k string) error {
+ if k != key || reads.Add(1) != c.at {
+ return nil
+ }
+ if c.at == 2 {
+ return reg.SetFleetBoxStatus("home", "b2", "active")
+ }
+ raw, etag, err := ds.Store.Get(ctx, key)
+ if err != nil {
+ return err
+ }
+ var rec provision.Record
+ if err := json.Unmarshal(raw, &rec); err != nil {
+ return err
+ }
+ rec.FleetBox = "b2"
+ if after, err = json.Marshal(rec); err != nil {
+ return err
+ }
+ _, err = ds.Store.Put(ctx, key, after, etag)
+ return err
+ })
+ t.Cleanup(func() { ds.setGetHook(nil) })
+ code, raw := f.post(t, fsHost, "placed", "home/b1", f.b1, fsBody{"id": id})
+ ds.setGetHook(nil)
+ if code != c.code || reads.Load() < c.at {
+ t.Fatalf("%d %q after %d reads, want %d", code, raw, reads.Load(), c.code)
+ }
+ if c.code == http.StatusNotFound && string(raw) != "404 page not found\n" {
+ t.Fatalf("404 body %q", raw)
+ }
+ want := before
+ if after != nil {
+ want = after
+ }
+ if got := f.raw(t, fsHost); !bytes.Equal(got, want) {
+ t.Fatalf("the record changed:\n%s\n%s", want, got)
+ }
+ if !f.hasSnapshot(t, fsHost, id) {
+ t.Fatal("a refused placed deleted the snapshot")
+ }
+ })
+ }
+}
+
+// P3-1: an aborted seed takes only copy-in-storage, and only over no
+// reason or FS-30; nothing else is written over it. Another code is the
+// record saying otherwise: 409.
+func TestFleetSeedDoorFailedOnAbortedOnlyRefreshesStorage(t *testing.T) {
+ f := newFleetSeedFx(t, false)
+ n := 0
+ aborted := func(t *testing.T, reason, held string) (string, string) {
+ t.Helper()
+ n++
+ host := "ab" + string(rune('a'+n)) + ".shop.example.com"
+ id := f.seed(t, host, "fleet/home", "", seeds.KindFile, seeds.StateReady)
+ if _, err := provision.SetRecordSeed(context.Background(), f.fx.rel, f.fx.acct, "shop", host, provision.SeedFail(id, reason, held)); err != nil {
+ t.Fatal(err)
+ }
+ return host, id
+ }
+ storage := func(held string) func(string) fsBody {
+ return func(id string) fsBody { return fsBody{"id": id, "code": provision.SeedCodeCopyInStorage, "held": held} }
+ }
+ code := func(c string) func(string) fsBody {
+ return func(id string) fsBody { return fsBody{"id": id, "code": c} }
+ }
+ for _, c := range []struct {
+ what, reason, held string
+ body func(id string) fsBody
+ code int
+ wantReason string
+ unchanged bool
+ }{
+ {"copy-in-storage over a member's abort", "", "", storage(provision.SeedHeldBoth), http.StatusOK, fileSeedCopyInStorageCopy, false},
+ {"copy-in-storage refreshes FS-30", fileSeedCopyInStorageCopy, provision.SeedHeldReplica, storage(provision.SeedHeldStaging), http.StatusOK, fileSeedCopyInStorageCopy, false},
+ {"copy-in-storage over FS-17", fileSeedMismatchCopy, "", storage(provision.SeedHeldBoth), http.StatusConflict, fileSeedMismatchCopy, true},
+ {"copy-in-storage over a box's FS-29", fileSeedCopyExistsCopy, "", storage(provision.SeedHeldBoth), http.StatusConflict, fileSeedCopyExistsCopy, true},
+ {"mismatch over FS-30", fileSeedCopyInStorageCopy, provision.SeedHeldReplica, code(provision.SeedCodeMismatch), http.StatusConflict, fileSeedCopyInStorageCopy, true},
+ {"copy-exists over a member's abort", "", "", code(provision.SeedCodeCopyExists), http.StatusConflict, "", true},
+ {"the same mismatch again", fileSeedBoxMismatchCopy, "", code(provision.SeedCodeMismatch), http.StatusOK, fileSeedBoxMismatchCopy, true},
+ } {
+ host, id := aborted(t, c.reason, c.held)
+ before := f.raw(t, host)
+ got, raw := f.post(t, host, "failed", "home/b1", f.b1, c.body(id))
+ if got != c.code {
+ t.Errorf("%s: %d %s, want %d", c.what, got, raw, c.code)
+ continue
+ }
+ rec := f.record(t, host)
+ if rec.Seed.State != seeds.StateAborted || rec.Seed.Reason != c.wantReason {
+ t.Errorf("%s: seed %+v, want aborted with %q", c.what, rec.Seed, c.wantReason)
+ }
+ if c.unchanged && !bytes.Equal(before, f.raw(t, host)) {
+ t.Errorf("%s: the record changed", c.what)
+ }
+ }
+}
+
+// P3-4: a fleet file seed's record with a replica assignment is not the
+// door's to own: the identical 404, nothing changed.
+func TestFleetSeedDoorRefusesReplicaAssignment(t *testing.T) {
+ f := newFleetSeedFx(t, false)
+ id := newSeedID(t)
+ now := time.Now().UTC()
+ rec := provision.Record{Schema: 1, Host: fsHost, Slug: "seeded", App: "shop", Account: f.fx.acct, Channel: "stable",
+ Region: "fleet/home", CreatedAt: now, ReplicaAssignment: &replicas.Assignment{Revision: 1, Desired: 1, Boxes: []string{"b1"}},
+ Seed: &provision.SeedSource{ID: id, Kind: seeds.KindFile, State: seeds.StateReady, SHA256: strings.Repeat("a", 64), Bytes: 4,
+ By: "alice@keymail.dev", At: now}}
+ // Written raw: no create path writes this shape (TestFileSeedBeginFleet
+ // WritesDeploymentKey pins that), so only a hand-made record has it.
+ ixPutRecord(t, f.fx, rec)
+ before := f.raw(t, fsHost)
+ for _, op := range fleetSeedOps {
+ code, raw := f.post(t, fsHost, op, "home/b1", f.b1, opBody(op, id))
+ if code != http.StatusNotFound || string(raw) != "404 page not found\n" {
+ t.Errorf("%s: %d %q, want the identical 404", op, code, raw)
+ }
+ }
+ if !bytes.Equal(before, f.raw(t, fsHost)) {
+ t.Fatal("the record changed")
+ }
+}
diff --git a/internal/console/seedfilefleet_test.go b/internal/console/seedfilefleet_test.go
index 22253c855..1acdef999 100644
--- a/internal/console/seedfilefleet_test.go
+++ b/internal/console/seedfilefleet_test.go
@@ -116,13 +116,19 @@ func TestFileSeedBeginFleetWritesDeploymentKey(t *testing.T) {
f := newFleetFileFixture(t)
body, _ := sqliteBytes(t)
begun := f.beginFile(t, fileHost, body)
- if begun.Region != "fleet/"+seedFleet || begun.FleetBox != "" {
- t.Fatalf("record placed %q labelled %q; want fleet/pi, unlabelled", begun.Region, begun.FleetBox)
+ // No replica assignment either (review P3-4): the seeds door refuses a
+ // file seed's record that carries one, so the create must never write
+ // one.
+ if begun.Region != "fleet/"+seedFleet || begun.FleetBox != "" || begun.ReplicaAssignment != nil {
+ t.Fatalf("record placed %q labelled %q assigned %+v; want fleet/pi, unlabelled, unassigned", begun.Region, begun.FleetBox, begun.ReplicaAssignment)
}
id := begun.Seed.ID
requireFileState(t, begun, id, seeds.StateReserving, "")
rec := f.uploaded(t, fileHost, body)
requireFileState(t, rec, id, seeds.StateReady, "")
+ if rec.ReplicaAssignment != nil {
+ t.Fatalf("the upload assigned replicas: %+v", rec.ReplicaAssignment)
+ }
got, _, err := f.fx.rel.Get(context.Background(), seeds.FleetSnapshotKey("bab", fromApp, fileHost, id))
if err != nil || !bytes.Equal(got, body) {
t.Fatalf("fleet snapshot = %d bytes, %v; want the upload", len(got), err)
diff --git a/internal/provision/fleet_seeded_create_3_census_test.go b/internal/provision/fleet_seeded_create_3_census_test.go
index 22569fd2b..c442f1180 100644
--- a/internal/provision/fleet_seeded_create_3_census_test.go
+++ b/internal/provision/fleet_seeded_create_3_census_test.go
@@ -19,8 +19,10 @@ package provision
// by provision.SetRecordSeed: SeedPlace, SeedFail (with the reason the
// code maps to and held) or SeedRolledBack, each bound to the record's
// region and box as read under the lock. A record already where the op
-// would put it is not written. It then deletes the seed's fleet
-// snapshot (placed, rolled back) and bumps the serial. The finish that
+// would put it is not written; an aborted seed takes only
+// copy-in-storage, over no reason or FS-30 (review P3-1). It then
+// deletes the seed's fleet snapshot on placed (a rolled-back seed's
+// goes with its finish) and bumps the serial. The finish that
// follows rolled-back runs after the lock is released, through
// finishSeed, whose own entry (finishSeedOnce) is unchanged.
// record-writer / key:SetRecordSeed, as the other seed doors' writers.
@@ -83,6 +85,6 @@ func fleetSeededCreate3WriteCensusInventory() []censusEntry {
}
}
return append(entries,
- censusEntry{"internal/console/apifleetseeds.go", "Server.fleetSeedTransition", "record-writer", "key:SetRecordSeed", "15987d1343dee7ce732f193d757ef7853376c0a666c9bb2a87b6ea1be2a4ab51"},
+ censusEntry{"internal/console/apifleetseeds.go", "Server.fleetSeedTransition", "record-writer", "key:SetRecordSeed", "360d42934163d3d07225d927ac7fee6fdd40a6d9d86658a829b587450dee3f7a"},
)
}