| index b2858574d..c707d755d 100644 |
| --- a/internal/activator/haleaseloss_test.go |
| +++ b/internal/activator/haleaseloss_test.go |
| @@ -45,9 +45,17 @@ func lostTestEntry(t *testing.T) (*haSvc, *actEntry, *lossTestRep) { |
| t.Helper() |
| s, e := haServiceEntry(t, "shop.example.com") |
| s.rt.HA = true |
| + if err := s.b.Registry.Upsert(s.rt); err != nil { |
| + t.Fatal(err) |
| + } |
| + fresh, err := s.b.Registry.Get(s.rt.Host) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + s.rt = *fresh |
| r := &lossTestRep{Rep: e.rep, haRep: e.rep.(haRep), entered: make(chan struct{}), unblock: make(chan struct{})} |
| s.a.mu.Lock() |
| - e.route.HA = true |
| + e.route = s.rt |
| e.rep = r |
| s.a.mu.Unlock() |
| r.lost.Store(true) |
| @@ -249,7 +257,6 @@ func TestHALostLeaseRecoversOnlyAfterFreshAcquireTemporaryS3Failure(t *testing.T |
| s.a.mu.Lock() |
| s.a.latchHALossLocked(e) |
| s.a.mu.Unlock() |
| - s.a.finishStop(e, &stopObservation{}) |
| s.a.mu.Lock() |
| current := s.a.entries[s.rt.Host] |
| refusal := s.a.haLeaseRefusalLocked(s.rt) |
| @@ -280,6 +287,8 @@ func TestHALostLeaseRecoversOnlyAfterFreshAcquireForeignLease(t *testing.T) { |
| s, e, r := lostTestEntry(t) |
| finishLostTestEntry(t, s, e, r) |
| putLockFor(t, s.b, s.rt, leaseBody(t, heldByB, time.Now().Add(-time.Minute))) |
| + clk := &fakeClock{t: time.Now()} |
| + s.a.now = clk.now |
| before := len(lockWriteLogOf(s)) |
| s.a.retryHALostHost(s.rt) |
| s.a.haLoss.joined.Wait() |
| @@ -287,9 +296,15 @@ func TestHALostLeaseRecoversOnlyAfterFreshAcquireForeignLease(t *testing.T) { |
| if len(lockWriteLogOf(s)) != before { |
| t.Fatal("foreign expiry authorized an acquire") |
| } |
| + reads := len(s.fx.Requests()) |
| + clk.advance(11 * time.Second) |
| + s.a.retryHALostHosts() |
| + s.a.haLoss.joined.Wait() |
| + if len(s.fx.Requests()) != reads { |
| + t.Error("expired foreign holder polled without a new request") |
| + } |
| // A live foreign holder permits forwarding, without opening local service. |
| - clk := &fakeClock{t: time.Now().Add(11 * time.Second)} |
| - s.a.now = clk.now |
| + clk.advance(11 * time.Second) |
| putLockFor(t, s.b, s.rt, leaseBody(t, heldByB, time.Now().Add(time.Minute))) |
| s.a.retryHALostHost(s.rt) |
| s.a.haLoss.joined.Wait() |
| @@ -298,6 +313,13 @@ func TestHALostLeaseRecoversOnlyAfterFreshAcquireForeignLease(t *testing.T) { |
| t.Fatalf("foreign forward=%+v, %v", d, ok) |
| } |
| assertLostResponse(t, s) |
| + clk.advance(11 * time.Second) |
| + putLockFor(t, s.b, s.rt, leaseBody(t, heldByB, time.Now().Add(-time.Minute))) |
| + s.a.retryHALostHost(s.rt) |
| + s.a.haLoss.joined.Wait() |
| + if d, ok := s.a.ForwardTargetFromMemory(s.rt, nil); !ok || !d.Unavailable { |
| + t.Fatalf("expired foreign holder reused a stale live hint: %+v", d) |
| + } |
| } |
| |
| func TestHALostLeaseRecoversOnlyAfterFreshAcquirePoolWithdrawal(t *testing.T) { |
| @@ -441,8 +463,11 @@ func (lostDiscoveryBackend) readHolderView(ctx context.Context, _ registry.Route |
| func TestHALostLeaseRecoveryWorkersBounded(t *testing.T) { |
| a := New(Config{}, lostDiscoveryBackend{}, nil) |
| a.haLoss.hosts = make(map[string]*haLeaseLoss) |
| + a.Routes = withdrawalLookup(func(host string) (*registry.Route, error) { |
| + return ®istry.Route{Host: host, HA: true, Kind: "instance"}, nil |
| + }) |
| for i := 0; i < haLossWorkers+1; i++ { |
| - rt := registry.Route{Host: fmt.Sprintf("host%d.example.com", i), HA: true} |
| + rt := registry.Route{Host: fmt.Sprintf("host%d.example.com", i), HA: true, Kind: "instance"} |
| a.mu.Lock() |
| a.haLoss.hosts[rt.Host] = &haLeaseLoss{route: rt} |
| a.mu.Unlock() |
| @@ -469,3 +494,230 @@ func TestHALostLeaseDuringOrdinaryStop(t *testing.T) { |
| r.lost.Store(true) |
| assertLostResponse(t, s) |
| } |
| + |
| +func TestHALostRecoveryShutdownDoesNotWaitForWake(t *testing.T) { |
| + s, e, r := lostTestEntry(t) |
| + finishLostTestEntry(t, s, e, r) |
| + entered, unblock := make(chan struct{}), make(chan struct{}) |
| + old := acquireHALease |
| + acquireHALease = func(ctx context.Context, bucket, prefix, region string, base hafresh.Marker, p hafresh.Proofs) (*lease.Held, error) { |
| + close(entered) |
| + <-unblock |
| + return nil, context.Canceled |
| + } |
| + defer func() { close(unblock); s.a.haLoss.joined.Wait(); acquireHALease = old }() |
| + other := &actEntry{host: "other.example.com", route: registry.Route{Host: "other.example.com", Kind: "instance"}, state: actAwake, proc: &fakeProc{}, rep: &fakeRep{}} |
| + s.a.mu.Lock() |
| + s.a.putEntryLocked(other.host, other) |
| + s.a.mu.Unlock() |
| + s.a.retryHALostHost(s.rt) |
| + <-entered |
| + ctx, cancel := context.WithTimeout(context.Background(), 150*time.Millisecond) |
| + defer cancel() |
| + done := make(chan struct{}) |
| + go func() { s.a.Shutdown(ctx); close(done) }() |
| + select { |
| + case <-done: |
| + case <-time.After(time.Second): |
| + t.Error("Shutdown waited for recovery wake beyond its context") |
| + } |
| + s.a.mu.Lock() |
| + stopped := other.stopped |
| + s.a.mu.Unlock() |
| + select { |
| + case <-stopped: |
| + case <-time.After(time.Second): |
| + t.Error("blocked recovery delayed the other host's stop") |
| + } |
| +} |
| + |
| +func TestHALostRecoveryNoDemandNoS3(t *testing.T) { |
| + s, e, r := lostTestEntry(t) |
| + finishLostTestEntry(t, s, e, r) |
| + before := len(s.fx.Requests()) |
| + s.a.retryHALostHosts() |
| + s.a.haLoss.joined.Wait() |
| + if got := len(s.fx.Requests()); got != before { |
| + t.Fatalf("no requests: S3 calls=%d", got-before) |
| + } |
| + putLockFor(t, s.b, s.rt, leaseBody(t, heldByB, time.Now().Add(time.Hour))) |
| + clk := &fakeClock{t: time.Now()} |
| + s.a.now = clk.now |
| + s.a.retryHALostHost(s.rt) |
| + s.a.haLoss.joined.Wait() |
| + before = len(s.fx.Requests()) |
| + for range 12 { |
| + clk.advance(10 * time.Second) |
| + s.a.retryHALostHosts() |
| + s.a.haLoss.joined.Wait() |
| + } |
| + if got := len(s.fx.Requests()); got != before { |
| + t.Fatalf("foreign holder without new demand: S3 calls=%d", got-before) |
| + } |
| + s.a.retryHALostHost(s.rt) |
| + s.a.haLoss.joined.Wait() |
| + if len(s.fx.Requests()) <= before { |
| + t.Fatal("new request did not retry discovery") |
| + } |
| +} |
| + |
| +func TestHALostRecoveryParksChangedRoutes(t *testing.T) { |
| + for _, change := range []string{"deleted", "disabled", "moved", "lifetime", "pool", "lookup error"} { |
| + t.Run(change, func(t *testing.T) { |
| + s, e, r := lostTestEntry(t) |
| + finishLostTestEntry(t, s, e, r) |
| + current := s.rt |
| + s.a.Routes = withdrawalLookup(func(string) (*registry.Route, error) { |
| + switch change { |
| + case "deleted": |
| + return nil, nil |
| + case "lookup error": |
| + return nil, errors.New("registry unavailable") |
| + } |
| + return ¤t, nil |
| + }) |
| + switch change { |
| + case "disabled": |
| + s.a.SetDisabledFor(func(registry.Route) bool { return true }) |
| + case "moved": |
| + current.Moved = true |
| + case "lifetime": |
| + current.Incarnation++ |
| + case "pool": |
| + current.Pool = "elsewhere" |
| + } |
| + before := len(s.fx.Requests()) |
| + s.a.retryHALostHost(s.rt) |
| + s.a.haLoss.joined.Wait() |
| + if got := len(s.fx.Requests()); got != before { |
| + t.Errorf("changed route performed %d S3 calls", got-before) |
| + } |
| + assertLossLatched(t, s) |
| + }) |
| + } |
| +} |
| + |
| +func TestHALostQueuedStopWaiterRefusesOnLoss(t *testing.T) { |
| + s, e, r := lostTestEntry(t) |
| + r.lost.Store(false) |
| + s.a.mu.Lock() |
| + s.a.beginStopLocked(e, StopIdle) |
| + s.a.mu.Unlock() |
| + <-r.entered |
| + ctx, cancel := context.WithCancel(context.Background()) |
| + defer cancel() |
| + w := httptest.NewRecorder() |
| + done := make(chan struct{}) |
| + go func() { |
| + s.a.ServeHTTP(w, httptest.NewRequest("GET", "http://shop.example.com/", nil).WithContext(ctx), s.rt, http.HandlerFunc(func(http.ResponseWriter, *http.Request) { t.Error("proxied") })) |
| + close(done) |
| + }() |
| + select { |
| + case <-done: |
| + t.Fatal("ordinary stop did not wait") |
| + case <-time.After(30 * time.Millisecond): |
| + } |
| + r.lost.Store(true) |
| + assertLostResponse(t, s) |
| + select { |
| + case <-done: |
| + if w.Code != 503 || w.Header().Get("Retry-After") != "10" || w.Header().Get("X-Carlos-Holder") != "none" || w.Body.String() != edge.HAUnavailableMessage+"\n" { |
| + t.Errorf("queued loss response: %d %v %q", w.Code, w.Header(), w.Body.String()) |
| + } |
| + case <-time.After(time.Second): |
| + cancel() |
| + <-done |
| + t.Fatal("queued waiter did not wake at loss latch") |
| + } |
| +} |
| + |
| +type lostCountingBackend struct { |
| + *fakeBackend |
| + reads atomic.Int32 |
| +} |
| + |
| +func (b *lostCountingBackend) self() hafresh.Self { return hafresh.Self{Ref: "a", IID: "i-a"} } |
| +func (b *lostCountingBackend) readHolderView(context.Context, registry.Route) (hafresh.View, error) { |
| + b.reads.Add(1) |
| + return hafresh.View{}, errors.New("S3 unavailable") |
| +} |
| + |
| +func TestHALostRecoveryMassLossAndDemandExpiry(t *testing.T) { |
| + b := &lostCountingBackend{fakeBackend: &fakeBackend{}} |
| + a := New(Config{}, b, nil) |
| + defer a.lifeStop() |
| + clk := &fakeClock{t: time.Now()} |
| + a.now = clk.now |
| + a.Routes = withdrawalLookup(func(host string) (*registry.Route, error) { |
| + return ®istry.Route{Host: host, Kind: "instance", HA: true}, nil |
| + }) |
| + a.haLoss.hosts = make(map[string]*haLeaseLoss) |
| + for i := range 100 { |
| + rt := registry.Route{Host: fmt.Sprintf("mass%d.example.com", i), Kind: "instance", HA: true} |
| + a.haLoss.hosts[rt.Host] = &haLeaseLoss{route: rt} |
| + } |
| + for range 3 { |
| + a.retryHALostHosts() |
| + a.haLoss.joined.Wait() |
| + clk.advance(time.Minute) |
| + } |
| + if b.reads.Load() != 0 { |
| + t.Fatalf("mass loss without traffic performed %d reads", b.reads.Load()) |
| + } |
| + rt := a.haLoss.hosts["mass0.example.com"].route |
| + a.retryHALostHost(rt) |
| + a.haLoss.joined.Wait() |
| + if b.reads.Load() != 1 { |
| + t.Fatal("request did not start discovery") |
| + } |
| + clk.advance(61 * time.Second) |
| + for range 3 { |
| + a.retryHALostHosts() |
| + a.haLoss.joined.Wait() |
| + clk.advance(time.Minute) |
| + } |
| + if b.reads.Load() != 1 { |
| + t.Fatalf("expired demand kept polling: %d reads", b.reads.Load()) |
| + } |
| + if b.acquires != 0 || b.starts != 0 { |
| + t.Fatal("mass loss woke an app") |
| + } |
| +} |
| + |
| +func TestHALostRecoveryFailedPostCheckStopsEntry(t *testing.T) { |
| + s, e, r := lostTestEntry(t) |
| + finishLostTestEntry(t, s, e, r) |
| + var withdrawn atomic.Bool |
| + original := s.b.ServedPools |
| + s.b.ServedPools = func() edgesfile.PoolEvidence { |
| + if withdrawn.Load() { |
| + return edgesfile.PoolEvidence(`["another-pool"]`) |
| + } |
| + return original() |
| + } |
| + var recovered *actEntry |
| + s.a.wakeEnded = func(host string) { |
| + s.a.mu.Lock() |
| + recovered = s.a.entries[host] |
| + s.a.mu.Unlock() |
| + withdrawn.Store(true) |
| + } |
| + s.a.retryHALostHost(s.rt) |
| + s.a.haLoss.joined.Wait() |
| + if recovered == nil { |
| + t.Fatal("recovery did not wake") |
| + } |
| + s.a.mu.Lock() |
| + stopped := recovered.stopped |
| + why := recovered.stopWhy |
| + s.a.mu.Unlock() |
| + if stopped == nil || why != StopLeaseLost { |
| + t.Fatal("failed post-check left a refused entry awake") |
| + } |
| + select { |
| + case <-stopped: |
| + case <-time.After(5 * time.Second): |
| + t.Fatal("failed recovery did not stop") |
| + } |
| + assertLossLatched(t, s) |
| +} |