storage: tests bound their drains and fail real fence slices

The held-drain controls run the drain loop on the test goroutine with a
cycle bound, and the leader-wait control joins its worker before the
store's cleanup on every path. The fence's slices are checked at their
fsync (the counter holds only the completed slices there), and a second
slice's zero write or fsync fails through test seams: no credit for the
failing slice, no completed fence recorded, the store latched.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
pingqiuandClaude Opus 5.5 committed 2026-12-02 01:26:24 -08:00
1 parent d5c2fbdeae
commit eec85d11ec
4 files changed
+112 -19

No files matched your search

+4 -2
View File
@@ -165,8 +165,10 @@ type WALStore struct {
testAfterAdmit func()
// testBeforeFileSync, when set, runs in syncFile just before fileSyncMu
// is taken. Tests only.
testBeforeFileSync func()
testAfterFenceFsync func() // after each fence slice's fsync returns; tests only
testBeforeFileSync func()
testBeforeFenceWrite func() error // before each fence slice's zero writes (an error fails the write); tests only
testBeforeFenceFsync func() error // before each fence slice's fsync (an error fails the fsync); tests only
testAfterFenceFsync func() // after each fence slice's fsync returns; tests only
// Generations cover completed append attempts and recovery publication.
walMutation atomic.Uint64
durableMutation atomic.Uint64
@@ -291,15 +291,20 @@ func TestWALStoreV3HeldCheckpointEndsTheDrain(t *testing.T) {
t.Fatal(err)
}
s.SetRecycleFloorSource(&fakeRecycleFloorSource{floor: 6, anyActive: true})
done := make(chan error, 1)
go func() { done <- s.flusher.flushOnce() }()
select {
case err := <-done:
// The drain's own loop (flushOnceInternal's), bounded on this goroutine: a
// pass that repeats its batch fails here instead of hanging the package.
p := flushPass{limit: math.MaxUint64}
ended := false
for i := 0; i < 64 && !ended; i++ {
r, err := s.flusher.flushCycle(false, p)
if err != nil {
t.Fatal(err)
}
case <-time.After(10 * time.Second):
t.Fatal("a drain held by the recycle floor did not end")
ended = !r.more
p = r.next(r.frontier)
}
if !ended {
t.Fatal("a drain held by the recycle floor did not end within 64 cycles")
}
if cp := s.CheckpointLSN(); cp != 6 {
t.Fatalf("checkpoint=%d, want the floor 6", cp)
@@ -342,16 +347,17 @@ func TestWALStoreV3HeldThenJumpRetiresEveryEntryBelowTheCheckpoint(t *testing.T)
// Held at 6: a pass writes every entry to the extent, 7..12 stay dirty.
s.SetRecycleFloorSource(&fakeRecycleFloorSource{floor: 6, anyActive: true})
p := flushPass{limit: math.MaxUint64}
for {
ended := false
for i := 0; i < 64 && !ended; i++ { // bounded: a pass that repeats its batch fails below
r, err := s.flusher.flushCycle(false, p)
if err != nil {
t.Fatal(err)
}
if !r.more {
p = r.next(math.MaxUint64) // the run loop's position after the held pass
break
}
p = r.next(math.MaxUint64)
ended = !r.more
p = r.next(math.MaxUint64) // after the last cycle: the run loop's position after the held pass
}
if !ended {
t.Fatal("the held pass did not end within 64 cycles")
}
if cp := s.CheckpointLSN(); cp != 6 || s.dm.len() != 6 {
t.Fatalf("held pass: checkpoint=%d dirty=%d, want 6 and 6", cp, s.dm.len())
@@ -441,19 +447,33 @@ func TestWALStoreV3CheckpointStepsAreTimed(t *testing.T) {
s.v3.syncMu.Lock() // the leader role, held elsewhere
done := make(chan error, 1)
go func() { done <- s.flusher.flushOnce() }()
// join releases the leader role and waits, bounded, for the drain to end
// before the store's cleanup closes it, on every path.
join := func() error {
s.v3.syncMu.Unlock()
select {
case err := <-done:
return err
case <-time.After(10 * time.Second):
t.Fatal("the drain did not end within 10 s of the leader role's release")
return nil
}
}
deadline := time.Now().Add(5 * time.Second)
for {
if cur := s.CheckpointPersistence().CheckpointSteps; cur.Step == "leader_wait" && !cur.Since.IsZero() {
break
}
if time.Now().After(deadline) {
s.v3.syncMu.Unlock()
t.Fatalf("a checkpoint waiting for the leader role: %+v", s.CheckpointPersistence().CheckpointSteps)
seen := s.CheckpointPersistence().CheckpointSteps
if err := join(); err != nil {
t.Errorf("drain: %v", err)
}
t.Fatalf("a checkpoint waiting for the leader role: %+v", seen)
}
time.Sleep(5 * time.Millisecond)
}
s.v3.syncMu.Unlock()
if err := <-done; err != nil {
if err := join(); err != nil {
t.Fatal(err)
}
}
@@ -102,10 +102,21 @@ func TestWALStoreV3FenceSlicesCountAfterEachSync(t *testing.T) {
writeSynced(t, s, 1, 1) // moves the head: the next recovery fences again
syncs := 0 // the fence's fsyncs that returned
s.testAfterFenceFsync = func() { syncs++ }
t.Cleanup(func() { s.testAfterFenceFsync = nil })
var slices []uint64
var syncsAt []int
before := s.CheckpointPersistence()
// At each slice's fsync the counter still holds only the completed slices.
s.testBeforeFenceFsync = func() error {
var done uint64
for _, b := range slices {
done += b
}
if counted := s.fenceZeroBytes.Load() - before.FenceZeroed; counted != done {
t.Errorf("slice %d counted before its fsync: counted %d, completed %d", len(slices), counted, done)
}
return nil
}
t.Cleanup(func() { s.testAfterFenceFsync, s.testBeforeFenceFsync = nil, nil })
s.v3.fenceSliceHook = func(n uint64) {
counted := s.fenceZeroBytes.Load() - before.FenceZeroed // atomics: the hook runs under the store lock
var reported uint64
@@ -193,3 +204,51 @@ func damageOpenRecord(t *testing.T, s *WALStore, lsn uint64) {
}
t.Fatalf("no WAL record with LSN %d", lsn)
}
// An injected failure of the second slice's zero write, or of its fsync,
// stops the fence: the failing slice earns no counter or hook credit, the
// fence is not recorded as completed (no repeat skip can rest on it), and
// the store latches.
func TestWALStoreV3FenceSliceWriteOrFsyncFailure(t *testing.T) {
for _, step := range []string{"write", "fsync"} {
t.Run(step, func(t *testing.T) {
const walSize = 2*fenceSliceBytes + 1<<20
s, err := createMappedWALStoreV3ForTest(filepath.Join(t.TempDir(), "volume"), 4096, 4096, walSize, WModeLagged, 64)
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { _ = s.Close() })
s.DisableAutoFlushForRecoveryTest()
writeSynced(t, s, 1, 1)
ioErr := errors.New("injected fence " + step + " failure")
calls := 0
fail := func() error {
if calls++; calls == 2 {
return ioErr
}
return nil
}
if step == "write" {
s.testBeforeFenceWrite = fail
} else {
s.testBeforeFenceFsync = fail
}
hooked := 0
s.v3.fenceSliceHook = func(uint64) { hooked++ }
lastBefore := s.v3.lastFence
before := s.CheckpointPersistence()
if _, err := s.Recover(); !errors.Is(err, ioErr) {
t.Fatalf("recovery over a failing slice %s: %v", step, err)
}
if got := s.fenceZeroBytes.Load() - before.FenceZeroed; got != fenceSliceBytes || hooked != 1 {
t.Fatalf("after the 2nd slice's %s failed: zeroed %d (want one slice, %d), hook calls %d", step, got, fenceSliceBytes, hooked)
}
if s.v3.lastFence != lastBefore {
t.Fatalf("a fence whose slice %s failed was recorded as completed", step)
}
if s.stickyIOFault() == nil {
t.Fatalf("a failed fence slice %s did not latch the store", step)
}
})
}
}
+12
View File
@@ -822,6 +822,13 @@ func (s *WALStore) fenceV3(E, floor, walSize uint64, sameNonce bool, walOffset u
n := min(r.Len-done, fenceSliceBytes)
off := int64(walOffset + r.Off + done)
err := s.syncFile(func() error {
if h := s.testBeforeFenceWrite; h != nil {
if err := h(); err != nil {
err = fmt.Errorf("storage: v3 drop fence: %w", err)
s.latchV3(err)
return err
}
}
for w := uint64(0); w < n; {
k := min(n-w, uint64(len(zero)))
if _, err := s.fd.WriteAt(zero[:k], off+int64(w)); err != nil {
@@ -833,6 +840,11 @@ func (s *WALStore) fenceV3(E, floor, walSize uint64, sameNonce bool, walOffset u
}
return nil
}, func() error {
if h := s.testBeforeFenceFsync; h != nil {
if err := h(); err != nil {
return err
}
}
if err := s.fd.Sync(); err != nil {
return err
}