From 4ab1a426b04b9e037bba87af2ba715f8a09ca697 Mon Sep 17 00:00:00 2001 From: pingqiu Date: Wed, 29 Jul 2026 21:56:45 -0700 Subject: [PATCH] phase168: prioritize durability barriers between rounds --- .../storage/parallelwal/native_submitter.go | 71 +++++++++++++------ .../native_submitter_linux_amd64_test.go | 70 ++++++++++++++++++ .../internal/iouring/probe_linux_amd64.go | 26 ++++--- .../iouring/probe_linux_amd64_test.go | 33 +++++++++ ...run-phase168-native-wal-durability-gate.sh | 13 +++- 5 files changed, 181 insertions(+), 32 deletions(-) diff --git a/seaweed-block/core/storage/parallelwal/native_submitter.go b/seaweed-block/core/storage/parallelwal/native_submitter.go index cc462b43f6..359b433016 100644 --- a/seaweed-block/core/storage/parallelwal/native_submitter.go +++ b/seaweed-block/core/storage/parallelwal/native_submitter.go @@ -133,37 +133,64 @@ func (submitter *nativeWALSubmitter) run() { defer close(submitter.done) for { select { - case <-submitter.wake: - _ = submitter.drainQueued() case result := <-submitter.barriers: - // Store.Sync waits for its target LSN to publish before sending - // this request, so queued later writes are outside the fence. - err := submitter.submitBarrier() - if err != nil { - submitter.failBatchesAndQueues(nil, err) + submitter.handleBarrier(result) + continue + default: + } + select { + case <-submitter.wake: + more, err := submitter.processOneRound() + if err == nil && more { + submitter.notify() } - result <- err + case result := <-submitter.barriers: + submitter.handleBarrier(result) case <-submitter.stop: return } } } -func (submitter *nativeWALSubmitter) drainQueued() error { - for { - batches, err := submitter.takeRound() - if err != nil { - submitter.failBatchesAndQueues(batches, err) - return err - } - if len(batches) == 0 { - return nil - } - if err := submitter.submitRound(batches); err != nil { - submitter.failBatchesAndQueues(batches, err) - return err - } +func (submitter *nativeWALSubmitter) handleBarrier(result chan error) { + // Store.Sync waits for its target LSN to publish before sending this + // request, so queued later writes are outside the fence. + err := submitter.submitBarrier() + if err != nil { + submitter.failBatchesAndQueues(nil, err) } + result <- err +} + +func (submitter *nativeWALSubmitter) processOneRound() (bool, error) { + batches, err := submitter.takeRound() + if err != nil { + submitter.failBatchesAndQueues(batches, err) + return false, err + } + if len(batches) == 0 { + return false, nil + } + if err := submitter.submitRound(batches); err != nil { + submitter.failBatchesAndQueues(batches, err) + return false, err + } + return submitter.hasQueued(), nil +} + +func (submitter *nativeWALSubmitter) hasQueued() bool { + hasQueued := false + for _, lane := range submitter.store.lanes { + lane.mu.Lock() + if len(lane.queue) == 0 { + lane.queue = nil + lane.draining = false + } else { + hasQueued = true + } + lane.mu.Unlock() + } + return hasQueued } func (submitter *nativeWALSubmitter) sync() error { diff --git a/seaweed-block/core/storage/parallelwal/native_submitter_linux_amd64_test.go b/seaweed-block/core/storage/parallelwal/native_submitter_linux_amd64_test.go index e88e916cb7..100cb7ac62 100644 --- a/seaweed-block/core/storage/parallelwal/native_submitter_linux_amd64_test.go +++ b/seaweed-block/core/storage/parallelwal/native_submitter_linux_amd64_test.go @@ -4,7 +4,10 @@ package parallelwal import ( "path/filepath" + "sync" + "sync/atomic" "testing" + "time" ) func TestIOUringOwnerBatchesAcrossLanesAndRecoversPortably(t *testing.T) { @@ -108,3 +111,70 @@ func TestIOUringOwnerRotatesAcrossLanesAtDepthOne(t *testing.T) { t.Fatalf("depth-one native stats=%+v", stats) } } + +func TestNativeSyncIsNotStarvedByLaterWriters(t *testing.T) { + path := filepath.Join(t.TempDir(), "sync-liveness.bin") + cfg := testConfig() + cfg.SlotsPerLane = 4096 + cfg.RetainPerLane = 2048 + cfg.QueueDepth = 64 + cfg.Execution = ExecutionIOUring + store, err := CreateStoreWithConfig(path, cfg) + if err != nil { + t.Fatal(err) + } + + var writes atomic.Uint64 + stop := make(chan struct{}) + var writers sync.WaitGroup + for writer := 0; writer < 8; writer++ { + writers.Add(1) + go func(writer int) { + defer writers.Done() + data := testBlock(byte(writer+1), cfg.BlockSize) + for { + select { + case <-stop: + return + default: + if _, err := store.Write(uint32(writer), data); err != nil { + return + } + writes.Add(1) + } + } + }(writer) + } + + deadline := time.Now().Add(2 * time.Second) + for writes.Load() < 32 && time.Now().Before(deadline) { + time.Sleep(time.Millisecond) + } + if writes.Load() < 32 { + close(stop) + writers.Wait() + _ = store.Close() + t.Fatal("writers did not establish continuous load") + } + + syncDone := make(chan error, 1) + go func() { + _, err := store.Sync() + syncDone <- err + }() + select { + case err := <-syncDone: + if err != nil { + t.Fatalf("Sync under later writes: %v", err) + } + case <-time.After(2 * time.Second): + close(stop) + writers.Wait() + t.Fatal("Sync was starved by later writes") + } + close(stop) + writers.Wait() + if err := store.Close(); err != nil { + t.Fatal(err) + } +} diff --git a/seaweed-block/internal/iouring/probe_linux_amd64.go b/seaweed-block/internal/iouring/probe_linux_amd64.go index a851c14260..ddc3ac23b8 100644 --- a/seaweed-block/internal/iouring/probe_linux_amd64.go +++ b/seaweed-block/internal/iouring/probe_linux_amd64.go @@ -11,6 +11,7 @@ import ( "strconv" "strings" "sync/atomic" + "time" "unsafe" "golang.org/x/sys/unix" @@ -457,19 +458,25 @@ func (ring *ioUring) submitAndWait(operations []ringOperation) ([]ioUringCQE, in if submitted == 0 { return nil, 0, err } - completions := ring.waitForAccepted(submitted) - return completions, submitted, err + completions, waitErr := ring.waitForAccepted(submitted) + return completions, submitted, errors.Join(err, waitErr) } if submitted == before { if submitted == 0 { return nil, 0, errors.New("io_uring_enter submitted zero operations") } - completions := ring.waitForAccepted(submitted) - return completions, submitted, errors.New("io_uring_enter submitted zero operations") + completions, waitErr := ring.waitForAccepted(submitted) + return completions, submitted, errors.Join( + errors.New("io_uring_enter submitted zero operations"), + waitErr, + ) } } - completions := ring.waitForAccepted(len(operations)) + completions, waitErr := ring.waitForAccepted(len(operations)) + if waitErr != nil { + return completions, submitted, waitErr + } if len(completions) != len(operations) { return completions, submitted, fmt.Errorf( "completion count=%d want=%d", @@ -483,19 +490,22 @@ func (ring *ioUring) submitAndWait(operations []ringOperation) ([]ioUringCQE, in // waitForAccepted does not return until every accepted SQE has a terminal CQE. // The registered eventfd separates completion wakeup from io_uring_enter, so a // GETEVENTS syscall failure cannot strand accepted buffers. -func (ring *ioUring) waitForAccepted(expected int) []ioUringCQE { +func (ring *ioUring) waitForAccepted(expected int) ([]ioUringCQE, error) { completions := make([]ioUringCQE, 0, expected) + var wakeErr error for len(completions) < expected { completions = append(completions, ring.drainCompletions()...) if len(completions) >= expected { ring.consumeCompletionEvent() break } - if err := ring.waitCompletionEvent(); err != nil { + if wakeErr != nil { + time.Sleep(time.Millisecond) continue } + wakeErr = ring.waitCompletionEvent() } - return completions + return completions, wakeErr } func (ring *ioUring) waitCompletionEvent() error { diff --git a/seaweed-block/internal/iouring/probe_linux_amd64_test.go b/seaweed-block/internal/iouring/probe_linux_amd64_test.go index f240e2a1ea..3a6e63b97b 100644 --- a/seaweed-block/internal/iouring/probe_linux_amd64_test.go +++ b/seaweed-block/internal/iouring/probe_linux_amd64_test.go @@ -4,7 +4,9 @@ package iouring import ( "errors" + "sync/atomic" "testing" + "time" "unsafe" "golang.org/x/sys/unix" @@ -79,3 +81,34 @@ func TestEnterRetriesInterruptedWait(t *testing.T) { t.Fatal("non-EINTR error was unexpectedly retried as success") } } + +func TestEventFDWaitErrorDrainsCQBeforeReturning(t *testing.T) { + ring, err := newIOUring(4) + if err != nil { + t.Fatal(err) + } + registeredEventFD := ring.eventFD + ring.eventFD = 1 << 30 + defer func() { + ring.close() + _ = unix.Close(registeredEventFD) + }() + + go func() { + time.Sleep(5 * time.Millisecond) + tail := atomic.LoadUint32(ring.cqTail) + ring.cqes[tail&atomic.LoadUint32(ring.cqMask)] = ioUringCQE{ + UserData: 91, + Result: probeBlockSize, + } + atomic.StoreUint32(ring.cqTail, tail+1) + }() + + completions, waitErr := ring.waitForAccepted(1) + if waitErr == nil { + t.Fatal("invalid eventfd wait unexpectedly succeeded") + } + if len(completions) != 1 || completions[0].UserData != 91 { + t.Fatalf("completions=%+v want one terminal CQE", completions) + } +} diff --git a/seaweed-block/scripts/run-phase168-native-wal-durability-gate.sh b/seaweed-block/scripts/run-phase168-native-wal-durability-gate.sh index f4d9e516d8..148385bc17 100755 --- a/seaweed-block/scripts/run-phase168-native-wal-durability-gate.sh +++ b/seaweed-block/scripts/run-phase168-native-wal-durability-gate.sh @@ -34,14 +34,16 @@ if [[ "$(go env GOOS)/$(go env GOARCH)" != "linux/amd64" ]]; then fi timeout 60s go test ./core/storage/parallelwal \ - -run '^TestIOUringOwnerBatchesAcrossLanesAndRecoversPortably$' \ + -run 'TestIOUringOwnerBatchesAcrossLanesAndRecoversPortably|TestNativeSyncIsNotStarvedByLaterWriters' \ -count=1 -v >"${TEST_LOG}" 2>&1 require_line '^--- PASS: TestIOUringOwnerBatchesAcrossLanesAndRecoversPortably ' "${TEST_LOG}" +require_line '^--- PASS: TestNativeSyncIsNotStarvedByLaterWriters ' "${TEST_LOG}" require_line 'native_durability_stats barriers=2 fsync_completions=2 submit_syscalls=[1-9][0-9]*' "${TEST_LOG}" write_summary "target_lsn_barrier=pass" write_summary "durability_barriers=2" write_summary "fsync_completions=2" write_summary "portable_reopen_recovery=pass" +write_summary "sync_under_continuous_later_writes=pass" timeout 60s go test ./core/storage/parallelwal \ -run '^TestNativeFsyncFailureTerminallyRejectsLaterWrites$' \ @@ -52,6 +54,13 @@ write_summary "fsync_failure_terminal=true" write_summary "later_write_after_fsync_failure=denied" write_summary "close_reports_terminal_failure=true" +timeout 60s go test ./internal/iouring \ + -run '^TestEventFDWaitErrorDrainsCQBeforeReturning$' \ + -count=1 -v >"${ARTIFACT_DIR}/eventfd-error-test.log" 2>&1 +require_line '^--- PASS: TestEventFDWaitErrorDrainsCQBeforeReturning ' \ + "${ARTIFACT_DIR}/eventfd-error-test.log" +write_summary "eventfd_error_terminal_drain=pass" + timeout 120s go test -race \ ./internal/iouring \ ./core/storage/parallelwal \ @@ -75,7 +84,7 @@ if command -v strace >/dev/null 2>&1; then echo "completion path unexpectedly used IORING_ENTER_GETEVENTS" >&2 exit 1 fi - write_summary "completion_wakeup=eventfd" + write_summary "completion_notification_registration=eventfd" write_summary "getevents_wait_calls=0" write_summary "external_native_syscall_validation=strace" else