activator: never wake or keep running a turned-off route (plans 4d)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
3 files changed,
+191
−0
internal/activator/activator.go+45 −0internal/activator/activator_disabledfor_test.go+144 −0internal/host/agent.go+2 −0
diff --git a/internal/activator/activator.go b/internal/activator/activator.go| index 97d513ce9..f6e43fe0f 100644 |
| --- a/internal/activator/activator.go |
| +++ b/internal/activator/activator.go |
| @@ -1147,6 +1147,10 @@ type Service struct { |
| // how long an instance stays awake, busy or not; 0 is none. Workers |
| // keep their own MaxRunSeconds and are never asked. |
| maxRunFor atomic.Pointer[func(registry.Route) time.Duration] |
| + // disabledFor is the plans turned-off hook (plans 4d): true for a route |
| + // a lapse turned off, which is never woken and does not stay running. |
| + // A move's probe wake is never asked. |
| + disabledFor atomic.Pointer[func(registry.Route) bool] |
| cfg Config |
| backend Backend |
| @@ -1775,6 +1779,11 @@ func (a *Service) wake(e *actEntry) { |
| if a.fences[e.host] != 0 || a.shuttingDown { |
| cancel() |
| } |
| + // A route a lapse turned off is refused like a fence (plans 4d); a |
| + // move's probe wake still runs, so moves keep working. |
| + if e.probe == nil && a.disabled(e.route) { |
| + cancel() |
| + } |
| a.mu.Unlock() |
| wakeStart := a.now() |
| w, err := a.doWake(ctx, e) |
| @@ -4666,6 +4675,13 @@ func (a *Service) sweepOnce() { |
| // below) — a stopped always-on worker has no wake source of its own. |
| // An instance is never in this arm: unflagged instances don't enter |
| // the activator. |
| + // A route a lapse turned off (plans 4d) stops, worker or instance, |
| + // whatever is in flight; never mid-command, and never a move's probe. |
| + if e.probe == nil && e.cmd == nil && a.disabled(e.route) { |
| + log.Printf("activator: %s was turned off by its plan — stopping", e.host) |
| + a.beginStopLocked(e, StopIdle) |
| + continue |
| + } |
| hibernates := !e.worker || e.route.Hibernate |
| // A held probe entry (ProbeWake) gets no traffic: the edge routes no |
| // held row. It is not idle until the target's open marks it open |
| @@ -4995,6 +5011,9 @@ func (a *Service) sweepOnce() { |
| if _, live := a.entries[rt.Host]; live { |
| continue |
| } |
| + if a.disabled(rt) { |
| + continue // turned off by its plan (plans 4d): its schedule waits |
| + } |
| // mesh seam: in the multi-box mesh only the HRW owner due-wakes a host — |
| // otherwise every box sweeping a due entry would race a wake per tick |
| // (the lease would let exactly one win, but loudly, forever). Eleven: |
| @@ -5046,6 +5065,9 @@ func (a *Service) sweepOnce() { |
| if _, live := a.entries[rt.Host]; live { |
| continue // awake, waking, or draining a stop — none of those is stranded |
| } |
| + if a.disabled(rt) { |
| + continue // turned off by its plan (plans 4d); starts again when it is back on |
| + } |
| log.Printf("activator: always-on sidecar %s is not running — starting it", rt.Host) |
| e := a.newWorkerEntry(rt) |
| a.entries[rt.Host] = e |
| @@ -5366,6 +5388,10 @@ func (a *Service) ServeScheduled(w http.ResponseWriter, r *http.Request, rt regi |
| maintenanceResponse(w, r) |
| return true |
| } |
| + if a.disabled(rt) { |
| + maintenanceResponse(w, r) // turned off by its plan (plans 4d) |
| + return true |
| + } |
| return a.serve(w, r, rt, proxy, false) |
| } |
| @@ -5506,6 +5532,25 @@ func (a *Service) SetIdleFor(f func(registry.Route) time.Duration) { |
| a.idleFor.Store(&f) |
| } |
| +// SetDisabledFor installs the plans turned-off hook (plans 4d): a route it |
| +// answers true for is never woken (a request, a due time, the always-on |
| +// arm, a scheduled run) and a live entry for it stops at the next sweep, |
| +// unless mid-command. A move's probe wake is never refused. nil, or a hook |
| +// answering false, changes nothing. |
| +func (a *Service) SetDisabledFor(f func(registry.Route) bool) { |
| + if f == nil { |
| + a.disabledFor.Store(nil) |
| + return |
| + } |
| + a.disabledFor.Store(&f) |
| +} |
| + |
| +// disabled asks the turned-off hook about rt; false without one. |
| +func (a *Service) disabled(rt registry.Route) bool { |
| + f := a.disabledFor.Load() |
| + return f != nil && (*f)(rt) |
| +} |
| + |
| // SetMaxRunFor installs the plans continuous-awake hook (plans 4c): an |
| // instance awake for that long is put to sleep even while busy, and wakes |
| // on its next request. nil, and a return of 0, mean no ceiling. |
diff --git a/internal/activator/activator_disabledfor_test.go b/internal/activator/activator_disabledfor_test.go| new file mode 100644 |
| index 000000000..ddd438d9a |
| --- /dev/null |
| +++ b/internal/activator/activator_disabledfor_test.go |
| @@ -0,0 +1,144 @@ |
| +package activator |
| + |
| +import ( |
| + "context" |
| + "errors" |
| + "net/http" |
| + "net/http/httptest" |
| + "strings" |
| + "testing" |
| + "time" |
| + |
| + "github.com/carlosframework/platform/internal/edge" |
| + "github.com/carlosframework/platform/internal/registry" |
| +) |
| + |
| +// The plans turned-off hook (plans 4d): a route a lapse turned off is never |
| +// woken, and a running one stops. A move's probe wake still works. |
| + |
| +func disabledFor(hosts ...string) func(registry.Route) bool { |
| + set := map[string]bool{} |
| + for _, h := range hosts { |
| + set[h] = true |
| + } |
| + return func(rt registry.Route) bool { return set[rt.Host] } |
| +} |
| + |
| +func TestDisabledForRefusesAWake(t *testing.T) { |
| + t.Parallel() |
| + a, b, _, _ := testActivator(t, 15*time.Minute) |
| + a.SetDisabledFor(disabledFor("one.example.com")) |
| + if release, err := a.beginRequest(context.Background(), testRoute("one.example.com")); err == nil { |
| + release() |
| + t.Fatal("a turned-off route woke") |
| + } |
| + if _, _, starts := b.counts(); starts != 0 { |
| + t.Fatalf("starts = %d, want 0", starts) |
| + } |
| +} |
| + |
| +func TestDisabledForSkipsDueAndAlwaysOn(t *testing.T) { |
| + t.Parallel() |
| + a, b, clk, reg := testActivator(t, time.Hour) |
| + due := testWorkerRoute("due.x.internal") |
| + if err := reg.Upsert(due); err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := reg.SetNextDue(due.Host, clk.now().Unix()-1); err != nil { |
| + t.Fatal(err) |
| + } |
| + inst := testRoute("due.example.com") |
| + inst.Status = "ready" |
| + if err := reg.Upsert(inst); err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := reg.SetNextDue(inst.Host, clk.now().Unix()-1); err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := reg.Upsert(alwaysOnWorkerRoute("bot.x.internal")); err != nil { |
| + t.Fatal(err) |
| + } |
| + a.SetDisabledFor(disabledFor("due.x.internal", "due.example.com", "bot.x.internal")) |
| + for i := 0; i < 3; i++ { |
| + a.sweepOnce() |
| + clk.advance(time.Minute) |
| + } |
| + time.Sleep(100 * time.Millisecond) |
| + if n := b.workerStarts(); n != 0 || b.startCount() != 0 || a.entryCount() != 0 { |
| + t.Fatalf("worker starts %d, starts %d, entries %d; want nothing started", n, b.startCount(), a.entryCount()) |
| + } |
| + // Turned back on: the next sweep starts them. |
| + a.SetDisabledFor(disabledFor()) |
| + a.sweepOnce() |
| + waitFor(t, "the workers back on", func() bool { return b.workerStarts() == 2 }) |
| +} |
| + |
| +func TestServeScheduledRefusesDisabled(t *testing.T) { |
| + t.Parallel() |
| + a, b, _, _ := testActivator(t, time.Hour) |
| + a.SetDisabledFor(disabledFor("one.example.com")) |
| + req := httptest.NewRequest(http.MethodPost, "https://one.example.com/_carlos/run", nil) |
| + req, answered := edge.WithAnswerFlag(req) |
| + out := httptest.NewRecorder() |
| + proxied := false |
| + proxy := http.HandlerFunc(func(http.ResponseWriter, *http.Request) { proxied = true }) |
| + if !a.ServeScheduled(out, req, testRoute("one.example.com"), proxy) || proxied || !*answered || out.Code != http.StatusServiceUnavailable { |
| + t.Fatalf("scheduled request to a turned-off route: code %d proxied %v answered %v", out.Code, proxied, *answered) |
| + } |
| + if _, _, starts := b.counts(); starts != 0 { |
| + t.Fatalf("starts = %d, want 0", starts) |
| + } |
| +} |
| + |
| +func TestDisabledForStopsALiveEntry(t *testing.T) { |
| + t.Parallel() |
| + a, _, clk, _ := testActivator(t, time.Hour) |
| + release, err := a.beginRequest(context.Background(), testRoute("one.example.com")) |
| + if err != nil { |
| + t.Fatalf("beginRequest: %v", err) |
| + } |
| + defer release() // busy: only the hook can stop it |
| + busy := testRoute("cmd.example.com") |
| + wakeInstance(t, a, busy.Host) |
| + a.mu.Lock() |
| + a.entries[busy.Host].cmd = &cmdHold{} |
| + a.mu.Unlock() |
| + a.SetDisabledFor(disabledFor("one.example.com", "cmd.example.com")) |
| + clk.advance(time.Second) |
| + a.sweepOnce() |
| + waitFor(t, "the turned-off instance stopped", func() bool { return !a.Owns("one.example.com") }) |
| + if !a.entryAwake(busy.Host) { |
| + t.Fatal("a mid-command entry was stopped") |
| + } |
| + a.mu.Lock() |
| + a.entries[busy.Host].cmd = nil |
| + a.mu.Unlock() |
| + a.sweepOnce() |
| + waitFor(t, "the entry stopped once its command ended", func() bool { return !a.Owns(busy.Host) }) |
| +} |
| + |
| +func TestDisabledForLeavesProbeWakes(t *testing.T) { |
| + r := &pwRuntime{observed: pwFlip(), stop: errors.New("materialize failed")} |
| + a, _ := pwService(t, r) |
| + a.SetDisabledFor(func(registry.Route) bool { return true }) |
| + cfg := pwConfig() |
| + _, _ = a.ProbeWake(t.Context(), cfg) |
| + if got := strings.Join(r.calls, ","); got != "prerequisites,admit,prepare,restore,reserve,materialize,failed" { |
| + t.Fatalf("a probe wake on a turned-off route did not run: %s", got) |
| + } |
| +} |
| + |
| +func TestDisabledForNilUnchanged(t *testing.T) { |
| + t.Parallel() |
| + a, b, _, _ := testActivator(t, time.Hour) |
| + a.SetDisabledFor(nil) |
| + wakeInstance(t, a, "one.example.com") |
| + if _, _, starts := b.counts(); starts != 1 { |
| + t.Fatalf("starts = %d, want 1", starts) |
| + } |
| + a.SetDisabledFor(func(registry.Route) bool { return false }) |
| + wakeInstance(t, a, "two.example.com") |
| + if _, _, starts := b.counts(); starts != 2 { |
| + t.Fatalf("a hook answering false: starts = %d, want 2", starts) |
| + } |
| +} |
diff --git a/internal/host/agent.go b/internal/host/agent.go| index 916c032fa..a4230ffba 100644 |
| --- a/internal/host/agent.go |
| +++ b/internal/host/agent.go |
| @@ -501,6 +501,8 @@ func runAgent(args []string, buildClaims ClaimFactory) { |
| // answers 0 and every window is the box's own. |
| svc.SetIdleFor(planIdleFor) |
| svc.SetMaxRunFor(planMaxRunFor) // plans 4c: the continuous-awake ceiling |
| + // Plans 4d: what a lapse turned off is never woken, and stops. |
| + svc.SetDisabledFor(func(rt registry.Route) bool { return planDisabledFor(rt, rt.Host) }) |
| // Settings spec §4.2: before any process runs a build against an |
| // instance's database, the build state names it, under the lease. A |
| // store that failed to start refuses every spawn (never "run |