From 3c7cef087dd8a907cc47c3da1c6eb8b57aeb6eec Mon Sep 17 00:00:00 2001 From: pingqiu Date: Thu, 19 Nov 2026 09:58:21 -0800 Subject: [PATCH] frontend: preserve one arrival deadline through reads and hydration RMW --- .../core/frontend/durable/storage_adapter.go | 63 +++++++++++++++++-- seaweed-block/core/frontend/nvme/session.go | 6 ++ seaweed-block/core/frontend/read_budget.go | 14 +++++ 3 files changed, 78 insertions(+), 5 deletions(-) create mode 100644 seaweed-block/core/frontend/read_budget.go diff --git a/seaweed-block/core/frontend/durable/storage_adapter.go b/seaweed-block/core/frontend/durable/storage_adapter.go index b607073763..70c049839b 100644 --- a/seaweed-block/core/frontend/durable/storage_adapter.go +++ b/seaweed-block/core/frontend/durable/storage_adapter.go @@ -554,6 +554,8 @@ func (b *StorageBackend) Sync(ctx context.Context) error { // Read copies up to len(p) bytes starting at offset. Reads past // end-of-volume return zero bytes (no error). Every read is gated. func (b *StorageBackend) Read(ctx context.Context, offset int64, p []byte) (int, error) { + ctx, cancel := frontend.WithReadBudget(ctx, time.Time{}) + defer cancel() release, err := b.admitAfterAttach(ctx) if err != nil { return 0, err @@ -565,7 +567,14 @@ func (b *StorageBackend) Read(ctx context.Context, offset int64, p []byte) (int, if ctx.Err() != nil { return 0, b.attachReadyTimeout(ctx.Err()) } - return b.readBytes(offset, p) + n, err := b.readBytes(ctx, offset, p) + if hydrationPendingOnly(err) { + if gateErr := b.gate(); gateErr != nil { + return 0, gateErr + } + return n, fmt.Errorf("%w: %w", frontend.ErrBackendBusy, err) + } + return n, err } // setChunkAttachPending is used only after successful local recovery. @@ -679,7 +688,7 @@ func (b *StorageBackend) Write(ctx context.Context, offset int64, p []byte) (int b.profile.recordBackendWriteRequest(len(p)) start := time.Now() n, err := b.writeBytes(ctx, offset, p) - if errors.Is(err, storage.ErrWALPressureTimeout) { + if errors.Is(err, storage.ErrWALPressureTimeout) || hydrationPendingOnly(err) { err = fmt.Errorf("%w: %w", frontend.ErrBackendBusy, err) } if n > 0 { @@ -772,6 +781,50 @@ func (b *StorageBackend) lineageCheck() error { return nil } +// readStorageBlock shares the request deadline across blocks and partial-write +// RMW. Legacy engines may still perform contextless local I/O (#221). +func (b *StorageBackend) readStorageBlock(ctx context.Context, lba uint32) ([]byte, error) { + if err := ctx.Err(); err != nil { + return nil, fmt.Errorf("%w: %w", storage.ErrChunkHydrationPending, err) + } + var data []byte + var err error + if r, ok := b.storage.(storage.ContextReader); ok { + data, err = r.ReadContext(ctx, lba) + } else { + data, err = b.storage.Read(lba) + } + if err != nil { + return nil, err + } // a known substrate failure wins expiry + if err := ctx.Err(); err != nil { + return nil, fmt.Errorf("%w: %w", storage.ErrChunkHydrationPending, err) + } + return data, nil +} + +func hydrationPendingOnly(err error) bool { + if err == storage.ErrChunkHydrationPending || err == context.DeadlineExceeded || err == context.Canceled { + return true + } + if err == nil { + return false + } + if j, ok := err.(interface{ Unwrap() []error }); ok { + causes := j.Unwrap() + if len(causes) == 0 { + return false + } + for _, cause := range causes { + if !hydrationPendingOnly(cause) { + return false + } + } + return true + } + return hydrationPendingOnly(errors.Unwrap(err)) +} + // readBytes performs the byte-range → LBA-range fanout for Read. // Reads block-at-a-time, copying the sub-slice covered by the // current byte range. Handles three cases: @@ -779,7 +832,7 @@ func (b *StorageBackend) lineageCheck() error { // - offset aligned, len >= block: copy full block, advance. // - offset misaligned: copy tail of first block only. // - final block, len < available: copy prefix only. -func (b *StorageBackend) readBytes(offset int64, p []byte) (int, error) { +func (b *StorageBackend) readBytes(ctx context.Context, offset int64, p []byte) (int, error) { if offset < 0 { return 0, errInvalidOffset } @@ -789,7 +842,7 @@ func (b *StorageBackend) readBytes(offset int64, p []byte) (int, error) { pos := offset for len(buf) > 0 { lba := uint32(pos / bs) - block, err := b.storage.Read(lba) + block, err := b.readStorageBlock(ctx, lba) if err != nil { return total, fmt.Errorf("durable: read lba=%d: %w", lba, err) } @@ -866,7 +919,7 @@ func (b *StorageBackend) writeBytes(ctx context.Context, offset int64, p []byte) block = buf[:chunkLen] } else { // Partial-block: RMW. - existing, err := b.storage.Read(lba) + existing, err := b.readStorageBlock(ctx, lba) if err != nil { writeLock.Unlock() return total, fmt.Errorf("durable: RMW read lba=%d: %w", lba, err) diff --git a/seaweed-block/core/frontend/nvme/session.go b/seaweed-block/core/frontend/nvme/session.go index 950b7a0124..48f76d5210 100644 --- a/seaweed-block/core/frontend/nvme/session.go +++ b/seaweed-block/core/frontend/nvme/session.go @@ -574,6 +574,12 @@ func (s *Session) enqueueResponse(resp *response) { // ---------- IO handlers (run in goroutines) ---------- func (s *Session) handleRead(ctx context.Context, req *Request) { + arrival := req.receivedAt + if arrival.IsZero() { + arrival = req.dispatchReadyAt + } + ctx, cancel := frontend.WithReadBudget(ctx, arrival) + defer cancel() s.recordReadCommand() res := s.handler.Handle(ctx, IOCommand{ Opcode: req.capsule.OpCode, diff --git a/seaweed-block/core/frontend/read_budget.go b/seaweed-block/core/frontend/read_budget.go new file mode 100644 index 0000000000..38685a03c4 --- /dev/null +++ b/seaweed-block/core/frontend/read_budget.go @@ -0,0 +1,14 @@ +package frontend + +import ( + "context" + "time" +) + +const ReadCommandBudget = WriteCommandBudget + +// WithReadBudget includes queue residence and all cooperative attachment and +// hydration waits. A previously established, earlier deadline is never reset. +func WithReadBudget(ctx context.Context, arrival time.Time) (context.Context, context.CancelFunc) { + return WithWriteBudget(ctx, arrival) +}