fleet-routes-followups: one owner-map fold, Registry.FleetOwners (#457 1a)
R2 triage follow-up "Reload's inline owner-map construction": the edge's Reload and the hub's hubFleetOwners each folded ListFleets into a fleet -> account map by hand. Both now call Registry.FleetOwners, so the forward phase, the edges file, the alias pass and the edge read the table the same way (they still read it separately and converge; see hubFleetOwners). No behaviour change: same read, same map, same error path. TestFleetOwnersMapsEachFleetToItsAccount (local and remote) came first (RED: undefined); a mutation folding to the bucket instead of the account fails it and the edge's forward tests.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
4 files changed,
+58
−19
internal/edge/edge.go+7 −10internal/host/hub.go+5 −9internal/registry/fleets.go+18 −0internal/registry/fleets_test.go+28 −0
diff --git a/internal/edge/edge.go b/internal/edge/edge.go| index a953c2455..f6a9f8421 100644 |
| --- a/internal/edge/edge.go |
| +++ b/internal/edge/edge.go |
| @@ -757,11 +757,12 @@ func (e *Edge) Reload() error { |
| // |
| // The store door's tenancy map comes from the SAME pass: a box's |
| // credentials and the account its reads are scoped to must never come |
| - // from two different reads of the registry. ListFleets goes over a fresh |
| - // connection (like FleetChannelAuth), so a fleet the console created |
| - // moments ago is visible here rather than stuck behind a stale WAL |
| - // snapshot on the long-lived handle. The same map is the owners map the |
| - // eligibility rule reads. |
| + // from two different reads of the registry. FleetOwners (ListFleets) |
| + // goes over a fresh connection (like FleetChannelAuth), so a fleet the |
| + // console created moments ago is visible here rather than stuck behind a |
| + // stale WAL snapshot on the long-lived handle. The same map is the |
| + // owners map the eligibility rule reads, folded as the hub's forward |
| + // phase folds it. |
| // |
| // hub is the one hub test (HubArmed, shared with the agent's bindings |
| // pass and the edges-file writer) on a box whose tunnels are enabled. |
| @@ -782,14 +783,10 @@ func (e *Edge) Reload() error { |
| if err != nil { |
| return err |
| } |
| - fleets, err := e.reg.ListFleets() |
| + owners, err = e.reg.FleetOwners() |
| if err != nil { |
| return err |
| } |
| - owners = make(map[string]string, len(fleets)) |
| - for _, f := range fleets { |
| - owners[f.Name] = f.Account |
| - } |
| hub = HubArmed(os.Getenv) |
| } |
| verdict := FleetArmVerdict(routes, owners, hub, e.tunnels != nil) |
diff --git a/internal/host/hub.go b/internal/host/hub.go| index 94cf7fadd..29c3f183f 100644 |
| --- a/internal/host/hub.go |
| +++ b/internal/host/hub.go |
| @@ -42,23 +42,19 @@ func fleetArmInputs(reg *registry.Registry, getenv func(string) string) (owners |
| // fleetArmInputs reads the owners the edges file and the alias pass judge |
| // by, so the forward phase and those two read the same table the same way. |
| // |
| -// The edge's Reload reads the table itself (ListFleets), so for one pass |
| -// after a fleet is created, deleted or changes owner the phase and the edge |
| -// can disagree. They converge on their next reads, and the disagreement |
| -// fails closed: the edge serves a forward row only when its own read names |
| +// The edge's Reload reads the table itself (the same FleetOwners), so for |
| +// one pass after a fleet is created, deleted or changes owner the phase and |
| +// the edge can disagree. They converge on their next reads, and the |
| +// disagreement fails closed: the edge serves a forward row only when its own read names |
| // the row's account as the fleet's owner, so a row the phase minted first |
| // is refused until the edge reads the same, and a row the phase has not yet |
| // retired is refused once the edge's read stops naming that owner. |
| func hubFleetOwners(reg *registry.Registry) func() (map[string]string, error) { |
| return func() (map[string]string, error) { |
| - fleets, err := reg.ListFleets() |
| + owners, err := reg.FleetOwners() |
| if err != nil { |
| return nil, fmt.Errorf("list fleets: %w", err) |
| } |
| - owners := make(map[string]string, len(fleets)) |
| - for _, f := range fleets { |
| - owners[f.Name] = f.Account |
| - } |
| return owners, nil |
| } |
| } |
diff --git a/internal/registry/fleets.go b/internal/registry/fleets.go| index 6676d6e61..7877503e0 100644 |
| --- a/internal/registry/fleets.go |
| +++ b/internal/registry/fleets.go |
| @@ -276,6 +276,24 @@ func (r *Registry) GetFleet(name string) (*Fleet, error) { |
| return &f, nil |
| } |
| +// FleetOwners is ListFleets folded to each fleet's name and its owning |
| +// account: the owners map the edge's Reload (FleetArmVerdict) and the hub's |
| +// forward phase judge a forward row by (fleet public routes spec §4.3, |
| +// §4.4). One fold, so the two read the table the same way; they still read |
| +// it separately, and converge on their next reads. Never nil when err is |
| +// nil. |
| +func (r *Registry) FleetOwners() (map[string]string, error) { |
| + fleets, err := r.ListFleets() |
| + if err != nil { |
| + return nil, err |
| + } |
| + owners := make(map[string]string, len(fleets)) |
| + for _, f := range fleets { |
| + owners[f.Name] = f.Account |
| + } |
| + return owners, nil |
| +} |
| + |
| // ListFleets returns every fleet, ordered by name, over a fresh connection. |
| func (r *Registry) ListFleets() ([]Fleet, error) { |
| if r.remote != nil { |
diff --git a/internal/registry/fleets_test.go b/internal/registry/fleets_test.go| index 39ccc134c..aa4432daa 100644 |
| --- a/internal/registry/fleets_test.go |
| +++ b/internal/registry/fleets_test.go |
| @@ -4,6 +4,7 @@ import ( |
| "database/sql" |
| "errors" |
| "path/filepath" |
| + "reflect" |
| "regexp" |
| "strings" |
| "sync" |
| @@ -700,3 +701,30 @@ CREATE TABLE fleet_boxes ( |
| t.Fatalf("pre-tags row should default tags=\"\": %+v", boxes) |
| } |
| } |
| + |
| +// TestFleetOwnersMapsEachFleetToItsAccount: FleetOwners is ListFleets folded |
| +// to fleet name -> owning account, the one map the edge's Reload and the |
| +// hub's forward phase both judge forward rows by. Locally and over the |
| +// remote transport; an empty table is an empty map, not nil. |
| +func TestFleetOwnersMapsEachFleetToItsAccount(t *testing.T) { |
| + local := openTestRegistry(t) |
| + remote, _ := remoteTestRegistry(t) |
| + for name, reg := range map[string]*Registry{"local": local, "remote": remote} { |
| + got, err := reg.FleetOwners() |
| + if err != nil || got == nil || len(got) != 0 { |
| + t.Fatalf("%s: FleetOwners on no fleets = %v, %v; want an empty map", name, got, err) |
| + } |
| + for _, f := range []Fleet{{Name: "home", Account: "bah", Bucket: "home-data"}, {Name: "away", Account: "baj", Bucket: "away-data"}} { |
| + if err := reg.UpsertFleet(f); err != nil { |
| + t.Fatal(err) |
| + } |
| + } |
| + got, err = reg.FleetOwners() |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if want := map[string]string{"home": "bah", "away": "baj"}; !reflect.DeepEqual(got, want) { |
| + t.Fatalf("%s: FleetOwners = %v, want %v", name, got, want) |
| + } |
| + } |
| +} |