status: the heartbeat carries the named-units record
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
6 files changed,
+139
−1
internal/host/agent.go+4 −0internal/host/namedunitsprobe.go+42 −0internal/host/namedunitsprobe_linux_test.go+43 −0internal/host/namedunitsprobe_test.go+22 −0internal/status/namedunits_probe_test.go+19 −0internal/status/snapshot.go+9 −1
diff --git a/internal/host/agent.go b/internal/host/agent.go| index 78822e5a0..a930c29be 100644 |
| --- a/internal/host/agent.go |
| +++ b/internal/host/agent.go |
| @@ -5302,6 +5302,10 @@ func statusLoop(reg *registry.Registry, svc *activator.Service, buildStates func |
| // move. Private Metrics only: hosts. |
| p.OfflineMove = offlineMoveFactsProbe(routes) |
| + // Root's named-units record, read from its public store; nil when |
| + // absent or unreadable. No bucket request. |
| + p.NamedUnits = namedUnitsStatusProbe() |
| + |
| // A fleet box (CARLOS_FLEET set) never gets here as a contender: it |
| // self-reports only, never attempting the election at all — no |
| // lease.Acquire call, no BuildFleet, no PublishFleet. See |
diff --git a/internal/host/namedunitsprobe.go b/internal/host/namedunitsprobe.go| new file mode 100644 |
| index 000000000..fc3f35804 |
| --- /dev/null |
| +++ b/internal/host/namedunitsprobe.go |
| @@ -0,0 +1,42 @@ |
| +package host |
| + |
| +import ( |
| + "encoding/json" |
| + |
| + "github.com/carlosframework/platform/internal/rootstate" |
| + "github.com/carlosframework/platform/internal/status" |
| +) |
| + |
| +// namedUnitsStatusMax caps the record the agent will carry in a heartbeat. |
| +const namedUnitsStatusMax = 64 << 10 |
| + |
| +// namedUnitsStatusProbe reads root's named-units record for the heartbeat. |
| +// Any failure (store absent, untrusted, unreadable, oversized, malformed, |
| +// or a platform without rootstate) is nil: it never fails the heartbeat. |
| +func namedUnitsStatusProbe() *status.NamedUnitsStatus { |
| + return readNamedUnitsStatus(namedUnitsStatusDir) |
| +} |
| + |
| +func readNamedUnitsStatus(dir string) *status.NamedUnitsStatus { |
| + s, err := rootstate.Open(dir, false, true) |
| + if err != nil { |
| + return nil |
| + } |
| + defer s.Close() |
| + raw, err := s.Read("status.json") |
| + if err != nil { |
| + return nil |
| + } |
| + return decodeNamedUnitsStatus(raw) |
| +} |
| + |
| +func decodeNamedUnitsStatus(raw []byte) *status.NamedUnitsStatus { |
| + if len(raw) > namedUnitsStatusMax { |
| + return nil |
| + } |
| + var st status.NamedUnitsStatus |
| + if json.Unmarshal(raw, &st) != nil { |
| + return nil |
| + } |
| + return &st |
| +} |
diff --git a/internal/host/namedunitsprobe_linux_test.go b/internal/host/namedunitsprobe_linux_test.go| new file mode 100644 |
| index 000000000..327cfb646 |
| --- /dev/null |
| +++ b/internal/host/namedunitsprobe_linux_test.go |
| @@ -0,0 +1,43 @@ |
| +package host |
| + |
| +import ( |
| + "path/filepath" |
| + "strings" |
| + "testing" |
| + |
| + "github.com/carlosframework/platform/internal/rootstate" |
| +) |
| + |
| +func TestNamedUnitsStatusProbeNilOnMissingOrOversize(t *testing.T) { |
| + dir := filepath.Join(t.TempDir(), "nu") |
| + if readNamedUnitsStatus(dir) != nil { |
| + t.Fatal("missing store must read nil") |
| + } |
| + s, err := rootstate.Open(dir, true, true) |
| + if err != nil { |
| + t.Skipf("rootstate unavailable here: %v", err) |
| + } |
| + defer s.Close() |
| + if readNamedUnitsStatus(dir) != nil { |
| + t.Fatal("missing record must read nil") |
| + } |
| + if err := s.Write("status.json", []byte(`{"outcome":"ok"}`)); err != nil { |
| + t.Fatal(err) |
| + } |
| + if got := readNamedUnitsStatus(dir); got == nil || got.Outcome != "ok" { |
| + t.Fatalf("good record = %+v", got) |
| + } |
| + big := `{"reason":"` + strings.Repeat("x", namedUnitsStatusMax) + `"}` |
| + if err := s.Write("status.json", []byte(big)); err != nil { |
| + t.Fatal(err) |
| + } |
| + if readNamedUnitsStatus(dir) != nil { |
| + t.Fatal("oversize record must read nil") |
| + } |
| + if err := s.Write("status.json", []byte(`{bad`)); err != nil { |
| + t.Fatal(err) |
| + } |
| + if readNamedUnitsStatus(dir) != nil { |
| + t.Fatal("malformed record must read nil") |
| + } |
| +} |
diff --git a/internal/host/namedunitsprobe_test.go b/internal/host/namedunitsprobe_test.go| new file mode 100644 |
| index 000000000..8fcafa199 |
| --- /dev/null |
| +++ b/internal/host/namedunitsprobe_test.go |
| @@ -0,0 +1,22 @@ |
| +package host |
| + |
| +import ( |
| + "strings" |
| + "testing" |
| +) |
| + |
| +func TestNamedUnitsStatusDecodeNilOnOversizeOrMalformed(t *testing.T) { |
| + if got := decodeNamedUnitsStatus([]byte(`{"outcome":"ok"}`)); got == nil || got.Outcome != "ok" { |
| + t.Fatalf("good record = %+v", got) |
| + } |
| + big := `{"reason":"` + strings.Repeat("x", namedUnitsStatusMax) + `"}` |
| + if decodeNamedUnitsStatus([]byte(big)) != nil { |
| + t.Error("oversize record must decode nil") |
| + } |
| + if decodeNamedUnitsStatus([]byte(`{bad`)) != nil { |
| + t.Error("malformed record must decode nil") |
| + } |
| + if readNamedUnitsStatus(t.TempDir()+"/absent") != nil { |
| + t.Error("absent store must read nil") |
| + } |
| +} |
diff --git a/internal/status/namedunits_probe_test.go b/internal/status/namedunits_probe_test.go| new file mode 100644 |
| index 000000000..3a907ed46 |
| --- /dev/null |
| +++ b/internal/status/namedunits_probe_test.go |
| @@ -0,0 +1,19 @@ |
| +package status |
| + |
| +import ( |
| + "reflect" |
| + "testing" |
| + "time" |
| +) |
| + |
| +func TestCollectBoxCarriesNamedUnits(t *testing.T) { |
| + rec := &NamedUnitsStatus{Parameter: "p", Outcome: "ok"} |
| + box := CollectBox(nil, Probes{NamedUnits: rec}, time.Now()) |
| + if box.Metrics == nil || !reflect.DeepEqual(box.Metrics.NamedUnits, rec) { |
| + t.Fatalf("Metrics.NamedUnits = %+v, want %+v", box.Metrics, rec) |
| + } |
| + box = CollectBox(nil, Probes{}, time.Now()) |
| + if box.Metrics != nil { |
| + t.Errorf("no record and nothing else known: Metrics = %+v, want nil", box.Metrics) |
| + } |
| +} |
diff --git a/internal/status/snapshot.go b/internal/status/snapshot.go| index a364bfb6d..34fd93a49 100644 |
| --- a/internal/status/snapshot.go |
| +++ b/internal/status/snapshot.go |
| @@ -237,6 +237,9 @@ type Probes struct { |
| // cannot take part in an offline move or cannot say. Private Metrics |
| // only: it names hosts. |
| OfflineMove *OfflineMoveFacts |
| + // NamedUnits is root's last named-units record, read by the agent from |
| + // the public root store; nil when root has written none. |
| + NamedUnits *NamedUnitsStatus |
| } |
| // OfflineMoveFacts is what a box that can take part in an offline move |
| @@ -853,6 +856,10 @@ type BoxMetrics struct { |
| // like RouteVersions. Omitted on a box that cannot take part in an |
| // offline move. |
| OfflineMove *OfflineMoveFacts `json:"offline_move,omitempty"` |
| + // NamedUnits is root's record of its last named-units step (Probes.NamedUnits), |
| + // carried in the heartbeat the agent already publishes. Omitted when root |
| + // has written none. |
| + NamedUnits *NamedUnitsStatus `json:"named_units,omitempty"` |
| } |
| // UpdateSettingsProtocol is the BoxMetrics.UpdateSettings a binary reports |
| @@ -1295,7 +1302,7 @@ func CollectBox(routes []registry.Route, p Probes, now time.Time) Box { |
| len(routeDBBytes) > 0 || p.Layout != nil || len(p.ConfigRefusals) > 0 || len(p.ReleaseRefusals) > 0 || len(p.ProofWaits) > 0 || p.Rollout != nil || p.ConfigGuard != "" || |
| p.Alerts != nil || p.GeoIP != nil || p.HeldStreams != nil || len(p.BuildStates) > 0 || p.BuildStatesErr != "" || |
| p.DemoClaimSeedReady || p.DemoMovedReady || p.UpdateSettings != 0 || len(p.Updates) > 0 || len(p.Egress) > 0 || p.SizeCommittedMiB != nil || |
| - p.MailerArmed || p.OfflineMove != nil { |
| + p.MailerArmed || p.OfflineMove != nil || p.NamedUnits != nil { |
| var capabilities []string |
| if p.DemoClaimSeedReady { |
| capabilities = append(capabilities, seeds.DemoClaimCapability) |
| @@ -1355,6 +1362,7 @@ func CollectBox(routes []registry.Route, p Probes, now time.Time) Box { |
| SizeCommittedMiB: p.SizeCommittedMiB, |
| MailerArmed: p.MailerArmed, |
| OfflineMove: p.OfflineMove, |
| + NamedUnits: p.NamedUnits, |
| } |
| } |