| index 942090974..29e16e4d5 100644 |
| --- a/internal/activator/rootfsworkers_test.go |
| +++ b/internal/activator/rootfsworkers_test.go |
| @@ -109,6 +109,19 @@ type fakeWorkerBackend struct { |
| // launches counts LaunchRootfsWorker calls per name: each is a |
| // preparation (prepareWorker binds the worker's log socket), gate or no. |
| launches map[string]int |
| + // hang, when set, makes the next start that passed its gate wait for |
| + // root's answer until its context ends (a start response that never |
| + // comes in time), then return its handle with the context's error; |
| + // it is cleared by that start. Its return is logged "returned <name>". |
| + hang bool |
| + // failStarts makes that many next starts that passed their gate return |
| + // their handle with an error (a 502: the start may have run). |
| + failStarts int |
| + // goneDelay is how long each RootfsWorkerGone takes (the worker's |
| + // drain); a proof that succeeds is logged "proven <name>" when it |
| + // returns, at provenAt. |
| + goneDelay time.Duration |
| + provenAt map[string][]time.Time |
| } |
| |
| func (b *fakeWorkerBackend) LaunchRootfsWorker(ctx context.Context, _ registry.Route, name string, g Gen, _ Proc) (Proc, error) { |
| @@ -138,13 +151,24 @@ func (b *fakeWorkerBackend) LaunchRootfsWorker(ctx context.Context, _ registry.R |
| p := &fakeWorkerProc{name: name} |
| b.wmu.Lock() |
| b.wprocs = append(b.wprocs, p) |
| - uncertain := b.uncertain |
| - b.uncertain = false |
| + uncertain, hang, fail := b.uncertain, b.hang, b.failStarts > 0 |
| + b.uncertain, b.hang = false, false |
| + if fail { |
| + b.failStarts-- |
| + } |
| b.wmu.Unlock() |
| if uncertain { |
| p.lingers = true |
| return p, errors.New("instance control: 502 Bad Gateway") |
| } |
| + if hang { |
| + <-ctx.Done() |
| + b.record("returned " + name) |
| + return p, fmt.Errorf("instance control: start %s: %w", name, ctx.Err()) |
| + } |
| + if fail { |
| + return p, errors.New("instance control: 502 Bad Gateway") |
| + } |
| return p, nil |
| } |
| |
| @@ -157,15 +181,23 @@ func (b *fakeWorkerBackend) launchCount(name string) int { |
| func (b *fakeWorkerBackend) RootfsWorkerGone(_ context.Context, _ registry.Route, name string) error { |
| b.record("gone " + name) |
| b.wmu.Lock() |
| - f := b.goneErr |
| + f, delay := b.goneErr, b.goneDelay |
| b.wmu.Unlock() |
| + if delay > 0 { |
| + time.Sleep(delay) |
| + } |
| if f != nil { |
| if err := f(name); err != nil { |
| return err |
| } |
| } |
| + b.record("proven " + name) |
| b.wmu.Lock() |
| defer b.wmu.Unlock() |
| + if b.provenAt == nil { |
| + b.provenAt = map[string][]time.Time{} |
| + } |
| + b.provenAt[name] = append(b.provenAt[name], time.Now()) |
| for _, p := range b.wprocs { |
| if p.name == name { |
| p.mu.Lock() |
| @@ -850,6 +882,17 @@ func TestRootfsWorkerStrayNamedAtShutdown(t *testing.T) { |
| // residentWorkerRig is a workerRig over an awake resident whose replica |
| // carries the lease's signal (its lease watch runs), with jobs and mailer. |
| func residentWorkerRig(t *testing.T) (*workerRig, func() *ownedRep) { |
| + t.Helper() |
| + r, rep := residentWorkerRigUntil(t, time.Hour) |
| + if n := len(r.b.workers("jobs")) + len(r.b.workers("mailer")); n != 2 { |
| + t.Fatalf("%d workers started, want 2", n) |
| + } |
| + return r, rep |
| +} |
| + |
| +// residentWorkerRigUntil is residentWorkerRig whose replica's safety |
| +// deadline is safe after its creation (on the real clock, as the lease's). |
| +func residentWorkerRigUntil(t *testing.T, safe time.Duration) (*workerRig, func() *ownedRep) { |
| t.Helper() |
| shrinkResidentPoll(t, time.Hour) // the test ticks by hand |
| rt := residentRoute("shop.example.com") |
| @@ -863,7 +906,7 @@ func residentWorkerRig(t *testing.T) (*workerRig, func() *ownedRep) { |
| fr.setOnClose(func() { kb.record("close") }) |
| mu.Lock() |
| defer mu.Unlock() |
| - rep = newOwnedRep(fr, time.Now().Add(time.Hour)) |
| + rep = newOwnedRep(fr, time.Now().Add(safe)) |
| return rep |
| } |
| kb.mu.Unlock() |
| @@ -875,9 +918,6 @@ func residentWorkerRig(t *testing.T) (*workerRig, func() *ownedRep) { |
| } |
| }) |
| startResident(t, r.a, r.host) |
| - if n := len(r.b.workers("jobs")) + len(r.b.workers("mailer")); n != 2 { |
| - t.Fatalf("%d workers started, want 2", n) |
| - } |
| return r, func() *ownedRep { |
| mu.Lock() |
| defer mu.Unlock() |
| @@ -1571,6 +1611,282 @@ func TestRootfsWorkerInstallNeedsTheLeaseWatch(t *testing.T) { |
| } |
| } |
| |
| +// shrinkWorkerBounds shrinks a worker start's bound and its proof's bound, |
| +// and so workerStartLead. Not parallel: they are package vars. |
| +func shrinkWorkerBounds(t *testing.T, start, proof time.Duration) { |
| + t.Helper() |
| + oldStart, oldProof := workerStartBound, workerProofBound |
| + workerStartBound, workerProofBound = start, proof |
| + t.Cleanup(func() { workerStartBound, workerProofBound = oldStart, oldProof }) |
| +} |
| + |
| +// sleepUntil waits on the real clock, as the lease runs. |
| +func sleepUntil(at time.Time) { |
| + if d := time.Until(at); d > 0 { |
| + time.Sleep(d) |
| + } |
| +} |
| + |
| +// [C7, Astra W1 r1 P1] Every worker is stopped and proven before the |
| +// lease's safety deadline, a worker whose start is in flight included, and |
| +// no worker start is admitted without the headroom to start, stop and |
| +// prove it first (workerStartLead, read from the lease's SafeUntil as a |
| +// release's is). On the real clock: lead 1.6 s (start 1 s, proof 600 ms), |
| +// each worker's drain 200 ms. |
| +func TestRootfsWorkerStartsFitBeforeTheLeaseDeadline(t *testing.T) { |
| + shrinkWorkerBounds(t, time.Second, 600*time.Millisecond) |
| + |
| + // A restart admitted with 2 s left, whose response never comes in |
| + // time: it waits out its 1 s bound, past the moment the lease's drain |
| + // begins (1.3 s before the deadline), and the lease then expires. The |
| + // other worker is proven at once, not behind the start; the started |
| + // one once its start ends; all before the deadline. |
| + t.Run("a delayed start in flight at the drain", func(t *testing.T) { |
| + shrinkDrainBudget(t, 1300*time.Millisecond) |
| + r, rep := residentWorkerRigUntil(t, 3*time.Second) |
| + until, err := rep().SafeUntil() |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if n := len(r.b.workers("jobs")) + len(r.b.workers("mailer")); n != 2 { |
| + t.Fatalf("%d workers started, want 2", n) |
| + } |
| + r.b.wmu.Lock() |
| + r.b.goneDelay = 200 * time.Millisecond |
| + r.b.wmu.Unlock() |
| + r.b.lastWorker(t, "jobs").exit("exited", "1") |
| + r.tick(t) |
| + r.clk.advance(sidecarRestartBase) |
| + sleepUntil(until.Add(-2 * time.Second)) |
| + r.b.wmu.Lock() |
| + r.b.hang = true |
| + r.b.wmu.Unlock() |
| + r.a.rootfsTick() |
| + waitFor(t, "the restart to reach root", func() bool { return len(r.b.workers("jobs")) == 2 }) |
| + waitFor(t, "the stop to finish", func() bool { return !r.a.hasEntry(r.host) }) |
| + ev := r.b.events() |
| + returned := index(ev, "returned jobs", 0) |
| + if mailer := index(ev, "proven mailer", 0); returned < 0 || mailer < 0 || mailer > returned { |
| + t.Fatalf("order %v: mailer's proof waited behind jobs' start in flight", ev) |
| + } |
| + if index(ev, "proven jobs", returned) < 0 { |
| + t.Fatalf("order %v: the started worker was not proven after its start ended", ev) |
| + } |
| + r.b.wmu.Lock() |
| + defer r.b.wmu.Unlock() |
| + for name, ats := range r.b.provenAt { |
| + if last := ats[len(ats)-1]; !last.Before(until) { |
| + t.Errorf("worker %s proven %s after the lease's safety deadline", name, last.Sub(until)) |
| + } |
| + } |
| + for _, p := range r.b.wprocs { |
| + if !p.Exited() { |
| + t.Errorf("worker %s runs after the stop", p.name) |
| + } |
| + } |
| + }) |
| + |
| + // A restart due with 1.4 s left (less than the 1.6 s lead) is not |
| + // admitted: root is asked nothing, and the drain proves the rest before |
| + // the deadline. |
| + t.Run("a restart without the headroom", func(t *testing.T) { |
| + shrinkDrainBudget(t, 1300*time.Millisecond) |
| + r, rep := residentWorkerRigUntil(t, 3*time.Second) |
| + until, err := rep().SafeUntil() |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + r.b.lastWorker(t, "jobs").exit("exited", "1") |
| + r.tick(t) |
| + r.clk.advance(sidecarRestartBase) |
| + sleepUntil(until.Add(-1400 * time.Millisecond)) |
| + r.a.rootfsTick() |
| + waitFor(t, "the restart to finish", r.settled) |
| + if n := len(r.b.workers("jobs")); n != 1 { |
| + t.Fatalf("root was asked for %d worker starts with 1.4 s left of a 1.6 s lead", n-1) |
| + } |
| + waitFor(t, "the stop to finish", func() bool { return !r.a.hasEntry(r.host) }) |
| + r.b.wmu.Lock() |
| + defer r.b.wmu.Unlock() |
| + if ats := r.b.provenAt["mailer"]; len(ats) == 0 || !ats[len(ats)-1].Before(until) { |
| + t.Errorf("mailer proven at %v, deadline %v", ats, until) |
| + } |
| + }) |
| + |
| + // The doWake start is bounded the same way: a start whose response |
| + // never comes ends on workerStartBound, the wake completes, web serves |
| + // and the worker is left for the supervisor, proven gone. |
| + t.Run("the wake's start is bounded", func(t *testing.T) { |
| + r := newWorkerRig(t, workerHost(), "jobs") |
| + r.b.wmu.Lock() |
| + r.b.hang = true |
| + r.b.wmu.Unlock() |
| + woke := make(chan struct{}) |
| + go func() { |
| + defer close(woke) |
| + if release, err := r.a.beginRequest(context.Background(), r.rt); err == nil { |
| + release() |
| + } |
| + }() |
| + select { |
| + case <-woke: |
| + case <-time.After(4 * time.Second): |
| + t.Fatal("the wake waited past its worker start's bound") |
| + } |
| + if !entryAwake(r.a, r.host) { |
| + t.Fatal("web does not serve after its worker's start timed out") |
| + } |
| + ev := r.b.events() |
| + if index(ev, "proven jobs", index(ev, "returned jobs", 0)) < 0 { |
| + t.Fatalf("order %v: the timed-out start was not proven gone", ev) |
| + } |
| + }) |
| + |
| + // The doWake start asks the same: a wake whose lease has less than the |
| + // lead left starts no worker, and web serves. |
| + t.Run("the wake's start without the headroom", func(t *testing.T) { |
| + shrinkDrainBudget(t, 100*time.Millisecond) |
| + r, rep := residentWorkerRigUntil(t, 1200*time.Millisecond) |
| + if n := len(r.b.workers("jobs")) + len(r.b.workers("mailer")); n != 0 { |
| + t.Fatalf("root was asked for %d worker starts with less than the lead left", n) |
| + } |
| + // The lease's drain then stops it (and the watch reads its budget |
| + // for the last time before that, under a.mu's order). |
| + until, err := rep().SafeUntil() |
| + if err == nil { |
| + sleepUntil(until.Add(-50 * time.Millisecond)) |
| + } |
| + waitFor(t, "the drain's stop", func() bool { return !r.a.hasEntry(r.host) }) |
| + }) |
| +} |
| + |
| +// [Astra W1 r1 P2] A failed proof before a restart never adds a stray: |
| +// the first one makes the exited worker's own handle a stray, once, and |
| +// every later failure keeps that one stray and reschedules, counting no |
| +// crash. |
| +func TestRootfsWorkerFailedProofsKeepOneStray(t *testing.T) { |
| + r := newWorkerRig(t, workerHost(), "jobs") |
| + r.wake(t) |
| + r.b.lastWorker(t, "jobs").exit("exited", "1") |
| + r.tick(t) |
| + r.b.setGone(func(string) error { return instancectl.ErrGenUnproven }) |
| + // Each round is past the restart's 2 s backoff and inside the stray's |
| + // actStopTimeout, so every failed proof is the restart's own. |
| + for i := 0; i < 8; i++ { |
| + r.clk.advance(3 * time.Second) |
| + r.tick(t) |
| + if got := r.strays(); len(got) != 1 { |
| + t.Fatalf("after %d failed proofs: strays %v, want the one", i+1, got) |
| + } |
| + } |
| + if n := r.b.launchCount("jobs"); n != 1 { |
| + t.Fatalf("%d starts beside an unproven worker", n-1) |
| + } |
| + if got := r.a.RootfsWorkers(r.host); len(got) != 1 || got[0].Restarts != 1 { |
| + t.Fatalf("report %+v: proof retries counted as crashes", got) |
| + } |
| + r.b.setGone(nil) |
| + for i := 0; i < 3 && r.b.launchCount("jobs") == 1; i++ { |
| + r.clk.advance(sidecarRestartMax) |
| + r.tick(t) |
| + } |
| + if n := r.b.launchCount("jobs"); n != 2 || len(r.strays()) != 0 { |
| + t.Fatalf("after the proof: %d starts, strays %v; want 1 and none", n-1, r.strays()) |
| + } |
| +} |
| + |
| +// [Astra W1 r1 P2] Each launch that fails (not a refusal) is one crash: |
| +// the backoff doubles and the count rises once per failure, C10 is |
| +// reached, whether its proof is given at once or the failure leaves a |
| +// stray, and the stray's proof retries count no more. |
| +func TestRootfsWorkerFailedStartsCountOnce(t *testing.T) { |
| + // restartAfter ticks second by second until jobs is asked again and |
| + // answers the wait. |
| + restartAfter := func(t *testing.T, r *workerRig) time.Duration { |
| + t.Helper() |
| + from, n := r.clk.now(), r.b.launchCount("jobs") |
| + for r.b.launchCount("jobs") == n { |
| + if r.clk.now().Sub(from) > 3*time.Minute { |
| + t.Fatal("no restart within 3m") |
| + } |
| + r.clk.advance(time.Second) |
| + r.tick(t) |
| + } |
| + return r.clk.now().Sub(from) |
| + } |
| + t.Run("proven at once", func(t *testing.T) { |
| + r := newWorkerRig(t, workerHost(), "jobs") |
| + r.wake(t) |
| + r.b.wmu.Lock() |
| + r.b.failStarts = 6 |
| + r.b.wmu.Unlock() |
| + r.b.lastWorker(t, "jobs").exit("exited", "1") |
| + r.tick(t) // the exit: one crash, 2 s |
| + for i, want := range []time.Duration{2, 4, 8, 16, 32, 60} { |
| + if got := restartAfter(t, r); got != want*time.Second { |
| + t.Errorf("start %d: asked after %s, want %s", i+1, got, want*time.Second) |
| + } |
| + if got := r.a.RootfsWorkers(r.host); len(got) != 1 || got[0].Restarts != i+2 { |
| + t.Errorf("after failed start %d: report %+v, want %d restarts", i+1, got, i+2) |
| + } |
| + } |
| + if n := len(r.sink.with(fmt.Sprintf(rolloutwords.EventWorkerCrashLoop, "jobs"))); n != 1 { |
| + t.Errorf("RFS-3-C10 emitted %d times, want once", n) |
| + } |
| + if len(r.strays()) != 0 { |
| + t.Errorf("strays %v for failures proven at once", r.strays()) |
| + } |
| + }) |
| + t.Run("left as a stray", func(t *testing.T) { |
| + r := newWorkerRig(t, workerHost(), "jobs") |
| + r.wake(t) |
| + r.b.lastWorker(t, "jobs").exit("exited", "1") |
| + r.tick(t) |
| + // The restart's own proof of the exited handle passes; its start |
| + // fails with a handle whose proof does not, and stays failing. |
| + var calls atomic.Int32 |
| + r.b.setGone(func(string) error { |
| + if calls.Add(1) >= 2 { |
| + return instancectl.ErrGenUnproven |
| + } |
| + return nil |
| + }) |
| + r.b.wmu.Lock() |
| + r.b.failStarts = 1 |
| + r.b.wmu.Unlock() |
| + if got := restartAfter(t, r); got != 2*time.Second { |
| + t.Errorf("first restart after %s, want 2s", got) |
| + } |
| + if got := r.a.RootfsWorkers(r.host); len(got) != 1 || got[0].Restarts != 2 { |
| + t.Fatalf("after the failed start: report %+v, want 2 restarts", got) |
| + } |
| + // The restart's own proofs fail (each round past its backoff, |
| + // inside the stray's actStopTimeout), then the tick's re-proofs. |
| + for i := 0; i < 4; i++ { |
| + r.clk.advance(5 * time.Second) |
| + r.tick(t) |
| + } |
| + for i := 0; i < 2; i++ { |
| + r.clk.advance(sidecarRestartMax) |
| + r.tick(t) |
| + } |
| + if got := r.a.RootfsWorkers(r.host); len(got) != 1 || got[0].Restarts != 2 { |
| + t.Fatalf("proof retries counted as crashes: report %+v", got) |
| + } |
| + if got := r.strays(); len(got) != 1 { |
| + t.Fatalf("strays %v, want the one failed start", got) |
| + } |
| + // Proven: the next start comes after the backoff the failure set |
| + // (4 s), not the first exit's. |
| + r.b.setGone(nil) |
| + r.clk.advance(sidecarRestartMax) |
| + r.tick(t) // the stray's proof |
| + if got := restartAfter(t, r); got > 4*time.Second { |
| + t.Errorf("restart after the proof took %s", got) |
| + } |
| + }) |
| +} |
| + |
| // The rootfs launch fixture's worker seams: the real backend's, over the |
| // rig's own row, as its other seams are. |
| func (b *rootfsLaunch) LaunchRootfsWorker(ctx context.Context, _ registry.Route, name string, g Gen, web Proc) (Proc, error) { |