diff --git a/seaweed-block/core/storage/walstore.go b/seaweed-block/core/storage/walstore.go index ac725f505e..c99ab8bb6e 100644 --- a/seaweed-block/core/storage/walstore.go +++ b/seaweed-block/core/storage/walstore.go @@ -759,7 +759,9 @@ func (s *WALStore) Write(lba uint32, data []byte) (uint64, error) { func (s *WALStore) WriteContext(ctx context.Context, lba uint32, data []byte) (uint64, error) { deadline := s.writeAdmissionDeadline(ctx) - s.captureMu.RLock() + if err := s.acquireWriteLock(ctx, deadline, s.captureMu.TryRLock, s.captureMu.RLock); err != nil { + return 0, err + } defer s.captureMu.RUnlock() if err := s.stickyIOFault(); err != nil { return 0, err @@ -776,7 +778,9 @@ func (s *WALStore) WriteContext(ctx context.Context, lba uint32, data []byte) (u // admission Acquire may block up to admissionTimeout — under // hard pressure it spins waiting for the flusher to drain. s.beforeChunkMutation(uint64(lba), 1) - s.lifecycleMu.RLock() + if err := s.acquireWriteLock(ctx, deadline, s.lifecycleMu.TryRLock, s.lifecycleMu.RLock); err != nil { + return 0, err + } defer s.lifecycleMu.RUnlock() if err := s.writeBudgetError(ctx, deadline); err != nil { @@ -797,7 +801,9 @@ func (s *WALStore) WriteContext(ctx context.Context, lba uint32, data []byte) (u } commitLockStart := time.Now() - s.mu.Lock() + if err := s.acquireWriteLock(ctx, deadline, s.mu.TryLock, s.mu.Lock); err != nil { + return 0, err + } s.instr.recordWriteCommitLockWait(time.Since(commitLockStart)) if s.closed { s.mu.Unlock() @@ -862,7 +868,9 @@ func (s *WALStore) WriteBatch(startLBA uint32, blocks [][]byte) ([]uint64, error func (s *WALStore) WriteBatchContext(ctx context.Context, startLBA uint32, blocks [][]byte) ([]uint64, error) { deadline := s.writeAdmissionDeadline(ctx) - s.captureMu.RLock() + if err := s.acquireWriteLock(ctx, deadline, s.captureMu.TryRLock, s.captureMu.RLock); err != nil { + return nil, err + } defer s.captureMu.RUnlock() if err := s.stickyIOFault(); err != nil { return nil, err @@ -881,7 +889,9 @@ func (s *WALStore) WriteBatchContext(ctx context.Context, startLBA uint32, block } s.beforeChunkMutation(uint64(startLBA), uint64(len(blocks))) - s.lifecycleMu.RLock() + if err := s.acquireWriteLock(ctx, deadline, s.lifecycleMu.TryRLock, s.lifecycleMu.RLock); err != nil { + return nil, err + } defer s.lifecycleMu.RUnlock() if err := s.writeBudgetError(ctx, deadline); err != nil { @@ -902,7 +912,9 @@ func (s *WALStore) WriteBatchContext(ctx context.Context, startLBA uint32, block } commitLockStart := time.Now() - s.mu.Lock() + if err := s.acquireWriteLock(ctx, deadline, s.mu.TryLock, s.mu.Lock); err != nil { + return nil, err + } s.instr.recordWriteCommitLockWait(time.Since(commitLockStart)) if s.closed { s.mu.Unlock() diff --git a/seaweed-block/core/storage/write_command_budget.go b/seaweed-block/core/storage/write_command_budget.go index 316303e3dd..a21c22233c 100644 --- a/seaweed-block/core/storage/write_command_budget.go +++ b/seaweed-block/core/storage/write_command_budget.go @@ -37,3 +37,33 @@ func (s *WALStore) writeBudgetError(ctx context.Context, deadline time.Time) err } return nil } + +// A parked command owns no new physical I/O. Stop waiting for admission locks +// on expiry without a helper goroutine that could acquire them after return. +func (s *WALStore) acquireWriteLock(ctx context.Context, deadline time.Time, try func() bool, lock func()) error { + if deadline.IsZero() && ctx.Done() == nil { + lock() + return nil + } + if err := s.writeBudgetError(ctx, deadline); err != nil { + return err + } + if try() { + return nil + } + tick := time.NewTicker(time.Millisecond) + defer tick.Stop() + for { + if err := s.writeBudgetError(ctx, deadline); err != nil { + return err + } + if try() { + return nil + } + select { + case <-ctx.Done(): + return s.writeBudgetError(ctx, deadline) + case <-tick.C: + } + } +} diff --git a/seaweed-block/core/storage/write_command_budget_test.go b/seaweed-block/core/storage/write_command_budget_test.go index ca9a88d4cb..606c1528fe 100644 --- a/seaweed-block/core/storage/write_command_budget_test.go +++ b/seaweed-block/core/storage/write_command_budget_test.go @@ -54,8 +54,7 @@ func TestWALStoreV3WriteContextDeadlineIncludesCaptureWait(t *testing.T) { } done <- err }() - <-ctx.Done() - s.captureMu.Unlock() + defer s.captureMu.Unlock() select { case err := <-done: if !errors.Is(err, ErrWALPressureTimeout) { @@ -81,3 +80,26 @@ func TestWALStoreV3WriteBudgetKeepsTerminalFault(t *testing.T) { t.Fatalf("fault hidden by deadline: %v", err) } } + +func TestWALStoreV3WriteBudgetBoundsAppendLockWait(t *testing.T) { + s, _ := newMappedCheckpointStoreV3(t) + ctx, cancel := context.WithTimeout(context.Background(), 30*time.Millisecond) + defer cancel() + s.mu.Lock() + done := make(chan error, 1) + go func() { _, err := s.WriteContext(ctx, 0, blk(1)); done <- err }() + select { + case err := <-done: + if !errors.Is(err, ErrWALPressureTimeout) { + s.mu.Unlock() + t.Fatal(err) + } + case <-time.After(time.Second): + s.mu.Unlock() + t.Fatal("expired writer still waits for append lock") + } + s.mu.Unlock() + if s.NextLSN() != 1 || s.dm.len() != 0 { + t.Fatal("expired append waiter mutated") + } +}