Managed fleets 1b task 6b, review round 1: detach reads under the lock; unknown is not unpaused; slots are judged
P1: both detach doors read the box row inside the fleet's lock and hold it through the status write, so a relabel that names the box before the door gets the lock is seen and the drain rule asked. A test seam (Server.detachBeforeLock) runs a relabel at that point: TestDetachSeesARelabelThatLandsBeforeTheLock, api and page, failed before the move and passes after. P2: `fleets ls` labels_paused is now a string: "paused", "unknown" when the fleet's heartbeat listing fails or is not read whole and no report it did read pauses the fleet, absent otherwise. A read failure never shows "-". The CLI prints yes, unknown or -; help and docs say what unknown means. TestLabelsPausedIsUnknownWhenReportsCannotBeRead (an unreadable below-floor report, a failing listing, and a readable below-floor box beside an unreadable one). P2: TestIdlePassJudgesEveryRecord: with lifetimes on, a warm idle pass reads no record and one incarnation slot per record; an unreadable slot makes the pass write nothing although a record sits on a lost box; once it reads, the move is made.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
8 files changed,
+226
−29
apimode.go+4 −3docs/site/cli.md+2 −1fleets.go+8 −2fleets_cli_test.go+8 −1internal/console/fleetrelabel.go+27 −8internal/console/fleetrelabel_test.go+150 −2internal/console/fleets.go+24 −12internal/console/server.go+3 −0
CI failed — run details
diff --git a/apimode.go b/apimode.go| index 6edab210a..386543c8b 100644 |
| --- a/apimode.go |
| +++ b/apimode.go |
| @@ -5068,10 +5068,11 @@ type apiFleetView struct { |
| // count when nothing is stale. |
| SecretsKeyFP string `json:"secrets_key_fp,omitempty"` |
| StaleSecrets int `json:"stale_secrets,omitempty"` |
| - // LabelsPaused is present while the console has stopped moving the |
| + // LabelsPaused is "paused" while the console has stopped moving the |
| // fleet's instances between boxes because a box runs a release below |
| - // the floor (managed fleets 1b, rule 8). |
| - LabelsPaused bool `json:"labels_paused,omitempty"` |
| + // the floor (managed fleets 1b, rule 8), "unknown" when the console |
| + // could not read every box's report, and absent otherwise. |
| + LabelsPaused string `json:"labels_paused,omitempty"` |
| } |
| // fleetKeyStale is one app's line of the secrets key door's stale report: |
diff --git a/docs/site/cli.md b/docs/site/cli.md| index afdfceb5e..dd4cf9fda 100644 |
| --- a/docs/site/cli.md |
| +++ b/docs/site/cli.md |
| @@ -2182,7 +2182,8 @@ instance detaches at once. |
| In `carlos fleets ls`, a draining box shows `draining` in STATUS. LABELS |
| PAUSED reads `yes` while a box of the fleet runs a release too old for the |
| console to move instances between boxes. The console stops moving them and |
| -logs an alert until that box is updated. |
| +logs an alert until that box is updated. It reads `unknown` when the console |
| +could not read every box's report. |
| An app on a fleet cannot hold a secret until the fleet has its own secrets key. The key is a pair. The private half stays on your fleet's boxes and the platform never sees it. The public half goes to the console, which seals every secret and credential for the fleet's apps to it. |
diff --git a/fleets.go b/fleets.go| index c83618ac4..91df9bf24 100644 |
| --- a/fleets.go |
| +++ b/fleets.go |
| @@ -96,7 +96,8 @@ const fleetsUsage = `usage: carlos fleets <create|add-box|rotate-token|tag-box|d |
| list every fleet the account owns, with its secrets key fingerprint and each box's label, |
| status and tags. STALE counts the sealed values its boxes cannot |
| open yet. LABELS PAUSED is yes while a box runs a release too old |
| - for the console to move instances between boxes. |
| + for the console to move instances between boxes, and unknown when |
| + the console could not read every box's report. |
| --account <sqid|name> account the fleet belongs to (default: your only |
| account, or the configured/chosen one) — |
| @@ -438,8 +439,13 @@ func runFleetsList(args []string) { |
| if f.StaleSecrets > 0 { |
| stale = strconv.Itoa(f.StaleSecrets) |
| } |
| - if f.LabelsPaused { |
| + switch f.LabelsPaused { |
| + case "paused": |
| paused = "yes" |
| + case "": |
| + default: |
| + // "unknown", or a state this CLI does not know: never "-". |
| + paused = "unknown" |
| } |
| if len(f.Boxes) == 0 { |
| fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\t-\t-\t-\n", f.Name, f.Bucket, key, stale, paused) |
diff --git a/fleets_cli_test.go b/fleets_cli_test.go| index a259d72ed..7844afe9d 100644 |
| --- a/fleets_cli_test.go |
| +++ b/fleets_cli_test.go |
| @@ -363,9 +363,12 @@ func newFakeFleetsConsole(t *testing.T) *fakeFleetsConsole { |
| {"label": "pi-1", "status": "active", "tags": "office,free"}, |
| {"label": "pi-2", "status": "detached"}, |
| }}, |
| - {"name": "pi", "bucket": "pi-data", "labels_paused": true, "boxes": []map[string]any{ |
| + {"name": "pi", "bucket": "pi-data", "labels_paused": "paused", "boxes": []map[string]any{ |
| {"label": "pi-3", "status": "active", "draining": true}, |
| }}, |
| + {"name": "lab", "bucket": "lab-data", "labels_paused": "unknown", "boxes": []map[string]any{ |
| + {"label": "lab-1", "status": "active"}, |
| + }}, |
| }, |
| }) |
| }) |
| @@ -592,6 +595,10 @@ func TestRunFleetsListShowsLabelsPausedAndDraining(t *testing.T) { |
| if slices.Contains(f, "yes") { |
| t.Fatalf("pi-1 row = %q, want labels not paused", line) |
| } |
| + case strings.Contains(line, "lab-1"): |
| + if !slices.Contains(f, "unknown") { |
| + t.Fatalf("lab-1 row = %q, want labels paused unknown", line) |
| + } |
| } |
| } |
| } |
diff --git a/internal/console/fleetrelabel.go b/internal/console/fleetrelabel.go| index d616881dc..799ee7a30 100644 |
| --- a/internal/console/fleetrelabel.go |
| +++ b/internal/console/fleetrelabel.go |
| @@ -523,27 +523,46 @@ func (s *Server) drainBox(ctx context.Context, fleet, label string) (stuck []str |
| return nil, nil |
| } |
| +// The values of `carlos fleets ls`'s labels_paused: absent when the fleet |
| +// is not paused, fleetLabelsPaused when a box of a fleet with labels |
| +// reports a release below the floor, fleetLabelsUnknown when some box's |
| +// report could not be read and no readable one pauses the fleet. |
| +const ( |
| + fleetLabelsPaused = "paused" |
| + fleetLabelsUnknown = "unknown" |
| +) |
| + |
| // labelsPausedFor is `carlos fleets ls`'s LABELS PAUSED: labelsPausedBy of |
| // fleet's active boxes and their heartbeats. Only a fleet some box of which |
| // the console ever named on a record has labels, so only such a fleet's |
| -// heartbeats are read. |
| -func (s *Server) labelsPausedFor(ctx context.Context, fleet string, boxes []registry.FleetBox) (bool, error) { |
| +// heartbeats are read. A listing that fails or is not whole is never "not |
| +// paused": it is fleetLabelsUnknown unless a report it did read pauses the |
| +// fleet. err is the listing's failure, for the log. |
| +func (s *Server) labelsPausedFor(ctx context.Context, fleet string, boxes []registry.FleetBox) (string, error) { |
| var v fleetView |
| for _, b := range boxes { |
| if b.Status == "active" { |
| v.Boxes = append(v.Boxes, fleetBoxViewOf(b)) |
| } |
| } |
| - if !v.labelled() || s.cfg.Releases == nil { |
| - return false, nil |
| + if !v.labelled() { |
| + return "", nil |
| } |
| - hbs, _, err := s.loadBoxes(ctx, s.cfg.Releases, "fleets/"+fleet+"/") |
| + if s.cfg.Releases == nil { |
| + return fleetLabelsUnknown, errors.New("no release store configured") |
| + } |
| + hbs, complete, err := s.loadBoxes(ctx, s.cfg.Releases, "fleets/"+fleet+"/") |
| if err != nil { |
| - return false, err |
| + return fleetLabelsUnknown, err |
| } |
| joinBeats(v.Boxes, hbs, time.Now()) |
| - box, _ := v.labelsPausedBy() |
| - return box != "", nil |
| + if box, _ := v.labelsPausedBy(); box != "" { |
| + return fleetLabelsPaused, nil |
| + } |
| + if !complete { |
| + return fleetLabelsUnknown, errors.New("the heartbeat listing was not read whole") |
| + } |
| + return "", nil |
| } |
| // msgDrainStrandsApps is the drain's refusal when some record of the box |
diff --git a/internal/console/fleetrelabel_test.go b/internal/console/fleetrelabel_test.go| index a1eaa3479..6604a8972 100644 |
| --- a/internal/console/fleetrelabel_test.go |
| +++ b/internal/console/fleetrelabel_test.go |
| @@ -3,6 +3,7 @@ package console |
| import ( |
| "bytes" |
| "context" |
| + "encoding/json" |
| "errors" |
| "net/http" |
| "net/url" |
| @@ -259,14 +260,77 @@ func TestBelowFloorBoxPausesAndAlerts(t *testing.T) { |
| } |
| } |
| -// fleetListPaused is fleet pi's labels_paused in `carlos fleets ls`. |
| +// fleetListPaused reports whether `carlos fleets ls` shows fleet pi paused. |
| func fleetListPaused(t *testing.T, fx *consoleFixture, token string) bool { |
| + t.Helper() |
| + return fleetListLabels(t, fx, token) == fleetLabelsPaused |
| +} |
| + |
| +// fleetListLabels is fleet pi's labels_paused in `carlos fleets ls`: "" |
| +// when the key is absent. |
| +func fleetListLabels(t *testing.T, fx *consoleFixture, token string) string { |
| t.Helper() |
| code, _, raw := rawReq(t, http.MethodGet, fleetsURL(fx, fx.sqid), token, "") |
| if code != http.StatusOK { |
| t.Fatalf("fleets ls = %d %s", code, raw) |
| } |
| - return strings.Contains(raw, `"labels_paused":true`) |
| + var out struct { |
| + Fleets []struct { |
| + Name string `json:"name"` |
| + LabelsPaused string `json:"labels_paused"` |
| + } `json:"fleets"` |
| + } |
| + if err := json.Unmarshal([]byte(raw), &out); err != nil { |
| + t.Fatalf("fleets ls body %s: %v", raw, err) |
| + } |
| + for _, f := range out.Fleets { |
| + if f.Name == "pi" { |
| + return f.LabelsPaused |
| + } |
| + } |
| + t.Fatalf("fleets ls has no fleet pi: %s", raw) |
| + return "" |
| +} |
| + |
| +// failListStore fails every listing under prefix. |
| +type failListStore struct { |
| + release.Store |
| + prefix string |
| +} |
| + |
| +func (f failListStore) List(ctx context.Context, prefix string) ([]string, error) { |
| + if strings.HasPrefix(prefix, f.prefix) { |
| + return nil, errors.New("injected listing failure") |
| + } |
| + return f.Store.List(ctx, prefix) |
| +} |
| + |
| +// Review fix round 1, P2: a heartbeat that cannot be read never reads as |
| +// "not paused". A box below the floor whose report fails to read, and a |
| +// listing that fails, both show unknown; a readable box below the floor |
| +// still shows paused beside an unreadable one. |
| +func TestLabelsPausedIsUnknownWhenReportsCannotBeRead(t *testing.T) { |
| + fx, token, _ := fleetBoxWorld(t, "code-rl-unknown", "pi-1", "pi-2", "pi-3") |
| + putFleetRecord(t, fx, "shop", "a.example.com", nil) |
| + bindTo(t, fx, "a.example.com", "pi-1", time.Now()) |
| + relabelPass(t, fx) // pi-1 holds a label: the fleet has labels |
| + if got := fleetListLabels(t, fx, token); got != "" { |
| + t.Fatalf("a current fleet shows labels_paused %q; want it absent", got) |
| + } |
| + piBeat(t, fx, "pi-3", true, time.Now(), "v0.32.0") |
| + base := fx.srv.cfg.Releases |
| + fx.srv.cfg.Releases = &failGetStore{Store: base, key: "fleets/pi/status/boxes/pi-3.json"} |
| + if got := fleetListLabels(t, fx, token); got != fleetLabelsUnknown { |
| + t.Fatalf("with the below-floor box's report unreadable, labels_paused = %q; want %q", got, fleetLabelsUnknown) |
| + } |
| + fx.srv.cfg.Releases = failListStore{Store: base, prefix: "fleets/pi/status/boxes/"} |
| + if got := fleetListLabels(t, fx, token); got != fleetLabelsUnknown { |
| + t.Fatalf("with the heartbeat listing failing, labels_paused = %q; want %q", got, fleetLabelsUnknown) |
| + } |
| + fx.srv.cfg.Releases = &failGetStore{Store: base, key: "fleets/pi/status/boxes/pi-2.json"} |
| + if got := fleetListLabels(t, fx, token); got != fleetLabelsPaused { |
| + t.Fatalf("pi-3 readable and below the floor, pi-2 unreadable: labels_paused = %q; want %q", got, fleetLabelsPaused) |
| + } |
| } |
| // drainURL is the drain door for box label of fleet pi. |
| @@ -545,6 +609,40 @@ func TestBothDetachDoorsUseDrainDone(t *testing.T) { |
| } |
| } |
| +// Review fix round 1, P1: a relabel that lands after the detach door is |
| +// called and before it takes the fleet's lock names the box on a record. |
| +// The door must see that and ask the drain rule, at both doors. |
| +func TestDetachSeesARelabelThatLandsBeforeTheLock(t *testing.T) { |
| + for _, door := range []string{"api", "page"} { |
| + t.Run(door, func(t *testing.T) { |
| + fx, token, reg := fleetBoxWorld(t, "code-rl-race-"+door, "pi-1", "pi-2") |
| + putFleetRecord(t, fx, "shop", "a.example.com", nil) |
| + bindTo(t, fx, "a.example.com", "pi-1", time.Now()) |
| + if b := boxRow(t, reg, "pi-1"); b.LabelledAt != 0 { |
| + t.Fatal("pi-1 is labelled before the race") |
| + } |
| + fx.srv.detachBeforeLock = func(fleet, label string) { |
| + fx.srv.detachBeforeLock = nil |
| + relabelPass(t, fx) |
| + } |
| + var refused bool |
| + if door == "api" { |
| + code, raw := detachAPI(t, fx, token, "pi-1") |
| + refused = code == http.StatusConflict && errorOf(t, raw) == msgDetachNotDrained("pi", "pi-1") |
| + } else { |
| + refused = strings.Contains(detachPage(t, fx, "pi-1"), msgDetachNotDrained("pi", "pi-1")) |
| + } |
| + wantLabel(t, shopRec(t, fx, "a.example.com"), "pi-1") |
| + if !refused { |
| + t.Fatal("the detach passed on the box as it read it before the relabel") |
| + } |
| + if b := boxRow(t, reg, "pi-1"); b.Status != "active" { |
| + t.Fatalf("box %+v; want still attached", b) |
| + } |
| + }) |
| + } |
| +} |
| + |
| // Global Constraints, request budget: an idle labelled fleet costs one |
| // heartbeat listing and one read per box, the deployment's listing, one |
| // instance and one sidecar listing per placed app, no record reads and no |
| @@ -589,6 +687,56 @@ func TestIdlePassCostsTheBudget(t *testing.T) { |
| } |
| } |
| +// Review fix round 1, P2: with lifetimes on, a warm idle pass still judges |
| +// every record, one incarnation-slot read each, and reads no record body; |
| +// a slot that cannot be read makes the pass write nothing for the fleet, |
| +// though a record is on a lost box and would otherwise move. |
| +func TestIdlePassJudgesEveryRecord(t *testing.T) { |
| + fx, _, _ := fleetBoxWorld(t, "code-rl-budget-lifetimes", "pi-1", "pi-2") |
| + hosts := []string{"a.example.com", "b.example.com", "c.example.com"} |
| + for i, h := range hosts { |
| + box := []string{"pi-1", "pi-2", "pi-2"}[i] |
| + putFleetRecord(t, fx, "shop", h, func(r *provision.Record) { r.FleetBox, r.Incarnation = box, 1 }) |
| + writeSlot(t, fx.rel, h, provision.HostIncarnation{Incarnation: 1, State: provision.SlotLive, Account: fx.sqid, App: "shop"}) |
| + } |
| + cs := newCountStore(fx.rel) |
| + fx.srv.cfg.Releases = cs |
| + ctx := provision.WithLifetimes(context.Background(), provision.NewLifetimeJudge()) |
| + pass := func() { |
| + t.Helper() |
| + if err := fx.srv.fleetRelabelPass(ctx); err != nil { |
| + t.Fatalf("relabel pass: %v", err) |
| + } |
| + } |
| + pass() // cold |
| + cs.reset() |
| + pass() // warm |
| + if r, sl := cs.recordGets(fx.sqid, "shop"), cs.slotGets(); r != 0 || sl != len(hosts) { |
| + t.Fatalf("a warm judged pass read %d records and %d slots; want 0 and one slot per record (%d)", r, sl, len(hosts)) |
| + } |
| + if n := cs.recordPuts(); n != 0 { |
| + t.Fatalf("an idle judged pass wrote %d records", n) |
| + } |
| + |
| + // pi-1 is lost, so a would move; b's slot cannot be read. |
| + piBeat(t, fx, "pi-1", true, time.Now().Add(-21*time.Minute), release.FleetBoxLabelFloor) |
| + fx.srv.cfg.Releases = &failGetStore{Store: cs, key: provision.HostIncarnationKey("b.example.com")} |
| + cs.reset() |
| + pass() |
| + if n := cs.recordPuts(); n != 0 { |
| + t.Fatalf("a pass with an unreadable slot wrote %d records; want none", n) |
| + } |
| + if r := shopRec(t, fx, "a.example.com"); r.FleetBox != "pi-1" { |
| + t.Fatalf("a moved to %q while b's slot was unreadable", r.FleetBox) |
| + } |
| + // The slot reads again: the move the pass held back is made. |
| + fx.srv.cfg.Releases = cs |
| + pass() |
| + if r := shopRec(t, fx, "a.example.com"); r.FleetBox != "pi-2" { |
| + t.Fatalf("a is on %q once every slot reads; want pi-2", r.FleetBox) |
| + } |
| +} |
| + |
| // The drain and the worker are off on a host-claim console, where no claim |
| // operation gates their record writes. |
| func TestDrainRefusedOnAHostClaimConsole(t *testing.T) { |
diff --git a/internal/console/fleets.go b/internal/console/fleets.go| index 0621dcc6e..4973aa81a 100644 |
| --- a/internal/console/fleets.go |
| +++ b/internal/console/fleets.go |
| @@ -71,10 +71,12 @@ type fleetWire struct { |
| // reads as it always did. |
| SecretsKeyFP string `json:"secrets_key_fp,omitempty"` |
| StaleSecrets int `json:"stale_secrets,omitempty"` |
| - // LabelsPaused is additive (managed fleets 1b, rule 8): present only |
| - // while a box of a fleet with labels reports a release below the label |
| - // floor, so the console has stopped moving its instances. |
| - LabelsPaused bool `json:"labels_paused,omitempty"` |
| + // LabelsPaused is additive (managed fleets 1b, rule 8): "paused" while |
| + // a box of a fleet with labels reports a release below the label floor, |
| + // so the console has stopped moving its instances; "unknown" when some |
| + // box's report could not be read and none that was read pauses it; |
| + // absent otherwise (labelsPausedFor). |
| + LabelsPaused string `json:"labels_paused,omitempty"` |
| } |
| // apiFleet resolves and authorizes one {account}/{fleet} CLI API request: |
| @@ -357,7 +359,8 @@ func (s *Server) handleAPIFleetList(w http.ResponseWriter, r *http.Request) { |
| return |
| } |
| view := fleetWire{Name: f.Name, Bucket: f.Bucket, Boxes: fleetBoxViews(boxes), SecretsKeyFP: f.SecretsKeyFP} |
| - // A heartbeat listing that cannot be read costs the column, not the list. |
| + // A heartbeat listing that cannot be read costs the column, not the |
| + // list: the column then says unknown, never not paused. |
| paused, err := s.labelsPausedFor(r.Context(), f.Name, boxes) |
| if err != nil { |
| log.Printf("console: fleet list %s: labels paused of %s: %v", acct.Sqid, f.Name, err) |
| @@ -570,6 +573,15 @@ func (s *Server) handleAPIFleetDetachBox(w http.ResponseWriter, r *http.Request) |
| return |
| } |
| label := r.PathValue("label") |
| + // The drain rule (managed fleets 1b, rule 5): the box is read, judged |
| + // and detached under the fleet's lock, the lock every writer of a label |
| + // holds, so the box row the check reads is the one the write acts on |
| + // and nothing names the box between them. |
| + if s.detachBeforeLock != nil { |
| + s.detachBeforeLock(fleet.Name, label) |
| + } |
| + unlock := s.lockFleet(fleet.Name) |
| + defer unlock() |
| box, err := findFleetBox(reg, fleet.Name, label) |
| if err != nil { |
| log.Printf("console: fleet detach %s/%s/%s: %v", acct.Sqid, fleet.Name, label, err) |
| @@ -580,10 +592,6 @@ func (s *Server) handleAPIFleetDetachBox(w http.ResponseWriter, r *http.Request) |
| jsonError(w, http.StatusNotFound, "no such box") |
| return |
| } |
| - // The drain rule (managed fleets 1b, rule 5): under the fleet's lock, |
| - // so no create names the box between the check and the write. |
| - unlock := s.lockFleet(fleet.Name) |
| - defer unlock() |
| held, err := s.detachHeld(r.Context(), fleet.Name, *box) |
| if err != nil { |
| log.Printf("console: fleet detach %s/%s/%s: heartbeat: %v", acct.Sqid, fleet.Name, label, err) |
| @@ -1555,6 +1563,13 @@ func (s *Server) handleFleetDetachBoxPage(w http.ResponseWriter, r *http.Request |
| http.Redirect(w, r, fleetPageURL(acct.Sqid, fleet.Name), http.StatusSeeOther) |
| } |
| label := strings.TrimSpace(r.FormValue("label")) |
| + // The drain rule, read, judged and written under the fleet's lock, as |
| + // handleAPIFleetDetachBox does. |
| + if s.detachBeforeLock != nil { |
| + s.detachBeforeLock(fleet.Name, label) |
| + } |
| + unlock := s.lockFleet(fleet.Name) |
| + defer unlock() |
| box, err := findFleetBox(reg, fleet.Name, label) |
| if err != nil { |
| log.Printf("console: fleet detach %s/%s/%s: %v", acct.Sqid, fleet.Name, label, err) |
| @@ -1565,9 +1580,6 @@ func (s *Server) handleFleetDetachBoxPage(w http.ResponseWriter, r *http.Request |
| back("err", "No such box.") |
| return |
| } |
| - // The drain rule, as handleAPIFleetDetachBox asks it. |
| - unlock := s.lockFleet(fleet.Name) |
| - defer unlock() |
| held, err := s.detachHeld(r.Context(), fleet.Name, *box) |
| if err != nil { |
| log.Printf("console: fleet detach %s/%s/%s: heartbeat: %v", acct.Sqid, fleet.Name, label, err) |
diff --git a/internal/console/server.go b/internal/console/server.go| index 8adb77392..0c13e3109 100644 |
| --- a/internal/console/server.go |
| +++ b/internal/console/server.go |
| @@ -981,6 +981,9 @@ type Server struct { |
| // (*fleetInventory), both made on first use. |
| fleetLocks sync.Map |
| fleetInventories sync.Map |
| + // detachBeforeLock, when set, runs in both detach doors just before |
| + // they take the fleet's lock (tests only: an interleaving seam). |
| + detachBeforeLock func(fleet, label string) |
| // consoleBuild, when set, is the release this console reports to the |
| // fleet label gate instead of version.Build (tests only). |
| consoleBuild string |