| index 342fe9f1c..5da2c094a 100644 |
| --- a/internal/lease/fence_test.go |
| +++ b/internal/lease/fence_test.go |
| @@ -325,8 +325,8 @@ func TestWriteFenceTripsWithFailingUploads(t *testing.T) { |
| } |
| |
| // Plan P1-2: the event does not depend on the renewer. A renewal failed |
| -// first; the fence still fires, keeps that first error, and frees a |
| -// release blocked behind the hung upload. |
| +// first; the fence still fires, keeps that first error, and cancels the |
| +// hung upload by itself. A release after it is unclean or owed. |
| func TestWriteFenceFiresAfterAnEarlierLoss(t *testing.T) { |
| haBox(t) |
| fx := leasetest.NewLockFixture(t) |
| @@ -343,24 +343,6 @@ func TestWriteFenceFiresAfterAnEarlierLoss(t *testing.T) { |
| t.Fatalf("Err after the renewal failed = %v", first) |
| } |
| |
| - hooked := make(chan struct{}) |
| - var once sync.Once |
| - t.Cleanup(lease.SetFinalSyncHookForTest(func() { once.Do(func() { close(hooked) }) })) |
| - released := make(chan error, 1) |
| - go func() { |
| - rc, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| - defer cancel() |
| - released <- g.r.ReleaseMarker(rc, hafresh.Marker{V: 1}) |
| - }() |
| - waitClosed(t, hooked, 5*time.Second, "the final-sync hook") |
| - time.Sleep(200 * time.Millisecond) |
| - select { |
| - case err := <-released: |
| - t.Fatalf("the release did not wait for the hung upload: %v", err) |
| - default: |
| - } |
| - |
| - start := time.Now() |
| tripped := -1 |
| for k := 0; k <= 40 && tripped < 0; k++ { |
| if g.step(t, k) { |
| @@ -376,22 +358,30 @@ func TestWriteFenceFiresAfterAnEarlierLoss(t *testing.T) { |
| if err := g.r.Err(); !errors.Is(err, first) || errors.Is(err, lease.ErrReplicaStale) { |
| t.Fatalf("Err = %v, want the first error kept (%v)", err, first) |
| } |
| - select { |
| - case err := <-released: |
| - var oe *lease.ObligationError |
| - if err != nil && !errors.As(err, &oe) { |
| - t.Fatalf("release: %v", err) |
| - } |
| - if err == nil { |
| - if v := lockView(t, fx, "instances/f"); v.Lock.Release == nil || v.Lock.Release.Clean { |
| - t.Fatalf("marker %+v, want unclean", v.Lock.Release) |
| - } |
| - } |
| - case <-time.After(8*time.Second - time.Since(start)): |
| - t.Fatal("the release did not return within 8 s of the trip") |
| + // The event alone cancels the hung upload: no release is running. |
| + deadline := time.Now().Add(5 * time.Second) |
| + for c.cancelled.Load() == 0 && time.Now().Before(deadline) { |
| + time.Sleep(10 * time.Millisecond) |
| } |
| if c.cancelled.Load() == 0 { |
| - t.Fatal("the hung upload was not cancelled") |
| + t.Fatal("the event did not cancel the hung upload") |
| + } |
| + |
| + rc, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| + defer cancel() |
| + start := time.Now() |
| + err := g.r.ReleaseMarker(rc, hafresh.Marker{V: 1, Clean: true}) |
| + if d := time.Since(start); d > 6*time.Second { |
| + t.Fatalf("the release took %s on a 5 s budget", d) |
| + } |
| + var oe *lease.ObligationError |
| + if err != nil && !errors.As(err, &oe) { |
| + t.Fatalf("release: %v", err) |
| + } |
| + if err == nil { |
| + if v := lockView(t, fx, "instances/f"); v.Lock.Release == nil || v.Lock.Release.Clean { |
| + t.Fatalf("marker %+v, want unclean", v.Lock.Release) |
| + } |
| } |
| } |
| |
| @@ -532,10 +522,13 @@ func TestWriteFenceShortOutageNoTrip(t *testing.T) { |
| }) |
| } |
| |
| -// G6-1.3, G6-2.3: a clean release already blocked behind a hung upload |
| -// is freed by the event, finishes its own finalization through the gate, |
| -// and is unclean; its marker is owed when the PUT fails. |
| -func TestWriteFenceUnblocksAReleaseAlreadyWaiting(t *testing.T) { |
| +// G6-1.3, G6-2.3, Astra branch R1 P2: a clean release started behind a |
| +// hung monitor upload finishes within its own deadline with no event: |
| +// the finalization takes the replica (the gate cancels the hung upload), |
| +// makes its own sync, which fails while the store is down, and is |
| +// unclean; its marker is owed when the PUT fails. Fence time stays at |
| +// step 0, nowhere near the 30 s limit. |
| +func TestWriteFenceReleaseKeepsItsDeadlineBehindAHungUpload(t *testing.T) { |
| dir := haBox(t) |
| now := time.Now() |
| t.Cleanup(lease.SetOwedClockForTest(func() time.Time { return now })) |
| @@ -548,9 +541,6 @@ func TestWriteFenceUnblocksAReleaseAlreadyWaiting(t *testing.T) { |
| if g.step(t, 0) { |
| t.Fatal("tripped at step 0") |
| } |
| - hooked := make(chan struct{}) |
| - var once sync.Once |
| - t.Cleanup(lease.SetFinalSyncHookForTest(func() { once.Do(func() { close(hooked) }) })) |
| fx.FailNextMatching(http.MethodPut, http.StatusServiceUnavailable, isMarkerBody) |
| c.down.Store(true) |
| lockPUTs := func() int { |
| @@ -577,36 +567,25 @@ func TestWriteFenceUnblocksAReleaseAlreadyWaiting(t *testing.T) { |
| t.Fatalf("%s: a replica write after the first marker attempt: %v", when, ev) |
| } |
| } |
| + |
| + const budget = 3 * time.Second |
| + rc, cancel := context.WithTimeout(context.Background(), budget) |
| + defer cancel() |
| released := make(chan error, 1) |
| - go func() { |
| - rc, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| - defer cancel() |
| - released <- g.r.ReleaseMarker(rc, hafresh.Marker{V: 1, Clean: true}) |
| - }() |
| - waitClosed(t, hooked, 5*time.Second, "the final-sync hook") |
| - time.Sleep(200 * time.Millisecond) |
| - select { |
| - case err := <-released: |
| - t.Fatalf("the release did not wait for the hung upload: %v", err) |
| - default: |
| - } |
| - if isClosed(g.r.Done()) { |
| - t.Fatal("Done closed while the finalization waits") |
| - } |
| - for k := 1; k <= 31; k++ { |
| - trip := g.step(t, k) |
| - if k <= 30 && trip { |
| - t.Fatalf("tripped at step %d, want first at 31", k) |
| - } |
| - if k == 31 && !trip { |
| - t.Fatal("no trip at step 31") |
| - } |
| - } |
| + start := time.Now() |
| + go func() { released <- g.r.ReleaseMarker(rc, hafresh.Marker{V: 1, Clean: true}) }() |
| var err error |
| select { |
| case err = <-released: |
| - case <-time.After(8 * time.Second): |
| - t.Fatal("the release did not return within 8 s of the trip") |
| + case <-time.After(budget + time.Second): |
| + t.Fatalf("the release did not return within its %s deadline (+1 s)", budget) |
| + } |
| + t.Logf("release returned after %s", time.Since(start).Round(time.Millisecond)) |
| + if isClosed(g.r.Fenced()) { |
| + t.Fatal("the fence fired: the release must not need it") |
| + } |
| + if c.cancelled.Load() == 0 { |
| + t.Fatal("the hung upload was not cancelled by the finalization") |
| } |
| var oe *lease.ObligationError |
| if !errors.As(err, &oe) { |
| @@ -650,6 +629,233 @@ func TestWriteFenceUnblocksAReleaseAlreadyWaiting(t *testing.T) { |
| } |
| } |
| |
| +// slowReplica delays each upload outside the finalization by delay |
| +// (until its context ends), then makes it: a slow store, not a dead one. |
| +type slowReplica struct { |
| + litestream.ReplicaClient |
| + delay time.Duration |
| + entered chan struct{} |
| + once sync.Once |
| +} |
| + |
| +func (c *slowReplica) WriteLTXFile(ctx context.Context, level int, minTXID, maxTXID ltx.TXID, r io.Reader) (*ltx.FileInfo, error) { |
| + if !lease.FinalizingForTest(ctx) { |
| + c.once.Do(func() { close(c.entered) }) |
| + select { |
| + case <-time.After(c.delay): |
| + case <-ctx.Done(): |
| + return nil, ctx.Err() |
| + } |
| + } |
| + return c.ReplicaClient.WriteLTXFile(ctx, level, minTXID, maxTXID, r) |
| +} |
| + |
| +// Astra branch R1 P2, the clean side: the finalization takes the replica |
| +// from a monitor upload in flight (slow, not dead) and stores it with its |
| +// own sync. The release stays clean and finalized, at the local TXID, and |
| +// does not wait for the slow upload. |
| +func TestWriteFenceCleanReleaseStaysCleanAndFast(t *testing.T) { |
| + haBox(t) |
| + fx := leasetest.NewLockFixture(t) |
| + c := &slowReplica{ReplicaClient: lsfile.NewReplicaClient(t.TempDir()), delay: 3 * time.Second, entered: make(chan struct{})} |
| + g := fencedReplicator(t, haHeld(t, fx, "instances/f"), c) |
| + addRows(t, g.path, 5) |
| + g.capture(t) // the monitor starts its (slow) upload |
| + waitClosed(t, c.entered, 10*time.Second, "the monitor's upload") |
| + start := time.Now() |
| + if err := g.r.ReleaseMarker(ctx, hafresh.Marker{V: 1, Clean: true, Finalized: true}); err != nil { |
| + t.Fatal(err) |
| + } |
| + took := time.Since(start) |
| + t.Logf("clean release with a 3 s upload in flight: %s", took.Round(time.Millisecond)) |
| + if took > time.Second { |
| + t.Fatalf("the clean release took %s: it waited for the monitor's upload", took) |
| + } |
| + txid, err := g.r.Finish(ctx) |
| + if err != nil || txid == 0 { |
| + t.Fatalf("finalization (%d, %v)", txid, err) |
| + } |
| + v := lockView(t, fx, "instances/f") |
| + if m := v.Lock.Release; m == nil || !m.Clean || !m.Finalized || m.TXID != txid { |
| + t.Fatalf("marker %+v, want clean and finalized at %d", m, txid) |
| + } |
| + if isClosed(g.r.Fenced()) { |
| + t.Fatal("the fence fired") |
| + } |
| +} |
| + |
| +// fenceAtTheLine brings a failing-replica rig to the step before its trip |
| +// (step 31 trips): a write captured at step 1 that the store never got. |
| +func fenceAtTheLine(t *testing.T, fx *leasetest.LockFixture) (*fenceRig, *failingReplica) { |
| + t.Helper() |
| + c := newFailingReplica(t) |
| + g := fencedReplicator(t, haHeld(t, fx, "instances/f"), c) |
| + t.Cleanup(func() { c.down.Store(false) }) |
| + g.syncAndWait(t) |
| + c.down.Store(true) |
| + g.steps(t, 0, 0, nil) |
| + addRows(t, g.path, 1) |
| + g.capture(t) |
| + g.steps(t, 1, 31, nil) |
| + return g, c |
| +} |
| + |
| +// stepTrip sends tick 32 (the trip) without waiting for its answer. |
| +func (g *fenceRig) stepTrip(t *testing.T) { |
| + t.Helper() |
| + select { |
| + case g.ticks <- g.t0.Add(32 * time.Second): |
| + case <-time.After(5 * time.Second): |
| + t.Fatal("tick 32 not taken") |
| + } |
| +} |
| + |
| +func (g *fenceRig) answer(t *testing.T) bool { |
| + t.Helper() |
| + select { |
| + case v := <-g.sampled: |
| + g.tripped = v |
| + return v |
| + case <-time.After(10 * time.Second): |
| + t.Fatal("the trip was not answered") |
| + } |
| + return false |
| +} |
| + |
| +// Astra branch R1 P1, the fence first: a trip that has marked itself, |
| +// even before it has closed anything or latched the renewer, poisons the |
| +// finalization's decision, so a release on a store that is back is |
| +// unclean and not finalized. |
| +func TestWriteFenceFirstPoisonsTheFinalization(t *testing.T) { |
| + haBox(t) |
| + fx := leasetest.NewLockFixture(t) |
| + marked, resume := make(chan struct{}), make(chan struct{}) |
| + t.Cleanup(lease.SetFenceTripHooksForTest(func() {}, func() { close(marked); <-resume })) |
| + g, c := fenceAtTheLine(t, fx) |
| + g.stepTrip(t) |
| + waitClosed(t, marked, 5*time.Second, "the trip's mark") |
| + c.down.Store(false) // the store is back; the final sync succeeds |
| + if err := g.r.Err(); err != nil { |
| + t.Fatalf("the renewer latched before the trip acted: %v", err) |
| + } |
| + if err := g.r.ReleaseMarker(ctx, hafresh.Marker{V: 1, Clean: true, Finalized: true}); err != nil { |
| + t.Fatal(err) |
| + } |
| + close(resume) |
| + if !g.answer(t) { |
| + t.Fatal("tick 32 did not trip") |
| + } |
| + v := lockView(t, fx, "instances/f") |
| + if m := v.Lock.Release; m == nil || m.Clean || m.Finalized { |
| + t.Fatalf("marker %+v, want unclean and not finalized", m) |
| + } |
| + if _, err := g.r.Finish(ctx); !errors.Is(err, lease.ErrReplicaStale) { |
| + t.Fatalf("finalization = %v, want it poisoned with ErrReplicaStale", err) |
| + } |
| + waitClosed(t, g.r.Fenced(), time.Second, "Fenced after the trip resumed") |
| +} |
| + |
| +// Astra branch R1 P1, the finalization first: a trip that passed its |
| +// Done check but reaches the ordering after the finalization decided does |
| +// nothing: no event, no latch, and the marker stands as the finalization |
| +// decided. |
| +func TestWriteFenceAfterTheFinalizationIsANoOp(t *testing.T) { |
| + haBox(t) |
| + fx := leasetest.NewLockFixture(t) |
| + entered, resume := make(chan struct{}), make(chan struct{}) |
| + var markedRan atomic.Bool |
| + t.Cleanup(lease.SetFenceTripHooksForTest(func() { close(entered); <-resume }, func() { markedRan.Store(true) })) |
| + g, c := fenceAtTheLine(t, fx) |
| + g.stepTrip(t) |
| + waitClosed(t, entered, 5*time.Second, "the trip's entry") |
| + c.down.Store(false) |
| + txid, err := g.r.Finish(ctx) // decides, clean |
| + if err != nil { |
| + t.Fatalf("finalization: %v", err) |
| + } |
| + close(resume) |
| + g.answer(t) // the trip has run |
| + if markedRan.Load() || isClosed(g.r.Fenced()) { |
| + t.Fatal("a trip after the finalization's decision fired") |
| + } |
| + if err := g.r.Err(); errors.Is(err, lease.ErrReplicaStale) { |
| + t.Fatalf("Err = %v after a no-op trip", err) |
| + } |
| + if err := g.r.ReleaseMarker(ctx, hafresh.Marker{V: 1, Clean: true, Finalized: true}); err != nil { |
| + t.Fatal(err) |
| + } |
| + v := lockView(t, fx, "instances/f") |
| + if m := v.Lock.Release; m == nil || !m.Clean || !m.Finalized || m.TXID != txid { |
| + t.Fatalf("marker %+v, want clean and finalized at %d", m, txid) |
| + } |
| +} |
| + |
| +// Fable branch R1 P2-1, with litestream's real monitor (1 s capture, its |
| +// own backoff) and the real 1 s fence ticker: a 20 s outage of |
| +// fast-failing requests, with a write at its start, must not fence (the |
| +// fence's own sync gets the write stored once the store is back, where |
| +// litestream's backoff alone would wait until about 31-36 s); a 40 s one |
| +// fences on time. Both run at once, about 40 s. |
| +func TestWriteFenceRealMonitorShortOutage(t *testing.T) { |
| + if testing.Short() { |
| + t.Skip("about 40 s of real time") |
| + } |
| + haBox(t) |
| + fx := leasetest.NewLockFixture(t) |
| + rig := func(prefix string) (*lease.Replicator, string, *failingReplica) { |
| + c := newFailingReplica(t) |
| + h := haHeld(t, fx, prefix) |
| + lease.SetReplicaClientForTest(h, c) |
| + h.SetMonitorInterval(time.Second) // the HA stream class's capture |
| + path := tempDB(t) |
| + r, err := h.Replicate(ctx, path, 0) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + t.Cleanup(func() { |
| + cc, cancel := context.WithTimeout(context.Background(), 10*time.Second) |
| + defer cancel() |
| + _ = r.Close(cc) |
| + }) |
| + t.Cleanup(func() { c.down.Store(false) }) |
| + if err := r.SyncAndWait(ctx); err != nil { |
| + t.Fatal(err) |
| + } |
| + return r, path, c |
| + } |
| + short, shortPath, shortC := rig("instances/short") |
| + long, longPath, longC := rig("instances/long") |
| + shortC.down.Store(true) |
| + longC.down.Store(true) |
| + start := time.Now() |
| + addRows(t, shortPath, 1) |
| + addRows(t, longPath, 1) |
| + |
| + shortUp := time.AfterFunc(20*time.Second, func() { shortC.down.Store(false) }) |
| + defer shortUp.Stop() |
| + select { |
| + case <-long.Fenced(): |
| + at := time.Since(start) |
| + t.Logf("the 40 s outage fenced at %s", at.Round(100*time.Millisecond)) |
| + if at < 30*time.Second || at > 35*time.Second { |
| + t.Fatalf("the 40 s outage fenced at %s, want 30-35 s after the write", at) |
| + } |
| + case <-short.Fenced(): |
| + t.Fatalf("the 20 s outage fenced at %s", time.Since(start).Round(100*time.Millisecond)) |
| + case <-time.After(40 * time.Second): |
| + t.Fatal("the 40 s outage did not fence") |
| + } |
| + // The short one is well past its own trip time by now. |
| + select { |
| + case <-short.Fenced(): |
| + t.Fatal("the 20 s outage fenced") |
| + case <-time.After(2 * time.Second): |
| + } |
| + if err := short.Err(); err != nil { |
| + t.Fatalf("the 20 s outage's replicator: %v", err) |
| + } |
| +} |
| + |
| func TestWriteFenceEndsWithTheReplicator(t *testing.T) { |
| haBox(t) |
| fx := leasetest.NewLockFixture(t) |