carlos / platform Public

activator: resolve a failed switched copy and unreadable workers (#511)

A copy whose web generation failed after its switch and before its
ACK (persistently unhealthy, exited, or a failed cold recovery) never
answered its permit, so the deploy and any rollback stayed refused
(Astra I3). A worker declaration that could not be read registered
nothing and proved clean (Astra I4).

ReplicaSwap now remembers the permit it switched under. A later step
that finds that generation exited, gone (always-on rows only), or
unhealthy past replicaWebFailBound stops it with proof
(FenceAndStopAs, fence lifted after) and answers switched with the
build failed. The ack carries web_failed; NextDeploy completes the box
and halts, as for workers. The scheduler runs a sleeping permit
holder's step, which swaps nothing. The console says so (SR-4-C79).

registerRootfsWorkers marks a failed registration (unreadable or
invalid declaration, no backend) on the entry; replicaSwitchProof
answers it as workers failed. An empty declaration still proves clean.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev c5ed4a51491473a9f02be1568c4897c82b29fbdc parent 2a2495c
15 files changed, +398 −5
  • internal/activator/activator.go +12 −0
  • internal/activator/replicadeploy.go +104 −0
  • internal/activator/replicaswitchfail_test.go +150 −0
  • internal/activator/rootfsworkers.go +15 −0
  • internal/activator/swap.go +6 −0
  • internal/console/replicadeploy.go +4 −0
  • internal/console/replicadeploy_test.go +6 −0
  • internal/host/replicadeploy.go +1 −1
  • internal/replicas/deploy.go +12 −1
  • internal/replicas/deploy_test.go +22 −0
  • internal/replicas/types.go +3 −0
  • internal/rollout/queue.go +22 −0
  • internal/rollout/replicabaseline_test.go +32 −0
  • internal/rollout/replicas.go +6 −1
  • internal/rollout/scheduler.go +3 −2
diff --git a/internal/activator/activator.go b/internal/activator/activator.go
index eed05ef26..c0488f6c5 100644
--- a/internal/activator/activator.go
+++ b/internal/activator/activator.go
@@ -901,6 +901,12 @@ type actEntry struct {
rootfsWorkers map[string]*rootfsWorker
strayRootfsWorkers []*rootfsWorker
workerProofs map[string]chan struct{}
+ // workersUnregistered is the generation whose declared workers could
+ // not be registered (registerRootfsWorkers: an unreadable or invalid
+ // declaration, or a backend that cannot run them), 0 if none. It is
+ // not an empty declaration: a replicated step answers it as switched
+ // with its workers failed (replicaSwitchProof). Written under a.mu.
+ workersUnregistered uint64
}
// shedChild is a respawn child that was exec'd and then refused at its
@@ -1309,6 +1315,12 @@ type Service struct {
// replicaFailed is, per set row, the permit whose step's successor
// failed here (ReplicaSwap): that step only proves what it kept.
replicaFailed map[string]replicaStepKey
+ // replicaSwitched is, per set row, the permit whose step switched this
+ // copy and when (ReplicaSwap): a later step of that permit that finds
+ // the switched generation failed (exited, gone, or unhealthy past
+ // replicaWebFailBound) answers switched with the build failed, once its
+ // processes are proven stopped.
+ replicaSwitched map[string]replicaSwitch
// Startup gates admission while the agent recovers installations. Set before publication.
Startup <-chan struct{}
diff --git a/internal/activator/replicadeploy.go b/internal/activator/replicadeploy.go
index 12e9bd5d6..8318e3c79 100644
--- a/internal/activator/replicadeploy.go
+++ b/internal/activator/replicadeploy.go
@@ -38,6 +38,7 @@ package activator
import (
"context"
"fmt"
+ "time"
"github.com/carlosframework/platform/internal/registry"
"github.com/carlosframework/platform/internal/release"
@@ -149,7 +150,17 @@ func (a *Service) ReplicaSwap(ctx context.Context, at Attempt, deploy replicas.D
route = e.route
}
failedHere := a.replicaFailed[at.Host] == key
+ sw, switched := a.replicaSwitched[at.Host]
+ switched = switched && sw.key == key
a.mu.Unlock()
+ if switched {
+ // This copy switched under this permit. A switched generation that
+ // failed since (exited, gone, unhealthy past its bound) answers
+ // switched with the build failed, once its stop is proven.
+ if res, decided := a.replicaSwitchFailure(ctx, at.Host, sw); decided {
+ return res
+ }
+ }
if e == nil {
return abandon(errSwapNotAwake)
}
@@ -158,6 +169,11 @@ func (a *Service) ReplicaSwap(ctx context.Context, at Attempt, deploy replicas.D
}
// Proof only: the copy already switched.
if cur, ok := a.Current(at.Host); ok && cur.Build == at.Build && cur.Digest == at.Digest {
+ if !switched {
+ // First seen after an agent restart, or a cold wake the permit
+ // allowed: its failure bound counts from now.
+ a.noteReplicaSwitch(at.Host, key, route)
+ }
return a.replicaSettle(ctx, at.Host, cur)
}
// Proof only: the step may not, or need not, swap again.
@@ -182,6 +198,7 @@ func (a *Service) ReplicaSwap(ctx context.Context, at Attempt, deploy replicas.D
res := a.Swap(ctx, at, newest)
switch res.Outcome {
case SwapDone:
+ a.noteReplicaSwitch(at.Host, key, route)
// The workers were only registered at the switch: a later step
// judges them (replicaSwitchProof), unless one has already failed.
res.Proven, res.WorkersFailed = a.replicaSwitchProof(at.Host, res.Gen)
@@ -258,6 +275,12 @@ func (a *Service) replicaSwitchProof(host string, g Gen) (proven, workersFailed
if len(e.ops.members) != 0 || len(e.strayRootfsWorkers) != 0 {
return false, false
}
+ if e.workersUnregistered == g.N {
+ // Its declared workers could not be registered (an unreadable or
+ // invalid declaration): unknown workers are not healthy workers
+ // (#511 final wave, item 4). Not an empty declaration.
+ return true, true
+ }
now, open := a.now(), false
for _, wk := range e.rootfsWorkers {
switch {
@@ -288,3 +311,84 @@ func (a *Service) replicaKeptProven(host, target string) bool {
e := a.entries[host]
return e != nil && e.state == actAwake && e.proc != nil && !e.proc.Exited() && e.gen.Build != target && len(e.ops.members) == 0
}
+
+// replicaWebFailBound is how long a switched generation may stay unhealthy
+// before its step answers switched with the build failed: the same bound
+// the step gives its workers (replicaSwitchProof).
+var replicaWebFailBound = residentPoll + workerStartBound
+
+// replicaSwitch is one permit's switch on this box (Service.replicaSwitched).
+type replicaSwitch struct {
+ key replicaStepKey
+ at time.Time
+ // resident: the row is always on, so a switched copy that is not awake
+ // did not hibernate: its generation exited or did not come back.
+ resident bool
+}
+
+// noteReplicaSwitch records that host switched under key, unless it
+// already did.
+func (a *Service) noteReplicaSwitch(host string, key replicaStepKey, rt registry.Route) {
+ a.mu.Lock()
+ defer a.mu.Unlock()
+ if cur, ok := a.replicaSwitched[host]; ok && cur.key == key {
+ return
+ }
+ if a.replicaSwitched == nil {
+ a.replicaSwitched = make(map[string]replicaSwitch)
+ }
+ a.replicaSwitched[host] = replicaSwitch{key: key, at: a.now(), resident: rt.Resident()}
+}
+
+// replicaSwitchFailure decides, for a copy that switched under sw's permit
+// before its ACK, whether its new generation has failed (#511 final wave,
+// item 3; Astra I3): the web process exited, the copy is no longer awake
+// (its cold recovery failed or it is still being recycled; only for an
+// always-on row, which never hibernates), or it has been unhealthy past
+// replicaWebFailBound since the switch. A failed one is stopped with
+// proof (FenceAndStopAs, the fence lifted again after), and the answer is
+// switched with the build failed: Proven and WebFailed. The ACK resolves
+// the permit and halts the deploy, as a worker failure does; the copy is
+// down until a retry, a fix build or a rollback. An unproven stop answers
+// nothing, and the permit stays open until a later step proves it.
+// decided is false when the generation has not failed: the step goes on.
+func (a *Service) replicaSwitchFailure(ctx context.Context, host string, sw replicaSwitch) (SwapResult, bool) {
+ a.mu.Lock()
+ e := a.entries[host]
+ failed := false
+ switch {
+ case e == nil || e.state != actAwake:
+ failed = sw.resident
+ case e.proc == nil || e.proc.Exited():
+ failed = true
+ }
+ var (
+ rt registry.Route
+ gen Gen
+ )
+ if e != nil {
+ rt, gen = e.route, e.gen
+ }
+ past := !a.now().Before(sw.at.Add(replicaWebFailBound))
+ a.mu.Unlock()
+ if !failed {
+ if !past || e == nil || e.state != actAwake {
+ return SwapResult{}, false
+ }
+ if err := a.genHealthy(ctx, rt, gen); err == nil {
+ return SwapResult{}, false
+ }
+ }
+ owner := fmt.Sprintf("replica-switch:%s:%d", sw.key.id, sw.key.permit)
+ sctx, cancel := context.WithTimeout(ctx, replicaWebFailBound)
+ _, err := a.FenceAndStopAs(sctx, host, sw.key.permit, owner)
+ cancel()
+ // The fence only proves the stop; the copy may come back afterwards.
+ a.LiftFenceAs(host, sw.key.permit, owner)
+ if err != nil {
+ res := abandon(err)
+ res.Reason = "the new build failed on this copy after it switched; its stop is not proven yet, so the step stays open"
+ return res, true
+ }
+ return SwapResult{Outcome: SwapAbandoned, Proven: true, WebFailed: true, Reason: "the new build failed on this copy after it switched; its processes are proven stopped"}, true
+}
diff --git a/internal/activator/replicaswitchfail_test.go b/internal/activator/replicaswitchfail_test.go
new file mode 100644
index 000000000..7a5cf0237
--- /dev/null
+++ b/internal/activator/replicaswitchfail_test.go
@@ -0,0 +1,150 @@
+package activator
+
+import (
+ "context"
+ "errors"
+ "testing"
+ "time"
+
+ "github.com/carlosframework/platform/internal/release"
+ "github.com/carlosframework/platform/internal/replicas"
+)
+
+// switchedRig is box b holding the permit for v2 and already switched to
+// it, its worker "jobs" registered and not yet started, so the step's
+// switch proof is still open (the worker wait).
+func switchedRig(t *testing.T) (*replicaSetRig, func() SwapResult) {
+ t.Helper()
+ prev := swapSettle
+ swapSettle = 20 * time.Millisecond
+ t.Cleanup(func() { swapSettle = prev })
+ r := newReplicaSetRig(t, "b", replicaAttempt("released", replicas.Swap{Order: []string{"a", "b"}, Completed: []string{"a"}, ActiveBox: "b", Permit: 5}))
+ // Copies need an always-on release (appcopies.go): a resident row.
+ r.rt.Hibernate, r.rt.Kind = false, "instance"
+ r.routes.set(r.rt)
+ if !r.rt.Resident() {
+ t.Fatalf("the rig's copy is not always on: %+v", r.rt)
+ }
+ workers := &fakeWorkerBackend{killBackend: &killBackend{fakeBackend: r.b.fakeBackend}}
+ r.a.backend = &statelessWakeBackend{r.b, workers}
+ r.b.procs["v2"] = map[string]release.Process{release.ProcessWeb: {Command: []string{"serve"}}, "jobs": {Command: []string{"jobs"}}}
+ r.awake(t)
+ step := func() SwapResult {
+ return r.a.ReplicaSwap(context.Background(), r.attempt(CauseRelease, ModeOverlap), *r.store.snapshot(), "b", newestOne)
+ }
+ if res := step(); res.Outcome != SwapDone || res.Proven {
+ t.Fatalf("switch: %+v", res)
+ }
+ return r, step
+}
+
+// webFailedAnswer checks a step's answer is switched with the build failed,
+// with the failed generation stopped and no fence left behind.
+func webFailedAnswer(t *testing.T, r *replicaSetRig, res SwapResult) {
+ t.Helper()
+ if !res.Proven || !res.WebFailed || res.Outcome == SwapDone {
+ t.Fatalf("not answered switched, failed: %+v", res)
+ }
+ r.a.mu.Lock()
+ e, fence := r.a.entries[r.rt.Host], r.a.fences[r.rt.Host]
+ r.a.mu.Unlock()
+ if e != nil || fence != 0 {
+ t.Fatalf("after the answer: entry %v, fence %d", e != nil, fence)
+ }
+}
+
+// TestReplicaSwitchedWebFailureResolves (#511 final wave, item 3; Astra
+// I3): a copy that switched and whose new web generation fails during the
+// worker wait, before its ACK, answers "switched, failed" once the failed
+// generation is proven stopped: persistently unhealthy past its bound, an
+// exit, and an exit whose cold recovery failed. The answer resolves the
+// permit and halts the deploy (TestReplicaSwapWebFailedAckHalts). A
+// healthy generation within the bound, and a transient unhealthy one
+// before it, answer nothing yet.
+func TestReplicaSwitchedWebFailureResolves(t *testing.T) {
+ t.Run("persistent unhealth", func(t *testing.T) {
+ r, step := switchedRig(t)
+ r.a.genHealth = func(context.Context, Gen) error { return errors.New("GET / answered 502") }
+ if res := step(); res.Proven || res.WebFailed {
+ t.Fatalf("answered before its bound: %+v", res)
+ }
+ r.clk.advance(replicaWebFailBound + time.Second)
+ webFailedAnswer(t, r, step())
+ })
+ t.Run("exit", func(t *testing.T) {
+ r, step := switchedRig(t)
+ r.a.mu.Lock()
+ n := r.a.entries[r.rt.Host].gen.N
+ r.a.mu.Unlock()
+ r.b.procOf(n).exit()
+ webFailedAnswer(t, r, step())
+ })
+ t.Run("failed cold recovery", func(t *testing.T) {
+ r, step := switchedRig(t)
+ r.a.mu.Lock()
+ n := r.a.entries[r.rt.Host].gen.N
+ r.a.mu.Unlock()
+ r.b.procOf(n).exit()
+ r.a.sweepOnce() // recycles the exited instance
+ waitFor(t, "the recycled entry", func() bool {
+ r.a.mu.Lock()
+ defer r.a.mu.Unlock()
+ return r.a.entries[r.rt.Host] == nil
+ })
+ r.b.mu.Lock()
+ r.b.startErr = errors.New("never healthy")
+ r.b.mu.Unlock()
+ r.setPlan(WakePlan{Boot: r.next, Mode: ModeOverlap, Token: 2})
+ if err := waitErr(t, r.wake(r.a)); err == nil {
+ t.Fatal("the cold recovery started")
+ }
+ webFailedAnswer(t, r, step())
+ })
+ t.Run("healthy within the bound", func(t *testing.T) {
+ _, step := switchedRig(t)
+ if res := step(); res.Proven || res.WebFailed {
+ t.Fatalf("answered during the worker wait: %+v", res)
+ }
+ })
+}
+
+// TestReplicaUnreadableWorkersAreFailedWorkers (#511 final wave, item 4;
+// Astra I4): a generation whose worker declaration could not be read
+// registered nothing, and its switch proof answers workers failed, never
+// a clean proof. A declaration read successfully with no worker in it
+// proves clean.
+func TestReplicaUnreadableWorkersAreFailedWorkers(t *testing.T) {
+ for _, tc := range []struct {
+ name string
+ failed bool
+ set func(*replicaSetRig)
+ }{
+ {"unreadable", true, func(r *replicaSetRig) { r.b.procErr["v1"] = errors.New("processes.json: unexpected end of JSON input") }},
+ {"invalid", true, func(r *replicaSetRig) {
+ r.b.procs["v1"] = map[string]release.Process{"jobs": {Command: []string{"jobs"}}}
+ }},
+ {"empty", false, func(r *replicaSetRig) {
+ r.b.procs["v1"] = map[string]release.Process{release.ProcessWeb: {Command: []string{"serve"}}}
+ }},
+ } {
+ t.Run(tc.name, func(t *testing.T) {
+ r := newReplicaSetRig(t, "b", replicaAttempt("released", replicas.Swap{Order: []string{"a", "b"}, ActiveBox: "a", Permit: 4}))
+ workers := &fakeWorkerBackend{killBackend: &killBackend{fakeBackend: r.b.fakeBackend}}
+ r.a.backend = &statelessWakeBackend{r.b, workers}
+ r.awake(t)
+ r.b.rmu.Lock()
+ tc.set(r)
+ r.b.rmu.Unlock()
+ r.a.mu.Lock()
+ e := r.a.entries[r.rt.Host]
+ g, rt := e.gen, e.route
+ r.a.mu.Unlock()
+ // The registration a wake or an overlap handover runs.
+ r.a.registerRootfsWorkers(e, rt, g, func() bool { return true })
+ proven, failed := r.a.replicaSwitchProof(r.rt.Host, g)
+ if !proven || failed != tc.failed {
+ t.Fatalf("proof: proven %t, workers failed %t; want failed %t", proven, failed, tc.failed)
+ }
+ })
+ }
+}
diff --git a/internal/activator/rootfsworkers.go b/internal/activator/rootfsworkers.go
index 6462f55a5..6928be82d 100644
--- a/internal/activator/rootfsworkers.go
+++ b/internal/activator/rootfsworkers.go
@@ -234,26 +234,41 @@ func (a *Service) registerRootfsWorkers(e *actEntry, rt registry.Route, g Gen, s
if !rt.Rootfs() || e.worker {
return
}
+ // failed marks g's registration as failed, not empty (#511 final wave,
+ // item 4): a replicated step answers it as workers failed, never as a
+ // clean proof.
+ failed := func() {
+ a.mu.Lock()
+ defer a.mu.Unlock()
+ if a.entries[e.host] == e && still() {
+ e.workersUnregistered = g.N
+ }
+ }
procs, err := a.declaredProcesses(rt, g)
if err != nil {
log.Printf("activator: %s: build %s: its declared processes cannot be read, so this box runs web alone: %v", rt.Host, g.Build, err)
+ failed()
return
}
names, err := workerNames(procs)
if err != nil {
log.Printf("activator: %s: build %s: its declared processes are not valid, so this box runs no worker of it: %v", rt.Host, g.Build, err)
+ failed()
return
}
if len(names) == 0 {
+ // A declaration read successfully that names no worker.
return
}
if _, ok := a.backend.(RootfsWorkerBackend); !ok {
log.Printf("activator: %s: build %s declares workers (%s); this backend cannot run them, so web runs alone", rt.Host, g.Build, strings.Join(names, ", "))
+ failed()
return
}
build, err := stagedIdentityOf(g)
if err != nil {
log.Printf("activator: %s: build %s: no worker registered: %v", rt.Host, g.Build, err)
+ failed()
return
}
a.mu.Lock()
diff --git a/internal/activator/swap.go b/internal/activator/swap.go
index 3f6bd968b..862a92b2e 100644
--- a/internal/activator/swap.go
+++ b/internal/activator/swap.go
@@ -116,6 +116,12 @@ type SwapResult struct {
// within the settle window: the box acknowledges it as switched with
// its workers failed, which halts the deploy (replicaSwitchProof).
WorkersFailed bool
+ // WebFailed, with Proven, is ReplicaSwap's answer for a copy that
+ // switched and whose new generation then failed (it exited, did not
+ // come back, or stayed unhealthy), once its processes are proven
+ // stopped: switched, the build failed here, which halts the deploy.
+ // Its outcome is SwapAbandoned: nothing is serving to record as good.
+ WebFailed bool
}
var (
diff --git a/internal/console/replicadeploy.go b/internal/console/replicadeploy.go
index 9f50a835c..a1992c331 100644
--- a/internal/console/replicadeploy.go
+++ b/internal/console/replicadeploy.go
@@ -40,6 +40,8 @@ const (
replicaDeployDone = "deploy complete"
// copy SR-4-C46 (provisional)
replicaDeployWorkersFailed = "the new build's workers failed on %s, so the deploy stopped. other copies keep serving"
+ // copy SR-4-C79
+ replicaDeployWebFailed = "the new build stopped working on %s after its switch, so the deploy stopped. other copies keep serving"
)
// replicaDeployStatus is the one-line deploy state of a record with a copy
@@ -62,6 +64,8 @@ func replicaDeployStatus(rec provision.Record) string {
return replicaDeployDone
case "released":
switch {
+ case d.Swap.Halted && d.Swap.WebFailed != "":
+ return fmt.Sprintf(replicaDeployWebFailed, d.Swap.WebFailed)
case d.Swap.Halted && d.Swap.WorkersFailed != "":
return fmt.Sprintf(replicaDeployWorkersFailed, d.Swap.WorkersFailed)
case d.Swap.Halted:
diff --git a/internal/console/replicadeploy_test.go b/internal/console/replicadeploy_test.go
index 8c0ad3e17..ebe9f0f1a 100644
--- a/internal/console/replicadeploy_test.go
+++ b/internal/console/replicadeploy_test.go
@@ -511,4 +511,10 @@ func TestReplicaSwapWorkersFailedStatus(t *testing.T) {
if got := replicaDeployStatus(rec); got != replicaDeployHalted {
t.Fatalf("other halt: %q", got)
}
+ // #511 final wave, item 3: a switched copy whose new build then failed
+ // says so, naming the box (SR-4-C79).
+ rec.ReplicaDeploy.Swap.WebFailed = "a"
+ if got, want := replicaDeployStatus(rec), "the new build stopped working on a after its switch, so the deploy stopped. other copies keep serving"; got != want {
+ t.Fatalf("status %q, want %q", got, want)
+ }
}
diff --git a/internal/host/replicadeploy.go b/internal/host/replicadeploy.go
index c0cca1226..77827f32f 100644
--- a/internal/host/replicadeploy.go
+++ b/internal/host/replicadeploy.go
@@ -261,7 +261,7 @@ func replicaAnswer(rec provision.Record, box string, step func(provision.Record)
if !ok {
return replicas.DeployChange{}, false
}
- return replicas.DeployChange{Op: "ack", ID: d.ID, Box: box, Revision: d.Revision, Target: d.Target, Permit: d.Swap.Permit, Switched: answer.Switched, WorkersFailed: answer.Switched && answer.WorkersFailed}, true
+ return replicas.DeployChange{Op: "ack", ID: d.ID, Box: box, Revision: d.Revision, Target: d.Target, Permit: d.Swap.Permit, Switched: answer.Switched, WorkersFailed: answer.Switched && answer.WorkersFailed, WebFailed: answer.Switched && answer.WebFailed}, true
}
// Pass runs at most one transition per noted host.
diff --git a/internal/replicas/deploy.go b/internal/replicas/deploy.go
index 3fcfc43cc..c4acc7bd1 100644
--- a/internal/replicas/deploy.go
+++ b/internal/replicas/deploy.go
@@ -32,6 +32,11 @@ type DeployChange struct {
// new build's workers crashed, restarted or did not all start within
// the settle window. The ack resolves the permit and halts the deploy.
WorkersFailed bool `json:"workers_failed,omitempty"`
+ // WebFailed, with Switched, reports that the box switched but the new
+ // build's web process then failed there (it exited, did not come back,
+ // or stayed unhealthy), and its processes are proven stopped. Like
+ // WorkersFailed, the ack resolves the permit and halts the deploy.
+ WebFailed bool `json:"web_failed,omitempty"`
}
// ReleasePassed reports a phase whose release command succeeded.
@@ -220,7 +225,7 @@ func NextDeploy(cur Deploy, change DeployChange, coordinator Coordinator, now ti
s.Permit = cur.Revision + 1
case "ack":
switch {
- case change.WorkersFailed && !change.Switched:
+ case (change.WorkersFailed || change.WebFailed) && !change.Switched:
return Deploy{}, ErrInvalid
case change.Switched:
s.Completed = append(s.Completed, s.ActiveBox)
@@ -228,6 +233,12 @@ func NextDeploy(cur Deploy, change DeployChange, coordinator Coordinator, now ti
// Switched, but its workers failed: no further permits.
s.Halted, s.WorkersFailed = true, s.ActiveBox
}
+ if change.WebFailed {
+ // Switched, but the new build then failed there: no
+ // further permits; the copy is down until a retry, a fix
+ // build or a rollback.
+ s.Halted, s.WebFailed = true, s.ActiveBox
+ }
default:
s.Halted = true
}
diff --git a/internal/replicas/deploy_test.go b/internal/replicas/deploy_test.go
index d170bd15c..0e7d41c6e 100644
--- a/internal/replicas/deploy_test.go
+++ b/internal/replicas/deploy_test.go
@@ -564,3 +564,25 @@ func TestReplicaCarryBaselinesPerBox(t *testing.T) {
t.Fatal("Clone shared Baselines")
}
}
+
+// TestReplicaSwapWebFailedAckHalts (#511 final wave, item 3): an ack that
+// the box switched and its new build then failed resolves the permit,
+// completes the box and halts the deploy, as a worker failure does. It is
+// refused without Switched.
+func TestReplicaSwapWebFailedAckHalts(t *testing.T) {
+ d := releasedDeploy()
+ d = mustNext(t, d, func() DeployChange { c := coordinatorChange(d, "order"); c.Order = []string{"a", "b"}; return c }())
+ d = mustNext(t, d, func() DeployChange { c := coordinatorChange(d, "permit"); c.Grant = "a"; return c }())
+ ack := DeployChange{Op: "ack", ID: d.ID, Box: "a", Revision: d.Revision, Target: d.Target, Permit: d.Swap.Permit, WebFailed: true}
+ if _, err := NextDeploy(d, ack, coordinatorA(), deployNow); !errors.Is(err, ErrInvalid) {
+ t.Fatalf("web failed without switched: %v", err)
+ }
+ ack.Switched = true
+ next := mustNext(t, d, ack)
+ if !next.Swap.Halted || next.PermitOutstanding() || next.Swap.WebFailed != "a" || !slices.Equal(next.Swap.Completed, []string{"a"}) || !Retryable(&next) {
+ t.Fatalf("web failure did not resolve and halt: %+v", next.Swap)
+ }
+ if _, err := NextDeploy(next, coordinatorChange(next, "permit"), coordinatorA(), deployNow); err == nil {
+ t.Fatal("a permit was granted after the failure")
+ }
+}
diff --git a/internal/replicas/types.go b/internal/replicas/types.go
index d8e7183dd..52316fd32 100644
--- a/internal/replicas/types.go
+++ b/internal/replicas/types.go
@@ -129,6 +129,9 @@ type Swap struct {
// WorkersFailed names the box whose switch completed while the new
// build's workers failed there; it halted the deploy (DeployChange).
WorkersFailed string `json:"workers_failed,omitempty"`
+ // WebFailed names the box whose switch completed and whose new build
+ // then failed there (DeployChange.WebFailed); it halted the deploy.
+ WebFailed string `json:"web_failed,omitempty"`
}
type ReleaseChange struct {
diff --git a/internal/rollout/queue.go b/internal/rollout/queue.go
index 0a62645ec..5ce4d1e76 100644
--- a/internal/rollout/queue.go
+++ b/internal/rollout/queue.go
@@ -348,6 +348,14 @@ func (q *appQueue) admit(view map[string]string, now time.Time) []dispatch {
h.serving = serving
if serving == "" {
h.wantRestart = ""
+ // A sleeping copy holding a permit still runs its step
+ // (#511 final wave, item 3): the step swaps nothing on a
+ // sleeping copy, and answers only when it proves that the
+ // generation it switched to failed and is stopped.
+ if d, ok := q.sleepingStep(h, now); ok && q.running < q.concurrency {
+ q.start(h, d)
+ out = append(out, d)
+ }
continue
}
d, ok := q.gate(h, now)
@@ -363,6 +371,20 @@ func (q *appQueue) admit(view map[string]string, now time.Time) []dispatch {
return out
}
+// sleepingStep is the permitted step of a sleeping copy of a set row, if
+// it holds this box's permit (replicaDecision), outside any deferral.
+func (q *appQueue) sleepingStep(h *hostSlot, now time.Time) (dispatch, bool) {
+ if q.replica == nil || now.Before(h.notBefore) {
+ return dispatch{}, false
+ }
+ step, _ := q.replica(h.host, "", h.wantBuild)
+ if step == "" {
+ return dispatch{}, false
+ }
+ id := Identity{Host: h.host, Cause: activator.CauseRelease, Build: step}
+ return dispatch{ID: id, SchedRev: h.buildRev, Class: classRecover, Cohort: cohortKey(step, classRecover), Replica: true, Canary: true}, true
+}
+
// gate is the per-host half of admit, shared with the worker recycle hook:
// the host's pending attempt, if its cohort lets it run now. pending does not
// read the two waits, so they are enforced here: a staging refusal holds
diff --git a/internal/rollout/replicabaseline_test.go b/internal/rollout/replicabaseline_test.go
index 212ffd7ed..fceadb047 100644
--- a/internal/rollout/replicabaseline_test.go
+++ b/internal/rollout/replicabaseline_test.go
@@ -5,6 +5,7 @@ import (
"testing"
"time"
+ "github.com/carlosframework/platform/internal/activator"
"github.com/carlosframework/platform/internal/provision"
"github.com/carlosframework/platform/internal/release"
"github.com/carlosframework/platform/internal/replicas"
@@ -142,3 +143,34 @@ func TestReplicaRetryPlanKeepsPerBoxBaseline(t *testing.T) {
}
}
}
+
+// TestReplicaSleepingPermitHolderSteps (#511 final wave, item 3): a copy
+// that holds this box's permit while asleep (its switched generation
+// exited and its cold recovery failed) still runs its step, which swaps
+// nothing; a proven "switched, failed" answer from it is the box's answer
+// to the permit. No swap event is emitted for it.
+func TestReplicaSleepingPermitHolderSteps(t *testing.T) {
+ h, ra := newReplicaHarness(t)
+ h.act.mu.Lock()
+ delete(h.act.gens, replicaHost)
+ h.act.mu.Unlock()
+ ra.mu.Lock()
+ ra.result = func(activator.Attempt) activator.SwapResult {
+ return activator.SwapResult{Outcome: activator.SwapAbandoned, Proven: true, WebFailed: true}
+ }
+ ra.mu.Unlock()
+ d := deployOf("v2", "v1", "released", replicas.Swap{Order: []string{"box-0", "box-1"}, Completed: []string{"box-0"}, ActiveBox: "box-1", Permit: 8})
+ noteReplica(h, d)
+ h.pass()
+ settle(h)
+ if ra.stepCount() == 0 {
+ t.Fatal("the sleeping permit holder never stepped")
+ }
+ answer, ok := h.m.ReplicaOutcome(replicaHost, d.ID, d.Swap.Permit)
+ if !ok || !answer.Switched || !answer.WebFailed {
+ t.Fatalf("answer %+v %t", answer, ok)
+ }
+ if plainSwaps(h, "v2") != 0 {
+ t.Fatal("a sleeping copy was swapped")
+ }
+}
diff --git a/internal/rollout/replicas.go b/internal/rollout/replicas.go
index 27d9b069d..dbab24783 100644
--- a/internal/rollout/replicas.go
+++ b/internal/rollout/replicas.go
@@ -49,6 +49,9 @@ type ReplicaActivator interface {
type ReplicaAnswer struct {
Switched bool
WorkersFailed bool
+ // WebFailed: switched, and the new build then failed here with its
+ // processes proven stopped (activator.SwapResult.WebFailed).
+ WebFailed bool
}
// replicaOutcome is this box's answer to one permit.
@@ -182,7 +185,9 @@ func (m *Manager) replicaStep(ctx context.Context, a activator.Attempt, newest f
// SwapDone with proof: switched, its workers possibly failed. SwapFailed
// with proof (the failed successor proven stopped) or a proof-only step
// (SwapAbandoned, ran nothing): kept.
- answer := ReplicaAnswer{Switched: res.Outcome == activator.SwapDone}
+ // A proven WebFailed: switched, its new generation then failed and is
+ // proven stopped (SwapAbandoned, nothing serving to record).
+ answer := ReplicaAnswer{Switched: res.Outcome == activator.SwapDone || res.WebFailed, WebFailed: res.WebFailed}
answer.WorkersFailed = answer.Switched && res.WorkersFailed
m.replicaMu.Lock()
if cur, ok := m.replicaNotes[a.Host]; ok && cur.ID == d.ID && cur.Swap.Permit == d.Swap.Permit {
diff --git a/internal/rollout/scheduler.go b/internal/rollout/scheduler.go
index 1f121848b..d03e566ce 100644
--- a/internal/rollout/scheduler.go
+++ b/internal/rollout/scheduler.go
@@ -216,8 +216,9 @@ func (s *scheduler) execute(ctx context.Context, d dispatch) {
}
if d.Replica {
// This box's permitted step of a replicated deploy (replicas.go).
- // A step that only proves an earlier switch moves nothing.
- if g, ok := m.d.Act.Current(rt.Host); !ok || g.Build != a.Build {
+ // A step that only proves an earlier switch moves nothing, and a
+ // sleeping copy's step swaps nothing.
+ if g, ok := m.d.Act.Current(rt.Host); ok && g.Build != a.Build {
m.emit(*rt, EvSwapping, "info", textSwapping(a.Build, a.Cause))
}
s.done(d, m.replicaStep(ctx, a, s.newest), a.Mode)