| 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 |