carlos / platform Public

copies: refuse a count with no room for its size (#511)

Drill 1, F2 and ruling R19.

The copies door counted eligible boxes without their size, so copies 2
was accepted on two 1858 MiB boxes although standard (the default size)
is more than a quarter of either. Placement then put the running copy
in Retiring with no box assigned, and its box removed it.

The door now asks placement's own predicate (replicaCandidates) for
every live instance at its effective size (policy SizeFor, standard
when none; replicaSizeName) and refuses a count with too few boxes
(SR-4-C85). Placement never retires a box that is up and not draining
when the result would fall short of the count: it writes nothing, the
record keeps its FleetBox or assignment, and the pass reports it
blocked (SR-4-C86). A draining box still leaves.

Witnesses: TestCopiesRefusedWithoutRoomForSize (HTTP door, then accepted
at size small) and TestReplicaPlacementKeepsRunningCopy (placement pass).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev 736b8ce94ffdc64788b4355e699209aaee63b32c parent 340b419
3 files changed, +160 −2
  • internal/console/appcopies.go +54 −0
  • internal/console/replica_drill1_test.go +74 −0
  • internal/console/replicaplacement.go +32 −2
diff --git a/internal/console/appcopies.go b/internal/console/appcopies.go
index d65349c8d..f46729be6 100644
--- a/internal/console/appcopies.go
+++ b/internal/console/appcopies.go
@@ -3,12 +3,15 @@ package console
import (
"context"
"encoding/json"
+ "errors"
"fmt"
"io"
"net/http"
+ "os"
"time"
"github.com/carlosframework/platform/internal/appconfig"
+ "github.com/carlosframework/platform/internal/apppolicy"
"github.com/carlosframework/platform/internal/provision"
"github.com/carlosframework/platform/internal/registry"
"github.com/carlosframework/platform/internal/release"
@@ -26,6 +29,8 @@ const (
copiesManaged = "more than one copy is available for platform-managed instances on an owned fleet only"
copiesMoving = "this instance has an import or move in progress. finish it before changing the copy count"
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"
)
func atReplicaFloor(build string) bool {
@@ -277,6 +282,13 @@ func (s *Server) copiesRefusal(ctx context.Context, acct *Account, app string, n
if n > available {
return refuse(fmt.Sprintf(copiesCapacity, n, n, available))
}
+ // The same predicate placement uses (replicaCandidates), at each
+ // instance's effective size, its policy default included (#511 drill
+ // 1, R19): a count placement could not place is refused here, rather
+ // than accepted and then left unplaced.
+ if ref := s.copiesSizeRefusal(ctx, acct, app, v, n, recs, rootfs); ref != nil {
+ return ref
+ }
// Reuse the heartbeat read already memoized by loadFleetView. Only this
// fleet's current roster can supply evidence about an instance's build.
beats, complete, err := s.loadBoxes(ctx, s.cfg.Releases, "fleets/"+fleet+"/")
@@ -352,3 +364,45 @@ func (s *Server) copiesRefusal(ctx context.Context, acct *Account, app string, n
}
return nil
}
+
+// copiesSizeRefusal refuses n copies when fewer than n of the fleet's
+// boxes are eligible for some live instance at its effective size
+// (apppolicy.Policy.SizeFor, standard when none), by placement's own
+// predicate (replicaCandidates). An app with no live instance yet is
+// judged at its default size. A policy that cannot be read refuses.
+func (s *Server) copiesSizeRefusal(ctx context.Context, acct *Account, app string, v fleetView, n int, recs []provision.Record, rootfs bool) *doorRefusal {
+ policy, _, err := apppolicy.Get(ctx, s.cfg.Releases, s.cfg.ReleasePub, acct.Sqid, app, time.Now())
+ if err != nil && !errors.Is(err, os.ErrNotExist) {
+ return &doorRefusal{Status: http.StatusServiceUnavailable, Msg: copiesUnread}
+ }
+ live := make([]provision.Record, 0, len(recs))
+ for _, rec := range recs {
+ if !rec.Retired {
+ live = append(live, rec)
+ }
+ }
+ if len(live) == 0 {
+ runtime := ""
+ if rootfs {
+ runtime = provision.RuntimeRootfsV1
+ }
+ live = append(live, provision.Record{Runtime: runtime})
+ }
+ for _, rec := range live {
+ size := replicaSizeName(policy.SizeFor(rec.Host, rec.CreatedAt))
+ candidates, err := replicaCandidates(rec, v, n, size)
+ if err != nil {
+ return &doorRefusal{Status: http.StatusServiceUnavailable, Msg: copiesUnread}
+ }
+ fit := 0
+ for _, c := range candidates {
+ if c.Eligible {
+ fit++
+ }
+ }
+ if fit < n {
+ return &doorRefusal{Status: http.StatusConflict, Msg: fmt.Sprintf(copiesSize, n, n, size, fit)}
+ }
+ }
+ return nil
+}
diff --git a/internal/console/replica_drill1_test.go b/internal/console/replica_drill1_test.go
index 8ae8e2f34..15695873a 100644
--- a/internal/console/replica_drill1_test.go
+++ b/internal/console/replica_drill1_test.go
@@ -8,8 +8,12 @@ import (
"testing"
"time"
+ "github.com/carlosframework/platform/internal/appconfig"
+ "github.com/carlosframework/platform/internal/apppolicy"
"github.com/carlosframework/platform/internal/provision"
+ "github.com/carlosframework/platform/internal/release"
"github.com/carlosframework/platform/internal/replicas"
+ "github.com/carlosframework/platform/internal/status"
)
// TestReplicaInstanceRetryCopiesOne (#511 drill 1, R20): `carlos instances
@@ -61,3 +65,73 @@ func TestReplicaInstanceRetryCopiesOne(t *testing.T) {
t.Fatalf("the record's retry was not armed: %+v", d)
}
}
+
+// smallBoxBeat is box's heartbeat as a 1858 MiB box reports it (the
+// drill's t4g.small): standard (512 MiB) is more than a quarter of it.
+func smallBoxBeat(t *testing.T, fx *consoleFixture, box string) {
+ t.Helper()
+ host, ver := "a.example.com", "v1"
+ if box == "pi-2" {
+ host, ver = "b.example.com", "v2"
+ }
+ 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}}}
+ if err := status.WriteBox(context.Background(), release.NewPrefixStore(fx.rel, "fleets/pi/"), b); err != nil {
+ t.Fatal(err)
+ }
+}
+
+// TestCopiesRefusedWithoutRoomForSize (#511 drill 1, F2/R19): the copies
+// door refuses copies N when fewer than N boxes are eligible for the
+// app's effective size, its default (standard) included, by placement's
+// own predicate. In the drill both boxes were 1858 MiB, too small for
+// standard, and copies 2 was accepted, then placed on no box. Once the
+// app's size is small, the same count is accepted.
+func TestCopiesRefusedWithoutRoomForSize(t *testing.T) {
+ fx, token := copiesWorld(t)
+ for _, box := range []string{"pi-1", "pi-2"} {
+ smallBoxBeat(t, fx, box)
+ }
+ want := "2 copies need 2 boxes with room for size standard. this fleet has 0. choose a smaller size"
+ if code, out := copiesPut(t, fx, token, `{"copies":2}`); code != http.StatusConflict || !strings.Contains(out, want) {
+ t.Fatalf("copies 2 with no box room for standard: %d %s", code, out)
+ }
+ if m, err := appconfig.Get(context.Background(), fx.rel, fx.sqid, "shop"); err != nil || appconfig.CopiesOf(m) != 1 {
+ t.Fatalf("the refused count was written: %v", err)
+ }
+ p := apppolicy.Policy{V: 1, Account: fx.sqid, App: "shop", Revision: 1, SignedAt: time.Now(), DefaultSize: "small"}
+ if _, err := apppolicy.PutIfMatchETag(context.Background(), fx.rel, fx.priv, p, ""); err != nil {
+ t.Fatal(err)
+ }
+ if code, out := copiesPut(t, fx, token, `{"copies":2}`); code != http.StatusOK {
+ t.Fatalf("copies 2 at size small: %d %s", code, out)
+ }
+}
+
+// TestReplicaPlacementKeepsRunningCopy (#511 drill 1, F2/R19): copies 2
+// is set while the boxes' memory is unknown; then both report 1858 MiB,
+// too small for standard. Placement would have put each instance's
+// running copy in Retiring with no box assigned, and its box removed it.
+// Now it writes nothing: each record keeps its FleetBox, and the pass
+// reports the placement blocked.
+func TestReplicaPlacementKeepsRunningCopy(t *testing.T) {
+ fx, _, _ := replicaPlacementWorld(t)
+ for _, box := range []string{"pi-1", "pi-2"} {
+ smallBoxBeat(t, fx, box)
+ }
+ v, err := fx.srv.loadFleetView(context.Background(), "pi")
+ if err != nil {
+ t.Fatal(err)
+ }
+ err = fx.srv.reconcileReplicaFleet(context.Background(), "pi", v, time.Now())
+ if err == nil || !strings.Contains(err.Error(), "placement blocked") {
+ t.Fatalf("placement did not report itself blocked: %v", err)
+ }
+ for host, box := range map[string]string{"a.example.com": "pi-1", "b.example.com": "pi-2"} {
+ rec := shopRec(t, fx, host)
+ if rec.ReplicaAssignment != nil || rec.FleetBox != box {
+ t.Fatalf("%s: placement moved the running copy: FleetBox %q, assignment %+v", host, rec.FleetBox, rec.ReplicaAssignment)
+ }
+ }
+}
diff --git a/internal/console/replicaplacement.go b/internal/console/replicaplacement.go
index 899548a83..6f0c66b25 100644
--- a/internal/console/replicaplacement.go
+++ b/internal/console/replicaplacement.go
@@ -117,6 +117,24 @@ func (s *Server) replicaNext(rec provision.Record, v fleetView, n int) (replicas
if err != nil {
return next, err
}
+ // Placement never removes a running copy because it cannot place
+ // (#511 drill 1, R19): a box that is up but no longer eligible (its
+ // 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 {
+ for _, box := range current.Boxes {
+ if next.Contains(box) {
+ continue
+ }
+ for _, b := range v.Boxes {
+ if b.Label == box && b.Fresh && !b.Draining {
+ return replicas.Assignment{}, fmt.Errorf(replicaPlacementBlocked, box, n)
+ }
+ }
+ }
+ }
// Keep an eligible coordinator when reducing the deterministic prefix.
if c := rec.Coordinator; c != nil && slices.Contains(current.Boxes, c.Box) && !next.Contains(c.Box) {
for _, candidate := range candidates {
@@ -134,10 +152,18 @@ func (s *Server) replicaNext(rec provision.Record, v fleetView, n int) (replicas
return next, nil
}
-func replicaCandidates(rec provision.Record, v fleetView, n int, sizeName string) ([]replicas.Candidate, error) {
+// replicaSizeName is the size a copy is placed at: the policy's size for
+// the instance (apppolicy.Policy.SizeFor), standard when it names none.
+// The copies door and placement share it (#511 drill 1, R19).
+func replicaSizeName(sizeName string) string {
if sizeName == "" {
- sizeName = "standard"
+ return "standard"
}
+ return sizeName
+}
+
+func replicaCandidates(rec provision.Record, v fleetView, n int, sizeName string) ([]replicas.Candidate, error) {
+ sizeName = replicaSizeName(sizeName)
size, ok := cgroup.SizeByName(sizeName)
if !ok {
return nil, replicas.ErrInvalid
@@ -154,6 +180,10 @@ func replicaCandidates(rec provision.Record, v fleetView, n int, sizeName string
return candidates, nil
}
+// 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"
+
type replicaActionKind uint8
const (