| index 5d6aaef30..e7d1f19a8 100644 |
| --- a/internal/activator/replica_install_wait_test.go |
| +++ b/internal/activator/replica_install_wait_test.go |
| @@ -3,259 +3,380 @@ package activator |
| import ( |
| "context" |
| "errors" |
| - "fmt" |
| "slices" |
| "testing" |
| "time" |
| |
| "github.com/carlosframework/platform/internal/registry" |
| + "github.com/carlosframework/platform/internal/release" |
| "github.com/carlosframework/platform/internal/replicas" |
| ) |
| |
| -// TestJoinInstallWaitKeepsStepOpen (#1061 W1): a joining permit holder |
| -// whose wakes are refused because no build is installed on its box yet |
| -// (WakePlan.Installing, errBuildNotInstalled) keeps its step open past |
| -// replicaRestartBound, up to releaseInstallBound from the first such |
| -// refusal, and is never answered switched-failed. The refusal is no crash: |
| -// it grows no resident backoff, so the next tick after root's stamp starts |
| -// the copy. |
| -// - installed at 60 s: the step stays open through the wait, the copy |
| -// launches once the planner boots it, and the step proves the switch. |
| -// - never installed: past releaseInstallBound the step answers kept with |
| -// the install reason (SR-6-C01); the ACK halts the join and the box is |
| -// not in Completed. |
| -func TestJoinInstallWaitKeepsStepOpen(t *testing.T) { |
| +// installJoin is box b's permitted join of deploy-v2 at permit (and |
| +// revision) rev, copies 1 -> 2: a ran the release and is Completed. |
| +func installJoin(rev uint64) func(string) *replicas.Deploy { |
| + return func(inc string) *replicas.Deploy { |
| + return &replicas.Deploy{ID: "deploy-v2", Incarnation: inc, Target: replicaTarget, BaselineSet: true, LastGood: replicaTarget, Phase: "released", Term: "term", Revision: rev, |
| + Swap: replicas.Swap{Order: []string{"a", "b"}, Completed: []string{"a"}, ActiveBox: "b", Permit: rev}} |
| + } |
| +} |
| + |
| +// installRig is an always-on joining copy on box b whose planner refuses |
| +// every wake as not installed (WakePlan.Installing) until the test says |
| +// otherwise, with wake and a proof-only (or awake) step of the permit. |
| +type installRig struct { |
| + *replicaSetRig |
| + t *testing.T |
| +} |
| + |
| +var ( |
| + planNotInstalled = WakePlan{Refuse: "no build is installed for this instance yet", Installing: true, Token: 2} |
| + // planUnconfirmed is the same words unmarked: a row the planner could |
| + // not read (a registry outage; fix round 1, P2). |
| + planUnconfirmed = WakePlan{Refuse: "no build is installed for this instance yet", Token: 3} |
| + // planMoving is the rollout planner's moving refusal: untyped. |
| + planMoving = WakePlan{Refuse: "this instance is still moving to another build", Token: 4} |
| +) |
| + |
| +func newInstallRig(t *testing.T) *installRig { |
| + t.Helper() |
| prev := swapSettle |
| swapSettle = 20 * time.Millisecond |
| t.Cleanup(func() { swapSettle = prev }) |
| - join := func(inc string) *replicas.Deploy { |
| - return &replicas.Deploy{ID: "deploy-v2", Incarnation: inc, Target: replicaTarget, BaselineSet: true, LastGood: replicaTarget, Phase: "released", Term: "term", Revision: 5, |
| - Swap: replicas.Swap{Order: []string{"a", "b"}, Completed: []string{"a"}, ActiveBox: "b", Permit: 5}} |
| + r := newReplicaSetRig(t, "b", installJoin(5)) |
| + r.rt.Hibernate = false |
| + r.routes.set(r.rt) |
| + r.setPlan(planNotInstalled) |
| + return &installRig{replicaSetRig: r, t: t} |
| +} |
| + |
| +// wakeWith wakes once under plan and returns the wake's error. |
| +func (r *installRig) wakeWith(plan WakePlan) error { |
| + r.t.Helper() |
| + r.setPlan(plan) |
| + return waitErr(r.t, r.wake(r.a)) |
| +} |
| + |
| +func (r *installRig) step(awake bool) SwapResult { |
| + at := Attempt{Host: r.rt.Host, Cause: CauseRelease, Build: "v2", StagedPath: r.next.StagedPath, Mode: ModeOverlap, SchedRev: 1, ProofOnly: true} |
| + if awake { |
| + at = r.attempt(CauseRelease, ModeOverlap) |
| } |
| - notInstalled := WakePlan{Refuse: "no build is installed for this instance yet", Installing: true, Token: 2} |
| - setup := func(t *testing.T) (*replicaSetRig, func() error, func(bool) SwapResult) { |
| - r := newReplicaSetRig(t, "b", join) |
| - r.rt.Hibernate = false |
| - r.routes.set(r.rt) |
| - r.setPlan(notInstalled) |
| - wake := func() error { return waitErr(t, r.wake(r.a)) } |
| - step := func(awake bool) SwapResult { |
| - at := Attempt{Host: r.rt.Host, Cause: CauseRelease, Build: "v2", StagedPath: r.next.StagedPath, Mode: ModeOverlap, SchedRev: 1, ProofOnly: true} |
| - if awake { |
| - at = r.attempt(CauseRelease, ModeOverlap) |
| - } |
| - return r.a.ReplicaSwap(context.Background(), at, *r.store.snapshot(), "b", newestOne) |
| - } |
| - return r, wake, step |
| + return r.a.ReplicaSwap(context.Background(), at, *r.store.snapshot(), "b", newestOne) |
| +} |
| + |
| +// refused wakes once as not installed and asserts the typed refusal, its |
| +// text unchanged, and no resident backoff from it. |
| +func (r *installRig) refused(when string) { |
| + r.t.Helper() |
| + r.a.mu.Lock() |
| + var before int |
| + if st := r.a.residents[r.rt.Host]; st != nil { |
| + before = st.failures |
| } |
| - // refused wakes once, asserts the typed refusal with its text |
| - // unchanged, and that it set no resident backoff. |
| - refused := func(t *testing.T, r *replicaSetRig, wake func() error, when string) { |
| - t.Helper() |
| - err := wake() |
| - if !errors.Is(err, errBuildNotInstalled) { |
| - t.Fatalf("%s: the wake was not refused as not installed: %v", when, err) |
| - } |
| - if want := r.rt.Host + ": refusing to wake: no build is installed for this instance yet"; err.Error() != want { |
| - t.Fatalf("%s: the refusal's text changed: %q, want %q", when, err.Error(), want) |
| - } |
| - r.a.mu.Lock() |
| - st := r.a.residents[r.rt.Host] |
| - var failures int |
| - var retryAt, since time.Time |
| - if st != nil { |
| - failures, retryAt, since = st.failures, st.retryAt, st.installSince |
| - } |
| - now := r.a.now() |
| - r.a.mu.Unlock() |
| - if failures != 0 || retryAt.After(now) { |
| - t.Fatalf("%s: the not-installed refusal was counted as a crash: failures %d, retry in %s", when, failures, retryAt.Sub(now)) |
| - } |
| - if since.IsZero() { |
| - t.Fatalf("%s: the install wait was not recorded", when) |
| - } |
| + r.a.mu.Unlock() |
| + err := r.wakeWith(planNotInstalled) |
| + if !errors.Is(err, errBuildNotInstalled) { |
| + r.t.Fatalf("%s: the wake was not refused as not installed: %v", when, err) |
| } |
| - open := func(t *testing.T, res SwapResult, when string) { |
| - t.Helper() |
| - if res.Proven || res.WebFailed { |
| - t.Fatalf("%s: the waiting copy's step answered: %+v", when, res) |
| - } |
| + if want := r.rt.Host + ": refusing to wake: no build is installed for this instance yet"; err.Error() != want { |
| + r.t.Fatalf("%s: the refusal's text changed: %q, want %q", when, err.Error(), want) |
| + } |
| + r.a.mu.Lock() |
| + st := r.a.residents[r.rt.Host] |
| + failures, retryAt, now := st.failures, st.retryAt, r.a.now() |
| + r.a.mu.Unlock() |
| + if failures != before || retryAt.After(now) { |
| + r.t.Fatalf("%s: the not-installed refusal was counted as a crash: failures %d (before %d), retry in %s", when, failures, before, retryAt.Sub(now)) |
| + } |
| +} |
| + |
| +// backedOff wakes once under plan, a refusal that is not an install wait, |
| +// and asserts it was counted toward the backoff. |
| +func (r *installRig) backedOff(plan WakePlan, when string) { |
| + r.t.Helper() |
| + r.a.mu.Lock() |
| + var before int |
| + if st := r.a.residents[r.rt.Host]; st != nil { |
| + before = st.failures |
| + } |
| + r.a.mu.Unlock() |
| + err := r.wakeWith(plan) |
| + if err == nil || errors.Is(err, errBuildNotInstalled) { |
| + r.t.Fatalf("%s: want an untyped refusal, got %v", when, err) |
| + } |
| + r.a.mu.Lock() |
| + failures := r.a.residents[r.rt.Host].failures |
| + r.a.mu.Unlock() |
| + if failures != before+1 { |
| + r.t.Fatalf("%s: the refusal kept no backoff: failures %d, want %d", when, failures, before+1) |
| + } |
| +} |
| + |
| +func (r *installRig) since() time.Time { |
| + r.a.mu.Lock() |
| + defer r.a.mu.Unlock() |
| + return r.a.installWaitLocked(r.rt) |
| +} |
| + |
| +func (r *installRig) open(res SwapResult, when string) { |
| + r.t.Helper() |
| + if res.Proven || res.WebFailed { |
| + r.t.Fatalf("%s: the waiting copy's step answered: %+v", when, res) |
| + } |
| +} |
| + |
| +func (r *installRig) kept(res SwapResult, when string) { |
| + r.t.Helper() |
| + if !res.Proven || res.WebFailed || res.Outcome == SwapDone || res.Reason != replicaInstallRefusal { |
| + r.t.Fatalf("%s: not answered kept for the install: %+v", when, res) |
| } |
| +} |
| + |
| +func (r *installRig) switchedFailed(res SwapResult, when string) { |
| + r.t.Helper() |
| + if !res.Proven || !res.WebFailed { |
| + r.t.Fatalf("%s: not answered switched-failed: %+v", when, res) |
| + } |
| +} |
| + |
| +// TestJoinInstallWaitKeepsStepOpen (#1061 W1, fix round 2): a joining |
| +// permit holder whose wakes are refused because no build is installed on |
| +// its box yet keeps its step open past replicaRestartBound, up to |
| +// releaseInstallBound from the first such refusal, and is never answered |
| +// switched-failed while it waits. The refusal grows no backoff. The wait |
| +// is set and cleared in one place (noteInstallWaitLocked): only a started |
| +// generation, its crash, or a new row lifetime, deploy, target or permit |
| +// clears it; a registry outage or the moving refusal does not. |
| +func TestJoinInstallWaitKeepsStepOpen(t *testing.T) { |
| t.Run("installed at 60s", func(t *testing.T) { |
| - r, wake, step := setup(t) |
| - refused(t, r, wake, "first wake") |
| - open(t, step(false), "at once") |
| + r := newInstallRig(t) |
| + r.refused("first wake") |
| + r.open(r.step(false), "at once") |
| r.clk.advance(60 * time.Second) |
| - refused(t, r, wake, "wake at 60s") |
| - open(t, step(false), "past replicaRestartBound") |
| - r.setPlan(WakePlan{Boot: r.next, Mode: ModeOverlap, Token: 3}) |
| - if err := wake(); err != nil { |
| + r.refused("wake at 60s") |
| + r.open(r.step(false), "past replicaRestartBound") |
| + if err := r.wakeWith(WakePlan{Boot: r.next, Mode: ModeOverlap, Token: 5}); err != nil { |
| t.Fatalf("wake with the build installed: %v", err) |
| } |
| - r.a.mu.Lock() |
| - since := r.a.residents[r.rt.Host].installSince |
| - r.a.mu.Unlock() |
| - if !since.IsZero() { |
| - t.Fatal("a successful wake left the install wait recorded") |
| + if !r.since().IsZero() { |
| + t.Fatal("a started generation left the install wait set") |
| } |
| - res := step(true) |
| + res := r.step(true) |
| if res.Outcome != SwapDone || !res.Proven || res.WebFailed { |
| t.Fatalf("the joined copy's switch was not proven: %+v", res) |
| } |
| }) |
| - // #1061 fix round 1, P1: a moving refusal (an attempt in flight) ran |
| - // nothing, so it neither grows the backoff nor restarts the install |
| - // deadline. A step after replicaRestartBound whose last wake was a |
| - // moving refusal stays open, and install, moving, install ... runs to |
| - // one deadline from the first install refusal, then kept. |
| - moving := WakePlan{Refuse: "this instance is still moving to another build", Moving: true, Token: 4} |
| - movingWake := func(t *testing.T, r *replicaSetRig, wake func() error, when string) { |
| - t.Helper() |
| - r.setPlan(moving) |
| - err := wake() |
| - r.setPlan(notInstalled) |
| - if !errors.Is(err, errInstanceMoving) || errors.Is(err, errBuildNotInstalled) { |
| - t.Fatalf("%s: the wake was not refused as moving: %v", when, err) |
| + t.Run("never installed", func(t *testing.T) { |
| + r := newInstallRig(t) |
| + r.refused("first wake") |
| + r.open(r.step(false), "at once") |
| + for gone := 50 * time.Second; gone < releaseInstallBound; gone += 50 * time.Second { |
| + r.clk.advance(50 * time.Second) |
| + r.refused("wake after " + gone.String()) |
| + r.open(r.step(false), "within the bound") |
| } |
| - if want := r.rt.Host + ": refusing to wake: this instance is still moving to another build"; err.Error() != want { |
| - t.Fatalf("%s: the refusal's text changed: %q", when, err.Error()) |
| + r.clk.advance(50 * time.Second) |
| + r.refused("wake past the bound") |
| + r.kept(r.step(false), "past the bound") |
| + r.kept(r.step(false), "the repeated step") |
| + if err := r.wakeWith(planNotInstalled); !errors.Is(err, ErrFenced) { |
| + t.Fatalf("a wake before the ACK was not refused by the kept fence: %v", err) |
| } |
| + cur := *r.store.snapshot() |
| + next, err := replicas.NextDeploy(cur, replicas.DeployChange{Op: "ack", ID: cur.ID, Box: "b", Revision: cur.Revision, Target: cur.Target, Permit: cur.Swap.Permit}, replicas.Coordinator{}, time.Now()) |
| + if err != nil || next.PermitOutstanding() || !next.Swap.Halted || slices.Contains(next.Swap.Completed, "b") { |
| + t.Fatalf("the join did not halt with b out of Completed: %v %+v", err, next.Swap) |
| + } |
| + r.a.SetReplicaDeploy(r.rt.Host, next, "b") |
| r.a.mu.Lock() |
| - failures, retryAt := r.a.residents[r.rt.Host].failures, r.a.residents[r.rt.Host].retryAt |
| - now := r.a.now() |
| + fence := r.a.fences[r.rt.Host] |
| r.a.mu.Unlock() |
| - if failures != 0 || retryAt.After(now) { |
| - t.Fatalf("%s: the moving refusal was counted as a crash: failures %d", when, failures) |
| + if fence != 0 { |
| + t.Fatalf("the kept fence outlived its ACK: %d", fence) |
| } |
| - } |
| - since := func(r *replicaSetRig) time.Time { |
| - r.a.mu.Lock() |
| - defer r.a.mu.Unlock() |
| - return r.a.residents[r.rt.Host].installSince |
| - } |
| - t.Run("moving refusal last, past replicaRestartBound", func(t *testing.T) { |
| - r, wake, step := setup(t) |
| - refused(t, r, wake, "first wake") |
| - first := since(r) |
| - open(t, step(false), "at once") |
| + }) |
| + // Astra re-review 1, P1: install refusal and a step at 0; a registry |
| + // error at 40 s; the registry recovers with no wake before the next |
| + // step. That step stays open (the outage kept its backoff and did not |
| + // clear the wait), and past the bound from the first refusal it is |
| + // kept, never switched-failed. |
| + t.Run("a registry outage between steps", func(t *testing.T) { |
| + r := newInstallRig(t) |
| + r.refused("first wake") |
| + r.open(r.step(false), "at once") |
| r.clk.advance(40 * time.Second) |
| - movingWake(t, r, wake, "moving at 40s") |
| - if got := since(r); !got.Equal(first) { |
| - t.Fatalf("the moving refusal moved the install deadline: %s, want %s", got, first) |
| - } |
| - open(t, step(false), "past replicaRestartBound, a moving refusal last") |
| + r.backedOff(planUnconfirmed, "the registry error at 40s") |
| + r.clk.advance(5 * time.Second) |
| + r.open(r.step(false), "past replicaRestartBound after the outage") |
| + r.clk.advance(releaseInstallBound) |
| + r.kept(r.step(false), "past the bound after the outage") |
| }) |
| + // install, moving, install ... is one deadline from the first install |
| + // refusal. The moving refusal keeps its crash backoff (P3 reverted), |
| + // so the wakes are spaced past the backoff's 72 s ceiling. |
| t.Run("install, moving, install keeps one deadline", func(t *testing.T) { |
| - r, wake, step := setup(t) |
| - refused(t, r, wake, "first wake") |
| - first := since(r) |
| - for gone := time.Duration(0); gone+50*time.Second < releaseInstallBound; gone += 50 * time.Second { |
| - r.clk.advance(25 * time.Second) |
| - movingWake(t, r, wake, "moving") |
| - r.clk.advance(25 * time.Second) |
| - refused(t, r, wake, "install") |
| - if got := since(r); !got.Equal(first) { |
| - t.Fatalf("after %s the install deadline restarted: %s, want %s", gone+50*time.Second, got, first) |
| + r := newInstallRig(t) |
| + r.refused("first wake") |
| + first := r.since() |
| + gap := 75 * time.Second |
| + for gone := time.Duration(0); gone+2*gap < releaseInstallBound; gone += 2 * gap { |
| + r.clk.advance(gap) |
| + r.backedOff(planMoving, "moving") |
| + if got := r.since(); !got.Equal(first) { |
| + t.Fatalf("the moving refusal moved the install deadline: %s, want %s", got, first) |
| + } |
| + r.open(r.step(false), "a moving refusal last") |
| + r.clk.advance(gap) |
| + r.refused("install") |
| + if got := r.since(); !got.Equal(first) { |
| + t.Fatalf("the install deadline restarted: %s, want %s", got, first) |
| } |
| - open(t, step(false), "within the bound") |
| + r.open(r.step(false), "within the bound") |
| } |
| r.clk.advance(releaseInstallBound) |
| - movingWake(t, r, wake, "moving past the bound") |
| - if res := step(false); !res.Proven || res.WebFailed || res.Reason != replicaInstallRefusal { |
| - t.Fatalf("past the one deadline, not answered kept for the install: %+v", res) |
| - } |
| + r.backedOff(planMoving, "moving past the bound") |
| + r.kept(r.step(false), "past the one deadline") |
| }) |
| - t.Run("never installed", func(t *testing.T) { |
| - r, wake, step := setup(t) |
| - refused(t, r, wake, "first wake") |
| - open(t, step(false), "at once") |
| - for gone := time.Duration(0); gone <= releaseInstallBound; gone += 50 * time.Second { |
| - r.clk.advance(50 * time.Second) |
| - refused(t, r, wake, "wake after "+(gone+50*time.Second).String()) |
| - if gone+50*time.Second < releaseInstallBound { |
| - open(t, step(false), "within the bound") |
| - } |
| + // A new permit (a retry of the join) gets its own install window: an |
| + // expired wait of the old permit is not inherited (concern 3). |
| + t.Run("a new permit starts its own wait", func(t *testing.T) { |
| + r := newInstallRig(t) |
| + r.refused("first wake") |
| + for gone := time.Duration(0); gone <= releaseInstallBound; gone += time.Minute { |
| + r.clk.advance(time.Minute) |
| + r.refused("waiting") |
| } |
| - res := step(false) |
| - if !res.Proven || res.WebFailed || res.Outcome == SwapDone || res.Reason != replicaInstallRefusal { |
| - t.Fatalf("past the bound, not answered kept for the install: %+v", res) |
| + next := *installJoin(6)(r.inc) |
| + r.store.mu.Lock() |
| + r.store.d = &next |
| + r.store.mu.Unlock() |
| + r.a.SetReplicaDeploy(r.rt.Host, next, "b") |
| + if !r.since().IsZero() { |
| + t.Fatal("the old permit's wait counts for the new permit") |
| } |
| - if again := step(false); !again.Proven || again.WebFailed || again.Reason != replicaInstallRefusal { |
| - t.Fatalf("the repeated step did not keep its answer: %+v", again) |
| + r.refused("first wake under the new permit") |
| + if got := r.since(); !got.Equal(r.clk.now()) { |
| + t.Fatalf("the new permit's wait starts at %s, want now (%s)", got, r.clk.now()) |
| } |
| - if err := wake(); !errors.Is(err, ErrFenced) { |
| - t.Fatalf("a wake before the ACK was not refused by the kept fence: %v", err) |
| - } |
| - cur := *r.store.snapshot() |
| - next, err := replicas.NextDeploy(cur, replicas.DeployChange{Op: "ack", ID: cur.ID, Box: "b", Revision: cur.Revision, Target: cur.Target, Permit: cur.Swap.Permit}, replicas.Coordinator{}, time.Now()) |
| - if err != nil || next.PermitOutstanding() || !next.Swap.Halted || slices.Contains(next.Swap.Completed, "b") { |
| - t.Fatalf("the join did not halt with b out of Completed: %v %+v", err, next.Swap) |
| + r.open(r.step(false), "the new permit's first step") |
| + r.clk.advance(time.Minute) |
| + r.refused("a minute into the new permit") |
| + r.open(r.step(false), "a minute into the new permit") |
| + }) |
| + // The old rules stand where no install wait is set: a started |
| + // generation that crashes, then cold wakes that fail, answers |
| + // switched-failed past replicaRestartBound; and an outage alone backs |
| + // off and follows the same 32 s rule. |
| + t.Run("a started generation that crashes", func(t *testing.T) { |
| + r := newInstallRig(t) |
| + r.refused("first wake") |
| + if err := r.wakeWith(WakePlan{Boot: r.next, Mode: ModeOverlap, Token: 5}); err != nil { |
| + t.Fatalf("wake with the build installed: %v", err) |
| } |
| - r.a.SetReplicaDeploy(r.rt.Host, next, "b") |
| r.a.mu.Lock() |
| - fence := r.a.fences[r.rt.Host] |
| + n := r.a.entries[r.rt.Host].gen.N |
| r.a.mu.Unlock() |
| - if fence != 0 { |
| - t.Fatalf("the kept fence outlived its ACK: %d", fence) |
| + r.b.procOf(n).exit() |
| + r.a.sweepOnce() |
| + waitFor(t, "the crashed entry to go", func() bool { |
| + r.a.mu.Lock() |
| + defer r.a.mu.Unlock() |
| + return r.a.entries[r.rt.Host] == nil |
| + }) |
| + if !r.since().IsZero() { |
| + t.Fatal("an install wait survived the started generation") |
| } |
| + r.a.StorageDeclaration = func(context.Context, registry.Route, release.StagedIdentity) (bool, string, error) { |
| + return false, "", errors.New("the staged build cannot be read") |
| + } |
| + r.clk.advance(2 * time.Second) |
| + if err := r.wakeWith(WakePlan{Boot: r.next, Mode: ModeOverlap, Token: 6}); err == nil { |
| + t.Fatal("the cold wake served") |
| + } |
| + r.open(r.step(false), "within replicaRestartBound") |
| + r.clk.advance(replicaRestartBound + time.Second) |
| + r.switchedFailed(r.step(false), "past replicaRestartBound") |
| + }) |
| + t.Run("an outage alone", func(t *testing.T) { |
| + r := newInstallRig(t) |
| + r.backedOff(planUnconfirmed, "the first outage") |
| + if !r.since().IsZero() { |
| + t.Fatal("an outage set an install wait") |
| + } |
| + r.open(r.step(false), "at once") |
| + r.clk.advance(replicaRestartBound + time.Second) |
| + r.backedOff(planUnconfirmed, "the outage past the bound") |
| + r.switchedFailed(r.step(false), "past replicaRestartBound") |
| }) |
| } |
| |
| -// TestInstallWaitDeadlineRules (#1061 fix round 1, P1): the install wait's |
| -// deadline is kept across refusals that ran nothing and cleared by a wake |
| -// that served, a failure of the build, or another row lifetime. |
| -func TestInstallWaitDeadlineRules(t *testing.T) { |
| - r := newWakeRelRig(t) |
| +// TestInstallWaitRules (#1061 fix round 2): noteInstallWaitLocked is the |
| +// one place the wait is set and cleared. Set by an install refusal for a |
| +// key with no wait; kept by every other wake outcome that started nothing |
| +// (a registry outage, the moving refusal, a fence, a crash-classified |
| +// refusal); cleared by a started generation and by its crash; and a wait |
| +// of another key (target, permit, row lifetime) is no wait. |
| +func TestInstallWaitRules(t *testing.T) { |
| + r := newInstallRig(t) |
| rt := r.rt |
| - rt.Hibernate = false |
| - rt.ReplicaIncarnation = "inc-1" |
| - notInstalled := fmt.Errorf("%s: refusing to wake: %w", rt.Host, typedRefusal{"no build is installed for this instance yet", errBuildNotInstalled}) |
| - moving := fmt.Errorf("%s: refusing to wake: %w", rt.Host, typedRefusal{"this instance is still moving to another build", errInstanceMoving}) |
| - note := func(rt registry.Route, err error) (time.Time, int) { |
| + note := func(rt registry.Route, err error) time.Time { |
| r.a.mu.Lock() |
| defer r.a.mu.Unlock() |
| r.a.noteResidentWakeLocked(&actEntry{host: rt.Host, route: rt}, err) |
| - st := r.a.residents[rt.Host] |
| - return st.installSince, st.failures |
| + return r.a.installWaitLocked(rt) |
| } |
| start := r.clk.now() |
| - if got, _ := note(rt, notInstalled); !got.Equal(start) { |
| - t.Fatalf("the first install refusal did not start the deadline: %s", got) |
| + if got := note(rt, planRefusal(rt.Host, planNotInstalled)); !got.Equal(start) { |
| + t.Fatalf("the first install refusal did not start the wait: %s", got) |
| } |
| r.clk.advance(time.Minute) |
| - for _, err := range []error{moving, ErrFenced, notInstalled} { |
| - if got, failures := note(rt, err); !got.Equal(start) || failures != 0 { |
| - t.Fatalf("%v moved the deadline to %s (failures %d), want %s kept", err, got, failures, start) |
| + for _, err := range []error{ |
| + planRefusal(rt.Host, planUnconfirmed), planRefusal(rt.Host, planMoving), ErrFenced, |
| + errors.New("registry: disk I/O error"), planRefusal(rt.Host, planNotInstalled), |
| + } { |
| + if got := note(rt, err); !got.Equal(start) { |
| + t.Fatalf("%v moved the wait to %s, want %s kept", err, got, start) |
| } |
| } |
| - if got, failures := note(rt, errors.New("the web process exited at start")); !got.IsZero() || failures != 1 { |
| - t.Fatalf("a failure of the build kept the deadline %s (failures %d); a crash stays a crash", got, failures) |
| + if got := note(rt, nil); !got.IsZero() { |
| + t.Fatalf("a started generation kept the wait %s", got) |
| } |
| - r.clk.advance(time.Minute) |
| - restart := r.clk.now() |
| - note(rt, notInstalled) |
| - if got, _ := note(rt, nil); !got.IsZero() { |
| - t.Fatalf("a wake that served kept the deadline %s", got) |
| + note(rt, planRefusal(rt.Host, planNotInstalled)) |
| + r.a.mu.Lock() |
| + r.a.noteResidentStopLocked(&actEntry{host: rt.Host, route: rt, stopWhy: StopExited, spawnedAt: r.clk.now()}) |
| + crashed := r.a.installWaitLocked(rt) |
| + r.a.mu.Unlock() |
| + if !crashed.IsZero() { |
| + t.Fatalf("a started generation's crash kept the wait %s", crashed) |
| } |
| - note(rt, notInstalled) |
| - other := rt |
| - other.ReplicaIncarnation = "inc-2" |
| - if got, _ := note(other, moving); !got.IsZero() { |
| - t.Fatalf("another row lifetime kept the deadline %s", got) |
| + // Another target: the observed deploy moves to v3. |
| + note(rt, planRefusal(rt.Host, planNotInstalled)) |
| + moved := *installJoin(5)(r.inc) |
| + moved.ID, moved.Target = "deploy-v3", replicas.Build{Version: "v3", Digest: "canonical-v3"} |
| + r.a.SetReplicaDeploy(rt.Host, moved, "b") |
| + r.a.mu.Lock() |
| + other := r.a.installWaitLocked(rt) |
| + r.a.mu.Unlock() |
| + if !other.IsZero() { |
| + t.Fatalf("the old target's wait counts for the new target: %s", other) |
| } |
| r.clk.advance(time.Minute) |
| - note(rt, notInstalled) |
| - if got, _ := note(other, notInstalled); !got.Equal(r.clk.now()) || got.Equal(restart) { |
| - t.Fatalf("another row lifetime's install refusal did not start its own deadline: %s", got) |
| + if got := note(rt, planRefusal(rt.Host, planNotInstalled)); !got.Equal(r.clk.now()) { |
| + t.Fatalf("the new target's first install refusal did not start its own wait: %s", got) |
| } |
| - // Fix round 1, P2: the same words unmarked (a row the planner could |
| - // not read) are no install wait: planRefusal leaves them untyped, they |
| - // count toward the backoff and they clear the deadline. |
| - unconfirmed := planRefusal(other.Host, WakePlan{Refuse: "no build is installed for this instance yet"}) |
| - if errors.Is(unconfirmed, errBuildNotInstalled) || !residentFailure(unconfirmed) { |
| - t.Fatalf("an unmarked refusal was typed as an install wait: %v", unconfirmed) |
| + // Another row lifetime. |
| + life := rt |
| + life.ReplicaIncarnation = "another-lifetime" |
| + r.a.mu.Lock() |
| + gone := r.a.installWaitLocked(life) |
| + r.a.mu.Unlock() |
| + if !gone.IsZero() { |
| + t.Fatalf("another row lifetime inherited the wait %s", gone) |
| } |
| - if got, failures := note(other, unconfirmed); !got.IsZero() || failures == 0 { |
| - t.Fatalf("an unconfirmed install kept the deadline %s or set no backoff (failures %d)", got, failures) |
| + // Backoff is separate: an install refusal counts nothing, the same |
| + // words unmarked do. |
| + if residentFailure(planRefusal(rt.Host, planNotInstalled)) || !residentFailure(planRefusal(rt.Host, planUnconfirmed)) { |
| + t.Fatal("the install refusal and the outage are not told apart for the backoff") |
| } |
| } |