region HA P1a final fix: F3 SetRecordRegion refuses to move an HA record under its CAS
The placement race closes in one place: SetRecordRegion's mutation refuses a region change of an HA record with *provision.HAMoveError (an ErrMoveConflict), and carlos apps place answers it with RHA-P1a-C1, the refusal its preflight gives. The preflight stays. Tests: an HA record created between the place's two scans is refused with C1 and not moved; SetRecordRegion refuses an HA record and lets a same-region write through.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
5 files changed,
+90
−1
internal/console/fleets.go+5 −1internal/console/ha_born_test.go+30 −0internal/console/seedmove.go+11 −0internal/provision/provision.go+20 −0internal/provision/seed_test.go+24 −0
diff --git a/internal/console/fleets.go b/internal/console/fleets.go| index 87f959f5d..eb01067c6 100644 |
| --- a/internal/console/fleets.go |
| +++ b/internal/console/fleets.go |
| @@ -611,7 +611,8 @@ func (s *Server) placeAppTarget(ctx context.Context, acct *Account, app, target |
| // yet (spec §5.8) refuses it, before any write. A record that cannot be |
| // read refuses it too. SetRecordRegion refuses such a seed again under |
| // its CAS, for a record created after this read. |
| - // An HA record does not move either (RHA-P1a-C1), on the same read. |
| + // An HA record does not move either (RHA-P1a-C1), on the same read, and |
| + // SetRecordRegion refuses one again under its CAS (final review F3). |
| switch block, ha, err := placeMoveBlock(ctx, s.cfg.Releases, provision.JudgeFrom(ctx), acct.Sqid, app); { |
| case err != nil: |
| log.Printf("console: place %s/%s: read instances: %v", acct.Sqid, app, err) |
| @@ -708,6 +709,9 @@ func (s *Server) placeAppTarget(ctx context.Context, acct *Account, app, target |
| if msg := seedMoveConflict(err); msg != "" { |
| return http.StatusConflict, msg |
| } |
| + if msg := s.haMoveConflict(err); msg != "" { |
| + return http.StatusConflict, msg |
| + } |
| log.Printf("console: place %s/%s: stamp instance %s: %v", acct.Sqid, app, host, err) |
| return http.StatusBadGateway, "could not update every instance — try again" |
| } |
diff --git a/internal/console/ha_born_test.go b/internal/console/ha_born_test.go| index 3516cb2bb..178122938 100644 |
| --- a/internal/console/ha_born_test.go |
| +++ b/internal/console/ha_born_test.go |
| @@ -229,3 +229,33 @@ func TestStaticRecordIsNotStampedAndDoesNotBlockAMove(t *testing.T) { |
| t.Fatalf("instance record = %+v, %v; want stamped", inst, err) |
| } |
| } |
| + |
| +// putHARecord creates a born-HA record of shop in region, as the create |
| +// door leaves one. |
| +func putHARecord(t *testing.T, st release.Store, sqid, host, region string) { |
| + t.Helper() |
| + rec := provision.Record{Schema: 1, Host: host, Slug: strings.SplitN(host, ".", 2)[0], App: "shop", Account: sqid, |
| + Region: region, Channel: "stable", CreatedAt: time.Now().UTC(), HA: true} |
| + if err := provision.CreateRecord(context.Background(), st, rec); err != nil { |
| + t.Fatal(err) |
| + } |
| +} |
| + |
| +// Final review F3: an HA record created after the place's preflight read |
| +// and before its walk is refused under SetRecordRegion's CAS. The place |
| +// answers RHA-P1a-C1, as its preflight does, and the record is not moved. |
| +func TestPlaceRefusesAnHARecordCreatedBetweenItsScans(t *testing.T) { |
| + fx, token := placeSeedFixture(t, "code-place-ha-race") |
| + putSeededRecord(t, fx.rel, "plain.example.com", "ie", seeds.KindReplica, seeds.StatePlaced) |
| + fx.srv.cfg.Releases = &createOnSecondListStore{Store: fx.rel, prefix: provision.RecordsPrefix(fx.sqid, "shop"), create: func(st release.Store) { |
| + putHARecord(t, st, fx.sqid, seedMoveHost, "ie") |
| + }} |
| + code, _, raw := rawReq(t, http.MethodPost, placeURL(fx, "shop"), token, `{"target":"fleet/pi"}`) |
| + if code != http.StatusConflict || errorText(raw) != haMoveC1 { |
| + t.Fatalf("place = %d %s", code, raw) |
| + } |
| + got, err := provision.GetRecord(context.Background(), fx.rel, fx.sqid, "shop", seedMoveHost) |
| + if err != nil || got.Region != "ie" || !got.HA { |
| + t.Fatalf("the late HA record was moved: %+v, %v", got, err) |
| + } |
| +} |
diff --git a/internal/console/seedmove.go b/internal/console/seedmove.go| index 827f8a71b..1b9b6f4c0 100644 |
| --- a/internal/console/seedmove.go |
| +++ b/internal/console/seedmove.go |
| @@ -120,6 +120,17 @@ func placeMoveBlock(ctx context.Context, st release.Store, judge *provision.Life |
| return "", ha, nil |
| } |
| +// haMoveConflict is RHA-P1a-C1 for a write SetRecordRegion refused under |
| +// its CAS because the record is HA (provision.HAMoveError), naming the |
| +// record's region as the place's preflight does; "" for any other error. |
| +func (s *Server) haMoveConflict(err error) string { |
| + var refusal *provision.HAMoveError |
| + if errors.As(err, &refusal) { |
| + return haMoveSentence(s.offlineName(refusal.Region)) |
| + } |
| + return "" |
| +} |
| + |
| // seedMoveConflict is the sentence of a write the record's seed refused |
| // under its CAS (provision.SeedMoveError), "" for any other error. |
| func seedMoveConflict(err error) string { |
diff --git a/internal/provision/provision.go b/internal/provision/provision.go| index d9a3b8e82..3e2d24ace 100644 |
| --- a/internal/provision/provision.go |
| +++ b/internal/provision/provision.go |
| @@ -1004,12 +1004,19 @@ func CreateRecord(ctx context.Context, s release.Store, rec Record) error { |
| // a key list that has gone stale between listing and this call should see |
| // the record is gone, not a manufactured success for a write that touched |
| // nothing. |
| +// |
| +// An HA record is never re-homed (region HA P1a, final review F3): the |
| +// refusal is *HAMoveError, decided under the CAS, so a record created HA |
| +// after a caller's own preflight read is refused too. |
| func SetRecordRegion(ctx context.Context, s release.Store, acct, app, host, region string) error { |
| _, err := updateRecord(ctx, s, acct, app, host, func(rec *Record) error { |
| if rec.Region != region { |
| if err := seedMoveRefusal(rec); err != nil { |
| return err |
| } |
| + if rec.HA { |
| + return &HAMoveError{Region: rec.Region} |
| + } |
| } |
| rec.Region = region |
| return nil |
| @@ -1017,6 +1024,19 @@ func SetRecordRegion(ctx context.Context, s release.Store, acct, app, host, regi |
| return err |
| } |
| +// HAMoveError is a region change refused because the record is HA (region |
| +// HA P1a: no offline move into or out of an HA host yet): an |
| +// ErrMoveConflict. Region is the record's. It is typed so a door that meets |
| +// it under the CAS (carlos apps place) answers the same refusal its |
| +// preflight does (the console's RHA-P1a-C1). |
| +type HAMoveError struct{ Region string } |
| + |
| +func (e *HAMoveError) Error() string { |
| + return ErrMoveConflict.Error() + ": the record is HA, and its region does not change" |
| +} |
| + |
| +func (e *HAMoveError) Unwrap() error { return ErrMoveConflict } |
| + |
| // updateRecord is the one read-CAS-put-one-retry scaffold under both |
| // in-place record writers (SetRecordRegion, SetRecordMove): re-reading and |
| // re-applying a narrow mutation on a lost race changes nothing about what |
diff --git a/internal/provision/seed_test.go b/internal/provision/seed_test.go| index a06220594..e23878af4 100644 |
| --- a/internal/provision/seed_test.go |
| +++ b/internal/provision/seed_test.go |
| @@ -623,3 +623,27 @@ func TestSeedUnfinished(t *testing.T) { |
| } |
| } |
| } |
| + |
| +// Final review F3: SetRecordRegion refuses to move an HA record, under its |
| +// CAS (region HA P1a: no offline move into or out of an HA host yet). A |
| +// write that leaves the region as it is moves nothing and is let through. |
| +func TestSetRecordRegionRefusesAnHARecord(t *testing.T) { |
| + ctx := context.Background() |
| + s := release.NewDirStore(t.TempDir()) |
| + rec := Record{Schema: 1, Host: "canary.example.com", Slug: "canary", App: "shop", Account: "bae", |
| + Region: "ie", Channel: "stable", CreatedAt: time.Date(2026, 10, 26, 9, 0, 0, 0, time.UTC), HA: true} |
| + if err := CreateRecord(ctx, s, rec); err != nil { |
| + t.Fatal(err) |
| + } |
| + err := SetRecordRegion(ctx, s, rec.Account, rec.App, rec.Host, "fleet/pi") |
| + var ha *HAMoveError |
| + if !errors.Is(err, ErrMoveConflict) || !errors.As(err, &ha) || ha.Region != "ie" { |
| + t.Fatalf("err = %v, want an HA move conflict naming ie", err) |
| + } |
| + if got, err := GetRecord(ctx, s, rec.Account, rec.App, rec.Host); err != nil || got.Region != "ie" || !got.HA { |
| + t.Fatalf("a refused re-home wrote: %+v, %v", got, err) |
| + } |
| + if err := SetRecordRegion(ctx, s, rec.Account, rec.App, rec.Host, rec.Region); err != nil { |
| + t.Fatalf("a write that keeps the region: %v", err) |
| + } |
| +} |