host: drive release recovery and retries on the coordinator tick (#511)
A pending or releasing attempt whose initiating operation died was held by the scheduler forever, an explicit retry stayed unconsumed when the coordinator already served the target, and a released attempt whose coordinator lost its term past the TTL stalled (Astra I2, Fable's lost-term minor). The swap worker's existing fast-tick pass gains one branch (replicaRecovery), on the record's coordinator box only: a pending or releasing attempt or an unconsumed retry is driven through rollout.Manager.DriveReplicaRelease and the activator's new ReplicaRelease, the one release path with no process swap; a released attempt with swaps remaining and no held term takes the term back. A release already in flight here costs nothing (ReplicaReleaseBusy). No new polling: the record observation and the tick, with the worker's existing backoff.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
7 files changed,
+558
−2
internal/activator/replicabaseline_test.go+71 −0internal/activator/statelessrelease.go+64 −0internal/host/agent.go+4 −0internal/host/replica_deploy_e2e_test.go+237 −0internal/host/replicadeploy.go+117 −1internal/host/replicadeploy_test.go+8 −1internal/rollout/replicas.go+57 −0
diff --git a/internal/activator/replicabaseline_test.go b/internal/activator/replicabaseline_test.go| index b2e9c701b..6d02a713c 100644 |
| --- a/internal/activator/replicabaseline_test.go |
| +++ b/internal/activator/replicabaseline_test.go |
| @@ -2,6 +2,7 @@ package activator |
| import ( |
| "context" |
| + "errors" |
| "testing" |
| "time" |
| @@ -193,3 +194,73 @@ func TestReplicaRetryKeepsPerBoxBaseline(t *testing.T) { |
| t.Fatalf("b cold-started v2 after the retry failed: %v", r.b.events()) |
| } |
| } |
| + |
| +// TestReplicaReleaseDrivesWithoutSwap (#511 final wave, item 2): the |
| +// coordinator's release-only drive runs the one release path with no |
| +// process swap. A retry requested after a's workers failed is consumed by |
| +// a new attempt whose release runs once while a keeps serving v2 on the |
| +// same generation; a pending attempt whose driver is gone runs its release |
| +// once while a keeps v1; a releasing one is closed as unknown and runs |
| +// nothing. |
| +func TestReplicaReleaseDrivesWithoutSwap(t *testing.T) { |
| + drive := func(t *testing.T, r *replicaSetRig) error { |
| + t.Helper() |
| + return r.a.ReplicaRelease(context.Background(), Attempt{Host: r.rt.Host, Cause: CauseRelease, Build: "v2", StagedPath: r.next.StagedPath, Digest: r.next.Digest}) |
| + } |
| + generation := func(r *replicaSetRig) Gen { |
| + r.a.mu.Lock() |
| + defer r.a.mu.Unlock() |
| + return r.a.entries[r.rt.Host].gen |
| + } |
| + t.Run("retry while serving the target", func(t *testing.T) { |
| + r := newReplicaSetRig(t, "a", func(inc string) *replicas.Deploy { |
| + d := replicaAttempt("released", replicas.Swap{Order: []string{"a", "b"}, Completed: []string{"a"}, Halted: true, WorkersFailed: "a"})(inc) |
| + d.RetryOf = d.ID |
| + return d |
| + }) |
| + r.a.ReplicaDeployID = func(_ replicas.Identity, target replicas.Build, old *replicas.Deploy) (string, error) { |
| + if replicas.RetryRequested(old) { |
| + return old.ID + "-retry", nil |
| + } |
| + return "deploy-" + target.Version, nil |
| + } |
| + r.awakeOn(t, r.next) |
| + before, starts := generation(r), len(r.b.started()) |
| + if err := drive(t, r); err != nil { |
| + t.Fatal(err) |
| + } |
| + d := r.store.snapshot() |
| + if d.ID != "deploy-v2-retry" || d.Phase != "released" || d.Revision != 5+2 { |
| + t.Fatalf("retry not consumed once (begin, released: a binary row has no release command): %+v", d) |
| + } |
| + if after := generation(r); after.N != before.N || after.Build != "v2" || len(r.b.started()) != starts { |
| + t.Fatalf("the drive swapped: %+v -> %+v, %v", before, after, r.b.events()) |
| + } |
| + // Driving again finds the passed attempt: no second release. |
| + if err := drive(t, r); err != nil || r.store.snapshot().Revision != d.Revision { |
| + t.Fatalf("second drive: %v, %+v", err, r.store.snapshot()) |
| + } |
| + }) |
| + t.Run("pending whose driver is gone", func(t *testing.T) { |
| + r := newReplicaSetRig(t, "a", replicaAttempt("pending", replicas.Swap{})) |
| + r.awake(t) |
| + before := generation(r) |
| + if err := drive(t, r); err != nil { |
| + t.Fatal(err) |
| + } |
| + if d := r.store.snapshot(); d.ID != "deploy-v2" || d.Phase != "released" || d.Revision != 5+1 { |
| + t.Fatalf("pending not run once (its begin replayed, released): %+v", d) |
| + } |
| + if after := generation(r); after.N != before.N || after.Build != "v1" || r.started("v2") { |
| + t.Fatalf("the drive swapped: %+v -> %+v", before, after) |
| + } |
| + }) |
| + t.Run("releasing whose driver is gone", func(t *testing.T) { |
| + r := newReplicaSetRig(t, "a", replicaAttempt("releasing", replicas.Swap{})) |
| + r.awake(t) |
| + err := drive(t, r) |
| + if d := r.store.snapshot(); d.Phase != "unknown" || d.Revision != 5+1 || !errors.Is(err, ErrReplicaReleaseUnknown) { |
| + t.Fatalf("releasing not closed as unknown: %+v, %v", d, err) |
| + } |
| + }) |
| +} |
diff --git a/internal/activator/statelessrelease.go b/internal/activator/statelessrelease.go| index 603240b30..bb3c37461 100644 |
| --- a/internal/activator/statelessrelease.go |
| +++ b/internal/activator/statelessrelease.go |
| @@ -369,3 +369,67 @@ func (a *Service) prepareStatelessRelease(ctx context.Context, rt registry.Route |
| } |
| return deployID, runErr |
| } |
| + |
| +// ReplicaReleaseBusy reports whether a release operation of host is in |
| +// flight on this box: a release command running or not proven stopped, or |
| +// a release step holding the host's release lock. The coordinator's tick |
| +// asks it before it resolves anything (ReplicaRelease), so a release that |
| +// is being driven costs the tick nothing. |
| +func (a *Service) ReplicaReleaseBusy(host string) bool { |
| + if a.releaseOpen(host) { |
| + return true |
| + } |
| + lock, ok := a.replicaReleaseLocks.Load(host) |
| + if !ok { |
| + return false |
| + } |
| + mu := lock.(*sync.Mutex) |
| + if !mu.TryLock() { |
| + return true |
| + } |
| + mu.Unlock() |
| + return false |
| +} |
| + |
| +// ReplicaRelease drives the release of at.Build for a copy of an assigned |
| +// instance with no process swap (#511 final wave, item 2): the |
| +// coordinator's tick duty for a durable release intent whose initiating |
| +// operation is gone, and for an unconsumed explicit retry. It is |
| +// prepareStatelessRelease, the one release path, on the record's |
| +// observation: a pending attempt is retaken (or replayed under this |
| +// box's own term), a releasing one is closed as unknown, and a requested |
| +// retry is consumed by a new attempt whose release runs once. It never |
| +// starts, stops or switches a generation, so a coordinator that already |
| +// serves the target consumes a retry without a swap; the permits that |
| +// follow a passed release do the switching. |
| +func (a *Service) ReplicaRelease(ctx context.Context, at Attempt) error { |
| + if a.Routes == nil || a.ReplicaDeployID == nil { |
| + return replicas.ErrFenced |
| + } |
| + rt, err := a.Routes.Get(at.Host) |
| + if err != nil { |
| + return err |
| + } |
| + if rt == nil || rt.FleetBoxes == "" || !rt.Stateless { |
| + return replicas.ErrFenced |
| + } |
| + id := release.StagedIdentity{Version: at.Build, StagedPath: at.StagedPath, Digest: at.Digest, Demand: at.Demand} |
| + if at.Declaration != nil { |
| + ctx = withDeclarations(ctx, *rt, []release.RuntimeSelection{*at.Declaration}) |
| + } |
| + mode, digest, err := a.storageMode(ctx, *rt, id) |
| + if err != nil { |
| + return err |
| + } |
| + if !mode || digest == "" { |
| + return replicas.ErrFenced |
| + } |
| + observed, err := a.statelessObservation(ctx, *rt, id, digest) |
| + if err != nil { |
| + return err |
| + } |
| + launch := *rt |
| + launch.Stateless = true |
| + _, err = a.prepareStatelessRelease(observed, launch, id, a.ReplicaDeployID) |
| + return err |
| +} |
diff --git a/internal/host/agent.go b/internal/host/agent.go| index 2f1a00223..6b5483382 100644 |
| --- a/internal/host/agent.go |
| +++ b/internal/host/agent.go |
| @@ -783,6 +783,10 @@ func runAgent(args []string, buildClaims ClaimFactory) { |
| // The box's permitted step runs on its rollout scheduler; the |
| // worker acknowledges only that step's proven answer. |
| deployWorker.step = armedReplicaStep |
| + // The coordinator's tick duties (replicaRecovery): a release |
| + // whose driver is gone, or an unconsumed retry, runs on the |
| + // rollout scheduler's activator with no process swap. |
| + deployWorker.drive = armedReplicaDrive |
| armedReplicaDeploy.Store(deployWorker) |
| defer func() { armedReplicaDeploy.Store(nil); deployWorker.close() }() |
| defer armedStatelessBindings.Store(nil) |
diff --git a/internal/host/replica_deploy_e2e_test.go b/internal/host/replica_deploy_e2e_test.go| index 24b559326..3ffdebb0a 100644 |
| --- a/internal/host/replica_deploy_e2e_test.go |
| +++ b/internal/host/replica_deploy_e2e_test.go |
| @@ -33,6 +33,60 @@ type e2eAct struct { |
| steps []provision.Record |
| pol replicas.Deploy |
| step func() activator.SwapResult |
| + // releases counts the release commands ReplicaRelease ran; drives |
| + // every call of it. |
| + releases, drives int |
| +} |
| + |
| +func (a *e2eAct) ReplicaReleaseBusy(string) bool { return false } |
| + |
| +// ReplicaRelease is the activator's release-only drive |
| +// (activator.Service.ReplicaRelease) over the world's real writer, as |
| +// prepareStatelessRelease takes it: a pending attempt is replayed and run, |
| +// a releasing one closed as unknown, a requested retry consumed by a new |
| +// attempt whose release runs once. It never touches the generation. |
| +func (a *e2eAct) ReplicaRelease(ctx context.Context, at activator.Attempt) error { |
| + a.mu.Lock() |
| + a.drives++ |
| + a.mu.Unlock() |
| + w := a.w |
| + d := w.record().ReplicaDeploy |
| + if d == nil || at.Build != d.Target.Version { |
| + return replicas.ErrFenced |
| + } |
| + write := func(next replicas.Deploy) error { |
| + next.Incarnation, next.Term, next.Target = w.id.Incarnation, w.term, d.Target |
| + got, err := provision.SetRecordReplicaRelease(ctx, w.s, w.id, d.Revision, next) |
| + if err == nil { |
| + d = got.ReplicaDeploy |
| + } |
| + return err |
| + } |
| + run := func(id string) error { |
| + if err := write(replicas.Deploy{ID: id, Phase: "releasing"}); err != nil { |
| + return err |
| + } |
| + a.mu.Lock() |
| + a.releases++ |
| + a.mu.Unlock() |
| + return write(replicas.Deploy{ID: id, Phase: "released"}) |
| + } |
| + switch { |
| + case d.Phase == "releasing": |
| + if err := write(replicas.Deploy{ID: d.ID, Phase: "unknown"}); err != nil { |
| + return err |
| + } |
| + return activator.ErrReplicaReleaseUnknown |
| + case d.Phase == "pending": |
| + return run(d.ID) |
| + case replicas.RetryRequested(d) && replicas.Retryable(d): |
| + id := d.ID + "-retry" |
| + if err := write(replicas.Deploy{ID: id, Phase: "pending", RetryOf: d.ID}); err != nil { |
| + return err |
| + } |
| + return run(id) |
| + } |
| + return nil |
| } |
| func (a *e2eAct) Current(string) (activator.Gen, bool) { |
| @@ -193,6 +247,7 @@ func newE2EBox(t *testing.T, w *deployStoreWorld, client replicaDeployClient, la |
| worker.terms = fixedTerm(w.term) |
| } |
| worker.step = replicaStepOf(mgr) |
| + worker.drive = mgr.DriveReplicaRelease |
| t.Cleanup(worker.close) |
| return &e2eBox{label: label, act: act, reg: reg, mgr: mgr, worker: worker, notify: replicaObserved(nil, worker, mgr)} |
| } |
| @@ -403,3 +458,185 @@ func TestReplicaSwapWorkersFailedHalts(t *testing.T) { |
| t.Fatalf("a fix build could not begin after the halt: %v", err) |
| } |
| } |
| + |
| +// acquiringTerms is a coordination client whose term was lost: HeldTerm |
| +// answers "" until Acquire takes it back. |
| +type acquiringTerms struct { |
| + mu sync.Mutex |
| + term string |
| + want string |
| + acquires int |
| +} |
| + |
| +func (f *acquiringTerms) HeldTerm(replicas.Identity) string { |
| + f.mu.Lock() |
| + defer f.mu.Unlock() |
| + return f.term |
| +} |
| + |
| +func (f *acquiringTerms) Acquire(context.Context, replicas.Identity) (replicas.Authority, error) { |
| + f.mu.Lock() |
| + defer f.mu.Unlock() |
| + f.acquires++ |
| + f.term = f.want |
| + return nil, nil |
| +} |
| + |
| +// waitDrives lets a background drive the worker started finish. |
| +func waitDrives(b *e2eBox) { |
| + for i := 0; i < 100; i++ { |
| + b.worker.mu.Lock() |
| + n := len(b.worker.driving) |
| + b.worker.mu.Unlock() |
| + if n == 0 { |
| + return |
| + } |
| + time.Sleep(5 * time.Millisecond) |
| + } |
| +} |
| + |
| +// roundDriven is round, then waits for the drives it started, then |
| +// clears the worker backoff so the next round looks again. |
| +func roundDriven(t *testing.T, w *deployStoreWorld, boxes ...*e2eBox) { |
| + t.Helper() |
| + round(t, w, boxes...) |
| + for _, b := range boxes { |
| + waitDrives(b) |
| + b.worker.mu.Lock() |
| + for host, p := range b.worker.pending { |
| + p.retry, p.delay = time.Time{}, 0 |
| + b.worker.pending[host] = p |
| + } |
| + b.worker.mu.Unlock() |
| + } |
| +} |
| + |
| +// TestReplicaCoordinatorTickDrivesRelease (#511 final wave, item 2; Astra |
| +// I2, Fable's minor on a lost term): the coordinator's existing fast tick |
| +// (the swap worker's Pass, fed by the reconcile observation) drives |
| +// durable release intent whether or not a process swap is needed, through |
| +// the production wiring: worker, then rollout.Manager.DriveReplicaRelease, |
| +// then the activator's release path. |
| +// |
| +// - A retry after a's workers failed: a already serves the target, so |
| +// no plain attempt runs, yet the retry is consumed once and the new |
| +// attempt swaps through to done. b never drives. |
| +// - A pending attempt whose driver is gone is run once; a releasing one |
| +// is closed as unknown with no release. |
| +// - A released attempt with swaps remaining whose term the coordinator |
| +// lost takes it back on the tick and goes on to done. |
| +func TestReplicaCoordinatorTickDrivesRelease(t *testing.T) { |
| + t.Run("retry while serving the target", func(t *testing.T) { |
| + w := newDeployStoreWorld(t) |
| + client := &e2eClient{w: w, fresh: []string{"a", "b"}} |
| + a, b := newE2EBox(t, w, client, "a", true), newE2EBox(t, w, client, "b", false) |
| + a.act.step = func() activator.SwapResult { |
| + return activator.SwapResult{Outcome: activator.SwapDone, Proven: true, WorkersFailed: true} |
| + } |
| + runUntil(t, w, "the worker-failure halt", func(rec provision.Record) bool { return rec.ReplicaDeploy.Swap.Halted }, a, b) |
| + if a.act.serving() != "v2" { |
| + t.Fatalf("a serves %s", a.act.serving()) |
| + } |
| + a.act.mu.Lock() |
| + a.act.step = nil |
| + a.act.mu.Unlock() |
| + if _, err := provision.RequestRecordReplicaRetry(context.Background(), w.s, w.id, "deploy-2", "req-1"); err != nil { |
| + t.Fatal(err) |
| + } |
| + // The coordinator's tick consumes the retry with no swap. (b's |
| + // scheduler meeting the requested retry first only defers its own |
| + // attempt, abandonDeferral, which this test does not wait out.) |
| + for i := 0; i < 50 && w.record().ReplicaDeploy.ID == "deploy-2"; i++ { |
| + roundDriven(t, w, a) |
| + } |
| + if plain, _ := a.act.counts(); slices.Contains(plain, "v2") { |
| + t.Fatalf("the coordinator ran a plain swap onto the build it serves: %v", plain) |
| + } |
| + for i := 0; i < 200 && !deployDone(w.record()); i++ { |
| + roundDriven(t, w, a, b) |
| + } |
| + d := w.record().ReplicaDeploy |
| + if d.ID != "deploy-2-retry" || d.RetryOf != "deploy-2" || d.Phase != "done" { |
| + t.Fatalf("the retry was not consumed and swapped through: %+v", d) |
| + } |
| + if a.act.releases != 1 || b.act.drives != 0 || b.act.releases != 0 { |
| + t.Fatalf("releases a=%d b=%d, b drives %d", a.act.releases, b.act.releases, b.act.drives) |
| + } |
| + }) |
| + t.Run("pending whose driver is gone", func(t *testing.T) { |
| + w := newDeployStoreWorldAt(t, "pending") |
| + client := &e2eClient{w: w, fresh: []string{"a", "b"}} |
| + a, b := newE2EBox(t, w, client, "a", true), newE2EBox(t, w, client, "b", false) |
| + for i := 0; i < 200 && !deployDone(w.record()); i++ { |
| + roundDriven(t, w, a, b) |
| + } |
| + if d := w.record().ReplicaDeploy; d.ID != "deploy-2" || d.Phase != "done" || a.act.releases != 1 || b.act.drives != 0 { |
| + t.Fatalf("pending not driven once: %+v (a releases %d, b drives %d)", d, a.act.releases, b.act.drives) |
| + } |
| + }) |
| + t.Run("releasing whose driver is gone", func(t *testing.T) { |
| + w := newDeployStoreWorldAt(t, "pending", "releasing") |
| + client := &e2eClient{w: w, fresh: []string{"a", "b"}} |
| + a, b := newE2EBox(t, w, client, "a", true), newE2EBox(t, w, client, "b", false) |
| + for i := 0; i < 20; i++ { |
| + roundDriven(t, w, a, b) |
| + } |
| + if d := w.record().ReplicaDeploy; d.Phase != "unknown" || a.act.releases != 0 || b.act.drives != 0 { |
| + t.Fatalf("releasing not closed as unknown: %+v (a releases %d, b drives %d)", d, a.act.releases, b.act.drives) |
| + } |
| + }) |
| + t.Run("lost term", func(t *testing.T) { |
| + w := newDeployStoreWorld(t) |
| + client := &e2eClient{w: w, fresh: []string{"a", "b"}} |
| + a, b := newE2EBox(t, w, client, "a", true), newE2EBox(t, w, client, "b", false) |
| + terms := &acquiringTerms{want: w.term} |
| + a.worker.terms, a.worker.acquire = terms, terms |
| + for i := 0; i < 200 && !deployDone(w.record()); i++ { |
| + roundDriven(t, w, a, b) |
| + } |
| + if !deployDone(w.record()) || terms.acquires != 1 { |
| + t.Fatalf("the lost term was not taken back: %+v (acquires %d)", w.record().ReplicaDeploy, terms.acquires) |
| + } |
| + }) |
| +} |
| + |
| +// TestReplicaRecoveryDuty (#511 final wave, item 2): the coordinator's |
| +// tick duty is decided from the record and the held term, on the record's |
| +// coordinator box only. |
| +func TestReplicaRecoveryDuty(t *testing.T) { |
| + now := time.Now() |
| + rec := func(phase string, swap replicas.Swap, coordinator string, expires time.Duration, retry bool) provision.Record { |
| + d := &replicas.Deploy{ID: "deploy-2", Target: replicas.Build{Version: "v2", Digest: "c2"}, Phase: phase, Term: "t1", Swap: swap} |
| + if retry { |
| + d.RetryOf = d.ID |
| + } |
| + return provision.Record{FleetBox: "a", ReplicaAssignment: &replicas.Assignment{Desired: 2, Revision: 1, Boxes: []string{"a", "b"}}, |
| + Coordinator: &replicas.Coordinator{Box: coordinator, Term: "t1", ExpiresAt: now.Add(expires)}, ReplicaDeploy: d} |
| + } |
| + halted := replicas.Swap{Order: []string{"a", "b"}, Completed: []string{"a"}, Halted: true} |
| + cases := []struct { |
| + name string |
| + rec provision.Record |
| + box string |
| + term string |
| + want string |
| + }{ |
| + {"pending on the coordinator", rec("pending", replicas.Swap{}, "a", time.Minute, false), "a", "t1", "drive"}, |
| + {"releasing on the coordinator", rec("releasing", replicas.Swap{}, "a", time.Minute, false), "a", "", "drive"}, |
| + {"pending, another box's live term", rec("pending", replicas.Swap{}, "a", time.Minute, false), "b", "", ""}, |
| + {"pending, expired term, successor", rec("pending", replicas.Swap{}, "b", -time.Minute, false), "a", "", "drive"}, |
| + {"retry while serving", rec("released", halted, "a", time.Minute, true), "a", "t1", "drive"}, |
| + {"retry, not coordinator", rec("released", halted, "a", time.Minute, true), "b", "", ""}, |
| + {"halted, no retry", rec("released", halted, "a", time.Minute, false), "a", "t1", ""}, |
| + {"released, term lost", rec("released", replicas.Swap{Order: []string{"a", "b"}, Completed: []string{"a"}}, "a", time.Minute, false), "a", "", "acquire"}, |
| + {"released, term held", rec("released", replicas.Swap{Order: []string{"a", "b"}, Completed: []string{"a"}}, "a", time.Minute, false), "a", "t1", ""}, |
| + {"released, permit out", rec("released", replicas.Swap{Order: []string{"a", "b"}, ActiveBox: "b", Permit: 3}, "a", time.Minute, false), "a", "", ""}, |
| + {"failed", rec("failed", replicas.Swap{}, "a", time.Minute, false), "a", "t1", ""}, |
| + {"done", rec("done", replicas.Swap{}, "a", time.Minute, false), "a", "", ""}, |
| + } |
| + for _, c := range cases { |
| + if got := replicaRecovery(c.rec, c.box, c.term, now); got != c.want { |
| + t.Errorf("%s: %q, want %q", c.name, got, c.want) |
| + } |
| + } |
| +} |
diff --git a/internal/host/replicadeploy.go b/internal/host/replicadeploy.go| index 12b81a86b..c0cca1226 100644 |
| --- a/internal/host/replicadeploy.go |
| +++ b/internal/host/replicadeploy.go |
| @@ -35,6 +35,12 @@ type heldTerms interface { |
| HeldTerm(replicas.Identity) string |
| } |
| +// termAcquirer is the coordination client's acquire, which the worker |
| +// uses only to take back a term its released attempt still needs. |
| +type termAcquirer interface { |
| + Acquire(context.Context, replicas.Identity) (replicas.Authority, error) |
| +} |
| + |
| type replicaDeployWork struct { |
| rec provision.Record |
| retry time.Time |
| @@ -64,6 +70,14 @@ type replicaDeployWorker struct { |
| // only after that step proved the switch (replicaStepOf). Nil, or no |
| // answer yet: the permit stays open and nothing is sent. |
| step func(provision.Record) (rollout.ReplicaAnswer, bool) |
| + // acquire takes the coordinator term back for a released attempt |
| + // whose remaining swaps need it (replicaRecovery). |
| + acquire termAcquirer |
| + // drive runs the record's release with no process swap |
| + // (rollout.Manager.DriveReplicaRelease), once per host at a time |
| + // (driving), off the pass. |
| + drive func(context.Context, provision.Record) error |
| + driving map[string]bool |
| } |
| var armedReplicaDeploy atomic.Pointer[replicaDeployWorker] |
| @@ -73,6 +87,7 @@ func newReplicaDeployWorker(client replicaDeployClient, box string, svc *activat |
| w := &replicaDeployWorker{client: client, box: box, ctx: ctx, cancel: cancel, now: time.Now} |
| if svc != nil { |
| w.terms, _ = svc.Coordinator.(heldTerms) |
| + w.acquire, _ = svc.Coordinator.(termAcquirer) |
| } |
| return w |
| } |
| @@ -153,6 +168,79 @@ func replicaDeployStep(rec provision.Record, box, term string) (replicas.DeployC |
| return change, true |
| } |
| +// replicaRecovery is the coordinator's tick duty for rec when no swap |
| +// transition is due (#511 final wave, item 2), decided only from the |
| +// durable record and the term this process holds, and only on the box the |
| +// record names as its coordinator now (provision.ReplicaCoordinatorBox): |
| +// |
| +// - "drive": a pending or releasing attempt, whose initiating operation |
| +// may be gone (lost term, interrupted, agent restart), or an explicit |
| +// retry not yet consumed. The release path retakes pending, closes |
| +// releasing as unknown, or consumes the retry, whether or not this |
| +// copy needs a process swap (a coordinator that already serves the |
| +// target still consumes a retry). A release in flight here is left |
| +// alone (ReplicaReleaseBusy). |
| +// - "acquire": a released attempt with swaps remaining whose coordinator |
| +// term this process does not hold (lost past its TTL, or never |
| +// taken by this agent): it takes the term back, so ordering, permits |
| +// and done go on. |
| +// |
| +// "" otherwise. It adds no polling: the worker runs it on the fast tick, |
| +// from the records the reconcile pass observed, with the same backoff as |
| +// a refused transition. |
| +func replicaRecovery(rec provision.Record, box, term string, now time.Time) string { |
| + d, a := rec.ReplicaDeploy, rec.ReplicaAssignment |
| + if d == nil || a == nil || !a.Contains(box) || provision.ReplicaCoordinatorBox(&rec, now) != box { |
| + return "" |
| + } |
| + switch { |
| + case d.Phase == "pending" || d.Phase == "releasing": |
| + return "drive" |
| + case replicas.RetryRequested(d) && replicas.Retryable(d): |
| + return "drive" |
| + case d.Phase == "released" && !d.Swap.Halted && d.Swap.ActiveBox == "" && (rec.Coordinator == nil || term == "" || rec.Coordinator.Term != term): |
| + return "acquire" |
| + } |
| + return "" |
| +} |
| + |
| +// recover runs replicaRecovery's duty for rec: an acquire inline (one |
| +// request, which the client's own retry window bounds), a drive in the |
| +// background, at most one per host. |
| +func (w *replicaDeployWorker) recover(ctx context.Context, rec provision.Record, id replicas.Identity, duty string) { |
| + switch duty { |
| + case "acquire": |
| + if w.acquire == nil { |
| + return |
| + } |
| + rctx, cancel := context.WithTimeout(ctx, 30*time.Second) |
| + // The new term reaches the record, and so replicaDeployStep, |
| + // through the next reconcile pass. |
| + _, _ = w.acquire.Acquire(rctx, id) |
| + cancel() |
| + case "drive": |
| + if w.drive == nil { |
| + return |
| + } |
| + w.mu.Lock() |
| + if w.stopped || w.driving[rec.Host] { |
| + w.mu.Unlock() |
| + return |
| + } |
| + if w.driving == nil { |
| + w.driving = map[string]bool{} |
| + } |
| + w.driving[rec.Host] = true |
| + w.wg.Add(1) |
| + w.mu.Unlock() |
| + go func() { |
| + defer w.wg.Done() |
| + defer func() { w.mu.Lock(); delete(w.driving, rec.Host); w.mu.Unlock() }() |
| + _ = w.drive(w.ctx, rec) |
| + }() |
| + } |
| +} |
| + |
| // replicaAnswer is box's ACK of its permit on rec, once step has the |
| // answer of the box's permitted step: Switched only when that step proved |
| // the switch (the new generation healthy through its settle window, the |
| @@ -202,7 +290,12 @@ func (w *replicaDeployWorker) Pass(ctx context.Context) { |
| if d.Swap.ActiveBox == w.box { |
| change, ok = replicaAnswer(rec, w.box, w.step) |
| } else if w.terms != nil { |
| - change, ok = replicaDeployStep(rec, w.box, w.terms.HeldTerm(id)) |
| + term := w.terms.HeldTerm(id) |
| + change, ok = replicaDeployStep(rec, w.box, term) |
| + if duty := replicaRecovery(rec, w.box, term, now); !ok && duty != "" { |
| + w.recover(ctx, rec, id, duty) |
| + w.backoff(rec, now) |
| + } |
| } |
| if !ok { |
| continue |
| @@ -227,6 +320,29 @@ func (w *replicaDeployWorker) Pass(ctx context.Context) { |
| } |
| } |
| +// backoff waits before rec's host is looked at again, unless a newer |
| +// record arrives first (Note resets it), as after a refused transition. |
| +func (w *replicaDeployWorker) backoff(rec provision.Record, now time.Time) { |
| + w.mu.Lock() |
| + defer w.mu.Unlock() |
| + current, ok := w.pending[rec.Host] |
| + if !ok || !current.rec.CreatedAt.Equal(rec.CreatedAt) || current.rec.ReplicaDeploy.ID != rec.ReplicaDeploy.ID || current.rec.ReplicaDeploy.Revision != rec.ReplicaDeploy.Revision { |
| + return |
| + } |
| + current.delay = min(max(current.delay*2, time.Second), time.Minute) |
| + current.retry = now.Add(current.delay) |
| + w.pending[rec.Host] = current |
| +} |
| + |
| +// armedReplicaDrive is DriveReplicaRelease on the scheduler armed now, |
| +// looked up per call, as armedReplicaStep is. |
| +func armedReplicaDrive(ctx context.Context, rec provision.Record) error { |
| + if m := armedRollout(); m != nil { |
| + return m.DriveReplicaRelease(ctx, rec) |
| + } |
| + return nil |
| +} |
| + |
| // replicaStepOf is the swap worker's step on a box whose rollout scheduler |
| // is m: the answer m's permitted step left for the record's permit |
| // (rollout.Manager.ReplicaOutcome). The step runs on the scheduler, so it |
diff --git a/internal/host/replicadeploy_test.go b/internal/host/replicadeploy_test.go| index 65c9d0210..fe5a1a1b5 100644 |
| --- a/internal/host/replicadeploy_test.go |
| +++ b/internal/host/replicadeploy_test.go |
| @@ -41,6 +41,13 @@ func (w *deployStoreWorld) reassign(next replicas.Assignment) { |
| } |
| func newDeployStoreWorld(t *testing.T) *deployStoreWorld { |
| + t.Helper() |
| + return newDeployStoreWorldAt(t, "pending", "releasing", "released") |
| +} |
| + |
| +// newDeployStoreWorldAt is newDeployStoreWorld with deploy-2 taken |
| +// through phases only. |
| +func newDeployStoreWorldAt(t *testing.T, phases ...string) *deployStoreWorld { |
| t.Helper() |
| ctx := provision.WithRecordSequence(context.Background()) |
| s := release.NewDirStore(t.TempDir()) |
| @@ -67,7 +74,7 @@ func newDeployStoreWorld(t *testing.T) *deployStoreWorld { |
| } |
| w := &deployStoreWorld{t: t, s: s, id: id, term: r.Coordinator.Term} |
| var revision uint64 |
| - for _, phase := range []string{"pending", "releasing", "released"} { |
| + for _, phase := range phases { |
| r, err = provision.SetRecordReplicaRelease(ctx, s, id, revision, replicas.Deploy{ID: "deploy-2", Incarnation: inc, Term: w.term, Target: replicas.Build{Version: "v2", Digest: "canonical-2"}, Phase: phase}) |
| if err != nil { |
| t.Fatal(err) |
diff --git a/internal/rollout/replicas.go b/internal/rollout/replicas.go| index edc931a6c..27d9b069d 100644 |
| --- a/internal/rollout/replicas.go |
| +++ b/internal/rollout/replicas.go |
| @@ -15,6 +15,12 @@ |
| // yet on the target joins it as an ordinary move: no new release; |
| // - a new build waits while another attempt's step is open (R2). |
| // |
| +// The scheduler only holds while a release is pending or running. The |
| +// coordinator's swap worker drives durable release intent on its fast |
| +// tick (DriveReplicaRelease, #511 final wave item 2): a pending or |
| +// releasing attempt whose driver is gone, and an unconsumed retry, even |
| +// when this copy already serves the target and no swap is due. |
| +// |
| // The cohort's canary and halt rules do not order these steps: the permit |
| // does. Restart and config attempts on the serving build go on as before. |
| // A wake is planned from the same record (replicaWant): the baseline while |
| @@ -27,6 +33,7 @@ import ( |
| "github.com/carlosframework/platform/internal/activator" |
| "github.com/carlosframework/platform/internal/provision" |
| "github.com/carlosframework/platform/internal/registry" |
| + "github.com/carlosframework/platform/internal/release" |
| "github.com/carlosframework/platform/internal/replicas" |
| ) |
| @@ -203,3 +210,53 @@ func (m *Manager) replicaWant(rt registry.Route) (replicas.Build, bool) { |
| } |
| return d.Want(m.d.Box), true |
| } |
| + |
| +// ReplicaReleaser is the activator's release-only drive of a set row |
| +// (activator.Service.ReplicaRelease, ReplicaReleaseBusy). |
| +type ReplicaReleaser interface { |
| + ReplicaReleaseBusy(host string) bool |
| + ReplicaRelease(ctx context.Context, at activator.Attempt) error |
| +} |
| + |
| +// DriveReplicaRelease is the coordinator's tick duty for rec's durable |
| +// release intent (#511 final wave, item 2): it runs the release of the |
| +// record's target through the activator, with no process swap, whether or |
| +// not this copy serves that build. The swap worker calls it, on the |
| +// existing fast tick, only on the record's coordinator box and only for a |
| +// pending or releasing attempt or an unconsumed explicit retry; a release |
| +// already in flight here costs nothing. Its result is on the record; the |
| +// error is for the log. |
| +func (m *Manager) DriveReplicaRelease(ctx context.Context, rec provision.Record) error { |
| + ra, ok := m.d.Act.(ReplicaReleaser) |
| + d := rec.ReplicaDeploy |
| + if !ok || d == nil || d.Target.Version == "" { |
| + return nil |
| + } |
| + if ra.ReplicaReleaseBusy(rec.Host) { |
| + return nil |
| + } |
| + if !m.enter(true) { |
| + return nil |
| + } |
| + defer m.leave() |
| + rt, err := m.d.Routes.Get(rec.Host) |
| + if err != nil { |
| + return err |
| + } |
| + inc, err := provision.Incarnation(rec.Account, rec.App, rec.Host, rec.CreatedAt) |
| + if err != nil { |
| + return err |
| + } |
| + if rt == nil || !Managed(*rt) || rt.FleetBoxes == "" || rt.ReplicaIncarnation != inc || d.Incarnation != inc || release.RouteAccount(*rt) != rec.Account || rt.App != rec.App { |
| + return nil |
| + } |
| + a, err := m.resolve(ctx, dispatch{ID: Identity{Host: rec.Host, Cause: activator.CauseRelease, Build: d.Target.Version}}, *rt) |
| + if err != nil { |
| + return err |
| + } |
| + err = ra.ReplicaRelease(ctx, a) |
| + if err != nil { |
| + m.d.Logf("rollout: %s: the coordinator's release of %s for attempt %s: %v", rec.Host, d.Target.Version, d.ID, err) |
| + } |
| + return err |
| +} |