carlos / platform Public

Managed fleets 1b task 2, review round 1: fail closed on missing evidence; static records unlabelled

- An active fleet box with no heartbeat has no readable version: the gate
  stays closed (DeploymentCurrent false).
- A failed listing with ETags fails the inventory; plain List is used only
  for a store without ETagLister.
- Controller ruling: a static record is never labelled and never counted
  for load or Unmovable (fleetLabelsRecord); movable is unchanged.
- New tests: TestGateNeedsAHeartbeatFromEveryActiveBox,
  TestInventoryListFailureClosesTheGate,
  TestStaticRecordsAreNeitherLabelledNorCounted, and the lifetime budget
  test TestInventoryJudgesEveryRecordEachLoad (one slot read per record on a
  warm load, verdict follows a moved slot, an unreadable slot closes the
  gate).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev 45f8ff5a2c47b809b35f7d5d8e16a69181dd5904 parent 1948cb6
3 files changed, +187 −13
  • internal/console/apiinstances.go +5 −4
  • internal/console/fleetbox.go +27 −9
  • internal/console/fleetbox_test.go +155 −0

CI failed — run details

diff --git a/internal/console/apiinstances.go b/internal/console/apiinstances.go
index ac83809b8..02c3e06a4 100644
--- a/internal/console/apiinstances.go
+++ b/internal/console/apiinstances.go
@@ -2308,10 +2308,11 @@ func (s *Server) handleAPIInstanceCreate(w http.ResponseWriter, r *http.Request)
// The fleet box (managed fleets 1b, rule 1): on a fleet the gate admits,
// the ready box holding the fewest records, stamped with the time it was
// assigned, before the claim, lifetime and plain writers below all write
- // this rec. A record the platform cannot move is left unlabelled (rule
- // 11). No box to choose leaves it unlabelled too; a rootfs create never
- // gets here without one (rootfsChosenBoxRefusal).
- if fleetActive != nil && movable(rec) {
+ // this rec. A static record, served by every box from the bucket, and a
+ // record the platform cannot move (rule 11) are left unlabelled. No box
+ // to choose leaves it unlabelled too; a rootfs create never gets here
+ // without one (rootfsChosenBoxRefusal).
+ if fleetActive != nil && fleetLabelsRecord(rec) && movable(rec) {
if box := fleetActive.chooseDestination(rec.Runtime == provision.RuntimeRootfsV1, ""); box != "" {
rec.FleetBox, rec.FleetBoxAt = box, time.Now().UTC().Format(time.RFC3339Nano)
} else {
diff --git a/internal/console/fleetbox.go b/internal/console/fleetbox.go
index 1d252921e..33a7a5e4b 100644
--- a/internal/console/fleetbox.go
+++ b/internal/console/fleetbox.go
@@ -39,9 +39,10 @@ import (
// the view is built from was complete. HasSidecars: some sidecar record is
// placed on the fleet (gate b). DeploymentCurrent: every heartbeat of the
// deployment, platform and fleet, and the console itself report a release
-// at or above release.FleetBoxLabelFloor, from a complete listing (gates c
-// and d). Unmovable: some record on the fleet fails movable (rule 11).
-// Load counts the fleet's records by the box they are labelled to.
+// at or above release.FleetBoxLabelFloor, from a complete listing, and
+// every active box of the fleet has a heartbeat (gates c and d). Unmovable: some record on the fleet fails movable (rule 11).
+// Load counts the fleet's records by the box they are labelled to. Static
+// records count toward neither (fleetLabelsRecord).
type fleetView struct {
Boxes []fleetBoxView
Active, HasSidecars, DeploymentCurrent bool
@@ -89,6 +90,15 @@ func (v fleetView) chooseDestination(rootfs bool, exclude string) string {
return best
}
+// fleetLabelsRecord reports whether labelling is about rec at all: every
+// record but a static one. A static record has no process; every box of
+// the fleet mints and serves it from the bucket, as on main, so it is never
+// labelled, holds no load and never closes the gate (controller ruling,
+// Task 2 review round 1). movable is asked only of the records this admits.
+func fleetLabelsRecord(rec provision.Record) bool {
+ return rec.KindOrDefault() != "static"
+}
+
// movable reports whether rec is a process the platform runs and can stop
// on one box and start on another by relabelling: no upstream address the
// platform does not manage (Addr, Addrs), and either a rootfs runtime or a
@@ -226,9 +236,14 @@ func (s *Server) loadFleetView(ctx context.Context, fleet string) (fleetView, er
beat[b.Label] = b
}
now := time.Now()
+ // Every active box must have said which release it runs: one with no
+ // heartbeat at all (attached and never reported, or silent past the
+ // listing's horizon) is a version the gate cannot read.
+ unheard := false
for i := range v.Boxes {
b, ok := beat[v.Boxes[i].Label]
if !ok {
+ unheard = true
continue
}
v.Boxes[i].Fresh = !status.BoxStale(b, now, status.SnapshotPeriod)
@@ -243,7 +258,7 @@ func (s *Server) loadFleetView(ctx context.Context, fleet string) (fleetView, er
log.Printf("console: fleet %s view: deployment heartbeats: complete %v, %v", fleet, deployComplete, err)
whole = false
}
- current := err == nil && deployComplete && s.consoleAtLabelFloor()
+ current := err == nil && deployComplete && !unheard && s.consoleAtLabelFloor()
for _, b := range deploy {
current = current && boxAtLabelFloor(b)
}
@@ -255,6 +270,9 @@ func (s *Server) loadFleetView(ctx context.Context, fleet string) (fleetView, er
whole = false
}
for _, rec := range recs {
+ if !fleetLabelsRecord(rec) {
+ continue
+ }
if rec.FleetBox != "" {
v.Load[rec.FleetBox]++
}
@@ -391,13 +409,13 @@ func (s *Server) fleetInventoryApps(ctx context.Context, placement string) ([]Ap
}
// listKeyETags lists prefix with each key's ETag. A store without
-// ETagLister, or one whose listing with ETags fails, is listed plainly with
-// no ETags, which only costs the reads the ETags would have saved.
+// ETagLister is listed plainly with no ETags, which only costs the reads the
+// ETags would have saved. A store with it that fails answers the failure:
+// a second, plain listing is neither in the budget nor evidence that the
+// first one's failure meant nothing (Task 2 review round 1).
func listKeyETags(ctx context.Context, st release.Store, prefix string) ([]release.KeyETag, error) {
if l, ok := st.(release.ETagLister); ok {
- if kes, err := l.ListETags(ctx, prefix); err == nil {
- return kes, nil
- }
+ return l.ListETags(ctx, prefix)
}
keys, err := st.List(ctx, prefix)
if err != nil {
diff --git a/internal/console/fleetbox_test.go b/internal/console/fleetbox_test.go
index cc786630f..3f2cff1cb 100644
--- a/internal/console/fleetbox_test.go
+++ b/internal/console/fleetbox_test.go
@@ -599,3 +599,158 @@ func TestFleetViewReadsBoxes(t *testing.T) {
t.Fatalf("boxes %+v", v.Boxes)
}
}
+
+// Review fix round 1, finding 1: a registered active box that has no
+// heartbeat has no version the gate can read, so the gate stays closed.
+func TestGateNeedsAHeartbeatFromEveryActiveBox(t *testing.T) {
+ fx, token, reg := fleetBoxWorld(t, "code-fb-no-beat", "pi-1", "pi-2")
+ wantLabel(t, createShopAt(t, fx, token, "z.example.com"), "pi-1") // open with both reporting
+ registerFleetBox(t, reg, "pi", "pi-3") // attached, never reported
+ wantNoLabel(t, createShopAt(t, fx, token, "a.example.com"))
+ v, err := fx.srv.loadFleetView(context.Background(), "pi")
+ if err != nil {
+ t.Fatal(err)
+ }
+ if v.DeploymentCurrent || v.labelActive() {
+ t.Fatalf("view %+v; want the gate closed while pi-3 has no heartbeat", v)
+ }
+ piBeat(t, fx, "pi-3", true, time.Now(), release.FleetBoxLabelFloor)
+ wantLabel(t, createShopAt(t, fx, token, "b.example.com"), "pi-2")
+}
+
+// failETagStore fails every listing with ETags under prefix; the plain
+// listing still answers.
+type failETagStore struct {
+ release.Store
+ prefix string
+}
+
+func (f failETagStore) ListETags(ctx context.Context, prefix string) ([]release.KeyETag, error) {
+ if strings.HasPrefix(prefix, f.prefix) {
+ return nil, fmt.Errorf("injected listing failure")
+ }
+ inner := f.Store
+ if d, ok := inner.(*deletableStore); ok {
+ inner = d.Store
+ }
+ return inner.(release.ETagLister).ListETags(ctx, prefix)
+}
+
+// Review fix round 1, finding 2: a failed listing with ETags is a failed
+// listing. The inventory does not fall back to a plain listing that
+// succeeds, and the gate stays closed.
+func TestInventoryListFailureClosesTheGate(t *testing.T) {
+ fx, token, _ := fleetBoxWorld(t, "code-fb-list-fail", "pi-1", "pi-2")
+ putFleetRecord(t, fx, "shop", "x.example.com", nil)
+ base := fx.srv.cfg.Releases
+ fx.srv.cfg.Releases = failETagStore{Store: base, prefix: "nothing/"}
+ wantLabel(t, createShopAt(t, fx, token, "z.example.com"), "pi-1") // open through the wrapper
+ fx.srv.cfg.Releases = failETagStore{Store: base, prefix: provision.RecordsPrefix(fx.sqid, "shop")}
+ if _, _, complete, err := fx.srv.fleetInventoryFor(context.Background(), "pi"); err != nil || complete {
+ t.Fatalf("inventory with a failed listing: complete %v, %v; want incomplete", complete, err)
+ }
+ wantNoLabel(t, createShopAt(t, fx, token, "a.example.com"))
+ fx.srv.cfg.Releases = failETagStore{Store: base, prefix: provision.SidecarsPrefix(fx.sqid, "shop")}
+ wantNoLabel(t, createShopAt(t, fx, token, "b.example.com"))
+}
+
+// Review fix round 1, finding 3 (controller ruling): a static record is
+// never labelled and does not close the gate, and it holds no load.
+func TestStaticRecordsAreNeitherLabelledNorCounted(t *testing.T) {
+ fx, token, _ := fleetBoxWorld(t, "code-fb-static", "pi-1", "pi-2")
+ putFleetRecord(t, fx, "shop", "site.example.com", func(r *provision.Record) { r.Kind = "static" })
+ if code, out := createShop(t, fx, token, `{"host":"www.example.com","kind":"static"}`); code != http.StatusCreated {
+ t.Fatalf("static create = %d %s", code, out)
+ }
+ rec, _ := storedShop(t, fx, "www.example.com")
+ wantNoLabel(t, rec)
+ v, err := fx.srv.loadFleetView(context.Background(), "pi")
+ if err != nil {
+ t.Fatal(err)
+ }
+ if !v.labelActive() || v.Unmovable || len(v.Load) != 0 {
+ t.Fatalf("view %+v; want the gate open, nothing unmovable and no load from static records", v)
+ }
+ wantLabel(t, createShopAt(t, fx, token, "a.example.com"), "pi-1")
+ wantLabel(t, createShopAt(t, fx, token, "b.example.com"), "pi-2")
+ for _, tc := range []struct {
+ rec provision.Record
+ want bool
+ }{
+ {provision.Record{Kind: "static"}, false},
+ {provision.Record{}, true},
+ {provision.Record{Kind: "instance"}, true},
+ {provision.Record{Runtime: provision.RuntimeRootfsV1}, true},
+ } {
+ if got := fleetLabelsRecord(tc.rec); got != tc.want {
+ t.Errorf("fleetLabelsRecord(kind %q, runtime %q) = %v; want %v", tc.rec.Kind, tc.rec.Runtime, got, tc.want)
+ }
+ }
+}
+
+// slotGets is how many incarnation-slot reads were made.
+func (c *countStore) slotGets() int {
+ c.mu.Lock()
+ defer c.mu.Unlock()
+ n := 0
+ for k, v := range c.gets {
+ if strings.HasPrefix(k, "control/host-incarnations/") {
+ n += v
+ }
+ }
+ return n
+}
+
+// Review fix round 1, finding 4: with lifetimes on, a warm load reads no
+// record body but judges every record again, one slot read each; the
+// verdict follows a slot that changed under an unchanged record; and a slot
+// that cannot be read makes the inventory incomplete and closes the gate.
+func TestInventoryJudgesEveryRecordEachLoad(t *testing.T) {
+ fx, _, _ := fleetBoxWorld(t, "code-fb-lifetimes", "pi-1", "pi-2")
+ hosts := []string{"a.example.com", "b.example.com", "c.example.com"}
+ for _, h := range hosts {
+ putFleetRecord(t, fx, "shop", h, func(r *provision.Record) { r.Incarnation = 1 })
+ writeSlot(t, fx.rel, h, provision.HostIncarnation{Incarnation: 1, State: provision.SlotLive, Account: fx.sqid, App: "shop"})
+ }
+ cs := newCountStore(fx.rel)
+ fx.srv.cfg.Releases = cs
+ ctx := provision.WithLifetimes(context.Background(), provision.NewLifetimeJudge())
+ load := func(want int) {
+ t.Helper()
+ recs, _, complete, err := fx.srv.fleetInventoryFor(ctx, "pi")
+ if err != nil || !complete || len(recs) != want {
+ t.Fatalf("inventory: %d records, complete %v, %v; want %d", len(recs), complete, err, want)
+ }
+ }
+ load(3) // cold
+ cs.reset()
+ load(3) // warm
+ if r, s := cs.recordGets(fx.sqid, "shop"), cs.slotGets(); r != 0 || s != 3 {
+ t.Fatalf("a warm judged load read %d records and %d slots; want 0 and one slot per record", r, s)
+ }
+ v, err := fx.srv.loadFleetView(ctx, "pi")
+ if err != nil || !v.labelActive() {
+ t.Fatalf("view %+v, %v; want the gate open", v, err)
+ }
+
+ // b's slot moves on to a later lifetime; its record's ETag does not.
+ writeSlot(t, fx.rel, "b.example.com", provision.HostIncarnation{Incarnation: 2, State: provision.SlotLive, Account: fx.sqid, App: "shop"})
+ recs, _, complete, err := fx.srv.fleetInventoryFor(ctx, "pi")
+ if err != nil || !complete || len(recs) != 2 {
+ t.Fatalf("after b's slot moved: %d records, complete %v, %v; want a and c", len(recs), complete, err)
+ }
+ for _, r := range recs {
+ if r.Host == "b.example.com" {
+ t.Fatal("the inventory kept b, whose slot no longer vouches for it")
+ }
+ }
+
+ // a's slot cannot be read.
+ fx.srv.cfg.Releases = &failGetStore{Store: cs, key: provision.HostIncarnationKey("a.example.com")}
+ if _, _, complete, err := fx.srv.fleetInventoryFor(ctx, "pi"); err != nil || complete {
+ t.Fatalf("an unreadable slot: complete %v, %v; want incomplete", complete, err)
+ }
+ if v, err := fx.srv.loadFleetView(ctx, "pi"); err != nil || v.Active || v.labelActive() {
+ t.Fatalf("view %+v, %v; want the gate closed on an unreadable slot", v, err)
+ }
+}