| index 04458e209..a8c6d097a 100644 |
| --- a/internal/activator/pendingwake_linux_test.go |
| +++ b/internal/activator/pendingwake_linux_test.go |
| @@ -19,6 +19,7 @@ import ( |
| "github.com/carlosframework/platform/internal/provision" |
| "github.com/carlosframework/platform/internal/registry" |
| "github.com/carlosframework/platform/internal/release" |
| + "github.com/carlosframework/platform/internal/replicas" |
| ) |
| |
| // A pending wake and the start rule (managed fleets 1b, branch review R1 |
| @@ -574,8 +575,8 @@ func TestLaggingReconcileDoesNotBootAStaleCopy(t *testing.T) { |
| } |
| |
| // The request budget: a wake of a current labelled route reads its record |
| -// exactly once; an unlabelled route, or a box that is not a fleet box, |
| -// reads none. |
| +// exactly once, as does an unlabelled fleet-placed route (the R1 fence); a |
| +// box that is not a fleet box reads none. |
| func TestLabelledWakeReadsTheRecordOnce(t *testing.T) { |
| for _, tc := range []struct { |
| name string |
| @@ -584,7 +585,7 @@ func TestLabelledWakeReadsTheRecordOnce(t *testing.T) { |
| wantReads int64 |
| }{ |
| {"labelled", assignA, true, 1}, |
| - {"unlabelled", "", true, 0}, |
| + {"unlabelled", "", true, 1}, |
| {"not a fleet box", assignA, false, 0}, |
| } { |
| t.Run(tc.name, func(t *testing.T) { |
| @@ -675,16 +676,16 @@ func TestUnreadableRecordRefusesTheWake(t *testing.T) { |
| // record, so it refuses every labelled wake under the lease, naming the |
| // identity error, and gives the lease back. Here A kept its labelled row |
| // and a copy whose marker matches it, and the record has moved to B: A |
| -// must boot nothing. An unlabelled wake on the same box is unaffected. |
| +// must boot nothing. An unlabelled wake on the same box is fenced by the |
| +// record instead (R1): it reads it once and refuses, since it names B. |
| func TestInvalidFleetIdentityRefusesLabelledWakes(t *testing.T) { |
| identityErr := errors.New(`fleet box label "Bad_Box" is invalid`) |
| for _, tc := range []struct { |
| name string |
| assigned string |
| - refused bool |
| }{ |
| - {"labelled", assignA, true}, |
| - {"unlabelled", "", false}, |
| + {"labelled", assignA}, |
| + {"unlabelled", ""}, |
| } { |
| t.Run(tc.name, func(t *testing.T) { |
| lock := &sharedLock{} |
| @@ -702,7 +703,7 @@ func TestInvalidFleetIdentityRefusesLabelledWakes(t *testing.T) { |
| writeAssignedMarker(t, rt.DBPath, tc.assigned) |
| } |
| a, fb, _ := fleetBox(t, "pi-1", lock, bucket, eb, rt) |
| - a.Fleet = FleetWakeIdentityUnknown("pi", identityErr) |
| + a.Fleet = FleetWakeIdentityUnknown("pi", identityErr, FleetRecordFrom(store)) |
| release, err := a.beginRequest(context.Background(), rt) |
| if err == nil { |
| release() |
| @@ -710,9 +711,15 @@ func TestInvalidFleetIdentityRefusesLabelledWakes(t *testing.T) { |
| fb.fakeBackend.mu.Lock() |
| starts, acquires := fb.starts, fb.acquires |
| fb.fakeBackend.mu.Unlock() |
| - if !tc.refused { |
| - if err != nil || starts != 1 { |
| - t.Fatalf("an unlabelled wake on a box with an invalid identity: err %v, %d starts; want it to start", err, starts) |
| + if tc.assigned == "" { |
| + if !errors.Is(err, errRouteChangedUnderLease) || starts != 0 { |
| + t.Fatalf("an unlabelled wake on a box with an invalid identity: err %v, %d starts; want the R1 fence", err, starts) |
| + } |
| + if n := store.gets.Load(); n != 1 { |
| + t.Fatalf("the fence read the record %d times, want 1", n) |
| + } |
| + if h := lock.heldBy(); h != "" { |
| + t.Fatalf("the lease is still held by %s", h) |
| } |
| return |
| } |
| @@ -746,3 +753,384 @@ func TestFleetIdentityRefusalReadsAsTheFence(t *testing.T) { |
| t.Fatalf("member reason %q, want the fence's %q", got, want) |
| } |
| } |
| + |
| +// R1, the stale unlabelled wake fence (fleet record labels spec §3). A |
| +// fleet box whose row has not converged still holds it unlabelled, while |
| +// the record already names a box (or carries copies). Its local copy is |
| +// then not the newest data: under the lease, before any restore or start, |
| +// the wake reads the record once and refuses, giving the lease back. |
| + |
| +// unlabelledRig is box A on an unlabelled fleet-placed row. Its local copy |
| +// holds one row; the bucket holds two, written by another box since. |
| +type unlabelledRig struct { |
| + lock *sharedLock |
| + bucket *fleetBucket |
| + restores *atomic.Int64 |
| + eb *ExecBackend |
| + rt registry.Route |
| + a *Service |
| + box *leasedBox |
| + reg *registry.Registry |
| +} |
| + |
| +func newUnlabelledRig(t *testing.T) *unlabelledRig { |
| + t.Helper() |
| + r := &unlabelledRig{lock: &sharedLock{}, bucket: &fleetBucket{path: filepath.Join(t.TempDir(), "replica.db")}, restores: &atomic.Int64{}} |
| + swapRestoreReplica(t, func(_ context.Context, dbPath, _, _, _ string) (bool, error) { |
| + r.restores.Add(1) |
| + return r.bucket.restoreTo(t, dbPath), nil |
| + }) |
| + newer := filepath.Join(t.TempDir(), "newer.db") |
| + writeUsableDB(t, newer) |
| + appendRow(t, newer) |
| + r.bucket.upload(t, newer) |
| + r.eb = testExecBackend(t) |
| + r.rt = recordRoute(r.eb, "", false) |
| + writeUsableDB(t, r.rt.DBPath) |
| + r.a, r.box, r.reg = fleetBox(t, "pi-1", r.lock, r.bucket, r.eb, r.rt) |
| + return r |
| +} |
| + |
| +func (r *unlabelledRig) wake(a *Service) error { |
| + release, err := a.beginRequest(context.Background(), r.rt) |
| + if err == nil { |
| + release() |
| + } |
| + return err |
| +} |
| + |
| +func (r *unlabelledRig) starts() int { |
| + r.box.fakeBackend.mu.Lock() |
| + defer r.box.fakeBackend.mu.Unlock() |
| + return r.box.starts |
| +} |
| + |
| +// assertRefused checks a refused wake: the error wraps want, the lease is |
| +// free, nothing was restored or started, and A's copy is as it was. |
| +func (r *unlabelledRig) assertRefused(t *testing.T, err, want error) { |
| + t.Helper() |
| + if !errors.Is(err, want) { |
| + t.Fatalf("wake err = %v, want %v", err, want) |
| + } |
| + if h := r.lock.heldBy(); h != "" { |
| + t.Fatalf("the refused wake left the lease held by %s", h) |
| + } |
| + if n := r.restores.Load(); n != 0 { |
| + t.Fatalf("the refused wake restored %d times", n) |
| + } |
| + if n := r.starts(); n != 0 { |
| + t.Fatalf("the refused wake started %d processes", n) |
| + } |
| + if n := countRows(t, r.rt.DBPath); n != 1 { |
| + t.Fatalf("A's copy has %d rows; the refusal must leave it at 1", n) |
| + } |
| +} |
| + |
| +// unlabelledStore is the deployment store with the host's record |
| +// unlabelled, its read count reset after any setup reads. |
| +func unlabelledStore(t *testing.T, labelTo string) *countingStore { |
| + t.Helper() |
| + store := deployStore(t, "", "") |
| + if labelTo != "" { |
| + moveRecord(t, store, "", labelTo, assignB) |
| + } |
| + store.gets.Store(0) |
| + return store |
| +} |
| + |
| +// staticRecord is a record reader answering rec and err, counting reads. |
| +func staticRecord(reads *atomic.Int64, rec *provision.Record, err error) func(context.Context, registry.Route) (*provision.Record, error) { |
| + return func(context.Context, registry.Route) (*provision.Record, error) { |
| + reads.Add(1) |
| + return rec, err |
| + } |
| +} |
| + |
| +// unlabelledRecord is the record behind recordRoute, unlabelled. |
| +func unlabelledRecord() provision.Record { |
| + created, _ := time.Parse(time.RFC3339, recCreated) |
| + return provision.Record{Schema: 1, Host: ruleHost, Slug: "shop", App: recApp, Account: recAcct, |
| + Region: recRegion, Channel: "edge", CreatedAt: created} |
| +} |
| + |
| +func TestUnlabelledStaleWakeRefusedWhenRecordNamesAnotherBox(t *testing.T) { |
| + r := newUnlabelledRig(t) |
| + store := unlabelledStore(t, "pi-2") |
| + asFleetBox(r.a, "pi-1", store) |
| + err := r.wake(r.a) |
| + r.assertRefused(t, err, errRouteChangedUnderLease) |
| + if !errors.Is(err, errUnlabelledRecordLabelled) { |
| + t.Fatalf("err = %v, want errUnlabelledRecordLabelled", err) |
| + } |
| + if n := store.gets.Load(); n != 1 { |
| + t.Fatalf("the wake read the record %d times, want 1", n) |
| + } |
| +} |
| + |
| +// Even a label naming this box refuses: the row has not converged, and |
| +// must wake on the labelled path once it has. |
| +func TestUnlabelledWakeRefusedWhenRecordLabelledToThisBox(t *testing.T) { |
| + r := newUnlabelledRig(t) |
| + asFleetBox(r.a, "pi-1", unlabelledStore(t, "pi-1")) |
| + r.assertRefused(t, r.wake(r.a), errRouteChangedUnderLease) |
| +} |
| + |
| +func TestUnlabelledWakeRefusedWhenRecordHasAssignment(t *testing.T) { |
| + r := newUnlabelledRig(t) |
| + rec := unlabelledRecord() |
| + rec.ReplicaAssignment = &replicas.Assignment{Desired: 1, Revision: 1, Boxes: []string{"pi-1"}} |
| + var reads atomic.Int64 |
| + r.a.Fleet = FleetWake{Mode: FleetWakeCheck, Fleet: "pi", Label: "pi-1", Record: staticRecord(&reads, &rec, nil)} |
| + r.assertRefused(t, r.wake(r.a), errRouteChangedUnderLease) |
| + if n := reads.Load(); n != 1 { |
| + t.Fatalf("the wake read the record %d times, want 1", n) |
| + } |
| +} |
| + |
| +func TestUnlabelledWakeRefusedWhenRecordUnreadable(t *testing.T) { |
| + retired := unlabelledRecord() |
| + retired.Retired = true |
| + for _, tc := range []struct { |
| + name string |
| + rec *provision.Record |
| + err error |
| + }{ |
| + {"read error", nil, errors.New("bucket unavailable")}, |
| + {"no record", nil, nil}, |
| + {"retired", &retired, nil}, |
| + } { |
| + t.Run(tc.name, func(t *testing.T) { |
| + r := newUnlabelledRig(t) |
| + var reads atomic.Int64 |
| + r.a.Fleet = FleetWake{Mode: FleetWakeCheck, Fleet: "pi", Label: "pi-1", Record: staticRecord(&reads, tc.rec, tc.err)} |
| + r.assertRefused(t, r.wake(r.a), errRecordUnreadUnderLease) |
| + if n := reads.Load(); n != 1 { |
| + t.Fatalf("the wake read the record %d times, want 1", n) |
| + } |
| + }) |
| + } |
| + t.Run("no reader", func(t *testing.T) { |
| + r := newUnlabelledRig(t) |
| + r.a.Fleet = FleetWake{Mode: FleetWakeCheck, Fleet: "pi", Label: "pi-1"} |
| + r.assertRefused(t, r.wake(r.a), errRecordUnreadUnderLease) |
| + }) |
| +} |
| + |
| +// The label lands while A's wake waits for the lease: the record read |
| +// under the lease sees it. |
| +func TestUnlabelledWakeFencedWhenLabelledWhileWaitingForTheLease(t *testing.T) { |
| + r := newUnlabelledRig(t) |
| + store := unlabelledStore(t, "") |
| + asFleetBox(r.a, "pi-1", store) |
| + r.box.gate, r.box.entered = make(chan struct{}), make(chan struct{}) |
| + woke := make(chan error, 1) |
| + go func() { woke <- r.wake(r.a) }() |
| + select { |
| + case <-r.box.entered: |
| + case <-time.After(10 * time.Second): |
| + t.Fatal("A's wake never reached its lease") |
| + } |
| + moveRecord(t, store, "", "pi-2", assignB) |
| + close(r.box.gate) |
| + var err error |
| + select { |
| + case err = <-woke: |
| + case <-time.After(10 * time.Second): |
| + t.Fatal("A's wake never finished") |
| + } |
| + r.assertRefused(t, err, errRouteChangedUnderLease) |
| +} |
| + |
| +// An agent restart forgets everything in memory: a fresh Service over A's |
| +// registry, whose row still reads unlabelled (no reconcile has run since |
| +// the label), is fenced by the record alone. |
| +func TestFenceHoldsAfterAgentRestartBeforeReconcile(t *testing.T) { |
| + r := newUnlabelledRig(t) |
| + store := unlabelledStore(t, "") |
| + asFleetBox(r.a, "pi-1", store) |
| + if err := r.wake(r.a); err != nil { |
| + t.Fatalf("A's first wake: %v", err) |
| + } |
| + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) |
| + defer cancel() |
| + r.a.Shutdown(ctx) |
| + if h := r.lock.heldBy(); h != "" { |
| + t.Fatalf("A's shutdown left the lease held by %s", h) |
| + } |
| + // Another box writes on, and the record is labelled to it. |
| + newer := filepath.Join(t.TempDir(), "newer.db") |
| + writeUsableDB(t, newer) |
| + appendRow(t, newer) |
| + r.bucket.upload(t, newer) |
| + moveRecord(t, store, "", "pi-2", assignB) |
| + |
| + restarted := New(Config{Bucket: "test-bucket", Idle: time.Hour}, r.box, r.reg) |
| + restarted.Routes = r.reg |
| + asFleetBox(restarted, "pi-1", store) |
| + t.Cleanup(func() { restarted.Shutdown(context.Background()) }) |
| + if row, err := r.reg.Get(r.rt.Host); err != nil || row == nil || row.Assigned != "" { |
| + t.Fatalf("A's row = %+v, %v; the test needs it still unlabelled", row, err) |
| + } |
| + r.box.fakeBackend.mu.Lock() |
| + r.box.starts = 0 |
| + r.box.fakeBackend.mu.Unlock() |
| + r.restores.Store(0) |
| + r.assertRefused(t, r.wake(restarted), errRouteChangedUnderLease) |
| +} |
| + |
| +// The request budget on the pass-through path: an unlabelled record wakes |
| +// as today, after one read. |
| +func TestUnlabelledWakeOnUnlabelledRecordWakesAsToday(t *testing.T) { |
| + r := newUnlabelledRig(t) |
| + store := unlabelledStore(t, "") |
| + asFleetBox(r.a, "pi-1", store) |
| + if err := r.wake(r.a); err != nil { |
| + t.Fatalf("wake: %v", err) |
| + } |
| + if n := r.starts(); n != 1 { |
| + t.Fatalf("starts = %d, want 1", n) |
| + } |
| + if n := store.gets.Load(); n != 1 { |
| + t.Fatalf("the wake read the record %d times, want 1", n) |
| + } |
| + if n := countRows(t, r.rt.DBPath); n != 1 { |
| + t.Fatalf("the warm copy has %d rows; today's wake keeps it", n) |
| + } |
| +} |
| + |
| +// A box not on a fleet, or a row not placed on one, reads nothing. The |
| +// record names another box, so a read would have refused. |
| +func TestUnlabelledWakeOffFleetReadsNothing(t *testing.T) { |
| + for _, tc := range []struct { |
| + name string |
| + mode FleetWakeMode |
| + region string |
| + }{ |
| + {"box not on a fleet", FleetWakeNone, recRegion}, |
| + {"row not on a fleet", FleetWakeCheck, ""}, |
| + } { |
| + t.Run(tc.name, func(t *testing.T) { |
| + r := newUnlabelledRig(t) |
| + if tc.region != r.rt.Region { |
| + r.rt.Region = tc.region |
| + if err := r.reg.Upsert(r.rt); err != nil { |
| + t.Fatal(err) |
| + } |
| + } |
| + store := unlabelledStore(t, "pi-2") |
| + r.a.Fleet = FleetWake{Mode: tc.mode, Fleet: "pi", Label: "pi-1", Record: FleetRecordFrom(store)} |
| + if err := r.wake(r.a); err != nil { |
| + t.Fatalf("wake: %v", err) |
| + } |
| + if n := r.starts(); n != 1 { |
| + t.Fatalf("starts = %d, want 1", n) |
| + } |
| + if n := store.gets.Load(); n != 0 { |
| + t.Fatalf("the wake read the record %d times, want 0", n) |
| + } |
| + }) |
| + } |
| +} |
| + |
| +// A box on a fleet whose identity is invalid fails closed: its unlabelled |
| +// wakes are fenced by the record, as a checking box's are. |
| +func TestUnlabelledWakeOnIdentityUnknownBoxIsFenced(t *testing.T) { |
| + r := newUnlabelledRig(t) |
| + store := unlabelledStore(t, "pi-2") |
| + r.a.Fleet = FleetWakeIdentityUnknown("pi", errors.New(`fleet box label "Bad_Box" is invalid`), FleetRecordFrom(store)) |
| + r.assertRefused(t, r.wake(r.a), errRouteChangedUnderLease) |
| + if n := store.gets.Load(); n != 1 { |
| + t.Fatalf("the wake read the record %d times, want 1", n) |
| + } |
| +} |
| + |
| +// The stateless final exec gate (statelessFinalRecord) goes through the |
| +// same fence: an unlabelled stateless row on a fleet whose record names a |
| +// box starts nothing; an unlabelled record starts, after one read. |
| +func TestStatelessUnlabelledWakeFenced(t *testing.T) { |
| + for _, labelled := range []bool{true, false} { |
| + t.Run(fmt.Sprint(labelled), func(t *testing.T) { |
| + r := newWakeRelRig(t) |
| + store := wireStatelessRig(r) |
| + r.rt.CreatedAt = 1 |
| + r.rt.Region = "fleet/pi" |
| + r.routes.set(r.rt) |
| + var reads atomic.Int64 |
| + r.a.Fleet = FleetWake{Mode: FleetWakeCheck, Fleet: "pi", Label: "box-a", Record: func(context.Context, registry.Route) (*provision.Record, error) { |
| + reads.Add(1) |
| + rec := &provision.Record{Account: r.rt.Account, App: r.rt.App, Host: r.rt.Host, CreatedAt: time.Unix(1, 0), Runtime: r.rt.Runtime, Channel: r.rt.Channel, Environment: r.rt.Environment, Region: r.rt.Region, ReplicaDeploy: store.snapshot()} |
| + if labelled { |
| + rec.FleetBox, rec.FleetBoxAt = "box-b", assignB |
| + } |
| + return rec, nil |
| + }} |
| + done := r.wake(r.a) |
| + unit := r.nextUnit(t) |
| + unit.done("exited", "0", "success") |
| + err := waitErr(t, done) |
| + if n := reads.Load(); n != 1 { |
| + t.Fatalf("record reads = %d, want 1", n) |
| + } |
| + if !labelled { |
| + if err != nil || len(r.b.started()) != 1 { |
| + t.Fatalf("unlabelled record: err %v, starts %v; want it to start", err, r.b.started()) |
| + } |
| + return |
| + } |
| + if !errors.Is(err, errRouteChangedUnderLease) || len(r.b.started()) != 0 { |
| + t.Fatalf("labelled record: err %v, starts %v; want the fence", err, r.b.started()) |
| + } |
| + }) |
| + } |
| +} |
| + |
| +// A declared worker's start (runWorkerStart's gate) on an unlabelled |
| +// stateless fleet row reads the record through the same fence: labelled |
| +// since the wake, the worker is refused. |
| +func TestWorkerUnlabelledWakeFenced(t *testing.T) { |
| + for _, labelled := range []bool{true, false} { |
| + t.Run(fmt.Sprint(labelled), func(t *testing.T) { |
| + r := newWakeRelRig(t) |
| + store := wireStatelessRig(r) |
| + r.rt.CreatedAt = 1 |
| + r.rt.Region = "fleet/pi" |
| + r.rt.Stateless = true |
| + r.routes.set(r.rt) |
| + workers := &fakeWorkerBackend{killBackend: &killBackend{fakeBackend: r.b.fakeBackend}} |
| + r.a.backend = &statelessWakeBackend{r.b, workers} |
| + r.b.procs["v2"] = map[string]release.Process{release.ProcessWeb: {Command: []string{"serve"}}, "jobs": {Command: []string{"jobs"}}} |
| + var label atomic.Bool |
| + var reads atomic.Int32 |
| + r.a.Fleet = FleetWake{Mode: FleetWakeCheck, Fleet: "pi", Label: "a", Record: func(context.Context, registry.Route) (*provision.Record, error) { |
| + reads.Add(1) |
| + rec := &provision.Record{Account: r.rt.Account, App: r.rt.App, Host: r.rt.Host, CreatedAt: time.Unix(1, 0), Runtime: r.rt.Runtime, Channel: r.rt.Channel, Environment: r.rt.Environment, Region: r.rt.Region, ReplicaDeploy: store.snapshot()} |
| + if label.Load() { |
| + rec.FleetBox, rec.FleetBoxAt = "b", assignB |
| + } |
| + return rec, nil |
| + }} |
| + if err := waitErr(t, r.wake(r.a)); err != nil { |
| + t.Fatal(err) |
| + } |
| + reads.Store(0) |
| + label.Store(labelled) |
| + r.a.rootfsTick() |
| + if !labelled { |
| + waitFor(t, "unlabelled worker", func() bool { return workers.alive() == 1 }) |
| + } else { |
| + waitFor(t, "labelled worker refused", func() bool { |
| + r.a.mu.Lock() |
| + defer r.a.mu.Unlock() |
| + wk := r.a.entries[r.rt.Host].rootfsWorkers["jobs"] |
| + return reads.Load() > 0 && !wk.starting |
| + }) |
| + if workers.alive() != 0 || len(workers.stampsOf("start jobs gen1")) != 0 { |
| + t.Fatal("a worker started on a row whose record names another box") |
| + } |
| + } |
| + if reads.Load() == 0 { |
| + t.Fatal("the worker start did not read the record") |
| + } |
| + }) |
| + } |
| +} |