carlos / platform Public

host: the ingress drain closes every tenant start from step 1

A start job for a unit bound to the edge, ordered before the drain unit,
waits behind the drain unit's stop job and in replace mode can cancel the
edge's pending stop. From step 1 until the process exits the agent now
refuses every start on the box, platform and unit routes included:
fixedAdmission.startAllowed, the ExecBackend gate and a new
activator.Service.TenantStartsClosed all answer closed, so a request that
needs a wake gets today's 503 with Retry-After. Admission stays open for
running instances (W6 final review F4).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev e70dba34ace77a8a4254e29978168a45f34c9fa3 parent bf4e08f
8 files changed, +126 −5
  • internal/activator/activator.go +3 −0
  • internal/activator/group_admission.go +3 −0
  • internal/activator/group_admission_test.go +23 −0
  • internal/host/agent.go +2 −0
  • internal/host/group_admission.go +21 −2
  • internal/host/group_admission_test.go +41 −0
  • internal/host/ingressdrain.go +11 −3
  • internal/host/ingressdrain_test.go +22 −0
diff --git a/internal/activator/activator.go b/internal/activator/activator.go
index 1e95f89d3..305bdb2f9 100644
--- a/internal/activator/activator.go
+++ b/internal/activator/activator.go
@@ -1282,6 +1282,9 @@ type Service struct {
TenantAdmissionStartingFunc func() bool
TenantExempt func(registry.Route) bool
TenantAdmissionClose func()
+ // TenantStartsClosed, when it answers true, refuses every start,
+ // exempt routes included (the fixed box's ingress drain, W6).
+ TenantStartsClosed func() bool
DutyStore DutyStore
AdoptionIdentity func(context.Context, string, string) (bool, error)
diff --git a/internal/activator/group_admission.go b/internal/activator/group_admission.go
index e96d2e48f..6a84333c6 100644
--- a/internal/activator/group_admission.go
+++ b/internal/activator/group_admission.go
@@ -13,6 +13,9 @@ func (a *Service) TenantAdmissionExempt(rt registry.Route) bool {
return a.TenantExempt != nil && a.TenantExempt(rt)
}
func (a *Service) tenantStartRefusal(rt registry.Route) error {
+ if a.TenantStartsClosed != nil && a.TenantStartsClosed() {
+ return ErrTenantAdmission
+ }
if a.TenantAdmissionExempt(rt) {
return nil
}
diff --git a/internal/activator/group_admission_test.go b/internal/activator/group_admission_test.go
index 375cc9015..88dc12bb9 100644
--- a/internal/activator/group_admission_test.go
+++ b/internal/activator/group_admission_test.go
@@ -136,3 +136,26 @@ func TestAdmissionClosesBeforeAcquire(t *testing.T) {
t.Fatalf("err=%v checks=%d acquires=%d wake=%+v", err, checks, b.acquires, w)
}
}
+
+// TenantStartsClosed refuses every start, exempt routes included: the
+// fixed box's ingress drain closes starts from its step 1, because a start
+// job for a unit bound to the edge would queue behind the drain unit's
+// stop and could cancel the edge's pending stop (W6 final review F4).
+func TestTenantStartsClosedRefusesExemptRoutesToo(t *testing.T) {
+ a := New(Config{}, &fakeBackend{}, nil)
+ a.TenantExempt = func(rt registry.Route) bool { return rt.Platform }
+ platform, tenant := registry.Route{Host: "console.example.com", Platform: true}, testRoute("shop.example.com")
+ if a.tenantStartRefusal(platform) != nil || a.tenantStartRefusal(tenant) != nil {
+ t.Fatal("refused before the drain")
+ }
+ closed := true
+ a.TenantStartsClosed = func() bool { return closed }
+ for _, rt := range []registry.Route{platform, tenant} {
+ if err := a.tenantStartRefusal(rt); !errors.Is(err, ErrTenantAdmission) {
+ t.Fatalf("%s during the drain: %v", rt.Host, err)
+ }
+ }
+ if _, err := a.doWakeOnce(t.Context(), &actEntry{route: platform}); !errors.Is(err, ErrTenantAdmission) {
+ t.Fatalf("platform wake during the drain: %v", err)
+ }
+}
diff --git a/internal/host/agent.go b/internal/host/agent.go
index 83c3050fa..0953b729d 100644
--- a/internal/host/agent.go
+++ b/internal/host/agent.go
@@ -1156,6 +1156,8 @@ func runAgent(args []string, buildClaims ClaimFactory) {
ingress := newIngressDrain(func() ingressArming {
return ingressDrainArming(os.Getenv, admission, lease.CurrentSelf())
}, lbhealth.Default)
+ // Step 1 closes this box's tenant starts for good (spec §3).
+ ingress.closeStarts = admission.closeStartsForIngressDrain
if ingressDrainServed(os.Getenv, admission) {
if a := ingress.arm(); a.Answer == ingressArmed {
log.Printf("agent: ingress drain armed for %d target group(s)", len(a.Groups))
diff --git a/internal/host/group_admission.go b/internal/host/group_admission.go
index 75cbd33b3..b562f1e06 100644
--- a/internal/host/group_admission.go
+++ b/internal/host/group_admission.go
@@ -37,6 +37,9 @@ type fixedAdmission struct {
launch *groupadmission.Launch
completer groupadmission.Completer
allowed atomic.Bool
+ // startsClosed is set by the ingress drain's step 1 and never cleared:
+ // the process exits after the drain (closeStartsForIngressDrain).
+ startsClosed atomic.Bool
openOnce sync.Once
knownReaders []string
readers func() edgesfile.ReaderSnapshot
@@ -76,7 +79,23 @@ func (a *fixedAdmission) bind(s *activator.Service, b *activator.ExecBackend) {
s.TenantStartAllowed = a.startAllowed
s.TenantExempt = a.exempt
s.TenantAdmissionClose = a.Close
- b.TenantStartAllowed = func(rt registry.Route) bool { return a.exempt(rt) || a.startAllowed() }
+ s.TenantStartsClosed = a.startsClosed.Load
+ b.TenantStartAllowed = func(rt registry.Route) bool { return !a.startsClosed.Load() && (a.exempt(rt) || a.startAllowed()) }
+}
+
+// closeStartsForIngressDrain is the fixed box's ingress drain, from its
+// step 1 until the process exits (W6 spec §3): no tenant unit starts on
+// this box, platform and unit routes included. Every one of those units
+// is bound to the edge and ordered before the drain unit, so a start job
+// would wait behind the drain unit's stop job, and in replace mode could
+// cancel the edge's pending stop. A request that needs a wake here gets
+// today's 503 with Retry-After; admission stays open, so running
+// instances are served.
+func (a *fixedAdmission) closeStartsForIngressDrain() {
+ if a == nil {
+ return
+ }
+ a.startsClosed.Store(true)
}
func (a *fixedAdmission) exempt(rt registry.Route) bool {
return !a.group && (rt.Platform || rt.Unit != "" || rt.Backing == registry.BackingUnit)
@@ -113,7 +132,7 @@ func (a *fixedAdmission) applyRole(role boxroles.Role, known bool) error {
func (a *fixedAdmission) startAllowed() bool {
a.mu.Lock()
defer a.mu.Unlock()
- return !a.closed && a.refusal == "" && a.known && a.role == boxroles.Serve && a.TenantAdmitted() && !(a.group && (startFenced() || drainStartsClosed()))
+ return !a.closed && !a.startsClosed.Load() && a.refusal == "" && a.known && a.role == boxroles.Serve && a.TenantAdmitted() && !(a.group && (startFenced() || drainStartsClosed()))
}
// drainStartsClosed is the agent's own start gate while it holds drain
diff --git a/internal/host/group_admission_test.go b/internal/host/group_admission_test.go
index e87202e89..67649d07f 100644
--- a/internal/host/group_admission_test.go
+++ b/internal/host/group_admission_test.go
@@ -360,3 +360,44 @@ func TestAdmissionFlagshipMetricBytes(t *testing.T) {
}
}
}
+
+// From step 1 of an ingress drain until the process exits, the box starts
+// no tenant unit, platform and unit routes included: their units are bound
+// to the edge, and a start job would wait behind the drain unit's stop and
+// could cancel the edge's stop (W6 final review F4). Requests that need a
+// wake get 503 with Retry-After.
+func TestIngressDrainClosesEveryStart(t *testing.T) {
+ a := newFixedAdmission(admissionEnv, admissionHealth())
+ _ = a.applyRole(boxroles.Serve, true)
+ a.observe(groupadmission.Facts{EnvLoaded: true, Reconciled: true, RolePublished: true, TerminationStarted: true})
+ a.tick(t.Context())
+ s, b := &activator.Service{}, &activator.ExecBackend{}
+ a.bind(s, b)
+ routes := []registry.Route{{Host: "shop.example.com"}, {Platform: true}, {Unit: "console.service"}, {Backing: registry.BackingUnit}}
+ if !a.startAllowed() {
+ t.Fatal("admission is not open")
+ }
+ for _, rt := range routes {
+ if !b.TenantStartAllowed(rt) {
+ t.Fatalf("%+v refused before the drain", rt)
+ }
+ }
+ if s.TenantStartsClosed == nil || s.TenantStartsClosed() {
+ t.Fatal("the service's starts are closed before the drain")
+ }
+ a.closeStartsForIngressDrain()
+ if a.startAllowed() || !s.TenantStartsClosed() {
+ t.Fatal("starts open during the drain")
+ }
+ for _, rt := range routes {
+ if b.TenantStartAllowed(rt) {
+ t.Fatalf("%+v allowed during the drain", rt)
+ }
+ }
+ // Admission itself stays open: running instances are served.
+ if !a.TenantAdmitted() {
+ t.Fatal("the drain closed admission")
+ }
+ var none *fixedAdmission
+ none.closeStartsForIngressDrain()
+}
diff --git a/internal/host/ingressdrain.go b/internal/host/ingressdrain.go
index 3e6f37c01..62723ace2 100644
--- a/internal/host/ingressdrain.go
+++ b/internal/host/ingressdrain.go
@@ -259,6 +259,9 @@ type ingressDrain struct {
// exit ends it at once (os.Exit), the backstop.
terminate func() error
exit func(code int)
+ // closeStarts closes the box's tenant starts at step 1, for good
+ // (fixedAdmission.closeStartsForIngressDrain). Nil closes nothing.
+ closeStarts func()
mu sync.Mutex
edge ingressEdge
@@ -365,15 +368,20 @@ func (d *ingressDrain) at() *time.Time {
func (d *ingressDrain) run(e ingressEdge, arm ingressArming, rep *ingressReport, started time.Time, done chan struct{}) {
defer close(done)
- // Step 1: Connection: close on every response, then the health check
- // fails. Nothing is refused. The edge refuses while a region HA
- // connection drain is on, and then nothing changes.
+ // Step 1: Connection: close on every response, no tenant unit starts
+ // from here on (spec §3), then the health check fails. No request is
+ // refused for being one; one that needs a wake gets 503 Retry-After.
+ // The edge refuses while a region HA connection drain is on, and then
+ // nothing changes.
if err := e.BeginIngressDrain(); err != nil {
t := d.at()
d.note(func() { rep.Result, rep.Reason, rep.FinishedAt = "refused", err.Error(), t })
log.Printf("agent: ingress drain refused: %v", err)
return
}
+ if d.closeStarts != nil {
+ d.closeStarts()
+ }
d.health.SetIngressDrain(true)
failed := d.at()
d.note(func() { rep.HealthFailedAt = failed })
diff --git a/internal/host/ingressdrain_test.go b/internal/host/ingressdrain_test.go
index 860904179..181ef60f3 100644
--- a/internal/host/ingressdrain_test.go
+++ b/internal/host/ingressdrain_test.go
@@ -1146,3 +1146,25 @@ func TestIngressDrainWithNoPublicListenersIsUnverified(t *testing.T) {
t.Fatalf("report %+v", rep)
}
}
+
+// Step 1 closes the box's tenant starts (W6 final review F4) once the edge
+// has taken the drain, before the health check fails; a refused drain
+// closes nothing.
+func TestIngressDrainClosesStartsAtStepOne(t *testing.T) {
+ f := newIngressFixture(t, healthyUntil(6*time.Second, ingresshealth.StateUnhealthy, ingresshealth.StateUnhealthy))
+ closedWithHealth := []bool{}
+ f.d.closeStarts = func() { closedWithHealth = append(closedWithHealth, f.health.on()) }
+ f.drain(t)
+ if !slices.Equal(closedWithHealth, []bool{false}) {
+ t.Fatalf("starts closed %v (health failing at each)", closedWithHealth)
+ }
+
+ f = newIngressFixture(t, healthyUntil(0, ingresshealth.StateUnhealthy, ingresshealth.StateUnhealthy))
+ f.edge.beginErr = edge.ErrConnectionDrainOn
+ closes := 0
+ f.d.closeStarts = func() { closes++ }
+ f.drain(t)
+ if closes != 0 {
+ t.Fatal("a refused drain closed the starts")
+ }
+}