diff --git a/seaweed-block/core/frontend/durable/storage_adapter.go b/seaweed-block/core/frontend/durable/storage_adapter.go index 06c74be304..fa45714338 100644 --- a/seaweed-block/core/frontend/durable/storage_adapter.go +++ b/seaweed-block/core/frontend/durable/storage_adapter.go @@ -1314,8 +1314,8 @@ func expiredCommand(ctx context.Context) error { // backend callers. The context carries one identity through all store batches. func (b *StorageBackend) WriteCommandContext(ctx context.Context, arrival time.Time) (context.Context, context.CancelFunc) { if s, ok := b.storage.(*storage.WALStore); ok { - ctx = storage.WithWALWriteArrival(ctx, s, arrival) if frontend.UsesLegacyWriteBudget(ctx) { + ctx = storage.WithWALWriteArrival(ctx, s, arrival) return frontend.WithWriteBudget(ctx, arrival) } cfg := b.walHold diff --git a/seaweed-block/core/storage/wal_admission.go b/seaweed-block/core/storage/wal_admission.go index 8f161c370a..9c964b3529 100644 --- a/seaweed-block/core/storage/wal_admission.go +++ b/seaweed-block/core/storage/wal_admission.go @@ -39,7 +39,7 @@ type walAdmission struct { queueMu sync.Mutex first, last *walAdmissionTicket active *walAdmissionTicket // an admitted append cannot be preempted - nextOrder uint64 + nextOrder atomic.Uint64 headVersion uint64 sem chan struct{} // counting semaphore for concurrent appenders walUsed func() float64 diff --git a/seaweed-block/core/storage/wal_admission_fifo.go b/seaweed-block/core/storage/wal_admission_fifo.go index 376c76d974..de6ec22f51 100644 --- a/seaweed-block/core/storage/wal_admission_fifo.go +++ b/seaweed-block/core/storage/wal_admission_fifo.go @@ -15,12 +15,7 @@ type walAdmissionTicket struct { order uint64 } -func (a *walAdmission) newCommandOrder() uint64 { - a.queueMu.Lock() - defer a.queueMu.Unlock() - a.nextOrder++ - return a.nextOrder -} +func (a *walAdmission) newCommandOrder() uint64 { return a.nextOrder.Add(1) } // Pending batches retain their command arrival order. An append that already // owns capacity remains pinned at the head until its store call returns. @@ -31,8 +26,7 @@ func (a *walAdmission) enqueueTicket(arrival *walWriteArrival) *walAdmissionTick if arrival != nil { t.arrival, t.order = arrival.arrival, arrival.order } else { - a.nextOrder++ - t.order = a.nextOrder + t.order = a.newCommandOrder() } priorFirst := a.first before := a.last diff --git a/seaweed-block/core/storage/wal_write_hold.go b/seaweed-block/core/storage/wal_write_hold.go index 207b605b85..ca2c780b0e 100644 --- a/seaweed-block/core/storage/wal_write_hold.go +++ b/seaweed-block/core/storage/wal_write_hold.go @@ -20,12 +20,7 @@ type walWriteArrival struct { order uint64 } -// WithWALWriteArrival preserves command ordering across sequential store calls, -// independently of the transport's pressure timeout policy. It takes no ticket. -func WithWALWriteArrival(ctx context.Context, s *WALStore, arrival time.Time) context.Context { - if v, _ := ctx.Value(walWriteArrivalKey{}).(*walWriteArrival); v != nil && v.store == s { - return ctx - } +func newWALWriteArrival(s *WALStore, arrival time.Time) walWriteArrival { if arrival.IsZero() { arrival = time.Now() } @@ -33,14 +28,36 @@ func WithWALWriteArrival(ctx context.Context, s *WALStore, arrival time.Time) co if s.admission != nil { order = s.admission.newCommandOrder() } - return context.WithValue(ctx, walWriteArrivalKey{}, &walWriteArrival{store: s, arrival: arrival, order: order}) + return walWriteArrival{store: s, arrival: arrival, order: order} +} + +func writeArrivalFor(ctx context.Context, s *WALStore) *walWriteArrival { + if v, _ := ctx.Value(walWriteArrivalKey{}).(*walWriteArrival); v != nil && v.store == s { + return v + } + if h := holdFor(ctx, s); h != nil { + return &h.walWriteArrival + } + return nil +} + +// WithWALWriteArrival preserves command ordering across sequential store calls, +// independently of the transport's pressure timeout policy. It takes no ticket. +func WithWALWriteArrival(ctx context.Context, s *WALStore, arrival time.Time) context.Context { + if writeArrivalFor(ctx, s) != nil { + return ctx + } + v := newWALWriteArrival(s, arrival) + return context.WithValue(ctx, walWriteArrivalKey{}, &v) } func admissionArrival(ctx context.Context, a *walAdmission) *walWriteArrival { - v, _ := ctx.Value(walWriteArrivalKey{}).(*walWriteArrival) - if v != nil && v.store.admission == a { + if v, _ := ctx.Value(walWriteArrivalKey{}).(*walWriteArrival); v != nil && v.store.admission == a { return v } + if h := admissionHold(ctx, a); h != nil { + return &h.walWriteArrival + } return nil } @@ -49,12 +66,11 @@ type walWriteHoldKey struct{} // A command owns this state across its sequential store batches. Only the // command's write goroutine observes progress or finishes the observations. type walWriteHold struct { + walWriteArrival waiting bool waitStarted time.Time waitNanos int64 reason walHoldReason - store *WALStore - arrival time.Time maxHold time.Duration final bool window walHoldWindow @@ -71,9 +87,14 @@ func WithWALWriteHold(ctx context.Context, s *WALStore, arrival time.Time, maxHo if h, _ := ctx.Value(walWriteHoldKey{}).(*walWriteHold); h != nil { return ctx, func() {} } - ctx = WithWALWriteArrival(ctx, s, arrival) - arrival = admissionArrival(ctx, s.admission).arrival - h := &walWriteHold{store: s, arrival: arrival, maxHold: maxHold, final: final} + var command walWriteArrival + if existing := writeArrivalFor(ctx, s); existing != nil { + command = *existing + } else { + command = newWALWriteArrival(s, arrival) + } + arrival = command.arrival + h := &walWriteHold{walWriteArrival: command, maxHold: maxHold, final: final} h.window = newWALHoldWindow(arrival, time.Now(), maxHold) if final { h.window = newWALNoRetryHoldWindow(arrival, time.Now(), maxHold)