console: rootfs readiness fails closed on unread heartbeats, and the create door reads a channel's head once (Astra r1 P2-1, P2-2)
rootfsReadyRefusal loads heartbeats with LoadBoxesComplete in both branches: a heartbeat it could not read is incomplete evidence, C07 at 503, even beside a readable ready box. The create door reads the channel's pointer and manifest once (readChannelHead) and shares the result between staticHeadRefusal and rootfsHeadFrom. A head it could not read still falls back to the app's history, fail closed; a read head is never read again, so a later failing read cannot refuse a binary create the first read decided.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
3 files changed,
+155
−29
internal/console/apiinstances.go+19 −16internal/console/rootfs.go+45 −13internal/console/rootfs_test.go+91 −0
diff --git a/internal/console/apiinstances.go b/internal/console/apiinstances.go| index d1c75c881..ac379f94b 100644 |
| --- a/internal/console/apiinstances.go |
| +++ b/internal/console/apiinstances.go |
| @@ -1626,7 +1626,14 @@ func (s *Server) handleAPIInstanceCreate(w http.ResponseWriter, r *http.Request) |
| jsonError(w, http.StatusBadRequest, "invalid channel name") |
| return |
| } |
| - if ref := s.staticHeadRefusal(ctx, acct.Sqid, app, channel, strings.TrimSpace(req.Kind), host); ref != nil { |
| + // The channel's head, read once (pointer, then manifest) for the two |
| + // checks that ask it: an exec-shaped create is the only one either |
| + // asks about. nil when it was not read or could not be. |
| + var chHead *channelHead |
| + if kind := strings.TrimSpace(req.Kind); kind == "" || kind == "instance" { |
| + chHead = s.readChannelHead(ctx, acct.Sqid, app, channel) |
| + } |
| + if ref := staticHeadRefusal(chHead, app, channel, strings.TrimSpace(req.Kind), host); ref != nil { |
| jsonError(w, ref.Status, ref.Msg) |
| return |
| } |
| @@ -1640,7 +1647,7 @@ func (s *Server) handleAPIInstanceCreate(w http.ResponseWriter, r *http.Request) |
| var head rootfsHead |
| if (provision.Record{Kind: strings.TrimSpace(req.Kind), Addrs: req.Addrs}).DerivesPaths() { |
| var ref *doorRefusal |
| - if head, ref = s.rootfsHeadFor(ctx, acct.Sqid, app, channel); ref != nil { |
| + if head, ref = s.rootfsHeadFrom(ctx, acct.Sqid, app, chHead); ref != nil { |
| jsonError(w, ref.Status, ref.Msg) |
| return |
| } |
| @@ -2130,26 +2137,22 @@ func (s *Server) instanceChannel(ctx context.Context, sqid, app string, m *provi |
| // is the default, which IS "instance": that is precisely the request the |
| // reporter made. |
| // |
| -// The check spends two bucket reads on a create, and only a create. Every |
| -// failure to read degrades to nil — this is a guard against a shape mistake, |
| -// not a rail against a hostile caller, and a bucket hiccup must not stop an |
| -// ordinary member declaring an ordinary instance. A brand-new app with |
| -// nothing promoted yet reads as nil too, which is the common first-instance |
| -// case and stays exactly as fast as it was. |
| -func (s *Server) staticHeadRefusal(ctx context.Context, sqid, app, channel, kind, host string) *doorRefusal { |
| +// The check reads nothing itself: head is the create door's one read of |
| +// the channel's pointer and manifest (readChannelHead), shared with the |
| +// rootfs check. A head that could not be read (nil) degrades to nil here. |
| +// This is a guard against a shape mistake, not a rail against a hostile |
| +// caller, and a bucket hiccup must not stop an ordinary member declaring an |
| +// ordinary instance. A brand-new app with nothing promoted yet reads as nil |
| +// too, which is the common first-instance case. |
| +func staticHeadRefusal(head *channelHead, app, channel, kind, host string) *doorRefusal { |
| if kind != "" && kind != "instance" { |
| return nil |
| } |
| - ptr, err := release.Channel(ctx, s.cfg.Releases, s.cfg.ReleasePub, sqid, app, channel) |
| - if err != nil || ptr == nil || ptr.Version == "" { |
| - return nil |
| - } |
| - m, err := release.GetManifest(ctx, s.cfg.Releases, sqid, app, ptr.Version) |
| - if err != nil || m == nil || m.Kind != "static" { |
| + if head == nil || head.Manifest.Kind != "static" { |
| return nil |
| } |
| return &doorRefusal{http.StatusUnprocessableEntity, |
| - "channel " + channel + " serves a static release (" + ptr.Version + "), and an instance runs a process — " + |
| + "channel " + channel + " serves a static release (" + head.Version + "), and an instance runs a process — " + |
| "a static site is served straight off the channel pointer, so it needs no instance at all. " + |
| "Pass --kind static, or let one command do the whole thing: carlos deploy --app " + app + |
| " --kind static --host " + host + " <site-dir>"} |
diff --git a/internal/console/rootfs.go b/internal/console/rootfs.go| index f5773c02b..da630d8bd 100644 |
| --- a/internal/console/rootfs.go |
| +++ b/internal/console/rootfs.go |
| @@ -63,16 +63,47 @@ type rootfsHead struct { |
| // A binary or static app answers the zero rootfsHead, wherever the answer |
| // came from: only a rootfs head carries a version and hibernation. |
| func (s *Server) rootfsHeadFor(ctx context.Context, sqid, app, channel string) (rootfsHead, *doorRefusal) { |
| + return s.rootfsHeadFrom(ctx, sqid, app, s.readChannelHead(ctx, sqid, app, channel)) |
| +} |
| + |
| +// channelHead is a channel's head as one read found it: the version its |
| +// verified pointer names and that version's manifest, both read. |
| +type channelHead struct { |
| + Version string |
| + Manifest *release.Manifest |
| +} |
| + |
| +// readChannelHead reads the channel's pointer and the manifest it names, |
| +// once. nil when either read fails or the pointer names nothing: the |
| +// callers decide what an unread head means (staticHeadRefusal passes it, |
| +// rootfsHeadFrom falls back to the app's history). |
| +func (s *Server) readChannelHead(ctx context.Context, sqid, app, channel string) *channelHead { |
| + if s.cfg.Releases == nil { |
| + return nil |
| + } |
| + ptr, err := release.Channel(ctx, s.cfg.Releases, s.cfg.ReleasePub, sqid, app, channel) |
| + if err != nil || ptr == nil || ptr.Version == "" { |
| + return nil |
| + } |
| + m, err := release.GetManifest(ctx, s.cfg.Releases, sqid, app, ptr.Version) |
| + if err != nil || m == nil { |
| + return nil |
| + } |
| + return &channelHead{Version: ptr.Version, Manifest: m} |
| +} |
| + |
| +// rootfsHeadFrom is rootfsHeadFor on a head already read (Astra r1 P2-2: |
| +// the create door reads it once, for this and staticHeadRefusal). A read |
| +// head answers alone; a nil one falls back to the history, fail closed. |
| +func (s *Server) rootfsHeadFrom(ctx context.Context, sqid, app string, head *channelHead) (rootfsHead, *doorRefusal) { |
| if s.cfg.Releases == nil { |
| return rootfsHead{}, &doorRefusal{http.StatusBadGateway, rootfsReleasesUnreadableMsg} |
| } |
| - if ptr, err := release.Channel(ctx, s.cfg.Releases, s.cfg.ReleasePub, sqid, app, channel); err == nil && ptr != nil && ptr.Version != "" { |
| - if m, err := release.GetManifest(ctx, s.cfg.Releases, sqid, app, ptr.Version); err == nil && m != nil { |
| - if m.Kind != release.KindRootfs { |
| - return rootfsHead{}, nil |
| - } |
| - return rootfsHead{Rootfs: true, Hibernate: m.Hibernate, Version: ptr.Version}, nil |
| + if head != nil { |
| + if head.Manifest.Kind != release.KindRootfs { |
| + return rootfsHead{}, nil |
| } |
| + return rootfsHead{Rootfs: true, Hibernate: head.Manifest.Hibernate, Version: head.Version}, nil |
| } |
| info, err := release.EstablishedKind(ctx, s.cfg.Releases, sqid, app) |
| switch { |
| @@ -124,7 +155,8 @@ func (s *Server) rootfsPlacementRefusal(reg consoleRegistry, acct *Account, app, |
| // still counts; the box's own refusal covers it. |
| // |
| // None ready: 409 C05 naming the fleet, or C06 naming the region. A read |
| -// that fails: 503 C07. |
| +// that fails, or any heartbeat that could not be read (LoadBoxesComplete: |
| +// incomplete evidence is neither ready nor not ready): 503 C07. |
| func (s *Server) rootfsReadyRefusal(ctx context.Context, reg consoleRegistry, placed, region string) *doorRefusal { |
| unreadable := &doorRefusal{http.StatusServiceUnavailable, rootfsReadyUnreadableMsg} |
| if s.cfg.Releases == nil { |
| @@ -144,9 +176,9 @@ func (s *Server) rootfsReadyRefusal(ctx context.Context, reg consoleRegistry, pl |
| active[b.Label] = true |
| } |
| } |
| - hbs, err := status.LoadBoxes(ctx, release.NewPrefixStore(s.cfg.Releases, "fleets/"+fleet+"/")) |
| - if err != nil { |
| - log.Printf("console: rootfs ready: load fleet %s's heartbeats: %v", fleet, err) |
| + hbs, complete, err := status.LoadBoxesComplete(ctx, release.NewPrefixStore(s.cfg.Releases, "fleets/"+fleet+"/")) |
| + if err != nil || !complete { |
| + log.Printf("console: rootfs ready: load fleet %s's heartbeats: complete %v, %v", fleet, complete, err) |
| return unreadable |
| } |
| for _, b := range hbs { |
| @@ -156,9 +188,9 @@ func (s *Server) rootfsReadyRefusal(ctx context.Context, reg consoleRegistry, pl |
| } |
| return &doorRefusal{http.StatusConflict, fmt.Sprintf(rootfsFleetNotReadyMsg, fleet)} |
| } |
| - hbs, err := status.LoadBoxes(ctx, s.cfg.Releases) |
| - if err != nil { |
| - log.Printf("console: rootfs ready: load the platform's heartbeats: %v", err) |
| + hbs, complete, err := status.LoadBoxesComplete(ctx, s.cfg.Releases) |
| + if err != nil || !complete { |
| + log.Printf("console: rootfs ready: load the platform's heartbeats: complete %v, %v", complete, err) |
| return unreadable |
| } |
| for _, b := range hbs { |
diff --git a/internal/console/rootfs_test.go b/internal/console/rootfs_test.go| index 76b97cce4..c0178adb1 100644 |
| --- a/internal/console/rootfs_test.go |
| +++ b/internal/console/rootfs_test.go |
| @@ -730,3 +730,94 @@ func TestSeedCreateRechecksTheFirstKindUnderTheWritersLock(t *testing.T) { |
| t.Fatalf("retry = %d %v; want 422 C09", code, out) |
| } |
| } |
| + |
| +// Astra r1 P2-1: a heartbeat that could not be read is incomplete |
| +// evidence, never ready and never not ready: C07 at 503, on a fleet and on |
| +// a single-tenant region, even beside a readable ready box. |
| +func TestRootfsReadyRefusesOnAnUnreadableHeartbeat(t *testing.T) { |
| + fx, reg := rootfsDoorWorld(t, "code-rfs-ready-hb") |
| + base := fx.srv.cfg.Releases |
| + now := time.Now() |
| + registerFleetBox(t, reg, "home", "pi-1") |
| + registerFleetBox(t, reg, "home", "pi-2") |
| + writeFleetBox(t, fx, "home", "pi-2", "", false, now) |
| + writeFleetBox(t, fx, "", "box-2", "", false, now) |
| + check := func(what, placed, failKey string) { |
| + t.Helper() |
| + fx.srv.cfg.Releases = &failGetStore{Store: base, key: failKey} |
| + if ref := fx.srv.rootfsReadyRefusal(context.Background(), reg, placed, ""); refStatus(ref) != http.StatusServiceUnavailable || ref.Msg != rootfsReadyUnreadableMsg { |
| + t.Errorf("%s: %v; want 503 C07", what, ref) |
| + } |
| + } |
| + // Alone: the unreadable box is not "not ready". |
| + check("fleet, alone", "fleet/home", "fleets/home/status/boxes/pi-2.json") |
| + fx.srv.cfg.DeploymentSingleTenant = true |
| + check("region, alone", "", "status/boxes/box-2.json") |
| + // Beside a readable ready box. |
| + writeFleetBox(t, fx, "home", "pi-1", "", true, now) |
| + writeFleetBox(t, fx, "", "box-1", "", true, now) |
| + check("fleet, beside a ready box", "fleet/home", "fleets/home/status/boxes/pi-2.json") |
| + check("region, beside a ready box", "", "status/boxes/box-2.json") |
| + // Every heartbeat read: ready. |
| + fx.srv.cfg.Releases = base |
| + for _, placed := range []string{"fleet/home", ""} { |
| + if ref := fx.srv.rootfsReadyRefusal(context.Background(), reg, placed, ""); ref != nil { |
| + t.Errorf("%q, all read: %v", placed, ref) |
| + } |
| + } |
| +} |
| + |
| +// headReadStore counts Gets of shop's head keys on edge and, once armed, |
| +// fails every later read of them and every listing of shop's releases. |
| +type headReadStore struct { |
| + release.Store |
| + keys map[string]bool |
| + prefix string |
| + gets atomic.Int32 |
| + failAt int32 // fail head reads after this many (0: never) |
| +} |
| + |
| +func (s *headReadStore) Get(ctx context.Context, key string) ([]byte, string, error) { |
| + if s.keys[key] { |
| + if n := s.gets.Add(1); s.failAt > 0 && n > s.failAt { |
| + return nil, "", errors.New("injected head read failure") |
| + } |
| + } |
| + return s.Store.Get(ctx, key) |
| +} |
| + |
| +func (s *headReadStore) List(ctx context.Context, prefix string) ([]string, error) { |
| + if s.failAt > 0 && strings.HasPrefix(prefix, s.prefix) { |
| + return nil, errors.New("injected release listing failure") |
| + } |
| + return s.Store.List(ctx, prefix) |
| +} |
| + |
| +// Astra r1 P2-2: a binary create reads the channel's pointer and manifest |
| +// once, for the static and the rootfs checks together, and a read after |
| +// that one failing does not refuse a create the first read already |
| +// decided. |
| +func TestBinaryCreateReadsTheHeadOnce(t *testing.T) { |
| + for _, failLater := range []bool{false, true} { |
| + t.Run(fmt.Sprintf("failLater=%v", failLater), func(t *testing.T) { |
| + fx, token, _ := rootfsCreateWorld(t, "code-rfs-head-once", rootfsShopManifest()) |
| + shipBinaryRelease(t, fx) |
| + hs := &headReadStore{Store: fx.srv.cfg.Releases, prefix: "apps/" + fx.sqid + "/shop/releases/", |
| + keys: map[string]bool{release.ChannelKey(fx.sqid, "shop", "edge"): true, release.ManifestKey(fx.sqid, "shop", "v1"): true}} |
| + if failLater { |
| + hs.failAt = 2 // the pointer and the manifest, once each |
| + } |
| + fx.srv.cfg.Releases = hs |
| + if code, out := createShop(t, fx, token, `{"host":"shop.example.com"}`); code != http.StatusCreated { |
| + t.Fatalf("create = %d %s", code, out) |
| + } |
| + if n := hs.gets.Load(); n != 2 { |
| + t.Fatalf("the head was read %d times; want the pointer and the manifest once each", n) |
| + } |
| + fx.srv.cfg.Releases = hs.Store |
| + if rec, _ := storedShop(t, fx, rootfsShopHost); rec.Runtime != "" || rec.Hibernate != nil { |
| + t.Fatalf("stored %+v", rec) |
| + } |
| + }) |
| + } |
| +} |