diff --git a/seaweed-block/core/storage/wal_admission.go b/seaweed-block/core/storage/wal_admission.go index 9c964b3529..9184811f4f 100644 --- a/seaweed-block/core/storage/wal_admission.go +++ b/seaweed-block/core/storage/wal_admission.go @@ -166,7 +166,7 @@ retry: continue retry } if hold != nil { - if err := hold.check(); err != nil { + if err := hold.checkFreed(a.belowHard(sample)); err != nil { return err } } @@ -248,6 +248,16 @@ retry: } } +// belowHard reports, from a fresh sample, whether the WAL is below the hard +// mark: the space a held command waits for is free even while the flush cycle +// that freed it has not yet recorded its reclaim. +func (a *walAdmission) belowHard(sample func() (float64, error)) func() bool { + return func() bool { + pressure, err := sample() + return err == nil && pressure < a.hardMark + } +} + // Release returns a write slot to the semaphore. func (a *walAdmission) Release() { <-a.sem diff --git a/seaweed-block/core/storage/wal_admission_fifo.go b/seaweed-block/core/storage/wal_admission_fifo.go index de6ec22f51..99a037b98b 100644 --- a/seaweed-block/core/storage/wal_admission_fifo.go +++ b/seaweed-block/core/storage/wal_admission_fifo.go @@ -117,7 +117,7 @@ func (a *walAdmission) waitForTicket(ctx context.Context, deadline *time.Timer, } else { hold.endWait() } - if err := hold.check(); err != nil { + if err := hold.checkFreed(a.belowHard(func() (float64, error) { return a.walUsed(), nil })); err != nil { return err } } diff --git a/seaweed-block/core/storage/wal_write_hold.go b/seaweed-block/core/storage/wal_write_hold.go index ca2c780b0e..facfd7e767 100644 --- a/seaweed-block/core/storage/wal_write_hold.go +++ b/seaweed-block/core/storage/wal_write_hold.go @@ -183,7 +183,14 @@ func (h *walWriteHold) endWait() { h.store.walHold.active.Add(-1) } -func (h *walWriteHold) check() error { +func (h *walWriteHold) check() error { return h.checkFreed(nil) } + +// checkFreed is check with one last look at the space a waiting command waits +// for. At the stall boundary, freed (a fresh WAL sample below the hard mark) +// counts as progress: a flush cycle publishes the freed space before it records +// its reclaimed bytes, so the counters can lag the space by the rest of that +// cycle. A fault or the command cap still answers first. +func (h *walWriteHold) checkFreed(freed func() bool) error { if fault := h.store.stickyIOFault(); fault != nil { return fault } @@ -211,6 +218,10 @@ func (h *walWriteHold) check() error { return ErrWALHoldCap case walReclaimStalled: if h.waiting { + if freed != nil && freed() { + h.window.observeProgress(now) + return nil + } h.reason = walReclaimStalled return ErrWALReclaimStalled } diff --git a/seaweed-block/core/storage/write_command_budget.go b/seaweed-block/core/storage/write_command_budget.go index 1ce99baf25..ac25d0970e 100644 --- a/seaweed-block/core/storage/write_command_budget.go +++ b/seaweed-block/core/storage/write_command_budget.go @@ -32,6 +32,12 @@ func (s *WALStore) writeAdmissionDeadline(ctx context.Context) time.Time { } func (s *WALStore) writeBudgetError(ctx context.Context, deadline time.Time) error { + return s.writeBudgetErrorFreed(ctx, deadline, nil) +} + +// writeBudgetErrorFreed is writeBudgetError with the hold's last look at the +// freed space (walWriteHold.checkFreed). +func (s *WALStore) writeBudgetErrorFreed(ctx context.Context, deadline time.Time, freed func() bool) error { if err := s.stickyIOFault(); err != nil { return err } @@ -39,7 +45,7 @@ func (s *WALStore) writeBudgetError(ctx context.Context, deadline time.Time) err return context.Canceled } if h := holdFor(ctx, s); h != nil { - if err := h.check(); err != nil { + if err := h.checkFreed(freed); err != nil { return err } } @@ -95,27 +101,36 @@ func (s *WALStore) writePressureSample(ctx context.Context, deadline time.Time) // checked once they are held. s.mu.RLock() defer s.mu.RUnlock() - if err := s.writeBudgetError(ctx, deadline); err != nil { + // The space is read before the budget answers: a stall is not answered + // while the WAL is already below the hard mark (walWriteHold.checkFreed). + freed := func() bool { return s.admission != nil && s.walPressureRLocked() < s.admission.hardMark } + if err := s.writeBudgetErrorFreed(ctx, deadline, freed); err != nil { return 0, err } if s.closed { return 0, errVolumeClosed } - v := s.v3 - w := v.w - if w == nil { - return 0, nil - } // append still refuses the missing binding - p := v.pub - p.descMu.Lock() - floor, valid := p.reuseFloorLocked() - d := p.cur - p.descMu.Unlock() // preserve snapshot ordering: no descriptor -> writer edge - w.mu.Lock() - defer w.mu.Unlock() - if !valid || d.WALSize == 0 || d.Nonce != w.nonce || w.head < floor || w.head-floor > d.WALSize { - return 0, nil - } - return float64(w.head-floor) / float64(d.WALSize), nil + return s.walPressureRLocked(), nil } } + +// walPressureRLocked is the V3 WAL fill fraction; s.mu is read-locked. A +// missing binding or an inconsistent snapshot reads 0 (append still refuses it). +func (s *WALStore) walPressureRLocked() float64 { + v := s.v3 + w := v.w + if w == nil { + return 0 + } + p := v.pub + p.descMu.Lock() + floor, valid := p.reuseFloorLocked() + d := p.cur + p.descMu.Unlock() // preserve snapshot ordering: no descriptor -> writer edge + w.mu.Lock() + defer w.mu.Unlock() + if !valid || d.WALSize == 0 || d.Nonce != w.nonce || w.head < floor || w.head-floor > d.WALSize { + return 0 + } + return float64(w.head-floor) / float64(d.WALSize) +}