console: subdomain remove retires; rollback undoes only its own write
Spec rev 4 (§4.3), fixing Astra's final-review P1 (remove then reassign), with P2-3, P2-4, Fable's display memo and a hermetic test. - Remove retires the namespace claim at the door's clock (subdomainNow, a test seam); it never deletes. Its refusals are as before. The view keeps retired names apart (namespaceView.retired): nobody's for creates, so every create under one is refused with the unassigned answer, the old holder's included; show and canonical hosts see no name; the inventory does not offer it as a label. - Set judges a fresh read of the key: the same account unretires at once; another account is refused with retired_cooldown (409, the approved refuse.retired_cooldown, the time rounded up to the minute) until an hour after retired_at, then replaces the claim behind the occupancy walk and the re-read. Old-holder occupants refuse as occupied. - Rollback (P2-3) is conditioned on the ETag its own write answered: a create is deleted, an unretire or replace has the retired claim put back. A later write at the key is never undone; the door then says the name is still held. - check_host (P2-4) falls back to the account's eligible routes in the console registry (platform app URLs), by derivesName. - Display reads (canonical hosts, the settings cell) share a 5 s memo of the namespace view, reset by subdomainWritten. Doors and the assignment walk read fresh. - canonFixture answers the instance-report fetch in process; the instance page no longer dials c1.shop.bab.oncarlos.com (Fable P3).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
11 files changed,
+895
−91
approved_account_subdomains_copy_test.go+1 −0internal/console/apisubdomain.go+220 −76internal/console/apisubdomain_test.go+12 −7internal/console/canonical.go+61 −1internal/console/canonical_test.go+7 −1internal/console/server.go+8 −0internal/console/subdomain_copy.go+2 −0internal/console/subdomain_finalfix_test.go+91 −0internal/console/subdomain_retire_test.go+469 −0internal/console/subdomains.go+21 −6internal/domains/namespace.go+3 −0
diff --git a/approved_account_subdomains_copy_test.go b/approved_account_subdomains_copy_test.go| index f17f5a724..f5d3f0935 100644 |
| --- a/approved_account_subdomains_copy_test.go |
| +++ b/approved_account_subdomains_copy_test.go |
| @@ -42,6 +42,7 @@ var accountSubdomainsCopyIDs = []string{ |
| "refuse.rolled_back", |
| "refuse.rollback_failed", |
| "refuse.remove_blocked", |
| + "refuse.retired_cooldown", |
| "refuse.not_operator", |
| "refuse.host_claims", |
| "refuse.create.unassigned", |
diff --git a/internal/console/apisubdomain.go b/internal/console/apisubdomain.go| index c80b0cdeb..25bae9831 100644 |
| --- a/internal/console/apisubdomain.go |
| +++ b/internal/console/apisubdomain.go |
| @@ -16,10 +16,13 @@ package console |
| // refuses next: the claim mutation boundary has no account-level writes. |
| // |
| // Set judges every §4.2 rule (subdomainRefusal), writes the namespace claim |
| -// create-only, and reads again (§4.2, the race): anything of another |
| -// account's under the name, a second name for the account, or a read that |
| -// fails, and it deletes its own claim (etag-conditional) and refuses. Show |
| -// and remove resolve deleted accounts (AccountAnyBySqid); set does not. |
| +// (create-only; or, over a retired claim, an unretire by the same account or |
| +// a replace by another one an hour after the remove, §4.3 rev 4), and reads |
| +// again (§4.2, the race): anything of another account's under the name, a |
| +// second name for the account, or a read that fails, and it undoes its own |
| +// write, conditional on the ETag that write answered, and refuses. Remove |
| +// retires the claim (etag-conditional) and never deletes it. Show and |
| +// remove resolve deleted accounts (AccountAnyBySqid); set does not. |
| // Every write that lands resets the claim cache and bumps the serial, so |
| // boxes and display reads see it at once; refused and no-op requests do |
| // neither. |
| @@ -209,15 +212,26 @@ func (s *Server) subdomainSetReply(ctx context.Context, acct *Account, name stri |
| // subdomainCheckHost is one host under name that a box derives for account |
| // (deriveNamespaceAliases): the first, by host, of the account's instance |
| // records at <prefix>.<sqid>.<apps-domain> that derivesName allows (not a |
| -// replica, not a sidecar), as <prefix>.<name>.<apps-domain>. A real host |
| -// answers over TLS however the deployment issues certificates, wildcard or |
| -// per host. "" when the account has none, or when the walk or every read |
| -// fails (logged): the CLI then skips its TLS check. |
| +// replica, not a sidecar), as <prefix>.<name>.<apps-domain>; with none, the |
| +// first such route of the account's in the console's registry |
| +// (subdomainCheckRoute: platform app URLs above all, which have no record). |
| +// A real host answers over TLS however the deployment issues certificates, |
| +// wildcard or per host. "" when the account has none, or when the walk or |
| +// every read fails (logged): the CLI then skips its TLS check. |
| func (s *Server) subdomainCheckHost(ctx context.Context, account, name string) string { |
| domain := s.appsDomain() |
| if domain == "" || s.cfg.Releases == nil { |
| return "" |
| } |
| + if h := s.subdomainCheckRecord(ctx, account, name); h != "" { |
| + return h |
| + } |
| + return s.subdomainCheckRoute(account, name) |
| +} |
| + |
| +// subdomainCheckRecord is subdomainCheckHost's instance-record half. |
| +func (s *Server) subdomainCheckRecord(ctx context.Context, account, name string) string { |
| + domain := s.appsDomain() |
| under := account + "." + domain |
| hosts, err := provision.RecordHostDeclaredUnder(ctx, s.cfg.Releases, under) |
| if err != nil { |
| @@ -250,11 +264,71 @@ func (s *Server) subdomainCheckHost(ctx context.Context, account, name string) s |
| return "" |
| } |
| +// subdomainCheckRoute is subdomainCheckHost's registry half: the first, by |
| +// shown host, of the account's routes in the console's registry that |
| +// derivesName allows (provisioned or platform, not a sidecar or replica) |
| +// and that sit at <prefix>.<sqid>.<apps-domain>, as |
| +// <prefix>.<name>.<apps-domain>. "" when there is none or the registry does |
| +// not read (logged). |
| +func (s *Server) subdomainCheckRoute(account, name string) string { |
| + domain := s.appsDomain() |
| + reg, err := openRegistry(s.cfg.Registry) |
| + if err != nil { |
| + log.Printf("console: subdomain set %s %s: check host: registry: %v", account, name, err) |
| + return "" |
| + } |
| + routes, err := reg.List() |
| + if err != nil { |
| + log.Printf("console: subdomain set %s %s: check host: routes: %v", account, name, err) |
| + return "" |
| + } |
| + view := namespaceView{byAccount: map[string]string{account: name}} |
| + best := "" |
| + for _, rt := range routes { |
| + if rt.Account != account || !derivesName(rt.Provisioned, rt.Platform, rt.Replica, rt.Kind) { |
| + continue |
| + } |
| + if shown := canonicalHostIn(view, domain, rt.Host); shown != rt.Host && (best == "" || shown < best) { |
| + best = shown |
| + } |
| + } |
| + return best |
| +} |
| + |
| +// subdomainClock is the door's clock: subdomainNow, or time.Now when it is |
| +// nil, so a &Server{} literal stays safe. |
| +func (s *Server) subdomainClock() time.Time { |
| + if s.subdomainNow != nil { |
| + return s.subdomainNow() |
| + } |
| + return time.Now() |
| +} |
| + |
| +// retiredCooldownShown is when a retired name becomes available to another |
| +// account, as the refusal names it ("2006-01-02 15:04 UTC"): rounded up to |
| +// the minute, so the time named is never earlier than the door's. |
| +func retiredCooldownShown(at time.Time) string { |
| + at = at.UTC() |
| + if t := at.Truncate(time.Minute); !t.Equal(at) { |
| + at = t.Add(time.Minute) |
| + } |
| + return at.Format("2006-01-02 15:04 UTC") |
| +} |
| + |
| +// refuseRetiredCooldown answers a set of a name another account removed |
| +// less than domains.NamespaceCooldown ago: 409, like the door's other |
| +// refusals of a name someone has, naming when it becomes available. |
| +func refuseRetiredCooldown(w http.ResponseWriter, name string, available time.Time) { |
| + subdomainRefuse(w, http.StatusConflict, domains.SubdomainRetiredCooldown, |
| + fmt.Sprintf(subdomainRefuseRetiredCooldownCopy, name, retiredCooldownShown(available)), nil, false) |
| +} |
| + |
| // subdomainWritten tells the readers a namespace claim changed: the claim |
| -// cache is reset (display reads re-read every claim) and the serial bumped |
| -// (boxes reconcile now, not on the hourly floor). |
| +// cache and the display memo are reset (display reads re-read every claim) |
| +// and the serial bumped (boxes reconcile now, not on the hourly floor). |
| func (s *Server) subdomainWritten(ctx context.Context) { |
| s.claims.Reset() |
| + s.resetDisplayNamespaces() |
| s.bumpSerial(ctx) |
| } |
| @@ -323,6 +397,10 @@ func (s *Server) handleAPISubdomainSet(w http.ResponseWriter, r *http.Request) { |
| return |
| } |
| if ref, occ := s.subdomainRefusal(ctx, v, acct.Sqid, name); ref != "" { |
| + if ref == domains.SubdomainRetiredCooldown { |
| + refuseRetiredCooldown(w, name, v.retired[name].AvailableAt()) |
| + return |
| + } |
| s.refuseSubdomain(w, ref, acct, req.Name, name, v.byAccount[acct.Sqid], occ) |
| return |
| } |
| @@ -341,49 +419,109 @@ func (s *Server) handleAPISubdomainSet(w http.ResponseWriter, r *http.Request) { |
| s.refuseSubdomain(w, domains.SubdomainUnreadable, acct, req.Name, name, "", nil) |
| return |
| } |
| - domain := s.appsDomain() |
| - host := domains.NamespaceHost(name, domain) |
| - _, created, err := domains.AttachAccountNamespace(ctx, s.cfg.Releases, name, domain, acct.Sqid, |
| - time.Now().UTC().Format(time.RFC3339), addr) |
| - if err != nil { |
| - log.Printf("console: subdomain set %s %s: write: %v", acct.Sqid, name, err) |
| - s.refuseSubdomain(w, domains.SubdomainUnreadable, acct, req.Name, name, "", nil) |
| - return |
| - } |
| - if !created { |
| - // The key holds a claim already: the account's own namespace claim |
| - // (a view that had not seen it, or a retry) is this same answer |
| - // again; anything else at the key (an instance catch-all on |
| - // <name>.<apps>) occupies the name. |
| - cur, _, err := domains.GetClaimETag(ctx, s.cfg.Releases, host) |
| - switch { |
| - case err != nil: |
| - log.Printf("console: subdomain set %s %s: read %s: %v", acct.Sqid, name, host, err) |
| - s.refuseSubdomain(w, domains.SubdomainUnreadable, acct, req.Name, name, "", nil) |
| - case cur.AccountNamespace && cur.Account == acct.Sqid: |
| - // Only a claim every reader trusts as the namespace is the |
| - // account's name; one they do not (pending, carrying more) |
| - // is not, and reads as unread until an operator fixes it. |
| - if byName, _ := domains.AccountNamespaces([]domains.Claim{*cur}, domain); byName[name] != acct.Sqid { |
| - s.refuseSubdomain(w, domains.SubdomainUnreadable, acct, req.Name, name, "", nil) |
| - return |
| - } |
| - writeJSON(w, http.StatusOK, s.subdomainSetReply(ctx, acct, name)) |
| - default: |
| - s.refuseSubdomain(w, domains.SubdomainOccupied, acct, req.Name, name, "", |
| - []occupant{{Host: cur.Host, Account: cur.Account, App: cur.App, Source: occupantClaim}}) |
| - } |
| + undo, wrote := s.subdomainWrite(w, ctx, acct, addr, req.Name, name, s.subdomainClock()) |
| + if !wrote { |
| return |
| } |
| log.Printf("console: subdomain set: %s gave account %s the subdomain %s", addr, acct.Sqid, name) |
| if found, conflict := s.subdomainReread(ctx, acct.Sqid, name); conflict { |
| - s.subdomainRollBack(w, ctx, acct, req.Name, name, found) |
| + s.subdomainRollBack(w, ctx, acct, req.Name, name, found, undo) |
| return |
| } |
| s.subdomainWritten(ctx) |
| writeJSON(w, http.StatusOK, s.subdomainSetReply(ctx, acct, name)) |
| } |
| +// subdomainUndo is set's own write, as its rollback undoes it: the ETag the |
| +// write answered, and the retired claim it wrote over (an unretire or a |
| +// replace), nil for a fresh create. |
| +type subdomainUndo struct { |
| + etag string |
| + prev *domains.Claim |
| +} |
| + |
| +// subdomainWrite is set's write, judged on a fresh read of the name's key |
| +// (the view may be the claim cache's, a moment old): |
| +// |
| +// - nothing there: the create-only write (domains.AttachAccountNamespace); |
| +// a create lost to a race judges what won it, below; |
| +// - the account's own retired claim: unretired at once; |
| +// - another account's retired claim: refused until an hour after its |
| +// retired_at (refuseRetiredCooldown), then replaced. The occupancy walk |
| +// before the write and the re-read after it are what refuse while |
| +// anything of the old holder's is still under the name; |
| +// - the account's own claim in force: answered again, nothing written; |
| +// - anything else: refused (a claim of another kind, or another |
| +// account's in force: occupied; the account's own untrusted one, or a |
| +// read that fails: unreadable). |
| +// |
| +// wrote is true only when a write landed; otherwise the answer is written. |
| +// Each write is conditional on the revision read, so one that finds the key |
| +// changed refuses, retryably, and writes nothing. |
| +func (s *Server) subdomainWrite(w http.ResponseWriter, ctx context.Context, acct *Account, addr, raw, name string, now time.Time) (undo subdomainUndo, wrote bool) { |
| + domain := s.appsDomain() |
| + host := domains.NamespaceHost(name, domain) |
| + unreadable := func(what string, err error) { |
| + log.Printf("console: subdomain set %s %s: %s: %v", acct.Sqid, name, what, err) |
| + s.refuseSubdomain(w, domains.SubdomainUnreadable, acct, raw, name, "", nil) |
| + } |
| + cur, etag, err := domains.GetClaimETag(ctx, s.cfg.Releases, host) |
| + if errors.Is(err, os.ErrNotExist) { |
| + newETag, created, err := domains.AttachAccountNamespace(ctx, s.cfg.Releases, name, domain, acct.Sqid, |
| + now.UTC().Format(time.RFC3339), addr) |
| + if err != nil { |
| + unreadable("write", err) |
| + return subdomainUndo{}, false |
| + } |
| + if created { |
| + return subdomainUndo{etag: newETag}, true |
| + } |
| + // The key holds a claim now: judge it as if it had been there. |
| + cur, etag, err = domains.GetClaimETag(ctx, s.cfg.Releases, host) |
| + } |
| + if err != nil { |
| + unreadable("read "+host, err) |
| + return subdomainUndo{}, false |
| + } |
| + if r, ok := domains.RetiredAccountNamespaces([]domains.Claim{*cur}, domain)[name]; ok { |
| + var newETag string |
| + var done bool |
| + if r.Account == acct.Sqid { |
| + newETag, done, err = domains.UnretireAccountNamespace(ctx, s.cfg.Releases, name, domain, acct.Sqid, etag, now, addr) |
| + } else { |
| + if now.Before(r.AvailableAt()) { |
| + refuseRetiredCooldown(w, name, r.AvailableAt()) |
| + return subdomainUndo{}, false |
| + } |
| + newETag, done, err = domains.ReplaceRetiredAccountNamespace(ctx, s.cfg.Releases, name, domain, acct.Sqid, etag, now, addr) |
| + } |
| + if err == nil && !done { |
| + err = errors.New("the retired claim changed before it could be written") |
| + } |
| + if err != nil { |
| + unreadable("write over the retired claim", err) |
| + return subdomainUndo{}, false |
| + } |
| + return subdomainUndo{etag: newETag, prev: cur}, true |
| + } |
| + switch byName, _ := domains.AccountNamespaces([]domains.Claim{*cur}, domain); { |
| + case byName[name] == acct.Sqid: |
| + // The account's own name, which a view that had not seen it, or a |
| + // retry, asked for again. |
| + writeJSON(w, http.StatusOK, s.subdomainSetReply(ctx, acct, name)) |
| + case cur.AccountNamespace && cur.Account == acct.Sqid: |
| + // Only a claim every reader trusts as the namespace is the |
| + // account's name; one they do not (pending, carrying more, a |
| + // retired_at that does not parse) is not, and reads as unread |
| + // until an operator fixes it. |
| + unreadable("its own claim at "+host+" is not one the console trusts", nil) |
| + default: |
| + s.refuseSubdomain(w, domains.SubdomainOccupied, acct, raw, name, "", |
| + []occupant{{Host: cur.Host, Account: cur.Account, App: cur.App, Source: occupantClaim}}) |
| + } |
| + return subdomainUndo{}, false |
| +} |
| + |
| // subdomainReread is set's read after its write (§4.2, the race): conflict |
| // when the walk under the name fails or finds another account's host, when |
| // the view does not show name as the account's, when the account now holds |
| @@ -421,35 +559,36 @@ func (s *Server) subdomainReread(ctx context.Context, account, name string) (fou |
| return nil, false |
| } |
| -// subdomainRollBack deletes the namespace claim set just wrote, if it is |
| -// still that claim, and refuses with what the re-read found. A delete that |
| -// fails leaves the name held: the answer is a 500 saying so and how to |
| -// remove it, and show reports the name. Either way the claim was written, |
| -// so the readers are told. |
| -func (s *Server) subdomainRollBack(w http.ResponseWriter, ctx context.Context, acct *Account, raw, name string, found []occupant) { |
| +// subdomainRollBack undoes set's own write and refuses with what the |
| +// re-read found: a fresh create is deleted (DetachAccountNamespaceAt), an |
| +// unretire or a replace has the retired claim it wrote over put back |
| +// (RestoreRetiredAccountNamespace). Either is conditional on the ETag the |
| +// write answered, never a fresh read, so a later write at the key (another |
| +// console's remove and set between the re-read and here) is never undone. |
| +// An undo that does not land leaves the name held: the answer is a 500 |
| +// saying so and how to remove it, and show reports the name; a key found |
| +// empty instead is rolled back all the same. Either way the claim was |
| +// written, so the readers are told. |
| +func (s *Server) subdomainRollBack(w http.ResponseWriter, ctx context.Context, acct *Account, raw, name string, found []occupant, undo subdomainUndo) { |
| defer s.subdomainWritten(ctx) |
| domain := s.appsDomain() |
| - host := domains.NamespaceHost(name, domain) |
| - failed := func(err error) { |
| + var undone bool |
| + var err error |
| + if undo.prev == nil { |
| + undone, err = domains.DetachAccountNamespaceAt(ctx, s.cfg.Releases, name, domain, acct.Sqid, undo.etag) |
| + } else { |
| + undone, err = domains.RestoreRetiredAccountNamespace(ctx, s.cfg.Releases, name, domain, acct.Sqid, undo.etag, *undo.prev) |
| + } |
| + if err == nil && !undone { |
| + if _, _, rerr := domains.GetClaimETag(ctx, s.cfg.Releases, domains.NamespaceHost(name, domain)); !errors.Is(rerr, os.ErrNotExist) { |
| + err = errors.New("the claim changed after set wrote it, so the later write stands") |
| + } |
| + } |
| + if err != nil { |
| log.Printf("console: subdomain set %s %s: rollback: %v; the name is still held", acct.Sqid, name, err) |
| jsonErrorCode(w, http.StatusInternalServerError, string(subdomainCodeRollbackFailed), |
| fmt.Sprintf(subdomainRefuseRollbackFailedCopy, name, acct.Sqid, acct.Sqid)) |
| - } |
| - cur, etag, err := domains.GetClaimETag(ctx, s.cfg.Releases, host) |
| - switch { |
| - case errors.Is(err, os.ErrNotExist): |
| - case err != nil: |
| - failed(err) |
| return |
| - case cur.AccountNamespace && cur.Account == acct.Sqid: |
| - removed, err := domains.DetachAccountNamespaceAt(ctx, s.cfg.Releases, name, domain, acct.Sqid, etag) |
| - if err == nil && !removed { |
| - err = errors.New("the claim changed before it could be removed") |
| - } |
| - if err != nil { |
| - failed(err) |
| - return |
| - } |
| } |
| log.Printf("console: subdomain set %s %s: rolled back", acct.Sqid, name) |
| s.refuseSubdomain(w, domains.SubdomainRolledBack, acct, raw, name, "", found) |
| @@ -458,9 +597,11 @@ func (s *Server) subdomainRollBack(w http.ResponseWriter, ctx context.Context, a |
| // handleAPISubdomainDelete is remove: refused while the account itself |
| // still has anything under the name (claims, records, pending allocations: |
| // hosts attached by hand, which the name would otherwise orphan), or when |
| -// the walk fails; otherwise the namespace claim is deleted against the |
| -// revision read. An account with no name answers as show does, writing |
| -// nothing. |
| +// the walk fails; otherwise the namespace claim is retired against the |
| +// revision read (spec §4.3, rev 4), never deleted: boxes stop deriving |
| +// from it, the account may set it again at once, and another account only |
| +// an hour later. An account with no name (a retired one included) answers |
| +// as show does, writing nothing. |
| func (s *Server) handleAPISubdomainDelete(w http.ResponseWriter, r *http.Request) { |
| addr, acct, ok := s.subdomainAccount(w, r, true) |
| if !ok { |
| @@ -518,13 +659,13 @@ func (s *Server) handleAPISubdomainDelete(w http.ResponseWriter, r *http.Request |
| refuseSubdomainUnreadable(w, root) |
| return |
| } |
| - removed, err := domains.DetachAccountNamespaceAt(ctx, s.cfg.Releases, name, domain, acct.Sqid, etag) |
| - if err != nil || !removed { |
| - log.Printf("console: subdomain remove %s %s: removed %v, %v", acct.Sqid, name, removed, err) |
| + _, retired, err := domains.RetireAccountNamespace(ctx, s.cfg.Releases, name, domain, acct.Sqid, etag, s.subdomainClock()) |
| + if err != nil || !retired { |
| + log.Printf("console: subdomain remove %s %s: retired %v, %v", acct.Sqid, name, retired, err) |
| refuseSubdomainUnreadable(w, root) |
| return |
| } |
| - log.Printf("console: subdomain remove: %s took the subdomain %s from account %s", addr, name, acct.Sqid) |
| + log.Printf("console: subdomain remove: %s took the subdomain %s from account %s (retired)", addr, name, acct.Sqid) |
| s.subdomainWritten(ctx) |
| writeJSON(w, http.StatusOK, s.subdomainShown(acct, "", nil)) |
| } |
| @@ -606,13 +747,16 @@ func (s *Server) subdomainInventory(ctx context.Context) ([]subdomainInventoryLa |
| } |
| } |
| // assigned answers whether label is an account's sqid (live or |
| - // deleted) or a held name, once per label. |
| + // deleted), a held name or a retired one, once per label. A retired |
| + // name is not free for anyone to adopt: its old holder sets it again, |
| + // or after the hour the occupancy walk judges another account. |
| assigned := map[string]bool{} |
| isAssigned := func(label string) (bool, error) { |
| if got, ok := assigned[label]; ok { |
| return got, nil |
| } |
| - held := v.byName[label] != "" |
| + _, retired := v.retired[label] |
| + held := v.byName[label] != "" || retired |
| if !held && domains.ValidSqid(label) { |
| a, _, err := s.store.AccountAnyBySqid(label) |
| if err != nil { |
| @@ -660,7 +804,7 @@ func (s *Server) accountSubdomainPane(ctx context.Context, p *page, acct *Accoun |
| p.AccountSubdomainKnown = true |
| return |
| } |
| - v, err := s.loadNamespaces(ctx) |
| + v, err := s.displayNamespaceView(ctx) |
| if err != nil { |
| log.Printf("console: account settings %s: subdomain: %v", acct.Sqid, err) |
| return |
diff --git a/internal/console/apisubdomain_test.go b/internal/console/apisubdomain_test.go| index 309736852..ebe947589 100644 |
| --- a/internal/console/apisubdomain_test.go |
| +++ b/internal/console/apisubdomain_test.go |
| @@ -168,7 +168,8 @@ func TestSubdomainDoorOperatorOnly(t *testing.T) { |
| // TestSubdomainDoorSetShowRemove: set writes the namespace claim and answers |
| // the name and its host; show answers the same; a second, different name is |
| -// refused (remove first); remove deletes the claim and show then has none. |
| +// refused (remove first); remove retires the claim (rev 4: it stays, stamped |
| +// retired_at) and show then has none. |
| func TestSubdomainDoorSetShowRemove(t *testing.T) { |
| fx, operator, _ := sdFixture(t, "code-sd-ssr") |
| want := sdAnswer{Account: "bdq", Name: "harbour", Host: "harbour." + nsApps, Foreign: []sdHost{}} |
| @@ -193,8 +194,8 @@ func TestSubdomainDoorSetShowRemove(t *testing.T) { |
| if code != http.StatusOK || a.Name != "" || a.Account != "bdq" { |
| t.Fatalf("remove = %d %s, want 200 with no name", code, out) |
| } |
| - if c := nsClaimAt(t, fx, nsHarbourHost); c != nil { |
| - t.Errorf("namespace claim after remove = %+v", c) |
| + if c := nsClaimAt(t, fx, nsHarbourHost); c == nil || c.Account != "bdq" || c.RetiredAt == "" { |
| + t.Errorf("namespace claim after remove = %+v, want bdq's, retired", c) |
| } |
| if code, a, out := sdReq(t, fx, http.MethodGet, "bdq", operator, ""); code != http.StatusOK || a.Name != "" || a.Host != "" { |
| t.Errorf("show after remove = %d %s, want no name", code, out) |
| @@ -567,14 +568,18 @@ func TestSubdomainRemoveRefusedOverOwnRecordOrAllocation(t *testing.T) { |
| } |
| } |
| -// TestSubdomainRemoveForDeletedAccount: a deleted account keeps its claim; |
| -// show and remove still find the account, set does not. |
| +// TestSubdomainRemoveForDeletedAccount: a deleted account keeps its claim |
| +// (account deletion never touches it); show and remove still find the |
| +// account, set does not; remove retires the claim, which stays. |
| func TestSubdomainRemoveForDeletedAccount(t *testing.T) { |
| fx, operator, _ := sdFixture(t, "code-sd-deleted") |
| nsHold(t, fx, "harbour", "bdq") |
| if err := fx.st.DeleteAccount(82, time.Now()); err != nil { |
| t.Fatal(err) |
| } |
| + if c := nsClaimAt(t, fx, nsHarbourHost); c == nil || c.Account != "bdq" || c.RetiredAt != "" { |
| + t.Fatalf("namespace claim after account deletion = %+v, want bdq's, in force", c) |
| + } |
| if code, a, out := sdReq(t, fx, http.MethodGet, "bdq", operator, ""); code != http.StatusOK || a.Name != "harbour" { |
| t.Errorf("show of a deleted account = %d %s, want harbour", code, out) |
| } |
| @@ -584,8 +589,8 @@ func TestSubdomainRemoveForDeletedAccount(t *testing.T) { |
| if code, a, out := sdReq(t, fx, http.MethodDelete, "bdq", operator, ""); code != http.StatusOK || a.Name != "" { |
| t.Errorf("remove on a deleted account = %d %s, want 200", code, out) |
| } |
| - if c := nsClaimAt(t, fx, nsHarbourHost); c != nil { |
| - t.Errorf("namespace claim after remove = %+v", c) |
| + if c := nsClaimAt(t, fx, nsHarbourHost); c == nil || c.Account != "bdq" || c.RetiredAt == "" { |
| + t.Errorf("namespace claim after remove = %+v, want bdq's, retired", c) |
| } |
| } |
diff --git a/internal/console/canonical.go b/internal/console/canonical.go| index 45c497ffe..90a756eff 100644 |
| --- a/internal/console/canonical.go |
| +++ b/internal/console/canonical.go |
| @@ -14,12 +14,20 @@ package console |
| // never under the apps domain, so it is never rewritten and keeps whatever |
| // precedence it has (apexHost picks among the hosts as they are; the pick is |
| // rewritten afterwards). |
| +// |
| +// Display reads share one namespace view for a few seconds |
| +// (displayNamespaceView), so a busy console does not list the claims bucket |
| +// on every page, instance list and deploy-watch poll. The memo is reset by |
| +// every namespace claim write this console makes (subdomainWritten); another |
| +// console's write shows within displayNamespaceTTL. Doors and the |
| +// assignment walk never read it: they load the view fresh. |
| import ( |
| "context" |
| "log" |
| "strings" |
| "sync" |
| + "time" |
| "github.com/carlosframework/platform/internal/provision" |
| "github.com/carlosframework/platform/internal/registry" |
| @@ -75,7 +83,7 @@ func (c *hostCanon) host(h string) string { |
| return h |
| } |
| c.once.Do(func() { |
| - v, err := c.s.loadNamespaces(c.ctx) |
| + v, err := c.s.displayNamespaceView(c.ctx) |
| if err != nil { |
| log.Printf("console: canonical hosts: %v; showing sqid hosts", err) |
| return |
| @@ -85,6 +93,58 @@ func (c *hostCanon) host(h string) string { |
| return canonicalHostIn(c.v, c.s.appsDomain(), h) |
| } |
| +// displayNamespaceTTL is how long display reads share one namespace view. |
| +const displayNamespaceTTL = 5 * time.Second |
| + |
| +// displayNamespaceMemo is the namespace view display reads share: the last |
| +// view loaded, when, and a generation that every reset moves on, so a load |
| +// that began before a reset is never kept after it. The zero value is |
| +// empty and ready. |
| +type displayNamespaceMemo struct { |
| + mu sync.Mutex |
| + v namespaceView |
| + at time.Time |
| + ok bool |
| + gen uint64 |
| +} |
| + |
| +// displayNamespaceView is loadNamespaces for display reads only (canonical |
| +// hosts, the account settings cell): the memo's view while it is younger |
| +// than displayNamespaceTTL, else a fresh load, kept unless a reset came |
| +// while it loaded. A load that fails is not kept. Callers only read the |
| +// view's maps; they are shared. |
| +func (s *Server) displayNamespaceView(ctx context.Context) (namespaceView, error) { |
| + m := &s.displayNamespaces |
| + now := s.subdomainClock() |
| + m.mu.Lock() |
| + if m.ok && !now.Before(m.at) && now.Sub(m.at) < displayNamespaceTTL { |
| + v := m.v |
| + m.mu.Unlock() |
| + return v, nil |
| + } |
| + gen := m.gen |
| + m.mu.Unlock() |
| + v, err := s.loadNamespaces(ctx) |
| + if err != nil { |
| + return v, err |
| + } |
| + m.mu.Lock() |
| + if m.gen == gen { |
| + m.v, m.at, m.ok = v, now, true |
| + } |
| + m.mu.Unlock() |
| + return v, nil |
| +} |
| + |
| +// resetDisplayNamespaces drops the display memo: a namespace claim changed. |
| +func (s *Server) resetDisplayNamespaces() { |
| + m := &s.displayNamespaces |
| + m.mu.Lock() |
| + m.v, m.ok = namespaceView{}, false |
| + m.gen++ |
| + m.mu.Unlock() |
| +} |
| + |
| // public is h's public_host on an API answer: the shown host when it is |
| // not h itself, else "" (the field is omitted, and a client prints host, |
| // so an answer about a host with no name is the bytes it always was). |
diff --git a/internal/console/canonical_test.go b/internal/console/canonical_test.go| index 901e7fc2e..3264fbb03 100644 |
| --- a/internal/console/canonical_test.go |
| +++ b/internal/console/canonical_test.go |
| @@ -49,9 +49,12 @@ func TestCanonicalHost(t *testing.T) { |
| // canonFixture is the base console on the apps domain with app shop in bab, |
| // a route at shop.bab.<apps>, an instance at c1.shop.bab.<apps>, and bab |
| // holding harbour; paul (an owner of bab, the ops account) is signed in. |
| +// The app-host client answers the instance-report contract in process |
| +// (instancesOnlyTransport), so no page dials the apps domain for real. |
| func canonFixture(t *testing.T) (*consoleFixture, *http.Client) { |
| t.Helper() |
| fx := newConsoleFixture(t) |
| + fx.srv.instances = fx.srv.newInstanceFetcher(&http.Client{Transport: instancesOnlyTransport{}}, "http") |
| fx.srv.cfg.AppsDomain = nsApps |
| if _, err := fx.st.CreateAppIn(OpsAccountID, "shop", "paul@keymail.dev", time.Now()); err != nil { |
| t.Fatal(err) |
| @@ -237,7 +240,9 @@ func (s *nsCountStore) count() int { |
| } |
| // TestCanonicalPageLoadsNamespacesOnce: a page that shows many app URLs |
| -// lists the claims once, not once per URL. |
| +// lists the claims once, not once per URL. The display memo is dropped |
| +// before each page, so each starts cold (TestCanonicalDisplayMemo covers |
| +// the memo). |
| func TestCanonicalPageLoadsNamespacesOnce(t *testing.T) { |
| fx, c := canonFixture(t) |
| for _, h := range []string{"c2.shop.bab." + nsApps, "c3.shop.bab." + nsApps} { |
| @@ -246,6 +251,7 @@ func TestCanonicalPageLoadsNamespacesOnce(t *testing.T) { |
| counter := &nsCountStore{Store: fx.rel} |
| fx.srv.cfg.Releases = counter |
| for p, want := range map[string]string{"/": "shop.harbour." + nsApps, "/apps/shop/instances": "c3.shop.harbour." + nsApps} { |
| + fx.srv.resetDisplayNamespaces() |
| before := counter.count() |
| _, _, body := get(t, c, fx.u(p)) |
| canonHas(t, p, body, want) |
diff --git a/internal/console/server.go b/internal/console/server.go| index 2f8d205b8..9bdc7280a 100644 |
| --- a/internal/console/server.go |
| +++ b/internal/console/server.go |
| @@ -1562,6 +1562,14 @@ type Server struct { |
| // It is not the guard against another writer (the bucket key is, and |
| // the re-read): consoles do not share it. The zero value is ready. |
| subdomainMu sync.Mutex |
| + // subdomainNow is the subdomain door's clock (subdomainClock): when a |
| + // remove retires a name and when its cooldown ends. nil means |
| + // time.Now; tests set it, nothing in production does. |
| + subdomainNow func() time.Time |
| + // displayNamespaces is the short memo of the namespace view that |
| + // display reads share (displayNamespaceView, canonical.go). Doors and |
| + // the assignment walk never read it. |
| + displayNamespaces displayNamespaceMemo |
| // fleetFloorMu is the fleet forward floor's mutex (fleet public routes |
| // spec §9): every system pointer publication holds it from its last |
| // fleet floor judgment to its pointer write (fleetFloorPublish). It is |
diff --git a/internal/console/subdomain_copy.go b/internal/console/subdomain_copy.go| index 000a61190..ae5f59797 100644 |
| --- a/internal/console/subdomain_copy.go |
| +++ b/internal/console/subdomain_copy.go |
| @@ -34,6 +34,8 @@ const ( |
| subdomainRefuseRollbackFailedCopy = "could not undo the change, so %s is still held by account %s. run `carlos accounts subdomain remove --account %s`" |
| // refuse.remove_blocked |
| subdomainRefuseRemoveBlockedCopy = "account %s still has hosts under %s. detach them first:" |
| + // refuse.retired_cooldown |
| + subdomainRefuseRetiredCooldownCopy = "%q was removed from another account less than an hour ago. it becomes available to another account at %s" |
| // refuse.not_operator |
| subdomainRefuseNotOperatorCopy = "only a platform operator can change subdomains" |
| // refuse.host_claims |
diff --git a/internal/console/subdomain_finalfix_test.go b/internal/console/subdomain_finalfix_test.go| new file mode 100644 |
| index 000000000..cf35e136f |
| --- /dev/null |
| +++ b/internal/console/subdomain_finalfix_test.go |
| @@ -0,0 +1,91 @@ |
| +package console |
| + |
| +// Account subdomains, final review fixes: set's check host comes from |
| +// platform routes too (Astra P2-4), and display reads share a short memo of |
| +// the namespace view (Fable). Neutral names: bab (the ops account), bdq, |
| +// the name harbour, the apps domain oncarlos.com. |
| + |
| +import ( |
| + "context" |
| + "encoding/json" |
| + "net/http" |
| + "testing" |
| + |
| + "github.com/carlosframework/platform/internal/registry" |
| +) |
| + |
| +// TestSubdomainSetCheckHostFromPlatformRoute: bdq has no instance records, |
| +// only a platform app URL in the console's registry; set names that URL's |
| +// name host as the check host. A replica and another account's route are |
| +// passed over. |
| +func TestSubdomainSetCheckHostFromPlatformRoute(t *testing.T) { |
| + fx, operator, _ := sdFixture(t, "code-sd-check-route") |
| + for _, rt := range []registry.Route{ |
| + {Host: "a.bdq." + nsApps, App: "shop", Account: "bdq", Kind: "service", Status: "ready", Platform: true, Replica: true}, |
| + {Host: "a.bcd." + nsApps, App: "web", Account: "bcd", Kind: "service", Status: "ready", Platform: true}, |
| + {Host: "shop.bdq." + nsApps, App: "shop", Account: "bdq", Kind: "service", Status: "ready", Platform: true}, |
| + } { |
| + fx.addRoute(t, rt) |
| + } |
| + code, _, out := rawReq(t, http.MethodPost, sdURL(fx, "bdq"), operator, `{"name":"harbour"}`) |
| + var a struct { |
| + CheckHost string `json:"check_host"` |
| + } |
| + if err := json.Unmarshal([]byte(out), &a); err != nil || code != http.StatusOK { |
| + t.Fatalf("set harbour = %d %s", code, out) |
| + } |
| + if want := "shop.harbour." + nsApps; a.CheckHost != want { |
| + t.Errorf("check_host = %q, want %q", a.CheckHost, want) |
| + } |
| +} |
| + |
| +// TestCanonicalDisplayMemo: display reads within displayNamespaceTTL share |
| +// one claims listing; past it they list again; a set or remove through the |
| +// door drops the memo at once; and a door reads fresh even while the memo |
| +// is warm. |
| +func TestCanonicalDisplayMemo(t *testing.T) { |
| + fx, c := canonFixture(t) |
| + clock := &sdClock{now: sdT0} |
| + fx.srv.subdomainNow = clock.Now |
| + counter := &nsCountStore{Store: fx.rel} |
| + fx.srv.cfg.Releases = counter |
| + fx.srv.claims.Reset() |
| + fx.srv.resetDisplayNamespaces() |
| + |
| + before := counter.count() |
| + for _, p := range []string{"/", "/apps/shop/instances", "/"} { |
| + _, _, body := get(t, c, fx.u(p)) |
| + canonHas(t, p, body, "shop.harbour."+nsApps) |
| + } |
| + if got := counter.count() - before; got != 1 { |
| + t.Errorf("three display reads in the window listed the claims %d times, want 1", got) |
| + } |
| + before = counter.count() |
| + if _, ref := fx.srv.doorNamespaces(context.Background(), "x.bab."+nsApps); ref != nil { |
| + t.Fatalf("doorNamespaces = %+v", ref) |
| + } |
| + if got := counter.count() - before; got != 1 { |
| + t.Errorf("a door with the memo warm listed the claims %d times, want 1 (fresh)", got) |
| + } |
| + clock.Set(sdT0.Add(displayNamespaceTTL)) |
| + before = counter.count() |
| + get(t, c, fx.u("/")) |
| + if got := counter.count() - before; got != 1 { |
| + t.Errorf("a display read past the window listed the claims %d times, want 1", got) |
| + } |
| + |
| + // Remove and set again through the door, the clock standing still: the |
| + // next page shows each change at once. |
| + op := cliToken(t, fx, "paul@keymail.dev", "code-canon-memo-cli") |
| + if code, _, out := sdReq(t, fx, http.MethodDelete, fx.sqid, op, ""); code != http.StatusOK { |
| + t.Fatalf("remove = %d %s", code, out) |
| + } |
| + _, _, body := get(t, c, fx.u("/apps/shop/instances")) |
| + canonHas(t, "the instance list after remove", body, `href="https://c1.shop.bab.`+nsApps+`"`) |
| + canonLacks(t, "the instance list after remove", body, `href="https://c1.shop.harbour.`+nsApps+`"`) |
| + if code, _, out := sdReq(t, fx, http.MethodPost, fx.sqid, op, `{"name":"harbour"}`); code != http.StatusOK { |
| + t.Fatalf("set again = %d %s", code, out) |
| + } |
| + _, _, body = get(t, c, fx.u("/apps/shop/instances")) |
| + canonHas(t, "the instance list after set", body, `href="https://c1.shop.harbour.`+nsApps+`"`) |
| +} |
diff --git a/internal/console/subdomain_retire_test.go b/internal/console/subdomain_retire_test.go| new file mode 100644 |
| index 000000000..466f254dc |
| --- /dev/null |
| +++ b/internal/console/subdomain_retire_test.go |
| @@ -0,0 +1,469 @@ |
| +package console |
| + |
| +// Account subdomains rev 4 (spec docs/superpowers/specs/ |
| +// 2026-11-11-account-subdomains-design.md §4.3): remove retires the name. |
| +// The same account sets it again at once; another account only an hour |
| +// after the remove, behind the occupancy walk. Every rollback undoes only |
| +// its own write. Neutral names: bab (the ops account), bdq and bcd, the |
| +// name harbour, the apps domain oncarlos.com. |
| + |
| +import ( |
| + "context" |
| + "encoding/json" |
| + "errors" |
| + "fmt" |
| + "io" |
| + "net/http" |
| + "reflect" |
| + "strings" |
| + "sync" |
| + "testing" |
| + "time" |
| + |
| + "github.com/carlosframework/platform/internal/domains" |
| + "github.com/carlosframework/platform/internal/release" |
| +) |
| + |
| +// sdT0 is the retire tests' fixed clock start. |
| +var sdT0 = time.Date(2026, 11, 12, 9, 0, 30, 0, time.UTC) |
| + |
| +// sdClock is a settable clock for the subdomain door (Server.subdomainNow). |
| +type sdClock struct { |
| + mu sync.Mutex |
| + now time.Time |
| +} |
| + |
| +func (c *sdClock) Now() time.Time { |
| + c.mu.Lock() |
| + defer c.mu.Unlock() |
| + return c.now |
| +} |
| + |
| +func (c *sdClock) Set(t time.Time) { |
| + c.mu.Lock() |
| + c.now = t |
| + c.mu.Unlock() |
| +} |
| + |
| +// sdRetireFixture is sdFixture with bcd too, the door on a fixed clock at |
| +// sdT0, and bdq holding harbour, then removing it through the door. |
| +func sdRetireFixture(t *testing.T, code string) (fx *consoleFixture, operator string, clock *sdClock) { |
| + t.Helper() |
| + fx, operator, _ = sdFixture(t, code) |
| + nsAccount(t, fx, 49, "bcd") |
| + clock = &sdClock{now: sdT0} |
| + fx.srv.subdomainNow = clock.Now |
| + if code, _, out := sdReq(t, fx, http.MethodPost, "bdq", operator, `{"name":"harbour"}`); code != http.StatusOK { |
| + t.Fatalf("set harbour for bdq = %d %s", code, out) |
| + } |
| + if code, a, out := sdReq(t, fx, http.MethodDelete, "bdq", operator, ""); code != http.StatusOK || a.Name != "" { |
| + t.Fatalf("remove harbour from bdq = %d %s", code, out) |
| + } |
| + return fx, operator, clock |
| +} |
| + |
| +// sdCooldownCopy is the cooldown refusal for harbour, available at the |
| +// first minute at or after sdT0 plus the hour. |
| +var sdCooldownCopy = fmt.Sprintf(subdomainRefuseRetiredCooldownCopy, "harbour", "2026-11-12 10:01 UTC") |
| + |
| +// TestSubdomainRemoveRetires: remove stamps retired_at from the door's |
| +// clock and keeps the claim. bdq has no name any more (show, the settings |
| +// cell, canonical hosts); the name is nobody's for creates, so every |
| +// account is refused under it with the unassigned answer, bdq included; |
| +// and the inventory does not list it as a label to assign. |
| +func TestSubdomainRemoveRetires(t *testing.T) { |
| + fx, operator, _ := sdRetireFixture(t, "code-sd-retire") |
| + c := nsClaimAt(t, fx, nsHarbourHost) |
| + if c == nil || !c.AccountNamespace || c.Account != "bdq" || c.RetiredAt != "2026-11-12T09:00:30Z" { |
| + t.Fatalf("namespace claim after remove = %+v, want bdq's, retired at the door's clock", c) |
| + } |
| + if code, a, out := sdReq(t, fx, http.MethodGet, "bdq", operator, ""); code != http.StatusOK || a.Name != "" || a.Host != "" { |
| + t.Errorf("show after remove = %d %s, want no name", code, out) |
| + } |
| + if got := serial(t, fx); got != 2 { |
| + t.Errorf("serial after set and remove = %d, want 2", got) |
| + } |
| + // Remove again: nothing to remove, nothing written. |
| + if code, a, out := sdReq(t, fx, http.MethodDelete, "bdq", operator, ""); code != http.StatusOK || a.Name != "" { |
| + t.Errorf("second remove = %d %s, want 200 with no name", code, out) |
| + } |
| + if got := serial(t, fx); got != 2 { |
| + t.Errorf("serial after a remove with nothing to remove = %d, want 2", got) |
| + } |
| + v := nsView(t, fx.srv) |
| + if _, ok := v.byName["harbour"]; ok || len(v.unread) != 0 || v.retired["harbour"].Account != "bdq" { |
| + t.Fatalf("view = %+v, want harbour retired by bdq, not held, nothing unread", v) |
| + } |
| + want := fmt.Sprintf(subdomainCreateUnassignedCopy, "harbour."+nsApps) |
| + for _, acct := range []string{"bdq", "bcd", fx.sqid} { |
| + host := "x.harbour." + nsApps |
| + if !fx.srv.createRefusal(v, host, acct, createNewHost) { |
| + t.Errorf("%s may create %s under the retired name", acct, host) |
| + } |
| + if ref := fx.srv.createDoorRefusal(v, host, acct); ref == nil || ref.Msg != want { |
| + t.Errorf("create refusal for %s = %+v, want %q", acct, ref, want) |
| + } |
| + } |
| + if got := canonicalHostIn(v, nsApps, "shop.bdq."+nsApps); got != "shop.bdq."+nsApps { |
| + t.Errorf("canonical host of bdq's app after remove = %q, want the sqid host", got) |
| + } |
| + // A host of bdq's under the retired name (a direct bucket write) does |
| + // not make harbour a label to assign in the inventory. |
| + nsPutClaim(t, fx, domains.Claim{Schema: 1, Host: "x.harbour." + nsApps, Account: "bdq", App: "web", CreatedAt: sdT0.Format(time.RFC3339)}) |
| + labels, err := fx.srv.subdomainInventory(context.Background()) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + for _, l := range labels { |
| + if l.Label == "harbour" { |
| + t.Errorf("the inventory lists the retired harbour: %+v", l) |
| + } |
| + } |
| +} |
| + |
| +// TestSubdomainRetiredSameAccountRetakes: bdq sets harbour again at once: |
| +// the retirement is cleared, the claim is bdq's in force, and the serial |
| +// moves. |
| +func TestSubdomainRetiredSameAccountRetakes(t *testing.T) { |
| + fx, operator, clock := sdRetireFixture(t, "code-sd-retake") |
| + clock.Set(sdT0.Add(time.Minute)) |
| + want := sdAnswer{Account: "bdq", Name: "harbour", Host: "harbour." + nsApps, Foreign: []sdHost{}} |
| + if code, a, out := sdReq(t, fx, http.MethodPost, "bdq", operator, `{"name":"harbour"}`); code != http.StatusOK || !reflect.DeepEqual(a, want) { |
| + t.Fatalf("bdq sets harbour again = %d %s, want 200 %+v", code, out, want) |
| + } |
| + c := nsClaimAt(t, fx, nsHarbourHost) |
| + if c == nil || c.Account != "bdq" || c.RetiredAt != "" || c.CreatedAt != "2026-11-12T09:01:30Z" { |
| + t.Errorf("claim after retaking = %+v, want bdq's in force, stamped now", c) |
| + } |
| + if got := serial(t, fx); got != 3 { |
| + t.Errorf("serial after set, remove and set = %d, want 3", got) |
| + } |
| + if code, a, out := sdReq(t, fx, http.MethodGet, "bdq", operator, ""); code != http.StatusOK || a.Name != "harbour" { |
| + t.Errorf("show after retaking = %d %s, want harbour", code, out) |
| + } |
| +} |
| + |
| +// TestSubdomainRetiredCooldownRefusesOthers: before the hour, bcd is |
| +// refused with retired_cooldown naming when the name becomes available (to |
| +// the minute, never early), and nothing is written. The refusal comes from |
| +// the view and, when the view has not seen the retirement, from the fresh |
| +// read before the write. |
| +func TestSubdomainRetiredCooldownRefusesOthers(t *testing.T) { |
| + fx, operator, clock := sdRetireFixture(t, "code-sd-cooldown") |
| + before := nsClaimAt(t, fx, nsHarbourHost) |
| + for _, at := range []time.Time{sdT0, sdT0.Add(domains.NamespaceCooldown - time.Second)} { |
| + clock.Set(at) |
| + code, a, out := sdReq(t, fx, http.MethodPost, "bcd", operator, `{"name":"harbour"}`) |
| + if code != http.StatusConflict || a.Code != string(domains.SubdomainRetiredCooldown) || a.Error != sdCooldownCopy { |
| + t.Errorf("bcd sets harbour at %s = %d %s, want 409 %q", at, code, out, sdCooldownCopy) |
| + } |
| + } |
| + // The view has not seen the claim: the write step reads it and refuses. |
| + fx.srv.cfg.Releases = &nsHideKeyStore{Store: fx.rel, key: domains.ClaimKey(nsHarbourHost)} |
| + fx.srv.claims.Reset() |
| + if code, a, out := sdReq(t, fx, http.MethodPost, "bcd", operator, `{"name":"harbour"}`); code != http.StatusConflict || a.Error != sdCooldownCopy { |
| + t.Errorf("bcd sets harbour, view stale = %d %s, want 409 %q", code, out, sdCooldownCopy) |
| + } |
| + if c := nsClaimAt(t, fx, nsHarbourHost); !reflect.DeepEqual(c, before) { |
| + t.Errorf("a refused set changed the claim: %+v, was %+v", c, before) |
| + } |
| + if got := serial(t, fx); got != 2 { |
| + t.Errorf("serial after refused sets = %d, want 2", got) |
| + } |
| +} |
| + |
| +// TestRetiredCooldownShown: the time named is rounded up to the minute. |
| +func TestSubdomainRetiredCooldownShown(t *testing.T) { |
| + for at, want := range map[time.Time]string{ |
| + time.Date(2026, 11, 12, 10, 0, 0, 0, time.UTC): "2026-11-12 10:00 UTC", |
| + time.Date(2026, 11, 12, 10, 0, 1, 0, time.UTC): "2026-11-12 10:01 UTC", |
| + time.Date(2026, 11, 12, 23, 59, 30, 0, time.UTC): "2026-11-13 00:00 UTC", |
| + time.Date(2026, 11, 12, 11, 0, 0, 0, time.FixedZone("x", 3600)): "2026-11-12 10:00 UTC", |
| + } { |
| + if got := retiredCooldownShown(at); got != want { |
| + t.Errorf("retiredCooldownShown(%s) = %q, want %q", at, got, want) |
| + } |
| + } |
| +} |
| + |
| +// TestSubdomainRetiredReplacedAfterHour: from the hour on, bcd gets the |
| +// name: a fresh claim of bcd's replaces the retired one. |
| +func TestSubdomainRetiredReplacedAfterHour(t *testing.T) { |
| + fx, operator, clock := sdRetireFixture(t, "code-sd-replace") |
| + clock.Set(sdT0.Add(domains.NamespaceCooldown)) |
| + want := sdAnswer{Account: "bcd", Name: "harbour", Host: "harbour." + nsApps, Foreign: []sdHost{}} |
| + if code, a, out := sdReq(t, fx, http.MethodPost, "bcd", operator, `{"name":"harbour"}`); code != http.StatusOK || !reflect.DeepEqual(a, want) { |
| + t.Fatalf("bcd sets harbour after the hour = %d %s, want 200 %+v", code, out, want) |
| + } |
| + if c := nsClaimAt(t, fx, nsHarbourHost); c == nil || c.Account != "bcd" || c.RetiredAt != "" || c.CreatedAt != "2026-11-12T10:00:30Z" { |
| + t.Errorf("claim after replace = %+v, want bcd's fresh claim", c) |
| + } |
| + if code, a, out := sdReq(t, fx, http.MethodGet, "bdq", operator, ""); code != http.StatusOK || a.Name != "" { |
| + t.Errorf("show for bdq = %d %s, want no name", code, out) |
| + } |
| + if code, a, out := sdReq(t, fx, http.MethodPost, "bdq", operator, `{"name":"harbour"}`); code != http.StatusConflict || a.Code != string(domains.SubdomainHeld) { |
| + t.Errorf("bdq sets harbour after bcd took it = %d %s, want 409 held", code, out) |
| + } |
| +} |
| + |
| +// TestSubdomainRetiredOldHolderOccupantRefuses: after the hour, anything of |
| +// bdq's still under harbour (a claim and a record, as a late write would |
| +// leave them) refuses bcd as occupied, listing them, and the retired claim |
| +// stays bdq's. |
| +func TestSubdomainRetiredOldHolderOccupantRefuses(t *testing.T) { |
| + fx, operator, clock := sdRetireFixture(t, "code-sd-replace-occupied") |
| + late := domains.Claim{Schema: 1, Host: "x.api.harbour." + nsApps, Account: "bdq", App: "web", CreatedAt: sdT0.Format(time.RFC3339)} |
| + nsPutClaim(t, fx, late) |
| + clock.Set(sdT0.Add(2 * domains.NamespaceCooldown)) |
| + code, a, out := sdReq(t, fx, http.MethodPost, "bcd", operator, `{"name":"harbour"}`) |
| + want := fmt.Sprintf(subdomainRefuseOccupiedCopy, "harbour."+nsApps) |
| + if code != http.StatusConflict || a.Code != string(domains.SubdomainOccupied) || a.Error != want { |
| + t.Fatalf("bcd sets harbour over bdq's late host = %d %s, want 409 %q", code, out, want) |
| + } |
| + if wantHosts := []sdHost{{Host: late.Host, Account: "bdq", App: "web", Source: occupantClaim}}; !reflect.DeepEqual(a.Hosts, wantHosts) { |
| + t.Errorf("occupied lists %+v, want %+v", a.Hosts, wantHosts) |
| + } |
| + if c := nsClaimAt(t, fx, nsHarbourHost); c == nil || c.Account != "bdq" || c.RetiredAt == "" { |
| + t.Errorf("claim after a refused replace = %+v, want bdq's, retired", c) |
| + } |
| +} |
| + |
| +// TestSubdomainRetiredRollbacks: when set's re-read finds a conflict, an |
| +// unretire is undone by putting the retirement back (retired_at as it was) |
| +// and a replace by putting the old holder's retired claim back. |
| +func TestSubdomainRetiredRollbacks(t *testing.T) { |
| + for _, c := range []struct { |
| + name, account string |
| + at time.Duration |
| + }{{"unretire", "bdq", time.Minute}, {"replace", "bcd", domains.NamespaceCooldown}} { |
| + t.Run(c.name, func(t *testing.T) { |
| + fx, operator, clock := sdRetireFixture(t, "code-sd-rb-"+c.name) |
| + retired := nsClaimAt(t, fx, nsHarbourHost) |
| + clock.Set(sdT0.Add(c.at)) |
| + foreign := domains.Claim{Schema: 1, Host: "x.harbour." + nsApps, Account: fx.sqid, App: "web", CreatedAt: sdT0.Format(time.RFC3339)} |
| + fx.srv.cfg.Releases = &nsRaceStore{Store: fx.rel, key: domains.ClaimKey(nsHarbourHost), after: func() { nsPutClaim(t, fx, foreign) }} |
| + fx.srv.claims.Reset() |
| + code, a, out := sdReq(t, fx, http.MethodPost, c.account, operator, `{"name":"harbour"}`) |
| + if code != http.StatusConflict || a.Code != string(domains.SubdomainRolledBack) { |
| + t.Fatalf("%s under a race = %d %s, want 409 rolled_back", c.name, code, out) |
| + } |
| + if got := nsClaimAt(t, fx, nsHarbourHost); !reflect.DeepEqual(got, retired) { |
| + t.Errorf("claim after the rollback = %+v, want the retired claim back %+v", got, retired) |
| + } |
| + }) |
| + } |
| +} |
| + |
| +// nsSuccessorStore is the F3 interleaving (Astra final review P2-3): set |
| +// creates the claim; the re-read's record walk fails, so set rolls back; |
| +// and between that failed re-read and the rollback, another console removes |
| +// and sets the same name again. The successor lands on the first read of |
| +// key after the failed walk, the rollback's own read. |
| +type nsSuccessorStore struct { |
| + release.Store |
| + key string |
| + successor func() |
| + mu sync.Mutex |
| + wrote bool // set's Put of key landed |
| + armed bool // the re-read's walk failed |
| + fired bool |
| +} |
| + |
| +func (s *nsSuccessorStore) Put(ctx context.Context, key string, body []byte, ifMatch string) (string, error) { |
| + etag, err := s.Store.Put(ctx, key, body, ifMatch) |
| + if err == nil && key == s.key { |
| + s.mu.Lock() |
| + s.wrote = true |
| + s.mu.Unlock() |
| + } |
| + return etag, err |
| +} |
| + |
| +func (s *nsSuccessorStore) List(ctx context.Context, prefix string) ([]string, error) { |
| + s.mu.Lock() |
| + fail := s.wrote && !s.armed && prefix == "accounts/" |
| + if fail { |
| + s.armed = true |
| + } |
| + s.mu.Unlock() |
| + if fail { |
| + return nil, errors.New("nsSuccessorStore: record walk refused") |
| + } |
| + return s.Store.List(ctx, prefix) |
| +} |
| + |
| +func (s *nsSuccessorStore) Get(ctx context.Context, key string) ([]byte, string, error) { |
| + s.mu.Lock() |
| + fire := key == s.key && s.armed && !s.fired |
| + if fire { |
| + s.fired = true |
| + } |
| + s.mu.Unlock() |
| + if fire { |
| + s.successor() |
| + } |
| + return s.Store.Get(ctx, key) |
| +} |
| + |
| +func (s *nsSuccessorStore) DeleteIfMatch(ctx context.Context, key, etag string) error { |
| + return release.DeleteIfMatch(ctx, s.Store, key, etag) |
| +} |
| + |
| +// TestSubdomainRollbackSparesASuccessor: set's rollback is conditioned on |
| +// the ETag its own create answered, so it never deletes the successor |
| +// another console wrote after the failed re-read. The successor stands and |
| +// the door says the name is still held. |
| +func TestSubdomainRollbackSparesASuccessor(t *testing.T) { |
| + fx, operator, _ := sdFixture(t, "code-sd-successor") |
| + ctx := context.Background() |
| + var successorETag string |
| + store := &nsSuccessorStore{Store: fx.rel, key: domains.ClaimKey(nsHarbourHost), successor: func() { |
| + // Another console: remove (retire), then set again (unretire). |
| + _, etag, err := domains.GetClaimETag(ctx, fx.rel, nsHarbourHost) |
| + if err != nil { |
| + t.Error(err) |
| + return |
| + } |
| + retired, ok, err := domains.RetireAccountNamespace(ctx, fx.rel, "harbour", nsApps, "bdq", etag, time.Now().Add(-time.Minute)) |
| + if err != nil || !ok { |
| + t.Errorf("successor retire = %v, %v", ok, err) |
| + return |
| + } |
| + successorETag, ok, err = domains.UnretireAccountNamespace(ctx, fx.rel, "harbour", nsApps, "bdq", retired, time.Now().Add(time.Hour), "other@example.com") |
| + if err != nil || !ok { |
| + t.Errorf("successor unretire = %v, %v", ok, err) |
| + } |
| + }} |
| + fx.srv.cfg.Releases = store |
| + fx.srv.claims.Reset() |
| + code, a, out := sdReq(t, fx, http.MethodPost, "bdq", operator, `{"name":"harbour"}`) |
| + if !store.fired { |
| + t.Fatalf("the successor never landed (set = %d %s); the test did not reach the rollback", code, out) |
| + } |
| + want := fmt.Sprintf(subdomainRefuseRollbackFailedCopy, "harbour", "bdq", "bdq") |
| + if code != http.StatusInternalServerError || a.Code != string(subdomainCodeRollbackFailed) || a.Error != want { |
| + t.Errorf("set whose rollback meets a successor = %d %s, want 500 %q", code, out, want) |
| + } |
| + c, etag, err := domains.GetClaimETag(ctx, fx.rel, nsHarbourHost) |
| + if err != nil || etag != successorETag || c.Account != "bdq" || c.RetiredAt != "" || c.CreatedBy != "other@example.com" { |
| + t.Errorf("after the rollback: %+v at %q (%v), want the successor at %q", c, etag, err, successorETag) |
| + } |
| +} |
| + |
| +// nsViewSignalStore signals the first claim listing after it is armed: the |
| +// moment a door has loaded its namespace view. |
| +type nsViewSignalStore struct { |
| + release.Store |
| + mu sync.Mutex |
| + armed bool |
| + listed chan struct{} |
| +} |
| + |
| +func (s *nsViewSignalStore) List(ctx context.Context, prefix string) ([]string, error) { |
| + keys, err := s.Store.List(ctx, prefix) |
| + s.mu.Lock() |
| + if s.armed && prefix == domains.ClaimsPrefix { |
| + s.armed = false |
| + close(s.listed) |
| + } |
| + s.mu.Unlock() |
| + return keys, err |
| +} |
| + |
| +func (s *nsViewSignalStore) DeleteIfMatch(ctx context.Context, key, etag string) error { |
| + return release.DeleteIfMatch(ctx, s.Store, key, etag) |
| +} |
| + |
| +// TestSubdomainRetiredInterleaving is Astra's final-review P1 ordering at |
| +// the placements door, which loads its view before it reads its body: bab |
| +// holds harbour; bab's PUT for x.api.harbour.<apps> is admitted and waits |
| +// on its body; bab's name is removed. Before the hour bcd cannot have it |
| +// (with the name freed at once, bcd would get it here, with bab's write |
| +// still to land). bab's late write then lands under the retired name; from |
| +// the hour on, bcd is refused as occupied while it sits there. |
| +func TestSubdomainRetiredInterleaving(t *testing.T) { |
| + w := newRegionalWorld(t, "code-sd-interleave") |
| + fx := w.fx |
| + fx.srv.cfg.AppsDomain = nsApps |
| + nsAccount(t, fx, 49, "bcd") |
| + clock := &sdClock{now: sdT0} |
| + fx.srv.subdomainNow = clock.Now |
| + if code, _, out := sdReq(t, fx, http.MethodPost, fx.sqid, w.token, `{"name":"harbour"}`); code != http.StatusOK { |
| + t.Fatalf("set harbour for bab = %d %s", code, out) |
| + } |
| + store := &nsViewSignalStore{Store: fx.rel, armed: true, listed: make(chan struct{})} |
| + fx.srv.cfg.Releases = store |
| + fx.srv.claims.Reset() |
| + |
| + host := "x.api.harbour." + nsApps |
| + body, err := json.Marshal(rdBoth()) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + pr, pw := io.Pipe() |
| + // A test that stops early must not leave the PUT waiting on its body: |
| + // the test server's own cleanup would wait on it forever. |
| + t.Cleanup(func() { pw.CloseWithError(errors.New("test over")) }) |
| + req, err := http.NewRequest(http.MethodPut, placementsURL(fx, "shop", host), pr) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + req.Header.Set("Content-Type", "application/json") |
| + req.Header.Set("Authorization", "Bearer "+w.token) |
| + type answer struct { |
| + code int |
| + out string |
| + err error |
| + } |
| + done := make(chan answer, 1) |
| + go func() { |
| + res, err := http.DefaultClient.Do(req) |
| + if err != nil { |
| + done <- answer{err: err} |
| + return |
| + } |
| + defer res.Body.Close() |
| + out, err := io.ReadAll(res.Body) |
| + done <- answer{code: res.StatusCode, out: string(out), err: err} |
| + }() |
| + select { |
| + case <-store.listed: |
| + case a := <-done: |
| + t.Fatalf("the PUT answered before its body was sent: %d %s %v", a.code, a.out, a.err) |
| + case <-time.After(10 * time.Second): |
| + t.Fatal("the PUT never loaded its namespace view") |
| + } |
| + |
| + // Admitted. Now the remove, and bcd at once. |
| + if code, a, out := sdReq(t, fx, http.MethodDelete, fx.sqid, w.token, ""); code != http.StatusOK || a.Name != "" { |
| + t.Fatalf("remove harbour from bab = %d %s", code, out) |
| + } |
| + if code, a, out := sdReq(t, fx, http.MethodPost, "bcd", w.token, `{"name":"harbour"}`); code != http.StatusConflict || a.Error != sdCooldownCopy { |
| + t.Errorf("bcd sets harbour right after the remove = %d %s, want 409 %q", code, out, sdCooldownCopy) |
| + } |
| + |
| + // The late write lands under the retired name. |
| + if _, err := pw.Write(body); err != nil { |
| + t.Fatal(err) |
| + } |
| + pw.Close() |
| + a := <-done |
| + if a.err != nil || a.code != http.StatusOK { |
| + t.Fatalf("bab's admitted PUT = %d %s %v, want 200 (the late write)", a.code, a.out, a.err) |
| + } |
| + if c, err := domains.GetClaim(context.Background(), fx.rel, host); err != nil || c.Account != fx.sqid { |
| + t.Fatalf("the late write = %+v, %v", c, err) |
| + } |
| + |
| + clock.Set(sdT0.Add(domains.NamespaceCooldown)) |
| + code, ans, out := sdReq(t, fx, http.MethodPost, "bcd", w.token, `{"name":"harbour"}`) |
| + if code != http.StatusConflict || ans.Code != string(domains.SubdomainOccupied) || len(ans.Hosts) != 1 || ans.Hosts[0].Host != host { |
| + t.Errorf("bcd sets harbour after the hour, bab's host under it = %d %s, want 409 occupied listing %s", code, out, host) |
| + } |
| + if c := nsClaimAt(t, fx, nsHarbourHost); c == nil || c.Account != fx.sqid || c.RetiredAt == "" { |
| + t.Errorf("claim = %+v, want bab's, retired", c) |
| + } |
| + if strings.Contains(out, `"name":"harbour"`) { |
| + t.Errorf("bcd was answered the name: %s", out) |
| + } |
| +} |
diff --git a/internal/console/subdomains.go b/internal/console/subdomains.go| index 424c745c5..10c451f8c 100644 |
| --- a/internal/console/subdomains.go |
| +++ b/internal/console/subdomains.go |
| @@ -43,7 +43,11 @@ import ( |
| // account's sqid, byAccount a sqid to its name (domains.AccountNamespaces), |
| // and unread holds the names whose namespace claim was listed but could not |
| // be trusted this read, with unreadBy the account each such claim names |
| -// when anything read said ("" when nothing did). The zero value holds no |
| +// when anything read said ("" when nothing did). retired holds the names |
| +// whose claim a remove retired (spec §4.3, rev 4): such a name is nobody's |
| +// for create purposes (labelOwner, so every create under it is refused, |
| +// its old holder's included), the account that held it has no name, and |
| +// only the assignment door looks at it again. The zero value holds no |
| // names, which is what a door that decides from sqids alone (detach, paths) |
| // passes. |
| type namespaceView struct { |
| @@ -51,6 +55,7 @@ type namespaceView struct { |
| byAccount map[string]string |
| unread map[string]bool |
| unreadBy map[string]string |
| + retired map[string]domains.RetiredNamespace |
| } |
| // mayHoldUnread reports whether account may hold a name this read could not |
| @@ -96,8 +101,10 @@ func (v namespaceView) labelOwner(label string) (owner string, known bool) { |
| // trust (pending, carrying anything but the namespace, an invalid account) |
| // counts as unread rather than as no name, and so does any claim at a |
| // namespace host whose body did not read (it may be a namespace claim). A |
| -// listing that fails is an error; the caller decides what that means (a |
| -// door refuses, a display site shows hosts unchanged). |
| +// retired claim the rules trust (domains.RetiredAccountNamespaces) is |
| +// neither: it goes to retired. A listing that fails is an error; the |
| +// caller decides what that means (a door refuses, a display site shows |
| +// hosts unchanged). |
| func (s *Server) loadNamespaces(ctx context.Context) (namespaceView, error) { |
| domain := s.appsDomain() |
| if domain == "" { |
| @@ -124,6 +131,7 @@ func (s *Server) loadNamespaces(ctx context.Context) (namespaceView, error) { |
| } |
| v := namespaceView{unread: map[string]bool{}, unreadBy: map[string]string{}} |
| v.byName, v.byAccount = domains.AccountNamespaces(kept, domain) |
| + v.retired = domains.RetiredAccountNamespaces(kept, domain) |
| for _, u := range unread { |
| if name, ok := domains.ParseNamespaceHost(u.Host, domain); ok && nameable(name) { |
| v.unread[name] = true |
| @@ -135,7 +143,8 @@ func (s *Server) loadNamespaces(ctx context.Context) (namespaceView, error) { |
| continue |
| } |
| if name, ok := domains.ParseNamespaceHost(c.Host, domain); ok { |
| - if _, held := v.byName[name]; !held { |
| + _, held := v.byName[name] |
| + if _, retired := v.retired[name]; !held && !retired { |
| v.unread[name] = true |
| v.unreadBy[name] = c.Account |
| sayUntrustedNamespaceOnce(c) |
| @@ -573,8 +582,11 @@ func (s *Server) subdomainInfrastructure(name string) bool { |
| // subdomainRefusal applies every §4.2 rule to account taking name, in the |
| // spec's order after the store-free rules: a held sqid (live or deleted), |
| -// another account's name or a name whose claim did not read, the |
| -// deployment's own hosts, an account that already holds another name, and |
| +// another account's name or a name whose claim did not read, a name another |
| +// account removed less than domains.NamespaceCooldown ago |
| +// (SubdomainRetiredCooldown, §4.3 rev 4; the caller names the time from |
| +// v.retired), the deployment's own hosts, an account that already holds |
| +// another name, and |
| // anything another account has at or under the name (SubdomainOccupied, |
| // with those occupants). A read that fails is SubdomainUnreadable. "" means |
| // allowed; the account's own name asked again is allowed (the door makes it |
| @@ -609,6 +621,9 @@ func (s *Server) subdomainRefusal(ctx context.Context, v namespaceView, account, |
| case owner != "" && owner != account && v.byName[name] != "": |
| return domains.SubdomainHeld, nil |
| } |
| + if r, ok := v.retired[name]; ok && r.Account != account && s.subdomainClock().Before(r.AvailableAt()) { |
| + return domains.SubdomainRetiredCooldown, nil |
| + } |
| if s.subdomainInfrastructure(name) { |
| return domains.SubdomainInfrastructure, nil |
| } |
diff --git a/internal/domains/namespace.go b/internal/domains/namespace.go| index 1930566bc..163c9956f 100644 |
| --- a/internal/domains/namespace.go |
| +++ b/internal/domains/namespace.go |
| @@ -50,6 +50,9 @@ const ( |
| // SubdomainRolledBack: the assignment door's re-read found a conflict |
| // after its write and removed its own claim again. |
| SubdomainRolledBack SubdomainRefusal = "rolled_back" |
| + // SubdomainRetiredCooldown: another account removed the name less than |
| + // NamespaceCooldown ago (spec §4.3, rev 4). |
| + SubdomainRetiredCooldown SubdomainRefusal = "retired_cooldown" |
| ) |
| // sqidShapedFloor is the length from which a frozen-alphabet word is allowed |