feat: fleet sizes need FleetSizesFloor on every box that reads them (#892 S2)
release.FleetSizesFloor = v0.43.0 (plan ruling P3). A fleet size needs the console and every possible destination box of every record of the app at the floor, judged as R16 judges replicas (release, and prev_release at the floor or "none" or the last apply finished); else PS-2-C04 at the policy, create, copies and place doors, and relabel, drain and replica placement skip the box (appDestinationRefusal). replicaPolicyBarrier takes the floor. At FleetSizesFloor it reads the channels of the app-wide box set; the policy door (a fleet size changing) and placement (a fleet-size app moving to a fleet, coordinator ruling) take replicaFloorMu after the name lock and hold it through their write; the copies door runs it under the mutex it holds. Unreadable answers sizeUnreadMsg at the size doors, copiesUnread at the copies door. replicaFloorJudge refuses a publication below the floor (PS-2-C15) while an app uses a fleet size (default or override, recorded or published) on a fleet with a box following the channel. Census: internal/release envelope re-pinned in the branch overlay.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
docs/superpowers/notes/census-proposal-stateless-scale-2-fleet-sizes.md+1 −1internal/console/apipolicy.go+8 −0internal/console/appcopies.go+33 −4internal/console/fleetbox.go+7 −0internal/console/fleetfloor_test.go+5 −4internal/console/fleets.go+18 −0internal/console/fleetsize.go+277 −5internal/console/fleetsize_test.go+23 −2internal/console/fleetsizefloor_test.go+466 −0internal/console/policysize.go+64 −23internal/console/replica_drill1_test.go+2 −2internal/console/replicafloor.go+79 −18internal/provision/testdata/plan14-root-reads-stateless-scale-2-fleet-sizes.tsv+2 −1internal/release/layoutintent.go+6 −0
Intent
<paseo-system version="1" kind="agent-message" source-agent-id="8a3d302a-eac7-407c-b61c-f5ccc66b5544" message-id="510c0edd-998c-45b9-88d7-5922cab9772e"> From 8a3d302: heads-up: a release from main is being cut now as v0.42.2 (Tito's #1293/#1348/restart fixes). It deliberately avoids v0.43.0 so your FleetSizesFloor = v0.43.0 stays correct; S2 must ride v0.43.0 or later. </paseo-system>
Attributed to this commit: 11.6 g CO₂e (1,427,292 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/docs/superpowers/notes/census-proposal-stateless-scale-2-fleet-sizes.md b/docs/superpowers/notes/census-proposal-stateless-scale-2-fleet-sizes.md| index 7808ad03e..8ea88e894 100644 |
| --- a/docs/superpowers/notes/census-proposal-stateless-scale-2-fleet-sizes.md |
| +++ b/docs/superpowers/notes/census-proposal-stateless-scale-2-fleet-sizes.md |
| @@ -10,7 +10,7 @@ Ruling `census-ruling-stateless-scale-2-fleet-sizes`. The independent reviewer a |
| | `internal/instancectl/server.go` `type Server` | body | unchanged (1) | root: new field Fleet cgroup.FleetGate (#892 S2), root's fleet-size gate; field declaration only; no read added | |
| | `internal/instancectl/size.go` `Server.handleSize` | body | unchanged (1) | root: after the unchanged SizeByName check, the named row must pass Server.Fleet.GenAllowed (a shared size always; a fleet size only on a CARLOS_FLEET box, not gVisor, within the MemTotal budget), else 400 before any systemctl call. Same one site; no read added in this body | |
| -Envelopes re-pinned: `internal/apppolicy`, `internal/cgroup`, `internal/host`, `internal/instancectl`. |
| +Envelopes re-pinned: `internal/apppolicy`, `internal/cgroup`, `internal/host`, `internal/instancectl`, `internal/release`. |
| Review: a model from the other family than the author reads the branch's diff against main on the amadan.net remote (`origin` or `amadan`, whichever points at amadan.net) and gives a verdict for every unit and every re-pinned envelope above. For a unit, compare the change with its recorded disposition and basis below; for an envelope, read every changed function in the package and what it reaches. Reject if the change lets root open, run, query or follow anything it did not before from a path or input a tenant, an app or a box agent can create, write or name; if it weakens a check, refusal, validation, argument or environment the basis relies on; or if the diff cannot settle either. Otherwise approve. Record each verdict on #280 with the tip. A rejected unit goes back to the author to correct; nothing escalates to the census owner. |
diff --git a/internal/console/apipolicy.go b/internal/console/apipolicy.go| index 1efebb848..9c44bf265 100644 |
| --- a/internal/console/apipolicy.go |
| +++ b/internal/console/apipolicy.go |
| @@ -541,6 +541,14 @@ func (s *Server) handleAPIPolicyPut(w http.ResponseWriter, r *http.Request) { |
| if s.cfg.HostClaim { |
| pol.AppID = cur |
| } |
| + // A fleet size needs FleetSizesFloor on every channel the app's boxes |
| + // follow (#892 S2 P3, R11): replicaFloorMu from the size check's |
| + // barrier through the publication, so a system publication below the |
| + // floor and this size never both pass. LOCK ORDER: after this app's |
| + // name lock (above), as the copies and place doors take it. |
| + if policyChecksFleetSize(old, pol, req) { |
| + defer s.lockFleetSizes()() |
| + } |
| committed, warnings, ref := s.policySizeCheck(ctx, acct, app, old, pol, req) |
| if ref != nil { |
| jsonError(w, ref.Status, ref.Msg) |
diff --git a/internal/console/appcopies.go b/internal/console/appcopies.go| index 0be23f2e8..d6fb708d7 100644 |
| --- a/internal/console/appcopies.go |
| +++ b/internal/console/appcopies.go |
| @@ -44,9 +44,21 @@ const ( |
| ) |
| func atReplicaFloor(build string) bool { |
| + return atReleaseFloor(build, release.StatelessReplicasFloor) |
| +} |
| + |
| +// atFleetSizesFloor: build is a release at release.FleetSizesFloor or |
| +// later (#892 S2 P3). |
| +func atFleetSizesFloor(build string) bool { |
| + return atReleaseFloor(build, release.FleetSizesFloor) |
| +} |
| + |
| +// atReleaseFloor: build is a release at floor or later. A build that is |
| +// not a release version is below every floor. |
| +func atReleaseFloor(build, floor string) bool { |
| v, ok := release.ParseReleaseVersion(build) |
| - floor, valid := release.ParseReleaseVersion(release.StatelessReplicasFloor) |
| - return ok && valid && v.Compare(floor) >= 0 |
| + f, valid := release.ParseReleaseVersion(floor) |
| + return ok && valid && v.Compare(f) >= 0 |
| } |
| // replicasActive is count admission's floor: the console and every box of |
| @@ -131,9 +143,14 @@ func (s *Server) handleAPICopies(w http.ResponseWriter, r *http.Request) { |
| // both pass. |
| defer s.lockReplicaFloor()() |
| } |
| + // One read of the app's policy for every fleet-size gate below. |
| + ctx = withAppPolicyMemo(ctx) |
| ref = s.copiesRefusal(ctx, acct, app, *req.Copies, nil) |
| if ref == nil && *req.Copies > 1 { |
| - ref = s.replicaPolicyBarrier(ctx, acct, app) |
| + ref = s.replicaPolicyBarrier(ctx, acct, app, release.StatelessReplicasFloor) |
| + } |
| + if ref == nil && *req.Copies > 1 { |
| + ref = s.copiesFleetSizesBarrier(ctx, acct, app) |
| } |
| if ref == nil && *req.Copies > 1 { |
| ref = s.copiesBaselineRefusal(ctx, acct, app) |
| @@ -422,9 +439,21 @@ func (s *Server) copiesSizeRefusal(ctx context.Context, acct *Account, app, flee |
| return &doorRefusal{Status: http.StatusServiceUnavailable, Msg: copiesUnread} |
| } |
| gate := withAppPolicyMemo(ctx) |
| - if _, _, ref := s.appFleetSizeOf(gate, acct, app); ref != nil { |
| + _, fleetSized, ref := s.appFleetSizeOf(gate, acct, app) |
| + if ref != nil { |
| return &doorRefusal{Status: http.StatusServiceUnavailable, Msg: copiesUnread} |
| } |
| + // A fleet size needs every box of the fleet, each a possible |
| + // destination, and the console at FleetSizesFloor (#892 S2 P3). |
| + if fleetSized { |
| + boxes := make([]sizeBox, 0, len(v.Boxes)) |
| + for _, b := range v.Boxes { |
| + boxes = append(boxes, b.sizeBox(fleet)) |
| + } |
| + if ref := s.fleetSizesFloorRefusal(boxes); ref != nil { |
| + return ref |
| + } |
| + } |
| v.DestRefused = s.appDestinationsRefused(gate, acct, app, fleet, v.Boxes) |
| live := make([]provision.Record, 0, len(recs)) |
| for _, rec := range recs { |
diff --git a/internal/console/fleetbox.go b/internal/console/fleetbox.go| index 988bce8da..1b8d61721 100644 |
| --- a/internal/console/fleetbox.go |
| +++ b/internal/console/fleetbox.go |
| @@ -88,6 +88,11 @@ type fleetBoxView struct { |
| // apply restores carlos.prev. An unknown prev and a missing |
| // apply_finished are neither. |
| RollbackAtReplicaFloor bool |
| + // AtFleetSizesFloor and RollbackAtFleetSizesFloor are the same pair at |
| + // release.FleetSizesFloor (#892 S2 P3), judged exactly as the replica |
| + // pair: the heartbeat's release at the floor; its prev_release at the |
| + // floor or "none", or its last apply finished. |
| + AtFleetSizesFloor, RollbackAtFleetSizesFloor bool |
| } |
| // fleetBoxViewOf is an active registry box before its heartbeat is read. |
| @@ -124,6 +129,8 @@ func joinBeats(boxes []fleetBoxView, hbs []status.Box, now time.Time) (unheard b |
| } |
| boxes[i].AtReplicaFloor = atReplicaFloor(b.Metrics.Release) |
| boxes[i].RollbackAtReplicaFloor = b.Metrics.PrevRelease == "none" || atReplicaFloor(b.Metrics.PrevRelease) || b.Metrics.ApplyFinished |
| + boxes[i].AtFleetSizesFloor = atFleetSizesFloor(b.Metrics.Release) |
| + boxes[i].RollbackAtFleetSizesFloor = fleetSizesRollbackAtFloor(b.Metrics) |
| } |
| } |
| return unheard |
diff --git a/internal/console/fleetfloor_test.go b/internal/console/fleetfloor_test.go| index ed2c1fd53..7469dd801 100644 |
| --- a/internal/console/fleetfloor_test.go |
| +++ b/internal/console/fleetfloor_test.go |
| @@ -530,13 +530,14 @@ func TestFleetFloorListErrorRefuses(t *testing.T) { |
| t.Fatalf("the pointer moved to %s", v) |
| } |
| // Moves that pass on the version alone read no fleets. Below the |
| - // stateless replicas floor that judge lists them too (replicafloor.go), |
| - // so these targets are at or above both floors. |
| - if _, err := s.systemFloorGate(t.Context(), "router", "canary", release.StatelessReplicasFloor, false, false, false); err != nil { |
| + // stateless replicas floor or the fleet sizes floor that judge lists |
| + // them too (replicafloor.go), so these targets are at or above every |
| + // floor, release.FleetSizesFloor the highest. |
| + if _, err := s.systemFloorGate(t.Context(), "router", "canary", release.FleetSizesFloor, false, false, false); err != nil { |
| t.Fatalf("a first publication at the floor = %v", err) |
| } |
| unreserved(t, d) |
| - if p, err := s.promoteHostUpdate(t.Context(), fpRelease(target, "v0.35.1"), "router", "stable", release.SystemPromoteOptions{}); err != nil || p.Current.Version != "v0.35.1" { |
| + if p, err := s.promoteHostUpdate(t.Context(), fpRelease(target, "v0.43.1"), "router", "stable", release.SystemPromoteOptions{}); err != nil || p.Current.Version != "v0.43.1" { |
| t.Fatalf("a promotion above the floor = %+v, %v", p, err) |
| } |
| } |
diff --git a/internal/console/fleets.go b/internal/console/fleets.go| index 333508f59..23c102e2e 100644 |
| --- a/internal/console/fleets.go |
| +++ b/internal/console/fleets.go |
| @@ -1021,6 +1021,9 @@ func (s *Server) placeAppTarget(ctx context.Context, acct *Account, app, target |
| if ref := s.appTargetRefusal(ctx, acct, app, target); ref != nil { |
| return ref.Status, ref.Msg |
| } |
| + if ref := s.appTargetFloorBarrier(ctx, acct, app, target); ref != nil { |
| + return ref.Status, ref.Msg |
| + } |
| } |
| // The store-of-record write (2026-08-06 ruling): appPlacement consults |
| @@ -1071,9 +1074,24 @@ func (s *Server) placeAppTarget(ctx context.Context, acct *Account, app, target |
| if s.placeNameLocked != nil { |
| s.placeNameLocked(acct.Sqid, app) |
| } |
| + // An app with a fleet size moving to a fleet also passes the |
| + // FleetSizesFloor barrier over that fleet's boxes (#892 S2 P3, |
| + // coordinator ruling on Task 3), under replicaFloorMu from the barrier |
| + // through the record walk, so a system publication below the floor to |
| + // a channel they follow and this move never both pass. The size is |
| + // read once (the memo) and cannot change while the name lock is held. |
| + ctx = withAppPolicyMemo(ctx) |
| + if _, sized, ref := s.appFleetSizeOf(ctx, acct, app); ref != nil { |
| + return ref.Status, ref.Msg |
| + } else if _, toFleet := registry.FleetPlacement(target); sized && toFleet { |
| + defer s.lockFleetSizes()() |
| + } |
| if ref := s.appTargetRefusal(ctx, acct, app, target); ref != nil { |
| return ref.Status, ref.Msg |
| } |
| + if ref := s.appTargetFloorBarrier(ctx, acct, app, target); ref != nil { |
| + return ref.Status, ref.Msg |
| + } |
| if s.placeAfterCheck != nil { |
| s.placeAfterCheck(acct.Sqid, app) |
| } |
diff --git a/internal/console/fleetsize.go b/internal/console/fleetsize.go| index 5c4bea554..f874cfa1a 100644 |
| --- a/internal/console/fleetsize.go |
| +++ b/internal/console/fleetsize.go |
| @@ -19,14 +19,20 @@ package console |
| import ( |
| "context" |
| + "errors" |
| "fmt" |
| "net/http" |
| + "os" |
| + "sort" |
| + "strings" |
| "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" |
| + "github.com/carlosframework/platform/internal/release" |
| + "github.com/carlosframework/platform/internal/status" |
| ) |
| // Door copy (deck 2026-11-11-stateless-replicas-scale-copy.md, section S2). |
| @@ -39,8 +45,267 @@ const ( |
| 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" |
| + // PS-2-C04 (the reworked SR-4-C20) |
| + fleetSizeFloorMsg = "this size needs a complete memory report and a platform release that supports it on every box the app can run on and in the console. update the boxes and try again" |
| ) |
| +// fleetSizesRollbackAtFloor is RollbackAtFleetSizesFloor, mirroring |
| +// RollbackAtReplicaFloor (#511 R16 and its amendment): no automatic |
| +// rollback can put the box below release.FleetSizesFloor, because its |
| +// prev_release is at the floor or "none", or its last system apply has |
| +// finished (only an unfinished apply restores carlos.prev). An unknown |
| +// prev and a missing apply_finished are neither. |
| +func fleetSizesRollbackAtFloor(m *status.BoxMetrics) bool { |
| + return m != nil && (m.PrevRelease == "none" || atFleetSizesFloor(m.PrevRelease) || m.ApplyFinished) |
| +} |
| + |
| +// fleetSizesBoxAtFloor is a box report's whole floor judgment for a fleet |
| +// size (#892 S2 P3): its release at release.FleetSizesFloor and no |
| +// rollback below it (fleetSizesRollbackAtFloor). No metrics is not. |
| +func fleetSizesBoxAtFloor(m *status.BoxMetrics) bool { |
| + return m != nil && atFleetSizesFloor(m.Release) && fleetSizesRollbackAtFloor(m) |
| +} |
| + |
| +// fleetSizesConsoleAtFloor: this console's own build is a release at |
| +// release.FleetSizesFloor. A build that is not a release is below it. |
| +func (s *Server) fleetSizesConsoleAtFloor() bool { |
| + return atFleetSizesFloor(s.replicaBuild()) |
| +} |
| + |
| +// fleetSizesFloorRefusal is PS-2-C04 (409) unless the console and every |
| +// one of boxes, the possible destinations of an app with a fleet size, |
| +// are at release.FleetSizesFloor with a memory report; nil otherwise. |
| +func (s *Server) fleetSizesFloorRefusal(boxes []sizeBox) *doorRefusal { |
| + refuse := &doorRefusal{Status: http.StatusConflict, Msg: fleetSizeFloorMsg} |
| + if !s.fleetSizesConsoleAtFloor() { |
| + return refuse |
| + } |
| + for _, b := range boxes { |
| + if !b.atFleetFloor || b.TotalMiB <= 0 { |
| + return refuse |
| + } |
| + } |
| + return nil |
| +} |
| + |
| +// fleetSizesChannelBarrier is the FleetSizesFloor barrier over boxes, the |
| +// possible destinations of an app with a fleet size (#892 S2 P3, R11): the |
| +// system channel each box's heartbeat follows (stable when it names none), |
| +// one router pointer GET per channel, in key order. A pointer below the |
| +// floor, or one that does not parse, is PS-2-C04 (409). A box with no |
| +// report, or a pointer that cannot be read or verified, is sizeUnreadMsg |
| +// (503). A followed channel with no pointer passes; a channel no box |
| +// follows is never read. The caller holds replicaFloorMu (lockFleetSizes) |
| +// through its write, so a publication judged after it sees the size. |
| +func (s *Server) fleetSizesChannelBarrier(ctx context.Context, boxes []sizeBox) *doorRefusal { |
| + unread := &doorRefusal{Status: http.StatusServiceUnavailable, Msg: sizeUnreadMsg} |
| + if s.cfg.Releases == nil { |
| + return unread |
| + } |
| + followed := map[string]bool{} |
| + for _, b := range boxes { |
| + if b.report == nil || b.report.Metrics == nil { |
| + s.policyLogf("console: fleet sizes barrier: box %s has not reported its system channel", b.Box) |
| + return unread |
| + } |
| + ch := b.report.Metrics.SystemChannel |
| + if ch == "" { |
| + ch = release.RungStable |
| + } |
| + followed[ch] = true |
| + } |
| + channels := make([]string, 0, len(followed)) |
| + for ch := range followed { |
| + channels = append(channels, ch) |
| + } |
| + sort.Strings(channels) |
| + for _, ch := range channels { |
| + p, _, err := release.GetSystemPointer(ctx, s.cfg.Releases, s.cfg.ReleasePub, release.SystemComponentRouter, ch) |
| + if errors.Is(err, os.ErrNotExist) { |
| + continue |
| + } |
| + if err != nil { |
| + s.policyLogf("console: fleet sizes barrier: system pointer %s: %v", ch, err) |
| + return unread |
| + } |
| + if !atFleetSizesFloor(p.Current.Version) { |
| + return &doorRefusal{Status: http.StatusConflict, Msg: fleetSizeFloorMsg} |
| + } |
| + } |
| + return nil |
| +} |
| + |
| +// fleetSizesPolicyBarrier is replicaPolicyBarrier at FleetSizesFloor: |
| +// fleetSizesChannelBarrier over every possible destination of every record |
| +// of the app (sizeBoxes over all its records, or the fleet it is placed on |
| +// when it has none), the set the policy door's size check judged. A read |
| +// that is not whole is sizeUnreadMsg (503). |
| +func (s *Server) fleetSizesPolicyBarrier(ctx context.Context, acct *Account, app string) *doorRefusal { |
| + unread := &doorRefusal{Status: http.StatusServiceUnavailable, Msg: sizeUnreadMsg} |
| + recs, ok := s.policySizeRecords(ctx, acct, app, "") |
| + if !ok { |
| + return unread |
| + } |
| + appFleet := "" |
| + if len(recs) == 0 { |
| + placement, err := s.sizeAppPlacement(acct, app) |
| + if err != nil { |
| + s.policyLogf("console: fleet sizes barrier %s/%s: placement: %v", acct.Sqid, app, err) |
| + return unread |
| + } |
| + appFleet, _ = registry.FleetPlacement(placement) |
| + } |
| + boxes, complete, _, err := s.sizeBoxes(ctx, acct, recs, nil, appFleet) |
| + if err != nil || !complete { |
| + s.policyLogf("console: fleet sizes barrier %s/%s: boxes (complete %t): %v", acct.Sqid, app, complete, err) |
| + return unread |
| + } |
| + return s.fleetSizesChannelBarrier(ctx, boxes) |
| +} |
| + |
| +// copiesFleetSizesBarrier is the copies door's FleetSizesFloor barrier, |
| +// under the replicaFloorMu it already holds: for an app with a fleet size, |
| +// replicaPolicyBarrier at FleetSizesFloor, an unread answer given as the |
| +// door's own copiesUnread. An app with no fleet size passes. |
| +func (s *Server) copiesFleetSizesBarrier(ctx context.Context, acct *Account, app string) *doorRefusal { |
| + _, ok, ref := s.appFleetSizeOf(withAppPolicyMemo(ctx), acct, app) |
| + if ref != nil { |
| + return &doorRefusal{Status: http.StatusServiceUnavailable, Msg: copiesUnread} |
| + } |
| + if !ok { |
| + return nil |
| + } |
| + if ref := s.replicaPolicyBarrier(ctx, acct, app, release.FleetSizesFloor); ref != nil { |
| + if ref.Status == http.StatusServiceUnavailable { |
| + return &doorRefusal{Status: http.StatusServiceUnavailable, Msg: copiesUnread} |
| + } |
| + return ref |
| + } |
| + return nil |
| +} |
| + |
| +// appTargetFloorBarrier is placement's FleetSizesFloor barrier (#892 S2, |
| +// coordinator ruling on Task 3): for an app with a fleet size moving to |
| +// fleet region, fleetSizesChannelBarrier over that fleet's active boxes, |
| +// so a channel already below the floor that they follow refuses the move. |
| +// Anything else passes (appTargetRefusal judges it). The caller holds |
| +// replicaFloorMu (lockFleetSizes) through its record walk. |
| +func (s *Server) appTargetFloorBarrier(ctx context.Context, acct *Account, app, region string) *doorRefusal { |
| + _, ok, ref := s.appFleetSizeOf(withAppPolicyMemo(ctx), acct, app) |
| + if ref != nil { |
| + return ref |
| + } |
| + fleet, placed := registry.FleetPlacement(region) |
| + if !ok || !placed { |
| + return nil |
| + } |
| + boxes, complete, _, err := s.sizeBoxes(ctx, acct, nil, nil, fleet) |
| + if err != nil || !complete { |
| + s.policyLogf("console: fleet sizes barrier %s/%s: boxes of %s (complete %t): %v", acct.Sqid, app, fleet, complete, err) |
| + return &doorRefusal{Status: http.StatusServiceUnavailable, Msg: sizeUnreadMsg} |
| + } |
| + return s.fleetSizesChannelBarrier(ctx, boxes) |
| +} |
| + |
| +// fleetSizeAppsInUse is every app whose policy names a fleet size |
| +// (appFleetSizeOf: the recorded and the published policy, stale overrides |
| +// included), with the fleets that can host it, for replicaFloorJudge. The |
| +// inventory is every policy of every fleet-owning account, from the bucket |
| +// listing and the console's own records, so neither alone can hide one. An |
| +// app's fleets are those its live records are placed on and the one it is |
| +// placed on; with none, every fleet of its account. Any read that fails |
| +// refuses. ctx should carry no write memo (withoutReqMemo). |
| +func (s *Server) fleetSizeAppsInUse(ctx context.Context, fleets []registry.Fleet) ([]replicaInUse, error) { |
| + if s.cfg.Releases == nil || s.store == nil { |
| + return nil, nil |
| + } |
| + accountFleets := map[string][]string{} |
| + for _, f := range fleets { |
| + if f.Account != "" { |
| + accountFleets[f.Account] = append(accountFleets[f.Account], f.Name) |
| + } |
| + } |
| + candidates := map[[2]string]bool{} |
| + for acct := range accountFleets { |
| + prefix := apppolicy.Prefix + acct + "/" |
| + keys, err := s.cfg.Releases.List(ctx, prefix) |
| + if err != nil { |
| + return nil, err |
| + } |
| + for _, key := range keys { |
| + app, ok := strings.CutSuffix(strings.TrimPrefix(key, prefix), ".json") |
| + if ok && app != "" && !strings.Contains(app, "/") { |
| + candidates[[2]string{acct, app}] = true |
| + } |
| + } |
| + } |
| + rows, err := s.store.AppPolicyRowsContext(ctx) |
| + if err != nil { |
| + return nil, err |
| + } |
| + for _, row := range rows { |
| + if _, ok := accountFleets[row.Sqid]; ok { |
| + candidates[[2]string{row.Sqid, row.App}] = true |
| + } |
| + } |
| + keys := make([][2]string, 0, len(candidates)) |
| + for k := range candidates { |
| + keys = append(keys, k) |
| + } |
| + sort.Slice(keys, func(i, j int) bool { |
| + if keys[i][0] != keys[j][0] { |
| + return keys[i][0] < keys[j][0] |
| + } |
| + return keys[i][1] < keys[j][1] |
| + }) |
| + var out []replicaInUse |
| + for _, k := range keys { |
| + sqid, app := k[0], k[1] |
| + acct, err := s.store.AccountBySqid(sqid) |
| + if err != nil { |
| + return nil, err |
| + } |
| + if acct == nil { |
| + return nil, fmt.Errorf("account %s not found", sqid) |
| + } |
| + _, sized, ref := s.appFleetSizeOf(ctx, acct, app) |
| + if ref != nil { |
| + return nil, fmt.Errorf("policy of %s/%s could not be read", sqid, app) |
| + } |
| + if !sized { |
| + continue |
| + } |
| + recs, err := placeRecords(ctx, s.cfg.Releases, provision.JudgeFrom(ctx), sqid, app) |
| + if err != nil { |
| + return nil, err |
| + } |
| + placed := map[string]bool{} |
| + for _, rec := range recs { |
| + if rec.Retired { |
| + continue |
| + } |
| + if fleet, ok := registry.FleetPlacement(rec.Region); ok { |
| + placed[fleet] = true |
| + } |
| + } |
| + if placement, err := s.sizeAppPlacement(acct, app); err != nil { |
| + return nil, err |
| + } else if fleet, ok := registry.FleetPlacement(placement); ok { |
| + placed[fleet] = true |
| + } |
| + use := replicaInUse{acct: sqid, app: app} |
| + for fleet := range placed { |
| + use.fleets = append(use.fleets, fleet) |
| + } |
| + if len(use.fleets) == 0 { |
| + use.fleets = append(use.fleets, accountFleets[sqid]...) |
| + } |
| + sort.Strings(use.fleets) |
| + out = append(out, use) |
| + } |
| + return out, nil |
| +} |
| + |
| // 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 |
| @@ -198,10 +463,17 @@ func (s *Server) appDestinationRefusal(ctx context.Context, acct *Account, app s |
| 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)} |
| + move := &doorRefusal{Status: http.StatusConflict, Msg: fmt.Sprintf(fleetSizeMoveMsg, app, size.Name)} |
| + if dest.fleet == "" { |
| + return move |
| + } |
| + // The FleetSizesFloor condition (#892 S2 P3), on dest and on the |
| + // console, so every caller asks it: PS-2-C04. |
| + if !s.fleetSizesConsoleAtFloor() || !dest.atFleetFloor { |
| + return &doorRefusal{Status: http.StatusConflict, Msg: fleetSizeFloorMsg} |
| + } |
| + if !sizeFits(dest.TotalMiB, size) { |
| + return move |
| } |
| return nil |
| } |
| @@ -242,7 +514,7 @@ func (s *Server) appTargetRefusal(ctx context.Context, acct *Account, app, regio |
| // 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} |
| + return sizeBox{Box: b.Label, TotalMiB: b.TotalMiB, fleet: fleet, atFleetFloor: b.AtFleetSizesFloor && b.RollbackAtFleetSizesFloor} |
| } |
| // appDestinationsRefused is the labels of boxes, active boxes of fleet, |
diff --git a/internal/console/fleetsize_test.go b/internal/console/fleetsize_test.go| index 62438b40d..2a41bfc44 100644 |
| --- a/internal/console/fleetsize_test.go |
| +++ b/internal/console/fleetsize_test.go |
| @@ -29,12 +29,12 @@ const ( |
| ) |
| // fleetMemBeat writes box's heartbeat on fleet: fresh, at every floor the |
| -// doors ask (release.FleetRecordLabelFloor), reporting mib of memory (0: |
| +// doors ask (release.FleetSizesFloor, the highest), 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"} |
| + Release: release.FleetSizesFloor, PrevRelease: "none"} |
| if mib > 0 { |
| total, avail := mib<<20, (mib/2)<<20 |
| m.MemTotalBytes, m.MemAvailBytes = &total, &avail |
| @@ -72,6 +72,12 @@ func fleetRec(t *testing.T, fx *consoleFixture, host, fleet, box string) provisi |
| return rec |
| } |
| +// fleetSizesConsole puts fx's console at release.FleetSizesFloor, which |
| +// every fleet size needs (#892 S2 Task 3). |
| +func fleetSizesConsole(fx *consoleFixture) { |
| + fx.srv.consoleBuild = release.FleetSizesFloor |
| +} |
| + |
| func wantFleetRefusal(t *testing.T, what string, got *doorRefusal, status int, msg string) { |
| t.Helper() |
| if got == nil || got.Status != status || got.Msg != msg { |
| @@ -111,6 +117,7 @@ func TestFleetSizeDoorBudget(t *testing.T) { |
| // 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") |
| + fleetSizesConsole(fx.consoleFixture) |
| 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 { |
| @@ -128,6 +135,7 @@ func TestFleetSizeDoorBudget(t *testing.T) { |
| func TestFleetSizeSharedRefused(t *testing.T) { |
| t.Parallel() |
| fx, acct := sizeFx(t) |
| + fleetSizesConsole(fx) |
| putSizeReport(t, fx.rel, "annie", "", time.Now(), nil) |
| putSizeEdges(t, fx.rel, "annie", 65536, 0) |
| sizeFleet(t, fx, "fx", fx.sqid, "bx-1") |
| @@ -170,6 +178,7 @@ func TestFleetSizeSharedRefused(t *testing.T) { |
| func TestFleetSizeSpareBoxCounts(t *testing.T) { |
| t.Parallel() |
| fx, acct := sizeFx(t) |
| + fleetSizesConsole(fx) |
| 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) |
| @@ -184,6 +193,7 @@ func TestFleetSizeSpareBoxCounts(t *testing.T) { |
| func TestFleetSizeIncompleteFailsClosed(t *testing.T) { |
| t.Parallel() |
| fx, acct := sizeFx(t) |
| + fleetSizesConsole(fx) |
| 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 |
| @@ -202,6 +212,7 @@ func TestFleetSizeIncompleteFailsClosed(t *testing.T) { |
| // blocked (SR-4-C86), and the policy's size stays as it is. |
| func TestFleetPlacementShortfall(t *testing.T) { |
| fx, _, reg := replicaPlacementWorld(t) |
| + fleetSizesConsole(fx) |
| registerFleetBox(t, reg, "pi", "pi-3") |
| putShopPolicy(t, fx, apppolicy.Policy{DefaultSize: "16g"}) |
| sizedBeat(t, fx, "pi-1", 32768) |
| @@ -234,6 +245,7 @@ func TestFleetPlacementShortfall(t *testing.T) { |
| func TestFleetSizeCreateChecked(t *testing.T) { |
| fx, token, _ := fleetBoxWorld(t, "code-fs-create", "pi-1", "pi-2") |
| + fleetSizesConsole(fx) |
| fleetMemBeat(t, fx, "pi", "pi-1", 32768, nil) |
| fleetMemBeat(t, fx, "pi", "pi-2", 8192, nil) |
| putShopPolicy(t, fx, apppolicy.Policy{DefaultSize: "12g"}) |
| @@ -251,6 +263,7 @@ func TestFleetSizeCreateChecked(t *testing.T) { |
| func TestFleetSizeCopiesDoor(t *testing.T) { |
| fx, token := copiesWorld(t) |
| + fleetSizesConsole(fx) |
| putShopPolicy(t, fx, apppolicy.Policy{DefaultSize: "12g"}) |
| sizedBeat(t, fx, "pi-1", 32768) |
| sizedBeat(t, fx, "pi-2", 8192) |
| @@ -274,6 +287,7 @@ func TestFleetSizeCopiesDoor(t *testing.T) { |
| func TestFleetSizeOverrideAppWide(t *testing.T) { |
| t.Parallel() |
| fx := newPolicyFx(t, "code-fs-appwide") |
| + fleetSizesConsole(fx.consoleFixture) |
| putSizeReport(t, fx.rel, "annie", "", time.Now(), nil) |
| putSizeEdges(t, fx.rel, "annie", 65536, 0) |
| sizeFleet(t, fx.consoleFixture, "fx", fx.sqid, "bx-1") |
| @@ -300,6 +314,7 @@ func TestFleetSizeOverrideAppWide(t *testing.T) { |
| func TestFleetSizeCommittedWarns(t *testing.T) { |
| t.Parallel() |
| fx := newPolicyFx(t, "code-fs-warn") |
| + fleetSizesConsole(fx.consoleFixture) |
| sizeFleet(t, fx.consoleFixture, "fx", fx.sqid, "bx-1") |
| fleetRec(t, fx.consoleFixture, shopHost, "fx", "bx-1") |
| set := func(etag, value string) policyReply { |
| @@ -336,6 +351,7 @@ func TestFleetSizeCommittedWarns(t *testing.T) { |
| // An unlabelled instance runs on every box of its fleet (provisioning's |
| // ownsRecord), so it counts on each of them (review round 1). |
| ux := newPolicyFx(t, "code-fs-warn-unlabelled") |
| + fleetSizesConsole(ux.consoleFixture) |
| sizeFleet(t, ux.consoleFixture, "fx", ux.sqid, "bx-1") |
| fleetRec(t, ux.consoleFixture, shopHost, "fx", "") |
| committed = 20480 |
| @@ -351,6 +367,7 @@ func TestFleetSizeCommittedWarns(t *testing.T) { |
| func TestFleetSizeEmptyFleetRefused(t *testing.T) { |
| t.Parallel() |
| fx := newPolicyFx(t, "code-fs-empty") |
| + fleetSizesConsole(fx.consoleFixture) |
| sizeFleet(t, fx.consoleFixture, "fx", fx.sqid) |
| if err := fx.st.SetAppPlacement(OpsAccountID, "shop", registry.PlacementTarget("fx")); err != nil { |
| t.Fatal(err) |
| @@ -387,6 +404,7 @@ func TestFleetSizeInterruptedPublication(t *testing.T) { |
| t.Run(tc.name, func(t *testing.T) { |
| t.Parallel() |
| fx := newPolicyFx(t, "code-fs-pub-"+strings.ReplaceAll(tc.name, " ", "-")) |
| + fleetSizesConsole(fx.consoleFixture) |
| sizeFleet(t, fx.consoleFixture, "fx", fx.sqid, "bx-1") |
| fleetMemBeat(t, fx.consoleFixture, "fx", "bx-1", 32768, nil) |
| if err := fx.st.SetAppPlacement(OpsAccountID, "shop", registry.PlacementTarget("fx")); err != nil { |
| @@ -429,6 +447,7 @@ func TestFleetSizePolicyAndPlaceSerialize(t *testing.T) { |
| setup := func(t *testing.T, code string) (*consoleFixture, string) { |
| t.Helper() |
| fx, token, _ := fleetBoxWorld(t, code, "pi-1") |
| + fleetSizesConsole(fx) |
| fleetMemBeat(t, fx, "pi", "pi-1", 32768, nil) |
| sizeFleet(t, fx, "small", fx.sqid, "sm-1") |
| fleetMemBeat(t, fx, "small", "sm-1", 8192, nil) |
| @@ -501,6 +520,7 @@ func TestFleetSizePolicyAndPlaceSerialize(t *testing.T) { |
| func TestFleetSizedAppMoveRefused(t *testing.T) { |
| fx, token, _ := fleetBoxWorld(t, "code-fs-move", "pi-1") |
| + fleetSizesConsole(fx) |
| fleetMemBeat(t, fx, "pi", "pi-1", 16384, nil) |
| sizeFleet(t, fx, "tiny", fx.sqid, "tn-1") |
| fleetMemBeat(t, fx, "tiny", "tn-1", 4096, nil) |
| @@ -541,6 +561,7 @@ 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") |
| + fleetSizesConsole(fx) |
| registerFleetBox(t, reg, "pi", "pi-3") |
| fleetMemBeat(t, fx, "pi", "pi-1", 32768, nil) |
| fleetMemBeat(t, fx, "pi", "pi-2", 32768, nil) |
diff --git a/internal/console/fleetsizefloor_test.go b/internal/console/fleetsizefloor_test.go| new file mode 100644 |
| index 000000000..b5d5af531 |
| --- /dev/null |
| +++ b/internal/console/fleetsizefloor_test.go |
| @@ -0,0 +1,466 @@ |
| +package console |
| + |
| +import ( |
| + "context" |
| + "errors" |
| + "net/http" |
| + "testing" |
| + "time" |
| + |
| + "github.com/carlosframework/platform/internal/apppolicy" |
| + "github.com/carlosframework/platform/internal/provision" |
| + "github.com/carlosframework/platform/internal/release" |
| + "github.com/carlosframework/platform/internal/status" |
| +) |
| + |
| +// #892 S2 Task 3: the fleet-size floor (plan ruling P3). A fleet size |
| +// needs release.FleetSizesFloor on the console and on every box that can |
| +// host any instance of the app, judged as R16 judges replicas, and no |
| +// system channel such a box follows may name a release below it. Apps |
| +// shop and blog, fleets pi, big and small, hosts under example.com. |
| + |
| +const ( |
| + // fsBelow is a release below FleetSizesFloor, above every other floor. |
| + fsBelow = "v0.42.1" |
| + // fsC04 is PS-2-C04. |
| + fsC04 = "this size needs a complete memory report and a platform release that supports it on every box the app can run on and in the console. update the boxes and try again" |
| + // fsC15 is PS-2-C15 at FleetSizesFloor. |
| + fsC15 = "sizes from 4g up are in use, so the platform cannot move below v0.43.0. set those apps and instances to xlarge or smaller first" |
| + // fsUnread is the size door's unread answer (sizeUnreadMsg). |
| + fsUnread = "could not read every box's memory total, so only small and standard can be set. try again in a minute" |
| +) |
| + |
| +// fsBeat writes box's heartbeat on fleet: fresh, rootfs-ready, nested, |
| +// with mib of memory, at release rel with its rollback binary at prev, |
| +// following system channel ch ("" is stable). On fleet pi it reports the |
| +// instance copiesWorld puts on the box (a on pi-1, b on pi-2), as the |
| +// copies door asks. |
| +func fsBeat(t *testing.T, fx *consoleFixture, fleet, box string, mib int64, rel, prev, ch string) { |
| + t.Helper() |
| + total, avail := mib<<20, (mib/2)<<20 |
| + m := &status.BoxMetrics{Layout: &status.LayoutReport{Mode: "nested"}, RootfsReady: true, Release: rel, PrevRelease: prev, |
| + SystemChannel: ch, MemTotalBytes: &total, MemAvailBytes: &avail} |
| + if fleet == "pi" && (box == "pi-1" || box == "pi-2") { |
| + host, ver := "a.example.com", "v1" |
| + if box == "pi-2" { |
| + host, ver = "b.example.com", "v2" |
| + } |
| + m.Rollout = &status.RolloutReport{Hosts: map[string]status.HostRollout{host: {Account: fx.sqid, App: "shop", Serving: ver, Installed: ver, Awake: true}}} |
| + } |
| + 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) |
| + } |
| +} |
| + |
| +// fsAtFloor beats every named pi box at the floor with 32 GiB, puts the |
| +// console at the floor, and gives it the system signing key. |
| +func fsAtFloor(t *testing.T, fx *consoleFixture, boxes ...string) { |
| + t.Helper() |
| + fx.srv.consoleBuild = release.FleetSizesFloor |
| + fx.srv.cfg.Key = fx.priv |
| + for _, b := range boxes { |
| + fsBeat(t, fx, "pi", b, 32768, release.FleetSizesFloor, "none", "") |
| + } |
| +} |
| + |
| +func fsPolicyPut(t *testing.T, fx *consoleFixture, token, body string) (int, string) { |
| + t.Helper() |
| + code, _, out := policyReq(t, fx, http.MethodPut, "shop", token, body, policyCreate) |
| + return code, out |
| +} |
| + |
| +func fsPlace(t *testing.T, fx *consoleFixture, token, target string) (int, string) { |
| + t.Helper() |
| + code, _, out := rawReq(t, http.MethodPost, placeURL(fx, "shop"), token, `{"target":"`+target+`"}`) |
| + return code, out |
| +} |
| + |
| +func fsWant(t *testing.T, what string, code int, out string, wantCode int, want string) { |
| + t.Helper() |
| + if code != wantCode || (want != "" && errorOf(t, out) != want) { |
| + t.Fatalf("%s = %d %s; want %d %q", what, code, out, wantCode, want) |
| + } |
| +} |
| + |
| +// TestFleetSizeFloorEveryReader: one box of the fleet below the floor, by |
| +// its release or by its rollback binary, refuses a fleet size at the |
| +// policy, create, copies and placement doors with PS-2-C04; the same fleet |
| +// all at the floor takes it; xlarge is untouched by the floor. |
| +func TestFleetSizeFloorEveryReader(t *testing.T) { |
| + belows := map[string]func(t *testing.T, fx *consoleFixture, fleet, box string){ |
| + "release": func(t *testing.T, fx *consoleFixture, fleet, box string) { |
| + fsBeat(t, fx, fleet, box, 32768, fsBelow, "none", "") |
| + }, |
| + "prev_release": func(t *testing.T, fx *consoleFixture, fleet, box string) { |
| + fsBeat(t, fx, fleet, box, 32768, release.FleetSizesFloor, fsBelow, "") |
| + }, |
| + } |
| + for name, below := range belows { |
| + t.Run("policy "+name, func(t *testing.T) { |
| + fx, token, _ := fleetBoxWorld(t, "code-fsf-pol-"+name, "pi-1", "pi-2") |
| + fsAtFloor(t, fx, "pi-1") |
| + below(t, fx, "pi", "pi-2") |
| + putFleetRecord(t, fx, "shop", "a.example.com", func(r *provision.Record) { r.FleetBox = "pi-1" }) |
| + code, out := fsPolicyPut(t, fx, token, `{"default_size":"12g"}`) |
| + fsWant(t, "12g with pi-2 below", code, out, http.StatusConflict, fsC04) |
| + fsAtFloor(t, fx, "pi-2") |
| + code, out = fsPolicyPut(t, fx, token, `{"default_size":"12g"}`) |
| + fsWant(t, "12g all at the floor", code, out, http.StatusOK, "") |
| + }) |
| + t.Run("create "+name, func(t *testing.T) { |
| + fx, token, _ := fleetBoxWorld(t, "code-fsf-new-"+name, "pi-1", "pi-2") |
| + fsAtFloor(t, fx, "pi-1") |
| + below(t, fx, "pi", "pi-2") |
| + putShopPolicy(t, fx, apppolicy.Policy{DefaultSize: "12g"}) |
| + code, out := createShop(t, fx, token, `{"host":"c.example.com"}`) |
| + fsWant(t, "create with pi-2 below", code, out, http.StatusConflict, fsC04) |
| + fsAtFloor(t, fx, "pi-2") |
| + createShopAt(t, fx, token, "c.example.com") |
| + }) |
| + t.Run("copies "+name, func(t *testing.T) { |
| + fx, token := copiesWorld(t) |
| + fsAtFloor(t, fx, "pi-1") |
| + below(t, fx, "pi", "pi-2") |
| + putShopPolicy(t, fx, apppolicy.Policy{DefaultSize: "12g"}) |
| + code, out := copiesPut(t, fx, token, `{"copies":2}`) |
| + fsWant(t, "copies 2 with pi-2 below", code, out, http.StatusConflict, fsC04) |
| + fsAtFloor(t, fx, "pi-2") |
| + code, out = copiesPut(t, fx, token, `{"copies":2}`) |
| + fsWant(t, "copies 2 all at the floor", code, out, http.StatusOK, "") |
| + }) |
| + t.Run("place "+name, func(t *testing.T) { |
| + fx, token, _ := fleetBoxWorld(t, "code-fsf-place-"+name, "pi-1") |
| + fsAtFloor(t, fx, "pi-1") |
| + sizeFleet(t, fx, "big", fx.sqid, "bg-1", "bg-2") |
| + fsBeat(t, fx, "big", "bg-1", 32768, release.FleetSizesFloor, "none", "") |
| + below(t, fx, "big", "bg-2") |
| + putFleetRecord(t, fx, "shop", "a.example.com", nil) |
| + putShopPolicy(t, fx, apppolicy.Policy{DefaultSize: "12g"}) |
| + code, out := fsPlace(t, fx, token, "fleet/big") |
| + fsWant(t, "place onto big with bg-2 below", code, out, http.StatusConflict, fsC04) |
| + if r := shopRec(t, fx, "a.example.com"); r.Region != "fleet/pi" { |
| + t.Fatalf("a refused move left the record at %q", r.Region) |
| + } |
| + fsBeat(t, fx, "big", "bg-2", 32768, release.FleetSizesFloor, "none", "") |
| + code, out = fsPlace(t, fx, token, "fleet/big") |
| + fsWant(t, "place onto big all at the floor", code, out, http.StatusOK, "") |
| + }) |
| + } |
| + // R16's amendment, mirrored: a prev below the floor is harmless once |
| + // the box's last apply finished. |
| + t.Run("prev below, apply finished", func(t *testing.T) { |
| + fx, token, _ := fleetBoxWorld(t, "code-fsf-applied", "pi-1") |
| + fsAtFloor(t, fx, "pi-1") |
| + total := int64(32768) << 20 |
| + b := status.Box{Label: "pi-1", GeneratedAt: time.Now(), Metrics: &status.BoxMetrics{Layout: &status.LayoutReport{Mode: "nested"}, |
| + RootfsReady: true, Release: release.FleetSizesFloor, PrevRelease: fsBelow, ApplyFinished: true, MemTotalBytes: &total}} |
| + if err := status.WriteBox(context.Background(), release.NewPrefixStore(fx.rel, "fleets/pi/"), b); err != nil { |
| + t.Fatal(err) |
| + } |
| + code, out := fsPolicyPut(t, fx, token, `{"default_size":"12g"}`) |
| + fsWant(t, "12g, prev below with its apply finished", code, out, http.StatusOK, "") |
| + }) |
| + // Existing sizes are unaffected: xlarge with a box and the console |
| + // below the floor goes through every door as today. |
| + t.Run("xlarge unaffected", func(t *testing.T) { |
| + fx, token := copiesWorld(t) |
| + fsAtFloor(t, fx, "pi-1") |
| + fsBeat(t, fx, "pi", "pi-2", 32768, fsBelow, fsBelow, "") |
| + fx.srv.consoleBuild = fsBelow |
| + code, out := fsPolicyPut(t, fx, token, `{"default_size":"xlarge"}`) |
| + fsWant(t, "xlarge below the floor", code, out, http.StatusOK, "") |
| + code, out = copiesPut(t, fx, token, `{"copies":2}`) |
| + fsWant(t, "copies 2 at xlarge below the floor", code, out, http.StatusOK, "") |
| + }) |
| +} |
| + |
| +// TestFleetSizeFloorOtherInstance: every reader of the app's policy |
| +// counts, so A's override is refused while B's box is below the floor. |
| +func TestFleetSizeFloorOtherInstance(t *testing.T) { |
| + fx, token, _ := fleetBoxWorld(t, "code-fsf-other", "pi-1", "pi-2") |
| + fsAtFloor(t, fx, "pi-1") |
| + fsBeat(t, fx, "pi", "pi-2", 32768, fsBelow, "none", "") |
| + 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" }) |
| + code, out := fsPolicyPut(t, fx, token, `{"size":{"host":"a.example.com","value":"4g"}}`) |
| + fsWant(t, "4g on A with B's box below", code, out, http.StatusConflict, fsC04) |
| +} |
| + |
| +// TestFleetSizeFloorRelabel: A at 4g and B at xlarge; a 32 GiB box below |
| +// the floor joins the roster and holds nothing. B, relabelled off a lost |
| +// box, goes to pi-1 and never to the new box. |
| +func TestFleetSizeFloorRelabel(t *testing.T) { |
| + fx, _, reg := fleetBoxWorld(t, "code-fsf-relabel", "pi-1", "pi-2") |
| + registerFleetBox(t, reg, "pi", "pi-3") |
| + fsAtFloor(t, fx, "pi-1", "pi-2") |
| + fsBeat(t, fx, "pi", "pi-3", 32768, fsBelow, "none", "") |
| + 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" }) |
| + a := shopRec(t, fx, "a.example.com") |
| + putShopPolicy(t, fx, apppolicy.Policy{DefaultSize: "xlarge", Sizes: map[string]apppolicy.SizeOverride{a.Host: {Size: "4g", CreatedAt: a.CreatedAt}}}) |
| + 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 pi-3 below the floor", b.FleetBox) |
| + } |
| +} |
| + |
| +// TestFleetSizeFloorConsoleBuild: a console below the floor, or one whose |
| +// build is not a release, refuses a fleet size. |
| +func TestFleetSizeFloorConsoleBuild(t *testing.T) { |
| + for _, build := range []string{fsBelow, "ac9ee36"} { |
| + t.Run(build, func(t *testing.T) { |
| + fx, token, _ := fleetBoxWorld(t, "code-fsf-console-"+build, "pi-1") |
| + fsAtFloor(t, fx, "pi-1") |
| + putFleetRecord(t, fx, "shop", "a.example.com", nil) |
| + fx.srv.consoleBuild = build |
| + code, out := fsPolicyPut(t, fx, token, `{"default_size":"4g"}`) |
| + fsWant(t, "4g on a console at "+build, code, out, http.StatusConflict, fsC04) |
| + putShopPolicy(t, fx, apppolicy.Policy{DefaultSize: "4g"}) |
| + code, out = createShop(t, fx, token, `{"host":"c.example.com"}`) |
| + fsWant(t, "create on a console at "+build, code, out, http.StatusConflict, fsC04) |
| + }) |
| + } |
| +} |
| + |
| +// fsBreakPointer makes channel's router pointer unreadable. |
| +func fsBreakPointer(t *testing.T, fx *consoleFixture, channel string) { |
| + t.Helper() |
| + rfPublish(t, fx, channel, release.FleetSizesFloor) |
| + key := release.SystemPointerKey("router", channel) |
| + _, tag, err := fx.rel.Get(context.Background(), key) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if _, err := fx.rel.Put(context.Background(), key, []byte("broken"), tag); err != nil { |
| + t.Fatal(err) |
| + } |
| +} |
| + |
| +// TestFleetSizeFloorUnreadable: a followed channel whose pointer cannot be |
| +// read refuses: the size doors with sizeUnreadMsg, the copies door with |
| +// copiesUnread, as it does today. A followed pointer below the floor |
| +// refuses with PS-2-C04; one no box follows is not read. |
| +func TestFleetSizeFloorUnreadable(t *testing.T) { |
| + t.Run("policy", func(t *testing.T) { |
| + fx, token, _ := fleetBoxWorld(t, "code-fsf-unread", "pi-1") |
| + fsAtFloor(t, fx, "pi-1") |
| + putFleetRecord(t, fx, "shop", "a.example.com", nil) |
| + fsBreakPointer(t, fx, "stable") |
| + code, out := fsPolicyPut(t, fx, token, `{"default_size":"4g"}`) |
| + fsWant(t, "4g with stable unreadable", code, out, http.StatusServiceUnavailable, fsUnread) |
| + }) |
| + t.Run("policy below", func(t *testing.T) { |
| + fx, token, _ := fleetBoxWorld(t, "code-fsf-chbelow", "pi-1") |
| + fsAtFloor(t, fx, "pi-1") |
| + putFleetRecord(t, fx, "shop", "a.example.com", nil) |
| + rfPublish(t, fx, "canary", fsBelow) |
| + code, h, out := policyReq(t, fx, http.MethodPut, "shop", token, `{"default_size":"4g"}`, policyCreate) |
| + fsWant(t, "4g with canary below and followed by nobody", code, out, http.StatusOK, "") |
| + fsBeat(t, fx, "pi", "pi-1", 32768, release.FleetSizesFloor, "none", "canary") |
| + code, _, out = policyReq(t, fx, http.MethodPut, "shop", token, `{"default_size":"6g"}`, policyIfMatch(h.Get("ETag"))) |
| + fsWant(t, "6g with pi-1 on canary below", code, out, http.StatusConflict, fsC04) |
| + }) |
| + t.Run("copies", func(t *testing.T) { |
| + fx, token := copiesWorld(t) |
| + fsAtFloor(t, fx, "pi-1", "pi-2") |
| + putShopPolicy(t, fx, apppolicy.Policy{DefaultSize: "12g"}) |
| + fsBreakPointer(t, fx, "stable") |
| + code, out := copiesPut(t, fx, token, `{"copies":2}`) |
| + fsWant(t, "copies 2 at 12g with stable unreadable", code, out, http.StatusServiceUnavailable, copiesUnread) |
| + }) |
| + t.Run("place", func(t *testing.T) { |
| + fx, token, _ := fleetBoxWorld(t, "code-fsf-unread-place", "pi-1") |
| + fsAtFloor(t, fx, "pi-1") |
| + sizeFleet(t, fx, "big", fx.sqid, "bg-1") |
| + fsBeat(t, fx, "big", "bg-1", 32768, release.FleetSizesFloor, "none", "canary") |
| + putFleetRecord(t, fx, "shop", "a.example.com", nil) |
| + putShopPolicy(t, fx, apppolicy.Policy{DefaultSize: "12g"}) |
| + fsBreakPointer(t, fx, "canary") |
| + code, out := fsPlace(t, fx, token, "fleet/big") |
| + fsWant(t, "place onto big with canary unreadable", code, out, http.StatusServiceUnavailable, fsUnread) |
| + }) |
| +} |
| + |
| +// TestFleetSizeFloorPlaceChannel: placement onto another fleet whose box |
| +// follows a channel already below the floor is refused for an app with a |
| +// fleet size, and allowed for one without (coordinator ruling, Task 3). |
| +func TestFleetSizeFloorPlaceChannel(t *testing.T) { |
| + world := func(t *testing.T, code string, size string) (*consoleFixture, string) { |
| + fx, token, _ := fleetBoxWorld(t, code, "pi-1") |
| + fsAtFloor(t, fx, "pi-1") |
| + sizeFleet(t, fx, "big", fx.sqid, "bg-1") |
| + fsBeat(t, fx, "big", "bg-1", 32768, release.FleetSizesFloor, "none", "canary") |
| + rfPublish(t, fx, "canary", fsBelow) |
| + putFleetRecord(t, fx, "shop", "a.example.com", nil) |
| + putShopPolicy(t, fx, apppolicy.Policy{DefaultSize: size}) |
| + return fx, token |
| + } |
| + t.Run("12g", func(t *testing.T) { |
| + fx, token := world(t, "code-fsf-pch-12g", "12g") |
| + code, out := fsPlace(t, fx, token, "fleet/big") |
| + fsWant(t, "place 12g onto big following canary below", code, out, http.StatusConflict, fsC04) |
| + if r := shopRec(t, fx, "a.example.com"); r.Region != "fleet/pi" { |
| + t.Fatalf("a refused move left the record at %q", r.Region) |
| + } |
| + rfPublish(t, fx, "canary", release.FleetSizesFloor) |
| + code, out = fsPlace(t, fx, token, "fleet/big") |
| + fsWant(t, "place 12g onto big once canary is at the floor", code, out, http.StatusOK, "") |
| + }) |
| + t.Run("xlarge", func(t *testing.T) { |
| + fx, token := world(t, "code-fsf-pch-xl", "xlarge") |
| + code, out := fsPlace(t, fx, token, "fleet/big") |
| + fsWant(t, "place xlarge onto big following canary below", code, out, http.StatusOK, "") |
| + }) |
| +} |
| + |
| +// TestFleetSizeFloorJudge: a system publication below the floor to a |
| +// channel a box of a fleet whose app uses a fleet size (default or |
| +// override) follows is refused with PS-2-C15; with no fleet size in use, |
| +// or on a channel no such box follows, it is not (R6 bullet 2). |
| +func TestFleetSizeFloorJudge(t *testing.T) { |
| + world := func(t *testing.T, code string, p func(a provision.Record) apppolicy.Policy) *consoleFixture { |
| + fx, _, _ := fleetBoxWorld(t, code, "pi-1") |
| + fsAtFloor(t, fx, "pi-1") |
| + putFleetRecord(t, fx, "shop", "a.example.com", nil) |
| + putShopPolicy(t, fx, p(*shopRec(t, fx, "a.example.com"))) |
| + return fx |
| + } |
| + publish := func(fx *consoleFixture, channel, v string) error { |
| + _, err := fx.srv.promoteHostUpdate(context.Background(), fcRelease(v), "router", channel, release.SystemPromoteOptions{}) |
| + return err |
| + } |
| + for name, p := range map[string]func(a provision.Record) apppolicy.Policy{ |
| + "default": func(provision.Record) apppolicy.Policy { return apppolicy.Policy{DefaultSize: "8g"} }, |
| + "override": func(a provision.Record) apppolicy.Policy { |
| + return apppolicy.Policy{Sizes: map[string]apppolicy.SizeOverride{a.Host: {Size: "8g", CreatedAt: a.CreatedAt}}} |
| + }, |
| + "stale": func(provision.Record) apppolicy.Policy { |
| + return apppolicy.Policy{Sizes: map[string]apppolicy.SizeOverride{"gone.example.com": {Size: "8g", CreatedAt: time.Unix(1, 0)}}} |
| + }, |
| + } { |
| + t.Run(name, func(t *testing.T) { |
| + fx := world(t, "code-fsf-judge-"+name, p) |
| + err := publish(fx, "stable", fsBelow) |
| + if !errors.Is(err, errReplicaFloorPromotion) || err.Error() != fsC15 { |
| + t.Fatalf("publish %s to stable with a fleet size in use = %v; want PS-2-C15", fsBelow, err) |
| + } |
| + if v := rfPointer(t, fx, "stable"); v != "" { |
| + t.Fatalf("stable = %q after a refused publication", v) |
| + } |
| + if err := publish(fx, "canary", fsBelow); err != nil { |
| + t.Fatalf("publish to canary, which no box follows = %v", err) |
| + } |
| + if err := publish(fx, "stable", release.FleetSizesFloor); err != nil { |
| + t.Fatalf("publish the floor = %v", err) |
| + } |
| + }) |
| + } |
| + t.Run("no fleet size", func(t *testing.T) { |
| + fx := world(t, "code-fsf-judge-none", func(provision.Record) apppolicy.Policy { return apppolicy.Policy{DefaultSize: "xlarge"} }) |
| + if err := publish(fx, "stable", fsBelow); err != nil { |
| + t.Fatalf("publish %s with no fleet size = %v", fsBelow, err) |
| + } |
| + }) |
| +} |
| + |
| +// TestFleetSizeFloorBothOrders (R11): the policy door and a publication |
| +// race under replicaFloorMu. Whichever takes it first, the run never ends |
| +// with a fleet size in the policy and a below-floor pointer on a channel |
| +// the app's box follows. |
| +func TestFleetSizeFloorBothOrders(t *testing.T) { |
| + setup := func(t *testing.T, code string) (*consoleFixture, string) { |
| + fx, token, _ := fleetBoxWorld(t, code, "pi-1") |
| + fsAtFloor(t, fx, "pi-1") |
| + putFleetRecord(t, fx, "shop", "a.example.com", nil) |
| + return fx, token |
| + } |
| + publish := func(fx *consoleFixture) error { |
| + _, err := fx.srv.promoteHostUpdate(context.Background(), fcRelease(fsBelow), "router", "stable", release.SystemPromoteOptions{}) |
| + return err |
| + } |
| + policySize := func(t *testing.T, fx *consoleFixture) string { |
| + p, _, err := apppolicy.Get(context.Background(), fx.rel, fx.pub, fx.sqid, "shop", time.Now()) |
| + if err != nil { |
| + return "" |
| + } |
| + return p.DefaultSize |
| + } |
| + |
| + t.Run("policy first", func(t *testing.T) { |
| + fx, token := setup(t, "code-fsf-race-policy") |
| + s := fx.srv |
| + ran := false |
| + pubDone := make(chan error, 1) |
| + s.replicaFloorHooks.sizeLocked = func() { |
| + if ran { |
| + return |
| + } |
| + ran = true |
| + if !fpHeld(&s.replicaFloorMu) { |
| + t.Error("sizeLocked: replicaFloorMu is not held") |
| + } |
| + // The size door holds replicaFloorMu, its barrier passed: the |
| + // publication reaches the mutex and waits for the policy. |
| + reached := make(chan struct{}) |
| + s.replicaFloorHooks.beforeLock = func() { close(reached) } |
| + go func() { pubDone <- publish(fx) }() |
| + <-reached |
| + } |
| + code, out := fsPolicyPut(t, fx, token, `{"default_size":"12g"}`) |
| + fsWant(t, "12g", code, out, http.StatusOK, "") |
| + if !ran { |
| + t.Fatal("the size door never took replicaFloorMu") |
| + } |
| + if err := <-pubDone; !errors.Is(err, errReplicaFloorPromotion) || err.Error() != fsC15 { |
| + t.Fatalf("publication after the 12g policy = %v; want PS-2-C15", err) |
| + } |
| + if v := rfPointer(t, fx, "stable"); v != "" || policySize(t, fx) != "12g" { |
| + t.Fatalf("pointer %q, size %q; want none and 12g", v, policySize(t, fx)) |
| + } |
| + }) |
| + t.Run("publication first", func(t *testing.T) { |
| + fx, token := setup(t, "code-fsf-race-pub") |
| + s := fx.srv |
| + ran := false |
| + type result struct { |
| + code int |
| + out string |
| + } |
| + putDone := make(chan result, 1) |
| + s.replicaFloorHooks.locked = func() { |
| + if ran { |
| + return |
| + } |
| + ran = true |
| + // The publication holds replicaFloorMu, judged with no fleet |
| + // size: the size door reaches the mutex and waits. |
| + reached := make(chan struct{}) |
| + s.replicaFloorHooks.sizeBeforeLock = func() { |
| + if fpHeld(&s.regionalMu) { |
| + t.Error("sizeBeforeLock: regionalMu held") |
| + } |
| + close(reached) |
| + } |
| + go func() { |
| + code, out := fsPolicyPut(t, fx, token, `{"default_size":"12g"}`) |
| + putDone <- result{code, out} |
| + }() |
| + <-reached |
| + } |
| + if err := publish(fx); err != nil { |
| + t.Fatalf("publication with no fleet size = %v", err) |
| + } |
| + if !ran { |
| + t.Fatal("the publication's locked hook never ran") |
| + } |
| + r := <-putDone |
| + fsWant(t, "12g after the publication", r.code, r.out, http.StatusConflict, fsC04) |
| + if v := rfPointer(t, fx, "stable"); v != fsBelow || policySize(t, fx) != "" { |
| + t.Fatalf("pointer %q, size %q; want %s and none", v, policySize(t, fx), fsBelow) |
| + } |
| + }) |
| +} |
diff --git a/internal/console/policysize.go b/internal/console/policysize.go| index b1da4d2cf..fe07cbd48 100644 |
| --- a/internal/console/policysize.go |
| +++ b/internal/console/policysize.go |
| @@ -52,6 +52,9 @@ type sizeBox struct { |
| TotalMiB int64 |
| fleet string |
| report *status.Box |
| + // atFleetFloor: the box's heartbeat says it can read a fleet size |
| + // (fleetSizesBoxAtFloor; #892 S2 P3). |
| + atFleetFloor bool |
| } |
| // sizeCommittedBox is one box's committed figure for the door's reply: |
| @@ -168,6 +171,7 @@ func (s *Server) sizeBoxes(ctx context.Context, acct *Account, recs []provision. |
| sb := sizeBox{Box: fb.Label, fleet: f.Name, report: fleetReports[fb.Label]} |
| if sb.report != nil { |
| sb.Region = sb.report.Region |
| + sb.atFleetFloor = fleetSizesBoxAtFloor(sb.report.Metrics) |
| } |
| boxes = append(boxes, sb) |
| } |
| @@ -247,7 +251,7 @@ func (s *Server) sizePlatformBoxes(ctx context.Context, recs []provision.Record, |
| } |
| } |
| seen[b.Label] = true |
| - boxes = append(boxes, sizeBox{Box: b.Label, Region: b.Region, TotalMiB: f.total, report: &reports[i]}) |
| + boxes = append(boxes, sizeBox{Box: b.Label, Region: b.Region, TotalMiB: f.total, report: &reports[i], atFleetFloor: fleetSizesBoxAtFloor(b.Metrics)}) |
| } |
| return boxes, complete, nil |
| } |
| @@ -329,7 +333,10 @@ func (s *Server) sizeCapacityCheck(ctx context.Context, acct *Account, recs []pr |
| complete = false |
| } |
| if cgroup.FleetSize(size.Name) { |
| - return boxes, fleetSizeRefusal(boxes, complete, fleetOnly, size) |
| + if ref := fleetSizeRefusal(boxes, complete, fleetOnly, size); ref != nil { |
| + return boxes, ref |
| + } |
| + return boxes, s.fleetSizesFloorRefusal(boxes) |
| } |
| return boxes, sizeRefusal(boxes, complete, size) |
| } |
| @@ -398,27 +405,13 @@ func sizeCommitted(boxes []sizeBox) []sizeCommittedBox { |
| // against every possible destination of every record of the app |
| // (fleetSizeRefusal), and it answers PS-2-C03 warnings (fleetSizeWarnings, |
| // P8), never a refusal. |
| +// |
| +// The fleet-size branch also passes the FleetSizesFloor barrier |
| +// (replicaPolicyBarrier). Its caller holds replicaFloorMu (lockFleetSizes) |
| +// whenever policyChecksFleetSize says a fleet size is checked, from here |
| +// through the publication. |
| 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 |
| - } |
| - var checks []check |
| - if req.DefaultSize != nil && next.DefaultSize != "" && next.DefaultSize != old.DefaultSize { |
| - if sz, ok := cgroup.SizeByName(next.DefaultSize); ok { |
| - checks = append(checks, check{size: sz}) |
| - } |
| - } |
| - if req.Size != nil && req.Size.Value != policySizeValueDefault { |
| - host := strings.ToLower(req.Size.Host) |
| - o, had := old.Sizes[host] |
| - n, has := next.Sizes[host] |
| - if has && (!had || o.Size != n.Size || !o.CreatedAt.Equal(n.CreatedAt)) { |
| - if sz, ok := cgroup.SizeByName(n.Size); ok { |
| - checks = append(checks, check{size: sz, host: host}) |
| - } |
| - } |
| - } |
| + checks := policySizeChecks(old, next, req) |
| if len(checks) == 0 { |
| return nil, nil, nil |
| } |
| @@ -434,7 +427,7 @@ func (s *Server) policySizeCheck(ctx context.Context, acct *Account, app string, |
| fleetChecked = true |
| // The whole app, at its largest fleet size, which is at least |
| // this one. |
| - c = check{size: c.size} |
| + c = policySizeCheckItem{size: c.size} |
| if fs, ok := appFleetSize(next); ok { |
| c.size = fs |
| } |
| @@ -476,6 +469,12 @@ func (s *Server) policySizeCheck(ctx context.Context, acct *Account, app string, |
| return nil, nil, ref |
| } |
| if cgroup.FleetSize(c.size.Name) { |
| + if ref := s.replicaPolicyBarrier(ctx, acct, app, release.FleetSizesFloor); ref != nil { |
| + return nil, nil, ref |
| + } |
| + if hook := s.replicaFloorHooks.sizeLocked; hook != nil { |
| + hook() |
| + } |
| warnings = append(warnings, fleetSizeWarnings(boxes, recs, next)...) |
| } |
| for _, b := range boxes { |
| @@ -489,6 +488,48 @@ func (s *Server) policySizeCheck(ctx context.Context, acct *Account, app string, |
| return sizeCommitted(checked), warnings, nil |
| } |
| +// policySizeCheckItem is one size policySizeCheck judges: for host, or |
| +// for the whole app when host is "". |
| +type policySizeCheckItem struct { |
| + size cgroup.Size |
| + host string |
| +} |
| + |
| +// policySizeChecks is the sizes a PUT changes: a default_size set to a |
| +// size other than the stored one, and a size override set or replaced on |
| +// a host. Clearing a size, re-sending the one in force and an egress-only |
| +// change give none. |
| +func policySizeChecks(old, next apppolicy.Policy, req policyPutReq) []policySizeCheckItem { |
| + var checks []policySizeCheckItem |
| + if req.DefaultSize != nil && next.DefaultSize != "" && next.DefaultSize != old.DefaultSize { |
| + if sz, ok := cgroup.SizeByName(next.DefaultSize); ok { |
| + checks = append(checks, policySizeCheckItem{size: sz}) |
| + } |
| + } |
| + if req.Size != nil && req.Size.Value != policySizeValueDefault { |
| + host := strings.ToLower(req.Size.Host) |
| + o, had := old.Sizes[host] |
| + n, has := next.Sizes[host] |
| + if has && (!had || o.Size != n.Size || !o.CreatedAt.Equal(n.CreatedAt)) { |
| + if sz, ok := cgroup.SizeByName(n.Size); ok { |
| + checks = append(checks, policySizeCheckItem{size: sz, host: host}) |
| + } |
| + } |
| + } |
| + return checks |
| +} |
| + |
| +// policyChecksFleetSize: policySizeCheck will judge a fleet size for this |
| +// PUT, so its caller takes replicaFloorMu first (#892 S2 P3, R11). |
| +func policyChecksFleetSize(old, next apppolicy.Policy, req policyPutReq) bool { |
| + for _, c := range policySizeChecks(old, next, req) { |
| + if cgroup.FleetSize(c.size.Name) { |
| + return true |
| + } |
| + } |
| + return false |
| +} |
| + |
| // policySizeRecords is the records a size check covers: host's own, or |
| // every record of the app for host "". ok is false when they could not all |
| // be read (a failed read, too many, or one that does not decode). |
diff --git a/internal/console/replica_drill1_test.go b/internal/console/replica_drill1_test.go| index 86304e34c..671590148 100644 |
| --- a/internal/console/replica_drill1_test.go |
| +++ b/internal/console/replica_drill1_test.go |
| @@ -77,7 +77,7 @@ func smallBoxBeat(t *testing.T, fx *consoleFixture, box string) { |
| } |
| total := int64(1858) << 20 |
| hosts := map[string]status.HostRollout{host: {Account: fx.sqid, App: "shop", Serving: ver, Installed: ver, Awake: true}} |
| - b := status.Box{Label: box, GeneratedAt: time.Now(), Metrics: &status.BoxMetrics{Release: release.StatelessReplicasFloor, PrevRelease: "none", RootfsReady: true, MemTotalBytes: &total, Rollout: &status.RolloutReport{Hosts: hosts}}} |
| + b := status.Box{Label: box, GeneratedAt: time.Now(), Metrics: &status.BoxMetrics{Release: release.FleetSizesFloor, PrevRelease: "none", RootfsReady: true, MemTotalBytes: &total, Rollout: &status.RolloutReport{Hosts: hosts}}} |
| if err := status.WriteBox(context.Background(), release.NewPrefixStore(fx.rel, "fleets/pi/"), b); err != nil { |
| t.Fatal(err) |
| } |
| @@ -149,7 +149,7 @@ func sizedBeat(t *testing.T, fx *consoleFixture, box string, mib int64) { |
| hosts["b.example.com"] = status.HostRollout{Account: fx.sqid, App: "shop", Serving: "v2", Installed: "v2", Awake: true} |
| } |
| total := mib << 20 |
| - b := status.Box{Label: box, GeneratedAt: time.Now(), Metrics: &status.BoxMetrics{Release: release.StatelessReplicasFloor, PrevRelease: "none", RootfsReady: true, MemTotalBytes: &total, Rollout: &status.RolloutReport{Hosts: hosts}}} |
| + b := status.Box{Label: box, GeneratedAt: time.Now(), Metrics: &status.BoxMetrics{Release: release.FleetSizesFloor, PrevRelease: "none", RootfsReady: true, MemTotalBytes: &total, Rollout: &status.RolloutReport{Hosts: hosts}}} |
| if err := status.WriteBox(context.Background(), release.NewPrefixStore(fx.rel, "fleets/pi/"), b); err != nil { |
| t.Fatal(err) |
| } |
diff --git a/internal/console/replicafloor.go b/internal/console/replicafloor.go| index 8d9ae9747..5708735f3 100644 |
| --- a/internal/console/replicafloor.go |
| +++ b/internal/console/replicafloor.go |
| @@ -40,6 +40,16 @@ import ( |
| // replicaFloorMu, then regionalMu; the copies door takes replicaFloorMu |
| // after its app lock and takes neither of the others. |
| // |
| +// Fleet sizes (#892 S2 P3) share the mutex and the judge. A system |
| +// publication below release.FleetSizesFloor is refused (PS-2-C15) while an |
| +// app uses a fleet size on a fleet with a box that follows the channel. |
| +// The policy door (a fleet size changing) and placement (an app with a |
| +// fleet size moving to a fleet) take replicaFloorMu (lockFleetSizes) |
| +// after the app's name lock, and hold it from their FleetSizesFloor |
| +// barrier through their write; the copies door runs that barrier too, |
| +// under the mutex it already holds. Whole order: account, app (lockApp), |
| +// the app's name lock, fleetFloorMu, replicaFloorMu, regionalMu. |
| +// |
| // The box itself has no backstop (R16): a manual root downgrade on a box |
| // with copies is not prevented, and the docs say so. |
| @@ -50,6 +60,9 @@ const consoleReplicaFloorRefusal = "copies above 1 are in use, so the platform c |
| // floor and the floor. |
| const copiesChannelBelow = "this deployment is on %s in its %s channel, below %s. copies above 1 wait until every channel is on %s or later. try again after the next deployment update" |
| +// consoleFleetSizesFloorRefusal (PS-2-C15) takes the floor. |
| +const consoleFleetSizesFloorRefusal = "sizes from 4g up are in use, so the platform cannot move below %s. set those apps and instances to xlarge or smaller first" |
| + |
| // copiesNoBaseline (SR-4-C61; R13). |
| const copiesNoBaseline = "every instance needs one successful deploy of a stateless release before it can run more than one copy. deploy once, then set copies" |
| @@ -73,8 +86,11 @@ func (e replicaFloorError) Unwrap() error { return errReplicaFloorPromotion } |
| // after every earlier judgment and before replicaFloorMu is taken; locked |
| // runs under it after the judgment and before the publication; |
| // copiesBeforeLock runs in the copies door just before it takes the mutex. |
| +// sizeBeforeLock and sizeLocked are the fleet-size doors' pair (#892 S2): |
| +// just before they take the mutex, and under it after their barrier. |
| type replicaFloorHooks struct { |
| beforeLock, locked, copiesBeforeLock func() |
| + sizeBeforeLock, sizeLocked func() |
| } |
| // replicaFloorJudge refuses moving channel from cur (nil: a first |
| @@ -92,8 +108,13 @@ type replicaFloorHooks struct { |
| // Every read here bypasses the request's write memo (withoutReqMemo, |
| // #511 final wave item 6): the judgment under replicaFloorMu binds, so it |
| // must see a copies config written after the early judgment read it. |
| +// |
| +// The fleet-sizes floor (#892 S2 P3, R6 bullet 2) is judged here too, the |
| +// same way: a target below release.FleetSizesFloor is refused (PS-2-C15) |
| +// while an app uses a fleet size (fleetSizeAppsInUse) on a fleet with a |
| +// box that follows channel. |
| func (s *Server) replicaFloorJudge(ctx context.Context, channel string, cur *release.SystemPointer, target string) error { |
| - if atReplicaFloor(target) || (cur != nil && cur.Current.Version == target) { |
| + if (atReplicaFloor(target) && atFleetSizesFloor(target)) || (cur != nil && cur.Current.Version == target) { |
| return nil |
| } |
| _, fleets, err := s.systemFleets() |
| @@ -104,24 +125,46 @@ func (s *Server) replicaFloorJudge(ctx context.Context, channel string, cur *rel |
| return nil |
| } |
| ctx = withoutReqMemo(ctx) |
| - apps, err := s.replicaAppsInUse(ctx, fleets) |
| - if err != nil { |
| - return fmt.Errorf("stateless replicas floor: cannot read the copy inventory: %w", err) |
| - } |
| followed := map[string]map[string]bool{} |
| - for _, app := range apps { |
| - for _, fleet := range app.fleets { |
| - chs, ok := followed[fleet] |
| - if !ok { |
| - if chs, err = s.replicaFleetChannels(ctx, fleet); err != nil { |
| - return fmt.Errorf("stateless replicas floor: cannot read the system channels of fleet %s: %w", fleet, err) |
| + // follows reports whether a box of one of apps' fleets follows channel. |
| + follows := func(apps []replicaInUse, what string) (bool, error) { |
| + for _, app := range apps { |
| + for _, fleet := range app.fleets { |
| + chs, ok := followed[fleet] |
| + if !ok { |
| + if chs, err = s.replicaFleetChannels(ctx, fleet); err != nil { |
| + return false, fmt.Errorf("%s: cannot read the system channels of fleet %s: %w", what, fleet, err) |
| + } |
| + followed[fleet] = chs |
| + } |
| + if chs[channel] { |
| + return true, nil |
| } |
| - followed[fleet] = chs |
| - } |
| - if chs[channel] { |
| - return replicaFloorError{msg: fmt.Sprintf(consoleReplicaFloorRefusal, release.StatelessReplicasFloor)} |
| } |
| } |
| + return false, nil |
| + } |
| + if !atReplicaFloor(target) { |
| + apps, err := s.replicaAppsInUse(ctx, fleets) |
| + if err != nil { |
| + return fmt.Errorf("stateless replicas floor: cannot read the copy inventory: %w", err) |
| + } |
| + if hit, err := follows(apps, "stateless replicas floor"); err != nil { |
| + return err |
| + } else if hit { |
| + return replicaFloorError{msg: fmt.Sprintf(consoleReplicaFloorRefusal, release.StatelessReplicasFloor)} |
| + } |
| + } |
| + if !atFleetSizesFloor(target) { |
| + apps, err := s.fleetSizeAppsInUse(ctx, fleets) |
| + if err != nil { |
| + return fmt.Errorf("fleet sizes floor: cannot read the size inventory: %w", err) |
| + } |
| + if hit, err := follows(apps, "fleet sizes floor"); err != nil { |
| + return err |
| + } else if hit { |
| + return replicaFloorError{msg: fmt.Sprintf(consoleFleetSizesFloorRefusal, release.FleetSizesFloor)} |
| + } |
| } |
| return nil |
| } |
| @@ -323,6 +366,17 @@ func (s *Server) replicaFloorPublish(ctx context.Context, channel string, curren |
| return publish() |
| } |
| +// lockFleetSizes takes replicaFloorMu for a fleet-size door (the policy |
| +// door and placement, #892 S2), after the app's name lock, and returns its |
| +// release. |
| +func (s *Server) lockFleetSizes() func() { |
| + if hook := s.replicaFloorHooks.sizeBeforeLock; hook != nil { |
| + hook() |
| + } |
| + s.replicaFloorMu.Lock() |
| + return s.replicaFloorMu.Unlock |
| +} |
| + |
| // lockReplicaFloor takes replicaFloorMu for the copies door, after its |
| // app lock, and returns its release. |
| func (s *Server) lockReplicaFloor() func() { |
| @@ -346,7 +400,15 @@ func (s *Server) lockReplicaFloor() func() { |
| // is judged under the same mutex with these copies visible. A channel no |
| // box of the fleet follows is never read: it can neither satisfy nor |
| // block the barrier. |
| -func (s *Server) replicaPolicyBarrier(ctx context.Context, acct *Account, app string) *doorRefusal { |
| +// |
| +// floor is the release the channels must be at: StatelessReplicasFloor |
| +// for the copies door, as above; FleetSizesFloor for a fleet size (#892 |
| +// S2 P3), whose boxes are every possible destination of every record of |
| +// the app (fleetSizesPolicyBarrier). |
| +func (s *Server) replicaPolicyBarrier(ctx context.Context, acct *Account, app string, floor string) *doorRefusal { |
| + if floor == release.FleetSizesFloor { |
| + return s.fleetSizesPolicyBarrier(ctx, acct, app) |
| + } |
| unread := &doorRefusal{Status: http.StatusServiceUnavailable, Msg: copiesUnread} |
| if s.store == nil { |
| return unread |
| @@ -378,8 +440,7 @@ func (s *Server) replicaPolicyBarrier(ctx context.Context, acct *Account, app st |
| log.Printf("console: copies barrier: system pointer %s: %v", ch, err) |
| return unread |
| } |
| - if !atReplicaFloor(p.Current.Version) { |
| - floor := release.StatelessReplicasFloor |
| + if !atReleaseFloor(p.Current.Version, floor) { |
| return &doorRefusal{Status: http.StatusConflict, Msg: fmt.Sprintf(copiesChannelBelow, p.Current.Version, ch, floor, floor)} |
| } |
| } |
diff --git a/internal/provision/testdata/plan14-root-reads-stateless-scale-2-fleet-sizes.tsv b/internal/provision/testdata/plan14-root-reads-stateless-scale-2-fleet-sizes.tsv| index 9523b5abf..cf411c27e 100644 |
| --- a/internal/provision/testdata/plan14-root-reads-stateless-scale-2-fleet-sizes.tsv |
| +++ b/internal/provision/testdata/plan14-root-reads-stateless-scale-2-fleet-sizes.tsv |
| @@ -1,5 +1,5 @@ |
| # Census overlay stateless-scale-2-fleet-sizes, written by census-propose (#369). census-ruling-stateless-scale-2-fleet-sizes, approved on #280 before the merge; the approval card is docs/superpowers/notes/census-proposal-stateless-scale-2-fleet-sizes.md. |
| -# 5 body, 0 sites, 4 envelope: each changed unit re-frozen at its existing disposition with its claim appended. |
| +# 5 body, 0 sites, 5 envelope: each changed unit re-frozen at its existing disposition with its claim appended. |
| # 1 new dispositions: 0 reads re-frozen at their disposition with the claim, 1 new units frozen accepted-legacy, 0 quiet units retired while they read nothing, 0 other. |
| @legacy 0fa85d969c48493b2c14c3600ab8508c25d2eb2267ee86b70c592c3c602adbb1 internal/cgroup/cgroup.go setup legacy accepted-legacy accepted-legacy;rev3=J4;basis=root-input;rev3=census-ruling-hunt-account-root-hardening;basis=setup records whether the flat IMDS fence is armed (CARLOS_IMDS_FLAT_FENCE); when armed, a flat start whose shim or placement is unavailable is refused instead of falling back to start-and-move; no read is added;rev3=census-ruling-stateless-scale-2-fleet-sizes;basis=the agent (carlos-edge.service, user carlos), not root: setup now also records a fleet-size gate on the agent's own Manager when CARLOS_FLEET is set in its environment (os.Getenv, beside the existing CARLOS_IMDS_FLAT_FENCE read). The gate's MemTotal closure reads /proc/meminfo through ReadMemTotal when a strict prepare is asked for a fleet size (#892 S2). It only lets the strict validators (PrepareStrict, PrepareGen, SetParentLimit) accept the new fleet rows within FleetMemoryBudget; without CARLOS_FLEET they take small..xlarge and today's parents exactly as before. Reads added: env CARLOS_FLEET; /proc/meminfo (kernel, fixed path) via the closure file:ReadFile@73 1c2d963c219576573ba11b9b1ba07c3823902b850615a93406f6cfc3c78065cb |
| @legacy bce72a2f498322b60434183b6db1c4703d8b691246083a08052e29757c6ea259 internal/instancectl/gen.go Server.handleParent legacy accepted-legacy accepted-legacy;rev3=J2,J4;basis=root-input;rev3=census-ruling-instance-egress-sizes-328-329;basis=#329 plan 3 Task 5: holds the host's slice lock (lockSliceWrites) around its one set-property, so /parent and /size never interleave on one slice. Its one site is still r.URL.Query() of the agent's request (counted as file:Query). The checks in this body are unchanged, though cgroup.ValidParentLimit, which it calls, now accepts one size or the sum of any two from the four-row table (up to 4 GiB, was 2 GiB); no read added.;rev3=census-ruling-agent-start-demo-1;basis=Reads fixed DemoBox and clamps the existing parent memory writer to small.;rev3=census-ruling-gvisor-demos;basis=Validate logical parent input as before, require the demo route in gVisor mode and apply Physical once to the small demo parent. Existing request query read unchanged; shares stay logical.;rev3=census-ruling-stateless-scale-2-fleet-sizes;basis=root: after the unchanged ValidParentLimit check, a parent outside today's shared set (one or two of small..xlarge) must also pass Server.Fleet.ParentAllowed: CARLOS_FLEET in root's host.env, not a gVisor box, readable MemTotal, and the whole overlap within max(0, MemTotal - max(1 GiB, MemTotal/8)), else 400 before any write. Today's parent set is accepted unchanged on every box. Same one site (r.URL.Query of the agent's request); no read added in this body (MemTotal is read by hostMemTotal through the gate) file:Query@78 2825f7708356c107eb3960e76865ce24eac36e23529374f1a4e78fb4e46b45df |
| @@ -16,3 +16,4 @@ internal/cgroup/fleetgate.go ReadMemTotal legacy accepted-legacy accepted-legacy |
| @coverage internal/cgroup 18eb14bb51403da80ca35f8046278f79557a890d21527ec868a16b80d508e54f |
| @coverage internal/host 3a3bc8cc219234ee5669caa1b041ef7596655cbd4ae6a2397ca9a55ecc15c353 |
| @coverage internal/instancectl 0dff99b0e92f914797c9c1004c9ad1af71e8704661a5dc14d0f6994c10f90f92 |
| +@coverage internal/release 97eb61c0652f9fe333f21f77359133ef4e0d9bbba777418ca67f8fab6799ad75 |
diff --git a/internal/release/layoutintent.go b/internal/release/layoutintent.go| index baa1d3f71..e908b20e0 100644 |
| --- a/internal/release/layoutintent.go |
| +++ b/internal/release/layoutintent.go |
| @@ -427,3 +427,9 @@ func RecordLayoutIntent(ctx context.Context, s Store, mode, box string, force bo |
| // Reserved first releases for stateless runtime and fleet replicas. |
| const StatelessRuntimeFloor = "v0.34.0" |
| const StatelessReplicasFloor = "v0.35.0" |
| + |
| +// FleetSizesFloor is the first release that knows the fleet sizes 4g to |
| +// 192g (#892 S2, plan ruling P3). A box below it rejects a whole app |
| +// policy that names one, so the console allows a fleet size only while it |
| +// and every box that can host the app are at it. |
| +const FleetSizesFloor = "v0.43.0" |