carlos / platform Public

activator: the startup-sweep latch and a blocked rootfs gate exercised through Service.Start's sweeper (Task 7 round 1, P2 3)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev 92e1922902aa0f7dd35909284aa95449663a343a parent bc288b2
1 file changed, +54 −16
  • internal/activator/rootfs_admission_test.go +54 −16
diff --git a/internal/activator/rootfs_admission_test.go b/internal/activator/rootfs_admission_test.go
index 90f6e8159..a18d1be34 100644
--- a/internal/activator/rootfs_admission_test.go
+++ b/internal/activator/rootfs_admission_test.go
@@ -11,6 +11,7 @@ import (
"context"
"errors"
"path/filepath"
+ "strings"
"sync"
"testing"
"time"
@@ -34,9 +35,23 @@ func (b *harnessBackend) Acquire(context.Context, registry.Route) (Lease, error)
b.mu.Lock()
defer b.mu.Unlock()
b.acquires++
- return bucketLease{rig: &bucketRig{}}, nil
+ return noopLease{}, nil
}
+// noopLease hands out a replica that does nothing: the harness's database
+// replication is not this test's question.
+type noopLease struct{}
+
+func (noopLease) Replicate(context.Context, string, time.Duration) (Rep, error) {
+ return noopRep{}, nil
+}
+func (noopLease) Release(context.Context) error { return nil }
+
+type noopRep struct{}
+
+func (noopRep) Err() error { return nil }
+func (noopRep) Close(context.Context) error { return nil }
+
func (b *harnessBackend) Restore(context.Context, registry.Route) (bool, error) {
b.mu.Lock()
defer b.mu.Unlock()
@@ -50,7 +65,10 @@ func (b *harnessBackend) counts() (int, int) {
return b.acquires, b.restores
}
-func harnessService(t *testing.T, h *rootfsCtlHarness) (*Service, *harnessBackend, *fakeClock) {
+// harnessService is a Service over the harness with the overlap scheduler
+// armed, a fake clock, and an idle window whose sweep runs every 100 ms
+// once Start is called.
+func harnessService(t *testing.T, h *rootfsCtlHarness) (*Service, *harnessBackend, *fakeClock, *registry.Registry) {
t.Helper()
reg, err := registry.Open(filepath.Join(t.TempDir(), "registry.db"))
if err != nil {
@@ -58,13 +76,13 @@ func harnessService(t *testing.T, h *rootfsCtlHarness) (*Service, *harnessBacken
}
t.Cleanup(func() { reg.Close() })
hb := &harnessBackend{SplitBackend: productionBackend(t, h.eb)}
- a := New(Config{Bucket: "test-bucket", Idle: time.Hour}, hb, reg)
+ a := New(Config{Bucket: "test-bucket", Idle: 400 * time.Millisecond}, hb, reg)
clk := &fakeClock{t: time.Unix(1_700_000_000, 0)}
a.now = clk.now
a.gens = true
a.ArmRootfs() // the scheduler armed on this boot; the sweep is the question
t.Cleanup(func() { a.Shutdown(context.Background()) })
- return a, hb, clk
+ return a, hb, clk, reg
}
// startupSweep is what internal/host's startup sweep wrapper does with the
@@ -83,9 +101,10 @@ func startupSweepFor(a *Service, h *rootfsCtlHarness) error {
// rootfs cmd unit (its cgroup populated, its stop refused). The readiness
// probes pass, yet a request, a wake that did not come through begin and
// a RunCmd cause no Acquire, no Restore and no launch (errRootfsUnswept).
-// Admission stays closed for the Service's life: after the leftover ends,
-// an hour of fake time and many sweeps later, still no Acquire and no
-// second sweep. A new Service whose startup sweep is clean wakes the
+// Admission stays closed for the Service's life, through Start's own
+// sweeper: the row is due, so every sweep tries to wake it and is refused
+// before the lease; after the leftover ends, an hour of fake time and many
+// sweeps later, still no Acquire and no second unit sweep. A new Service whose startup sweep is clean wakes the
// instance on the next request, through the rootfs generation unit. Task 8
// adds the resident tick's row.
func TestRootfsAdmissionWaitsForTheStartupProof(t *testing.T) {
@@ -94,18 +113,40 @@ func TestRootfsAdmissionWaitsForTheStartupProof(t *testing.T) {
h.leave(leftover, true)
h.runProbes()
- a, hb, clk := harnessService(t, h)
+ a, hb, clk, reg := harnessService(t, h)
+ if err := reg.Upsert(h.rt); err != nil {
+ t.Fatal(err)
+ }
+ if err := reg.SetNextDue(h.rt.Host, clk.now().Add(-time.Minute).Unix()); err != nil {
+ t.Fatal(err)
+ }
+ // startActivator's order: the sweep, then Start.
if err := startupSweepFor(a, h); err == nil {
t.Fatal("the sweep proved a unit whose stop failed gone")
}
+ a.Start()
+ sweeps := func(n int) {
+ t.Helper()
+ for i := 0; i < n; i++ {
+ clk.advance(time.Minute)
+ last := a.LastSweep()
+ waitFor(t, "the sweeper to run", func() bool { return a.LastSweep().After(last) })
+ }
+ }
+ sweeps(3)
+ a.rootfsGateMu.Lock()
+ seen := a.rootfsRefusedSeen[h.rt.Host]
+ a.rootfsGateMu.Unlock()
+ if !strings.Contains(seen, "startup sweep did not prove") {
+ t.Fatalf("the sweeper's due wake was not refused by the latch (last refusal %q)", seen)
+ }
refused := func(when string) {
t.Helper()
if _, err := a.beginRequest(context.Background(), h.rt); !errors.Is(err, errRootfsUnswept) {
t.Fatalf("%s: request = %v, want errRootfsUnswept", when, err)
}
- if err := dueWake(a, h.rt); !errors.Is(err, errRootfsUnswept) {
- t.Fatalf("%s: wake = %v, want errRootfsUnswept", when, err)
- }
+ // The wake that did not come through begin is the sweeper's own
+ // due wake (above); a request or a run may coalesce on it.
_, err := a.RunCmd(context.Background(), h.rt, CmdSpec{RunID: "r1", Args: []string{"true"}})
var we *CmdWakeError
if !errors.As(err, &we) || !errors.Is(err, errRootfsUnswept) {
@@ -124,17 +165,14 @@ func TestRootfsAdmissionWaitsForTheStartupProof(t *testing.T) {
refused("after the failed sweep")
h.release(leftover)
- for i := 0; i < 60; i++ {
- clk.advance(time.Minute)
- a.sweepOnce()
- }
+ sweeps(60)
refused("an hour after the leftover ended")
if sweeps := h.callsLike("list-units"); len(sweeps) != 1 {
t.Fatalf("the box swept %d times; a sweep after Start would stop this agent's own units", len(sweeps))
}
// The agent restarts and its startup sweep is clean.
- b, hb2, _ := harnessService(t, h)
+ b, hb2, _, _ := harnessService(t, h)
if err := startupSweepFor(b, h); err != nil {
t.Fatalf("the second startup sweep: %v", err)
}