authority: tests follow the lease deadline barrier; renewals write nothing

Tests that pinned the removed horizon (barrier at the persisted
issue+1.5*TTL, extension writes and their status counters) now use this
process's deadline (or restart floor) plus skew. The golden wm lost range
is the same file: the master restarts half a lease after the issue, so
both versions' barriers are the same instant.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
pingqiuandClaude Opus 5.5 committed 2026-12-01 15:12:45 -08:00
1 parent ddc349abe8
commit 2e9d6e04a7
18 files changed
+95 -125

No files matched your search

@@ -112,7 +112,12 @@ func goldenWorkspace(t *testing.T, dir string) {
if r, err := p.MoveWorkspace(ctx, goldenCleanMove("wm"), predecessorHead); err != nil || !r.Issued {
t.Fatalf("clean move of wm: %+v %v", r, err)
}
now = now.Add(grantTTL + grantTTL/2 + goldenWorkspaceSkew)
// The master restarts half a lease after the issue, so the forced move's
// barrier is the same instant on v1.0.0 (the persisted issue+1.5*TTL) and
// today (the restart floor, restart+TTL): wm's lost range is the same file.
now = now.Add(grantTTL / 2)
p = goldenWorkspacePublisher(store, &now)
now = now.Add(grantTTL + goldenWorkspaceSkew)
if r, err := p.MoveWorkspace(ctx, goldenForcedMove("wm"), predecessorHead); err != nil || !r.Issued {
t.Fatalf("forced move of wm: %+v %v", r, err)
}
@@ -22,8 +22,8 @@ func TestDeletionReservationGuardSites(t *testing.T) {
"publishIssuedLeaseLocked": true, "setLeaseDeadlineLocked": true,
"RenewWorkspaceLease": true, "regrantWorkspaceLease": true,
// Reply-only copies never increase published validity.
"WorkspaceLeaseReply": true, "AckWorkspaceRevocation": true,
// Copies the published horizon into the leaf view under mu -> leaseMu.
"WorkspaceLeaseReply": true,
// Copies the persisted lease into the leaf view under mu -> leaseMu.
"deriveLeaseLocked": true,
// Positive lease construction and publication, or a plain clone.
"newWorkspaceLease": true, "firstWorkspaceGrant": true,
@@ -15,7 +15,7 @@ func TestWorkspaceForcedMoveAdmittedMatchesMoveAdmission(t *testing.T) {
disk, _ := newTempStore(t)
now := time.Unix(1000, 0)
p, issued := cleanMoveToGrantee(t, disk, &now)
barrier := issued.Add(grantTTL + grantTTL/2 + p.workspaceSkew(p.WorkspaceState("v").Lease))
barrier := issued.Add(grantTTL + p.workspaceSkew(p.WorkspaceState("v").Lease))
now = barrier.Add(-time.Nanosecond)
if err := p.WorkspaceForcedMoveAdmitted(ctx, "v", "forced", "r", 2); err == nil || !strings.Contains(err.Error(), "expiry plus clock-skew margin has not elapsed") {
t.Fatalf("admitted before expiry+skew: %v", err)
@@ -115,9 +115,9 @@ func forcedMove(volume, id string) (WorkspaceMoveRequest, WorkspaceHead) {
}
// The view equals derive(published) after every writer kind: bind, first
// grant, extension, endpoint refresh, revocation, ACK, clean move issue, a
// move reservation whose issue write fails (uncertain via apply), a failed
// extension (uncertain via persistWorkspaceLocked), reload and close.
// grant, renewal, endpoint refresh, revocation, ACK, clean move issue, a
// move reservation whose issue write fails (uncertain via apply), reload and
// close.
func TestWorkspaceLeaseViewMatchesDeriveAfterEveryWriter(t *testing.T) {
disk, _ := newTempStore(t)
hs := &hookStore{AuthorityStore: disk}
@@ -147,10 +147,10 @@ func TestWorkspaceLeaseViewMatchesDeriveAfterEveryWriter(t *testing.T) {
if _, err := p.RenewWorkspaceLease(ctx, "v", "r", 1); err != nil {
t.Fatal(err)
}
if got := p.WorkspaceState("v").Lease.ExpiresAtNS; got != c.sec(55).UnixNano() || got <= firstH {
t.Fatalf("extension did not publish trigger+1.5*TTL: %d", got)
if got := p.WorkspaceState("v").Lease.ExpiresAtNS; got != firstH {
t.Fatalf("a renewal changed the persisted lease: %d, was %d", got, firstH)
}
check("extension")
check("renewal")
if err := p.apply(AssignmentAsk{VolumeID: "v", ReplicaID: "r", DataAddr: "d2", CtrlAddr: "c", Intent: IntentRefreshEndpoint}); err != nil {
t.Fatal(err)
}
@@ -174,7 +174,7 @@ func TestWorkspaceLeaseViewMatchesDeriveAfterEveryWriter(t *testing.T) {
t.Fatalf("move issue deadline %s; want the new holder's issue+TTL %s", got, c.sec(40))
}
// w: forced move whose issue write fails after the reservation persisted.
c.at(30 + 15 + 20 + 1) // past w's horizon (45) + skew (20)
c.at(30 + 20 + 1) // past w's deadline (30) + skew (20)
hs.setBefore(func(r DurableRecord) error {
if r.VolumeID == "w" && r.Workspace != nil && len(r.Workspace.Moves) > 0 && r.Workspace.Moves[len(r.Workspace.Moves)-1].Issued {
return errors.New("injected issue write failure")
@@ -32,11 +32,9 @@ func forcedLostRangeFixture(t *testing.T, store AuthorityStore, restart bool) (p
if restart && !p.leaseDeadlineLockedForTest("v").After(time.Unix(0, lease.ExpiresAtNS)) {
t.Fatal("fixture: the restart deadline is not the later barrier term")
}
barrier = time.Unix(0, lease.ExpiresAtNS)
if d := p.leaseDeadlineLockedForTest("v"); d.After(barrier) {
barrier = d
}
return p, now, barrier.Add(p.workspaceSkew(lease))
// The barrier is this process's deadline (the grant's, or after a
// restart its floor) plus skew; the persisted ExpiresAtNS is not a term.
return p, now, p.leaseDeadlineLockedForTest("v").Add(p.workspaceSkew(lease))
}
var forcedLostRangeReq = WorkspaceMoveRequest{RequestID: "f", VolumeID: "v", ExpectedReplicaID: "old", ExpectedHost: "old-node", TargetHost: "new-node", ExpectedEpoch: 1,
@@ -44,8 +42,8 @@ var forcedLostRangeReq = WorkspaceMoveRequest{RequestID: "f", VolumeID: "v", Exp
var forcedLostRangeHead = WorkspaceHead{Gen: 7, Epoch: 1, Holder: "old-node/old", RemoteLSN: 42}
// A forced move records the abandoned range beside its record: the kept HEAD
// frontier and the forced barrier (lease horizon, or after a restart the
// later restart deadline, plus skew) as the latest master time an old-holder
// frontier and the forced barrier (the lease deadline, or after a restart
// the restart floor, plus skew) as the latest master time an old-holder
// write can have been acknowledged. A reload returns it; the move record's
// own encoding is unchanged, so older binaries still verify and load it.
func TestWorkspaceForcedMoveRecordsTheLostRange(t *testing.T) {
@@ -216,14 +216,14 @@ func TestWorkspaceLeaseRegrantWaitsForFullSkew(t *testing.T) {
p := NewPublisher(nil, WithStore(store), WithWorkspaceLeaseConfig(10*time.Second, clock.now), WithWorkspaceMaxClockSkew(4*time.Second))
grant := bindAndGrant(t, p, "v", "r")
if grant.ExpiresAtNS != clock.sec(10).UnixNano() || p.WorkspaceState("v").Lease.ExpiresAtNS != clock.sec(15).UnixNano() {
t.Fatal("fixture did not create the declared grant/horizon", grant)
t.Fatal("fixture did not create the declared grant and record", grant)
}
writes := 0
store.setBefore(func(DurableRecord) error { writes++; return nil })
// The deadline+skew boundary is 14 s; horizon+skew is 19 s.
// Re-grant uses the same MAX barrier as a forced move, including the
// interval after the shorter deadline has passed and its final ns.
for _, now := range []time.Time{clock.sec(13), clock.sec(14), clock.sec(19).Add(-time.Nanosecond)} {
// The barrier is deadline+skew, 14 s; the persisted record's 15 s is
// not a term. Re-grant uses the same barrier as a forced move, including
// the interval after the deadline has passed and its final ns.
for _, now := range []time.Time{clock.sec(10), clock.sec(13), clock.sec(14).Add(-time.Nanosecond)} {
clock.ns.Store(now.UnixNano())
if got, err := p.RenewWorkspaceLease(context.Background(), "v", "r", 1); err == nil {
t.Fatal("re-grant crossed the later expiry/skew boundary early", now, got)
@@ -235,7 +235,7 @@ func TestWorkspaceLeaseRegrantWaitsForFullSkew(t *testing.T) {
t.Fatal("unsafe early re-grant attempted persistence", writes)
}
}
clock.at(19)
clock.at(14)
if err := p.WorkspaceForcedMoveAdmitted(context.Background(), "v", "force", "r", 1); err != nil {
t.Fatal("fixture did not reach the shared forced-move barrier", err)
}
@@ -27,7 +27,8 @@ func (s *slowLeaseStore) Put(r DurableRecord) error {
// A renewal of a live lease must not wait for the authority store's fsync:
// the host's renewal RPC times out after 2 s, so a slower store write on the
// renewal path turns into a host fence and a volume shutdown (issue #205).
// renewal path turns into a host fence and a volume shutdown (issue #205). A
// renewal writes nothing, so it never reaches the stalled store.
func TestWorkspaceRenewalDoesNotWaitForSlowStore(t *testing.T) {
disk, _ := newTempStore(t)
slow := &slowLeaseStore{AuthorityStore: disk}
@@ -57,8 +58,8 @@ func TestWorkspaceRenewalDoesNotWaitForSlowStore(t *testing.T) {
t.Fatalf("renewal of a live lease took %s behind a 2.5 s store write; the host's renewal RPC times out at 2 s", took.Round(time.Millisecond))
}
t.Logf("renewal took %s", took)
if slow.puts.Load() == 0 {
t.Fatal("no lease write went through the 2.5 s stall: the test did not exercise it")
if slow.puts.Load() != 0 {
t.Fatalf("a renewal of a live lease wrote %d lease records; it writes nothing", slow.puts.Load())
}
}
@@ -113,8 +114,8 @@ func TestWorkspaceConcurrentRenewalsDoNotSerializeOnStore(t *testing.T) {
t.Fatalf("slowest of %d concurrent renewals took %s (all: %v); renewals serialize behind each other's store writes", n, slowest.Round(time.Millisecond), took)
}
t.Logf("slowest of %d concurrent renewals took %s", n, slowest)
if slow.puts.Load() == 0 {
t.Fatal("no lease write went through the 0.5 s stall: the test did not exercise it")
if slow.puts.Load() != 0 {
t.Fatalf("renewals of live leases wrote %d lease records; they write nothing", slow.puts.Load())
}
}
@@ -23,10 +23,9 @@ func TestWorkspaceClockSkewPersistsAndProtectsRestart(t *testing.T) {
if lease.MaxClockSkewNS != int64(4*time.Second) {
t.Fatal("configured margin not in durable grant", lease)
}
// The first grant persists the durable horizon start+1.5*TTL (9 s),
// the upper bound of every later grant; the forced barrier is
// horizon + skew, TTL/2 later than the grant itself (#205).
boundary := start.Add(9*time.Second + 4*time.Second)
// The forced barrier is the grant's deadline (start+TTL) plus the
// persisted skew; after a restart, the restart floor plus it.
boundary := start.Add(6*time.Second + 4*time.Second)
if restart {
now = start.Add(7 * time.Second)
p = NewPublisher(nil, WithStore(disk), WithWorkspaceLeaseConfig(6*time.Second, func() time.Time { return now }), WithWorkspaceMaxClockSkew(time.Second))
@@ -35,7 +35,7 @@ func TestWorkspaceCrashAfterJournalBeforeEpochResumesSameGrant(t *testing.T) {
if _, err := p.RenewWorkspaceLease(context.Background(), "v", "old", 1); err != nil {
t.Fatal(err)
}
// Past the durable horizon (1.5*TTL) plus skew, not only the grant (#205).
// Past the grant's deadline plus skew.
now = now.Add(3 * time.Second)
req := WorkspaceMoveRequest{RequestID: "m", VolumeID: "v", ExpectedReplicaID: "old", ExpectedHost: "old-node", TargetHost: "new-node", ExpectedEpoch: 1, TargetReplicaID: "old", TargetDataAddr: "nd", TargetCtrlAddr: "nc", Kind: "forced", Force: true}
head := WorkspaceHead{Gen: 7, Epoch: 1, Holder: "old-node/old", RemoteLSN: 42}
@@ -172,7 +172,9 @@ func TestWorkspaceForcedMoveRequiresExplicitForceAndLeaseExpiry(t *testing.T) {
if _, err := p.MoveWorkspace(ctx, req, head); err == nil {
t.Fatal("unexpired lease moved")
}
now = now.Add(2 * time.Second)
// Past the deadline, before deadline plus skew: the expired holder is
// not re-granted (a re-grant waits for the move barrier).
now = now.Add(1500 * time.Millisecond)
if _, err := p.RenewWorkspaceLease(ctx, "v", "old", 1); err == nil {
t.Fatal("expired host renewed")
}
@@ -236,7 +238,7 @@ func TestWorkspaceMoveUsesQualifiedHEADHolder(t *testing.T) {
if _, err := p.RenewWorkspaceLease(ctx, "v", "same", 1); err != nil {
t.Fatal(err)
}
// Past the durable horizon (1.5*TTL) plus skew, not only the grant (#205).
// Past the grant's deadline plus skew.
now = now.Add(3 * time.Second)
q := WorkspaceMoveRequest{RequestID: "qualified", VolumeID: "v", ExpectedReplicaID: "same", TargetReplicaID: "same", ExpectedHost: "old-node", TargetHost: "new-node", ExpectedEpoch: 1, TargetDataAddr: "nd", TargetCtrlAddr: "nc", Kind: "forced", Force: true}
for _, holder := range []string{"same", "other-node/same", "old-node/other"} {
@@ -94,12 +94,15 @@ func TestWorkspaceUnclaimedGrantForcedReassignAfterExpiry(t *testing.T) {
assertLeaseViewMatches(t, p, "after restart")
}
skew := p.workspaceSkew(p.WorkspaceState("v").Lease)
// The horizon (issue+1.5*TTL) is the later bound: the deadline is
// issue+TTL, or restart+TTL after a restart 0.2*TTL later.
barrier := issued.Add(grantTTL + grantTTL/2 + skew)
// The deadline is issue+TTL, or the restart floor restart+TTL
// after a restart 0.2 s later; the barrier is it plus skew.
barrier := issued.Add(grantTTL + skew)
if restart {
barrier = barrier.Add(200 * time.Millisecond)
}
// The predecessor's lease (renewed 0.1 s before the issue) has
// expired plus skew by then: only the grant's lease may admit.
if old := issued.Add(-100*time.Millisecond + grantTTL + grantTTL/2 + skew); !barrier.Add(-time.Nanosecond).After(old) {
if old := issued.Add(-100*time.Millisecond + grantTTL + skew); !barrier.Add(-time.Nanosecond).After(old) {
t.Fatal("test clock does not separate the predecessor's barrier from the grant's")
}
now = barrier.Add(-time.Nanosecond)
@@ -214,17 +217,15 @@ func TestWorkspaceUnclaimedGrantGranteeRenewsNormally(t *testing.T) {
if _, err := p.RenewWorkspaceLease(ctx, "v", "r", 1); err == nil {
t.Fatal("old epoch renewed after the move")
}
// Past 0.25*TTL the renewal starts an extension of the issued lease.
// A later renewal moves the deadline and writes nothing: the persisted
// lease stays the one the issue wrote.
now = issued.Add(grantTTL / 2)
if _, err := p.RenewWorkspaceLease(ctx, "v", "r", 2); err != nil {
t.Fatal(err)
if l, err := p.RenewWorkspaceLease(ctx, "v", "r", 2); err != nil || l.ExpiresAtNS != now.Add(grantTTL).UnixNano() {
t.Fatal("later renewal:", l, err)
}
st := p.WorkspaceState("v")
for wait := time.Now().Add(5 * time.Second); time.Now().Before(wait) && (st.Lease == nil || st.Lease.ExpiresAtNS == issued.Add(grantTTL+grantTTL/2).UnixNano()); st = p.WorkspaceState("v") {
time.Sleep(5 * time.Millisecond)
}
if st.Lease == nil || st.Lease.Epoch != 2 || st.Lease.ExpiresAtNS != now.Add(grantTTL+grantTTL/2).UnixNano() {
t.Fatalf("issued lease not extended: %+v", st.Lease)
if store.puts.Load() != writes || st.Lease == nil || st.Lease.Epoch != 2 || st.Lease.ExpiresAtNS != issued.Add(grantTTL+grantTTL/2).UnixNano() {
t.Fatalf("a renewal wrote: %d records, lease %+v", store.puts.Load()-writes, st.Lease)
}
if line, _ := p.VolumeAuthorityLine("v"); line.Epoch != 2 || len(st.Moves) != 1 {
t.Fatal("renewals changed authority", line)
@@ -16,9 +16,9 @@ func TestWorkspaceAckUsesGrantedHalfSkewBoundary(t *testing.T) {
{"before", 28*time.Second - time.Nanosecond, true, false},
{"at", 28 * time.Second, false, false},
{"after", 29 * time.Second, false, false},
{"horizon-before", 43*time.Second - time.Nanosecond, true, true},
{"horizon-at", 43 * time.Second, false, true},
{"horizon-after", 44 * time.Second, false, true},
{"restart-before", 48*time.Second - time.Nanosecond, true, true},
{"restart-at", 48 * time.Second, false, true},
{"restart-after", 49 * time.Second, false, true},
} {
t.Run(tc.name, func(t *testing.T) {
disk, _ := newTempStore(t)
@@ -36,8 +36,9 @@ func TestWorkspaceAckUsesGrantedHalfSkewBoundary(t *testing.T) {
t.Fatal(err)
}
if tc.restart {
// Reopening at +20s gives a restart deadline of +50s, beyond
// the persisted +45s horizon. ACK must stop at +43s.
// Reopening at +20s gives a restart floor of +50s, beyond the
// persisted record's +45s: the ACK follows the floor and stops
// at +48s (the floor minus half the skew).
now = start.Add(20 * time.Second)
p = NewPublisher(nil, WithStore(disk), WithWorkspaceLeaseConfig(30*time.Second, func() time.Time { return now }), WithWorkspaceMaxClockSkew(4*time.Second))
p.mu.Lock()
@@ -2,13 +2,11 @@ package master
import (
"context"
"encoding/json"
"fmt"
"io"
"log"
"net"
"net/http"
"net/http/httptest"
"path/filepath"
"strings"
"sync"
@@ -301,51 +299,6 @@ func TestHostCloseForceClosesAStalledStatusConnection(t *testing.T) {
}
}
// The status endpoint carries the extension write latency: -1 before any
// extension, then the measured write once a renewal past 1.25 TTL of
// headroom (7.5 s after the first grant, TTL 30 s) has started one.
func TestStatusReportsExtensionWriteLatency(t *testing.T) {
h, c, _ := reviewBindingHost(t)
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
reviewLegacyBind(t, h, c, ctx, "vol-b")
line := waitAuthorityLine(t, h.Publisher(), "vol-b")
svc := &services{host: h}
id := &control.WorkspaceIdentity{ServerId: "node-a", VolumeId: "vol-b", ReplicaId: line.ReplicaID, Epoch: line.Epoch}
if _, err := svc.RenewWorkspaceLease(loopbackPeer(ctx), id); err != nil {
t.Fatal(err) // first grant: not an extension
}
get := func() map[string]any {
t.Helper()
rec := httptest.NewRecorder()
h.statusHandler().ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "/status/workspace-lease", nil))
var body map[string]any
if err := json.NewDecoder(rec.Body).Decode(&body); err != nil {
t.Fatal(err)
}
return body
}
if b := get(); b["extension_write_last_ms"] != float64(-1) || b["extension_write_max_last_minute_ms"] != float64(-1) {
t.Fatalf("before any extension: %v", b)
}
time.Sleep(8 * time.Second)
if _, err := svc.RenewWorkspaceLease(loopbackPeer(ctx), id); err != nil {
t.Fatal(err)
}
for deadline := time.Now().Add(5 * time.Second); ; time.Sleep(20 * time.Millisecond) {
b := get()
last, _ := b["extension_write_last_ms"].(float64)
max, _ := b["extension_write_max_last_minute_ms"].(float64)
if last >= 0 && max >= last {
t.Logf("extension write: last %v ms, max in the last minute %v ms", last, max)
return
}
if time.Now().After(deadline) {
t.Fatalf("no extension write reported: %v", b)
}
}
}
// With a known file system id on both sides, machine id + device alone no
// longer make a node co-located: a different fsid is another file system.
// The same three facts are co-located; a heartbeat without an fsid (an older
@@ -86,15 +86,15 @@ func TestLeaseRefusalKinds(t *testing.T) {
t.Fatal("first grant:", err)
}
// BARRIER: past the grant (TTL) but before the horizon (1.5 TTL) plus
// the default skew (2/3 TTL), i.e. 65 s after the grant.
// BARRIER: past the grant (TTL, 30 s) but before it plus the default
// skew (2/3 TTL), i.e. 50 s after the grant.
nowNS.Add(int64(40 * time.Second))
text, r := refusalOf(t, "expired re-grant before the barrier", func() error { _, err := ws.RenewWorkspaceLease(ctx, e1); return err }())
if r == nil || r.GetKind() != control.WorkspaceLeaseRefusal_BARRIER {
t.Fatalf("re-grant before the barrier: detail %v; want BARRIER", r)
}
if left := time.Duration(r.GetRetryAfterNs()); left < 24*time.Second || left > 26*time.Second {
t.Fatalf("BARRIER retry_after %v; want 25 s (the barrier 65 s after the grant, now +40 s)", left)
if left := time.Duration(r.GetRetryAfterNs()); left < 9*time.Second || left > 11*time.Second {
t.Fatalf("BARRIER retry_after %v; want 10 s (the barrier 50 s after the grant, now +40 s)", left)
}
if !strings.HasPrefix(text, "workspace forced move lease expiry plus clock-skew margin has not elapsed") || !strings.Contains(text, "re-grant refused until") {
t.Fatalf("BARRIER text %q: want the barrier text with its time", text)
@@ -85,7 +85,12 @@ func TestWorkspacePausedGranteeRefusedAtAttach(t *testing.T) {
if _, err := p.RenewWorkspaceLease(ctx, "v", "r", 1); err != nil {
t.Fatal(err)
}
oldBarrier := time.Unix(0, p.WorkspaceState("v").Lease.ExpiresAtNS)
// The predecessor's deadline (this process's grant, not the record's).
oldLease, ok := p.WorkspaceLeaseReply("v")
if !ok {
t.Fatal("no predecessor lease")
}
oldBarrier := time.Unix(0, oldLease.ExpiresAtNS)
fixture := testutil.NewLayoutFixture(t, "v", 64, 4)
readHead := func() authority.WorkspaceHead {
@@ -176,7 +181,8 @@ func TestWorkspacePausedGranteeRefusedAtAttach(t *testing.T) {
t.Fatalf("grantee lease %+v", lease)
}
skew := time.Duration(lease.MaxClockSkewNS)
grantBarrier := time.Unix(0, lease.ExpiresAtNS).Add(skew)
granted, _ := p.WorkspaceLeaseReply("v") // the grantee's deadline after its renewal
grantBarrier := time.Unix(0, granted.ExpiresAtNS).Add(skew)
forced := authority.WorkspaceMoveRequest{RequestID: "forced", VolumeID: "v", ExpectedReplicaID: "r", TargetReplicaID: "r", ExpectedHost: "new-node", TargetHost: "third-node", ExpectedEpoch: 2,
TargetDataAddr: "td", TargetCtrlAddr: "tc", Kind: "forced", Force: true}
@@ -101,13 +101,13 @@ func TestWorkspaceHeldRenewalBoundary(t *testing.T) {
case <-ctx.Done():
t.Fatal("renewal did not enter", ctx.Err())
}
// A renewal is answered from memory: the grant is in the reply, and
// the durable lease holds the horizon (>= every grant), so the
// persisted expiry is not the grant any more (#205).
// A renewal is answered from memory: the grant is in the reply and
// nothing is written, so the persisted lease is still the first
// grant's record.
renewed := *p.WorkspaceState("v").Lease
freshEarly := requestStart.Add(time.Duration(reply.ValidForNs))
if freshEarly.Sub(oldEarly) < time.Second || reply.ExpiresAtNs <= client.initial.ExpiresAtNs || renewed.ExpiresAtNS < reply.ExpiresAtNs || renewed.MaxClockSkewNS != int64(4*time.Second) {
t.Fatal("fixture did not extend the grant under the durable horizon", client.initial, reply, oldLease, renewed)
t.Fatal("fixture did not extend the grant", client.initial, reply, oldLease, renewed)
}
arrival := oldEarly.Add(-time.Second)
if late {
@@ -140,14 +140,10 @@ func TestWorkspaceHeldRenewalBoundary(t *testing.T) {
}
q := authority.WorkspaceMoveRequest{RequestID: "renewed-forced", VolumeID: "v", ExpectedReplicaID: "r", TargetReplicaID: "r", ExpectedHost: "old", TargetHost: "new", ExpectedEpoch: 1, TargetDataAddr: "nd", TargetCtrlAddr: "nc", Kind: "forced", Force: true}
head := authority.WorkspaceHead{Gen: 7, Epoch: 1, Holder: "old/r", RemoteLSN: 42}
// The barrier is max(durable horizon, renewed grant) + skew; the old
// The barrier is the renewed grant + the persisted skew; the old
// grant's expiry plus skew is still too early.
barrierBase := renewed.ExpiresAtNS
if reply.ExpiresAtNs > barrierBase {
barrierBase = reply.ExpiresAtNs
}
boundary := time.Unix(0, barrierBase).Add(time.Duration(renewed.MaxClockSkewNS))
for _, when := range []time.Time{time.Unix(0, client.initial.ExpiresAtNs).Add(4 * time.Second), time.Unix(0, reply.ExpiresAtNs).Add(4 * time.Second), boundary.Add(-time.Nanosecond)} {
boundary := time.Unix(0, reply.ExpiresAtNs).Add(time.Duration(renewed.MaxClockSkewNS))
for _, when := range []time.Time{time.Unix(0, client.initial.ExpiresAtNs).Add(4 * time.Second), boundary.Add(-time.Nanosecond)} {
masterNS.Store(when.UnixNano())
if _, err := p.MoveWorkspace(ctx, q, head); err == nil {
t.Fatal("forced move ignored renewed expiry plus persisted margin")
@@ -172,7 +172,8 @@ func TestWorkspaceLeaseGuardSurvivesSlowAuthorityStore(t *testing.T) {
// Every lease write is now held until a renewal succeeded during its hold,
// so a renewal that waited for the store would deadlock this test. The
// sampler records any fence while armed; three held writes must complete.
// sampler records any fence while armed; four renewals must succeed (about
// 5 s), and none of them may write the lease (a renewal writes nothing).
var fenced atomic.Bool
var sinceOKAtFence atomic.Int64 // time since the latest successful renewal when the fence was first seen
sampled := make(chan struct{})
@@ -188,14 +189,20 @@ func TestWorkspaceLeaseGuardSurvivesSlowAuthorityStore(t *testing.T) {
time.Sleep(20 * time.Millisecond)
}
}()
for slow.held.Load() < 3 {
if fenced.Load() {
base := slow.renewals.Load()
for wait := time.Now().Add(30 * time.Second); slow.renewals.Load() < base+4 && !fenced.Load(); time.Sleep(20 * time.Millisecond) {
if time.Now().After(wait) {
break
}
time.Sleep(20 * time.Millisecond)
}
slow.armed.Store(false)
<-sampled
if n := slow.renewals.Load() - base; n < 4 && !fenced.Load() {
t.Fatalf("only %d renewals succeeded in 30 s while armed", n)
}
if n := slow.gen.Load(); n != 0 {
t.Fatalf("renewals wrote the lease %d times while armed; a renewal writes nothing", n)
}
if fenced.Load() || !g.Projection().Healthy || stops.Load() != 0 {
// The gaps tell a starved runner (a gap near the 4 s validity) from a
// guard that fenced although renewals kept succeeding in time.
@@ -50,7 +50,7 @@ func TestWorkspaceSkewSeparatesOldServingFromForcedGrant(t *testing.T) {
if err := g.Refresh(context.Background()); err != nil {
t.Fatal(err)
}
lease := p.WorkspaceState("v").Lease
lease, _ := p.WorkspaceLeaseReply("v") // this process's deadline for the grant
q := authority.WorkspaceMoveRequest{RequestID: "forced", VolumeID: "v", ExpectedReplicaID: "r", TargetReplicaID: "r", ExpectedHost: "old", TargetHost: "new", ExpectedEpoch: 1, TargetDataAddr: "nd", TargetCtrlAddr: "nc", Kind: "forced", Force: true}
head := authority.WorkspaceHead{Gen: 7, Epoch: 1, Holder: "old/r", RemoteLSN: 42}
if check == "host-early-fence" {
@@ -35,7 +35,7 @@ func seedDetachedHead(t *testing.T, ctx context.Context, remote *storage.MemChun
// then) and is granted on the predecessor's unchanged HEAD. A late claim by
// the dead grantee never displaces the new owner's higher epoch.
func TestWorkspaceUnclaimedGrantForcedReassignOverRPC(t *testing.T) {
// The real lease horizon (1.5 x the 30 s TTL) plus skew takes ~45 s.
// The real lease (the 30 s TTL) plus skew takes ~50 s.
ctx, cancel := context.WithTimeout(context.Background(), 180*time.Second)
defer cancel()
remote := storage.NewMemChunkRemote()
@@ -114,7 +114,8 @@ func TestWorkspaceUnclaimedGrantForcedReassignOverRPC(t *testing.T) {
if st == nil || st.Lease == nil || st.Lease.ReplicaID != "r1" || st.Lease.Epoch != grantee || st.Lease.Revoking {
t.Fatalf("the issued move must start node-b's lease: %+v", st)
}
barrier := time.Unix(0, st.Lease.ExpiresAtNS).Add(skew)
granted, _ := m.Publisher().WorkspaceLeaseReply("vol-a") // the issue's deadline, not the record's
barrier := time.Unix(0, granted.ExpiresAtNS).Add(skew)
if q, err := ws.QueryWorkspace(ctx, &control.WorkspaceVolume{VolumeId: "vol-a"}); err != nil || q.GetLease() == nil || q.GetLease().GetEpoch() != grantee {
t.Fatalf("QueryWorkspace after the issue: %+v %v", q.GetLease(), err)
}