copies: raising copies keeps the serving box (#511)
Drill 1 review round 1, Astra A2 and Fable M3. Placement kept a running copy only when it could not fill the count, so a small serving box a, with larger b and c eligible, was retired for [b, c] before either served. The copies door now refuses a count when an instance's serving box (its FleetBox, or an assignment member that is up and not draining) has no room for the effective size, naming it (SR-4-C91). Place already keeps every eligible current box; placement now also blocks, writing nothing, when it would retire a current box that is up and not draining unless the count is lowered (SR-4-C86 reworded). A draining box still leaves. Witness: TestReplicaRaiseKeepsServingBox (small pi-1 serving, large pi-2 and pi-3: refused at standard; admitted at small with pi-1 kept; the size grown afterwards blocks placement and pi-1 stays).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
3 files changed,
+109
−4
internal/console/appcopies.go+11 −0internal/console/replica_drill1_test.go+69 −0internal/console/replicaplacement.go+29 −4
diff --git a/internal/console/appcopies.go b/internal/console/appcopies.go| index f46729be6..f37000c5c 100644 |
| --- a/internal/console/appcopies.go |
| +++ b/internal/console/appcopies.go |
| @@ -31,6 +31,8 @@ const ( |
| copiesOverlap = "more than one copy needs a release and app policy that let each copy switch builds while its old build keeps serving. change them first" |
| // copy SR-4-C85 (#511 drill 1, R19) |
| copiesSize = "%d copies need %d boxes with room for size %s. this fleet has %d. choose a smaller size" |
| + // copy SR-4-C91 (#511 drill 1 round 1, Astra A2) |
| + copiesServing = "%s serves a copy of %s and has no room for size %s. choose a smaller size or move the instance first" |
| ) |
| func atReplicaFloor(build string) bool { |
| @@ -395,14 +397,23 @@ func (s *Server) copiesSizeRefusal(ctx context.Context, acct *Account, app strin |
| return &doorRefusal{Status: http.StatusServiceUnavailable, Msg: copiesUnread} |
| } |
| fit := 0 |
| + eligible := map[string]bool{} |
| for _, c := range candidates { |
| if c.Eligible { |
| fit++ |
| + eligible[c.Label] = true |
| } |
| } |
| if fit < n { |
| return &doorRefusal{Status: http.StatusConflict, Msg: fmt.Sprintf(copiesSize, n, n, size, fit)} |
| } |
| + // Raising copies keeps the serving box (round 1, Astra A2): one |
| + // that cannot hold the size is refused here, never retired. |
| + for _, box := range replicaServingBoxes(rec, v) { |
| + if !eligible[box] { |
| + return &doorRefusal{Status: http.StatusConflict, Msg: fmt.Sprintf(copiesServing, box, rec.Host, size)} |
| + } |
| + } |
| } |
| return nil |
| } |
diff --git a/internal/console/replica_drill1_test.go b/internal/console/replica_drill1_test.go| index 15695873a..44bef0519 100644 |
| --- a/internal/console/replica_drill1_test.go |
| +++ b/internal/console/replica_drill1_test.go |
| @@ -4,6 +4,7 @@ import ( |
| "context" |
| "encoding/json" |
| "net/http" |
| + "slices" |
| "strings" |
| "testing" |
| "time" |
| @@ -135,3 +136,71 @@ func TestReplicaPlacementKeepsRunningCopy(t *testing.T) { |
| } |
| } |
| } |
| + |
| +// sizedBeat is box's heartbeat reporting mib of memory, at the floor, |
| +// serving the copies-1 instance copiesWorld put on it (none for pi-3). |
| +func sizedBeat(t *testing.T, fx *consoleFixture, box string, mib int64) { |
| + t.Helper() |
| + hosts := map[string]status.HostRollout{} |
| + switch box { |
| + case "pi-1": |
| + hosts["a.example.com"] = status.HostRollout{Account: fx.sqid, App: "shop", Serving: "v1", Installed: "v1", Awake: true} |
| + case "pi-2": |
| + 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}}} |
| + if err := status.WriteBox(context.Background(), release.NewPrefixStore(fx.rel, "fleets/pi/"), b); err != nil { |
| + t.Fatal(err) |
| + } |
| +} |
| + |
| +// TestReplicaRaiseKeepsServingBox (#511 drill 1 round 1, Astra A2 and |
| +// Fable M3): a heterogeneous fleet, small pi-1 serving a.example.com at |
| +// copies 1, large pi-2 and pi-3. Raising copies never retires pi-1: |
| +// - at size standard pi-1 has no room, and the door refuses, naming it, |
| +// although pi-2 and pi-3 could take the count; |
| +// - at size small the door admits, and placement keeps pi-1; |
| +// - when the size grows afterwards, placement reports itself blocked |
| +// and keeps pi-1, rather than move the copy to pi-3. |
| +func TestReplicaRaiseKeepsServingBox(t *testing.T) { |
| + fx, token := copiesWorld(t) |
| + reg := openTestReg(t, fx) |
| + defer reg.Close() |
| + registerFleetBox(t, reg, "pi", "pi-3") |
| + sizedBeat(t, fx, "pi-1", 1858) |
| + sizedBeat(t, fx, "pi-2", 8192) |
| + sizedBeat(t, fx, "pi-3", 8192) |
| + want := "pi-1 serves a copy of a.example.com and has no room for size standard. choose a smaller size or move the instance first" |
| + if code, out := copiesPut(t, fx, token, `{"copies":2}`); code != http.StatusConflict || !strings.Contains(out, want) { |
| + t.Fatalf("copies 2 with the serving box too small: %d %s", code, out) |
| + } |
| + policy := func(rev uint64, size string) { |
| + t.Helper() |
| + _, cur, _ := fx.rel.Get(context.Background(), apppolicy.Key(fx.sqid, "shop")) |
| + p := apppolicy.Policy{V: 1, Account: fx.sqid, App: "shop", Revision: rev, SignedAt: time.Now(), DefaultSize: size} |
| + if _, err := apppolicy.PutIfMatchETag(context.Background(), fx.rel, fx.priv, p, cur); err != nil { |
| + t.Fatal(err) |
| + } |
| + } |
| + policy(1, "small") |
| + if code, out := copiesPut(t, fx, token, `{"copies":2}`); code != http.StatusOK { |
| + t.Fatalf("copies 2 at size small: %d %s", code, out) |
| + } |
| + relabelPass(t, fx) |
| + a := shopRec(t, fx, "a.example.com") |
| + if a.ReplicaAssignment == nil || !a.ReplicaAssignment.Contains("pi-1") { |
| + t.Fatalf("placement left out the serving box: %+v", a.ReplicaAssignment) |
| + } |
| + policy(2, "large") |
| + v, err := fx.srv.loadFleetView(context.Background(), "pi") |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := fx.srv.reconcileReplicaFleet(context.Background(), "pi", v, time.Now()); err == nil || !strings.Contains(err.Error(), "placement blocked: pi-1") { |
| + t.Fatalf("placement did not report pi-1 blocked: %v", err) |
| + } |
| + if a := shopRec(t, fx, "a.example.com"); !a.ReplicaAssignment.Contains("pi-1") || slices.Contains(a.ReplicaAssignment.Retiring, "pi-1") { |
| + t.Fatalf("placement retired the serving box: %+v", a.ReplicaAssignment) |
| + } |
| +} |
diff --git a/internal/console/replicaplacement.go b/internal/console/replicaplacement.go| index 6f0c66b25..9898939e8 100644 |
| --- a/internal/console/replicaplacement.go |
| +++ b/internal/console/replicaplacement.go |
| @@ -122,8 +122,12 @@ func (s *Server) replicaNext(rec provision.Record, v fleetView, n int) (replicas |
| // size, its floor) is not retired while the result falls short of the |
| // count. The record keeps its FleetBox or its assignment as it is, and |
| // the pass reports the placement blocked. A draining box still leaves: |
| - // the operator asked for that. |
| - if len(next.Boxes) < n { |
| + // the operator asked for that. Raising or keeping the count never |
| + // retires a serving box either, even when other boxes could take the |
| + // count (round 1, Astra A2): Place keeps every eligible current box, |
| + // and an ineligible one blocks rather than leave before a replacement |
| + // serves. Only lowering the count retires up boxes. |
| + if len(next.Boxes) < n || n >= len(current.Boxes) { |
| for _, box := range current.Boxes { |
| if next.Contains(box) { |
| continue |
| @@ -181,8 +185,29 @@ func replicaCandidates(rec provision.Record, v fleetView, n int, sizeName string |
| } |
| // replicaPlacementBlocked is placement's report when it would retire a |
| -// running copy only because too few boxes can hold the count. copy SR-4-C86 |
| -const replicaPlacementBlocked = "placement blocked: fewer boxes can hold this instance's copies than the %[2]d it asks for, so its copy on %[1]s keeps running where it is" |
| +// running copy only because that box cannot hold it. copy SR-4-C86 |
| +const replicaPlacementBlocked = "placement blocked: %[1]s has no room left for this instance's copies (%[2]d asked), so its copy there keeps running where it is" |
| + |
| +// replicaServingBoxes are the boxes serving rec's copies now: its FleetBox |
| +// before any assignment, else the assignment's members that are up (fresh) |
| +// and not draining in v. |
| +func replicaServingBoxes(rec provision.Record, v fleetView) []string { |
| + if rec.ReplicaAssignment == nil { |
| + if rec.FleetBox == "" { |
| + return nil |
| + } |
| + return []string{rec.FleetBox} |
| + } |
| + var out []string |
| + for _, box := range rec.ReplicaAssignment.Boxes { |
| + for _, b := range v.Boxes { |
| + if b.Label == box && b.Fresh && !b.Draining { |
| + out = append(out, box) |
| + } |
| + } |
| + } |
| + return out |
| +} |
| type replicaActionKind uint8 |