Managed fleets 1b task 3, review round 1: no relabel while an import is in flight
SetRecordFleetBox refuses a label change, the first labelling included, with ErrFleetBoxImportInFlight (wrapping a *SeedMoveError) while the record's seed blocks a move: SeedSource.MoveBlock, the test SetRecordRegion applies to a region change. Checked inside the update after the expectation, so a retry judges the record it read. MoveBlock is asked directly rather than through seedMoveRefusal, whose callers are the cross-region move doors that also refuse rootfs records; a relabel inside a fleet moves rootfs records. journalPass and the removal exemption are unchanged: a committed import reaches normal removal on the next pass. Tests: TestRelabelRefusedWhileAnImportIsInFlight (ready, swapped, placed, first labelling refused and nothing written; committed relabelled), TestCommittedImportRelabelRemovesTheOldBoxRow. Census: SetRecordFleetBox re-pinned.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
4 files changed,
+132
−2
internal/provision/fleetbox.go+25 −0internal/provision/fleetbox_test.go+53 −0internal/provision/managed_fleets_1b_census_test.go+10 −2internal/provision/seedjournal_test.go+44 −0
CI failed — run details
diff --git a/internal/provision/fleetbox.go b/internal/provision/fleetbox.go| index 075b239fa..b7a0aaae3 100644 |
| --- a/internal/provision/fleetbox.go |
| +++ b/internal/provision/fleetbox.go |
| @@ -3,6 +3,7 @@ package provision |
| import ( |
| "context" |
| "errors" |
| + "fmt" |
| "time" |
| "github.com/carlosframework/platform/internal/release" |
| @@ -14,6 +15,15 @@ import ( |
| // caller chose is not written. |
| var ErrFleetBoxMoved = errors.New("provision: the record changed since the fleet box was chosen") |
| +// ErrFleetBoxImportInFlight is SetRecordFleetBox's refusal of a label change |
| +// while the record's seed blocks a move (SeedSource.MoveBlock: an import not |
| +// yet committed, or a --from seed not yet placed), the same test |
| +// SetRecordRegion applies to a region change. The import journal and the |
| +// gated row live on the box that swapped it, so the record keeps its box |
| +// until the import commits; a caller skips the record and retries on a later |
| +// pass. It wraps a *SeedMoveError that says why. |
| +var ErrFleetBoxImportInFlight = errors.New("provision: the record has an import in flight") |
| + |
| // FleetBoxExpect is what a caller read before choosing a box: the record's |
| // Region and FleetBox as read ("" when unlabelled), and its CreatedAt, which |
| // tells a record apart from a later one created at the same host. |
| @@ -32,11 +42,26 @@ type FleetBoxExpect struct { |
| // A relabel from one non-empty label to a different non-empty label also sets |
| // FleetBoxMoved, which nothing here ever clears. The first labelling (from |
| // "") leaves it false. |
| +// |
| +// A label change, the first labelling included, is refused with |
| +// ErrFleetBoxImportInFlight while the record's seed blocks a move, checked |
| +// inside each attempt after the expectation. Writing the label it already |
| +// has is not a change and is not refused, as SetRecordRegion does not refuse |
| +// an unchanged region. |
| func SetRecordFleetBox(ctx context.Context, s release.Store, acct, app, host string, want FleetBoxExpect, label string, at time.Time) error { |
| _, err := updateRecord(ctx, s, acct, app, host, func(rec *Record) error { |
| if rec.Region != want.Region || rec.FleetBox != want.Label || !rec.CreatedAt.Equal(want.CreatedAt) { |
| return ErrFleetBoxMoved |
| } |
| + // The seed's move block, SetRecordRegion's test, asked of MoveBlock |
| + // directly: seedMoveRefusal's callers are the cross-region move |
| + // doors, which also refuse rootfs records (rootfsRefusalCallers), |
| + // and a relabel inside a fleet moves rootfs records by design. |
| + if rec.FleetBox != label { |
| + if b := rec.Seed.MoveBlock(); b != "" { |
| + return fmt.Errorf("%w: %w", ErrFleetBoxImportInFlight, &SeedMoveError{Block: b}) |
| + } |
| + } |
| if rec.FleetBox != "" && label != "" && rec.FleetBox != label { |
| rec.FleetBoxMoved = true |
| } |
diff --git a/internal/provision/fleetbox_test.go b/internal/provision/fleetbox_test.go| index b60e4681c..8b2086aa7 100644 |
| --- a/internal/provision/fleetbox_test.go |
| +++ b/internal/provision/fleetbox_test.go |
| @@ -9,6 +9,7 @@ import ( |
| "time" |
| "github.com/carlosframework/platform/internal/release" |
| + "github.com/carlosframework/platform/internal/seeds" |
| ) |
| // fleetBoxFixture creates one fleet-placed record on a fresh store and |
| @@ -199,3 +200,55 @@ func TestSetRecordRegionClearsTheBox(t *testing.T) { |
| t.Fatalf("after a region change: region %q box %q at %q", got.Region, got.FleetBox, got.FleetBoxAt) |
| } |
| } |
| + |
| +// An import in flight pins its record's box, as it pins its region |
| +// (SetRecordRegion's seedMoveRefusal): a label change, the first labelling |
| +// included, is refused with ErrFleetBoxImportInFlight and writes nothing, so |
| +// the console's pass can skip the record and retry once the import commits. |
| +// A committed import is relabelled as any record is. |
| +func TestRelabelRefusedWhileAnImportIsInFlight(t *testing.T) { |
| + ctx := context.Background() |
| + at := time.Date(2026, 10, 30, 11, 0, 0, 0, time.UTC) |
| + for _, c := range []struct { |
| + state, from, to string |
| + refused bool |
| + }{ |
| + {seeds.StateSwapped, "pi-1", "pi-2", true}, |
| + {seeds.StatePlaced, "pi-1", "pi-2", true}, |
| + {seeds.StateReady, "pi-1", "pi-2", true}, |
| + {seeds.StatePlaced, "", "pi-1", true}, |
| + {seeds.StateCommitted, "pi-1", "pi-2", false}, |
| + } { |
| + t.Run(c.state+" "+c.from+" to "+c.to, func(t *testing.T) { |
| + s := release.NewDirStore(t.TempDir()) |
| + rec := Record{ |
| + Schema: 1, Host: "web.shop.example.com", Slug: "web", App: "shop", Account: "acme", |
| + Pool: "shop", Region: "fleet/pi", Channel: "stable", FleetBox: c.from, |
| + CreatedAt: time.Date(2026, 10, 30, 9, 0, 0, 0, time.UTC), |
| + } |
| + if c.from != "" { |
| + rec.FleetBoxAt = "2026-10-30T09:00:00Z" |
| + } |
| + id, _ := seeds.NewID() |
| + rec.Seed = &SeedSource{ID: id, Kind: seeds.KindImport, State: c.state, SHA256: strings.Repeat("a", 64), Bytes: 10, |
| + Replaces: "127.0.0.1:8401", VerifyHash: strings.Repeat("b", 64), By: "t", At: time.Now()} |
| + if err := CreateRecord(ctx, s, rec); err != nil { |
| + t.Fatal(err) |
| + } |
| + err := SetRecordFleetBox(ctx, s, "acme", "shop", rec.Host, FleetBoxExpect{Region: "fleet/pi", Label: c.from, CreatedAt: rec.CreatedAt}, c.to, at) |
| + got := fleetBoxGet(t, s) |
| + if !c.refused { |
| + if err != nil || got.FleetBox != c.to { |
| + t.Fatalf("committed import: err %v, box %q; want relabelled to %q", err, got.FleetBox, c.to) |
| + } |
| + return |
| + } |
| + if !errors.Is(err, ErrFleetBoxImportInFlight) { |
| + t.Fatalf("err = %v, want ErrFleetBoxImportInFlight", err) |
| + } |
| + if got.FleetBox != c.from || got.FleetBoxAt != rec.FleetBoxAt || got.FleetBoxMoved { |
| + t.Fatalf("a refused relabel wrote: box %q at %q moved %v", got.FleetBox, got.FleetBoxAt, got.FleetBoxMoved) |
| + } |
| + }) |
| + } |
| +} |
diff --git a/internal/provision/managed_fleets_1b_census_test.go b/internal/provision/managed_fleets_1b_census_test.go| index ba6e759cf..07a290d24 100644 |
| --- a/internal/provision/managed_fleets_1b_census_test.go |
| +++ b/internal/provision/managed_fleets_1b_census_test.go |
| @@ -33,13 +33,21 @@ package provision |
| // only when its record is in scope AND this box's. Same keys, no new |
| // write: fewer records admitted, and a row of another box's record goes |
| // through the existing removal (claimRetire / DeleteIfHold). |
| +// |
| +// Task 3 fix round 1, one re-pin: |
| +// - internal/provision/fleetbox.go SetRecordFleetBox (record-writer): after |
| +// the expectation check, a label change returns ErrFleetBoxImportInFlight |
| +// (wrapping a *SeedMoveError from SeedSource.MoveBlock) while the record's seed |
| +// blocks a move, the test SetRecordRegion applies. It writes less, never |
| +// more; same updateRecord scaffold. |
| func managedFleets1bWriteCensusInventory() []censusEntry { |
| entries := append([]censusEntry(nil), fleetPublicRoutesWriteCensusInventory()...) |
| replacements := []censusEntry{ |
| // Task 1: a region change clears the box; the same scaffold. |
| {"internal/provision/provision.go", "SetRecordRegion", "record-writer", "key:SetRecordRegion,key:updateRecord", "f9daa5f51657cedf65d654f2c9386c240e6cd4ef1f5072a0a854da3bf437efad"}, |
| - // Task 1: the conditional box setter. |
| - {"internal/provision/fleetbox.go", "SetRecordFleetBox", "record-writer", "key:SetRecordFleetBox,key:updateRecord", "f2617c61887a841e7a1de2bafdbd1927f368b7451f2da349030ed98d0313906e"}, |
| + // Task 1: the conditional box setter. Re-pinned in Task 3 (fix |
| + // round 1): a label change refuses while the seed blocks a move. |
| + {"internal/provision/fleetbox.go", "SetRecordFleetBox", "record-writer", "key:SetRecordFleetBox,key:updateRecord", "ff30a14c034dd49569ac265808be7681d3dcee2020374e8357f2cc9234da3575"}, |
| // Task 2: the console's fleet inventory reads (classified in Task 3). |
| {"internal/console/fleetbox.go", "fleetInventory.readRecords", "key-site", "key:RecordsPrefix", "708adbede017825865e2306f0380d5d6494f7e7c88dbf4f18627caf46356b8ed"}, |
| {"internal/console/fleetbox.go", "fleetInventory.readSidecars", "key-site", "key:SidecarsPrefix", "45585354225333e57e9e8bdea495528000bbfed60758fe54a78ac0d63f7cd90c"}, |
diff --git a/internal/provision/seedjournal_test.go b/internal/provision/seedjournal_test.go| index 532429f23..997f13d1c 100644 |
| --- a/internal/provision/seedjournal_test.go |
| +++ b/internal/provision/seedjournal_test.go |
| @@ -853,3 +853,47 @@ func TestJournalPassMovesForwardWithClaimControlOff(t *testing.T) { |
| t.Fatalf("gate = %q, want it cleared", got.ImportGate) |
| } |
| } |
| + |
| +// Managed fleets 1b: a committed import's record relabelled to another |
| +// fleet box leaves the old box the normal way. The journal commits it on |
| +// the first pass (read by its key, as for an out-of-scope record), and the |
| +// next pass, with no journal row left, removes the row as record gone. |
| +func TestCommittedImportRelabelRemovesTheOldBoxRow(t *testing.T) { |
| + s, sqid := shopSetup(t) |
| + rec := newImport(sqid) |
| + rec.Seed.State = seeds.StateCommitted |
| + rec.Region, rec.FleetBox, rec.FleetBoxAt = "fleet/pi", "pi-1", "2026-10-30T09:00:00Z" |
| + if err := CreateRecord(ctx, s, rec); err != nil { |
| + t.Fatal(err) |
| + } |
| + reg := regWith(t, addrRow(rec, sqid)) |
| + original := mustGet(t, reg, rec.Host) |
| + if err := reg.SwapForImport(registry.ImportJournal{Host: rec.Host, SeedID: rec.Seed.ID, Original: *original, |
| + RecordKey: RecordKey(rec.Account, rec.App, rec.Host), At: 1}, importRow(rec, "/data", nil)); err != nil { |
| + t.Fatal(err) |
| + } |
| + cfg := importCfg(t, nil) |
| + cfg.Region, cfg.FleetBox = "fleet/pi", "pi-1" |
| + got, err := GetRecord(ctx, s, rec.Account, rec.App, rec.Host) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := SetRecordFleetBox(ctx, s, rec.Account, rec.App, rec.Host, FleetBoxExpect{Region: "fleet/pi", Label: "pi-1", CreatedAt: got.CreatedAt}, "pi-2", time.Now()); err != nil { |
| + t.Fatalf("relabel of a committed import: %v", err) |
| + } |
| + if err := Reconcile(ctx, s, reg, cfg); err != nil { |
| + t.Fatal(err) |
| + } |
| + if j, _ := reg.ImportJournal(rec.Host); j != nil { |
| + t.Fatalf("journal = %+v, want it dropped by the commit", j) |
| + } |
| + if rt := mustGet(t, reg, rec.Host); rt.ImportGate != "" { |
| + t.Fatalf("gate = %q, want it cleared", rt.ImportGate) |
| + } |
| + if err := Reconcile(ctx, s, reg, cfg); err != nil { |
| + t.Fatal(err) |
| + } |
| + if rt, _ := reg.Get(rec.Host); rt != nil { |
| + t.Fatalf("the old box kept the relabelled import's row: %+v", rt) |
| + } |
| +} |