storage: #959 tests: contested arrival, a busy-barrier witness, a failed held sync

The gate test runs in a bounded child, sees each side reach the contested
boundary (the hold pending on the gate, the new extent write counted as
waiting) before asserting, and releases and joins on every exit. A new red
holds a capture reader without WAL pressure: the barrier must give the
hold up while that reader is still held and report barrier_busy. A new
control fails the held last pass's own sync (a test-only fault seam) and
requires that error, a released hold and extent draining again; the
cancellation control stays.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
pingqiuandClaude Opus 5.5 committed 2026-12-02 11:43:00 -08:00
1 parent e0be768983
commit 419f2b65b5
2 files changed
+148 -11

No files matched your search

@@ -2,7 +2,9 @@ package storage
import (
"context"
"errors"
"path/filepath"
"sync"
"sync/atomic"
"testing"
"time"
@@ -329,33 +331,75 @@ func TestAHeldFlusherWritesThroughForAWriterAtTheHardMark(t *testing.T) {
// The hold is serialized with extent-write admission: an extent write the
// flusher was admitted to before the hold is finished (and counted) before
// holdExtentWrites returns, so the last pass's baseline cannot miss it; one
// asked for after the hold waits until the release. (A check-only hold
// returned at once, with that write still to land in the held pass.)
// asked for after the hold waits until the release. Each side is first seen
// at the contested boundary: the hold pending on the gate (a pending writer
// makes TryRLock fail) and the new write counted as waiting. (A check-only
// hold returned at once, with that write still to land in the held pass.)
func TestAHoldWaitsForAnAdmittedExtentWriteAndHoldsNewOnes(t *testing.T) {
if !inBoundedChild(t) {
if err := runBoundedChild(t.Name(), boundedChildBound); err != nil {
t.Fatal(err)
}
return
}
s, _ := newChunkTestStore(t)
stop := make(chan struct{})
defer close(stop)
admitted := s.admitExtentWrite(stop) // the flusher, admitted and mid-write
var once sync.Once
finish := func() { once.Do(admitted) }
held := make(chan func(), 1)
go func() { held <- s.holdExtentWrites() }()
select {
case <-held:
t.Fatal("the hold returned while an admitted extent write was still in flight")
case <-time.After(20 * time.Millisecond):
}
admitted()
var release func()
t.Cleanup(func() { // every exit: the admitted write ends, the hold is released, waiters stop
finish()
if release == nil {
select {
case release = <-held:
case <-time.After(ctLocalBound):
}
}
if release != nil {
release()
}
close(stop)
})
for deadline := time.Now().Add(ctLocalBound); ; time.Sleep(100 * time.Microsecond) {
select {
case release = <-held:
t.Fatal("the hold returned while an admitted extent write was still in flight")
default:
}
if !s.extentGate.TryRLock() {
break // the hold is pending on the gate, behind the admitted write
}
s.extentGate.RUnlock()
if time.Now().After(deadline) {
t.Fatal("setup: the hold never reached the gate")
}
}
finish()
select {
case release = <-held:
case <-time.After(ctLocalBound):
t.Fatal("the hold did not return after the admitted write finished")
}
waits := s.extentHeldWaits.Load()
next := make(chan struct{})
go func() { s.admitExtentWrite(stop)(); close(next) }()
for deadline := time.Now().Add(ctLocalBound); s.extentHeldWaits.Load() == waits; time.Sleep(100 * time.Microsecond) {
select {
case <-next:
t.Fatal("an extent write was admitted while the hold was on")
default:
}
if time.Now().After(deadline) {
t.Fatal("setup: the new extent write never reached the hold")
}
}
select {
case <-next:
t.Fatal("an extent write was admitted while the hold was on")
case <-time.After(20 * time.Millisecond):
default:
}
release()
select {
@@ -363,7 +407,54 @@ func TestAHoldWaitsForAnAdmittedExtentWriteAndHoldsNewOnes(t *testing.T) {
case <-time.After(ctLocalBound):
t.Fatal("the held extent write did not resume after the release")
}
release() // idempotent
}
// A capture reader the barrier cannot get past within its short try, with no
// WAL pressure: the barrier gives the hold up while that reader is still
// held (it never waits for it with the flusher stopped), then takes its lock
// once the reader leaves; the cut completes and reports barrier_busy. (A
// barrier that simply waited for its lock kept the hold on until the reader
// left.)
func TestABusyBarrierGivesItsHoldUpBeforeWaiting(t *testing.T) {
if !inBoundedChild(t) {
if err := runBoundedChild(t.Name(), boundedChildBound); err != nil {
t.Fatal(err)
}
return
}
s, l := hardMarkStore(t)
releasedWhileHeld := make(chan bool, 1)
l.testBeforeBarrierLockHook = func() { // the hold is on; the barrier is next
l.testBeforeBarrierLockHook = nil
s.captureMu.RLock() // a capture reader, as a guest write in flight holds it
go func() {
defer s.captureMu.RUnlock()
deadline := time.Now().Add(ctLocalBound)
for s.extentHeld.Load() && time.Now().Before(deadline) {
time.Sleep(100 * time.Microsecond)
}
releasedWhileHeld <- !s.extentHeld.Load()
}()
}
if _, err := s.Write(0, makeBlock(ctBlockSize, 0x6a)); err != nil {
t.Fatal(err)
}
r, err := l.Cut(ctTimeout(t))
if err != nil {
t.Fatal(err)
}
select {
case ok := <-releasedWhileHeld:
if !ok {
t.Fatal("the barrier kept the hold on while it waited for a capture reader")
}
case <-time.After(ctLocalBound):
t.Fatal("the reader never reported")
}
st := r.Stats
if !st.TailOverBound || st.TailCause != "barrier_busy" {
t.Fatalf("the finding: over=%v cause=%q, want barrier_busy", st.TailOverBound, st.TailCause)
}
}
// A cut that fails after the hold is on (its context ends during the held
@@ -404,3 +495,43 @@ func TestACutThatFailsInItsHeldPassReleasesTheFlusher(t *testing.T) {
}
}
}
// A held last pass whose own sync fails: the cut returns that error, the
// hold is released on the way out, and the flusher drains again.
func TestACutWhoseHeldSyncFailsReleasesTheFlusher(t *testing.T) {
if !inBoundedChild(t) {
if err := runBoundedChild(t.Name(), boundedChildBound); err != nil {
t.Fatal(err)
}
return
}
s, l := hardMarkStore(t)
injected := errors.New("injected held-pass sync failure")
var failed atomic.Bool
s.testSyncCacheFault = func() error {
if !l.paused && s.extentHeld.Load() && failed.CompareAndSwap(false, true) {
return injected
}
return nil
}
t.Cleanup(func() { s.testSyncCacheFault = nil })
if _, err := s.Write(0, makeBlock(ctBlockSize, 0x5c)); err != nil {
t.Fatal(err)
}
if _, err := l.Cut(ctTimeout(t)); !errors.Is(err, injected) {
t.Fatalf("the cut's answer: %v, want the held sync's failure", err)
}
if s.extentHeld.Load() {
t.Fatal("the failed cut left the flusher's extent writes held")
}
before := s.flusher.instr.extentWriteBytes.Load()
if _, err := s.Write(1, makeBlock(ctBlockSize, 0x5d)); err != nil {
t.Fatal(err)
}
for deadline := time.Now().Add(ctLocalBound); s.flusher.instr.extentWriteBytes.Load() == before; time.Sleep(time.Millisecond) {
s.flusher.Notify()
if time.Now().After(deadline) {
t.Fatal("the flusher wrote no extent after the failed cut")
}
}
}
+6
View File
@@ -170,6 +170,7 @@ type WALStore struct {
// is taken. Tests only.
testBeforeFileSync func()
testBeforeSyncCache func() // before a store Sync's file sync (syncWithPhases); tests only
testSyncCacheFault func() error // after it: a non-nil error fails that Sync (nothing latched); tests only
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
@@ -891,6 +892,11 @@ func (s *WALStore) syncWithPhases(phases *SyncPhaseStats) (uint64, error) {
if h := s.testBeforeSyncCache; h != nil {
h() // tests only: a stand-in for write-back that grows with what is dirty
}
if f := s.testSyncCacheFault; f != nil {
if err := f(); err != nil {
return 0, fmt.Errorf("storage: group commit fsync: %w", err)
}
}
if err := s.syncCache(); err != nil {
return 0, fmt.Errorf("storage: group commit fsync: %w", err)
}