diff --git a/seaweed-block/core/storage/chunk_cut_presync_test.go b/seaweed-block/core/storage/chunk_cut_presync_test.go index 988e125986..0055173a7a 100644 --- a/seaweed-block/core/storage/chunk_cut_presync_test.go +++ b/seaweed-block/core/storage/chunk_cut_presync_test.go @@ -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") + } + } +} diff --git a/seaweed-block/core/storage/walstore.go b/seaweed-block/core/storage/walstore.go index 75e5a5587d..6b24288997 100644 --- a/seaweed-block/core/storage/walstore.go +++ b/seaweed-block/core/storage/walstore.go @@ -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) }