Merge remote-tracking branch 'origin/main' into remove-tito-console-alias-refs
10 files changed,
+464
−18
docs/superpowers/plans/2026-11-01-stateless-replicas-copy.md+2 −0internal/console/apicli.go+9 −1internal/console/apiinstances.go+2 −2internal/console/appcopies.go+175 −8internal/console/copiespublication_test.go+9 −3internal/console/copiesunlabelled_test.go+249 −0internal/console/fleets.go+7 −0internal/console/managedapi.go+8 −1internal/console/rootfs.go+1 −1internal/console/server.go+2 −2
CI passed — run details
diff --git a/docs/superpowers/plans/2026-11-01-stateless-replicas-copy.md b/docs/superpowers/plans/2026-11-01-stateless-replicas-copy.md| index 266232099..6946413ba 100644 |
| --- a/docs/superpowers/plans/2026-11-01-stateless-replicas-copy.md |
| +++ b/docs/superpowers/plans/2026-11-01-stateless-replicas-copy.md |
| @@ -166,3 +166,5 @@ copy too. Internal wire enums are not an excuse to expose unreviewed text. |
| | SR-4-C92 | internal/activator/replicadeploy.go | the new copy's configuration was not written on its box in time, so it did not start. existing copies keep serving | approved (controller; Luna house rules) (drill 1 round 2) | |
| | SR-4-C93 | docs/site/stateless-replicas.md | An app that creates its own instance.key or report.token file must take those values from a shared source, its configuration or its environment, or run one copy. The platform gives every copy the same CARLOS_INSTANCE_KEY, but it cannot stop each copy of an app from writing a different file of its own. | approved (controller; Luna house rules) (drill 1 round 2, A3 restriction) | |
| | SR-4-C94 | docs/site/stateless-replicas.md | When a copy switches, its old build stops once the new one serves. A response still streaming from the old build when it stops is cut, as when a box stops. Websockets on the old build disconnect and their clients reconnect to a healthy copy. | approved (controller; Luna house rules) (drill 3, H4) | |
| +| SR-4-C95 | internal/console/appcopies.go | <host> is not tied to one box of fleet <fleet>, and that fleet has more than one box. a release without a local database needs that first, so keep this app on a release with a local database for now | pending (Paul's copy review, #1008: refused at ship, at any publication of a stateless build, and at copies above 1; reviewed as SR-5-C01) | |
| +| SR-4-C96 | internal/console/appcopies.go | <host> runs a release without a local database and is not tied to a box of fleet <fleet>, so a second active box would stop it. move it to a release with a local database before adding a box | pending (Paul's copy review, #1008: refused at fleet add-box, box reactivation and a managed fleet add; reviewed as SR-5-C02) | |
diff --git a/internal/console/apicli.go b/internal/console/apicli.go| index 4cc2af184..690a744c1 100644 |
| --- a/internal/console/apicli.go |
| +++ b/internal/console/apicli.go |
| @@ -1037,7 +1037,7 @@ func (s *Server) handleAPIPromote(w http.ResponseWriter, r *http.Request) { |
| jsonError(w, ref.Status, ref.Msg) |
| return |
| } |
| - if ref := s.statelessRuntimeRefusal(ctx, channel, man, nil, ""); ref != nil { |
| + if ref := s.statelessPublicationRefusal(ctx, acct.Sqid, app, channel, man, nil, ""); ref != nil { |
| jsonError(w, ref.Status, ref.Msg) |
| return |
| } |
| @@ -2339,6 +2339,14 @@ func (s *Server) handleAPIShip(w http.ResponseWriter, r *http.Request) { |
| jsonError(w, http.StatusBadRequest, err.Error()) |
| return |
| } |
| + // A stateless build an instance of this app could never wake is |
| + // refused before anything is uploaded (#1008). |
| + if meta.Stateless { |
| + if ref := s.unlabelledFleetRefusal(r.Context(), acct.Sqid, app); ref != nil { |
| + writeDoorRefusal(w, ref) |
| + return |
| + } |
| + } |
| if refuseBadSwap(w, meta.Swap) { |
| return |
| } |
diff --git a/internal/console/apiinstances.go b/internal/console/apiinstances.go| index 797be8a7a..1861a84a6 100644 |
| --- a/internal/console/apiinstances.go |
| +++ b/internal/console/apiinstances.go |
| @@ -2189,7 +2189,7 @@ func (s *Server) handleAPIInstanceCreate(w http.ResponseWriter, r *http.Request) |
| return |
| } |
| if chHead != nil { |
| - if ref := s.statelessRuntimeRefusal(ctx, channel, chHead.Manifest, nil, ""); ref != nil { |
| + if ref := s.statelessPublicationRefusal(ctx, acct.Sqid, app, channel, chHead.Manifest, nil, ""); ref != nil { |
| jsonError(w, ref.Status, ref.Msg) |
| return |
| } |
| @@ -2511,7 +2511,7 @@ func (s *Server) handleAPIInstanceCreate(w http.ResponseWriter, r *http.Request) |
| if fleetActive != nil && fleetLabelsRecord(rec) && movable(rec) { |
| if box := fleetActive.chooseDestination(rec.Runtime == provision.RuntimeRootfsV1, ""); box != "" { |
| if chHead != nil { |
| - if ref := s.statelessRuntimeRefusal(ctx, channel, chHead.Manifest, fleetActive, box); ref != nil { |
| + if ref := s.statelessPublicationRefusal(ctx, acct.Sqid, app, channel, chHead.Manifest, fleetActive, box); ref != nil { |
| jsonError(w, ref.Status, ref.Msg) |
| return |
| } |
diff --git a/internal/console/appcopies.go b/internal/console/appcopies.go| index 30b29a4f6..1c0499f4c 100644 |
| --- a/internal/console/appcopies.go |
| +++ b/internal/console/appcopies.go |
| @@ -21,14 +21,18 @@ import ( |
| const ( |
| copiesStopping = "copies are still stopping on other boxes. wait for that to finish before selecting a release with a local database" |
| - copiesBounds = "copies must be a whole number of at least 1" |
| - copiesCapacity = "%d copies need %d eligible boxes. this fleet has %d" |
| - copiesRelease = "more than one copy requires a stateless, always-on release. set --copies 1 before selecting this release" |
| - copiesFloor = "copies above 1 need every fleet box and the console on %s or later. update them and try again" |
| - copiesUnread = "could not check every instance, release and fleet box, so the copy count was left unchanged. try again" |
| - copiesManaged = "more than one copy is available for platform-managed instances on an owned fleet only" |
| - copiesMoving = "this instance has an import or move in progress. finish it before changing the copy count" |
| - copiesOverlap = "more than one copy needs a release and app policy that let each copy switch builds while its old build keeps serving. change them first" |
| + // copiesUnlabelledGrowth (#1008, draft for copy review): host, fleet. |
| + copiesUnlabelledGrowth = "%s runs a release without a local database and is not tied to a box of fleet %s, so a second active box would stop it. move it to a release with a local database before adding a box" |
| + // copiesUnlabelled (#1008, draft for copy review): host, fleet. |
| + copiesUnlabelled = "%s is not tied to one box of fleet %s, and that fleet has more than one box. a release without a local database needs that first, so keep this app on a release with a local database for now" |
| + copiesBounds = "copies must be a whole number of at least 1" |
| + copiesCapacity = "%d copies need %d eligible boxes. this fleet has %d" |
| + copiesRelease = "more than one copy requires a stateless, always-on release. set --copies 1 before selecting this release" |
| + copiesFloor = "copies above 1 need every fleet box and the console on %s or later. update them and try again" |
| + copiesUnread = "could not check every instance, release and fleet box, so the copy count was left unchanged. try again" |
| + copiesManaged = "more than one copy is available for platform-managed instances on an owned fleet only" |
| + copiesMoving = "this instance has an import or move in progress. finish it before changing the copy count" |
| + copiesOverlap = "more than one copy needs a release and app policy that let each copy switch builds while its old build keeps serving. change them first" |
| // copy SR-4-C85 (#511 drill 1, R19) |
| copiesSize = "%d copies need %d boxes with room for size %s. this fleet has %d. choose a smaller size" |
| // copy SR-4-C91 (#511 drill 1 round 1, Astra A2) |
| @@ -213,6 +217,9 @@ func (s *Server) copiesRefusal(ctx context.Context, acct *Account, app string, n |
| if err != nil { |
| return unread() |
| } |
| + if ref := unlabelledFleetRecordRefusal(reg, recs); ref != nil { |
| + return ref |
| + } |
| // A durable create allocation may publish its body during recovery. |
| // Do not admit a larger count while that record is still pending. |
| allocations, err := s.store.PendingLifetimesForApp(acct.Sqid, app) |
| @@ -424,3 +431,163 @@ func (s *Server) copiesSizeRefusal(ctx context.Context, acct *Account, app strin |
| } |
| return nil |
| } |
| + |
| +// statelessPublicationRefusal is the check every door that selects a |
| +// stateless build for an app's instances asks (promote, new canary, |
| +// rollback, fast-track, instance create): the runtime floor, then |
| +// unlabelledFleetRefusal (#1008). Hibernating stateless builds included: |
| +// their wake needs the coordinator too. |
| +func (s *Server) statelessPublicationRefusal(ctx context.Context, sqid, app, channel string, m *release.Manifest, fleet *fleetView, box string) *doorRefusal { |
| + if ref := s.statelessRuntimeRefusal(ctx, channel, m, fleet, box); ref != nil { |
| + return ref |
| + } |
| + if m == nil || !m.Stateless { |
| + return nil |
| + } |
| + return s.unlabelledFleetRefusal(ctx, sqid, app) |
| +} |
| + |
| +// unlabelledFleetRefusal refuses a release without a local database, or |
| +// more than one copy, for an app with a live instance whose record is |
| +// placed on a fleet but tied to no box (no fleet_box, no replica |
| +// assignment) while that fleet has more than one active box (#1008). Every |
| +// box of the fleet serves such a record (provision ownsRecord), and the |
| +// console's coordinator door accepts it only on a one-box fleet |
| +// (replicaFleetRecord's sole-box rule), refusing every box otherwise: a |
| +// stateless build of it could never wake. Refused here, before anything is |
| +// published, rather than left to fail at the next wake. |
| +func (s *Server) unlabelledFleetRefusal(ctx context.Context, sqid, app string) *doorRefusal { |
| + recs, err := placeRecords(ctx, s.cfg.Releases, provision.JudgeFrom(ctx), sqid, app) |
| + if err != nil { |
| + return &doorRefusal{Status: http.StatusServiceUnavailable, Msg: copiesUnread} |
| + } |
| + if !anyUnlabelledFleetRecord(recs) { |
| + return nil |
| + } |
| + reg, err := s.openPassRegistry() |
| + if err != nil { |
| + return &doorRefusal{Status: http.StatusServiceUnavailable, Msg: copiesUnread} |
| + } |
| + return unlabelledFleetRecordRefusal(reg, recs) |
| +} |
| + |
| +func anyUnlabelledFleetRecord(recs []provision.Record) bool { |
| + for _, rec := range recs { |
| + if _, onFleet := registry.FleetPlacement(rec.Region); onFleet && !rec.Retired && rec.FleetBox == "" && rec.ReplicaAssignment == nil { |
| + return true |
| + } |
| + } |
| + return false |
| +} |
| + |
| +// unlabelledFleetRecordRefusal is unlabelledFleetRefusal's judgment over |
| +// records already read. A fleet whose boxes can't be read answers 503. |
| +func unlabelledFleetRecordRefusal(reg consoleRegistry, recs []provision.Record) *doorRefusal { |
| + active := map[string]int{} |
| + for _, rec := range recs { |
| + fleet, onFleet := registry.FleetPlacement(rec.Region) |
| + if !onFleet || rec.Retired || rec.FleetBox != "" || rec.ReplicaAssignment != nil { |
| + continue |
| + } |
| + n, seen := active[fleet] |
| + if !seen { |
| + boxes, err := reg.ListFleetBoxes(fleet) |
| + if err != nil { |
| + return &doorRefusal{Status: http.StatusServiceUnavailable, Msg: copiesUnread} |
| + } |
| + for _, box := range boxes { |
| + if box.Status == "active" { |
| + n++ |
| + } |
| + } |
| + active[fleet] = n |
| + } |
| + if n > 1 { |
| + return &doorRefusal{Status: http.StatusConflict, Msg: fmt.Sprintf(copiesUnlabelled, rec.Host, fleet)} |
| + } |
| + } |
| + return nil |
| +} |
| + |
| +// fleetGrowthRefusal refuses adding or reactivating label on fleet when that |
| +// would take the fleet from at most one active box to more than one while |
| +// an unlabelled instance of the fleet runs a stateless build (#1008): the |
| +// coordinator door accepts such a record only on a one-box fleet, so the |
| +// growth alone would cut it off. Reads it can't finish answer 503. |
| +func (s *Server) fleetGrowthRefusal(ctx context.Context, reg consoleRegistry, acct *Account, fleet, label string) *doorRefusal { |
| + return s.fleetGrowthBy(ctx, reg, acct, fleet, label, 1) |
| +} |
| + |
| +// fleetGrowthBy is fleetGrowthRefusal for adding boxes boxes at once (a |
| +// managed fleet's add, judged at reservation so nothing is provisioned). |
| +// label, when set, is the one box being added: already active, it is not |
| +// growth. |
| +func (s *Server) fleetGrowthBy(ctx context.Context, reg consoleRegistry, acct *Account, fleet, label string, boxes int) *doorRefusal { |
| + unread := &doorRefusal{Status: http.StatusServiceUnavailable, Msg: msgFleetBoxNotAdded} |
| + listed, err := reg.ListFleetBoxes(fleet) |
| + if err != nil { |
| + return unread |
| + } |
| + active := 0 |
| + for _, b := range listed { |
| + if b.Status == "active" { |
| + if label != "" && b.Label == label { |
| + return nil // already active: not growth (the door answers the duplicate) |
| + } |
| + active++ |
| + } |
| + } |
| + if boxes < 1 || active+boxes <= 1 { |
| + return nil |
| + } |
| + if s.cfg.Releases == nil { |
| + return unread |
| + } |
| + apps, err := s.store.AppsIn(acct.ID) |
| + if err != nil { |
| + return unread |
| + } |
| + for _, app := range apps { |
| + recs, err := placeRecords(ctx, s.cfg.Releases, provision.JudgeFrom(ctx), acct.Sqid, app) |
| + if err != nil { |
| + return unread |
| + } |
| + for _, rec := range recs { |
| + if rec.Retired || rec.Region != "fleet/"+fleet || rec.FleetBox != "" || rec.ReplicaAssignment != nil { |
| + continue |
| + } |
| + stateless, err := s.channelHeadStateless(ctx, acct.Sqid, app, rec.Channel) |
| + if err != nil { |
| + return unread |
| + } |
| + if stateless { |
| + return &doorRefusal{Status: http.StatusConflict, Msg: fmt.Sprintf(copiesUnlabelledGrowth, rec.Host, fleet)} |
| + } |
| + } |
| + } |
| + return nil |
| +} |
| + |
| +// channelHeadStateless reports whether channel's current build declares no |
| +// local database. No channel, or an empty one, is false; a read that |
| +// fails is an error, never false. |
| +func (s *Server) channelHeadStateless(ctx context.Context, sqid, app, channel string) (bool, error) { |
| + if channel == "" { |
| + return false, nil |
| + } |
| + ptr, err := release.Channel(ctx, s.cfg.Releases, s.cfg.ReleasePub, sqid, app, channel) |
| + if errors.Is(err, os.ErrNotExist) { |
| + return false, nil |
| + } |
| + if err != nil { |
| + return false, err |
| + } |
| + if ptr == nil || ptr.Version == "" { |
| + return false, nil |
| + } |
| + m, err := release.GetManifest(ctx, s.cfg.Releases, sqid, app, ptr.Version) |
| + if err != nil { |
| + return false, err |
| + } |
| + return m != nil && m.Stateless, nil |
| +} |
diff --git a/internal/console/copiespublication_test.go b/internal/console/copiespublication_test.go| index ba4544803..dc226cda1 100644 |
| --- a/internal/console/copiespublication_test.go |
| +++ b/internal/console/copiespublication_test.go |
| @@ -175,7 +175,10 @@ func TestCopiesPublicationEvidence(t *testing.T) { |
| {"unread config unsafe", false, false, "config", 503}, |
| {"unread config safe", false, true, "config", 200}, |
| {"unread inventory unsafe", true, false, "record", 503}, |
| - {"unread inventory safe", true, true, "record", 200}, |
| + // A stateless target reads the inventory for #1008's guard (an |
| + // unlabelled record on a multi-box fleet), and an unreadable one |
| + // refuses with a retry rather than publish unchecked. |
| + {"unread inventory safe", true, true, "record", 503}, |
| } { |
| t.Run(tc.name, func(t *testing.T) { |
| fx, token := copiesWorld(t) |
| @@ -210,8 +213,11 @@ func TestCopiesPublicationEvidence(t *testing.T) { |
| if counter.gets[cfgKey] != 1 { |
| t.Fatalf("config GETs=%d, want exactly one", counter.gets[cfgKey]) |
| } |
| - if (!tc.explicit || tc.safe) && counter.gets[recKey] != 0 { |
| - t.Fatal("legacy/safe target read assignments") |
| + if !tc.explicit && !tc.safe && counter.gets[recKey] != 0 { |
| + t.Fatal("legacy target read assignments") |
| + } |
| + if tc.safe && tc.failure != "record" && counter.gets[recKey] == 0 { |
| + t.Fatal("stateless target published without reading the inventory (#1008)") |
| } |
| }) |
| } |
diff --git a/internal/console/copiesunlabelled_test.go b/internal/console/copiesunlabelled_test.go| new file mode 100644 |
| index 000000000..45196679d |
| --- /dev/null |
| +++ b/internal/console/copiesunlabelled_test.go |
| @@ -0,0 +1,249 @@ |
| +package console |
| + |
| +import ( |
| + "context" |
| + "crypto/ed25519" |
| + "crypto/rand" |
| + "fmt" |
| + "net/http" |
| + "strings" |
| + "testing" |
| + "time" |
| + |
| + "github.com/carlosframework/platform/internal/provision" |
| + "github.com/carlosframework/platform/internal/registry" |
| + "github.com/carlosframework/platform/internal/release" |
| +) |
| + |
| +// unlabelledStatelessCutOff lists the instances of shop that no box of fleet |
| +// pi could ever coordinate (#1008): live, on the fleet, tied to no box (no |
| +// fleet_box, no replica assignment), on a fleet with more than one active box |
| +// (the coordinator door's sole-box rule refuses every box), while their |
| +// channel's current build declares no local database. |
| +func unlabelledStatelessCutOff(t *testing.T, fx *consoleFixture) []string { |
| + t.Helper() |
| + ctx := context.Background() |
| + reg := openTestReg(t, fx) |
| + defer reg.Close() |
| + boxes, err := reg.ListFleetBoxes("pi") |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + active := 0 |
| + for _, b := range boxes { |
| + if b.Status == "active" { |
| + active++ |
| + } |
| + } |
| + recs, err := placeRecords(ctx, fx.rel, nil, fx.sqid, "shop") |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + var out []string |
| + for _, rec := range recs { |
| + if rec.Retired || rec.Region != "fleet/pi" || rec.FleetBox != "" || rec.ReplicaAssignment != nil || active <= 1 { |
| + continue |
| + } |
| + stateless, err := fx.srv.channelHeadStateless(ctx, fx.sqid, "shop", rec.Channel) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if stateless { |
| + out = append(out, rec.Host) |
| + } |
| + } |
| + return out |
| +} |
| + |
| +// TestNoTransitionCutsOffAStatelessInstance (#1008, invariant): from each |
| +// starting world, every transition the console ACCEPTS leaves no stateless |
| +// instance that every fleet box would be refused (unlabelledStatelessCutOff |
| +// is empty). The transitions are the trimmed guard's doors: a --stateless |
| +// ship, a stateless promote, a rollback onto a stateless build, copies 2, |
| +// and adding or reactivating a box. The labelled world is the control: |
| +// there the guard refuses nothing. |
| +func TestNoTransitionCutsOffAStatelessInstance(t *testing.T) { |
| + unlabel := func(t *testing.T, fx *consoleFixture) { |
| + placementEdit(t, fx, "a.example.com", func(r *provision.Record) { r.FleetBox, r.FleetBoxAt = "", "" }) |
| + } |
| + type world struct { |
| + name string |
| + setup func(t *testing.T, fx *consoleFixture) |
| + // refused names the transitions this world must refuse with the |
| + // #1008 refusal; every other transition must succeed, except a |
| + // reactivation of a box that is already active (the duplicate). |
| + refused map[string]bool |
| + // other names a transition this world refuses for its own reason |
| + // (not #1008), by a fragment of the refusal. |
| + other map[string]string |
| + } |
| + // edgeHistory gives edge a previous stateless build, so a rollback |
| + // selects one (v2, then back to v1). |
| + edgeHistory := func(t *testing.T, fx *consoleFixture) { |
| + if err := release.Promote(context.Background(), fx.rel, fx.priv, fx.sqid, "shop", "edge", "v2"); err != nil { |
| + t.Fatal(err) |
| + } |
| + } |
| + worlds := []world{ |
| + {"two boxes, unlabelled, edge on a local-database build", func(t *testing.T, fx *consoleFixture) { |
| + unlabel(t, fx) |
| + shipVersion(t, fx, "shop", "s1") |
| + if err := release.Promote(context.Background(), fx.rel, fx.priv, fx.sqid, "shop", "edge", "s1"); err != nil { |
| + t.Fatal(err) |
| + } |
| + }, map[string]bool{"ship stateless": true, "promote stateless onto edge": true, "rollback edge": true, "copies 2": true}, nil}, |
| + {"one box, unlabelled, edge stateless and working", func(t *testing.T, fx *consoleFixture) { |
| + edgeHistory(t, fx) |
| + unlabel(t, fx) |
| + placementEdit(t, fx, "b.example.com", func(r *provision.Record) { r.FleetBox = "pi-1" }) |
| + reg := openTestReg(t, fx) |
| + defer reg.Close() |
| + if err := reg.SetFleetBoxStatus("pi", "pi-2", "detached"); err != nil { |
| + t.Fatal(err) |
| + } |
| + }, map[string]bool{"reactivate pi-2": true, "add pi-3": true}, map[string]string{"copies 2": "eligible boxes"}}, |
| + {"two boxes, labelled", edgeHistory, nil, nil}, |
| + } |
| + type transition struct { |
| + name string |
| + do func(t *testing.T, fx *consoleFixture, token string) (int, string) |
| + } |
| + api := func(method, path, body string) func(*testing.T, *consoleFixture, string) (int, string) { |
| + return func(t *testing.T, fx *consoleFixture, token string) (int, string) { |
| + return copiesCall(t, fx, token, copiesDoor{path, method, path, body}) |
| + } |
| + } |
| + addBox := func(label string) func(*testing.T, *consoleFixture, string) (int, string) { |
| + return func(t *testing.T, fx *consoleFixture, token string) (int, string) { |
| + code, body := postJSON(t, fleetsURL(fx, fx.sqid)+"/pi/boxes", map[string]string{"label": label}, token) |
| + return code, fmt.Sprint(body) |
| + } |
| + } |
| + transitions := []transition{ |
| + {"ship stateless", func(t *testing.T, fx *consoleFixture, token string) (int, string) { |
| + code, body := postShip(t, fx.ts.URL+"/api/cli/apps/"+fx.sqid+"/shop/releases", token, []shipPart{metaStateless(t, "v3"), {name: "linux-amd64", body: tinyProgramForConsole(t, "example.com/shop")}}) |
| + return code, fmt.Sprint(body) |
| + }}, |
| + {"promote stateless onto edge", api("POST", "/promote", `{"channel":"edge","version":"v2","hotfix":true}`)}, |
| + {"rollback edge", api("POST", "/rollback", `{"channel":"edge"}`)}, |
| + {"copies 2", func(t *testing.T, fx *consoleFixture, token string) (int, string) { |
| + return copiesPut(t, fx, token, `{"copies":2}`) |
| + }}, |
| + {"reactivate pi-2", addBox("pi-2")}, |
| + {"add pi-3", addBox("pi-3")}, |
| + } |
| + for _, w := range worlds { |
| + for _, tr := range transitions { |
| + t.Run(w.name+"/"+tr.name, func(t *testing.T) { |
| + fx, token := copiesWorld(t) |
| + w.setup(t, fx) |
| + if cut := unlabelledStatelessCutOff(t, fx); len(cut) != 0 { |
| + t.Fatalf("world starts broken: %v", cut) |
| + } |
| + code, out := tr.do(t, fx, token) |
| + ours := code == http.StatusConflict && (strings.Contains(out, "is not tied to one box") || strings.Contains(out, "is not tied to a box")) |
| + switch { |
| + case w.refused[tr.name]: |
| + if !ours { |
| + t.Fatalf("want the #1008 refusal, got %d %s", code, out) |
| + } |
| + case w.other[tr.name] != "": |
| + if code < 300 || ours || !strings.Contains(out, w.other[tr.name]) { |
| + t.Fatalf("want this world's own refusal %q, got %d %s", w.other[tr.name], code, out) |
| + } |
| + case tr.name == "reactivate pi-2" && w.name != "one box, unlabelled, edge stateless and working": |
| + // pi-2 is already active: the duplicate answer. |
| + if code != http.StatusConflict || ours { |
| + t.Fatalf("duplicate box: %d %s", code, out) |
| + } |
| + default: |
| + if code >= 300 { |
| + t.Fatalf("want accepted, got %d %s", code, out) |
| + } |
| + } |
| + if code < 300 { |
| + if cut := unlabelledStatelessCutOff(t, fx); len(cut) != 0 { |
| + t.Fatalf("accepted %d %s, and left %v cut off from every box", code, out, cut) |
| + } |
| + } |
| + }) |
| + } |
| + } |
| +} |
| + |
| +// TestStatelessPublicationCoversHibernating: a hibernating stateless build |
| +// needs the coordinator to wake, so the publication check refuses it too. |
| +func TestStatelessPublicationCoversHibernating(t *testing.T) { |
| + fx, _ := copiesWorld(t) |
| + placementEdit(t, fx, "a.example.com", func(r *provision.Record) { r.FleetBox, r.FleetBoxAt = "", "" }) |
| + ctx := withWriteMemo(t.Context()) |
| + m := &release.Manifest{App: "shop", Version: "h1", Kind: release.KindRootfs, Stateless: true, Hibernate: true} |
| + ref := fx.srv.unlabelledFleetRefusal(ctx, fx.sqid, "shop") |
| + if ref == nil || ref.Msg != fmt.Sprintf(copiesUnlabelled, "a.example.com", "pi") { |
| + t.Fatalf("unlabelled guard: %+v", ref) |
| + } |
| + if got := fx.srv.statelessPublicationRefusal(ctx, fx.sqid, "shop", "edge", m, nil, ""); got == nil || got.Status != http.StatusConflict || got.Msg != fmt.Sprintf(copiesUnlabelled, "a.example.com", "pi") { |
| + t.Fatalf("hibernating stateless publication: %+v", got) |
| + } |
| +} |
| + |
| +func metaStateless(t *testing.T, version string) shipPart { |
| + t.Helper() |
| + return shipPart{name: "meta", body: []byte(`{"version":"` + version + `","kind":"binary","stateless":true}`)} |
| +} |
| + |
| +// TestManagedGrowthCannotCutOffStatelessInstance (#1008): a managed fleet's |
| +// add is the other way a fleet grows. With one active box and an unlabelled |
| +// instance whose channel runs a stateless build, the add is refused before |
| +// anything is reserved; with the record labelled, it is reserved. |
| +func TestManagedGrowthCannotCutOffStatelessInstance(t *testing.T) { |
| + for _, labelled := range []bool{false, true} { |
| + t.Run(fmt.Sprintf("labelled=%v", labelled), func(t *testing.T) { |
| + s := managedAPIFixture(t) |
| + ctx := context.Background() |
| + managedAPIReply(t, managedAPICall(s, "create", "abc", "", managedAPIBody("a", 1))) |
| + // The create has run its course; an add may be asked. |
| + s.store.db.Exec(`UPDATE managed_ops SET state='failed',error='managed_preparation_failed'`) |
| + reg, err := registry.Open(s.cfg.Registry) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + defer reg.Close() |
| + if err := reg.AddManagedFleetBox(registry.FleetBox{Fleet: "production", Label: "m-1", TokenHash: strings.Repeat("a", 64), Status: "active", AddedAt: time.Now().Unix()}, time.Now().Unix()); err != nil { |
| + t.Fatal(err) |
| + } |
| + pub, priv, _ := ed25519.GenerateKey(rand.Reader) |
| + s.cfg.ReleasePub = pub |
| + if _, err := s.store.CreateAppIn(42, "shop", "owner@example.com", time.Now()); err != nil { |
| + t.Fatal(err) |
| + } |
| + m := release.Manifest{Schema: 1, Kind: "binary", App: "shop", Version: "v1", Stateless: true, Artifacts: map[string]release.Artifact{"linux-amd64": {File: "app"}}} |
| + if err := release.Ship(ctx, s.cfg.Releases, "abc", m, map[string][]byte{"linux-amd64": []byte("app")}); err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := release.Promote(ctx, s.cfg.Releases, priv, "abc", "shop", "edge", "v1"); err != nil { |
| + t.Fatal(err) |
| + } |
| + rec := provision.Record{Schema: 1, Host: "a.example.com", Slug: "a", App: "shop", Account: "abc", Region: "fleet/production", Channel: "edge", CreatedAt: time.Now().UTC()} |
| + if labelled { |
| + rec.FleetBox = "m-1" |
| + } |
| + if err := provision.CreateRecord(ctx, s.cfg.Releases, rec); err != nil { |
| + t.Fatal(err) |
| + } |
| + w := managedAPICall(s, "add", "abc", "production", `{"boxes":1,"request_id":"bbbbbbbbbbbbbbbbbbbbbb"}`) |
| + var adds int |
| + s.store.db.QueryRow(`SELECT count(*) FROM managed_ops WHERE kind='add'`).Scan(&adds) |
| + want := fmt.Sprintf(copiesUnlabelledGrowth, "a.example.com", "production") |
| + if labelled { |
| + if w.Code >= 300 || adds != 1 { |
| + t.Fatalf("labelled control: %d %s, add ops %d", w.Code, w.Body, adds) |
| + } |
| + return |
| + } |
| + if w.Code != http.StatusConflict || !strings.Contains(w.Body.String(), want) || adds != 0 { |
| + t.Fatalf("unlabelled: %d %s, add ops %d, want 409 %q and nothing reserved", w.Code, w.Body, adds, want) |
| + } |
| + }) |
| + } |
| +} |
diff --git a/internal/console/fleets.go b/internal/console/fleets.go| index 06f861fbc..4257d039d 100644 |
| --- a/internal/console/fleets.go |
| +++ b/internal/console/fleets.go |
| @@ -414,6 +414,13 @@ func (s *Server) handleAPIFleetAddBox(w http.ResponseWriter, r *http.Request) { |
| "invalid box label — 1-32 characters of a-z, 0-9 and hyphen") |
| return |
| } |
| + // #1008: a second active box would cut off an unlabelled instance that |
| + // runs a stateless build on a one-box fleet (the coordinator's sole-box |
| + // rule). AddFleetBox both adds and reactivates, so this covers both. |
| + if ref := s.fleetGrowthRefusal(r.Context(), reg, acct, fleet.Name, label); ref != nil { |
| + writeDoorRefusalJSON(w, r, ref) |
| + return |
| + } |
| plaintext, hash, err := mintFleetToken() |
| if err != nil { |
| log.Printf("console: fleet add-box %s/%s: mint token: %v", acct.Sqid, fleet.Name, err) |
diff --git a/internal/console/managedapi.go b/internal/console/managedapi.go| index cae3d2791..a957f8c5a 100644 |
| --- a/internal/console/managedapi.go |
| +++ b/internal/console/managedapi.go |
| @@ -137,7 +137,7 @@ func (s *Server) handleAPIManagedFleetCreate(w http.ResponseWriter, r *http.Requ |
| s.managedAPIReserved(w, r, op) |
| } |
| func (s *Server) handleAPIManagedFleetAdd(w http.ResponseWriter, r *http.Request) { |
| - addr, acct, fleet, _, ok := s.apiFleet(w, r) |
| + addr, acct, fleet, reg, ok := s.apiFleet(w, r) |
| if !ok { |
| return |
| } |
| @@ -181,6 +181,13 @@ func (s *Server) handleAPIManagedFleetAdd(w http.ResponseWriter, r *http.Request |
| if !s.managedAPIEntitlement(w, acct) { |
| return |
| } |
| + // #1008: growth past one active box must not cut off an unlabelled |
| + // instance running a stateless build. Judged before the reservation, |
| + // so nothing is provisioned on a refusal. |
| + if ref := s.fleetGrowthBy(r.Context(), reg, acct, fleet.Name, "", req.Boxes); ref != nil { |
| + writeDoorRefusalJSON(w, r, ref) |
| + return |
| + } |
| op, _, _, err := s.reserveManagedFleet(r.Context(), req) |
| if err != nil { |
| s.managedAPIError(w, err) |
diff --git a/internal/console/rootfs.go b/internal/console/rootfs.go| index 0752ad035..e0e4a7dae 100644 |
| --- a/internal/console/rootfs.go |
| +++ b/internal/console/rootfs.go |
| @@ -79,7 +79,7 @@ func (s *Server) statelessVersionRefusal(ctx context.Context, acct, app, channel |
| if err != nil { |
| return nil |
| } |
| - return s.statelessRuntimeRefusal(ctx, channel, m, nil, "") |
| + return s.statelessPublicationRefusal(ctx, acct, app, channel, m, nil, "") |
| } |
| type statelessSelectionError struct{ ref *doorRefusal } |
diff --git a/internal/console/server.go b/internal/console/server.go| index 98e73d774..d56ffc903 100644 |
| --- a/internal/console/server.go |
| +++ b/internal/console/server.go |
| @@ -5876,7 +5876,7 @@ func (s *Server) handlePromote(w http.ResponseWriter, r *http.Request) { |
| back("err", ref.Msg) |
| return |
| } |
| - if ref := s.statelessRuntimeRefusal(ctx, channel, man, nil, ""); ref != nil { |
| + if ref := s.statelessPublicationRefusal(ctx, acct.Sqid, app, channel, man, nil, ""); ref != nil { |
| back("err", ref.Msg) |
| return |
| } |
| @@ -6610,7 +6610,7 @@ func (s *Server) handleNewCanary(w http.ResponseWriter, r *http.Request) { |
| back("err", ref.Msg) |
| return |
| } |
| - if ref := s.statelessRuntimeRefusal(ctx, channel, man, nil, ""); ref != nil { |
| + if ref := s.statelessPublicationRefusal(ctx, acct.Sqid, app, channel, man, nil, ""); ref != nil { |
| back("err", ref.Msg) |
| return |
| } |