moves: carlos apps place refuses a rootfs-kind app; SetPlacement refuses a runtime row in SQL
Task 13 round 3, Paul's ruling under stopping rule 2: - placeAppTarget refuses, with C24 and before any write, an app whose established kind (release.EstablishedKind, fixed at first ship) is rootfs, with or without records and routes; a kind that cannot be read refuses it (fail closed). Binary apps place as before. - registry.SetPlacement leaves a row with a runtime as it is in the UPDATE itself (runtime = '' or the target is already its region) and answers ErrRuntimePlacement (a remote error); the place answers it with C24. - The app lock from round 2 stays: it keeps a first ship that lands during a place from putting a record under it.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
6 files changed,
+174
−11
internal/console/fleets.go+23 −9internal/console/rootfs_move_test.go+92 −0internal/registry/registry.go+11 −1internal/registry/remote.go+1 −1internal/registry/runtime.go+5 −0internal/registry/runtime_test.go+42 −0
diff --git a/internal/console/fleets.go b/internal/console/fleets.go| index 7dd5cbc6e..3a5287262 100644 |
| --- a/internal/console/fleets.go |
| +++ b/internal/console/fleets.go |
| @@ -607,16 +607,27 @@ func (s *Server) placeAppTarget(ctx context.Context, acct *Account, app, target |
| } |
| } |
| - // A place re-homes every record of the app, so a record or route with a |
| - // runtime (which never moves) and a seed that may not move yet (spec |
| - // §5.8) refuse it, before any write. A record that cannot be read |
| - // refuses it too. The app lock holds from these reads through the last |
| - // write: every console create of an instance record takes it |
| - // (createRecordWithLifetime, createRecordMarked), so no record can |
| - // appear between the check and the writes it vouches for. |
| - // SetRecordRegion refuses both again under its CAS, for a writer outside |
| - // the console. |
| + // A place re-homes every record of the app, so it is refused before any |
| + // write for an app whose established kind is rootfs (Paul's ruling, |
| + // Task 13 round 3): an instance with a runtime never moves, and only a |
| + // rootfs app has them. The kind is fixed at the app's first ship |
| + // (release.EstablishedKind), so no record, route or reconcile pass can |
| + // race this check; a kind that cannot be read refuses it too. Then a |
| + // record or route with a runtime and a seed that may not move yet (spec |
| + // §5.8) refuse it, and a record that cannot be read. The app lock holds |
| + // from these reads through the last write: every console create of an |
| + // instance record takes it (createRecordWithLifetime, |
| + // createRecordMarked), so a first ship that lands during the place cannot |
| + // put a record under it. SetRecordRegion and registry.SetPlacement refuse |
| + // a runtime row again in the write itself. |
| defer s.lockApp(acct.Sqid, app)() |
| + switch kind, err := release.EstablishedKind(ctx, s.cfg.Releases, acct.Sqid, app); { |
| + case err != nil: |
| + log.Printf("console: place %s/%s: read the app's kind: %v", acct.Sqid, app, err) |
| + return http.StatusBadGateway, placeSeedReadFailed |
| + case kind.Kind == release.KindRootfs: |
| + return http.StatusConflict, rootfsPlaceWords |
| + } |
| recs, err := placeRecords(ctx, s.cfg.Releases, provision.JudgeFrom(ctx), acct.Sqid, app) |
| if err != nil { |
| log.Printf("console: place %s/%s: read instances: %v", acct.Sqid, app, err) |
| @@ -684,6 +695,9 @@ func (s *Server) placeAppTarget(ctx context.Context, acct *Account, app, target |
| continue |
| } |
| if err := reg.SetPlacement(rt.Host, target); err != nil { |
| + if errors.Is(err, registry.ErrRuntimePlacement) { |
| + return http.StatusConflict, rootfsPlaceWords |
| + } |
| log.Printf("console: place %s/%s: set placement on %s: %v", acct.Sqid, app, rt.Host, err) |
| return http.StatusBadGateway, "could not update every route — try again" |
| } |
diff --git a/internal/console/rootfs_move_test.go b/internal/console/rootfs_move_test.go| index 171c008e1..7f9727bbe 100644 |
| --- a/internal/console/rootfs_move_test.go |
| +++ b/internal/console/rootfs_move_test.go |
| @@ -407,3 +407,95 @@ func TestEverySeedMoveDoorRefusesRootfs(t *testing.T) { |
| t.Errorf("the seed move doors are %v, pinned %v", doors, rootfsMoveDoors) |
| } |
| } |
| + |
| +// putKindMarker writes the shop app's kind marker as its first ship would. |
| +func putKindMarker(t *testing.T, fx *consoleFixture, kind string) { |
| + t.Helper() |
| + body, err := json.Marshal(release.AppKindMarker{Schema: 1, Account: fx.sqid, App: "shop", Kind: kind, |
| + Hibernate: kind == release.KindRootfs, Version: "0.1.0-1", CreatedAt: time.Now().UTC()}) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if _, err := fx.rel.Put(context.Background(), release.AppKindMarkerKey(fx.sqid, "shop"), body, ""); err != nil { |
| + t.Fatal(err) |
| + } |
| +} |
| + |
| +// placeKindWorld is the shop app with no route and no record, and an owned |
| +// fleet "pi". |
| +func placeKindWorld(t *testing.T, code string) (*consoleFixture, string, *registry.Registry) { |
| + t.Helper() |
| + fx, token := fleetFixture(t, code) |
| + if _, err := fx.st.CreateAppIn(OpsAccountID, "shop", "paul@keymail.dev", time.Now()); err != nil { |
| + t.Fatal(err) |
| + } |
| + reg := openTestReg(t, fx) |
| + if status, body := postJSON(t, fleetsURL(fx, fx.sqid)+"/new", |
| + map[string]string{"name": "pi", "bucket": "pi-fleet-data"}, token); status != http.StatusOK { |
| + t.Fatalf("create fleet = %d %v", status, body) |
| + } |
| + return fx, token, reg |
| +} |
| + |
| +// Round 3 (Paul's ruling): carlos apps place refuses an app whose |
| +// established kind is rootfs, with C24 and before any write, whether or not |
| +// it has a record or route yet. A kind that cannot be read refuses it too. |
| +// An app whose kind is binary places as before. |
| +func TestPlaceRefusesARootfsKindApp(t *testing.T) { |
| + place := func(fx *consoleFixture, token string) (int, string) { |
| + code, _, raw := rawReq(t, http.MethodPost, placeURL(fx, "shop"), token, `{"target":"fleet/pi"}`) |
| + return code, errorText(raw) |
| + } |
| + unchanged := func(name string, fx *consoleFixture, reg *registry.Registry, before placeState) { |
| + t.Helper() |
| + if after := readPlaceState(t, fx, reg); !after.equal(before) { |
| + t.Fatalf("%s: a refused place wrote:\nbefore %+v\nafter %+v", name, before, after) |
| + } |
| + } |
| + |
| + fx, token, reg := placeKindWorld(t, "code-place-kind-empty") |
| + putKindMarker(t, fx, release.KindRootfs) |
| + before := readPlaceState(t, fx, reg) |
| + if code, msg := place(fx, token); code != http.StatusConflict || msg != rootfsPlaceWant { |
| + t.Fatalf("no record or route: place = %d %s", code, msg) |
| + } |
| + unchanged("no record or route", fx, reg, before) |
| + |
| + fx, token, reg = placeRootfsWorld(t, "code-place-kind-binary-rows") |
| + putKindMarker(t, fx, release.KindRootfs) |
| + before = readPlaceState(t, fx, reg) |
| + if code, msg := place(fx, token); code != http.StatusConflict || msg != rootfsPlaceWant { |
| + t.Fatalf("with a record and route: place = %d %s", code, msg) |
| + } |
| + unchanged("with a record and route", fx, reg, before) |
| + |
| + // Fail closed: a marker that does not decode, and a read that fails. |
| + fx, token, reg = placeRootfsWorld(t, "code-place-kind-garbled") |
| + if _, err := fx.rel.Put(context.Background(), release.AppKindMarkerKey(fx.sqid, "shop"), []byte("{not json"), ""); err != nil { |
| + t.Fatal(err) |
| + } |
| + before = readPlaceState(t, fx, reg) |
| + if code, msg := place(fx, token); code != http.StatusBadGateway || msg != placeSeedReadFailed { |
| + t.Fatalf("garbled marker: place = %d %s", code, msg) |
| + } |
| + unchanged("garbled marker", fx, reg, before) |
| + |
| + fx, token, reg = placeRootfsWorld(t, "code-place-kind-unreadable") |
| + putKindMarker(t, fx, release.KindRootfs) |
| + before = readPlaceState(t, fx, reg) |
| + fx.srv.cfg.Releases = &failGetStore{Store: fx.rel, key: release.AppKindMarkerKey(fx.sqid, "shop")} |
| + if code, msg := place(fx, token); code != http.StatusBadGateway || msg != placeSeedReadFailed { |
| + t.Fatalf("marker read fails: place = %d %s", code, msg) |
| + } |
| + unchanged("marker read fails", fx, reg, before) |
| + |
| + // A binary app places as before. |
| + fx, token, reg = placeRootfsWorld(t, "code-place-kind-binary") |
| + putKindMarker(t, fx, "binary") |
| + if code, msg := place(fx, token); code != http.StatusOK { |
| + t.Fatalf("binary app: place = %d %s", code, msg) |
| + } |
| + if after := readPlaceState(t, fx, reg); after.placement != "fleet/pi" || after.routes["plain.example.com"] != "fleet/pi" { |
| + t.Fatalf("binary app did not place: %+v", after) |
| + } |
| +} |
diff --git a/internal/registry/registry.go b/internal/registry/registry.go| index a73429fac..c88e62d28 100644 |
| --- a/internal/registry/registry.go |
| +++ b/internal/registry/registry.go |
| @@ -2603,7 +2603,10 @@ func (r *Registry) SetPlacement(host, target string) error { |
| return fmt.Errorf("invalid placement target %q (want \"\" or \"fleet/<name>\")", target) |
| } |
| } |
| - res, err := r.sql.Exec(`UPDATE routes SET region = ? WHERE host = ?`, target, host) |
| + // A row with a runtime never moves (rootfs apps Plan 3 Task 13): the |
| + // statement itself leaves it as it is unless the target is already its |
| + // region, so the refusal holds whoever else writes the row meanwhile. |
| + res, err := r.sql.Exec(`UPDATE routes SET region = ? WHERE host = ? AND (runtime = '' OR region = ?)`, target, host, target) |
| if err != nil { |
| return err |
| } |
| @@ -2612,6 +2615,13 @@ func (r *Registry) SetPlacement(host, target string) error { |
| return err |
| } |
| if n == 0 { |
| + rt, err := r.Get(host) |
| + if err != nil { |
| + return err |
| + } |
| + if rt != nil && rt.Runtime != "" { |
| + return ErrRuntimePlacement |
| + } |
| return fmt.Errorf("no route for host %q", host) |
| } |
| return nil |
diff --git a/internal/registry/remote.go b/internal/registry/remote.go| index 1ce0675fb..0273300cb 100644 |
| --- a/internal/registry/remote.go |
| +++ b/internal/registry/remote.go |
| @@ -14,7 +14,7 @@ import ( |
| var errRemoteMethod = errors.New("registry: unsupported remote operation") |
| -var remoteErrors = []error{os.ErrNotExist, os.ErrPermission, sql.ErrNoRows, ErrClaimOwnership, ErrTimerChanged, ErrTimerKind, ErrPathsChanged, ErrFleetBoxActive, context.Canceled, context.DeadlineExceeded, ErrGoodBuildChanged, ErrRuntimeFixed, ErrRootfsRow} |
| +var remoteErrors = []error{os.ErrNotExist, os.ErrPermission, sql.ErrNoRows, ErrClaimOwnership, ErrTimerChanged, ErrTimerKind, ErrPathsChanged, ErrFleetBoxActive, context.Canceled, context.DeadlineExceeded, ErrGoodBuildChanged, ErrRuntimeFixed, ErrRootfsRow, ErrRuntimePlacement} |
| func responseError(out remoteResponse) error { |
| if !out.Failed { |
diff --git a/internal/registry/runtime.go b/internal/registry/runtime.go| index 388263332..825745476 100644 |
| --- a/internal/registry/runtime.go |
| +++ b/internal/registry/runtime.go |
| @@ -8,6 +8,11 @@ import "errors" |
| // the service-user child that root's `carlos route hibernate` writes through. |
| var ErrRuntimeFixed = errors.New("registry: a rootfs row's hibernation is fixed when it is minted") |
| +// ErrRuntimePlacement is SetPlacement's refusal of a row whose Runtime is |
| +// not "": an instance with a runtime never moves, so its placement is fixed |
| +// when it is minted. One of remoteErrors. |
| +var ErrRuntimePlacement = errors.New("registry: a row with a runtime keeps the placement it was minted with") |
| + |
| // Rootfs reports whether t runs a rootfs image (Runtime RuntimeRootfsV1). |
| // An unknown runtime is not rootfs: it refuses before any start (Plan 2). |
| func (t Route) Rootfs() bool { return t.Runtime == RuntimeRootfsV1 } |
diff --git a/internal/registry/runtime_test.go b/internal/registry/runtime_test.go| index 0297f6fe2..e561930aa 100644 |
| --- a/internal/registry/runtime_test.go |
| +++ b/internal/registry/runtime_test.go |
| @@ -410,3 +410,45 @@ func assertUpsertUnlessRootfs(t *testing.T, reg *Registry) { |
| } |
| } |
| } |
| + |
| +// A row with a runtime keeps the placement it was minted with: SetPlacement |
| +// refuses it with ErrRuntimePlacement in the statement itself, for rootfs-v1 |
| +// and an unknown runtime alike, unless the target is already its region. A |
| +// binary row moves as before, and a missing row is still an error. |
| +func TestSetPlacementRefusesARuntimeRow(t *testing.T) { |
| + reg := openRuntimeTestRegistry(t) |
| + for _, rt := range []Route{ |
| + {Host: "bab.example.com", Kind: "instance", Runtime: RuntimeRootfsV1, Region: "ie"}, |
| + {Host: "future.example.com", Kind: "instance", Runtime: "rootfs-v9", Region: "ie"}, |
| + {Host: "plain.example.com", Kind: "instance", Region: "ie"}, |
| + } { |
| + if err := reg.Upsert(rt); err != nil { |
| + t.Fatal(err) |
| + } |
| + } |
| + for _, host := range []string{"bab.example.com", "future.example.com"} { |
| + for _, target := range []string{"fleet/pi", ""} { |
| + if err := reg.SetPlacement(host, target); !errors.Is(err, ErrRuntimePlacement) { |
| + t.Fatalf("%s to %q: err = %v, want ErrRuntimePlacement", host, target, err) |
| + } |
| + if got := mustGet(t, reg, host); got.Region != "ie" { |
| + t.Fatalf("%s to %q: region %q, want ie", host, target, got.Region) |
| + } |
| + } |
| + } |
| + if err := reg.Upsert(Route{Host: "fleet.example.com", Kind: "instance", Runtime: RuntimeRootfsV1, Region: "fleet/pi"}); err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := reg.SetPlacement("fleet.example.com", "fleet/pi"); err != nil { |
| + t.Fatalf("a runtime row placed where it is: %v", err) |
| + } |
| + if err := reg.SetPlacement("plain.example.com", "fleet/pi"); err != nil { |
| + t.Fatalf("a binary row: %v", err) |
| + } |
| + if got := mustGet(t, reg, "plain.example.com"); got.Region != "fleet/pi" { |
| + t.Fatalf("binary row region %q, want fleet/pi", got.Region) |
| + } |
| + if err := reg.SetPlacement("absent.example.com", "fleet/pi"); err == nil || errors.Is(err, ErrRuntimePlacement) { |
| + t.Fatalf("a missing row: err = %v", err) |
| + } |
| +} |