test: witness attach readiness races and start holds at the first waiter

This commit is contained in:
pingqiu committed 2026-11-19 07:44:52 -08:00
1 parent c8abeec62d
commit b967075b56
4 files changed
+47 -1

No files matched your search

@@ -24,3 +24,8 @@ func SetTestDetachTrace(p *DurableProvider, f func(stage string, deadline time.T
// SetTestBeforeIOLock runs f in the backend's admission, right before the I/O
// lock (test-only).
func SetTestBeforeIOLock(b frontend.Backend, f func()) { b.(*StorageBackend).testBeforeIOLock = f }
// SetTestBeforeAttachWait observes the first registered readiness wait.
func SetTestBeforeAttachWait(b frontend.Backend, f func()) {
b.(*StorageBackend).testBeforeReadReadyWait = f
}
@@ -83,3 +83,30 @@ func TestReadReadinessWaitDoesNotHoldAdmissionAndCloseWakesIt(t *testing.T) {
})
}
}
type readinessProjection frontend.Projection
func (p readinessProjection) Projection() frontend.Projection { return frontend.Projection(p) }
func TestAttachReadinessTimeoutPreservesStaleLineage(t *testing.T) {
id := frontend.Identity{VolumeID: "v", ReplicaID: "r", Epoch: 1, EndpointVersion: 1}
b := &StorageBackend{id: id, view: readinessProjection{VolumeID: "v", ReplicaID: "r", Epoch: 2, EndpointVersion: 1, Healthy: true}}
b.setChunkAttachPending("pending")
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
b.testBeforeReadReadyWait = func() { b.SetOperational(true, "attached under another epoch"); cancel() }
_, err := b.Read(ctx, 0, make([]byte, 4096))
if !errors.Is(err, frontend.ErrStalePrimary) {
t.Fatalf("timeout lost stale lineage: %v", err)
}
}
func TestAttachReadinessPublicationRetriesAdmission(t *testing.T) {
id := frontend.Identity{VolumeID: "v", ReplicaID: "r", Epoch: 1, EndpointVersion: 1}
b := &StorageBackend{id: id, view: readinessProjection{VolumeID: "v", ReplicaID: "r", Epoch: 1, EndpointVersion: 1, Healthy: true}}
b.setChunkAttachPending("pending")
b.testAfterAttachRefusal = func() { b.SetOperational(true, "attached") }
if _, err := b.Read(context.Background(), 0, nil); err != nil {
t.Fatalf("readiness publication returned the old refusal: %v", err)
}
}
@@ -151,6 +151,7 @@ type StorageBackend struct {
testBeforeIOLock func()
// Runs outside locks after a read registers its readiness notification.
testBeforeReadReadyWait func()
testAfterAttachRefusal func()
storage storage.LogicalStorage
view frontend.ProjectionView
@@ -574,6 +575,9 @@ func (b *StorageBackend) admitAfterAttach(ctx context.Context) (func(), error) {
if !errors.Is(err, frontend.ErrNotReady) {
return release, err
}
if hook := b.testAfterAttachRefusal; hook != nil {
hook()
}
b.mu.Lock()
pending := b.waitAttachReady
b.mu.Unlock()
@@ -22,6 +22,7 @@ type heldTakeoverClaim struct {
release chan struct{}
once, releaseOnce sync.Once
holdFor time.Duration
readRegistered chan struct{}
}
func (r *heldTakeoverClaim) unblock() { r.releaseOnce.Do(func() { close(r.release) }) }
@@ -33,6 +34,13 @@ func (r *heldTakeoverClaim) CASHead(ctx context.Context, expected storage.ChunkH
r.once.Do(func() { close(r.claimed) })
var expired <-chan time.Time
if r.holdFor > 0 {
select {
case <-r.readRegistered:
case <-r.release:
return gen, nil
case <-ctx.Done():
return gen, ctx.Err()
}
timer := time.NewTimer(r.holdFor)
defer timer.Stop()
expired = timer.C
@@ -49,7 +57,7 @@ func (r *heldTakeoverClaim) CASHead(ctx context.Context, expected storage.ChunkH
func pendingTakeoverRead(t *testing.T, hold time.Duration) (*durable.DurableProvider, frontend.Backend, *nvme.IOHandler) {
t.Helper()
remote := &heldTakeoverClaim{ChunkRemote: seededRemote(t), claimed: make(chan struct{}), release: make(chan struct{}), holdFor: hold}
remote := &heldTakeoverClaim{ChunkRemote: seededRemote(t), claimed: make(chan struct{}), release: make(chan struct{}), holdFor: hold, readRegistered: make(chan struct{})}
view := newStubView(frontend.Projection{VolumeID: "v1", ReplicaID: "r1"})
p := chunkProvider(t, t.TempDir(), remote, view, 0)
// Registered after provider cleanup, so release precedes its worker join.
@@ -74,6 +82,8 @@ func pendingTakeoverRead(t *testing.T, hold time.Duration) (*durable.DurableProv
if err != nil {
t.Fatal(err)
}
var firstWait sync.Once
durable.SetTestBeforeAttachWait(backend, func() { firstWait.Do(func() { close(remote.readRegistered) }) })
handler := nvme.NewIOHandler(nvme.HandlerConfig{Backend: backend, BlockSize: 4096, VolumeSize: pcBlocks * 4096, NSID: 1})
return p, backend, handler
}