carlos / platform Public

rootfs: the release decision reads the committed good build; an interrupted release phase reopens with no failure

Group R, R2 fix round 1 (Astra R2 round 1 P1 1 and P1 2, R3 round 1
finding 3).

P1 1: releaseBeforeSwap judges whether the release already ran against
the registry's current good build (committedGood), validated as the
same instance and lifetime (wakeRouteChanged, Incarnation), not the
entry's copy of the row, which an overlap install leaves naming A. A
restart or config attempt onto B after A to B runs no release.

P1 2: a rootfs overlap adoption's and a rootfs first wake's write-ahead
intent start in a release phase (Intent.Phase). The activator ends it
through Attempt.BeforeWeb / WakePlan.BeforeWeb (FailureBook.Advance)
after the release passed and right before the web starts. Reopening a
book with an intent still in the phase records no failure (a hold keeps
its hold-ran marker), so the attempt or the next wake runs the release
again. Binary rows and hold adoptions write no phase and reconcile as
before.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev 033137678cdacf5ec2ddc58f618dd8ee21cf1bc5 parent 4415f28
8 files changed, +790 −6
  • internal/activator/rootfs_swap_release_test.go +275 −0
  • internal/activator/rootfsrelease.go +43 −0
  • internal/activator/swap.go +48 −2
  • internal/activator/wakeplan.go +16 −0
  • internal/rollout/failures.go +73 −4
  • internal/rollout/rootfs_release_phase_test.go +313 −0
  • internal/rollout/scheduler.go +11 −0
  • internal/rollout/wakeplan.go +11 −0
diff --git a/internal/activator/rootfs_swap_release_test.go b/internal/activator/rootfs_swap_release_test.go
index 1a2b70aaa..1334ccaa1 100644
--- a/internal/activator/rootfs_swap_release_test.go
+++ b/internal/activator/rootfs_swap_release_test.go
@@ -1103,3 +1103,278 @@ func TestReleaseSwapResultIsTheOverlapColumn(t *testing.T) {
t.Errorf("the failure's reason does not carry C05 (%q)", got)
}
}
+
+// committedRow puts the fixture's rootfs row into its registry, good on
+// v1, makes it the entry's own row (the wake's refresh reads it so), and
+// has the Service read the registry (a.Routes), as production does.
+func (f *relSwapFixture) committedRow(t *testing.T) {
+ t.Helper()
+ f.a.mu.Lock()
+ rt := f.a.entries[f.host].route
+ f.a.mu.Unlock()
+ if err := f.reg.Upsert(rt); err != nil {
+ t.Fatal(err)
+ }
+ cur, err := f.reg.Get(f.host)
+ if err != nil || cur == nil {
+ t.Fatalf("the row: %v, %v", cur, err)
+ }
+ if err := f.reg.SetGoodBuild(f.host, cur.Good(), registry.GoodBuild{Version: "v1", Digest: "aa11"}); err != nil {
+ t.Fatal(err)
+ }
+ if cur, err = f.reg.Get(f.host); err != nil {
+ t.Fatal(err)
+ }
+ f.a.mu.Lock()
+ f.a.entries[f.host].route = *cur
+ f.a.mu.Unlock()
+ f.a.Routes = f.reg
+}
+
+// Astra R2 round 1, P1 1 (D3): after a successful A to B overlap, a
+// restart and a config attempt onto B run no release. The scheduler
+// records B as good in the registry alone (writeGoodLocked); the entry's
+// copy of the row still names A.
+func TestRestartAfterAnOverlapRunsNoRelease(t *testing.T) {
+ f := newRelSwapFixture(t, true)
+ f.committedRow(t)
+ ch := f.swapAsync(f.attempt(ModeOverlap))
+ f.nextUnit(t).done("1", "0", "success")
+ res := waitSwap(t, ch)
+ if res.Outcome != SwapDone || res.Gen.Build != "v2" {
+ t.Fatalf("A to B = %+v", res)
+ }
+ // The scheduler's good write (rollout writeGoodLocked).
+ cur, _ := f.reg.Get(f.host)
+ if err := f.reg.SetGoodBuild(f.host, cur.Good(), registry.GoodBuild{Version: "v2", Digest: f.digest}); err != nil {
+ t.Fatal(err)
+ }
+ f.a.mu.Lock()
+ cached := f.a.entries[f.host].route.GoodVersion
+ f.a.mu.Unlock()
+ if cached != "v1" {
+ t.Fatalf("the entry's row names %q good; the test needs the stale copy", cached)
+ }
+ reads := f.rb.readCount()
+ for i, cause := range []Cause{CauseRestart, CauseConfig} {
+ f.rev.Add(1)
+ at := f.attempt(ModeOverlap)
+ at.Cause = cause
+ if cause == CauseRestart {
+ at.RestartID = "rs-1"
+ } else {
+ at.ConfigRev = "cfg-2"
+ }
+ res := f.swapNoRelease(t, at)
+ if res.Outcome != SwapDone || res.Gen.Build != "v2" {
+ t.Fatalf("%s onto B (%d) = %+v", cause, i, res)
+ }
+ }
+ if got := f.rb.startList(); !slices.Equal(got, []string{"v2"}) {
+ t.Errorf("releases started %v; want B's once, for its install", got)
+ }
+ // The release decision read no processes for the good build (R3's
+ // early return); the swap's own processes hook still reads them.
+ if n := f.rb.readCount() - reads; n != 2 {
+ t.Errorf("%d process reads for two attempts onto the good build; want the two swap hooks' alone", n)
+ }
+}
+
+// committedGood reads only a row of the same instance and lifetime: one
+// that is gone, cannot be read or is another lifetime refuses the release,
+// marked, before anything runs.
+func TestReleaseRefusedWhenTheRowIsNotTheInstance(t *testing.T) {
+ for _, tc := range []struct {
+ name string
+ alter func(f *relSwapFixture)
+ }{
+ {"another lifetime", func(f *relSwapFixture) {
+ f.a.mu.Lock()
+ f.a.entries[f.host].route.Incarnation++
+ f.a.mu.Unlock()
+ }},
+ {"another app", func(f *relSwapFixture) {
+ f.a.mu.Lock()
+ f.a.entries[f.host].route.App = "other"
+ f.a.mu.Unlock()
+ }},
+ {"gone", func(f *relSwapFixture) {
+ if err := f.reg.Delete(f.host); err != nil {
+ t.Fatal(err)
+ }
+ }},
+ } {
+ t.Run(tc.name, func(t *testing.T) {
+ f := newRelSwapFixture(t, true)
+ f.committedRow(t)
+ tc.alter(f)
+ res := f.swapNoRelease(t, f.attempt(ModeOverlap))
+ if res.Outcome != SwapAbandoned || res.Executed || !strings.Contains(res.Reason, errReleaseRowChanged.Error()) {
+ t.Fatalf("Swap = %+v; want abandoned, not executed, the row changed", res)
+ }
+ if n := f.rb.readCount(); n != 0 {
+ t.Errorf("%d process reads", n)
+ }
+ f.nothingAdmitted(t, tc.name)
+ })
+ }
+ if verdictOf(refuseRootfs(errReleaseRowChanged)) != gateNoOwnership {
+ t.Error("the row's refusal is not a refusal")
+ }
+}
+
+// Astra R2 round 1, P1 2: the release phase ends (Attempt.BeforeWeb) after
+// the release is proven and before the web's exec, never on a release that
+// failed or is unproven; a phase that cannot be recorded starts no web.
+func TestReleasePhaseEndsRightBeforeTheWeb(t *testing.T) {
+ withPhase := func(f *relSwapFixture, err error) (Attempt, *atomic.Int32) {
+ var calls atomic.Int32
+ at := f.attempt(ModeOverlap)
+ at.BeforeWeb = func() error {
+ calls.Add(1)
+ f.b.record("release-phase-ended")
+ return err
+ }
+ return at, &calls
+ }
+ t.Run("a passed release", func(t *testing.T) {
+ f := newRelSwapFixture(t, true)
+ at, calls := withPhase(f, nil)
+ ch := f.swapAsync(at)
+ f.nextUnit(t).done("1", "0", "success")
+ if res := waitSwap(t, ch); res.Outcome != SwapDone {
+ t.Fatalf("Swap = %+v", res)
+ }
+ ev := f.b.events()
+ order := []int{indexPrefix(ev, "release:proven:v2"), indexPrefix(ev, "raise"), indexPrefix(ev, "release-phase-ended"), indexPrefix(ev, "startGen:")}
+ for i := range order {
+ if order[i] < 0 || (i > 0 && order[i] < order[i-1]) {
+ t.Fatalf("events %v (indexes %v): want the release proven, the admission, the phase's end, then the web", ev, order)
+ }
+ }
+ if calls.Load() != 1 {
+ t.Errorf("BeforeWeb asked %d times", calls.Load())
+ }
+ })
+ for _, tc := range []struct {
+ name string
+ stuck bool
+ code string
+ }{{"a failed release", false, "1"}, {"an unproven release", true, "0"}} {
+ t.Run(tc.name, func(t *testing.T) {
+ f := newRelSwapFixture(t, true)
+ f.rb.setStuck(tc.stuck)
+ at, calls := withPhase(f, nil)
+ ch := f.swapAsync(at)
+ f.nextUnit(t).done("1", tc.code, "")
+ res := waitSwap(t, ch)
+ if res.Outcome == SwapDone || calls.Load() != 0 {
+ t.Fatalf("Swap = %+v, BeforeWeb asked %d times; want the phase never ended", res, calls.Load())
+ }
+ if tc.stuck {
+ f.proveRecord(t)
+ }
+ })
+ }
+ t.Run("the phase cannot be recorded", func(t *testing.T) {
+ f := newRelSwapFixture(t, true)
+ incumbent := f.b.lastProc()
+ at, _ := withPhase(f, errors.New("the failure book cannot be written"))
+ ch := f.swapAsync(at)
+ f.nextUnit(t).done("1", "0", "success")
+ res := waitSwap(t, ch)
+ if res.Outcome != SwapAbandoned || !res.Executed || !strings.Contains(res.Reason, errWebPhaseUnrecorded.Error()) {
+ t.Fatalf("Swap = %+v; want abandoned, executed (its release ran)", res)
+ }
+ if gens, raises, restores := f.b.calls(); gens != 0 || raises != restores {
+ t.Errorf("StartStaged %d, raises %d, restores %d", gens, raises, restores)
+ }
+ f.incumbentServing(t, incumbent, "after the phase could not be recorded")
+ })
+ t.Run("a hold ends it before the stop", func(t *testing.T) {
+ f := newRelSwapFixture(t, true)
+ incumbent := f.b.lastProc()
+ at, calls := withPhase(f, errors.New("the failure book cannot be written"))
+ at.Mode = ModeHold
+ res := f.swapNoRelease(t, at)
+ if res.Outcome != SwapAbandoned || res.Executed || calls.Load() != 1 || incumbent.isStopped() {
+ t.Fatalf("hold = %+v, BeforeWeb %d, incumbent stopped %v", res, calls.Load(), incumbent.isStopped())
+ }
+ })
+}
+
+// Astra R3 round 1, finding 3 (the activator's half): a planned first wake
+// ends its release phase (WakePlan.BeforeWeb) after B's release is proven
+// and before B's web, never on a release that failed (the fallback boots
+// the good build with the phase still open) or is unproven; a phase that
+// cannot be recorded starts no web and is no failure of B.
+func TestFirstWakeReleasePhaseEndsRightBeforeTheWeb(t *testing.T) {
+ withPhase := func(r *wakeRelRig, err error) *atomic.Int32 {
+ var calls atomic.Int32
+ p := r.plan
+ p.BeforeWeb = func() error {
+ calls.Add(1)
+ r.b.record("release-phase-ended")
+ return err
+ }
+ r.setPlan(p)
+ return &calls
+ }
+ t.Run("a passed release", func(t *testing.T) {
+ r := newWakeRelRig(t)
+ calls := withPhase(r, nil)
+ ch := r.wake(r.a)
+ r.nextUnit(t).done("exited", "0", "success")
+ if err := waitErr(t, ch); err != nil {
+ t.Fatal(err)
+ }
+ rp, ph, st := r.index("release:proven:v2"), r.index("release-phase-ended"), r.index("start:v2")
+ if rp < 0 || ph < rp || st < ph || calls.Load() != 1 {
+ t.Fatalf("order %v (BeforeWeb %d): want release proven, the phase's end, then web", r.b.events(), calls.Load())
+ }
+ })
+ t.Run("a failed release falls back with the phase open", func(t *testing.T) {
+ r := newWakeRelRig(t)
+ calls := withPhase(r, nil)
+ ch := r.wake(r.a)
+ r.nextUnit(t).done("exited", "1", "exit-code")
+ if err := waitErr(t, ch); err != nil {
+ t.Fatal(err)
+ }
+ r.evs.wait(t, GenFellBack)
+ if calls.Load() != 0 {
+ t.Fatalf("BeforeWeb asked %d times for a failed release", calls.Load())
+ }
+ })
+ t.Run("an unproven release", func(t *testing.T) {
+ r := newWakeRelRig(t)
+ calls := withPhase(r, nil)
+ r.b.setStuck(true)
+ ch := r.wake(r.a)
+ r.nextUnit(t).done("exited", "0", "success")
+ if err := waitErr(t, ch); err == nil {
+ t.Fatal("the wake served over an unproven release")
+ }
+ if calls.Load() != 0 || r.webStarts() != 0 {
+ t.Fatalf("BeforeWeb %d, web starts %d", calls.Load(), r.webStarts())
+ }
+ })
+ t.Run("the phase cannot be recorded", func(t *testing.T) {
+ r := newWakeRelRig(t)
+ withPhase(r, errors.New("the failure book cannot be written"))
+ ch := r.wake(r.a)
+ r.nextUnit(t).done("exited", "0", "success")
+ err := waitErr(t, ch)
+ if !errors.Is(err, errWebPhaseUnrecorded) || verdictOf(err) != gateNoOwnership {
+ t.Fatalf("wake = %v; want the unrecorded phase, a refusal", err)
+ }
+ if n := r.webStarts(); n != 0 {
+ t.Fatalf("%d web starts", n)
+ }
+ ev := r.evs.wait(t, GenCancelled)
+ noBuildFailure(t, r.evs)
+ if !ev.Executed {
+ t.Errorf("GenCancelled %+v: the release ran, so the wake executed", ev)
+ }
+ })
+}
diff --git a/internal/activator/rootfsrelease.go b/internal/activator/rootfsrelease.go
index 2fd49b21f..7f8ed2ec3 100644
--- a/internal/activator/rootfsrelease.go
+++ b/internal/activator/rootfsrelease.go
@@ -913,6 +913,15 @@ func (a *Service) releaseBeforeSwap(ctx context.Context, at Attempt, newest func
if err := gate(); err != nil {
return nil, false, abandon(err), false
}
+ // Whether the release already ran is the row's committed good build,
+ // not the entry's copy of the row: an overlap install leaves e.route as
+ // it woke, and the scheduler records the new good build in the
+ // registry alone (Astra R2 round 1, P1 1, D3).
+ good, err := a.committedGood(rt)
+ if err != nil {
+ return nil, false, abandon(err), false
+ }
+ rt.GoodVersion, rt.GoodDigest, rt.GoodDemand = good.Version, good.Digest, good.Demand
id := release.StagedIdentity{Version: at.Build, StagedPath: at.StagedPath, Digest: at.Digest, Demand: at.Demand}
// The bytes first, as swapOverlap checks them again [pf R2.5].
if err := release.VerifyStaged(id); err != nil {
@@ -960,3 +969,37 @@ func releaseSwapResult(err error, executed, recordOpen bool) SwapResult {
res.Executed = executed
return res
}
+
+// committedGood is rt's good build as the registry holds it now, the build
+// whose release has already run on this instance (D3). The entry's own
+// copy of the row (rt) is what the instance woke with: an overlap install
+// does not change it, so after A to B it still names A. The fresh row must
+// be the same instance in the same lifetime as rt (the fields a wake's
+// refresh checks, wakeRouteChanged, and the row's Incarnation); a row that
+// is gone, cannot be read or is another instance refuses the release,
+// marked, before anything runs. With no registry (a.Routes nil) the
+// entry's row is the only one there is.
+func (a *Service) committedGood(rt registry.Route) (registry.GoodBuild, error) {
+ if a.Routes == nil {
+ return rt.Good(), nil
+ }
+ fresh, err := a.Routes.Get(rt.Host)
+ if err != nil {
+ return registry.GoodBuild{}, refuseRootfs(fmt.Errorf("%s: read the committed good build: %w", rt.Host, err))
+ }
+ if fresh == nil {
+ return registry.GoodBuild{}, refuseRootfs(fmt.Errorf("%s: read the committed good build: %w", rt.Host, errReleaseRowChanged))
+ }
+ if err := wakeRouteChanged(rt, *fresh, false); err != nil {
+ return registry.GoodBuild{}, refuseRootfs(fmt.Errorf("%w: %v", errReleaseRowChanged, err))
+ }
+ if fresh.Incarnation != rt.Incarnation {
+ return registry.GoodBuild{}, refuseRootfs(fmt.Errorf("%s: %w: its lifetime is %d now, not %d", rt.Host, errReleaseRowChanged, fresh.Incarnation, rt.Incarnation))
+ }
+ return fresh.Good(), nil
+}
+
+// errReleaseRowChanged: the registry's row is not the instance the entry
+// woke for (gone, or another lifetime), so its good build says nothing
+// about this instance's database. Marked refuseRootfs where returned.
+var errReleaseRowChanged = errors.New("the instance's row is not the one it woke with")
diff --git a/internal/activator/swap.go b/internal/activator/swap.go
index 7bd05aec5..9ad4da01b 100644
--- a/internal/activator/swap.go
+++ b/internal/activator/swap.go
@@ -51,6 +51,17 @@ type Attempt struct {
RestartID string
SchedRev uint64 // scheduler revision; the newest-attempt check compares it
Canary bool // observe the 10 s settle
+ // BeforeWeb, when set, ends the attempt's release phase durably before
+ // anything of the build but its release runs (rootfs apps Plan 3,
+ // Group R; Astra R2 round 1, P1 2). The scheduler sets it on a rootfs
+ // overlap adoption, whose write-ahead intent starts in the release
+ // phase: an agent that dies while the release runs, or after it passed
+ // and before the web starts, reopens its failure book with no failure
+ // for the build, and the attempt runs again. Swap asks it right before
+ // the successor's spawn (an overlap) or before the incumbent's stop (a
+ // hold, whose release runs after the stop); an error abandons the
+ // attempt with nothing of its web run. Nil on every other attempt.
+ BeforeWeb func() error
}
// SwapOutcome is how an attempt ended.
@@ -226,7 +237,8 @@ const (
// too: an open release record of the host (errReleaseOpen, INV-R4), an
// overlap whose build needs a release without this attempt's proven pass
// (errReleaseNotPassed, INV-R5), and a release refused for a route its
-// plan turned off (errReleaseTurnedOff). Each refuses before anything of
+// plan turned off (errReleaseTurnedOff), and a release phase that could
+// not be recorded as ended (errWebPhaseUnrecorded). Each refuses before anything of
// the build runs, at the admission or at the exec, so a refusal that
// surfaces from inside the spawn is abandonment, never the build's failure.
//
@@ -255,7 +267,8 @@ func verdictOf(err error) gateVerdict {
errors.Is(err, instancectl.ErrStagedImage), errors.Is(err, errRootfsRefused),
errors.Is(err, errRootfsBinaryLaunch), errors.Is(err, errNoRootfsControl),
errors.Is(err, errBuildStateUnwritten), errors.Is(err, errOwnershipRefused),
- errors.Is(err, errReleaseOpen), errors.Is(err, errReleaseNotPassed), errors.Is(err, errReleaseTurnedOff):
+ errors.Is(err, errReleaseOpen), errors.Is(err, errReleaseNotPassed), errors.Is(err, errReleaseTurnedOff),
+ errors.Is(err, errWebPhaseUnrecorded):
return gateNoOwnership
}
return gateProceed
@@ -332,6 +345,11 @@ func (a *Service) Swap(ctx context.Context, at Attempt, newest func(host string)
// (spec §2.9, rev 17). Its result reads as a hold's, SwapDown
// when the new build fails, and Mode tells the scheduler so: a
// hold build that ran is held down (spec §6.4, §8.3).
+ if err := webPhase(at); err != nil {
+ res := abandon(err)
+ res.Mode = ModeHold
+ return res
+ }
res := a.swapHold(ctx, at, newest)
res.Mode = ModeHold
return res
@@ -349,6 +367,11 @@ func (a *Service) Swap(ctx context.Context, at Attempt, newest func(host string)
res.Mode = ModeOverlap
return res
case ModeHold:
+ if err := webPhase(at); err != nil {
+ res := abandon(err)
+ res.Mode = ModeHold
+ return res
+ }
res := a.swapHold(ctx, at, newest)
res.Mode = ModeHold
return res
@@ -356,6 +379,21 @@ func (a *Service) Swap(ctx context.Context, at Attempt, newest func(host string)
return abandon(errSwapMode)
}
+// errWebPhaseUnrecorded: the attempt's release phase could not be ended
+// durably (Attempt.BeforeWeb), so nothing of its web runs.
+var errWebPhaseUnrecorded = errors.New("swap: the attempt's release phase could not be recorded as ended")
+
+// webPhase ends at's release phase (Attempt.BeforeWeb), when it has one.
+func webPhase(at Attempt) error {
+ if at.BeforeWeb == nil {
+ return nil
+ }
+ if err := at.BeforeWeb(); err != nil {
+ return fmt.Errorf("%w: %v", errWebPhaseUnrecorded, err)
+ }
+ return nil
+}
+
// EffectiveMode is the mode Swap runs an attempt of mode m on host in: an
// overlap on a foreign-layout row runs as a hold (foreignRow, spec §2.9),
// anything else as given. The scheduler writes it into the attempt's
@@ -617,6 +655,14 @@ func (a *Service) swapOverlap(ctx context.Context, gb SwapBackend, at Attempt, n
return abandon(err)
}
+ // The release phase ends, durably, before the web's exec: from here an
+ // agent crash is the build's possible failure, as a binary attempt's
+ // always is (Attempt.BeforeWeb).
+ if err := webPhase(at); err != nil {
+ a.dropMember(e, m) // restores the raised parent (leaveSet)
+ return abandon(err)
+ }
+
// Step 3: exec the attempt's staged build by path. The backend reports
// what it ran, and anything but the attempt's choice is refused.
g, proc, spawnErr := a.spawnSuccessor(mctx, gb, e, rt, work, m.gen, gate)
diff --git a/internal/activator/wakeplan.go b/internal/activator/wakeplan.go
index 3c9393ab1..d8e0aafaa 100644
--- a/internal/activator/wakeplan.go
+++ b/internal/activator/wakeplan.go
@@ -60,6 +60,13 @@ type WakePlan struct {
// the instance's env file at the spawn), so it carries this one,
// unchanged, into every GenEvent of the wake, beside Token.
ConfigRev string
+ // BeforeWeb, when set, ends a first wake's release phase durably, as
+ // Attempt.BeforeWeb does for an overlap (Group R; Astra R2 round 1,
+ // P1 2, and R3 round 1, finding 3). The planner sets it on a rootfs
+ // row's first wake, whose write-ahead intent starts in the release
+ // phase. spawnPlanned asks it after Boot's release and before Boot's
+ // web; an error starts no web and is no failure of Boot.
+ BeforeWeb func() error
}
func (p WakePlan) planned() bool { return p.Boot.StagedPath != "" }
@@ -384,6 +391,15 @@ func (a *Service) spawnPlanned(ctx context.Context, e *actEntry, rt registry.Rou
}
released = executed
}
+ // The release phase ends, durably, before Boot's web: from here an
+ // agent crash is Boot's possible failure, as a binary first wake's
+ // always is (WakePlan.BeforeWeb).
+ if plan.BeforeWeb != nil {
+ if err := plan.BeforeWeb(); err != nil {
+ w.bootRan = released
+ return nil, "", Gen{}, fmt.Errorf("%w: %v", errWebPhaseUnrecorded, err)
+ }
+ }
gate := a.planGate(ctx, e, lease, plan, boot.Version)
var healthBy time.Time
var left time.Duration
diff --git a/internal/rollout/failures.go b/internal/rollout/failures.go
index 04b50f5d7..1434de7aa 100644
--- a/internal/rollout/failures.go
+++ b/internal/rollout/failures.go
@@ -184,8 +184,24 @@ type Intent struct {
// time): a reconciled hold intent's marker carries it as its At (ruling
// 205). Zero when not known; reconciliation then uses the open time.
Start time.Time `json:"start"`
+ // Phase is IntentPhaseRelease while a rootfs overlap adoption's or a
+ // rootfs first wake's release may be running and nothing else of its
+ // build has (rootfs
+ // apps Plan 3, Group R; Astra R2 round 1, P1 2); Advance clears it
+ // before the web starts. "" on every other intent, and once advanced.
+ // An agent before it ignores the key and reconciles the intent as a
+ // failure, the safe direction.
+ Phase string `json:"phase,omitempty"`
}
+// IntentPhaseRelease is an intent in its release phase (a rootfs overlap
+// adoption or a rootfs first wake): the build's release command may be
+// running (or has passed) and its web has not started. An interruption
+// here is not the build's failure: reconciliation drops the intent with no
+// record (a hold keeps its hold-ran marker), and the attempt or the next
+// wake runs the build again, release first (D3, INV-R3).
+const IntentPhaseRelease = "release"
+
// wakeIntent reports whether id is a planned wake's intent: "w" leads a
// wake's identity, in this agent's form and an earlier one's alike.
func wakeIntent(id string) bool { return strings.HasPrefix(id, "w") }
@@ -384,9 +400,10 @@ type FailureBook struct {
held map[string]HoldRan
// intents are the executions in flight by host (Intent), saved with the
// records. reconciled is how many intents OpenFailureBook turned into
- // records.
- intents map[string]Intent
- reconciled int
+ // records, interrupted how many it dropped in their release phase.
+ intents map[string]Intent
+ reconciled int
+ interrupted int
// dirty is set whenever memory differs from the last save that landed
// (rule 2): a save failed, or the open changed something (a rotated
// identity, reconciled intents, epochs given to older entries). Memory is
@@ -616,8 +633,24 @@ func (b *FailureBook) reconcileLocked() {
hosts = append(hosts, host)
}
slices.Sort(hosts) // one order for every open of the same file
+ recorded := 0
for _, host := range hosts {
in := b.intents[host]
+ if in.Phase == IntentPhaseRelease {
+ // An execution interrupted in its release phase: only the
+ // release may have run, and its result is unknown. No failure
+ // and no hold-down: the attempt or the wake runs again, release
+ // first. A hold's release ran against the database the good
+ // build would boot on, so a hold keeps the hold-ran marker, as
+ // a hold that ran and did not pass does: the next wake boots the
+ // marked build (its release again), never the good build.
+ b.interrupted++
+ if in.Mode == string(activator.ModeHold) {
+ b.markLocked(host, HoldRan{Account: in.Account, App: in.App, Build: in.Build, Exec: in.ID, At: in.Start})
+ }
+ continue
+ }
+ recorded++
r := FailureRecord{Account: in.Account, App: in.App, Build: in.Build, Host: host,
Cause: string(activator.CauseRelease), Mode: in.Mode, Reason: reconciledReason, PromotedAt: now, At: now}
if in.Mode == string(activator.ModeHold) {
@@ -627,7 +660,7 @@ func (b *FailureBook) reconcileLocked() {
}
b.recordLocked(r)
}
- b.reconciled = len(hosts)
+ b.reconciled = recorded
b.intents = nil
b.dirty = true
}
@@ -641,6 +674,42 @@ func (b *FailureBook) Reconciled() int {
return b.reconciled
}
+// Interrupted is how many intents OpenFailureBook dropped with no record:
+// executions an earlier agent left in their release phase
+// (IntentPhaseRelease), which run again.
+func (b *FailureBook) Interrupted() int {
+ b.mu.Lock()
+ defer b.mu.Unlock()
+ return b.interrupted
+}
+
+// Advance ends the release phase of host's intent for execution id
+// (IntentPhaseRelease) and saves, before anything of the build but its
+// release runs (activator Attempt.BeforeWeb). From then on the intent
+// reconciles as any execution's. On a save error the phase is put back
+// and the error returned: the caller must not start the web, since a
+// restart would then read a failure as an interruption. An intent of
+// another execution, or none, is refused the same way.
+func (b *FailureBook) Advance(host, id string) error {
+ b.mu.Lock()
+ defer b.mu.Unlock()
+ in, ok := b.intents[host]
+ if !ok || id == "" || in.ID != id {
+ return fmt.Errorf("%s: no intent of execution %q stands to advance", host, id)
+ }
+ if in.Phase == "" {
+ return nil
+ }
+ prev := in
+ in.Phase = ""
+ b.intents[host] = in
+ if err := b.saveLocked(); err != nil {
+ b.intents[host] = prev
+ return err
+ }
+ return nil
+}
+
// Flush saves the book now: after reconciliation, so the records it made
// and the intents it removed reach the disk before anything runs.
func (b *FailureBook) Flush() error {
diff --git a/internal/rollout/rootfs_release_phase_test.go b/internal/rollout/rootfs_release_phase_test.go
new file mode 100644
index 000000000..44a39299d
--- /dev/null
+++ b/internal/rollout/rootfs_release_phase_test.go
@@ -0,0 +1,313 @@
+package rollout
+
+// Rootfs apps Plan 3, Group R (Astra R2 round 1, P1 2): a rootfs overlap
+// adoption's write-ahead intent starts in its release phase, and the
+// activator ends the phase (Attempt.BeforeWeb, FailureBook.Advance) right
+// before the web starts. An agent that dies while the release runs, or
+// after it passed and before the web started, reopens its failure book with
+// no failure for the build, and the next agent runs the attempt again
+// (release first). Once the web has started, a crash is the build's possible
+// failure, as a binary attempt's always was.
+
+import (
+ "context"
+ "sync/atomic"
+ "testing"
+ "time"
+
+ "github.com/carlosframework/platform/internal/activator"
+ "github.com/carlosframework/platform/internal/registry"
+)
+
+func TestAnInterruptedReleasePhaseReopensWithNoFailure(t *testing.T) {
+ for _, tc := range []struct {
+ name string
+ rootfs bool
+ webRan bool // the activator ended the release phase before the crash
+ failure bool // the reopened book records the build's failure
+ }{
+ {"rootfs, mid-release", true, false, false},
+ {"rootfs, after the release passed, before the web", true, false, false},
+ {"rootfs, after the web started", true, true, true},
+ {"binary, mid-swap (unchanged)", false, false, true},
+ } {
+ t.Run(tc.name, func(t *testing.T) {
+ h := newHarness(t, "a")
+ if tc.rootfs {
+ r := h.rows.get("a")
+ r.Runtime = registry.RuntimeRootfsV1
+ h.rows.put(r)
+ }
+ dir := h.onDisk()
+ h.stage("v2")
+ var (
+ runs atomic.Int32
+ hadHook atomic.Bool
+ advanceErr atomic.Value
+ )
+ reached, park := make(chan struct{}), make(chan struct{})
+ // The fake activator's Swap, as the real one orders it: the
+ // release (here, the crash points around it), then BeforeWeb
+ // right before the web. Only the first agent's attempt parks.
+ h.act.outcome = func(a activator.Attempt) activator.SwapOutcome {
+ if a.Build != "v2" || runs.Add(1) > 1 {
+ if a.BeforeWeb != nil {
+ if err := a.BeforeWeb(); err != nil {
+ return activator.SwapAbandoned
+ }
+ }
+ return activator.SwapDone
+ }
+ hadHook.Store(a.BeforeWeb != nil)
+ if tc.webRan {
+ if err := a.BeforeWeb(); err != nil {
+ advanceErr.Store(err)
+ }
+ }
+ close(reached)
+ <-park
+ return activator.SwapDone
+ }
+ first := h.m
+ t.Cleanup(func() {
+ close(park)
+ ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
+ defer cancel()
+ first.Close(ctx)
+ h.m.Close(ctx)
+ })
+ h.install("v2", "a")
+ h.pass()
+ select {
+ case <-reached:
+ case <-time.After(10 * time.Second):
+ t.Fatal("the adoption never reached the activator")
+ }
+ if err, _ := advanceErr.Load().(error); err != nil {
+ t.Fatalf("Advance: %v", err)
+ }
+ if hadHook.Load() != tc.rootfs {
+ t.Fatalf("Attempt.BeforeWeb set %v on a rootfs row %v", hadHook.Load(), tc.rootfs)
+ }
+ in, ok := fileOf(t, dir).Intents["a"]
+ wantPhase := ""
+ if tc.rootfs && !tc.webRan {
+ wantPhase = IntentPhaseRelease
+ }
+ if !ok || in.Build != "v2" || in.Mode != "overlap" || in.Phase != wantPhase {
+ t.Fatalf("intent on disk at the crash: %+v, %v; want v2, overlap, phase %q", in, ok, wantPhase)
+ }
+
+ // The agent dies here; the next one opens the book.
+ b, err := OpenFailureBook(dir)
+ if err != nil {
+ t.Fatal(err)
+ }
+ recorded := false
+ for _, r := range b.List() {
+ recorded = recorded || (r.Build == "v2" && r.Host == "a")
+ }
+ if recorded != tc.failure {
+ t.Fatalf("reopened: v2 recorded failed %v, want %v (reconciled %d, interrupted %d)", recorded, tc.failure, b.Reconciled(), b.Interrupted())
+ }
+ wantInterrupted := 0
+ if !tc.failure {
+ wantInterrupted = 1
+ }
+ if b.Interrupted() != wantInterrupted || b.Reconciled() != 1-wantInterrupted {
+ t.Fatalf("reconciled %d, interrupted %d", b.Reconciled(), b.Interrupted())
+ }
+ if err := b.Flush(); err != nil {
+ t.Fatal(err)
+ }
+ if f := fileOf(t, dir); len(f.Intents) != 0 {
+ t.Fatalf("intents left after the reopen: %+v", f.Intents)
+ }
+
+ // The new agent's scheduler. An interrupted release phase runs
+ // the attempt again (release first) and B serves; a failure
+ // keeps B off the host.
+ h.restartOn(b)
+ h.pass()
+ if !tc.failure {
+ waitFor(t, "the attempt to run again", func() bool { return runs.Load() >= 2 && h.servingAll("v2", "a") })
+ waitFor(t, "the rerun's intent to end", func() bool { return len(fileOf(t, dir).Intents) == 0 })
+ for _, r := range fileOf(t, dir).Records {
+ if r.Build == "v2" {
+ t.Fatalf("a failure of v2 after the rerun: %+v", r)
+ }
+ }
+ return
+ }
+ time.Sleep(200 * time.Millisecond)
+ h.pass()
+ time.Sleep(200 * time.Millisecond)
+ if n := runs.Load(); n != 1 {
+ t.Fatalf("v2 ran %d times after its reconciled failure", n)
+ }
+ })
+ }
+}
+
+// Advance ends only its own execution's release phase, and a save it cannot
+// make leaves the phase in place: the web must not start then.
+func TestAdvanceEndsOnlyItsOwnReleasePhase(t *testing.T) {
+ dir := t.TempDir()
+ b, err := OpenFailureBook(dir)
+ if err != nil {
+ t.Fatal(err)
+ }
+ in := Intent{Account: "bab", App: "blog", Build: "v2", Mode: "overlap", ID: "a1-1", Phase: IntentPhaseRelease}
+ if err := b.Begin("a", in); err != nil {
+ t.Fatal(err)
+ }
+ if err := b.Advance("a", "a1-2"); err == nil {
+ t.Fatal("Advance of another execution's intent succeeded")
+ }
+ if err := b.Advance("b", "a1-1"); err == nil {
+ t.Fatal("Advance of a host with no intent succeeded")
+ }
+ if got := fileOf(t, dir).Intents["a"].Phase; got != IntentPhaseRelease {
+ t.Fatalf("phase %q after refused advances", got)
+ }
+ readOnly(t, dir)
+ if err := b.Advance("a", "a1-1"); err == nil {
+ t.Fatal("Advance succeeded with the book unwritable")
+ }
+ writable(t, dir)
+ if err := b.SaveIfDirty(); err != nil {
+ t.Fatal(err)
+ }
+ if got := fileOf(t, dir).Intents["a"].Phase; got != IntentPhaseRelease {
+ t.Fatalf("phase %q after a failed advance", got)
+ }
+ if err := b.Advance("a", "a1-1"); err != nil {
+ t.Fatal(err)
+ }
+ if got := fileOf(t, dir).Intents["a"].Phase; got != "" {
+ t.Fatalf("phase %q after the advance", got)
+ }
+ // A hold interrupted in its release phase: no failure, no hold-down,
+ // and the hold-ran marker (its release ran against the database).
+ if err := b.End("a", "a1-1"); err != nil {
+ t.Fatal(err)
+ }
+ if err := b.Begin("a", Intent{Account: "bab", App: "blog", Build: "v2", Mode: "hold", ID: "a1-3", Phase: IntentPhaseRelease}); err != nil {
+ t.Fatal(err)
+ }
+ r, err := OpenFailureBook(dir)
+ if err != nil {
+ t.Fatal(err)
+ }
+ mark, marked := r.HoldRanOn("a")
+ if r.Interrupted() != 1 || r.Reconciled() != 0 || r.HeldDown("bab", "blog", "v2", "a") || len(r.List()) != 0 ||
+ !marked || mark.Build != "v2" || mark.Exec != "a1-3" {
+ t.Fatalf("a hold intent in a release phase: reconciled %d, interrupted %d, records %v, marker %+v %v",
+ r.Reconciled(), r.Interrupted(), r.List(), mark, marked)
+ }
+}
+
+// Astra R3 round 1, finding 3: a rootfs first wake's intent starts in its
+// release phase too. An agent that dies during B's release (before the
+// activator ends the phase, WakePlan.BeforeWeb) reopens with no failure of
+// B and no hold-down, and the next agent's planner boots B again, release
+// first: an overlap row is not moved onto the good build, a hold row is not
+// left down (it keeps the hold-ran marker, so the good build never boots on
+// a database B's release ran against). Once the phase has ended, a crash is
+// B's possible failure, as a binary first wake's always is.
+func TestAnInterruptedFirstWakeReleaseReopensWithNoFailure(t *testing.T) {
+ for _, tc := range []struct {
+ name string
+ rootfs bool
+ hold bool
+ webRan bool
+ }{
+ {"rootfs overlap, during the release", true, false, false},
+ {"rootfs hold, during the release", true, true, false},
+ {"rootfs overlap, after the web started", true, false, true},
+ {"rootfs hold, after the web started", true, true, true},
+ {"binary overlap (unchanged)", false, false, false},
+ {"binary hold (unchanged)", false, true, false},
+ } {
+ t.Run(tc.name, func(t *testing.T) {
+ h := newHarness(t, "a")
+ if tc.rootfs {
+ r := h.rows.get("a")
+ r.Runtime = registry.RuntimeRootfsV1
+ h.rows.put(r)
+ }
+ if tc.hold {
+ h.mu.Lock()
+ h.demands["v2"] = "hold"
+ h.mu.Unlock()
+ }
+ dir := h.onDisk()
+ h.stage("v2")
+ h.install("v2", "a")
+ h.wakeHost("a")
+ p := h.m.Plan(context.Background(), h.rows.get("a"))
+ wantMode := activator.ModeOverlap
+ if tc.hold {
+ wantMode = activator.ModeHold
+ }
+ if !p.FirstWake || p.Boot.Version != "v2" || p.Mode != wantMode || (p.BeforeWeb != nil) != tc.rootfs {
+ t.Fatalf("plan %+v (BeforeWeb set %v): want a first wake of v2 in %s, BeforeWeb on a rootfs row only", p, p.BeforeWeb != nil, wantMode)
+ }
+ if tc.webRan {
+ if err := p.BeforeWeb(); err != nil {
+ t.Fatalf("BeforeWeb: %v", err)
+ }
+ }
+ wantPhase := ""
+ if tc.rootfs && !tc.webRan {
+ wantPhase = IntentPhaseRelease
+ }
+ if in, ok := fileOf(t, dir).Intents["a"]; !ok || in.Build != "v2" || in.Mode != string(wantMode) || in.Phase != wantPhase {
+ t.Fatalf("the wake's intent on disk at the crash: %+v, %v; want phase %q", in, ok, wantPhase)
+ }
+
+ // The agent dies during the wake; the next one opens the book.
+ b, err := OpenFailureBook(dir)
+ if err != nil {
+ t.Fatal(err)
+ }
+ failed := false
+ for _, r := range b.List() {
+ failed = failed || (r.Build == "v2" && r.Host == "a")
+ }
+ interrupted := tc.rootfs && !tc.webRan
+ if failed == interrupted || (b.Interrupted() == 1) != interrupted {
+ t.Fatalf("reopened: v2 failed %v, interrupted %d; want an interruption %v", failed, b.Interrupted(), interrupted)
+ }
+ heldDown := b.HeldDown("bab", "blog", "v2", "a")
+ if heldDown != (tc.hold && !interrupted) {
+ t.Fatalf("reopened: held down %v", heldDown)
+ }
+ if mark, ok := b.HoldRanOn("a"); ok != tc.hold || (ok && mark.Build != "v2") {
+ t.Fatalf("reopened: hold-ran marker %+v, %v; want one on a hold", mark, ok)
+ }
+ if err := b.Flush(); err != nil {
+ t.Fatal(err)
+ }
+
+ // The next agent's planner.
+ h.restartOn(b)
+ h.wakeHost("a")
+ next := h.m.Plan(context.Background(), h.rows.get("a"))
+ switch {
+ case interrupted:
+ if !next.FirstWake || next.Boot.Version != "v2" || next.Mode != wantMode || next.Refuse != "" || next.BeforeWeb == nil {
+ t.Fatalf("after an interrupted release: plan %+v; want v2's first wake again, its release first", next)
+ }
+ case tc.hold:
+ if next.Refuse == "" {
+ t.Fatalf("after a hold's possible failure: plan %+v; want it refused (held down)", next)
+ }
+ default:
+ if next.FirstWake || next.Boot.Version != "v1" {
+ t.Fatalf("after a possible failure: plan %+v; want the good build v1", next)
+ }
+ }
+ })
+ }
+}
diff --git a/internal/rollout/scheduler.go b/internal/rollout/scheduler.go
index 409650d3c..26b6bbfcf 100644
--- a/internal/rollout/scheduler.go
+++ b/internal/rollout/scheduler.go
@@ -194,6 +194,17 @@ func (s *scheduler) execute(ctx context.Context, d dispatch) {
mode = m.d.Act.EffectiveMode(rt.Host, a.Mode)
}
in := Intent{Account: s.key.acct, App: s.key.app, Build: d.ID.Build, Mode: string(mode), ID: d.ExecID, Start: d.Started}
+ // A rootfs overlap begins in its release phase (Group R; Astra R2
+ // round 1, P1 2): its release runs beside the serving incumbent
+ // before anything else of the build, and an agent that dies there
+ // has not seen the build fail. The activator ends the phase through
+ // BeforeWeb, before the web starts. A binary row's intent, and a
+ // hold's, are as they were.
+ if !rt.IsSidecar() && rt.Rootfs() && mode == activator.ModeOverlap {
+ in.Phase = IntentPhaseRelease
+ host, exec := d.ID.Host, d.ExecID
+ a.BeforeWeb = func() error { return m.d.Book.Advance(host, exec) }
+ }
if err := m.d.Book.Begin(d.ID.Host, in); err != nil { // rolloutsim:M10
m.d.Logf("rollout: %s: write the intent of its %s attempt onto %s: %v; not running it", d.ID.Host, d.ID.Cause, d.ID.Build, err)
reason := "failure records could not be written"
diff --git a/internal/rollout/wakeplan.go b/internal/rollout/wakeplan.go
index bf4544ea1..67ce2b934 100644
--- a/internal/rollout/wakeplan.go
+++ b/internal/rollout/wakeplan.go
@@ -309,6 +309,17 @@ func (m *Manager) beginWake(rt registry.Route, plan activator.WakePlan, tok uint
}
in := Intent{Account: release.RouteAccount(rt), App: rt.App, Build: plan.Boot.Version, Mode: string(plan.Mode),
ID: m.wakeExecID(tok), Start: planAt}
+ // A rootfs first wake begins in its release phase (Group R; Astra R2
+ // round 1, P1 2, R3 round 1, finding 3): the build's release runs
+ // under the wake's lease before anything else of it, and an agent that
+ // dies there has not seen the build fail. The activator ends the phase
+ // through BeforeWeb, before the build's web starts. A binary row's
+ // intent is as it was.
+ if rt.Rootfs() && !rt.IsSidecar() {
+ in.Phase = IntentPhaseRelease
+ host, exec := rt.Host, in.ID
+ plan.BeforeWeb = func() error { return m.d.Book.Advance(host, exec) }
+ }
err := m.d.Book.Begin(rt.Host, in)
if err == nil {
// The intent is on disk: Close now waits for the terminal event