carlos / platform Public

console: the committed report covers fleet boxes from their fleet's status reports

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev f1152b275cf2ee6805401036cad021e2a450a948 parent c2690bd
2 files changed, +100 −39
  • internal/console/policysize.go +53 −31
  • internal/console/policysize_test.go +47 −8
diff --git a/internal/console/policysize.go b/internal/console/policysize.go
index 5768b9570..76bdcb0fb 100644
--- a/internal/console/policysize.go
+++ b/internal/console/policysize.go
@@ -16,7 +16,6 @@ package console
import (
"context"
- "encoding/json"
"errors"
"fmt"
"net/http"
@@ -28,6 +27,8 @@ import (
"github.com/carlosframework/platform/internal/cgroup"
"github.com/carlosframework/platform/internal/edgesfile"
"github.com/carlosframework/platform/internal/provision"
+ "github.com/carlosframework/platform/internal/registry"
+ "github.com/carlosframework/platform/internal/release"
"github.com/carlosframework/platform/internal/status"
)
@@ -38,10 +39,16 @@ const (
sizeUnreadMsg = "Could not read every box's memory total, so only small and standard can be set. Try again in a minute."
)
-// sizeBox is one box a size may land on. TotalMiB 0 is unknown.
+// sizeBox is one box a size may land on. TotalMiB 0 is unknown. fleet is
+// the fleet a fleet box belongs to ("" for a platform box); report is the
+// box's own status report as sizeBoxes read it (status/boxes/<box>.json, or
+// fleets/<fleet>/status/boxes/<box>.json for a fleet box), nil when it has
+// none or it could not be read.
type sizeBox struct {
Box, Region string
TotalMiB int64
+ fleet string
+ report *status.Box
}
// sizeCommittedBox is one box's committed figure for the door's reply:
@@ -96,7 +103,7 @@ func (s *Server) sizeBoxes(ctx context.Context, acct *Account, recs []provision.
}
now := time.Now()
seen := map[string]bool{}
- for _, b := range reports {
+ for i, b := range reports {
if b.Label == "" || seen[b.Label] {
continue
}
@@ -110,7 +117,7 @@ func (s *Server) sizeBoxes(ctx context.Context, acct *Account, recs []provision.
continue
}
seen[b.Label] = true
- boxes = append(boxes, sizeBox{Box: b.Label, Region: b.Region, TotalMiB: f.total})
+ boxes = append(boxes, sizeBox{Box: b.Label, Region: b.Region, TotalMiB: f.total, report: &reports[i]})
}
reg, fleets, err := s.systemFleets()
@@ -128,10 +135,32 @@ func (s *Server) sizeBoxes(ctx context.Context, acct *Account, recs []provision.
complete = false
continue
}
+ // The fleet's own status reports, the prefix systemBoxes reads.
+ // Read only when the fleet has an active box.
+ active := slices.IndexFunc(roster, func(fb registry.FleetBox) bool { return fb.Status == "active" }) >= 0
+ var fleetReports map[string]*status.Box
+ if active {
+ fr, frComplete, err := status.LoadBoxesComplete(ctx, release.NewPrefixStore(s.cfg.Releases, "fleets/"+f.Name+"/"))
+ if err != nil || !frComplete {
+ s.policyLogf("console: size check: fleet %s status reports (complete %t): %v", f.Name, frComplete, err)
+ complete = false
+ }
+ fleetReports = make(map[string]*status.Box, len(fr))
+ for i := range fr {
+ if _, dup := fleetReports[fr[i].Label]; !dup {
+ fleetReports[fr[i].Label] = &fr[i]
+ }
+ }
+ }
for _, fb := range roster {
- if fb.Status == "active" {
- boxes = append(boxes, sizeBox{Box: fb.Label})
+ if fb.Status != "active" {
+ continue
}
+ sb := sizeBox{Box: fb.Label, fleet: f.Name, report: fleetReports[fb.Label]}
+ if sb.report != nil {
+ sb.Region = sb.report.Region
+ }
+ boxes = append(boxes, sb)
}
}
slices.SortStableFunc(boxes, func(a, b sizeBox) int { return strings.Compare(a.Box, b.Box) })
@@ -226,33 +255,22 @@ func sizeAboveStandard(size cgroup.Size) bool {
return size.Limit.MemoryMax > std.Limit.MemoryMax
}
-// sizeCommitted is each box's committed figure from its status report
-// (status/boxes/<box>.json): the sum of its awake instances' sizes, its
-// memory total and its available memory, in MiB. A box whose report cannot
-// be read or lacks one of the three is left out. A fleet box's report is
-// under its fleet's prefix, so a fleet box is left out too.
-func (s *Server) sizeCommitted(ctx context.Context, boxes []string) []sizeCommittedBox {
- if s.cfg.Releases == nil {
- return nil
- }
+// sizeCommitted is each box's committed figure from the status report
+// sizeBoxes read for it (a fleet box's under its fleet's prefix): the sum
+// of its awake instances' sizes, its memory total and its available memory,
+// in MiB. A box with no report, or a report lacking one of the three, is
+// left out. It reads nothing: the reports are the size check's own.
+func sizeCommitted(boxes []sizeBox) []sizeCommittedBox {
var out []sizeCommittedBox
- for _, box := range boxes {
- if box == "" || strings.ContainsAny(box, "/\\") || strings.Contains(box, "..") {
+ for _, b := range boxes {
+ if b.report == nil {
continue
}
- body, _, err := s.cfg.Releases.Get(ctx, "status/boxes/"+box+".json")
- if err != nil {
- continue
- }
- var b status.Box
- if json.Unmarshal(body, &b) != nil || b.Label != box {
- continue
- }
- m := b.Metrics
+ m := b.report.Metrics
if m == nil || m.SizeCommittedMiB == nil || m.MemTotalBytes == nil || m.MemAvailBytes == nil {
continue
}
- out = append(out, sizeCommittedBox{Box: box, CommittedMiB: *m.SizeCommittedMiB,
+ out = append(out, sizeCommittedBox{Box: b.Box, CommittedMiB: *m.SizeCommittedMiB,
TotalMiB: *m.MemTotalBytes >> 20, FreeMiB: *m.MemAvailBytes >> 20})
}
return out
@@ -289,7 +307,8 @@ func (s *Server) policySizeCheck(ctx context.Context, acct *Account, app string,
if len(checks) == 0 {
return nil, nil
}
- var labels []string
+ var checked []sizeBox
+ seen := map[[2]string]bool{}
for _, c := range checks {
recs, ok := s.policySizeRecords(ctx, acct, app, c.host)
if !ok && sizeAboveStandard(c.size) {
@@ -302,11 +321,14 @@ func (s *Server) policySizeCheck(ctx context.Context, acct *Account, app string,
return nil, ref
}
for _, b := range boxes {
- labels = append(labels, b.Box)
+ if k := [2]string{b.fleet, b.Box}; !seen[k] {
+ seen[k] = true
+ checked = append(checked, b)
+ }
}
}
- slices.Sort(labels)
- return s.sizeCommitted(ctx, slices.Compact(labels)), nil
+ slices.SortStableFunc(checked, func(a, b sizeBox) int { return strings.Compare(a.Box, b.Box) })
+ return sizeCommitted(checked), 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 fba16b591..9fbf92ee6 100644
--- a/internal/console/policysize_test.go
+++ b/internal/console/policysize_test.go
@@ -5,6 +5,7 @@ import (
"encoding/json"
"net/http"
"reflect"
+ "slices"
"strings"
"testing"
"time"
@@ -94,6 +95,33 @@ func checkSizes(t *testing.T, s *Server, acct *Account, recs []provision.Record,
const shopHost = "shop-a.example.com"
+// noReports is boxes without the status reports they carry, for comparing.
+func noReports(boxes []sizeBox) []sizeBox {
+ out := slices.Clone(boxes)
+ for i := range out {
+ out[i].report = nil
+ }
+ return out
+}
+
+// sizeFleet creates fleet name for account with the given active boxes.
+func sizeFleet(t *testing.T, fx *consoleFixture, name, account string, labels ...string) {
+ t.Helper()
+ reg, err := registry.Open(fx.regPath)
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer reg.Close()
+ if _, err := reg.CreateFleet(registry.Fleet{Name: name, Account: account, Bucket: name + "-data"}); err != nil {
+ t.Fatal(err)
+ }
+ for _, l := range labels {
+ if err := reg.AddFleetBox(registry.FleetBox{Fleet: name, Label: l, TokenHash: strings.Repeat("a", 64)}); err != nil {
+ t.Fatal(err)
+ }
+ }
+}
+
func TestSizeCapacityRefusesAboveAQuarterOfTheSmallestBox(t *testing.T) {
t.Parallel()
fx, acct := sizeFx(t)
@@ -143,7 +171,7 @@ func TestSizeCapacityRefusesAboveStandardWhenATotalIsMissing(t *testing.T) {
if err != nil || !complete {
t.Fatalf("sizeBoxes = %v, %v", complete, err)
}
- if want := []sizeBox{{Box: "annie", TotalMiB: 65536}, {Box: "betty", TotalMiB: 16384}}; !reflect.DeepEqual(boxes, want) {
+ if want := []sizeBox{{Box: "annie", TotalMiB: 65536}, {Box: "betty", TotalMiB: 16384}}; !reflect.DeepEqual(noReports(boxes), want) {
t.Fatalf("boxes = %+v, want %+v", boxes, want)
}
if r := checkSizes(t, fx.srv, acct, recs, "large")["large"]; r != nil {
@@ -234,8 +262,8 @@ func TestSizeBoxesComeFromTheListingsNotTheEdgesFiles(t *testing.T) {
if err != nil || !complete {
t.Fatalf("sizeBoxes = %v, %v", complete, err)
}
- want := []sizeBox{{Box: "annie", TotalMiB: 3884}, {Box: "bx-1"}, {Box: "iris", Region: "ie", TotalMiB: 7820}}
- if !reflect.DeepEqual(boxes, want) {
+ want := []sizeBox{{Box: "annie", TotalMiB: 3884}, {Box: "bx-1", fleet: "fx"}, {Box: "iris", Region: "ie", TotalMiB: 7820}}
+ if !reflect.DeepEqual(noReports(boxes), want) {
t.Fatalf("boxes = %+v, want %+v", boxes, want)
}
@@ -245,8 +273,8 @@ func TestSizeBoxesComeFromTheListingsNotTheEdgesFiles(t *testing.T) {
if err != nil || !complete {
t.Fatalf("sizeBoxes with no records = %v, %v", complete, err)
}
- want = []sizeBox{{Box: "annie", TotalMiB: 3884}, {Box: "bx-1"}, {Box: "ivy", Region: "ie", TotalMiB: 3884}}
- if !reflect.DeepEqual(boxes, want) {
+ want = []sizeBox{{Box: "annie", TotalMiB: 3884}, {Box: "bx-1", fleet: "fx"}, {Box: "ivy", Region: "ie", TotalMiB: 3884}}
+ if !reflect.DeepEqual(noReports(boxes), want) {
t.Fatalf("boxes with no records = %+v, want %+v", boxes, want)
}
}
@@ -273,12 +301,23 @@ func TestSizeCapacityRefusesAboveStandardWhenTheBoxListingIsIncomplete(t *testin
func TestSizeCommittedReportsEachBox(t *testing.T) {
t.Parallel()
- fx, _ := sizeFx(t)
+ fx, acct := sizeFx(t)
committed, total, avail := int64(2304), int64(3884)<<20, int64(1200)<<20
putSizeReport(t, fx.rel, "annie", "", time.Now(), &status.BoxMetrics{SizeCommittedMiB: &committed, MemTotalBytes: &total, MemAvailBytes: &avail})
putSizeReport(t, fx.rel, "betty", "", time.Now(), &status.BoxMetrics{MemTotalBytes: &total, MemAvailBytes: &avail}) // an older box
- got := fx.srv.sizeCommitted(context.Background(), []string{"annie", "betty", "nobody"})
- want := []sizeCommittedBox{{Box: "annie", CommittedMiB: 2304, TotalMiB: 3884, FreeMiB: 1200}}
+ // A fleet box's report lives under its fleet's prefix.
+ sizeFleet(t, fx, "fx", fx.sqid, "bx-1", "bx-2")
+ fc, ft, fa := int64(1024), int64(16384)<<20, int64(9000)<<20
+ 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)
+ if err != nil || !complete {
+ t.Fatalf("sizeBoxes = %v, %v", complete, err)
+ }
+ got := sizeCommitted(boxes)
+ want := []sizeCommittedBox{{Box: "annie", CommittedMiB: 2304, TotalMiB: 3884, FreeMiB: 1200},
+ {Box: "bx-1", CommittedMiB: 1024, TotalMiB: 16384, FreeMiB: 9000}}
if !reflect.DeepEqual(got, want) {
t.Fatalf("committed = %+v, want %+v", got, want)
}