replicas: raise copies as a join, keep the serving copy (#511)
Drill 1, F3/F4 and ruling R18.
F4: the activator entry of a copy that woke at copies 1 keeps the row it
woke on, with no assignment. Raising copies converts that row in place
(registry.SeedReplicaAssignment), so ProbeCopy and ReplicaSwap refused
the running copy ("activator: maintenance fence active"): the hub saw
no healthy copy and answered 503, and box-1's permit never resolved.
adoptReplicaRow gives the entry the row's assignment columns when the
row is the one it woke on (same lifetime, shape, paths, placement);
ProbeCopy, ReplicaSwap and ConvergeAdopted call it first. The reconcile
pass also binds the row as convergeOwned left it, so the binding is no
longer refused "replicas: fenced" on the converting pass.
R18 join: at copies 1 the writer records the box that ran a passed
release in Swap.Completed (it runs the target with no permit). When
copies are raised, the coordinator's existing order/permit/ACK path is
the join: NextSwapBox skips the serving box, only added boxes get a
permit, one at a time, on the same passed attempt; no release command
runs; the join closes as done. A failed join halts with its permit
resolved.
Witnesses: TestReplicaCopyKeepsServingWhenCopiesRaised (activator),
TestReplicaCopiesOneReleaseJoins and TestReplicaBindingAfterConversion
(provision), TestReplicaJoinAfterCopiesRaised and
TestReplicaJoinWebFailedHalts (host, agent wiring and real writers).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
internal/activator/activator.go+2 −0internal/activator/replica_drill1_test.go+79 −0internal/activator/replicadeploy.go+2 −0internal/activator/replicaprobe.go+42 −0internal/host/replica_drill1_test.go+133 −0internal/provision/reconcile.go+13 −2internal/provision/replica_drill1_test.go+120 −0internal/provision/replicadeploy.go+9 −0
Intent
<task-notification> <task-id>a0399498f0a4c02e9</task-id> <tool-use-id>toolu_01PqGHno5yGEeXHiXN2z7HzV</tool-use-id> <output-file>/home/paulca/.cache/tmp/claude-1001/-home-paulca-amadan-net-carlos-platform/72128d15-58b8-452f-9f7e-8e864e8854d2/tasks/a0399498f0a4c02e9.output</output-file> <status>completed</status> <summary>Agent "Narrow fix: stateless cleanup flag" finished</summary> <note>A task-notification fires each time this agent stops with no live background children of its own. The user can send it another message and resume it, so the same task-id may notify more than once.</note> <result>Commit 871987c0 on stateless-replicas-4 fixes the stuck permit. When a stateless failed-wake cleanup proves the generation stopped, it now clears the uncertainty and completes the sidecar proof if the wake started no sidecar, as the reaper does (`internal/activator/maintenance.go`). It clears nothing when no proof ran, when a legacy start is unproven, or when the proof fails. - **Witness:** `TestReplicaRestartedHolderStartedWakeFails`, beside the old witness in `internal/activator/replicaswitchfail_test.go`. If the stop is proven, the permit resolves as switched-failed after `replicaRestartBound` and a retry is allowed. If not, `StopUncertain` stays true and the permit stays open. The proven case fails without the fix. - **Tests:** the activator `-run` set, replicas and rollout, provision Census/Plan14/Plan9 and the three cross-compiles all pass. I re-proposed `stateless-replicas-4-final` with one new claim, for `cleanupFailedWake`, and `make census-card` is clean (seal 52cf21b0). The #280 review of the changed card is still owed. - **copies=1:** affected. Every stateless cold wake takes this path, so one failed wake that had started left `StopUncertain` true until the agent restarted. The fix covers it. - **Concern (not fixed, outside the approved fix):** if cleanup can't prove the stop straight away, the reaper proves it later, but `rememberStopLocked` keeps the earlier uncertain record. `StopUncertain` then stays true until an agent restart, so the permit can still stay stuck. I confirmed this with a temporary probe that I didn't commit. - **Report:** I appended the section to `final-wave-report.md`. Git ignores that file, as it did for earlier rounds, so it is not in the commit. Scratch is removed.</result> <usage><subagent_tokens>137651</subagent_tokens><tool_uses>50</tool_uses><duration_ms>517051</duration_ms></usage> </task-notification>
<task-notification> <task-id>bg6obhin4</task-id> <tool-use-id>toolu_01DDkSUGrT5JZax3HVZ4wvWE</tool-use-id> <output-file>/home/paulca/.cache/tmp/claude-1001/-home-paulca-amadan-net-carlos-platform/72128d15-58b8-452f-9f7e-8e864e8854d2/tasks/bg6obhin4.output</output-file> <status>completed</status> <summary>Background command "Astra scoped check of the narrow fix" completed (exit code 0)</summary> </task-notification>
<task-notification> <task-id>bmqsch269</task-id> <tool-use-id>toolu_01NoK4MVW9iGX1vihJ7Pcccz</tool-use-id> <output-file>/home/paulca/.cache/tmp/claude-1001/-home-paulca-amadan-net-carlos-platform/72128d15-58b8-452f-9f7e-8e864e8854d2/tasks/bmqsch269.output</output-file> <status>completed</status> <summary>Background command "Wait for CI on 871987c0" completed (exit code 0)</summary> </task-notification>
<task-notification> <task-id>a61ddcd61d29f1392</task-id> <tool-use-id>toolu_01Dp8232QuptNqvYwnPrcPtY</tool-use-id> <output-file>/home/paulca/.cache/tmp/claude-1001/-home-paulca-amadan-net-carlos-platform/72128d15-58b8-452f-9f7e-8e864e8854d2/tasks/a61ddcd61d29f1392.output</output-file> <status>completed</status> <summary>Agent "Run the stateless replicas dev drill" finished</summary> <note>A task-notification fires each time this agent stops with no live background children of its own. The user can send it another message and resume it, so the same task-id may notify more than once.</note> <result>**STOPPED.** The R17 drill never ran: the fixture couldn't get to 2 of 2 copies. Cleanup is complete and the dev console is back on v0.34.46 (exe hash fc979d67, healthz ok). - **B2 window (on v0.34.46, before the candidate):** passed. Console was down 0.3 s, then 62.6 s. The scan covered all 15 targets, including both IE HA members; activate recorded 555 names; the floor moved v0.34.13 → v0.34.46. HA rows were unchanged before and after (held-1/held-2 10/10 200, member edges hashes, ASG, target groups, housekeeper lease, locks). - **Candidate v0.35.900:** ran only on the dev console (07:25–08:18Z) and the two drill boxes; nothing published. Pre-checks passed. - **Stop reason:** moving a healthy instance from copies 1 to 2 took it down, on two separate apps ("replicas: fenced", then "maintenance fence active"). - **Other findings:** - Every first wake runs the release before the app's config arrives, so it fails; `instances retry` refuses and only `promote --retry` recovers it. - Copies 2 was accepted even though no t4g.small could hold a standard-size copy, and the running copy was removed. - On the first app, the second copy stalled on "waiting for shared secrets". - **Deviations to check:** - I created the drill fleet and box-1 before the B2 window, because the window's scan needs the boxes and bucket to exist. - I restarted drill box-1's agent once at 07:58Z, before any drill step. It didn't help and may be a confounder for the copies failure. - **Cleanup:** deleted the fixture instances (apps trashed), candidate release objects, both boxes, the fleet bucket, SG, IAM user and local scratch. - **Left for you:** - #421 is posted as PAUSED; resume or release it. - The drill-sr fleet row and history entry remain, so the next re-complete needs `-close-fleet drill-sr`. - The canary/console history now names v0.35.900; roll the console with promote, not rollback. - Backups on sophie: `/root/console-db-backups` and `/root/hu-sr4`. Files are in /home/paulca/.cache/tmp/claude-1001/sr4-drill: - drill-report.md - progress.log - evidence/</result> <usage><subagent_tokens>354622</subagent_tokens><tool_uses>177</tool_uses><duration_ms>4780240</duration_ms></usage> </task-notification>
diff --git a/internal/activator/activator.go b/internal/activator/activator.go| index a35f11e0c..a344966bb 100644 |
| --- a/internal/activator/activator.go |
| +++ b/internal/activator/activator.go |
| @@ -3978,6 +3978,8 @@ func (a *Service) Drifted() map[string]string { |
| // this rests on does not exist for one; the sweep's convergeWorkerLocked, |
| // behind the grace, remains a worker's path onto a new build. |
| func (a *Service) ConvergeAdopted(host, ver string) bool { |
| + // A copy that woke at copies 1 and is now one of a set never cycles here. |
| + a.adoptReplicaRow(host) |
| a.mu.Lock() |
| defer a.mu.Unlock() |
| e := a.entries[host] |
diff --git a/internal/activator/replica_drill1_test.go b/internal/activator/replica_drill1_test.go| new file mode 100644 |
| index 000000000..f8378a134 |
| --- /dev/null |
| +++ b/internal/activator/replica_drill1_test.go |
| @@ -0,0 +1,79 @@ |
| +package activator |
| + |
| +import ( |
| + "context" |
| + "errors" |
| + "testing" |
| + "time" |
| + |
| + "github.com/carlosframework/platform/internal/provision" |
| + "github.com/carlosframework/platform/internal/registry" |
| + "github.com/carlosframework/platform/internal/replicas" |
| +) |
| + |
| +// TestReplicaCopyKeepsServingWhenCopiesRaised (#511 drill 1, F4): a copy |
| +// that woke at copies 1 keeps serving when copies are raised. The |
| +// reconcile pass converts its row in place into a set row |
| +// (registry.SeedReplicaAssignment); the awake entry still held the row it |
| +// woke on, with no assignment, so the hub's probe and the permitted step |
| +// refused the running copy ("activator: maintenance fence active") and the |
| +// host answered 503 while the process ran on. Now the entry takes the |
| +// row's assignment: the probe reports the same generation, a permit on |
| +// it (a record from before the copies-1 release recorded its box) is |
| +// proved on the running generation without starting anything, and |
| +// nothing stopped. |
| +func TestReplicaCopyKeepsServingWhenCopiesRaised(t *testing.T) { |
| + prev := swapSettle |
| + swapSettle = 20 * time.Millisecond |
| + t.Cleanup(func() { swapSettle = prev }) |
| + r := newWakeRelRig(t) |
| + store := wireStatelessRig(r) |
| + r.rt.Region, r.rt.Hibernate, r.rt.Stateless = "fleet/pi", false, true |
| + r.routes.set(r.rt) |
| + r.setPlan(WakePlan{Boot: r.next, Mode: ModeOverlap, Token: 1}) |
| + // An always-on copy, as every copy of a set is: the resident tick wakes it. |
| + r.a.residentLister, r.a.residentJitter = residentRows{rows: r.routes}, func() float64 { return 0 } |
| + r.a.rootfsTick() |
| + r.nextUnit(t).done("exited", "0", "success") |
| + waitFor(t, "the copies-1 copy awake", func() bool { _, ok := r.a.Current(r.rt.Host); return ok }) |
| + served, ok := r.a.Current(r.rt.Host) |
| + if !ok || served.Build != "v2" || store.snapshot().Phase != "released" { |
| + t.Fatalf("copies 1 not serving its passed release: %+v %+v events %v gens %v", served, store.snapshot(), r.b.events(), r.evs) |
| + } |
| + starts := len(r.b.started()) |
| + |
| + // copies 2: placement assigns a and b; this box's reconcile pass |
| + // converts the row it serves. |
| + inc, err := provision.Incarnation(r.rt.Account, r.rt.App, r.rt.Host, time.Unix(1, 0)) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + set := r.rt |
| + set.FleetBoxes, set.Copies, set.ReplicaRevision, set.ReplicaIncarnation, set.Stateless = `["a","b"]`, 2, 1, inc, true |
| + r.routes.set(set) |
| + r.a.Fleet = FleetWake{Mode: FleetWakeCheck, Fleet: "pi", Label: "a", Record: func(context.Context, registry.Route) (*provision.Record, error) { |
| + return nil, errors.New("the probe and the proof read no record") |
| + }} |
| + r.a.Coordinator = NewCoordinatorClient("a", "boot", func(context.Context, replicas.Identity, replicas.CoordinatorChange) (replicas.CoordinatorReply, error) { |
| + return replicas.CoordinatorReply{}, errors.New("the probe and the proof acquire nothing") |
| + }, nil) |
| + |
| + c, err := r.a.ProbeCopy(t.Context(), r.rt.Host) |
| + if err != nil { |
| + t.Fatalf("the running copy was refused by the hub's probe once copies were raised: %v", err) |
| + } |
| + if c.Generation != served.N || c.Version != "v2" || c.Incarnation != inc || c.AssignmentRevision != 1 || c.Box != "a" { |
| + t.Fatalf("probe evidence: %+v", c) |
| + } |
| + |
| + d := *store.snapshot() |
| + d.Swap = replicas.Swap{Order: []string{"a", "b"}, ActiveBox: "a", Permit: d.Revision} |
| + at := Attempt{Host: r.rt.Host, Cause: CauseRelease, Build: "v2", StagedPath: r.next.StagedPath, Digest: r.next.Digest, Mode: ModeOverlap, SchedRev: 1} |
| + res := r.a.ReplicaSwap(t.Context(), at, d, "a", newestOne) |
| + if res.Outcome != SwapDone || !res.Proven || res.Gen.N != served.N { |
| + t.Fatalf("the permit on the running copy was not proved on it: %+v", res) |
| + } |
| + if now, ok := r.a.Current(r.rt.Host); !ok || now.N != served.N || len(r.b.started()) != starts || len(r.b.releaseStarts()) != 1 { |
| + t.Fatalf("raising copies stopped, restarted or re-released the running copy: %+v starts %d->%d releases %d", now, starts, len(r.b.started()), len(r.b.releaseStarts())) |
| + } |
| +} |
diff --git a/internal/activator/replicadeploy.go b/internal/activator/replicadeploy.go| index 834ec3004..e6e15a77f 100644 |
| --- a/internal/activator/replicadeploy.go |
| +++ b/internal/activator/replicadeploy.go |
| @@ -145,6 +145,8 @@ func (a *Service) ReplicaSwap(ctx context.Context, at Attempt, deploy replicas.D |
| if box == "" || box != a.Fleet.Label || deploy.Phase != "released" || deploy.Swap.ActiveBox != box || at.Build != deploy.Target.Version { |
| return abandon(errReplicaTurn) |
| } |
| + // A copy that woke at copies 1 takes its row's assignment first. |
| + a.adoptReplicaRow(at.Host) |
| a.mu.Lock() |
| e := a.entries[at.Host] |
| var route registry.Route |
diff --git a/internal/activator/replicaprobe.go b/internal/activator/replicaprobe.go| index eaf944047..eeffba533 100644 |
| --- a/internal/activator/replicaprobe.go |
| +++ b/internal/activator/replicaprobe.go |
| @@ -8,12 +8,54 @@ import ( |
| "github.com/carlosframework/platform/internal/replicas" |
| ) |
| +// adoptReplicaRow moves a running copy onto its row's copy assignment |
| +// (#511 drill 1, F4). An entry keeps the row its wake read. A copy that |
| +// woke at copies 1 holds a row with no assignment, and raising copies |
| +// converts that same row in place (registry.SeedReplicaAssignment): only |
| +// its assignment columns change. Until the entry carries them, the copy |
| +// looked like no copy of the set, so its probe and its permitted step |
| +// refused it while it went on serving. The process is the same, so the |
| +// entry takes the row's assignment columns, and nothing else, when the |
| +// row is the one it woke on: the same row lifetime, shape, paths and |
| +// placement, now a valid assignment. It starts, stops and reads nothing |
| +// but the local registry row; any other change is left to the next wake. |
| +func (a *Service) adoptReplicaRow(host string) { |
| + if a.Routes == nil { |
| + return |
| + } |
| + a.mu.Lock() |
| + e := a.entries[host] |
| + legacy := e != nil && !e.worker && e.gen.Stateless && e.route.FleetBoxes == "" && e.route.ReplicaIncarnation == "" |
| + a.mu.Unlock() |
| + if !legacy { |
| + return |
| + } |
| + now, err := a.Routes.Get(host) |
| + if err != nil || now == nil || now.FleetBoxes == "" || !now.ValidReplicaProjection() || !now.Stateless { |
| + return |
| + } |
| + a.mu.Lock() |
| + defer a.mu.Unlock() |
| + cur := a.entries[host] |
| + if cur != e || e.worker || !e.gen.Stateless || e.route.FleetBoxes != "" || e.route.ReplicaIncarnation != "" { |
| + return |
| + } |
| + rt := e.route |
| + if wakeRouteChanged(rt, *now, false) != nil || rt.Incarnation != now.Incarnation || rt.Kind != now.Kind || rt.Region != now.Region || rt.Socket != now.Socket || rt.DBPath != now.DBPath || rt.Hibernate != now.Hibernate || rt.HA != now.HA { |
| + return |
| + } |
| + rt.FleetBoxes, rt.Copies, rt.ReplicaRevision, rt.ReplicaIncarnation = now.FleetBoxes, now.Copies, now.ReplicaRevision, now.ReplicaIncarnation |
| + rt.FleetBox, rt.FleetLabel, rt.Stateless = now.FleetBox, now.FleetLabel, true |
| + e.route = rt |
| +} |
| + |
| // ProbeCopy reads the already-serving copy. It cannot join a wake, start a |
| // process, acquire authority, read a bucket, or record activity. |
| func (a *Service) ProbeCopy(ctx context.Context, host string) (replicas.Copy, error) { |
| if err := ctx.Err(); err != nil { |
| return replicas.Copy{}, err |
| } |
| + a.adoptReplicaRow(host) |
| a.mu.Lock() |
| defer a.mu.Unlock() |
| e := a.entries[host] |
diff --git a/internal/host/replica_drill1_test.go b/internal/host/replica_drill1_test.go| new file mode 100644 |
| index 000000000..9a6b4fdb5 |
| --- /dev/null |
| +++ b/internal/host/replica_drill1_test.go |
| @@ -0,0 +1,133 @@ |
| +package host |
| + |
| +import ( |
| + "context" |
| + "slices" |
| + "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" |
| +) |
| + |
| +// newCopiesOneWorld is the drill's instance before copies were raised: one |
| +// copy on box a (its FleetBox and coordinator), whose release of v2 passed |
| +// at copies 1 through the real writer. raise then has placement assign a |
| +// and b, as `carlos apps set --copies 2` leads it to. |
| +func newCopiesOneWorld(t *testing.T) (w *deployStoreWorld, raise func()) { |
| + t.Helper() |
| + ctx := provision.WithRecordSequence(context.Background()) |
| + s := release.NewDirStore(t.TempDir()) |
| + rec := provision.Record{Schema: 1, Account: "acme", App: "shop", Host: "shop.example.com", Slug: "shop", Channel: "edge", Region: "fleet/pi", Hibernate: new(bool), CreatedAt: time.Date(2026, 11, 1, 0, 0, 0, 0, time.UTC)} |
| + if err := provision.CreateRecord(ctx, s, rec); err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := provision.SetRecordFleetBox(ctx, s, rec.Account, rec.App, rec.Host, provision.FleetBoxExpect{Region: rec.Region, CreatedAt: rec.CreatedAt}, "a", time.Now()); err != nil { |
| + t.Fatal(err) |
| + } |
| + inc, _ := provision.Incarnation(rec.Account, rec.App, rec.Host, rec.CreatedAt) |
| + id := replicas.Identity{Account: rec.Account, App: rec.App, Host: rec.Host, Incarnation: inc} |
| + r, err := provision.SetRecordCoordinator(ctx, s, id, replicas.CoordinatorChange{Op: "acquire", Box: "a", BootID: "boot"}, time.Now()) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + w = &deployStoreWorld{t: t, s: s, id: id, term: r.Coordinator.Term} |
| + var revision uint64 |
| + for _, phase := range []string{"pending", "releasing", "released"} { |
| + 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) |
| + } |
| + revision = r.ReplicaDeploy.Revision |
| + } |
| + raise = func() { |
| + t.Helper() |
| + got := w.record() |
| + mctx, err := provision.WithReplicaAssignmentManifest(ctx, got, &release.Manifest{Schema: 1, App: "shop", Version: "v2", Kind: "binary", Stateless: true}) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if _, err := provision.SetRecordReplicaAssignment(mctx, s, id, replicas.Expect{Incarnation: inc, Region: got.Region, RecordSeq: got.RecordSeq}, replicas.Assignment{Desired: 2, Boxes: []string{"a", "b"}}); err != nil { |
| + t.Fatal(err) |
| + } |
| + } |
| + return w, raise |
| +} |
| + |
| +// joinBoxes are the two boxes after the raise: a serves v2, the release it |
| +// ran at copies 1; b runs nothing yet. |
| +func joinBoxes(t *testing.T, w *deployStoreWorld, client *e2eClient) (a, b *e2eBox) { |
| + t.Helper() |
| + a, b = newE2EBox(t, w, client, "a", true), newE2EBox(t, w, client, "b", false) |
| + a.act.mu.Lock() |
| + a.act.serveLocked("v2") |
| + a.act.mu.Unlock() |
| + b.act.mu.Lock() |
| + b.act.gen = activator.Gen{} |
| + b.act.mu.Unlock() |
| + return a, b |
| +} |
| + |
| +// TestReplicaJoinAfterCopiesRaised (#511 drill 1, R18; F3 and F4): an |
| +// instance serving its passed release at copies 1 is raised to copies 2. |
| +// Through the agent's own wiring (the record fan-out, the scheduler and |
| +// the swap worker on each box) and the real writers, the coordinator runs |
| +// a join on the same attempt: box b, the one the assignment added, gets |
| +// the only permit, starts the target in its permitted step and is |
| +// acknowledged after its proof; box a keeps serving and never holds a |
| +// permit; no release command runs anywhere; the result is 2 of 2. |
| +func TestReplicaJoinAfterCopiesRaised(t *testing.T) { |
| + w, raise := newCopiesOneWorld(t) |
| + raise() |
| + client := &e2eClient{w: w, fresh: []string{"a", "b"}} |
| + a, b := joinBoxes(t, w, client) |
| + runUntil(t, w, "done", deployDone, a, b) |
| + d := w.record().ReplicaDeploy |
| + if d.ID != "deploy-2" || !slices.Equal(d.Swap.Completed, []string{"a", "b"}) || d.LastGood != d.Target { |
| + t.Fatalf("the join did not close on the passed attempt: %+v", d) |
| + } |
| + if _, steps := a.act.counts(); len(steps) != 0 { |
| + t.Fatalf("the serving box was given a step: %d", len(steps)) |
| + } |
| + if _, steps := b.act.counts(); len(steps) == 0 { |
| + t.Fatal("the joining box was never permitted") |
| + } |
| + for _, box := range []*e2eBox{a, b} { |
| + if box.act.serving() != "v2" || box.act.releases != 0 { |
| + t.Fatalf("box %s: serving %q, release commands %d", box.label, box.act.serving(), box.act.releases) |
| + } |
| + } |
| + if want := []string{"order:a", "permit:a", "ack:b", "done:a"}; !slices.Equal(client.ops, want) { |
| + t.Fatalf("transitions %v, want %v", client.ops, want) |
| + } |
| +} |
| + |
| +// TestReplicaJoinWebFailedHalts (#511 drill 1, R18): the joining copy's |
| +// web fails after its start. Its step answers switched with the build |
| +// failed once its stop is proven; the ACK resolves the permit and halts |
| +// the join. Box a keeps serving, and no release command runs. |
| +func TestReplicaJoinWebFailedHalts(t *testing.T) { |
| + w, raise := newCopiesOneWorld(t) |
| + raise() |
| + client := &e2eClient{w: w, fresh: []string{"a", "b"}} |
| + a, b := joinBoxes(t, w, client) |
| + b.act.step = func() activator.SwapResult { |
| + return activator.SwapResult{Outcome: activator.SwapAbandoned, Proven: true, WebFailed: true, Reason: "the new build failed on this copy after it switched"} |
| + } |
| + runUntil(t, w, "halted", func(rec provision.Record) bool { return rec.ReplicaDeploy.Swap.Halted }, a, b) |
| + d := w.record().ReplicaDeploy |
| + if d.PermitOutstanding() || d.Swap.WebFailed != "b" || d.Phase != "released" || d.ID != "deploy-2" { |
| + t.Fatalf("the failed join did not halt with its permit resolved: %+v", d) |
| + } |
| + for i := 0; i < 5; i++ { |
| + round(t, w, a, b) |
| + } |
| + if got := w.record().ReplicaDeploy; got.Revision != d.Revision { |
| + t.Fatalf("the halted join moved on: %+v", got) |
| + } |
| + if a.act.serving() != "v2" || !d.Allows("a", w.id.Incarnation, d.Target.Digest) || a.act.releases+b.act.releases != 0 { |
| + t.Fatalf("a serving %q, allowed %t, release commands %d", a.act.serving(), d.Allows("a", w.id.Incarnation, d.Target.Digest), a.act.releases+b.act.releases) |
| + } |
| +} |
diff --git a/internal/provision/reconcile.go b/internal/provision/reconcile.go| index fd48c8ce4..7a0fae556 100644 |
| --- a/internal/provision/reconcile.go |
| +++ b/internal/provision/reconcile.go |
| @@ -2289,8 +2289,19 @@ func Reconcile(ctx context.Context, s release.Store, reg *registry.Registry, cfg |
| } |
| } |
| if applied && existing.Stateless && !cfg.DryRun && cfg.StatelessBinding != nil { |
| - if err := cfg.StatelessBinding(ctx, rec, existing); err != nil { |
| - log.Printf("provision: stateless binding %s: %v", rec.Host, err) |
| + // convergeOwned may have just converted the row's copy |
| + // assignment (copies raised, #511 drill 1, F4): bind the |
| + // row as it is now, not as this pass first read it, or |
| + // the binding is refused as fenced. A row that cannot be |
| + // read again is bound on the next pass. |
| + bound := &existing |
| + if rec.ReplicaAssignment != nil || existing.FleetBoxes != "" { |
| + bound, _ = reg.Get(rec.Host) |
| + } |
| + if bound != nil { |
| + if err := cfg.StatelessBinding(ctx, rec, *bound); err != nil { |
| + log.Printf("provision: stateless binding %s: %v", rec.Host, err) |
| + } |
| } |
| } |
| pr.applied[rec.Host] = applied |
diff --git a/internal/provision/replica_drill1_test.go b/internal/provision/replica_drill1_test.go| new file mode 100644 |
| index 000000000..6e0456fff |
| --- /dev/null |
| +++ b/internal/provision/replica_drill1_test.go |
| @@ -0,0 +1,120 @@ |
| +package provision |
| + |
| +import ( |
| + "context" |
| + "errors" |
| + "slices" |
| + "testing" |
| + "time" |
| + |
| + "github.com/carlosframework/platform/internal/registry" |
| + "github.com/carlosframework/platform/internal/replicas" |
| +) |
| + |
| +// TestReplicaCopiesOneReleaseJoins (#511 drill 1, R18): at copies 1 the |
| +// box that ran the passed release runs its target with no permit, and the |
| +// writer records it (Swap.Completed). When copies are raised the |
| +// coordinator's order is a join: only the box the assignment added gets a |
| +// permit, the serving box keeps its baseline and is never ordered, and |
| +// the join closes as done with no new attempt (no release). |
| +func TestReplicaCopiesOneReleaseJoins(t *testing.T) { |
| + ctx, s, rec, id, want := assignmentFixture(t) |
| + if err := SetRecordFleetBox(ctx, s, id.Account, id.App, id.Host, FleetBoxExpect{Region: rec.Region, CreatedAt: rec.CreatedAt}, "a", time.Now()); err != nil { |
| + t.Fatal(err) |
| + } |
| + r, err := SetRecordCoordinator(ctx, s, id, replicas.CoordinatorChange{Op: "acquire", Box: "a", BootID: "boot"}, time.Now()) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + term := r.Coordinator.Term |
| + d1 := replicas.Build{Version: "d1", Digest: "canonical-d1"} |
| + revision := uint64(0) |
| + for _, phase := range []string{"pending", "releasing", "released"} { |
| + r, err = SetRecordReplicaRelease(ctx, s, id, revision, replicas.Deploy{ID: "deploy-d1", Incarnation: id.Incarnation, Term: term, Target: d1, Phase: phase}) |
| + if err != nil { |
| + t.Fatalf("%s: %v", phase, err) |
| + } |
| + revision = r.ReplicaDeploy.Revision |
| + } |
| + if d := r.ReplicaDeploy; !slices.Equal(d.Swap.Completed, []string{"a"}) || len(d.Swap.Order) != 0 || d.LastGood != d1 { |
| + t.Fatalf("copies 1: the passed release did not record the box that runs it: %+v", d) |
| + } |
| + |
| + // copies 2: placement assigns a (the serving box) and b. |
| + want.RecordSeq = r.RecordSeq |
| + if r, err = SetRecordReplicaAssignment(ctx, s, id, want, replicas.Assignment{Desired: 2, Boxes: []string{"a", "b"}}); err != nil { |
| + t.Fatal(err) |
| + } |
| + allows := func(step string, a, b bool) { |
| + t.Helper() |
| + d := r.ReplicaDeploy |
| + if d.Allows("a", id.Incarnation, d1.Digest) != a || d.Allows("b", id.Incarnation, d1.Digest) != b { |
| + t.Fatalf("%s: a may run d1 %t (want %t), b %t (want %t): %+v", step, !a, a, !b, b, d) |
| + } |
| + } |
| + allows("assigned", true, false) |
| + apply := func(c replicas.DeployChange) error { |
| + c.ID, c.Target = r.ReplicaDeploy.ID, d1 |
| + if c.Op != "ack" { |
| + c.Term, c.Box = term, "a" |
| + } |
| + next, err := SetRecordReplicaDeploy(ctx, s, id, r.ReplicaDeploy.Revision, c, time.Now()) |
| + if err == nil { |
| + r = next |
| + } |
| + return err |
| + } |
| + if err := apply(replicas.DeployChange{Op: "order", Order: []string{"a", "b"}}); err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := apply(replicas.DeployChange{Op: "permit", Grant: "a"}); !errors.Is(err, replicas.ErrInvalid) { |
| + t.Fatalf("the serving box was ordered a permit: %v", err) |
| + } |
| + if err := apply(replicas.DeployChange{Op: "permit", Grant: "b"}); err != nil { |
| + t.Fatal(err) |
| + } |
| + allows("b permitted", true, true) |
| + if err := apply(replicas.DeployChange{Op: "ack", Box: "b", Permit: r.ReplicaDeploy.Swap.Permit, Switched: true}); err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := apply(replicas.DeployChange{Op: "done"}); err != nil { |
| + t.Fatal(err) |
| + } |
| + if d := r.ReplicaDeploy; d.Phase != "done" || d.ID != "deploy-d1" || d.Target != d1 || d.LastGood != d1 { |
| + t.Fatalf("the join did not close on the same attempt: %+v", d) |
| + } |
| + allows("done", true, true) |
| +} |
| + |
| +// TestReplicaBindingAfterConversion (#511 drill 1, F4): the pass that |
| +// converts a serving copies-1 row into a copy assignment publishes the |
| +// stateless binding with the converted row. It handed the binding the row |
| +// as it read it before the conversion, which the publisher refuses |
| +// ("stateless binding <host>: replicas: fenced"): the projection it |
| +// checks was not there yet. |
| +func TestReplicaBindingAfterConversion(t *testing.T) { |
| + s, rec, cfg := replicaReconcileFixture(t) |
| + cfg.FleetBox = "a" |
| + legacy := rec |
| + legacy.ReplicaAssignment = nil |
| + reg := regWith(t, RouteFor(legacy, cfg.DataDir, "", false, nil)) |
| + defer reg.Close() |
| + if err := reg.SetServingMode(rec.Host, "v1", true); err != nil { |
| + t.Fatal(err) |
| + } |
| + var bound []string |
| + cfg.StatelessBinding = func(_ context.Context, r Record, rt registry.Route) error { |
| + if r.ReplicaAssignment != nil && (!rt.ValidReplicaProjection() || rt.ReplicaRevision != r.ReplicaAssignment.Revision) { |
| + bound = append(bound, "fenced") |
| + return replicas.ErrFenced |
| + } |
| + bound = append(bound, rt.FleetBoxes) |
| + return nil |
| + } |
| + if err := Reconcile(ctx, s, reg, cfg); err != nil { |
| + t.Fatal(err) |
| + } |
| + if !slices.Equal(bound, []string{`["a","b"]`}) { |
| + t.Fatalf("the converting pass bound %q, want the converted row", bound) |
| + } |
| +} |
diff --git a/internal/provision/replicadeploy.go b/internal/provision/replicadeploy.go| index c352e1352..7a92cdc2a 100644 |
| --- a/internal/provision/replicadeploy.go |
| +++ b/internal/provision/replicadeploy.go |
| @@ -95,6 +95,15 @@ func SetRecordReplicaRelease(ctx context.Context, s release.Store, id replicas.I |
| next.LastGood = old.LastGood |
| if next.Phase == "released" { |
| next.LastGood = next.Target |
| + if rec.ReplicaAssignment == nil { |
| + // One copy (no assignment): the box that ran the |
| + // release runs its target with no permit, so the |
| + // record says so (#511 drill 1, R18). When copies |
| + // are raised, that box keeps serving and is never |
| + // ordered a permit; only the boxes the assignment |
| + // adds join (replicas.Swap.NextSwapBox). |
| + next.Swap.Completed = []string{c.Box} |
| + } |
| } |
| // Pending precedes command admission: a successor may retake it. |
| // Releasing may only be closed as unknown by a successor. |