diff --git a/seaweed-block/core/frontend/ublk/target.go b/seaweed-block/core/frontend/ublk/target.go index ff0288cce4..48fa152af3 100644 --- a/seaweed-block/core/frontend/ublk/target.go +++ b/seaweed-block/core/frontend/ublk/target.go @@ -5,8 +5,10 @@ import ( "errors" "fmt" "sync" + "time" "github.com/seaweedfs/seaweed-block/core/frontend" + "github.com/seaweedfs/seaweed-block/core/iolatency" ) // TargetConfig wires a ublk device to a volume through a Provider. @@ -119,7 +121,33 @@ type providerBackend struct { backend frontend.Backend } +// An open attempted by the timing hook must not be retried by this same I/O. +// A later request gets a fresh context and retains the original retry behavior. +type providerOpenFailure struct { + owner *providerBackend + err error +} +type providerOpenFailureKey struct{} + +func (b *providerBackend) BeginDurabilityRequest(ctx context.Context, kind string) (context.Context, *iolatency.Request) { + if kind != "write" && kind != "fua" && kind != "flush" { + return ctx, nil + } + received := time.Now() + backend, err := b.get(ctx) + if err != nil { + return context.WithValue(ctx, providerOpenFailureKey{}, providerOpenFailure{b, err}), nil + } + ctx, trace := frontend.BeginDurabilityRequest(ctx, backend, kind) + trace.IncludeReceive(received) + return ctx, trace +} + func (b *providerBackend) get(ctx context.Context) (frontend.Backend, error) { + if failed, ok := ctx.Value(providerOpenFailureKey{}).(providerOpenFailure); ok && failed.owner == b { + return nil, failed.err + } + b.mu.Lock() defer b.mu.Unlock() if b.backend != nil { diff --git a/seaweed-block/core/frontend/ublk/target_latency_test.go b/seaweed-block/core/frontend/ublk/target_latency_test.go index 4048558bd6..a6b9f7af71 100644 --- a/seaweed-block/core/frontend/ublk/target_latency_test.go +++ b/seaweed-block/core/frontend/ublk/target_latency_test.go @@ -2,6 +2,7 @@ package ublk import ( "context" + "errors" "github.com/seaweedfs/seaweed-block/core/frontend" "github.com/seaweedfs/seaweed-block/core/frontend/durable" "testing" @@ -63,3 +64,41 @@ func TestTargetDurabilityLatencyThroughProviderBackend(t *testing.T) { t.Fatal("storage stages missing", s) } } + +type retryLatencyProvider struct { + inner frontend.Provider + calls int +} + +func (p *retryLatencyProvider) Open(ctx context.Context, id string) (frontend.Backend, error) { + p.calls++ + if p.calls == 1 { + return nil, errors.New("first open refused") + } + return p.inner.Open(ctx, id) +} +func TestTargetLatencyOpenFailureIsNotRetriedInsideRequest(t *testing.T) { + p := &retryLatencyProvider{inner: failingProvider{err: errors.New("still refused")}} + tg := NewTarget(TargetConfig{Provider: p, VolumeID: "v1", VolumeSize: 4096, BlockSize: 4096}) + var h handler + tg.startDevice = func(cfg Config) (targetDevice, error) { + h = handler{backend: cfg.Backend, size: cfg.Size} + return &latencyDevice{h: h}, nil + } + if _, err := tg.Start(); err != nil { + t.Fatal(err) + } + defer tg.Close() + if n := h.handle(context.Background(), request{op: ioOpFlush}, nil); n >= 0 { + t.Fatal("open failure acknowledged") + } + if p.calls != 1 { + t.Fatalf("timing caused %d open attempts in one request", p.calls) + } + if n := h.handle(context.Background(), request{op: ioOpFlush}, nil); n >= 0 { + t.Fatal("retry failure acknowledged") + } + if p.calls != 2 { + t.Fatalf("next request did not retry exactly once: %d", p.calls) + } +} diff --git a/seaweed-block/core/iolatency/latency.go b/seaweed-block/core/iolatency/latency.go index 5c09b0bd50..21a2a82688 100644 --- a/seaweed-block/core/iolatency/latency.go +++ b/seaweed-block/core/iolatency/latency.go @@ -122,6 +122,15 @@ func BackendExit(ctx context.Context) { r.backendEnd = time.Now() } } + +// IncludeReceive includes a lazy provider lookup preceding the delegated Begin. +// Called by the owning synchronous dispatcher before any backend call. +func (r *Request) IncludeReceive(received time.Time) { + if r != nil && received.Before(r.start) { + r.start = received + } +} + func (r *Request) Finish(failed bool) { if r == nil { return diff --git a/seaweed-block/docs/fsync-latency.md b/seaweed-block/docs/fsync-latency.md index 30b2198516..5d2875cbc7 100644 --- a/seaweed-block/docs/fsync-latency.md +++ b/seaweed-block/docs/fsync-latency.md @@ -29,3 +29,5 @@ A guest fsync may produce two Flush commands; compare command counts and physica Unit controls cover known histogram quantiles, concurrent counters, failed/refused requests, both frontend dispatchers, batch waiter-versus-physical counts and dedup. The initial status contract test fails on unmodified main because the existing JSON has no durability histograms. The PR records an interleaved main-versus-instrumented comparison of the existing file-backed TCP paired-Flush proxy (`SW_BLOCK_SYNC_PERF=1`, `TestNVMePairedFlushLatency`), using prebuilt binaries on tp04. It measures request-path default-on overhead; it is not mounted fio, a guest fsync result, or a device-specific latency promise. M5 may proceed independently if this PR has not landed. + +The ublk Target forwards the optional timing hook through its lazy providerBackend to the actual durable backend. Initial provider lookup is included in ReceiveToBackend. If provider open fails before a durable backend is available, the request returns that error without a second open attempt, and no backend-owned frontend histogram exists for that failure. The device-level error counters remain the source for such failures. A later request may retry normally. Caller tests start the real Target with only the kernel device constructor replaced; NVMe uses a real TCP target and FUA capsule.