| index ae0bdb046..c97e43012 100644 |
| --- a/internal/activator/startrule_test.go |
| +++ b/internal/activator/startrule_test.go |
| @@ -163,14 +163,18 @@ func replicaCalls(t *testing.T, has bool) *int { |
| return &n |
| } |
| |
| -// ruleBackend runs a real ExecBackend's Restore and NoteAssignment under |
| -// the fake's start, lease and replication, so a test drives doWake's own |
| -// order: the rule in the restore, the marker after the start. |
| +// ruleBackend runs a real ExecBackend's Restore under the fake's start, |
| +// lease and replication, so a test drives doWake's own order: the rule |
| +// and the marker in the restore, then the start. |
| type ruleBackend struct { |
| *fakeBackend |
| - eb *ExecBackend |
| - noted int |
| - mu sync.Mutex |
| + eb *ExecBackend |
| + t *testing.T |
| + // startWrites makes the start create the app's database before it |
| + // answers, as an app that opens CARLOS_DB and then fails its health |
| + // probe does. |
| + startWrites bool |
| + mu sync.Mutex |
| } |
| |
| func (r *ruleBackend) Restore(ctx context.Context, rt registry.Route) (bool, error) { |
| @@ -178,22 +182,26 @@ func (r *ruleBackend) Restore(ctx context.Context, rt registry.Route) (bool, err |
| return r.eb.Restore(ctx, rt) |
| } |
| |
| -func (r *ruleBackend) NoteAssignment(ctx context.Context, rt registry.Route) error { |
| +func (r *ruleBackend) Start(ctx context.Context, rt registry.Route) (Proc, string, error) { |
| r.mu.Lock() |
| - r.noted++ |
| + writes := r.startWrites |
| r.mu.Unlock() |
| - return r.eb.NoteAssignment(ctx, rt) |
| + if writes { |
| + if _, err := os.Stat(rt.DBPath); errors.Is(err, os.ErrNotExist) { |
| + writeUsableDB(r.t, rt.DBPath) |
| + } |
| + } |
| + return r.fakeBackend.Start(ctx, rt) |
| } |
| |
| // wakeOn wakes rt once through a Service over eb, and returns the wake's |
| // error and the fake half (its start count). |
| -func wakeOn(t *testing.T, eb *ExecBackend, rt registry.Route, tweak func(*fakeBackend)) (*ruleBackend, error) { |
| +func wakeOn(t *testing.T, eb *ExecBackend, rt registry.Route, tweak func(*ruleBackend)) (*ruleBackend, error) { |
| t.Helper() |
| - fb := &fakeBackend{} |
| + rb := &ruleBackend{fakeBackend: &fakeBackend{}, eb: eb, t: t} |
| if tweak != nil { |
| - tweak(fb) |
| + tweak(rb) |
| } |
| - rb := &ruleBackend{fakeBackend: fb, eb: eb} |
| reg, err := registry.Open(filepath.Join(t.TempDir(), "registry.db")) |
| if err != nil { |
| t.Fatal(err) |
| @@ -328,8 +336,9 @@ func TestMissingMarkerSetsAside(t *testing.T) { |
| t.Fatalf("restores = %d; a copy with no marker is set aside and restored", *restores) |
| } |
| assertSetAside(t, b, rt.Host, want) |
| - if _, ok := markerAt(t, rt.DBPath); ok { |
| - t.Fatal("Restore wrote the marker; it is written only after the start") |
| + // The restored copy is established under the assignment at once. |
| + if got, ok := markerAt(t, rt.DBPath); !ok || got != assignA { |
| + t.Fatalf("marker = %q (present %v); want %q after the restore", got, ok, assignA) |
| } |
| } |
| |
| @@ -469,12 +478,16 @@ func TestNewInstanceBootsFresh(t *testing.T) { |
| if err != nil || !fresh { |
| t.Fatalf("fresh %v err %v; a never-moved record with nothing set aside boots fresh", fresh, err) |
| } |
| + // Permitted empty, so established before any spawn. |
| + if got, ok := markerAt(t, rt.DBPath); !ok || got != assignA { |
| + t.Fatalf("marker = %q (present %v); want %q once the empty start is permitted", got, ok, assignA) |
| + } |
| rb, err := wakeOn(t, b, rt, nil) |
| if err != nil || rb.starts != 1 { |
| t.Fatalf("wake: err %v starts %d", err, rb.starts) |
| } |
| if got, ok := markerAt(t, rt.DBPath); !ok || got != assignA { |
| - t.Fatalf("marker = %q (present %v); want %q after the start", got, ok, assignA) |
| + t.Fatalf("marker = %q (present %v); want %q after the wake", got, ok, assignA) |
| } |
| // The marker went through a temp file and a rename: nothing else is |
| // left beside it. |
| @@ -486,26 +499,72 @@ func TestNewInstanceBootsFresh(t *testing.T) { |
| } |
| } |
| |
| -// The production agent's backend is a SplitBackend: it forwards the marker |
| -// write to the exec half, and a unit-backed row writes nothing. |
| -func TestSplitBackendNotesTheAssignment(t *testing.T) { |
| - b := testExecBackend(t) |
| - rt := fleetRoute(b, assignA, false) |
| - sb := &SplitBackend{Exec: b, Unit: &UnitBackend{OptDir: t.TempDir()}} |
| - unit := rt |
| - unit.Backing = registry.BackingUnit |
| - if err := sb.NoteAssignment(context.Background(), unit); err != nil { |
| - t.Fatal(err) |
| - } |
| - if _, ok := markerAt(t, rt.DBPath); ok { |
| - t.Fatal("a unit-backed row wrote a marker") |
| - } |
| - if err := sb.NoteAssignment(context.Background(), rt); err != nil { |
| - t.Fatal(err) |
| - } |
| - if got, ok := markerAt(t, rt.DBPath); !ok || got != assignA { |
| - t.Fatalf("marker = %q (present %v); want %q", got, ok, assignA) |
| +// A never-moved instance's permitted empty start establishes its copy |
| +// before the spawn: when the spawn then fails (a broken first build that |
| +// created its database and missed its health probe), the next wake keeps |
| +// that copy instead of setting it aside and refusing for ever. The same |
| +// holds for a restored copy whose start fails. |
| +func TestFailedFirstStartKeepsItsCopy(t *testing.T) { |
| + for _, tc := range []struct { |
| + name string |
| + replica bool |
| + }{{"empty start", false}, {"restored copy", true}} { |
| + t.Run(tc.name, func(t *testing.T) { |
| + b := testExecBackend(t) |
| + rt := fleetRoute(b, assignA, false) |
| + restores := replicaCalls(t, tc.replica) |
| + _, err := wakeOn(t, b, rt, func(r *ruleBackend) { |
| + r.startWrites = true |
| + r.startErr = errors.New("injected: health probe timed out") |
| + }) |
| + if err == nil { |
| + t.Fatal("the wake succeeded with a failing start") |
| + } |
| + if got, ok := markerAt(t, rt.DBPath); !ok || got != assignA { |
| + t.Fatalf("marker = %q (present %v); want %q, written before the spawn", got, ok, assignA) |
| + } |
| + before, err := os.ReadFile(rt.DBPath) |
| + if err != nil { |
| + t.Fatalf("the first start's database: %v", err) |
| + } |
| + asked := *restores |
| + |
| + rb, err := wakeOn(t, b, rt, nil) |
| + if err != nil || rb.starts != 1 { |
| + t.Fatalf("second wake: err %v starts %d; want the kept copy started", err, rb.starts) |
| + } |
| + if *restores != asked { |
| + t.Fatalf("restores %d -> %d; the kept copy needs no restore", asked, *restores) |
| + } |
| + if names := supersededEntries(t, b, rt.Host); len(names) != 0 { |
| + t.Fatalf("set aside %q: the copy was established under this assignment", names) |
| + } |
| + if after, _ := os.ReadFile(rt.DBPath); string(after) != string(before) { |
| + t.Fatal("the kept copy changed") |
| + } |
| + }) |
| } |
| + // A labelled seed placement is established at once too: a wake that |
| + // dies after it resumes from the placed copy, as an unlabelled one does. |
| + t.Run("seed placement", func(t *testing.T) { |
| + noOwnReplica(t) |
| + b, rt := nestedBackend(t, &fakeControl{}, &fakeUnit{}) |
| + rt.Assigned = assignA |
| + rt, objs, _ := seededRoute(t, b, rt, seeds.StateReady) |
| + replicaCalls(t, false) |
| + if fresh, err := b.Restore(context.Background(), rt); err != nil || fresh { |
| + t.Fatalf("placement: fresh %v err %v", fresh, err) |
| + } |
| + if got, ok := markerAt(t, rt.DBPath); !ok || got != assignA { |
| + t.Fatalf("marker = %q (present %v); want %q after the placement", got, ok, assignA) |
| + } |
| + if fresh, err := b.Restore(context.Background(), rt); err != nil || fresh || objs.opens != 1 { |
| + t.Fatalf("resume: fresh %v err %v opens %d; want the placed copy kept", fresh, err, objs.opens) |
| + } |
| + if names := supersededEntries(t, b, rt.Host); len(names) != 0 { |
| + t.Fatalf("set aside %q", names) |
| + } |
| + }) |
| } |
| |
| func TestFailedRestoreLeavesNoMarker(t *testing.T) { |
| @@ -538,21 +597,6 @@ func TestFailedRestoreLeavesNoMarker(t *testing.T) { |
| t.Fatal("a corrupt restore left a marker") |
| } |
| }) |
| - t.Run("start fails", func(t *testing.T) { |
| - b := testExecBackend(t) |
| - rt := fleetRoute(b, assignA, false) |
| - replicaCalls(t, true) |
| - rb, err := wakeOn(t, b, rt, func(f *fakeBackend) { f.startErr = errors.New("injected: health probe timed out") }) |
| - if err == nil { |
| - t.Fatal("the wake succeeded with a failing start") |
| - } |
| - if rb.noted != 0 { |
| - t.Fatalf("NoteAssignment called %d times after a failed start", rb.noted) |
| - } |
| - if _, ok := markerAt(t, rt.DBPath); ok { |
| - t.Fatal("a failed start left a marker") |
| - } |
| - }) |
| } |
| |
| // walCopy builds a WAL-mode database whose last rows live only in its |
| @@ -703,6 +747,9 @@ func TestEveryFreshBootPathConsultsTheRule(t *testing.T) { |
| if !uses["startAllowed"] { |
| t.Errorf("%s (%s) reaches %v without consulting startAllowed", fd.Name.Name, fset.Position(fd.Pos()), hit) |
| } |
| + if !uses["establishAssignment"] && !uses["restorePlain"] { |
| + t.Errorf("%s (%s) reaches %v without establishing the assignment marker", fd.Name.Name, fset.Position(fd.Pos()), hit) |
| + } |
| if !uses["reconcileAssignment"] && !uses["restorePlain"] { |
| t.Errorf("%s (%s) reaches %v without reconcileAssignment", fd.Name.Name, fset.Position(fd.Pos()), hit) |
| } |