diff --git a/seaweed-block/core/frontend/durable/latency_status_test.go b/seaweed-block/core/frontend/durable/latency_status_test.go index 5b9eb7654a..fa3537c2d3 100644 --- a/seaweed-block/core/frontend/durable/latency_status_test.go +++ b/seaweed-block/core/frontend/durable/latency_status_test.go @@ -3,6 +3,7 @@ package durable_test import ( "context" "encoding/json" + "github.com/seaweedfs/seaweed-block/core/frontend/nvme" "testing" ) @@ -26,3 +27,22 @@ func TestDurabilityLatencyStatusPresent(t *testing.T) { t.Fatalf("durability stage histograms missing from existing WriteProfile status: %s", data) } } + +func TestDurabilityLatencyNVMeRequests(t *testing.T) { + b, _, _ := newTestBackend(t, logicalStorageFactories()[0], 16, 4096) + h := nvme.NewIOHandler(nvme.HandlerConfig{Backend: b, BlockSize: 4096, VolumeSize: 16 * 4096, NSID: 1}) + for _, cmd := range []nvme.IOCommand{ + {Opcode: 1, NSID: 1, NLB: 1, Data: make([]byte, 4096), FUA: true}, + {Opcode: 0, NSID: 1}, + {Opcode: 1, NSID: 1, SLBA: 99, NLB: 1, Data: make([]byte, 4096)}, + } { + h.Handle(context.Background(), cmd) + } + s := b.WriteProfile().DurabilityLatency + if s.Frontend.Writes != 2 || s.Frontend.FUAWrites != 1 || s.Frontend.Flushes != 1 || s.Frontend.Failed != 1 || s.Frontend.RequestTotal.Count != 3 || s.Frontend.ReceiveToBackend.Count != 2 || s.Frontend.AckDispatch.Count != 2 || s.BackendSync.Count != 2 { + t.Fatal(s) + } + if s.WAL == nil || s.WAL.WALAppend.Count == 0 || s.WAL.FileSync.Count == 0 { + t.Fatal("missing WAL stages", s) + } +} diff --git a/seaweed-block/core/frontend/ublk/latency_test.go b/seaweed-block/core/frontend/ublk/latency_test.go new file mode 100644 index 0000000000..0acaf6ba9d --- /dev/null +++ b/seaweed-block/core/frontend/ublk/latency_test.go @@ -0,0 +1,42 @@ +package ublk + +import ( + "context" + "github.com/seaweedfs/seaweed-block/core/frontend" + "github.com/seaweedfs/seaweed-block/core/frontend/testback" + "github.com/seaweedfs/seaweed-block/core/iolatency" + "testing" +) + +type timingBackend struct { + *testback.RecordingBackend + requests iolatency.Requests +} + +func (b *timingBackend) BeginDurabilityRequest(ctx context.Context, k string) (context.Context, *iolatency.Request) { + return b.requests.Begin(ctx, k) +} +func (b *timingBackend) Write(ctx context.Context, o int64, p []byte) (int, error) { + iolatency.BackendEnter(ctx) + defer iolatency.BackendExit(ctx) + return b.RecordingBackend.Write(ctx, o, p) +} +func (b *timingBackend) Sync(ctx context.Context) error { + iolatency.BackendEnter(ctx) + defer iolatency.BackendExit(ctx) + return b.RecordingBackend.Sync(ctx) +} +func TestUblkDurabilityLatency(t *testing.T) { + b := &timingBackend{RecordingBackend: testback.NewRecordingBackend(frontend.Identity{VolumeID: "v1"})} + h := newTestHandler(b) + h.handle(context.Background(), request{op: ioOpWrite, flags: ioFlagFUA, nrSectors: 8}, make([]byte, 4096)) + h.handle(context.Background(), request{op: ioOpFlush}, nil) + h.handle(context.Background(), request{op: ioOpWrite, startSector: 1 << 30, nrSectors: 8}, make([]byte, 4096)) + s := b.requests.Snapshot() + if s.Writes != 2 || s.FUAWrites != 1 || s.Flushes != 1 || s.Failed != 1 || s.RequestTotal.Count != 3 || s.ReceiveToBackend.Count != 2 || s.AckDispatch.Count != 2 { + t.Fatal(s) + } + if b.SyncCount() != 2 || b.WriteCount() != 1 { + t.Fatal("timing changed dispatch") + } +} diff --git a/seaweed-block/core/iolatency/latency_test.go b/seaweed-block/core/iolatency/latency_test.go new file mode 100644 index 0000000000..fa2d2f440e --- /dev/null +++ b/seaweed-block/core/iolatency/latency_test.go @@ -0,0 +1,68 @@ +package iolatency + +import ( + "context" + "sync" + "testing" + "time" +) + +func TestHistogramKnownQuantiles(t *testing.T) { + var h Histogram + if s := h.Snapshot(); s.Count != 0 || s.P99UpperNanos != 0 { + t.Fatal(s) + } + for i := 0; i < 100; i++ { + d := time.Duration(3) + if i >= 99 { + d = 9 + } + h.Observe(d) + } + s := h.Snapshot() + if s.Count != 100 || s.SumNanos != 306 || s.P50UpperNanos != 4 || s.P99UpperNanos != 4 || s.Buckets[2] != 99 || s.Buckets[4] != 1 { + t.Fatal(s) + } +} +func TestHistogramConcurrentAccounting(t *testing.T) { + var h Histogram + var wg sync.WaitGroup + for i := 0; i < 8; i++ { + wg.Add(1) + go func() { + defer wg.Done() + for j := 0; j < 1000; j++ { + h.Observe(10 * time.Nanosecond) + } + }() + } + wg.Wait() + s := h.Snapshot() + if s.Count != 8000 || s.SumNanos != 80000 { + t.Fatal(s) + } +} +func TestRequestKindsAndMissingBackend(t *testing.T) { + var c Requests + for _, kind := range []string{"write", "fua", "flush"} { + ctx, r := c.Begin(context.Background(), kind) + BackendEnter(ctx) + BackendExit(ctx) + r.Finish(kind == "flush") + } + _, r := c.Begin(context.Background(), "read") + if r != nil { + t.Fatal("read traced") + } + r.Finish(false) + s := c.Snapshot() + if s.Writes != 2 || s.Flushes != 1 || s.FUAWrites != 1 || s.Failed != 1 || s.RequestTotal.Count != 3 || s.ReceiveToBackend.Count != 3 || s.AckDispatch.Count != 3 { + t.Fatal(s) + } + _, r = c.Begin(context.Background(), "flush") + r.Finish(true) + s = c.Snapshot() + if s.Failed != 2 || s.RequestTotal.Count != 4 || s.AckDispatch.Count != 3 { + t.Fatal("refused request fabricated backend ACK", s) + } +} diff --git a/seaweed-block/core/storage/durability_latency_test.go b/seaweed-block/core/storage/durability_latency_test.go new file mode 100644 index 0000000000..15edcfdbd8 --- /dev/null +++ b/seaweed-block/core/storage/durability_latency_test.go @@ -0,0 +1,38 @@ +package storage + +import ( + "errors" + "testing" +) + +func TestDurabilityLatencyCountsSharedPhysicalSyncAndDedup(t *testing.T) { + s := newGenerationStore(t) + if _, err := s.Write(0, makeBlock(4096, 1)); err != nil { + t.Fatal(err) + } + if _, err := s.Sync(); err != nil { + t.Fatal(err) + } + first := s.DurabilityLatency() + if first.WALAppend.Count == 0 || first.GroupCommitWait.Count != 1 || first.FileSync.Count != 1 { + t.Fatal(first) + } + if _, err := s.Sync(); err != nil { + t.Fatal(err) + } + next := s.DurabilityLatency() + if next.FileSync.Count != 1 || next.GroupCommitWait.Count != 1 { + t.Fatal("dedup fabricated physical or queued sample", next) + } + if _, err := s.Write(1, makeBlock(4096, 2)); err != nil { + t.Fatal(err) + } + s.syncWALFile = func() error { return errors.New("injected fsync") } + if _, err := s.Sync(); err == nil { + t.Fatal("failed sync acknowledged") + } + next = s.DurabilityLatency() + if next.FileSync.Count != 2 || next.GroupCommitWait.Count != 2 { + t.Fatal("failed physical attempt missing", next) + } +} diff --git a/seaweed-block/core/storage/walstore.go b/seaweed-block/core/storage/walstore.go index ff749b90a8..1e786a1283 100644 --- a/seaweed-block/core/storage/walstore.go +++ b/seaweed-block/core/storage/walstore.go @@ -263,8 +263,6 @@ func openInitializedWithMetadata(path string, f *os.File, sb *superblock, metada s.walMutation.Store(1) s.syncWALFile = func() error { s.walSyncCalls.Add(1) - start := time.Now() - defer func() { s.fileSyncLatency.Observe(time.Since(start)) }() return f.Sync() } committer := NewGroupCommitter(GroupCommitterConfig{ @@ -1292,6 +1290,8 @@ func (s *WALStore) syncWALBatch() error { if !s.bypassSyncDedup.Load() && s.chunk.Load() == nil && cut <= s.durableMutation.Load() { return nil } + start := time.Now() + defer func() { s.fileSyncLatency.Observe(time.Since(start)) }() if err := s.syncWALFile(); err != nil { return err }