carlos / platform Public

console: size a fleet-placed app against its fleet's boxes only (#875)

The size check counted every platform box for an app placed on a fleet,
so a small platform box it can never run on capped its size. Records
placed on a fleet, and an app with no records placed on one, now make no
platform box eligible and read no platform listing; only their fleets'
active boxes count. A placed fleet the account does not own leaves the
read incomplete, so above standard is refused rather than passed.

The quarter rule is unchanged: a t4g.large (~7990 MiB) still refuses
xlarge (2048 MiB > 1997). Reviewed by Astra, no findings.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Squashed from fleet-size-cap-875 into main.
claude agent, operated by paul@keymail.dev 225c41d05b9436056d58e4516fe63c6ed1db40f3 parent a130aea
2 files changed, +253 −58
  • internal/console/policysize.go +123 −51
  • internal/console/policysize_test.go +130 −7

CI cancelled — superseded by a newer push — run details

diff --git a/internal/console/policysize.go b/internal/console/policysize.go
index f634ad1c8..5c3bd1d9c 100644
--- a/internal/console/policysize.go
+++ b/internal/console/policysize.go
@@ -66,8 +66,15 @@ type sizeCommittedBox struct {
// sizeBoxes is the boxes recs can be placed on, sorted by label, with each
// one's memory total.
//
-// Eligible are the platform boxes of status.LoadBoxesComplete and the
-// active roster rows of every fleet the account owns. With records, a
+// A record placed on a fleet (registry.FleetPlacement of its Region) runs
+// on that fleet's boxes and nowhere else (#875): it makes no platform box
+// eligible. With no records, appFleet is the fleet the app is placed on
+// ("" for none) and stands in for them. When every record, or the app, is
+// placed on a fleet, the eligible boxes are the active roster rows of those
+// fleets alone, and no platform listing is read.
+//
+// Otherwise eligible are the platform boxes of status.LoadBoxesComplete and
+// the active roster rows of every fleet the account owns. With records, a
// platform box counts when it is not stale and in the region of one of
// them (homeRegionIn on both sides) and its known served_pools do not
// exclude that record, or when its edges file lists one of their hosts.
@@ -75,6 +82,9 @@ type sizeCommittedBox struct {
// across regions; nil means the pool is unknown and excludes no box.
// Every active fleet box of the account counts.
//
+// A placed fleet the account does not own, or that is gone, leaves the
+// read incomplete.
+//
// A box's total is, in order: its edges file's box_mem_total_mib; region
// HA's headroom mem_total_mib (ruling C3); its own status report's
// mem_total_bytes, when that report is not stale. None of them is 0.
@@ -83,57 +93,29 @@ 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) (boxes []sizeBox, complete bool, err error) {
+func (s *Server) sizeBoxes(ctx context.Context, acct *Account, recs []provision.Record, defaultPool *string, appFleet string) (boxes []sizeBox, complete bool, err error) {
if s.cfg.Releases == nil {
return nil, false, errors.New("no deployment bucket")
}
- reports, complete, err := status.LoadBoxesComplete(ctx, s.cfg.Releases)
- if err != nil {
- return nil, false, fmt.Errorf("status reports: %w", err)
- }
- snap, err := readEdgesSnapshot(ctx, s.cfg.Releases)
- if err != nil {
- return nil, false, fmt.Errorf("list %s: %w", edgesfile.Prefix, err)
- }
- complete = complete && snap.Complete
-
- files := map[string]sizeEdges{}
- for _, f := range snap.Files {
- files[f.Box] = files[f.Box].join(f)
- }
- hosts := map[string]bool{}
- regions := map[string][]string{}
+ placed := map[string]bool{}
+ var platformRecs []provision.Record
for _, r := range recs {
- hosts[r.Host] = true
- region := s.homeRegionIn(r.Region)
- regions[region] = append(regions[region], r.Pool)
- }
- now := time.Now()
- seen := map[string]bool{}
- for i, b := range reports {
- if b.Label == "" || seen[b.Label] {
- continue
- }
- stale := status.BoxStale(b, now, status.SnapshotPeriod)
- f := files[b.Label]
- if stale {
- continue
- }
- if len(recs) == 0 {
- if defaultPool != nil && !f.mayServe(*defaultPool) {
- continue
- }
+ if f, ok := registry.FleetPlacement(r.Region); ok {
+ placed[f] = true
} else {
- eligible := f.servesAny(hosts)
- for _, pool := range regions[s.homeRegionIn(b.Region)] {
- eligible = eligible || f.mayServe(pool)
- }
- if !eligible {
- continue
- }
+ platformRecs = append(platformRecs, r)
+ }
+ }
+ if len(recs) == 0 && appFleet != "" {
+ placed[appFleet] = true
+ }
+ fleetOnly := len(placed) > 0 && len(platformRecs) == 0
+ complete = true
+ if !fleetOnly {
+ boxes, complete, err = s.sizePlatformBoxes(ctx, platformRecs, defaultPool)
+ if err != nil {
+ return nil, false, err
}
- seen[b.Label] = true
- boxes = append(boxes, sizeBox{Box: b.Label, Region: b.Region, TotalMiB: f.total, report: &reports[i]})
}
reg, fleets, err := s.systemFleets()
@@ -141,10 +123,12 @@ func (s *Server) sizeBoxes(ctx context.Context, acct *Account, recs []provision.
s.policyLogf("console: size check: fleets: %v", err)
complete = false
}
+ found := map[string]bool{}
for _, f := range fleets {
- if f.Account != acct.Sqid {
+ if f.Account != acct.Sqid || (fleetOnly && !placed[f.Name]) {
continue
}
+ found[f.Name] = true
roster, err := reg.ListFleetBoxes(f.Name)
if err != nil {
s.policyLogf("console: size check: fleet %s roster: %v", f.Name, err)
@@ -179,9 +163,16 @@ func (s *Server) sizeBoxes(ctx context.Context, acct *Account, recs []provision.
boxes = append(boxes, sb)
}
}
+ for f := range placed {
+ if !found[f] && err == nil {
+ s.policyLogf("console: size check: placed fleet %s is not one of the account's fleets", f)
+ complete = false
+ }
+ }
// 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.
+ now := time.Now()
for i := range boxes {
b := &boxes[i]
if b.TotalMiB > 0 || b.report == nil || status.BoxStale(*b.report, now, status.SnapshotPeriod) {
@@ -195,6 +186,61 @@ func (s *Server) sizeBoxes(ctx context.Context, acct *Account, recs []provision.
return boxes, complete, nil
}
+// sizePlatformBoxes is sizeBoxes's platform half: the eligible platform
+// boxes for recs (none of them fleet-placed), or for defaultPool with no
+// records, each with its edges file total.
+func (s *Server) sizePlatformBoxes(ctx context.Context, recs []provision.Record, defaultPool *string) (boxes []sizeBox, complete bool, err error) {
+ reports, complete, err := status.LoadBoxesComplete(ctx, s.cfg.Releases)
+ if err != nil {
+ return nil, false, fmt.Errorf("status reports: %w", err)
+ }
+ snap, err := readEdgesSnapshot(ctx, s.cfg.Releases)
+ if err != nil {
+ return nil, false, fmt.Errorf("list %s: %w", edgesfile.Prefix, err)
+ }
+ complete = complete && snap.Complete
+
+ files := map[string]sizeEdges{}
+ for _, f := range snap.Files {
+ files[f.Box] = files[f.Box].join(f)
+ }
+ hosts := map[string]bool{}
+ regions := map[string][]string{}
+ for _, r := range recs {
+ hosts[r.Host] = true
+ region := s.homeRegionIn(r.Region)
+ regions[region] = append(regions[region], r.Pool)
+ }
+ now := time.Now()
+ seen := map[string]bool{}
+ for i, b := range reports {
+ if b.Label == "" || seen[b.Label] {
+ continue
+ }
+ stale := status.BoxStale(b, now, status.SnapshotPeriod)
+ f := files[b.Label]
+ if stale {
+ continue
+ }
+ if len(recs) == 0 {
+ if defaultPool != nil && !f.mayServe(*defaultPool) {
+ continue
+ }
+ } else {
+ eligible := f.servesAny(hosts)
+ for _, pool := range regions[s.homeRegionIn(b.Region)] {
+ eligible = eligible || f.mayServe(pool)
+ }
+ if !eligible {
+ continue
+ }
+ }
+ seen[b.Label] = true
+ boxes = append(boxes, sizeBox{Box: b.Label, Region: b.Region, TotalMiB: f.total, report: &reports[i]})
+ }
+ return boxes, complete, nil
+}
+
// sizeEdges is what the size check takes from a box's edges files: its
// total, served pools and the hosts it serves. Two files naming one box
// join conservatively: an unknown total, or the smaller one; either file
@@ -262,8 +308,8 @@ 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.
-func (s *Server) sizeCapacityCheck(ctx context.Context, acct *Account, recs []provision.Record, size cgroup.Size, defaultPool *string) ([]sizeBox, *doorRefusal) {
- boxes, complete, err := s.sizeBoxes(ctx, acct, recs, defaultPool)
+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)
if err != nil {
s.policyLogf("console: size check: %v", err)
complete = false
@@ -363,7 +409,20 @@ func (s *Server) policySizeCheck(ctx context.Context, acct *Account, app string,
// Only a complete empty record set may use the app's default pool.
// Unread records or manifests retain the conservative all-box check.
var defaultPool *string
+ appFleet := ""
if ok && len(recs) == 0 {
+ // An app placed on a fleet is sized against that fleet's boxes
+ // alone (#875), whatever its default pool.
+ placement, err := s.sizeAppPlacement(acct, app)
+ switch {
+ case err == nil:
+ 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}
+ }
+ }
+ if ok && len(recs) == 0 && appFleet == "" {
manifest, err := provision.GetManifest(ctx, s.cfg.Releases, acct.Sqid, app)
switch {
case err == nil:
@@ -375,7 +434,7 @@ func (s *Server) policySizeCheck(ctx context.Context, acct *Account, app string,
return nil, &doorRefusal{Status: http.StatusServiceUnavailable, Msg: sizeUnreadMsg}
}
}
- boxes, ref := s.sizeCapacityCheck(ctx, acct, recs, c.size, defaultPool)
+ boxes, ref := s.sizeCapacityCheck(ctx, acct, recs, c.size, defaultPool, appFleet)
if ref != nil {
return nil, ref
}
@@ -409,3 +468,16 @@ func (s *Server) policySizeRecords(ctx context.Context, acct *Account, app, host
}
return recs, true
}
+
+// sizeAppPlacement is the app's placement as the change door reads it
+// (appPlacement); with no registry, the stored column alone.
+func (s *Server) sizeAppPlacement(acct *Account, app string) (string, error) {
+ if s.cfg.Registry == "" {
+ return s.store.AppPlacement(acct.ID, app)
+ }
+ reg, err := openRegistry(s.cfg.Registry)
+ if err != nil {
+ return "", err
+ }
+ return s.appPlacement(reg, acct, app)
+}
diff --git a/internal/console/policysize_test.go b/internal/console/policysize_test.go
index ebdd1a60f..12a69a137 100644
--- a/internal/console/policysize_test.go
+++ b/internal/console/policysize_test.go
@@ -87,7 +87,7 @@ func checkSizes(t *testing.T, s *Server, acct *Account, recs []provision.Record,
t.Helper()
out := map[string]*doorRefusal{}
for _, n := range names {
- _, ref := s.sizeCapacityCheck(context.Background(), acct, recs, mustSize(t, n), nil)
+ _, ref := s.sizeCapacityCheck(context.Background(), acct, recs, mustSize(t, n), nil, "")
out[n] = ref
}
return out
@@ -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)
}
@@ -437,3 +437,126 @@ func TestPolicySizeCheckReadsOnlyOnASizeChange(t *testing.T) {
}
}
}
+
+// #875: an app placed on a fleet runs on that fleet's boxes and nowhere
+// else, so its size is judged against them alone. A small platform box and
+// another of the account's fleets do not cap it, and the platform listings
+// are not read (an unreadable platform report leaves the read complete).
+func TestSizeChecksAFleetPlacedAppAgainstItsFleetOnly(t *testing.T) {
+ t.Parallel()
+ fx, acct := sizeFx(t)
+ now := time.Now()
+ putSizeReport(t, fx.rel, "annie", "", now, nil)
+ putSizeEdges(t, fx.rel, "annie", 3847, 0, shopHost)
+ putFixtureObject(t, fx.rel, "status/boxes/zed.json", []byte("{not json"))
+ sizeFleet(t, fx, "classic", fx.sqid, "box-1")
+ sizeFleet(t, fx, "other", fx.sqid, "bo-1")
+ big, small := int64(7990)<<20, int64(2048)<<20
+ putSizeReport(t, release.NewPrefixStore(fx.rel, "fleets/classic/"), "box-1", "", now, &status.BoxMetrics{MemTotalBytes: &big})
+ putSizeReport(t, release.NewPrefixStore(fx.rel, "fleets/other/"), "bo-1", "", now, &status.BoxMetrics{MemTotalBytes: &small})
+ 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, "")
+ 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")
+ 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)
+ }
+ // The quarter rule still holds on the fleet's own box: 7990 MiB (a
+ // t4g.large) allows 1997 MiB, short of xlarge's 2048.
+ if r := checkSizes(t, fx.srv, acct, placed, "xlarge")["xlarge"]; r == nil || r.Status != http.StatusConflict ||
+ !strings.Contains(r.Msg, "the memory on box-1 (7990 MiB)") {
+ t.Fatalf("xlarge on box-1 (7990 MiB) = %+v, want 409 naming box-1", r)
+ }
+ if r := checkSizes(t, fx.srv, acct, placed, "large")["large"]; r != nil {
+ t.Fatalf("large on box-1 (7990 MiB) = %+v, want it to pass", r)
+ }
+ sixteen := int64(16384) << 20
+ putSizeReport(t, release.NewPrefixStore(fx.rel, "fleets/classic/"), "box-1", "", now, &status.BoxMetrics{MemTotalBytes: &sixteen})
+ if r := checkSizes(t, fx.srv, acct, placed, "xlarge")["xlarge"]; r != nil {
+ t.Fatalf("xlarge on box-1 (16384 MiB) = %+v, want it to pass", r)
+ }
+
+ // The same app unplaced is still judged against every box it may land
+ // 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 {
+ 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 {
+ t.Fatal("xlarge for a platform record passed, want a refusal")
+ }
+}
+
+// A record naming a fleet the account does not own (or no longer has) is
+// not a fleet with no boxes: the read is incomplete, so above standard is
+// refused.
+func TestSizeRefusesAboveStandardForAnUnknownPlacedFleet(t *testing.T) {
+ t.Parallel()
+ fx, acct := sizeFx(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, "")
+ if err != nil || complete || len(boxes) != 0 {
+ t.Fatalf("sizeBoxes = %+v, %v, %v, want no boxes and incomplete", boxes, complete, err)
+ }
+ got := checkSizes(t, fx.srv, acct, recs, "xlarge", "standard")
+ if r := got["xlarge"]; r == nil || r.Status != http.StatusServiceUnavailable {
+ t.Fatalf("xlarge = %+v, want 503 (string 3)", r)
+ }
+ if got["standard"] != nil {
+ t.Fatalf("standard = %+v, want it to pass", got["standard"])
+ }
+}
+
+// The door, end to end: an app with no instances yet, placed on a fleet,
+// sets a default size its fleet's box can hold even though a platform box
+// could not.
+func TestPolicyDoorSizesAFleetPlacedAppAgainstItsFleet(t *testing.T) {
+ t.Parallel()
+ fx := newPolicyFx(t, "code-policy-fleet-size")
+ now := time.Now()
+ putSizeReport(t, fx.rel, "annie", "", now, nil)
+ putSizeEdges(t, fx.rel, "annie", 3847, 0)
+ sizeFleet(t, fx.consoleFixture, "classic", fx.sqid, "box-1")
+ big := int64(16384) << 20
+ putSizeReport(t, release.NewPrefixStore(fx.rel, "fleets/classic/"), "box-1", "", now, &status.BoxMetrics{MemTotalBytes: &big})
+
+ st, _, out := fx.put(t, "shop", `{"default_size":"xlarge"}`, policyCreate)
+ if st != http.StatusConflict || !strings.Contains(errorOf(t, out), "the memory on annie (3847 MiB)") {
+ t.Fatalf("xlarge unplaced = %d %s, want 409 naming annie", st, out)
+ }
+ if err := fx.st.SetAppPlacement(OpsAccountID, "shop", registry.PlacementTarget("classic")); err != nil {
+ t.Fatal(err)
+ }
+ if st, _, out := fx.put(t, "shop", `{"default_size":"xlarge"}`, policyCreate); st != http.StatusOK {
+ t.Fatalf("xlarge placed on classic = %d %s, want 200", st, out)
+ }
+}
+
+// One platform record among fleet-placed ones keeps the platform boxes it
+// may land on and every fleet of the account in the check.
+func TestSizeChecksMixedRecordsAgainstBothPlatformAndFleets(t *testing.T) {
+ t.Parallel()
+ fx, acct := sizeFx(t)
+ putSizeReport(t, fx.rel, "annie", "", time.Now(), nil)
+ putSizeEdges(t, fx.rel, "annie", 3847, 0, shopHost)
+ sizeFleet(t, fx, "classic", fx.sqid, "box-1")
+ sizeFleet(t, fx, "other", fx.sqid, "bo-1")
+ recs := []provision.Record{
+ sizeRecord(t, fx.rel, fx.sqid, "shop", shopHost, ""),
+ 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, "")
+ 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)
+ }
+}