feat: console checks fleet sizes against every possible box (#892 S2)
Task 2 of the S2 plan. internal/console/fleetsize.go adds the one fit predicate, sizeFits (fleet budget for 4g..192g, quarter rule for small..xlarge), fleetSizeRefusal for the policy door, appFleetSize and the single destination gate appDestinationRefusal (ruling P7), judged against the app's largest fleet size, stale overrides included. The policy door checks a fleet size app-wide over every record and spare roster row (sizeBoxes now returns fleetOnly) and answers PS-2-C03 warnings (P8). Placement and every create door refuse with PS-2-C14; replica placement, the copies door, the relabel worker and the drain skip boxes the gate refuses. Size messages are PS-2-C09 and PS-2-C13.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
internal/console/apipolicy.go+10 −4internal/console/apipolicy_test.go+2 −2internal/console/appcopies.go+8 −4internal/console/copiespublication.go+6 −0internal/console/fleetbox.go+10 −4internal/console/fleetcreate.go+1 −1internal/console/fleetrelabel.go+7 −4internal/console/fleetrelabel_test.go+1 −1internal/console/fleets.go+6 −0internal/console/fleetsize.go+324 −0internal/console/fleetsize_test.go+434 −0internal/console/policysize.go+56 −16internal/console/policysize_test.go+11 −11internal/console/replicaplacement.go+10 −1
Intent
<task-notification> <task-id>a508e1a686688a589</task-id> <tool-use-id>toolu_01Uyprsu3meLQJPfeE9S8y2o</tool-use-id> <output-file>/home/paulca/.cache/tmp/claude-1001/-home-paulca-amadan-net-carlos-platform/29a43d67-87d2-4557-8e9d-9b70d303a90b/tasks/a508e1a686688a589.output</output-file> <status>completed</status> <summary>Agent "Implement S2 Task 1" finished</summary> <note>A task-notification fires each time this agent stops with no live background children of its own. The user can send it another message and resume it, so the same task-id may notify more than once.</note> <result>Status: DONE Commits: - `d6d9537e fix: read systemd's minute quota form for fleet sizes (#892 S2 review)` (new) - `607b5689 feat: fleet sizes 4g to 192g in the size table and root's gate (#892 S2)` Fix: `cpuQuotaShown` now prints the CPU quota the way systemd 257 does (`format_timespan` with accuracy 0, read from the source logic). So 32g, 48g, 96g and 192g read back as "1min 4s", "1min 36s", "3min 12s" and "6min 24s". Before, they read back as quota 0, so the activator kept retrying the resize. Quotas under a minute print exactly as before. Tests: The new fixtures, all literal strings, failed before the fix and pass now. They cover the quota string for every fleet size, `parseSizeShow` and `sliceAtTier` for 24g to 192g, and a `/size 32g` call that reads back 6400. The instancectl size, parent, slice, tier, gen, start and rootfs tests pass, and so do the activator Size tests. Census: The card shows only the `internal/instancectl` envelope, re-pinned. I re-ran `census-propose` with the same NAME and claims and none of the claim text needed to change. The units and the one new disposition (`ReadMemTotal`) are the same as before, and the census tests pass. Concerns: The two concerns from round 1 still stand: the agent's strict cgroup paths take the fleet gate, and this should not merge before Task 2. Both commits printed a `packed-refs.lock` error from another git process, but both landed and the tree is clean. Report: /home/paulca/amadan.net/carlos/platform/.claude/worktrees/stateless-scale-1/.superpowers/sdd/2026-11-22-892-s2-fleet-sizes/task-1-report.md (fix round 1 section appended)</result> <usage><subagent_tokens>220216</subagent_tokens><tool_uses>12</tool_uses><duration_ms>202690</duration_ms></usage> </task-notification>
<task-notification> <task-id>b76hhhe2e</task-id> <tool-use-id>toolu_012raYeaTVJ5GUDRAsa58K3k</tool-use-id> <output-file>/home/paulca/.cache/tmp/claude-1001/-home-paulca-amadan-net-carlos-platform/29a43d67-87d2-4557-8e9d-9b70d303a90b/tasks/b76hhhe2e.output</output-file> <status>completed</status> <summary>Background command "WS=/home/paulca/amadan.net/carlos/platform/.claude/worktrees/stateless-scale-1/.superpowers/sdd/2026-11-22-892-s2-fleet-sizes; $WS/astra.sh $WS/astra-task1-r2-brief.md $WS/task-1-rereview-1-astra.md > $WS/astra-task1-r2.log 2>&1" completed (exit code 0)</summary> </task-notification>
Attributed to this commit: 33.8 g CO₂e (1,589,638 tokens). This is an attribution, not a measurement — usage counted from this commit's own marker forward to the next one on stateless-scale-2-fleet-sizes, never backward from it. A turn that produced more than one commit books all of its tokens to the last one, and a concurrent session on this branch can add to the same figure. Some of it was priced using the shared default rate, because no model-specific factor row exists yet for at least one model involved. The Carbon view has the repo total, which is never derived from this attribution.
diff --git a/internal/console/apipolicy.go b/internal/console/apipolicy.go| index 03b8628fa..bf005aff4 100644 |
| --- a/internal/console/apipolicy.go |
| +++ b/internal/console/apipolicy.go |
| @@ -60,11 +60,12 @@ import ( |
| "github.com/carlosframework/platform/internal/release" |
| ) |
| -// Door copy (approved by delegation, Luna pass 2026-10-26; deck copy-instance-policy-plan1.md). |
| +// Door copy (approved by delegation, Luna pass 2026-10-26; deck copy-instance-policy-plan1.md; |
| +// the two size lists are PS-2-C09 and PS-2-C13 of the S2 deck). |
| const ( |
| policyNothingMsg = "nothing to set: send egress_allow, default_size or size" |
| - policyDefaultSizeMsg = "default_size must be small, standard, large or xlarge, or empty for the build's size" |
| - policySizeMsg = "size must be small, standard, large, xlarge or default" |
| + policyDefaultSizeMsg = "default_size must be small, standard, large, xlarge, one of 4g, 6g, 8g, 12g, 16g, 24g, 32g, 48g, 96g or 192g, or empty for the build's size" |
| + policySizeMsg = "size must be small, standard, large, xlarge, one of 4g, 6g, 8g, 12g, 16g, 24g, 32g, 48g, 96g or 192g, or default" |
| policyNoInstanceMsg = "no instance %s in this app" |
| policyPreconditionMsg = "send If-Match with the policy's etag, or If-None-Match: * when there is none" |
| policyChangedMsg = "the policy changed since it was read. read it again and retry" |
| @@ -106,6 +107,10 @@ type policyReply struct { |
| // Committed (PUT only) is each box's committed figure after a size |
| // change (policysize.go): information, never a gate. |
| Committed []sizeCommittedBox `json:"committed,omitempty"` |
| + // Warnings (PUT only) is PS-2-C03 for each fleet box whose sizes may |
| + // add up to more than its budget after a fleet size change |
| + // (fleetSizeWarnings, #892 S2 P8): information, never a gate. |
| + Warnings []string `json:"warnings,omitempty"` |
| } |
| // policyLock is the app's policy mutex, the store's (Store.AppPolicyLock), so |
| @@ -531,7 +536,7 @@ func (s *Server) handleAPIPolicyPut(w http.ResponseWriter, r *http.Request) { |
| if s.cfg.HostClaim { |
| pol.AppID = cur |
| } |
| - committed, ref := s.policySizeCheck(ctx, acct, app, old, pol, req) |
| + committed, warnings, ref := s.policySizeCheck(ctx, acct, app, old, pol, req) |
| if ref != nil { |
| jsonError(w, ref.Status, ref.Msg) |
| return |
| @@ -590,6 +595,7 @@ func (s *Server) handleAPIPolicyPut(w http.ResponseWriter, r *http.Request) { |
| s.bumpSerial(ctx) |
| reply.NotChecked = notChecked |
| reply.Committed = committed |
| + reply.Warnings = warnings |
| w.Header().Set("ETag", reply.ETag) |
| writeJSON(w, http.StatusOK, reply) |
| } |
diff --git a/internal/console/apipolicy_test.go b/internal/console/apipolicy_test.go| index 3997669d5..94ae169e9 100644 |
| --- a/internal/console/apipolicy_test.go |
| +++ b/internal/console/apipolicy_test.go |
| @@ -391,8 +391,8 @@ func TestPolicyPutRefusesBeforeWriting(t *testing.T) { |
| {`{"egress_allow":["10.0.5.20:5432"]}`, http.StatusBadRequest, ""}, |
| {`{"egress_allow":["8.8.8.8"]}`, http.StatusBadRequest, ""}, |
| {`{"egress_allow":[` + strings.Join(nine, ",") + `]}`, http.StatusBadRequest, ""}, |
| - {`{"default_size":"huge"}`, http.StatusBadRequest, "default_size must be small, standard, large or xlarge, or empty for the build's size"}, |
| - {`{"size":{"host":"shop-a.example.com","value":"huge"}}`, http.StatusBadRequest, "size must be small, standard, large, xlarge or default"}, |
| + {`{"default_size":"huge"}`, http.StatusBadRequest, "default_size must be small, standard, large, xlarge, one of 4g, 6g, 8g, 12g, 16g, 24g, 32g, 48g, 96g or 192g, or empty for the build's size"}, |
| + {`{"size":{"host":"shop-a.example.com","value":"huge"}}`, http.StatusBadRequest, "size must be small, standard, large, xlarge, one of 4g, 6g, 8g, 12g, 16g, 24g, 32g, 48g, 96g or 192g, or default"}, |
| {`{"size":{"host":"../x","value":"large"}}`, http.StatusBadRequest, "invalid host"}, |
| {`{"egress_alow":[]}`, http.StatusBadRequest, ""}, |
| {`{"size":{"host":"shop-a.example.com","value":"large"}}`, http.StatusNotFound, "no instance shop-a.example.com in this app"}, |
diff --git a/internal/console/appcopies.go b/internal/console/appcopies.go| index 89639f735..770c1dd5b 100644 |
| --- a/internal/console/appcopies.go |
| +++ b/internal/console/appcopies.go |
| @@ -329,7 +329,7 @@ func (s *Server) copiesRefusal(ctx context.Context, acct *Account, app string, n |
| // The serving-box check applies only when the count is raised (round |
| // 2): lowering it never needs a box to take a copy. |
| raising := n > appconfig.CopiesOf(&cfg.manifest) |
| - if ref := s.copiesSizeRefusal(ctx, acct, app, v, n, recs, rootfs, raising); ref != nil { |
| + if ref := s.copiesSizeRefusal(ctx, acct, app, fleet, v, n, recs, rootfs, raising); ref != nil { |
| return ref |
| } |
| // Reuse the heartbeat read already memoized by loadFleetView. Only this |
| @@ -412,12 +412,16 @@ func (s *Server) copiesRefusal(ctx context.Context, acct *Account, app string, n |
| // boxes are eligible for some live instance at its effective size |
| // (apppolicy.Policy.SizeFor, standard when none), by placement's own |
| // predicate (replicaCandidates). An app with no live instance yet is |
| -// judged at its default size. A policy that cannot be read refuses. |
| -func (s *Server) copiesSizeRefusal(ctx context.Context, acct *Account, app string, v fleetView, n int, recs []provision.Record, rootfs, raising bool) *doorRefusal { |
| +// judged at its default size. A policy that cannot be read refuses. A box |
| +// the app's destination gate refuses (appDestinationRefusal, #892 S2 P7: |
| +// the app's largest fleet size) is not eligible either. fleet is the |
| +// fleet, one acct owns, that v views. |
| +func (s *Server) copiesSizeRefusal(ctx context.Context, acct *Account, app, fleet string, v fleetView, n int, recs []provision.Record, rootfs, raising bool) *doorRefusal { |
| policy, _, err := apppolicy.Get(ctx, s.cfg.Releases, s.cfg.ReleasePub, acct.Sqid, app, time.Now()) |
| if err != nil && !errors.Is(err, os.ErrNotExist) { |
| return &doorRefusal{Status: http.StatusServiceUnavailable, Msg: copiesUnread} |
| } |
| + v.DestRefused = s.appDestinationsRefused(withAppPolicyRead(ctx, acct, app, policy), acct, app, fleet, v.Boxes) |
| live := make([]provision.Record, 0, len(recs)) |
| for _, rec := range recs { |
| if !rec.Retired { |
| @@ -429,7 +433,7 @@ func (s *Server) copiesSizeRefusal(ctx context.Context, acct *Account, app strin |
| if rootfs { |
| runtime = provision.RuntimeRootfsV1 |
| } |
| - live = append(live, provision.Record{Runtime: runtime}) |
| + live = append(live, provision.Record{Account: acct.Sqid, App: app, Runtime: runtime}) |
| } |
| for _, rec := range live { |
| size := replicaSizeName(policy.SizeFor(rec.Host, rec.CreatedAt)) |
diff --git a/internal/console/copiespublication.go b/internal/console/copiespublication.go| index 7138caf16..236733727 100644 |
| --- a/internal/console/copiespublication.go |
| +++ b/internal/console/copiespublication.go |
| @@ -231,6 +231,12 @@ func (s *Server) copiesCreateCheck(ctx context.Context, rec *provision.Record) e |
| if !ok { |
| return nil |
| } |
| + // Every create door: an app with a fleet size gets a new instance only |
| + // where each box it can run on has room for that size (#892 S2 P7, |
| + // PS-2-C14), under the app lock the caller holds. |
| + if ref := s.appTargetRefusal(ctx, &Account{ID: admitted.account, Sqid: rec.Account}, rec.App, rec.Region); ref != nil { |
| + return &statelessSelectionError{ref} |
| + } |
| cfg, err := s.copiesDefault(ctx, rec.Account, rec.App) |
| if err == nil && !cfg.explicit { |
| if ref := s.copiesStillApp(ctx, &Account{ID: admitted.account, Sqid: rec.Account}, rec.App); ref != nil { |
diff --git a/internal/console/fleetbox.go b/internal/console/fleetbox.go| index a3e5b1d64..988bce8da 100644 |
| --- a/internal/console/fleetbox.go |
| +++ b/internal/console/fleetbox.go |
| @@ -62,6 +62,10 @@ type fleetView struct { |
| ReplicaManifests map[string]*release.Manifest |
| ReplicaCounts map[AppRef]int |
| ReplicaSizes map[string]string |
| + // DestRefused is the boxes appDestinationRefusal refuses for the one |
| + // app being placed (#892 S2 P7: replica placement and the copies door |
| + // set it on their own copy of the view); nil refuses none. |
| + DestRefused map[string]bool |
| } |
| // fleetBoxView is one active box of the fleet (registry status "active"), |
| @@ -149,7 +153,7 @@ func (v fleetView) labelActive() bool { |
| // ties broken by label. "" when there is none. It asks nothing of the other |
| // boxes, so a survivor is chosen whatever state its siblings are in. |
| func (v fleetView) chooseDestination(rootfs bool, exclude string) string { |
| - return v.chooseMoveDestination(rootfs, false, exclude) |
| + return v.chooseMoveDestination(rootfs, false, exclude, nil) |
| } |
| // chooseMoveDestination is chooseDestination for a record a label is acted |
| @@ -157,11 +161,13 @@ func (v fleetView) chooseDestination(rootfs bool, exclude string) string { |
| // release is stateless (movableVerified) also needs a box at the stateless |
| // runtime floor (release.StatelessRuntimeFloor), above the label floor, as |
| // one-copy placement asks (replicaCandidates): a box below it cannot run |
| -// the release (M1 review Astra P1-1). |
| -func (v fleetView) chooseMoveDestination(rootfs, stateless bool, exclude string) string { |
| +// the release (M1 review Astra P1-1). refused is the boxes the record's |
| +// app may not be put on (appDestinationsRefused, #892 S2 P7), skipped like |
| +// any other unfit box; nil refuses none. |
| +func (v fleetView) chooseMoveDestination(rootfs, stateless bool, exclude string, refused map[string]bool) string { |
| best, bestLoad := "", 0 |
| for _, b := range v.Boxes { |
| - if b.Label == exclude || !b.destinationFor(rootfs) || (stateless && !atStatelessRuntimeFloor(b.Release)) { |
| + if b.Label == exclude || refused[b.Label] || !b.destinationFor(rootfs) || (stateless && !atStatelessRuntimeFloor(b.Release)) { |
| continue |
| } |
| load := v.Load[b.Label] |
diff --git a/internal/console/fleetcreate.go b/internal/console/fleetcreate.go| index 13db06ba2..0d94c2c44 100644 |
| --- a/internal/console/fleetcreate.go |
| +++ b/internal/console/fleetcreate.go |
| @@ -136,7 +136,7 @@ func (s *Server) fleetCreateLabel(ctx context.Context, reg consoleRegistry, flee |
| } |
| return unavailable |
| } |
| - box := v.chooseMoveDestination(rec.Runtime == provision.RuntimeRootfsV1, m != nil && m.Stateless, "") |
| + box := v.chooseMoveDestination(rec.Runtime == provision.RuntimeRootfsV1, m != nil && m.Stateless, "", nil) |
| if box == "" { |
| log.Printf("%s: no box can take it; refused", tag) |
| return unavailable |
diff --git a/internal/console/fleetrelabel.go b/internal/console/fleetrelabel.go| index b5176106b..cb1a8f388 100644 |
| --- a/internal/console/fleetrelabel.go |
| +++ b/internal/console/fleetrelabel.go |
| @@ -209,6 +209,7 @@ type relabel struct { |
| // (D2); a manifest that cannot be verified leaves its record, and a write |
| // that fails is logged and the others still run. |
| func (s *Server) relabelFleet(ctx context.Context, fleet string, v fleetView, now time.Time) (moved int, err error) { |
| + ctx = withAppPolicyMemo(ctx) // each app's policy read once a pass (#892 S2 P7) |
| if box, rel := v.labelsPausedBy(); box != "" { |
| log.Printf("console: ERROR fleet %s: box %s reports release %s, below the label floor %s: relabelling is paused until it is updated", |
| fleet, box, releaseOrUnknown(rel), release.FleetBoxLabelFloor) |
| @@ -243,7 +244,8 @@ func (s *Server) relabelFleet(ctx context.Context, fleet string, v fleetView, no |
| log.Printf("console: fleet relabel %s: %s on %s (%s) is not a record the platform can move; left", fleet, rec.Host, rec.FleetBox, reason) |
| continue |
| } |
| - to := v.chooseMoveDestination(rec.Runtime == provision.RuntimeRootfsV1, stateless, rec.FleetBox) |
| + refused := s.recordDestinationsRefused(ctx, rec, fleet, v.Boxes) |
| + to := v.chooseMoveDestination(rec.Runtime == provision.RuntimeRootfsV1, stateless, rec.FleetBox, refused) |
| if to == "" { |
| log.Printf("console: fleet relabel %s: %s on %s (%s) has no box to move to; left", fleet, rec.Host, rec.FleetBox, reason) |
| continue |
| @@ -468,6 +470,7 @@ func (e *fleetPausedError) Error() string { return msgLabelsPaused(e.fleet, e.bo |
| // every record of a draining box; so is one whose write failed, which is |
| // err. |
| func (s *Server) drainBox(ctx context.Context, fleet, label string) (stuck []string, err error) { |
| + ctx = withAppPolicyMemo(ctx) // each app's policy read once a drain (#892 S2 P7) |
| if s.cfg.HostClaim { |
| return nil, errFleetDrainHostClaim |
| } |
| @@ -554,7 +557,7 @@ func (s *Server) drainBox(ctx context.Context, fleet, label string) (stuck []str |
| // test reads nothing; the stateless one below needs the |
| // manifest and is asked only of a trashed app's record. |
| rootfs := rec.Runtime == provision.RuntimeRootfsV1 |
| - if v.chooseMoveDestination(rootfs, false, label) == "" { |
| + if v.chooseMoveDestination(rootfs, false, label, nil) == "" { |
| strand[rec.App] = true |
| continue |
| } |
| @@ -588,7 +591,7 @@ func (s *Server) drainBox(ctx context.Context, fleet, label string) (stuck []str |
| strand[rec.App] = true |
| continue |
| } |
| - if !ok || v.chooseMoveDestination(rootfs, stateless, label) == "" { |
| + if !ok || v.chooseMoveDestination(rootfs, stateless, label, s.recordDestinationsRefused(ctx, rec, fleet, v.Boxes)) == "" { |
| strand[rec.App] = true |
| continue |
| } |
| @@ -607,7 +610,7 @@ func (s *Server) drainBox(ctx context.Context, fleet, label string) (stuck []str |
| strand[rec.App] = true |
| continue |
| } |
| - to := v.chooseMoveDestination(rec.Runtime == provision.RuntimeRootfsV1, stateless, label) |
| + to := v.chooseMoveDestination(rec.Runtime == provision.RuntimeRootfsV1, stateless, label, s.recordDestinationsRefused(ctx, rec, fleet, v.Boxes)) |
| if to == "" { |
| strand[rec.App] = true |
| continue |
diff --git a/internal/console/fleetrelabel_test.go b/internal/console/fleetrelabel_test.go| index a63a67e46..3eaf98927 100644 |
| --- a/internal/console/fleetrelabel_test.go |
| +++ b/internal/console/fleetrelabel_test.go |
| @@ -656,7 +656,7 @@ func TestDrainRacingARestoreLeavesTheInstanceOnTheSurvivor(t *testing.T) { |
| if err != nil { |
| t.Fatal(err) |
| } |
| - if to := v.chooseMoveDestination(r.Runtime == provision.RuntimeRootfsV1, stateless, "pi-1"); to != "pi-2" { |
| + if to := v.chooseMoveDestination(r.Runtime == provision.RuntimeRootfsV1, stateless, "pi-1", nil); to != "pi-2" { |
| t.Fatalf("the restored instance's survivor = %q; want pi-2, able to run it (stateless %v): %+v", to, stateless, v.Boxes) |
| } |
| } |
diff --git a/internal/console/fleets.go b/internal/console/fleets.go| index aabe82b1d..376125d94 100644 |
| --- a/internal/console/fleets.go |
| +++ b/internal/console/fleets.go |
| @@ -1012,6 +1012,12 @@ func (s *Server) placeAppTarget(ctx context.Context, acct *Account, app, target |
| if len(stale) > 0 { |
| return http.StatusConflict, staleSealedRefusal(app, stale) |
| } |
| + // An app with a fleet size moves only to a fleet of the account's own |
| + // whose every box has room for it (#892 S2 P7, PS-2-C14), before |
| + // anything is written. |
| + if ref := s.appTargetRefusal(ctx, acct, app, target); ref != nil { |
| + return ref.Status, ref.Msg |
| + } |
| // The store-of-record write (2026-08-06 ruling): appPlacement consults |
| // THIS before it ever walks a route. It has to land before the records |
diff --git a/internal/console/fleetsize.go b/internal/console/fleetsize.go| new file mode 100644 |
| index 000000000..1fe5c335d |
| --- /dev/null |
| +++ b/internal/console/fleetsize.go |
| @@ -0,0 +1,324 @@ |
| +// Fleet sizes at the console's doors (#892 S2, plan 2026-11-22 Task 2, |
| +// rulings P7 and P8). |
| +// |
| +// The sizes after xlarge (cgroup.FleetSize: 4g to 192g) run only on boxes |
| +// of a fleet the account owns, up to each box's budget |
| +// (cgroup.FleetMemoryBudget: its memory less max(1 GiB, an eighth)). Every |
| +// door that can put a size on a box, or put an instance of an app on a box, |
| +// asks the one fit predicate here, sizeFits, so no two of them disagree: |
| +// |
| +// - the policy door (policySizeCheck), through fleetSizeRefusal, over every |
| +// possible destination of every instance of the app; |
| +// - app placement, create, replica placement, the copies door, and the |
| +// relabel worker and the drain, through appDestinationRefusal, which |
| +// judges a destination against the app's whole policy (appFleetSize), |
| +// never against the moved instance's own size. |
| +// |
| +// small to xlarge keep the quarter rule (sizeRefusal, R12) everywhere. |
| +package console |
| + |
| +import ( |
| + "context" |
| + "fmt" |
| + "net/http" |
| + "sync" |
| + |
| + "github.com/carlosframework/platform/internal/apppolicy" |
| + "github.com/carlosframework/platform/internal/cgroup" |
| + "github.com/carlosframework/platform/internal/provision" |
| + "github.com/carlosframework/platform/internal/registry" |
| +) |
| + |
| +// Door copy (deck 2026-11-11-stateless-replicas-scale-copy.md, section S2). |
| +const ( |
| + // PS-2-C01 |
| + fleetSizeRoomMsg = "%s leaves too little memory on %s. choose a smaller size" |
| + // PS-2-C02 |
| + fleetSizeSharedMsg = "%s is only for apps on your own fleet. choose xlarge or smaller" |
| + // PS-2-C03 |
| + fleetSizeWarnMsg = "warning: sizes on %s may now add up to %d MiB, more than the %d MiB it can give. an instance there may run out of memory" |
| + // PS-2-C14 |
| + fleetSizeMoveMsg = "%s uses size %s, which needs a fleet of your own whose every box has room for it. choose such a fleet, or set a size of xlarge or smaller first" |
| +) |
| + |
| +// sizeFits is the one fit predicate: whether a box of totalMiB (0 is |
| +// unknown) can hold size. A fleet size needs a known total whose fleet |
| +// budget is at least the size. small to xlarge keep the quarter rule; with |
| +// an unknown total only small and standard fit. |
| +func sizeFits(totalMiB int64, size cgroup.Size) bool { |
| + if cgroup.FleetSize(size.Name) { |
| + return totalMiB > 0 && size.Limit.MemoryMax <= cgroup.FleetMemoryBudget(totalMiB<<20) |
| + } |
| + if totalMiB <= 0 { |
| + return !sizeAboveStandard(size) |
| + } |
| + return size.Limit.MemoryMax>>20 <= totalMiB/4 |
| +} |
| + |
| +// fleetSizeRefusal is the policy door's rule for a fleet size over one |
| +// read of every box the app's instances can be placed on (sizeBoxes), |
| +// sorted by label; nil when the size is allowed. fleetOnly is sizeBoxes's: |
| +// every instance, or the app with none, is placed on a fleet the account |
| +// owns. A size refused anywhere else (a platform box, a mixed placement, a |
| +// route the platform does not run) is PS-2-C02. Then, as for sizes above |
| +// standard: an incomplete read is 503 and a box with no total is 409. Last, |
| +// the smallest box must hold the size (sizeFits), else PS-2-C01 names it. |
| +func fleetSizeRefusal(boxes []sizeBox, complete, fleetOnly bool, size cgroup.Size) *doorRefusal { |
| + if !fleetOnly { |
| + return &doorRefusal{Status: http.StatusConflict, Msg: fmt.Sprintf(fleetSizeSharedMsg, size.Name)} |
| + } |
| + if !complete { |
| + return &doorRefusal{Status: http.StatusServiceUnavailable, Msg: sizeUnreadMsg} |
| + } |
| + for _, b := range boxes { |
| + if b.fleet == "" { |
| + return &doorRefusal{Status: http.StatusConflict, Msg: fmt.Sprintf(fleetSizeSharedMsg, size.Name)} |
| + } |
| + } |
| + for _, b := range boxes { |
| + if b.TotalMiB == 0 { |
| + return &doorRefusal{Status: http.StatusConflict, Msg: fmt.Sprintf(sizeNoTotalMsg, b.Box)} |
| + } |
| + } |
| + var smallest *sizeBox |
| + for i := range boxes { |
| + if b := &boxes[i]; smallest == nil || b.TotalMiB < smallest.TotalMiB { |
| + smallest = b |
| + } |
| + } |
| + if smallest != nil && !sizeFits(smallest.TotalMiB, size) { |
| + return &doorRefusal{Status: http.StatusConflict, Msg: fmt.Sprintf(fleetSizeRoomMsg, size.Name, smallest.Box)} |
| + } |
| + return nil |
| +} |
| + |
| +// appFleetSize is the largest fleet size p names, in its default or in any |
| +// override, a stale one (an instance gone or recreated) included: every box |
| +// reading the policy validates every override (Global Constraints). ok is |
| +// false when p names no fleet size. |
| +func appFleetSize(p apppolicy.Policy) (cgroup.Size, bool) { |
| + var best cgroup.Size |
| + ok := false |
| + consider := func(name string) { |
| + if !cgroup.FleetSize(name) { |
| + return |
| + } |
| + if sz, found := cgroup.SizeByName(name); found && (!ok || sz.Limit.MemoryMax > best.Limit.MemoryMax) { |
| + best, ok = sz, true |
| + } |
| + } |
| + consider(p.DefaultSize) |
| + for _, o := range p.Sizes { |
| + consider(o.Size) |
| + } |
| + return best, ok |
| +} |
| + |
| +// appPolicyMemoKey holds a per-pass memo of appFleetSizeOf's answers, so a |
| +// door or worker judging many boxes reads each app's policy once. |
| +type appPolicyMemoKey struct{} |
| + |
| +type appPolicyMemo struct { |
| + mu sync.Mutex |
| + answers map[string]appFleetSizeAnswer |
| +} |
| + |
| +type appFleetSizeAnswer struct { |
| + size cgroup.Size |
| + ok bool |
| + ref *doorRefusal |
| +} |
| + |
| +// withAppPolicyMemo is ctx with a policy memo, or ctx itself when it |
| +// already has one. |
| +func withAppPolicyMemo(ctx context.Context) context.Context { |
| + if _, ok := ctx.Value(appPolicyMemoKey{}).(*appPolicyMemo); ok { |
| + return ctx |
| + } |
| + return context.WithValue(ctx, appPolicyMemoKey{}, &appPolicyMemo{answers: map[string]appFleetSizeAnswer{}}) |
| +} |
| + |
| +// withAppPolicyRead is withAppPolicyMemo holding p, app's policy as the |
| +// caller has just read it (nil: none), so a worker that already read the |
| +// policy for its own use (replica placement, the copies door) judges |
| +// destinations from that read and makes no second one (request budget). |
| +func withAppPolicyRead(ctx context.Context, acct *Account, app string, p *apppolicy.Policy) context.Context { |
| + ctx = withAppPolicyMemo(ctx) |
| + memo := ctx.Value(appPolicyMemoKey{}).(*appPolicyMemo) |
| + var a appFleetSizeAnswer |
| + if p != nil { |
| + a.size, a.ok = appFleetSize(*p) |
| + } |
| + memo.mu.Lock() |
| + memo.answers[acct.Sqid+"/"+app] = a |
| + memo.mu.Unlock() |
| + return ctx |
| +} |
| + |
| +// appFleetSizeOf is appFleetSize of app's policy as the policy door reads |
| +// it (policyBase, the read its GET makes: nothing recorded, no holding |
| +// marked). Never from provision.Record, which carries no size, and never |
| +// from fleetView.ReplicaSizes, which skips unassigned records. A policy |
| +// that cannot be read refuses (fail closed): 503 policyReadFailedMsg. |
| +func (s *Server) appFleetSizeOf(ctx context.Context, acct *Account, app string) (cgroup.Size, bool, *doorRefusal) { |
| + key := acct.Sqid + "/" + app |
| + memo, _ := ctx.Value(appPolicyMemoKey{}).(*appPolicyMemo) |
| + if memo != nil { |
| + memo.mu.Lock() |
| + a, hit := memo.answers[key] |
| + memo.mu.Unlock() |
| + if hit { |
| + return a.size, a.ok, a.ref |
| + } |
| + } |
| + var a appFleetSizeAnswer |
| + base, _, _, _, err := s.policyBase(ctx, acct, app, s.policyReadHolding(), false) |
| + if err != nil { |
| + s.policyLogf("console: fleet size %s/%s: policy: %v", acct.Sqid, app, err) |
| + a.ref = &doorRefusal{Status: http.StatusServiceUnavailable, Msg: policyReadFailedMsg} |
| + } else { |
| + a.size, a.ok = appFleetSize(base) |
| + } |
| + if memo != nil { |
| + memo.mu.Lock() |
| + memo.answers[key] = a |
| + memo.mu.Unlock() |
| + } |
| + return a.size, a.ok, a.ref |
| +} |
| + |
| +// appDestinationRefusal is the one destination gate (P7): whether any |
| +// instance of app may be put on dest, judged against the app's whole policy |
| +// (appFleetSizeOf), whichever instance is moving and whatever its own size. |
| +// dest.fleet is "" for a platform box and otherwise a fleet acct owns. An |
| +// app whose policy names no fleet size passes exactly as before. One that |
| +// does needs dest to be an owned fleet's box with room for its largest |
| +// fleet size (sizeFits), else PS-2-C14. |
| +func (s *Server) appDestinationRefusal(ctx context.Context, acct *Account, app string, dest sizeBox) *doorRefusal { |
| + size, ok, ref := s.appFleetSizeOf(ctx, acct, app) |
| + if ref != nil { |
| + return ref |
| + } |
| + if !ok { |
| + return nil |
| + } |
| + // Task 3 (#892 S2): the FleetSizesFloor condition on dest and on the |
| + // console joins here, so every caller asks it. |
| + if dest.fleet == "" || !sizeFits(dest.TotalMiB, size) { |
| + return &doorRefusal{Status: http.StatusConflict, Msg: fmt.Sprintf(fleetSizeMoveMsg, app, size.Name)} |
| + } |
| + return nil |
| +} |
| + |
| +// appTargetRefusal is appDestinationRefusal for every box an instance of |
| +// app placed at region can run on: a platform region is one platform |
| +// destination; a fleet is each active roster row of it (sizeBoxes, spare |
| +// rows included), and an incomplete read or a fleet acct does not own is |
| +// refused. App placement and create ask it. An app with no fleet size reads |
| +// nothing more than its policy. |
| +func (s *Server) appTargetRefusal(ctx context.Context, acct *Account, app, region string) *doorRefusal { |
| + ctx = withAppPolicyMemo(ctx) |
| + size, ok, ref := s.appFleetSizeOf(ctx, acct, app) |
| + if ref != nil { |
| + return ref |
| + } |
| + if !ok { |
| + return nil |
| + } |
| + fleet, placed := registry.FleetPlacement(region) |
| + if !placed { |
| + return s.appDestinationRefusal(ctx, acct, app, sizeBox{Box: region}) |
| + } |
| + boxes, complete, fleetOnly, err := s.sizeBoxes(ctx, acct, nil, nil, fleet) |
| + if err != nil || !complete || !fleetOnly { |
| + if err != nil { |
| + s.policyLogf("console: fleet size %s/%s: boxes of %s: %v", acct.Sqid, app, fleet, err) |
| + } |
| + return &doorRefusal{Status: http.StatusConflict, Msg: fmt.Sprintf(fleetSizeMoveMsg, app, size.Name)} |
| + } |
| + for _, b := range boxes { |
| + if ref := s.appDestinationRefusal(ctx, acct, app, b); ref != nil { |
| + return ref |
| + } |
| + } |
| + return nil |
| +} |
| + |
| +// sizeBox is the box as appDestinationRefusal judges it, on fleet. |
| +func (b fleetBoxView) sizeBox(fleet string) sizeBox { |
| + return sizeBox{Box: b.Label, TotalMiB: b.TotalMiB, fleet: fleet} |
| +} |
| + |
| +// appDestinationsRefused is the labels of boxes, active boxes of fleet, |
| +// that appDestinationRefusal refuses for app; nil when it refuses none. |
| +// fleet is one acct owns: the copies door and replica placement check that |
| +// first, and the relabel worker and the drain act on records already |
| +// placed on it. Each app's policy is read once per ctx memo. |
| +func (s *Server) appDestinationsRefused(ctx context.Context, acct *Account, app, fleet string, boxes []fleetBoxView) map[string]bool { |
| + ctx = withAppPolicyMemo(ctx) |
| + var out map[string]bool |
| + for _, b := range boxes { |
| + if s.appDestinationRefusal(ctx, acct, app, b.sizeBox(fleet)) != nil { |
| + if out == nil { |
| + out = map[string]bool{} |
| + } |
| + out[b.Label] = true |
| + } |
| + } |
| + return out |
| +} |
| + |
| +// recordDestinationsRefused is appDestinationsRefused for rec's app, for |
| +// the relabel worker and the drain, which hold no account: an account |
| +// that cannot be read refuses every box, so the record takes the |
| +// no-destination path rather than move unchecked. |
| +func (s *Server) recordDestinationsRefused(ctx context.Context, rec provision.Record, fleet string, boxes []fleetBoxView) map[string]bool { |
| + acct, err := s.store.AccountBySqid(rec.Account) |
| + if err != nil || acct == nil { |
| + s.policyLogf("console: fleet size %s/%s: account: %v", rec.Account, rec.App, err) |
| + all := make(map[string]bool, len(boxes)) |
| + for _, b := range boxes { |
| + all[b.Label] = true |
| + } |
| + return all |
| + } |
| + return s.appDestinationsRefused(ctx, acct, rec.App, fleet, boxes) |
| +} |
| + |
| +// fleetSizeWarnings is the policy door's P8 warning (PS-2-C03), reply |
| +// information and never a refusal: for each fleet box with a memory total |
| +// and a committed figure in its heartbeat, the figure plus next's size for |
| +// every instance of the app placed on that box, against the box's fleet |
| +// budget. The heartbeat carries only the box's total, so this app's |
| +// current share is counted twice: an upper bound, which may warn early but |
| +// never misses a crossing. |
| +func fleetSizeWarnings(boxes []sizeBox, recs []provision.Record, next apppolicy.Policy) []string { |
| + var out []string |
| + for _, b := range boxes { |
| + if b.fleet == "" || b.TotalMiB <= 0 || b.report == nil || b.report.Metrics == nil || b.report.Metrics.SizeCommittedMiB == nil { |
| + continue |
| + } |
| + projected := *b.report.Metrics.SizeCommittedMiB |
| + for _, rec := range recs { |
| + if f, ok := registry.FleetPlacement(rec.Region); !ok || f != b.fleet || !recordOnBox(rec, b.Box) { |
| + continue |
| + } |
| + if sz, ok := cgroup.SizeByName(replicaSizeName(next.SizeFor(rec.Host, rec.CreatedAt))); ok { |
| + projected += sz.Limit.MemoryMax >> 20 |
| + } |
| + } |
| + if budget := cgroup.FleetMemoryBudget(b.TotalMiB<<20) >> 20; projected > budget { |
| + out = append(out, fmt.Sprintf(fleetSizeWarnMsg, b.Box, projected, budget)) |
| + } |
| + } |
| + return out |
| +} |
| + |
| +// recordOnBox reports whether rec runs on box: its label, or a box of its |
| +// copy assignment. |
| +func recordOnBox(rec provision.Record, box string) bool { |
| + if rec.ReplicaAssignment != nil { |
| + return rec.ReplicaAssignment.Contains(box) |
| + } |
| + return rec.FleetBox == box |
| +} |
diff --git a/internal/console/fleetsize_test.go b/internal/console/fleetsize_test.go| new file mode 100644 |
| index 000000000..75acf3ecd |
| --- /dev/null |
| +++ b/internal/console/fleetsize_test.go |
| @@ -0,0 +1,434 @@ |
| +package console |
| + |
| +import ( |
| + "context" |
| + "encoding/json" |
| + "fmt" |
| + "net/http" |
| + "slices" |
| + "strings" |
| + "testing" |
| + "time" |
| + |
| + "github.com/carlosframework/platform/internal/apppolicy" |
| + "github.com/carlosframework/platform/internal/provision" |
| + "github.com/carlosframework/platform/internal/registry" |
| + "github.com/carlosframework/platform/internal/release" |
| + "github.com/carlosframework/platform/internal/status" |
| +) |
| + |
| +// #892 S2 Task 2: the fleet sizes (4g to 192g) at every console door, with |
| +// one fit predicate (sizeFits) and one app-wide destination gate |
| +// (appDestinationRefusal). Apps shop and blog, fleets under neutral names, |
| +// hosts under example.com. |
| + |
| +const ( |
| + c01 = "%s leaves too little memory on %s. choose a smaller size" |
| + c02 = "%s is only for apps on your own fleet. choose xlarge or smaller" |
| +) |
| + |
| +// fleetMemBeat writes box's heartbeat on fleet: fresh, at every floor the |
| +// doors ask (release.FleetRecordLabelFloor), reporting mib of memory (0: |
| +// no total) and, when committed is not nil, that committed figure. |
| +func fleetMemBeat(t *testing.T, fx *consoleFixture, fleet, box string, mib int64, committed *int64) { |
| + t.Helper() |
| + m := &status.BoxMetrics{Layout: &status.LayoutReport{Mode: "nested"}, RootfsReady: true, |
| + Release: release.FleetRecordLabelFloor, PrevRelease: "none"} |
| + if mib > 0 { |
| + total, avail := mib<<20, (mib/2)<<20 |
| + m.MemTotalBytes, m.MemAvailBytes = &total, &avail |
| + } |
| + m.SizeCommittedMiB = committed |
| + b := status.Box{Label: box, GeneratedAt: time.Now(), Metrics: m} |
| + if err := status.WriteBox(context.Background(), release.NewPrefixStore(fx.rel, "fleets/"+fleet+"/"), b); err != nil { |
| + t.Fatal(err) |
| + } |
| +} |
| + |
| +// putShopPolicy signs p as shop's policy in the bucket, replacing any. |
| +func putShopPolicy(t *testing.T, fx *consoleFixture, p apppolicy.Policy) { |
| + t.Helper() |
| + _, cur, _ := fx.rel.Get(context.Background(), apppolicy.Key(fx.sqid, "shop")) |
| + old, _, _ := apppolicy.Get(context.Background(), fx.rel, fx.pub, fx.sqid, "shop", time.Now()) |
| + p.V, p.Account, p.App, p.SignedAt, p.Revision = 1, fx.sqid, "shop", time.Now(), 1 |
| + if old != nil { |
| + p.Revision = old.Revision + 1 |
| + } |
| + if _, err := apppolicy.PutIfMatchETag(context.Background(), fx.rel, fx.priv, p, cur); err != nil { |
| + t.Fatal(err) |
| + } |
| +} |
| + |
| +// fleetRec is shop's record of host placed on fleet, labelled to box when |
| +// box is not "", stored in the bucket. |
| +func fleetRec(t *testing.T, fx *consoleFixture, host, fleet, box string) provision.Record { |
| + t.Helper() |
| + rec := provision.Record{Schema: 1, Host: host, Slug: strings.Split(host, ".")[0], App: "shop", Account: fx.sqid, |
| + Region: registry.PlacementTarget(fleet), FleetBox: box, Channel: "stable", CreatedAt: time.Now().UTC().Truncate(time.Second)} |
| + if err := provision.CreateRecord(context.Background(), fx.rel, rec); err != nil { |
| + t.Fatal(err) |
| + } |
| + return rec |
| +} |
| + |
| +func wantFleetRefusal(t *testing.T, what string, got *doorRefusal, status int, msg string) { |
| + t.Helper() |
| + if got == nil || got.Status != status || got.Msg != msg { |
| + t.Fatalf("%s = %+v; want %d %q", what, got, status, msg) |
| + } |
| +} |
| + |
| +func TestFleetSizeDoorBudget(t *testing.T) { |
| + t.Parallel() |
| + // The predicate at the budget's edge, both arms of max(1 GiB, total/8): |
| + // up to 8 GiB the reserve is 1 GiB (5120 MiB gives 4096, 7168 gives |
| + // 6144); above it an eighth (9363 MiB gives 8192.6, 9362 gives 8191.75). |
| + for _, tc := range []struct { |
| + size string |
| + total int64 |
| + ok bool |
| + }{ |
| + {"4g", 5120, true}, {"4g", 5119, false}, |
| + {"6g", 7168, true}, {"6g", 7167, false}, |
| + {"8g", 9363, true}, {"8g", 9362, false}, |
| + {"6g", 8192, true}, {"8g", 8192, false}, |
| + {"24g", 32768, true}, {"32g", 32768, false}, |
| + } { |
| + boxes := []sizeBox{{Box: "bx-1", TotalMiB: tc.total, fleet: "fx"}} |
| + got := fleetSizeRefusal(boxes, true, true, mustSize(t, tc.size)) |
| + if tc.ok && got != nil { |
| + t.Fatalf("%s on %d MiB = %+v; want allowed", tc.size, tc.total, got) |
| + } |
| + if !tc.ok { |
| + wantFleetRefusal(t, tc.size, got, http.StatusConflict, fmt.Sprintf(c01, tc.size, "bx-1")) |
| + } |
| + if sizeFits(tc.total, mustSize(t, tc.size)) != tc.ok { |
| + t.Fatalf("sizeFits(%d, %s) = %v; want %v", tc.total, tc.size, !tc.ok, tc.ok) |
| + } |
| + } |
| + |
| + // Through the policy door: an owned fleet of 8 GiB boxes takes 6g as |
| + // the default size and as an override, and refuses 8g. |
| + fx := newPolicyFx(t, "code-fs-budget") |
| + sizeFleet(t, fx.consoleFixture, "fx", fx.sqid, "bx-1") |
| + fleetMemBeat(t, fx.consoleFixture, "fx", "bx-1", 8192, nil) |
| + if err := fx.st.SetAppPlacement(OpsAccountID, "shop", registry.PlacementTarget("fx")); err != nil { |
| + t.Fatal(err) |
| + } |
| + r := fx.mustPut(t, "shop", `{"default_size":"6g"}`, policyCreate) |
| + fleetRec(t, fx.consoleFixture, shopHost, "fx", "") |
| + r = fx.mustPut(t, "shop", `{"size":{"host":"shop-a.example.com","value":"6g"}}`, policyIfMatch(r.ETag)) |
| + st, _, out := fx.put(t, "shop", `{"default_size":"8g"}`, policyIfMatch(r.ETag)) |
| + if st != http.StatusConflict || errorOf(t, out) != "8g leaves too little memory on bx-1. choose a smaller size" { |
| + t.Fatalf("8g on an 8 GiB box = %d %s; want 409 PS-2-C01", st, out) |
| + } |
| +} |
| + |
| +func TestFleetSizeSharedRefused(t *testing.T) { |
| + t.Parallel() |
| + fx, acct := sizeFx(t) |
| + putSizeReport(t, fx.rel, "annie", "", time.Now(), nil) |
| + putSizeEdges(t, fx.rel, "annie", 65536, 0) |
| + sizeFleet(t, fx, "fx", fx.sqid, "bx-1") |
| + sizeFleet(t, fx, "fy", "zzz", "by-1") |
| + fleetMemBeat(t, fx, "fx", "bx-1", 16384, nil) |
| + fleetMemBeat(t, fx, "fy", "by-1", 16384, nil) |
| + onFleet := provision.Record{Host: shopHost, App: "shop", Account: fx.sqid, Region: registry.PlacementTarget("fx")} |
| + platform := provision.Record{Host: "shop-b.example.com", App: "shop", Account: fx.sqid} |
| + external := onFleet |
| + external.Addr = "192.0.2.10:8080" |
| + foreign := onFleet |
| + foreign.Region = registry.PlacementTarget("fy") |
| + shared := fmt.Sprintf(c02, "6g") |
| + for name, recs := range map[string][]provision.Record{ |
| + "platform route": {platform}, |
| + "external route": {external}, |
| + "mixed": {onFleet, platform}, |
| + "no records": nil, |
| + "foreign fleet": {foreign}, |
| + } { |
| + wantFleetRefusal(t, name, checkSizes(t, fx.srv, acct, recs, "6g")["6g"], http.StatusConflict, shared) |
| + } |
| + if r := checkSizes(t, fx.srv, acct, []provision.Record{onFleet}, "6g")["6g"]; r != nil { |
| + t.Fatalf("6g on the owned fleet's 16 GiB box = %+v; want allowed", r) |
| + } |
| + |
| + // The quarter rule still judges xlarge on a fleet box (R12). |
| + fleetMemBeat(t, fx, "fx", "bx-1", 7990, nil) |
| + if r := checkSizes(t, fx.srv, acct, []provision.Record{onFleet}, "xlarge")["xlarge"]; r == nil || !strings.Contains(r.Msg, "more than a quarter of the memory on bx-1 (7990 MiB)") { |
| + t.Fatalf("xlarge on 7990 MiB = %+v; want the quarter rule", r) |
| + } |
| + fleetMemBeat(t, fx, "fx", "bx-1", 16384, nil) |
| + if r := checkSizes(t, fx.srv, acct, []provision.Record{onFleet}, "xlarge")["xlarge"]; r != nil { |
| + t.Fatalf("xlarge on 16384 MiB = %+v; want allowed", r) |
| + } |
| +} |
| + |
| +// D7: every active roster row is a possible destination, spare or not, and |
| +// the smallest decides. |
| +func TestFleetSizeSpareBoxCounts(t *testing.T) { |
| + t.Parallel() |
| + fx, acct := sizeFx(t) |
| + sizeFleet(t, fx, "fx", fx.sqid, "bx-1", "bx-2") |
| + fleetMemBeat(t, fx, "fx", "bx-1", 32768, nil) |
| + fleetMemBeat(t, fx, "fx", "bx-2", 8192, nil) |
| + recs := []provision.Record{fleetRec(t, fx, shopHost, "fx", "bx-1")} |
| + got := checkSizes(t, fx.srv, acct, recs, "12g", "6g") |
| + wantFleetRefusal(t, "12g with an 8 GiB spare", got["12g"], http.StatusConflict, "12g leaves too little memory on bx-2. choose a smaller size") |
| + if got["6g"] != nil { |
| + t.Fatalf("6g with an 8 GiB spare = %+v; want allowed", got["6g"]) |
| + } |
| +} |
| + |
| +func TestFleetSizeIncompleteFailsClosed(t *testing.T) { |
| + t.Parallel() |
| + fx, acct := sizeFx(t) |
| + sizeFleet(t, fx, "fx", fx.sqid, "bx-1", "bx-2") |
| + fleetMemBeat(t, fx, "fx", "bx-1", 32768, nil) |
| + fleetMemBeat(t, fx, "fx", "bx-2", 0, nil) // no memory report |
| + recs := []provision.Record{fleetRec(t, fx, shopHost, "fx", "bx-1")} |
| + wantFleetRefusal(t, "4g with bx-2's total unknown", checkSizes(t, fx.srv, acct, recs, "4g")["4g"], http.StatusConflict, |
| + "bx-2 does not publish its memory total, so only small and standard can be set. upgrade the box, then try again") |
| + |
| + fleetMemBeat(t, fx, "fx", "bx-2", 32768, nil) |
| + putFixtureObject(t, fx.rel, "fleets/fx/status/boxes/zed.json", []byte("{not json")) |
| + wantFleetRefusal(t, "4g with an unreadable report", checkSizes(t, fx.srv, acct, recs, "4g")["4g"], http.StatusServiceUnavailable, |
| + "could not read every box's memory total, so only small and standard can be set. try again in a minute") |
| +} |
| + |
| +// Placement at a fleet size: a box without room is not a candidate, a |
| +// copy already serving there keeps serving and the pass reports itself |
| +// blocked (SR-4-C86), and the policy's size stays as it is. |
| +func TestFleetPlacementShortfall(t *testing.T) { |
| + fx, _, reg := replicaPlacementWorld(t) |
| + registerFleetBox(t, reg, "pi", "pi-3") |
| + putShopPolicy(t, fx, apppolicy.Policy{DefaultSize: "16g"}) |
| + sizedBeat(t, fx, "pi-1", 32768) |
| + sizedBeat(t, fx, "pi-2", 16384) |
| + sizedBeat(t, fx, "pi-3", 16384) |
| + v, err := fx.srv.loadFleetView(context.Background(), "pi") |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + cands, err := replicaCandidates(*shopRec(t, fx, "a.example.com"), v, 2, "16g") |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + for _, c := range cands { |
| + if c.Eligible != (c.Label == "pi-1") { |
| + t.Fatalf("16g candidates %+v; want pi-1 alone (16 GiB boxes give 14336 MiB)", cands) |
| + } |
| + } |
| + err = fx.srv.reconcileReplicaFleet(context.Background(), "pi", v, time.Now()) |
| + if err == nil || !strings.Contains(err.Error(), "placement blocked: pi-2 has no room left for this instance's copies (2 asked)") { |
| + t.Fatalf("placement = %v; want SR-4-C86 naming pi-2", err) |
| + } |
| + if b := shopRec(t, fx, "b.example.com"); b.ReplicaAssignment != nil || b.FleetBox != "pi-2" { |
| + t.Fatalf("b moved off its serving box: %+v %q", b.ReplicaAssignment, b.FleetBox) |
| + } |
| + if p, _, err := apppolicy.Get(context.Background(), fx.rel, fx.pub, fx.sqid, "shop", time.Now()); err != nil || p.DefaultSize != "16g" { |
| + t.Fatalf("policy after placement = %+v, %v; want 16g kept", p, err) |
| + } |
| +} |
| + |
| +func TestFleetSizeCreateChecked(t *testing.T) { |
| + fx, token, _ := fleetBoxWorld(t, "code-fs-create", "pi-1", "pi-2") |
| + fleetMemBeat(t, fx, "pi", "pi-1", 32768, nil) |
| + fleetMemBeat(t, fx, "pi", "pi-2", 8192, nil) |
| + putShopPolicy(t, fx, apppolicy.Policy{DefaultSize: "12g"}) |
| + code, out := createShop(t, fx, token, `{"host":"c.example.com"}`) |
| + want := "shop uses size 12g, which needs a fleet of your own whose every box has room for it. choose such a fleet, or set a size of xlarge or smaller first" |
| + if code != http.StatusConflict || errorOf(t, out) != want { |
| + t.Fatalf("create of a 12g app with an 8 GiB box = %d %s; want 409 PS-2-C14", code, out) |
| + } |
| + if _, err := provision.GetRecord(context.Background(), fx.rel, fx.sqid, "shop", "c.example.com"); err == nil { |
| + t.Fatal("a refused create wrote its record") |
| + } |
| + fleetMemBeat(t, fx, "pi", "pi-2", 32768, nil) |
| + createShopAt(t, fx, token, "c.example.com") |
| +} |
| + |
| +func TestFleetSizeCopiesDoor(t *testing.T) { |
| + fx, token := copiesWorld(t) |
| + putShopPolicy(t, fx, apppolicy.Policy{DefaultSize: "12g"}) |
| + sizedBeat(t, fx, "pi-1", 32768) |
| + sizedBeat(t, fx, "pi-2", 8192) |
| + want := "2 copies need 2 boxes with room for size 12g. this fleet has 1. choose a smaller size" |
| + if code, out := copiesPut(t, fx, token, `{"copies":2}`); code != http.StatusConflict || !strings.Contains(out, want) { |
| + t.Fatalf("copies 2 at 12g with an 8 GiB box: %d %s", code, out) |
| + } |
| + // The app-wide gate: b runs at xlarge, which an 8 GiB box holds, but |
| + // a's 12g override keeps that box from every instance of the app. |
| + a := shopRec(t, fx, "a.example.com") |
| + putShopPolicy(t, fx, apppolicy.Policy{DefaultSize: "xlarge", Sizes: map[string]apppolicy.SizeOverride{a.Host: {Size: "12g", CreatedAt: a.CreatedAt}}}) |
| + if code, out := copiesPut(t, fx, token, `{"copies":2}`); code != http.StatusConflict || !strings.Contains(out, "this fleet has 1") { |
| + t.Fatalf("copies 2 with a's 12g override and an 8 GiB box: %d %s", code, out) |
| + } |
| + sizedBeat(t, fx, "pi-2", 32768) |
| + if code, out := copiesPut(t, fx, token, `{"copies":2}`); code != http.StatusOK { |
| + t.Fatalf("copies 2 at 12g on two 32 GiB boxes: %d %s", code, out) |
| + } |
| +} |
| + |
| +func TestFleetSizeOverrideAppWide(t *testing.T) { |
| + t.Parallel() |
| + fx := newPolicyFx(t, "code-fs-appwide") |
| + putSizeReport(t, fx.rel, "annie", "", time.Now(), nil) |
| + putSizeEdges(t, fx.rel, "annie", 65536, 0) |
| + sizeFleet(t, fx.consoleFixture, "fx", fx.sqid, "bx-1") |
| + fleetMemBeat(t, fx.consoleFixture, "fx", "bx-1", 8192, nil) |
| + fleetRec(t, fx.consoleFixture, shopHost, "fx", "") |
| + sizeRecord(t, fx.rel, fx.sqid, "shop", "shop-b.example.com", "") |
| + st, _, out := fx.put(t, "shop", `{"size":{"host":"shop-a.example.com","value":"4g"}}`, policyCreate) |
| + if st != http.StatusConflict || errorOf(t, out) != "4g is only for apps on your own fleet. choose xlarge or smaller" { |
| + t.Fatalf("4g on A with B on the platform = %d %s; want 409 PS-2-C02", st, out) |
| + } |
| + fx.noObject(t, "shop") |
| + |
| + // appFleetSize: the largest fleet size, stale overrides included. |
| + p := apppolicy.Policy{DefaultSize: "6g", Sizes: map[string]apppolicy.SizeOverride{ |
| + "gone.example.com": {Size: "24g", CreatedAt: time.Unix(1, 0)}, shopHost: {Size: "xlarge"}}} |
| + if sz, ok := appFleetSize(p); !ok || sz.Name != "24g" { |
| + t.Fatalf("appFleetSize = %v, %v; want 24g from the stale override", sz.Name, ok) |
| + } |
| + if _, ok := appFleetSize(apppolicy.Policy{DefaultSize: "xlarge"}); ok { |
| + t.Fatal("appFleetSize found a fleet size in xlarge") |
| + } |
| +} |
| + |
| +func TestFleetSizeCommittedWarns(t *testing.T) { |
| + t.Parallel() |
| + fx := newPolicyFx(t, "code-fs-warn") |
| + sizeFleet(t, fx.consoleFixture, "fx", fx.sqid, "bx-1") |
| + fleetRec(t, fx.consoleFixture, shopHost, "fx", "bx-1") |
| + set := func(etag, value string) policyReply { |
| + t.Helper() |
| + hdr := policyCreate |
| + if etag != "" { |
| + hdr = policyIfMatch(etag) |
| + } |
| + return fx.mustPut(t, "shop", `{"size":{"host":"shop-a.example.com","value":"`+value+`"}}`, hdr) |
| + } |
| + committed := int64(20480) |
| + fleetMemBeat(t, fx.consoleFixture, "fx", "bx-1", 32768, &committed) |
| + r := set("", "12g") |
| + want := []string{"warning: sizes on bx-1 may now add up to 32768 MiB, more than the 28672 MiB it can give. an instance there may run out of memory"} |
| + if !slices.Equal(r.Warnings, want) { |
| + t.Fatalf("warnings at 20480 + 12288 = %q; want %q", r.Warnings, want) |
| + } |
| + r = set(r.ETag, "default") |
| + committed = 4096 |
| + fleetMemBeat(t, fx.consoleFixture, "fx", "bx-1", 32768, &committed) |
| + if r = set(r.ETag, "12g"); len(r.Warnings) != 0 { |
| + t.Fatalf("warnings at 4096 + 12288 = %q; want none", r.Warnings) |
| + } |
| + r = set(r.ETag, "default") |
| + fleetMemBeat(t, fx.consoleFixture, "fx", "bx-1", 32768, nil) |
| + if r = set(r.ETag, "12g"); len(r.Warnings) != 0 { |
| + t.Fatalf("warnings with no committed figure = %q; want none", r.Warnings) |
| + } |
| + raw, _ := json.Marshal(r) |
| + if strings.Contains(string(raw), `"warnings"`) { |
| + t.Fatalf("a reply with no warning carries the field: %s", raw) |
| + } |
| +} |
| + |
| +func TestFleetSizedAppMoveRefused(t *testing.T) { |
| + fx, token, _ := fleetBoxWorld(t, "code-fs-move", "pi-1") |
| + fleetMemBeat(t, fx, "pi", "pi-1", 16384, nil) |
| + sizeFleet(t, fx, "tiny", fx.sqid, "tn-1") |
| + fleetMemBeat(t, fx, "tiny", "tn-1", 4096, nil) |
| + sizeFleet(t, fx, "big", fx.sqid, "bg-1") |
| + fleetMemBeat(t, fx, "big", "bg-1", 16384, nil) |
| + putFleetRecord(t, fx, "shop", "a.example.com", nil) |
| + putShopPolicy(t, fx, apppolicy.Policy{DefaultSize: "6g"}) |
| + place := func(target string) (int, string) { |
| + code, _, out := rawReq(t, http.MethodPost, placeURL(fx, "shop"), token, `{"target":"`+target+`"}`) |
| + return code, out |
| + } |
| + want := "shop uses size 6g, which needs a fleet of your own whose every box has room for it. choose such a fleet, or set a size of xlarge or smaller first" |
| + for _, target := range []string{"", "fleet/tiny"} { |
| + if code, out := place(target); code != http.StatusConflict || errorOf(t, out) != want { |
| + t.Fatalf("place a 6g app at %q = %d %s; want 409 PS-2-C14", target, code, out) |
| + } |
| + if r := shopRec(t, fx, "a.example.com"); r.Region != "fleet/pi" { |
| + t.Fatalf("a refused move changed the record's region to %q", r.Region) |
| + } |
| + } |
| + if code, out := place("fleet/big"); code != http.StatusOK { |
| + t.Fatalf("place a 6g app on 16 GiB boxes = %d %s", code, out) |
| + } |
| + if r := shopRec(t, fx, "a.example.com"); r.Region != "fleet/big" { |
| + t.Fatalf("the move left the record at %q", r.Region) |
| + } |
| + // xlarge moves as today, onto a 4 GiB box too. |
| + putShopPolicy(t, fx, apppolicy.Policy{DefaultSize: "xlarge"}) |
| + if code, out := place("fleet/tiny"); code != http.StatusOK { |
| + t.Fatalf("place an xlarge app on fleet tiny = %d %s", code, out) |
| + } |
| +} |
| + |
| +// Through the real drain and relabel worker: a box that joins a fleet too |
| +// small for the app's largest fleet size is never chosen for any instance |
| +// of the app, whatever that instance's own size. |
| +func TestFleetRelabelSkipsUnfit(t *testing.T) { |
| + world := func(t *testing.T, code string, policy func(a provision.Record) apppolicy.Policy) (*consoleFixture, *registry.Registry) { |
| + t.Helper() |
| + fx, _, reg := fleetBoxWorld(t, code, "pi-1", "pi-2") |
| + registerFleetBox(t, reg, "pi", "pi-3") |
| + fleetMemBeat(t, fx, "pi", "pi-1", 32768, nil) |
| + fleetMemBeat(t, fx, "pi", "pi-2", 32768, nil) |
| + fleetMemBeat(t, fx, "pi", "pi-3", 8192, nil) // joined late, holds nothing |
| + placeBlog(t, fx) |
| + putFleetRecord(t, fx, "blog", "c.example.com", func(r *provision.Record) { r.FleetBox = "pi-1" }) |
| + putFleetRecord(t, fx, "shop", "a.example.com", func(r *provision.Record) { r.FleetBox = "pi-1" }) |
| + putFleetRecord(t, fx, "shop", "b.example.com", func(r *provision.Record) { r.FleetBox = "pi-2" }) |
| + putShopPolicy(t, fx, policy(*shopRec(t, fx, "a.example.com"))) |
| + return fx, reg |
| + } |
| + override := func(a provision.Record) apppolicy.Policy { |
| + return apppolicy.Policy{DefaultSize: "xlarge", Sizes: map[string]apppolicy.SizeOverride{a.Host: {Size: "12g", CreatedAt: a.CreatedAt}}} |
| + } |
| + stale := func(provision.Record) apppolicy.Policy { |
| + return apppolicy.Policy{DefaultSize: "xlarge", Sizes: map[string]apppolicy.SizeOverride{"gone.example.com": {Size: "12g", CreatedAt: time.Unix(1, 0)}}} |
| + } |
| + plain := func(provision.Record) apppolicy.Policy { return apppolicy.Policy{DefaultSize: "xlarge"} } |
| + |
| + for name, policy := range map[string]func(provision.Record) apppolicy.Policy{"override": override, "stale override": stale} { |
| + t.Run("drain "+name, func(t *testing.T) { |
| + fx, reg := world(t, "code-fs-drain-"+strings.ReplaceAll(name, " ", "-"), policy) |
| + fleetMemBeat(t, fx, "pi", "pi-1", 8192, nil) |
| + stuck, err := fx.srv.drainBox(context.Background(), "pi", "pi-2") |
| + if err != nil || !slices.Equal(stuck, []string{"shop"}) { |
| + t.Fatalf("drain with no box fit for 12g = %v, %v; want shop stranded", stuck, err) |
| + } |
| + if b := boxRow(t, reg, "pi-2"); b.DrainingSince != 0 { |
| + t.Fatal("a refused drain marked pi-2 draining") |
| + } |
| + fleetMemBeat(t, fx, "pi", "pi-1", 32768, nil) |
| + if stuck, err := fx.srv.drainBox(context.Background(), "pi", "pi-2"); err != nil || len(stuck) != 0 { |
| + t.Fatalf("drain = %v, %v", stuck, err) |
| + } |
| + if b := shopRec(t, fx, "b.example.com"); b.FleetBox != "pi-1" { |
| + t.Fatalf("b drained to %q; want pi-1, never the 8 GiB pi-3", b.FleetBox) |
| + } |
| + }) |
| + t.Run("relabel "+name, func(t *testing.T) { |
| + fx, _ := world(t, "code-fs-relabel-"+strings.ReplaceAll(name, " ", "-"), policy) |
| + beat(t, fx, "pi-2", time.Now().Add(-2*time.Hour), nil) // lost |
| + relabelPass(t, fx) |
| + if b := shopRec(t, fx, "b.example.com"); b.FleetBox != "pi-1" { |
| + t.Fatalf("b relabelled to %q; want pi-1, never the 8 GiB pi-3", b.FleetBox) |
| + } |
| + }) |
| + } |
| + t.Run("no fleet size", func(t *testing.T) { |
| + fx, _ := world(t, "code-fs-drain-plain", plain) |
| + if stuck, err := fx.srv.drainBox(context.Background(), "pi", "pi-2"); err != nil || len(stuck) != 0 { |
| + t.Fatalf("drain = %v, %v", stuck, err) |
| + } |
| + if b := shopRec(t, fx, "b.example.com"); b.FleetBox != "pi-3" { |
| + t.Fatalf("b drained to %q; want pi-3, the least loaded, as today", b.FleetBox) |
| + } |
| + }) |
| +} |
diff --git a/internal/console/policysize.go b/internal/console/policysize.go| index 5c3bd1d9c..b1da4d2cf 100644 |
| --- a/internal/console/policysize.go |
| +++ b/internal/console/policysize.go |
| @@ -93,28 +93,37 @@ type sizeCommittedBox struct { |
| // or a fleet's roster could not be read, or an edges file could not be read |
| // or parsed. A failed listing of the status reports or of control/edges/ is |
| // the error, and no boxes. |
| -func (s *Server) sizeBoxes(ctx context.Context, acct *Account, recs []provision.Record, defaultPool *string, appFleet string) (boxes []sizeBox, complete bool, err error) { |
| +// |
| +// fleetOnly (#892 S2, the fleet sizes' first condition) reports that every |
| +// box recs can be placed on is a box of a fleet the account owns: every |
| +// record (or, with none, the app) is placed on a fleet, every such fleet |
| +// is one of the account's, and no record routes to an address the |
| +// platform does not run (Addr, Addrs). |
| +func (s *Server) sizeBoxes(ctx context.Context, acct *Account, recs []provision.Record, defaultPool *string, appFleet string) (boxes []sizeBox, complete, fleetOnly bool, err error) { |
| if s.cfg.Releases == nil { |
| - return nil, false, errors.New("no deployment bucket") |
| + return nil, false, false, errors.New("no deployment bucket") |
| } |
| placed := map[string]bool{} |
| var platformRecs []provision.Record |
| + external := false |
| for _, r := range recs { |
| if f, ok := registry.FleetPlacement(r.Region); ok { |
| placed[f] = true |
| } else { |
| platformRecs = append(platformRecs, r) |
| } |
| + external = external || r.Addr != "" || len(r.Addrs) > 0 |
| } |
| if len(recs) == 0 && appFleet != "" { |
| placed[appFleet] = true |
| } |
| - fleetOnly := len(placed) > 0 && len(platformRecs) == 0 |
| + fleetOnly = len(placed) > 0 && len(platformRecs) == 0 |
| + onlyPlaced := fleetOnly |
| complete = true |
| - if !fleetOnly { |
| + if !onlyPlaced { |
| boxes, complete, err = s.sizePlatformBoxes(ctx, platformRecs, defaultPool) |
| if err != nil { |
| - return nil, false, err |
| + return nil, false, false, err |
| } |
| } |
| @@ -125,7 +134,7 @@ func (s *Server) sizeBoxes(ctx context.Context, acct *Account, recs []provision. |
| } |
| found := map[string]bool{} |
| for _, f := range fleets { |
| - if f.Account != acct.Sqid || (fleetOnly && !placed[f.Name]) { |
| + if f.Account != acct.Sqid || (onlyPlaced && !placed[f.Name]) { |
| continue |
| } |
| found[f.Name] = true |
| @@ -167,8 +176,10 @@ func (s *Server) sizeBoxes(ctx context.Context, acct *Account, recs []provision. |
| if !found[f] && err == nil { |
| s.policyLogf("console: size check: placed fleet %s is not one of the account's fleets", f) |
| complete = false |
| + fleetOnly = false |
| } |
| } |
| + fleetOnly = fleetOnly && !external |
| // A box with no total in an edges file falls back to its own status |
| // report's memory total, when the report is fresh (ruling, Task 9 |
| // review): fleet boxes publish no edges file, so this is their total. |
| @@ -183,7 +194,7 @@ func (s *Server) sizeBoxes(ctx context.Context, acct *Account, recs []provision. |
| } |
| } |
| slices.SortStableFunc(boxes, func(a, b sizeBox) int { return strings.Compare(a.Box, b.Box) }) |
| - return boxes, complete, nil |
| + return boxes, complete, fleetOnly, nil |
| } |
| // sizePlatformBoxes is sizeBoxes's platform half: the eligible platform |
| @@ -308,12 +319,18 @@ func (e sizeEdges) servesAny(hosts map[string]bool) bool { |
| // any size: 409 (string 1) when the size's MemoryMax is more than a quarter |
| // of the smallest known total, naming that box. small and standard are |
| // never refused for an unknown total or an incomplete read. |
| +// |
| +// A fleet size is judged by fleetSizeRefusal instead (#892 S2): the quarter |
| +// rule never applies to it. |
| func (s *Server) sizeCapacityCheck(ctx context.Context, acct *Account, recs []provision.Record, size cgroup.Size, defaultPool *string, appFleet string) ([]sizeBox, *doorRefusal) { |
| - boxes, complete, err := s.sizeBoxes(ctx, acct, recs, defaultPool, appFleet) |
| + boxes, complete, fleetOnly, err := s.sizeBoxes(ctx, acct, recs, defaultPool, appFleet) |
| if err != nil { |
| s.policyLogf("console: size check: %v", err) |
| complete = false |
| } |
| + if cgroup.FleetSize(size.Name) { |
| + return boxes, fleetSizeRefusal(boxes, complete, fleetOnly, size) |
| + } |
| return boxes, sizeRefusal(boxes, complete, size) |
| } |
| @@ -336,7 +353,7 @@ func sizeRefusal(boxes []sizeBox, complete bool, size cgroup.Size) *doorRefusal |
| smallest = b |
| } |
| } |
| - if smallest != nil && size.Limit.MemoryMax>>20 > smallest.TotalMiB/4 { |
| + if smallest != nil && !sizeFits(smallest.TotalMiB, size) { |
| return &doorRefusal{Status: http.StatusConflict, Msg: fmt.Sprintf(sizeQuarterMsg, size.Name, smallest.Box, smallest.TotalMiB)} |
| } |
| return nil |
| @@ -375,7 +392,13 @@ func sizeCommitted(boxes []sizeBox) []sizeCommittedBox { |
| // set or replaced on a host, checked against that host's record. Clearing a |
| // size, re-sending the one in force and an egress-only change read nothing. |
| // On success it answers each checked box's committed figure. |
| -func (s *Server) policySizeCheck(ctx context.Context, acct *Account, app string, old, next apppolicy.Policy, req policyPutReq) ([]sizeCommittedBox, *doorRefusal) { |
| +// |
| +// A change to a fleet size (#892 S2) is checked app-wide instead, whichever |
| +// instance's size changes: the largest fleet size of next (appFleetSize) |
| +// against every possible destination of every record of the app |
| +// (fleetSizeRefusal), and it answers PS-2-C03 warnings (fleetSizeWarnings, |
| +// P8), never a refusal. |
| +func (s *Server) policySizeCheck(ctx context.Context, acct *Account, app string, old, next apppolicy.Policy, req policyPutReq) ([]sizeCommittedBox, []string, *doorRefusal) { |
| type check struct { |
| size cgroup.Size |
| host string // "" is the whole app |
| @@ -397,14 +420,28 @@ func (s *Server) policySizeCheck(ctx context.Context, acct *Account, app string, |
| } |
| } |
| if len(checks) == 0 { |
| - return nil, nil |
| + return nil, nil, nil |
| } |
| var checked []sizeBox |
| + var warnings []string |
| seen := map[[2]string]bool{} |
| + fleetChecked := false |
| for _, c := range checks { |
| + if cgroup.FleetSize(c.size.Name) { |
| + if fleetChecked { |
| + continue |
| + } |
| + fleetChecked = true |
| + // The whole app, at its largest fleet size, which is at least |
| + // this one. |
| + c = check{size: c.size} |
| + if fs, ok := appFleetSize(next); ok { |
| + c.size = fs |
| + } |
| + } |
| recs, ok := s.policySizeRecords(ctx, acct, app, c.host) |
| if !ok && sizeAboveStandard(c.size) { |
| - return nil, &doorRefusal{Status: http.StatusServiceUnavailable, Msg: sizeUnreadMsg} |
| + return nil, nil, &doorRefusal{Status: http.StatusServiceUnavailable, Msg: sizeUnreadMsg} |
| } |
| // Only a complete empty record set may use the app's default pool. |
| // Unread records or manifests retain the conservative all-box check. |
| @@ -419,7 +456,7 @@ func (s *Server) policySizeCheck(ctx context.Context, acct *Account, app string, |
| appFleet, _ = registry.FleetPlacement(placement) |
| case sizeAboveStandard(c.size): |
| s.policyLogf("console: size check %s/%s: placement: %v", acct.Sqid, app, err) |
| - return nil, &doorRefusal{Status: http.StatusServiceUnavailable, Msg: sizeUnreadMsg} |
| + return nil, nil, &doorRefusal{Status: http.StatusServiceUnavailable, Msg: sizeUnreadMsg} |
| } |
| } |
| if ok && len(recs) == 0 && appFleet == "" { |
| @@ -431,12 +468,15 @@ func (s *Server) policySizeCheck(ctx context.Context, acct *Account, app string, |
| pool := "" |
| defaultPool = &pool |
| case sizeAboveStandard(c.size): |
| - return nil, &doorRefusal{Status: http.StatusServiceUnavailable, Msg: sizeUnreadMsg} |
| + return nil, nil, &doorRefusal{Status: http.StatusServiceUnavailable, Msg: sizeUnreadMsg} |
| } |
| } |
| boxes, ref := s.sizeCapacityCheck(ctx, acct, recs, c.size, defaultPool, appFleet) |
| if ref != nil { |
| - return nil, ref |
| + return nil, nil, ref |
| + } |
| + if cgroup.FleetSize(c.size.Name) { |
| + warnings = append(warnings, fleetSizeWarnings(boxes, recs, next)...) |
| } |
| for _, b := range boxes { |
| if k := [2]string{b.fleet, b.Box}; !seen[k] { |
| @@ -446,7 +486,7 @@ func (s *Server) policySizeCheck(ctx context.Context, acct *Account, app string, |
| } |
| } |
| slices.SortStableFunc(checked, func(a, b sizeBox) int { return strings.Compare(a.Box, b.Box) }) |
| - return sizeCommitted(checked), nil |
| + return sizeCommitted(checked), warnings, nil |
| } |
| // policySizeRecords is the records a size check covers: host's own, or |
diff --git a/internal/console/policysize_test.go b/internal/console/policysize_test.go| index 12a69a137..5382b1b36 100644 |
| --- a/internal/console/policysize_test.go |
| +++ b/internal/console/policysize_test.go |
| @@ -167,7 +167,7 @@ func TestSizeCapacityRefusesAboveStandardWhenATotalIsMissing(t *testing.T) { |
| } |
| putSizeEdges(t, fx.rel, "betty", 0, 16384) // region HA's headroom only |
| - boxes, complete, err := fx.srv.sizeBoxes(context.Background(), acct, recs, nil, "") |
| + boxes, complete, _, err := fx.srv.sizeBoxes(context.Background(), acct, recs, nil, "") |
| if err != nil || !complete { |
| t.Fatalf("sizeBoxes = %v, %v", complete, err) |
| } |
| @@ -245,7 +245,7 @@ func TestSizeCapacityReadsATotalFromTheBoxsStatusReport(t *testing.T) { |
| t.Fatalf("large with no report from bx-1 = %+v, want 409 naming bx-1", r) |
| } |
| putSizeReport(t, fleet, "bx-1", "", time.Now(), fresh) |
| - boxes, complete, err := fx.srv.sizeBoxes(context.Background(), acct, recs, nil, "") |
| + boxes, complete, _, err := fx.srv.sizeBoxes(context.Background(), acct, recs, nil, "") |
| if err != nil || !complete { |
| t.Fatalf("sizeBoxes = %v, %v", complete, err) |
| } |
| @@ -295,7 +295,7 @@ func TestSizeBoxesComeFromTheListingsNotTheEdgesFiles(t *testing.T) { |
| reg.Close() |
| recs := []provision.Record{sizeRecord(t, fx.rel, fx.sqid, "shop", shopHost, "")} |
| - boxes, complete, err := fx.srv.sizeBoxes(context.Background(), acct, recs, nil, "") |
| + boxes, complete, _, err := fx.srv.sizeBoxes(context.Background(), acct, recs, nil, "") |
| if err != nil || !complete { |
| t.Fatalf("sizeBoxes = %v, %v", complete, err) |
| } |
| @@ -306,7 +306,7 @@ func TestSizeBoxesComeFromTheListingsNotTheEdgesFiles(t *testing.T) { |
| // With no records (an app default before any instance), every |
| // non-stale platform box and the account's active fleet boxes. |
| - boxes, complete, err = fx.srv.sizeBoxes(context.Background(), acct, nil, nil, "") |
| + boxes, complete, _, err = fx.srv.sizeBoxes(context.Background(), acct, nil, nil, "") |
| if err != nil || !complete { |
| t.Fatalf("sizeBoxes with no records = %v, %v", complete, err) |
| } |
| @@ -324,7 +324,7 @@ func TestSizeCapacityRefusesAboveStandardWhenTheBoxListingIsIncomplete(t *testin |
| putFixtureObject(t, fx.rel, "status/boxes/zed.json", []byte("{not json")) |
| recs := []provision.Record{sizeRecord(t, fx.rel, fx.sqid, "shop", shopHost, "")} |
| - if _, complete, _ := fx.srv.sizeBoxes(context.Background(), acct, recs, nil, ""); complete { |
| + if _, complete, _, _ := fx.srv.sizeBoxes(context.Background(), acct, recs, nil, ""); complete { |
| t.Fatal("sizeBoxes says complete with an unreadable status report") |
| } |
| got := checkSizes(t, fx.srv, acct, recs, "large", "standard") |
| @@ -348,7 +348,7 @@ func TestSizeCommittedReportsEachBox(t *testing.T) { |
| putSizeReport(t, release.NewPrefixStore(fx.rel, "fleets/fx/"), "bx-1", "", time.Now(), |
| &status.BoxMetrics{SizeCommittedMiB: &fc, MemTotalBytes: &ft, MemAvailBytes: &fa}) |
| - boxes, complete, err := fx.srv.sizeBoxes(context.Background(), acct, nil, nil, "") |
| + boxes, complete, _, err := fx.srv.sizeBoxes(context.Background(), acct, nil, nil, "") |
| if err != nil || !complete { |
| t.Fatalf("sizeBoxes = %v, %v", complete, err) |
| } |
| @@ -457,12 +457,12 @@ func TestSizeChecksAFleetPlacedAppAgainstItsFleetOnly(t *testing.T) { |
| placed := []provision.Record{sizeRecord(t, fx.rel, fx.sqid, "shop", shopHost, registry.PlacementTarget("classic"))} |
| want := []sizeBox{{Box: "box-1", TotalMiB: 7990, fleet: "classic"}} |
| - boxes, complete, err := fx.srv.sizeBoxes(context.Background(), acct, placed, nil, "") |
| + boxes, complete, _, err := fx.srv.sizeBoxes(context.Background(), acct, placed, nil, "") |
| if err != nil || !complete || !reflect.DeepEqual(noReports(boxes), want) { |
| t.Fatalf("sizeBoxes for a fleet-placed record = %+v, %v, %v, want %+v complete", boxes, complete, err, want) |
| } |
| // No records yet: the app's own placement stands in for them. |
| - boxes, complete, err = fx.srv.sizeBoxes(context.Background(), acct, nil, nil, "classic") |
| + boxes, complete, _, err = fx.srv.sizeBoxes(context.Background(), acct, nil, nil, "classic") |
| if err != nil || !complete || !reflect.DeepEqual(noReports(boxes), want) { |
| t.Fatalf("sizeBoxes for a fleet-placed app = %+v, %v, %v, want %+v complete", boxes, complete, err, want) |
| } |
| @@ -485,7 +485,7 @@ func TestSizeChecksAFleetPlacedAppAgainstItsFleetOnly(t *testing.T) { |
| // on, and the unreadable platform report counts again. |
| unplaced := []provision.Record{placed[0]} |
| unplaced[0].Region = "" |
| - if _, complete, _ := fx.srv.sizeBoxes(context.Background(), acct, unplaced, nil, ""); complete { |
| + if _, complete, _, _ := fx.srv.sizeBoxes(context.Background(), acct, unplaced, nil, ""); complete { |
| t.Fatal("sizeBoxes for a platform record says complete with an unreadable status report") |
| } |
| if r := checkSizes(t, fx.srv, acct, unplaced, "xlarge")["xlarge"]; r == nil { |
| @@ -502,7 +502,7 @@ func TestSizeRefusesAboveStandardForAnUnknownPlacedFleet(t *testing.T) { |
| sizeFleet(t, fx, "fy", "zzz", "by-1") |
| recs := []provision.Record{sizeRecord(t, fx.rel, fx.sqid, "shop", shopHost, registry.PlacementTarget("fy"))} |
| - boxes, complete, err := fx.srv.sizeBoxes(context.Background(), acct, recs, nil, "") |
| + boxes, complete, _, err := fx.srv.sizeBoxes(context.Background(), acct, recs, nil, "") |
| if err != nil || complete || len(boxes) != 0 { |
| t.Fatalf("sizeBoxes = %+v, %v, %v, want no boxes and incomplete", boxes, complete, err) |
| } |
| @@ -554,7 +554,7 @@ func TestSizeChecksMixedRecordsAgainstBothPlatformAndFleets(t *testing.T) { |
| sizeRecord(t, fx.rel, fx.sqid, "shop", "shop-b.example.com", registry.PlacementTarget("classic")), |
| } |
| - boxes, complete, err := fx.srv.sizeBoxes(context.Background(), acct, recs, nil, "") |
| + boxes, complete, _, err := fx.srv.sizeBoxes(context.Background(), acct, recs, nil, "") |
| want := []sizeBox{{Box: "annie", TotalMiB: 3847}, {Box: "bo-1", fleet: "other"}, {Box: "box-1", fleet: "classic"}} |
| if err != nil || !complete || !reflect.DeepEqual(noReports(boxes), want) { |
| t.Fatalf("sizeBoxes = %+v, %v, %v, want %+v complete", boxes, complete, err, want) |
diff --git a/internal/console/replicaplacement.go b/internal/console/replicaplacement.go| index 700a52a9d..98bdd6871 100644 |
| --- a/internal/console/replicaplacement.go |
| +++ b/internal/console/replicaplacement.go |
| @@ -180,7 +180,10 @@ func replicaCandidates(rec provision.Record, v fleetView, n int, sizeName string |
| if n == 1 { |
| eligible = atStatelessRuntimeFloor(b.Release) && b.destinationFor(rec.Runtime == provision.RuntimeRootfsV1) |
| } |
| - eligible = eligible && sizeRefusal([]sizeBox{{Box: b.Label, TotalMiB: b.TotalMiB}}, true, size) == nil |
| + // The one fit predicate (sizeFits) at the instance's own size, and |
| + // the app's destination gate (DestRefused, #892 S2 P7). The size is |
| + // never lowered to fit. |
| + eligible = eligible && sizeFits(b.TotalMiB, size) && !v.DestRefused[b.Label] |
| candidates = append(candidates, replicas.Candidate{Label: b.Label, Load: v.Load[b.Label], Eligible: eligible}) |
| } |
| return candidates, nil |
| @@ -640,6 +643,7 @@ func (s *Server) reconcileReplicaApp(ctx context.Context, fleet string, app AppR |
| } |
| var errs []error |
| changed := false |
| + destJudged := false |
| for _, i := range indices { |
| rec := v.Records[i] |
| if !needs[i] || (n == 1 && rec.ReplicaAssignment == nil) { |
| @@ -656,6 +660,11 @@ func (s *Server) reconcileReplicaApp(ctx context.Context, fleet string, app AppR |
| if policy != nil { |
| v.ReplicaSizes[key] = policy.SizeFor(rec.Host, rec.CreatedAt) |
| } |
| + if !destJudged { |
| + // Every box of the fleet, judged once for the app (P7). |
| + v.DestRefused = s.appDestinationsRefused(withAppPolicyRead(ctx, acct, app.App, policy), acct, app.App, fleet, v.Boxes) |
| + destJudged = true |
| + } |
| action, err := s.replicaAction(rec, v, n) |
| if err == nil { |
| var result *provision.Record |