| index 06c0f11eb..8e5be15c3 100644 |
| --- a/internal/activator/rootfsrelease_test.go |
| +++ b/internal/activator/rootfsrelease_test.go |
| @@ -1957,6 +1957,124 @@ func TestShutdownDrainsReleaseOps(t *testing.T) { |
| return unstick, owed |
| } |
| |
| + // Astra R1 P1: the record stays the host's obligation until its |
| + // cleanup is done. A lease return that blocks after the proof keeps |
| + // Shutdown waiting and a fence refused; a failed one hands the |
| + // obligation to owedReleases with no gap. |
| + t.Run("the lease's return blocks after the proof", func(t *testing.T) { |
| + r := newConsumerRig(t, true) |
| + unstick, _ := orphan(t, r) |
| + gl := newGatedLease(nil) |
| + r.a.mu.Lock() |
| + r.a.releaseOps[r.rt.Host].owed = gl |
| + r.a.rememberStopLocked(r.rt.Host, &stopObservation{mainStop: true, mainExited: true, sidecarStop: true, sidecarExited: true, replicaClosed: true, leaseReleased: true}) |
| + r.a.mu.Unlock() |
| + unstick() |
| + r.clk.advance(actStopTimeout) |
| + done := shutdownIn(r.a, 10*time.Second) |
| + select { |
| + case <-gl.entered: |
| + case <-time.After(5 * time.Second): |
| + t.Fatal("the proven record's lease was never given back") |
| + } |
| + time.Sleep(300 * time.Millisecond) |
| + if returned(done) { |
| + t.Fatal("Shutdown returned while the proven record's lease return was in flight") |
| + } |
| + if r.record() == nil { |
| + t.Fatal("the record went before its lease was given back") |
| + } |
| + fctx, fcancel := context.WithTimeout(context.Background(), 2*time.Second) |
| + if _, err := r.a.FenceAndStop(fctx, r.rt.Host, 1); !errors.Is(err, ErrStopUnproven) { |
| + t.Errorf("a fence during the lease return = %v, want ErrStopUnproven", err) |
| + } |
| + fcancel() |
| + r.a.LiftFence(r.rt.Host, 1) |
| + close(gl.gate) |
| + select { |
| + case <-done: |
| + case <-time.After(5 * time.Second): |
| + t.Fatal("Shutdown did not return once the lease was given back") |
| + } |
| + r.a.mu.Lock() |
| + history := r.a.stopHistory[r.rt.Host] |
| + left := r.a.releaseOps[r.rt.Host] |
| + r.a.mu.Unlock() |
| + if left != nil || history == nil || !history.uncertain || !history.leaseReleased { |
| + t.Fatalf("record %v, history %+v; want the record gone over an uncertain history with the lease released", left, history) |
| + } |
| + }) |
| + |
| + t.Run("a failed lease return is handed to owedReleases", func(t *testing.T) { |
| + r := newConsumerRig(t, true) |
| + unstick, _ := orphan(t, r) |
| + gl := newGatedLease(errors.New("store: DeleteObject timed out")) |
| + gl.setRetryErr(errors.New("store: DeleteObject timed out")) |
| + r.a.mu.Lock() |
| + r.a.releaseOps[r.rt.Host].owed = gl |
| + r.a.rememberStopLocked(r.rt.Host, &stopObservation{mainStop: true, mainExited: true, sidecarStop: true, sidecarExited: true, replicaClosed: true, leaseReleased: true}) |
| + r.a.mu.Unlock() |
| + // Watch the obligation the whole way: the record, or the owed |
| + // release over an uncertain history. |
| + var gap atomic.Bool |
| + stop := make(chan struct{}) |
| + watched := make(chan struct{}) |
| + go func() { |
| + defer close(watched) |
| + for { |
| + r.a.mu.Lock() |
| + owed := false |
| + for o := range r.a.owedReleases { |
| + owed = owed || o.host == r.rt.Host |
| + } |
| + h := r.a.stopHistory[r.rt.Host] |
| + if r.a.releaseOps[r.rt.Host] == nil && (!owed || h == nil || !h.uncertain) { |
| + gap.Store(true) |
| + } |
| + r.a.mu.Unlock() |
| + select { |
| + case <-stop: |
| + return |
| + default: |
| + } |
| + } |
| + }() |
| + unstick() |
| + r.clk.advance(actStopTimeout) |
| + r.sweepPass() |
| + select { |
| + case <-gl.entered: |
| + case <-time.After(5 * time.Second): |
| + t.Fatal("the proven record's lease was never given back") |
| + } |
| + close(gl.gate) |
| + waitFor(t, "the record to go", func() bool { return r.record() == nil }) |
| + close(stop) |
| + <-watched |
| + if gap.Load() { |
| + t.Fatal("the obligation had a gap: no record and no owed release over an uncertain history") |
| + } |
| + r.a.mu.Lock() |
| + history := r.a.stopHistory[r.rt.Host] |
| + r.a.mu.Unlock() |
| + if history == nil || !history.uncertain || history.leaseReleased { |
| + t.Fatalf("history %+v; want uncertain with the lease not released", history) |
| + } |
| + // Shutdown now waits on the owed release, and returns once its |
| + // retry lands. |
| + done := shutdownIn(r.a, 10*time.Second) |
| + time.Sleep(300 * time.Millisecond) |
| + if returned(done) { |
| + t.Fatal("Shutdown returned with the lease release still owed") |
| + } |
| + gl.setRetryErr(nil) |
| + select { |
| + case <-done: |
| + case <-time.After(5 * time.Second): |
| + t.Fatal("Shutdown did not return once the retry landed") |
| + } |
| + }) |
| + |
| t.Run("an ownerless record with an owed lease", func(t *testing.T) { |
| r := newConsumerRig(t, true) |
| unstick, _ := orphan(t, r) |
| @@ -2096,3 +2214,47 @@ func TestStopSettlesARunningRelease(t *testing.T) { |
| }) |
| } |
| } |
| + |
| +// gatedLease is a lease whose Release blocks until gate closes (entered |
| +// closes as it is asked) and then answers err; RetryRelease answers the |
| +// retry error set at the time. |
| +type gatedLease struct { |
| + gate chan struct{} |
| + entered chan struct{} |
| + once sync.Once |
| + err error |
| + mu sync.Mutex |
| + retryErr error |
| +} |
| + |
| +func newGatedLease(err error) *gatedLease { |
| + return &gatedLease{gate: make(chan struct{}), entered: make(chan struct{}), err: err} |
| +} |
| + |
| +func (l *gatedLease) Replicate(context.Context, string, time.Duration) (Rep, error) { |
| + return nil, errors.New("not used") |
| +} |
| + |
| +func (l *gatedLease) Release(ctx context.Context) error { |
| + l.once.Do(func() { close(l.entered) }) |
| + select { |
| + case <-l.gate: |
| + return l.err |
| + case <-ctx.Done(): |
| + return ctx.Err() |
| + } |
| +} |
| + |
| +func (l *gatedLease) RetryRelease(context.Context) error { |
| + l.mu.Lock() |
| + defer l.mu.Unlock() |
| + return l.retryErr |
| +} |
| + |
| +func (l *gatedLease) setRetryErr(err error) { |
| + l.mu.Lock() |
| + l.retryErr = err |
| + l.mu.Unlock() |
| +} |
| + |
| +var _ releaseRetrier = (*gatedLease)(nil) |