carlos / platform Public

rollout: the box reports each rootfs instance's worker states

Status copies the activator's RootfsWorkers answer for each host into
HostRollout.Workers (sorted by name), from the same per-host ask that
answers the transient flag, with no Manager or scheduler lock held. A
binary host, an asleep one or one with no workers has no workers key.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev 0c2675ede323fe912b993f5896331300829fd7e7 parent 8fc52c3
6 files changed, +189 −11
  • internal/rollout/fake_test.go +49 −0
  • internal/rollout/manager.go +3 −0
  • internal/rollout/rootfs_status_test.go +92 −0
  • internal/rollout/sim_test.go +3 −0
  • internal/rollout/status.go +29 −11
  • internal/status/snapshot.go +13 −0
diff --git a/internal/rollout/fake_test.go b/internal/rollout/fake_test.go
index 0bbbc486a..95a2529cb 100644
--- a/internal/rollout/fake_test.go
+++ b/internal/rollout/fake_test.go
@@ -54,6 +54,36 @@ type fakeAct struct {
// for an attempt that ended out. Unset, only an abandoned attempt ran
// nothing.
executed func(a activator.Attempt, out activator.SwapOutcome) bool
+ // workers is what RootfsWorkers answers per host (nil: none), and
+ // workerCalls counts the asks per host.
+ workers map[string][]activator.WorkerReport
+ workerCalls map[string]int
+}
+
+// RootfsWorkers answers the workers a test set for host.
+func (f *fakeAct) RootfsWorkers(host string) []activator.WorkerReport {
+ f.mu.Lock()
+ defer f.mu.Unlock()
+ if f.workerCalls == nil {
+ f.workerCalls = map[string]int{}
+ }
+ f.workerCalls[host]++
+ return append([]activator.WorkerReport(nil), f.workers[host]...)
+}
+
+func (f *fakeAct) setWorkers(host string, ws []activator.WorkerReport) {
+ f.mu.Lock()
+ defer f.mu.Unlock()
+ if f.workers == nil {
+ f.workers = map[string][]activator.WorkerReport{}
+ }
+ f.workers[host] = ws
+}
+
+func (f *fakeAct) workerAsks(host string) int {
+ f.mu.Lock()
+ defer f.mu.Unlock()
+ return f.workerCalls[host]
}
// PublishHeldServing runs onHeldPublish, when set.
@@ -90,6 +120,18 @@ func newFakeAct() *fakeAct {
type probeAct struct {
*fakeAct
hook atomic.Pointer[func(method, host string)]
+ // workersHook is run before every RootfsWorkers, apart from hook so the
+ // tests that install a hook for Owns and Current see no new method.
+ workersHook atomic.Pointer[func(method, host string)]
+}
+
+// onWorkersAsk installs f as the RootfsWorkers hook; nil removes it.
+func (p *probeAct) onWorkersAsk(f func(method, host string)) {
+ if f == nil {
+ p.workersHook.Store(nil)
+ return
+ }
+ p.workersHook.Store(&f)
}
// onAsk installs f as the hook; nil removes it.
@@ -117,6 +159,13 @@ func (p *probeAct) Current(host string) (activator.Gen, bool) {
return p.fakeAct.Current(host)
}
+func (p *probeAct) RootfsWorkers(host string) []activator.WorkerReport {
+ if f := p.workersHook.Load(); f != nil {
+ (*f)("RootfsWorkers", host)
+ }
+ return p.fakeAct.RootfsWorkers(host)
+}
+
func (f *fakeAct) Current(host string) (activator.Gen, bool) {
f.mu.Lock()
defer f.mu.Unlock()
diff --git a/internal/rollout/manager.go b/internal/rollout/manager.go
index fd239baca..f80314788 100644
--- a/internal/rollout/manager.go
+++ b/internal/rollout/manager.go
@@ -30,6 +30,9 @@ import (
type Activator interface {
Current(host string) (activator.Gen, bool)
Owns(host string) bool
+ // RootfsWorkers is host's workers, sorted by name: nil for a binary host
+ // or one with none (Plan 3 Group W).
+ RootfsWorkers(host string) []activator.WorkerReport
Swap(ctx context.Context, a activator.Attempt, newest func(host string) uint64) activator.SwapResult
ReplaceWorker(ctx context.Context, a activator.Attempt, fallback *release.StagedIdentity, newest func(host string) uint64) activator.SwapResult
PublishHeldServing(host string)
diff --git a/internal/rollout/rootfs_status_test.go b/internal/rollout/rootfs_status_test.go
new file mode 100644
index 000000000..dbe56d95b
--- /dev/null
+++ b/internal/rollout/rootfs_status_test.go
@@ -0,0 +1,92 @@
+package rollout
+
+import (
+ "encoding/json"
+ "strings"
+ "sync/atomic"
+ "testing"
+
+ "github.com/carlosframework/platform/internal/activator"
+ "github.com/carlosframework/platform/internal/status"
+)
+
+// Rootfs apps Plan 3, Group W, Task W3: the box's rollout report carries each
+// rootfs instance's worker states, as the activator reports them [pf W3.2].
+
+func TestRolloutReportCarriesWorkers(t *testing.T) {
+ h := newHarness(t, "shop", "bin", "bare")
+ h.pass()
+ // The activator answers out of order; a binary host and a rootfs host
+ // with no declared workers answer none.
+ h.act.setWorkers("shop", []activator.WorkerReport{
+ {Name: "queue", State: activator.WorkerCrashLoop, Restarts: 7},
+ {Name: "mailer", State: activator.WorkerRunning},
+ {Name: "cron", State: activator.WorkerRestarting, Restarts: 2},
+ })
+ r := h.m.Status()
+ want := []status.WorkerState{
+ {Name: "cron", State: "restarting", Restarts: 2},
+ {Name: "mailer", State: "running"},
+ {Name: "queue", State: "crash-loop", Restarts: 7},
+ }
+ got := r.Hosts["shop"].Workers
+ if len(got) != len(want) {
+ t.Fatalf("shop workers = %+v, want %+v", got, want)
+ }
+ for i := range want {
+ if got[i] != want[i] {
+ t.Fatalf("shop workers[%d] = %+v, want %+v (all: %+v)", i, got[i], want[i], got)
+ }
+ }
+ for _, host := range []string{"bin", "bare"} {
+ hr := r.Hosts[host]
+ if hr.Workers != nil {
+ t.Fatalf("%s workers = %+v, want none", host, hr.Workers)
+ }
+ b, err := json.Marshal(hr)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if strings.Contains(string(b), `"workers"`) {
+ t.Fatalf("%s JSON = %s, want no workers key", host, b)
+ }
+ }
+ b, err := json.Marshal(r.Hosts["shop"])
+ if err != nil {
+ t.Fatal(err)
+ }
+ if !strings.Contains(string(b), `"workers":[{"name":"cron","state":"restarting","restarts":2},{"name":"mailer","state":"running"},`) {
+ t.Fatalf("shop JSON = %s, want the workers in Plan 4's shape", b)
+ }
+ // The activator is asked once per host.
+ if n := h.act.workerAsks("shop"); n != 1 {
+ t.Fatalf("RootfsWorkers asked %d times for shop in one report, want 1", n)
+ }
+}
+
+func TestRolloutReportAsksWorkersWithoutLocks(t *testing.T) {
+ h := newHarness(t, "shop", "bin")
+ h.pass()
+ h.act.setWorkers("shop", []activator.WorkerReport{{Name: "mailer", State: activator.WorkerRunning}})
+ h.m.mu.Lock()
+ scheds := make([]*scheduler, 0, len(h.m.apps))
+ for _, s := range h.m.apps {
+ scheds = append(scheds, s)
+ }
+ h.m.mu.Unlock()
+ if len(scheds) == 0 {
+ t.Fatal("the pass built no scheduler")
+ }
+ // The ask must find the Manager's and every scheduler's lock free; the
+ // probe fails the test (never deadlocks) when it does not.
+ lp := &lockProbe{m: h.m, scheds: scheds, t: t, asked: map[string]*atomic.Int64{"RootfsWorkers": {}}}
+ h.probe.onWorkersAsk(lp.check)
+ defer h.probe.onWorkersAsk(nil)
+ r := h.m.Status()
+ if lp.asked["RootfsWorkers"].Load() == 0 {
+ t.Fatal("Status never asked RootfsWorkers")
+ }
+ if len(r.Hosts["shop"].Workers) != 1 {
+ t.Fatalf("shop workers = %+v, want the one the activator answered", r.Hosts["shop"].Workers)
+ }
+}
diff --git a/internal/rollout/sim_test.go b/internal/rollout/sim_test.go
index 6ed82ed45..f6d5fba28 100644
--- a/internal/rollout/sim_test.go
+++ b/internal/rollout/sim_test.go
@@ -923,6 +923,9 @@ func (f simAct) ReplaceWorker(context.Context, activator.Attempt, *release.Stage
func (f simAct) PublishHeldServing(string) {}
+// RootfsWorkers: the simulation models binary instances, which have none.
+func (f simAct) RootfsWorkers(string) []activator.WorkerReport { return nil }
+
// EffectiveMode: the simulation has no foreign-layout rows.
func (f simAct) EffectiveMode(_ string, m activator.SwapMode) activator.SwapMode { return m }
diff --git a/internal/rollout/status.go b/internal/rollout/status.go
index 6d3bbe429..b3c5f8df2 100644
--- a/internal/rollout/status.go
+++ b/internal/rollout/status.go
@@ -1,6 +1,9 @@
package rollout
import (
+ "sort"
+
+ "github.com/carlosframework/platform/internal/activator"
"github.com/carlosframework/platform/internal/status"
)
@@ -57,9 +60,16 @@ func (m *Manager) Status() status.RolloutReport {
// A host the activator was not asked about joined between the name
// read and this loop: nothing says it is settled, so this report
// calls it transient and the next one asks.
- if tr, ok := asked[host]; !ok || tr {
+ if a, ok := asked[host]; !ok || a.transient {
hr.Serving, hr.Awake, hr.Transient = "", false, true
}
+ if len(asked[host].workers) > 0 {
+ hr.Workers = make([]status.WorkerState, 0, len(asked[host].workers))
+ for _, w := range asked[host].workers {
+ hr.Workers = append(hr.Workers, status.WorkerState{Name: w.Name, State: w.State, Restarts: w.Restarts})
+ }
+ sort.Slice(hr.Workers, func(i, j int) bool { return hr.Workers[i].Name < hr.Workers[j].Name })
+ }
if v, ok := cfg[host]; ok {
hr.ConfigPending, hr.ConfigError = true, v.reason
}
@@ -130,16 +140,24 @@ func (m *Manager) Status() status.RolloutReport {
return r
}
+// hostAsk is what one pass of askActivator learned about a host.
+type hostAsk struct {
+ transient bool
+ workers []activator.WorkerReport
+}
+
// askActivator answers, for each host among the schedulers' at the moment
// their names are read, whether its process is starting or stopping: the
// activator owns the host and has no current generation for it (a wake or an
// observation not yet current, a hibernation under way). A host the activator
// does not own is asleep, and one it has a current generation for is awake;
-// both answer false. The names are read under each scheduler.mu and the
-// activator is asked after it is released (scheduler.view's rule), so no
-// Manager or scheduler lock is held across the ask. A host missing from the
-// answer joined after the names were read, and Status reports it transient.
-func (m *Manager) askActivator(scheds []*scheduler) map[string]bool {
+// both answer false. The same ask gets the host's rootfs workers (nil for a
+// binary host or one with none). The names are read under each scheduler.mu
+// and the activator is asked after it is released (scheduler.view's rule), so
+// no Manager or scheduler lock is held across the ask. A host missing from
+// the answer joined after the names were read, and Status reports it
+// transient.
+func (m *Manager) askActivator(scheds []*scheduler) map[string]hostAsk {
var names []string
for _, s := range scheds {
s.mu.Lock()
@@ -148,13 +166,13 @@ func (m *Manager) askActivator(scheds []*scheduler) map[string]bool {
}
s.mu.Unlock()
}
- out := make(map[string]bool, len(names))
+ out := make(map[string]hostAsk, len(names))
for _, host := range names {
- if g, ok := m.d.Act.Current(host); ok && g.Build != "" {
- out[host] = false
- continue
+ a := hostAsk{workers: m.d.Act.RootfsWorkers(host)}
+ if g, ok := m.d.Act.Current(host); !ok || g.Build == "" {
+ a.transient = m.d.Act.Owns(host)
}
- out[host] = m.d.Act.Owns(host)
+ out[host] = a
}
return out
}
diff --git a/internal/status/snapshot.go b/internal/status/snapshot.go
index f6d8e74c6..b7c8f1abb 100644
--- a/internal/status/snapshot.go
+++ b/internal/status/snapshot.go
@@ -397,6 +397,19 @@ type HostRollout struct {
// field sees a transient host as Serving "" and Awake false, that is,
// asleep.
Transient bool `json:"transient,omitempty"`
+ // Workers is a rootfs instance's workers, sorted by name, as the
+ // activator reports them (Plan 3 Group W). Absent for a binary
+ // instance, an asleep one and one that declares none.
+ Workers []WorkerState `json:"workers,omitempty"`
+}
+
+// WorkerState is one worker of a rootfs instance. State is the activator's
+// word, passed through: running, restarting, crash-loop or stopped.
+// Restarts counts the restarts since the worker was last declared.
+type WorkerState struct {
+ Name string `json:"name"`
+ State string `json:"state"` // running | restarting | crash-loop | stopped
+ Restarts int `json:"restarts,omitempty"`
}
// AppRollout counts one app's attempts. Settling is canary settles and