mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-05 02:27:26 +01:00
test: cover pressure locks faults pins retry status and native burst
This commit is contained in:
1 parent
8c16952789
commit
7e9f07818a
3 files changed
+358
No files matched your search
@@ -0,0 +1,123 @@
|
||||
package durable_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/binary"
|
||||
"fmt"
|
||||
"github.com/seaweedfs/seaweed-block/core/frontend/durable"
|
||||
"github.com/seaweedfs/seaweed-block/core/frontend/nvme"
|
||||
"github.com/seaweedfs/seaweed-block/core/storage"
|
||||
"sort"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Public provider + actual NVMe handler, file-backed v3 with the default
|
||||
// 64 MiB WAL. No injected I/O failures, private selector, or flusher control.
|
||||
func TestWALStoreChunkV3_NoClientFlushBurst(t *testing.T) {
|
||||
for _, mode := range []storage.WMode{storage.WModeLagged, storage.WModeStrict} {
|
||||
t.Run(fmt.Sprintf("mode-%d", mode), func(t *testing.T) {
|
||||
runNoClientFlushBurst(t, mode)
|
||||
})
|
||||
}
|
||||
}
|
||||
func runNoClientFlushBurst(t *testing.T, mode storage.WMode) {
|
||||
const total = 400 * 1024 * 1024
|
||||
const transfer = 128 * 1024
|
||||
ctx := context.Background()
|
||||
remote := storage.NewMemChunkRemote()
|
||||
p, err := durable.NewDurableProvider(durable.ProviderConfig{
|
||||
Impl: durable.ImplWALStoreChunk, StorageRoot: t.TempDir(), BlockSize: 4096,
|
||||
NumBlocks: (1 << 30) / 4096, ChunkBlocks: 1024, ChunkCaptureInterval: 0,
|
||||
ChunkRemoteFor: func(string) (storage.ChunkRemote, error) { return remote, nil },
|
||||
}, v1View())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() {
|
||||
if err := p.Close(); err != nil {
|
||||
t.Logf("close: %v", err)
|
||||
}
|
||||
})
|
||||
s, err := p.EnsureStorage("v1")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err = p.RecoverVolume(ctx, "v1"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
be, err := p.Open(ctx, "v1")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
before, err := remote.GetHead(ctx)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ws := s.(*storage.WALStore)
|
||||
if err := ws.SetWMode(mode); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
syncsBefore := ws.WALSyncCount()
|
||||
var durations []time.Duration
|
||||
t.Logf("before HEAD gen=%d remoteLSN=%d cp=%d next=%d retention=%d", before.Gen, before.RemoteLSN, ws.CheckpointLSN(), ws.NextLSN(), ws.RecoveryRetentionLSNs())
|
||||
h := nvme.NewIOHandler(nvme.HandlerConfig{Backend: be, BlockSize: 4096, VolumeSize: 1 << 30})
|
||||
data := make([]byte, transfer)
|
||||
start := time.Now()
|
||||
for off := 0; off < total; off += transfer {
|
||||
for i := range data {
|
||||
if i%8 == 0 {
|
||||
binary.LittleEndian.PutUint64(data[i:i+8], uint64(off+i))
|
||||
}
|
||||
}
|
||||
writeStart := time.Now()
|
||||
r := h.Handle(ctx, nvme.IOCommand{Opcode: 0x01, NSID: 1, SLBA: uint64(off / 4096), NLB: transfer / 4096, Data: data})
|
||||
durations = append(durations, time.Since(writeStart))
|
||||
if r.AsError() != nil {
|
||||
t.Fatalf("NO FLUSH write bytes=%d elapsed=%v result=%+v cp=%d next=%d", off, time.Since(start), r, ws.CheckpointLSN(), ws.NextLSN())
|
||||
}
|
||||
if (off+transfer)%(16*1024*1024) == 0 {
|
||||
t.Logf("NO FLUSH progress bytes=%d elapsed=%v cp=%d next=%d", off+transfer, time.Since(start), ws.CheckpointLSN(), ws.NextLSN())
|
||||
}
|
||||
}
|
||||
// The expected bytes come from the submitted pattern, not another reader.
|
||||
for off := 0; off < total; off += transfer {
|
||||
got := make([]byte, transfer)
|
||||
n, err := be.Read(ctx, int64(off), got)
|
||||
if err != nil {
|
||||
t.Fatalf("readback at%d: %v", off, err)
|
||||
}
|
||||
if n != transfer {
|
||||
t.Fatalf("short readback at%d: %d", off, n)
|
||||
}
|
||||
for i := 0; i < len(got); i += 8 {
|
||||
if value := binary.LittleEndian.Uint64(got[i : i+8]); value != uint64(off+i) {
|
||||
t.Fatalf("readback offset%d got%d", off+i, value)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
sort.Slice(durations, func(i, j int) bool { return durations[i] < durations[j] })
|
||||
batches := ws.WALSyncCount() - syncsBefore
|
||||
fsyncs := batches
|
||||
if mode == storage.WModeStrict {
|
||||
fsyncs *= 2
|
||||
}
|
||||
t.Logf("COST mode=%d bytes=%d pressureBatches=%d internalFsyncsPerGiB=%.2f writeP99=%v max=%v", mode, total, batches, float64(fsyncs)/(float64(total)/(1<<30)), durations[(len(durations)-1)*99/100], durations[len(durations)-1])
|
||||
after, err := remote.GetHead(ctx)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
st := p.DurableStatuses()
|
||||
if after.Gen != before.Gen || after.RemoteLSN != before.RemoteLSN {
|
||||
t.Fatalf("remote moved without explicit cut: before=%+v after=%+v", before, after)
|
||||
}
|
||||
if len(st) != 1 || st[0].Chunk == nil || st[0].Chunk.Stats.CutsOK != 0 {
|
||||
t.Fatalf("unexpected cuts: %+v", st)
|
||||
}
|
||||
if ws.CheckpointLSN() == 0 || st[0].WriteProfile.WALAppendWrapCount == 0 {
|
||||
t.Fatal("no checkpoint or physical wrap observed")
|
||||
}
|
||||
t.Logf("after HEAD gen=%d remoteLSN=%d cp=%d next=%d wraps=%d cuts=%d", after.Gen, after.RemoteLSN, ws.CheckpointLSN(), ws.NextLSN(), st[0].WriteProfile.WALAppendWrapCount, st[0].Chunk.Stats.CutsOK)
|
||||
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package nvme
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"github.com/seaweedfs/seaweed-block/core/frontend"
|
||||
"github.com/seaweedfs/seaweed-block/core/frontend/testback"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type pressureWriteBackend struct {
|
||||
frontend.Backend
|
||||
err error
|
||||
}
|
||||
|
||||
func (b *pressureWriteBackend) Write(context.Context, int64, []byte) (int, error) { return 0, b.err }
|
||||
func TestSessionPressureTimeoutIsRetryableNamespaceNotReady(t *testing.T) {
|
||||
b := &pressureWriteBackend{Backend: testback.NewRecordingBackend(frontend.Identity{VolumeID: "v1"}), err: frontend.ErrBackendBusy}
|
||||
s := &Session{handler: NewIOHandler(HandlerConfig{Backend: b}), respCh: make(chan *response, 1), done: make(chan struct{})}
|
||||
for _, busy := range []bool{true, false} {
|
||||
if !busy {
|
||||
b.err = errors.New("sticky physical I/O fault")
|
||||
}
|
||||
req := &Request{payload: make([]byte, 512)}
|
||||
req.capsule.OpCode = ioWrite
|
||||
req.capsule.NSID = 1
|
||||
s.handleWrite(context.Background(), req)
|
||||
resp := <-s.respCh
|
||||
want := MakeStatusField(SCTGeneric, SCInternalError, true)
|
||||
if busy {
|
||||
want = MakeStatusField(SCTGeneric, SCNamespaceNotReady, false)
|
||||
}
|
||||
if resp.resp.Status != want {
|
||||
t.Fatalf("busy=%v wire status=%#x want%#x", busy, resp.resp.Status, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,8 +2,10 @@ package storage
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
@@ -40,3 +42,199 @@ func TestWALStoreV3NoClientSyncSixRings(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func pressureStoreNoSync(t *testing.T, records uint32) *WALStore {
|
||||
t.Helper()
|
||||
s, err := createMappedWALStoreV3ForTest(filepath.Join(t.TempDir(), "volume"), 96, 4096, 64*1024, WModeLagged, 4)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { _ = s.Close() })
|
||||
s.DisableAutoFlushForRecoveryTest()
|
||||
for i := uint32(0); i < records; i++ {
|
||||
if _, err := s.Write(i, blk(byte(i+1))); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
func TestWALStoreV3PressureSyncFaultDoesNotPublishFrontiers(t *testing.T) {
|
||||
for _, mode := range []WMode{WModeLagged, WModeStrict} {
|
||||
for _, stage := range []string{"fsync", "slot"} {
|
||||
t.Run(fmt.Sprintf("mode-%d/%s", mode, stage), func(t *testing.T) {
|
||||
s := pressureStoreNoSync(t, 12)
|
||||
if err := s.SetWMode(mode); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// SetWMode syncs; add fresh uncovered writes without changing pressure.
|
||||
if _, err := s.Write(0, blk(99)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
before := s.v3.pub.current()
|
||||
ack := s.v3.wack.Load()
|
||||
boom := errors.New("pressure persistence fault")
|
||||
if stage == "slot" {
|
||||
s.v3.testBeforeSlotWrite = func() error { return boom }
|
||||
} else {
|
||||
s.syncWALFile = func() error { return boom }
|
||||
s.v3.pub.io.fsync = func() error { return boom }
|
||||
}
|
||||
err := s.syncWALPressureV3()
|
||||
if !errors.Is(err, boom) || s.stickyIOFault() == nil {
|
||||
t.Fatalf("lost pressure fault: %v", err)
|
||||
}
|
||||
if s.v3.wack.Load() != ack || s.CheckpointLSN() != before.CheckpointLSN {
|
||||
t.Fatal("failed pressure persistence advanced frontier")
|
||||
}
|
||||
if _, err := s.Write(1, blk(98)); !errors.Is(err, boom) {
|
||||
t.Fatalf("subsequent write lost cause: %v", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestWALStoreV3PressureSyncSkipsInstalling(t *testing.T) {
|
||||
s, _ := newStoreV3(t, WModeLagged)
|
||||
t.Cleanup(func() { crashRecoveryHardeningStore(t, s) })
|
||||
h := beginV3(t, s, 0, idA)
|
||||
for i := uint64(1); i <= 12; i++ {
|
||||
if err := h.ApplyInstallEntry(i, blk(byte(i)), i); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
before := s.v3.pub.current()
|
||||
ack := s.v3.wack.Load()
|
||||
if err := s.syncWALPressureV3(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if s.v3.pub.current() != before || s.v3.wack.Load() != ack {
|
||||
t.Fatal("ordinary pressure sync bypassed install-log gate")
|
||||
}
|
||||
}
|
||||
|
||||
func TestWALStoreV3PressureWaitBudgetAndPin(t *testing.T) {
|
||||
s, err := createMappedWALStoreV3ForTest(filepath.Join(t.TempDir(), "volume"), 96, 4096, 64*1024, WModeLagged, 4)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { _ = s.Close() })
|
||||
if s.admissionTimeout <= 0 || s.admissionTimeout >= 5*time.Second {
|
||||
t.Fatalf("pressure budget %v", s.admissionTimeout)
|
||||
}
|
||||
// Contract used by an active RF2 catch-up session: do not release its records.
|
||||
s.SetRecycleFloorSource(&fakeRecycleFloorSource{floor: 0, anyActive: true})
|
||||
start := time.Now()
|
||||
var got error
|
||||
for i := uint32(0); i < 96; i++ {
|
||||
if _, got = s.Write(i, blk(byte(i+1))); got != nil {
|
||||
break
|
||||
}
|
||||
}
|
||||
if !errors.Is(got, ErrWALPressureTimeout) || time.Since(start) >= 5*time.Second {
|
||||
t.Fatalf("pinned burst outcome %v after %v", got, time.Since(start))
|
||||
}
|
||||
if s.CheckpointLSN() != 0 {
|
||||
t.Fatal("active catch-up pin was bypassed")
|
||||
}
|
||||
}
|
||||
|
||||
// TryRLock refuses only once the pending exclusive operation is queued: the
|
||||
// parked Write still owns a read lock. No sleep guesses when the Lock queues.
|
||||
func TestWALStoreV3PressureProgressWithPendingCutOrClose(t *testing.T) {
|
||||
for _, closing := range []bool{false, true} {
|
||||
t.Run(fmt.Sprintf("close-%v", closing), func(t *testing.T) {
|
||||
s := pressureStoreNoSync(t, 15)
|
||||
s.admissionTimeout = 2 * time.Second
|
||||
parked, resume := make(chan struct{}), make(chan struct{})
|
||||
release := sync.OnceFunc(func() { close(resume) })
|
||||
cutRelease := make(chan struct{})
|
||||
releaseCut := sync.OnceFunc(func() { close(cutRelease) })
|
||||
var joins []chan struct{}
|
||||
t.Cleanup(func() {
|
||||
release()
|
||||
releaseCut()
|
||||
for _, done := range joins {
|
||||
select {
|
||||
case <-done:
|
||||
case <-time.After(6 * time.Second):
|
||||
t.Error("pressure worker did not join")
|
||||
}
|
||||
}
|
||||
})
|
||||
var once sync.Once
|
||||
s.admission.sleepFn = func(d time.Duration) { once.Do(func() { close(parked); <-resume }); time.Sleep(d) }
|
||||
writerDone := make(chan struct{})
|
||||
joins = append(joins, writerDone)
|
||||
var writeErr error
|
||||
go func() { defer close(writerDone); _, writeErr = s.Write(16, blk(16)) }()
|
||||
select {
|
||||
case <-parked:
|
||||
case <-time.After(2 * time.Second):
|
||||
t.Fatal("writer not parked")
|
||||
}
|
||||
exclusiveDone := make(chan struct{})
|
||||
joins = append(joins, exclusiveDone)
|
||||
go func() {
|
||||
defer close(exclusiveDone)
|
||||
if closing {
|
||||
_ = s.Close()
|
||||
} else {
|
||||
s.captureMu.Lock()
|
||||
defer s.captureMu.Unlock()
|
||||
<-cutRelease
|
||||
}
|
||||
}()
|
||||
deadline := time.Now().Add(time.Second)
|
||||
for {
|
||||
var admitted bool
|
||||
if closing {
|
||||
admitted = s.lifecycleMu.TryRLock()
|
||||
if admitted {
|
||||
s.lifecycleMu.RUnlock()
|
||||
}
|
||||
} else {
|
||||
admitted = s.captureMu.TryRLock()
|
||||
if admitted {
|
||||
s.captureMu.RUnlock()
|
||||
}
|
||||
}
|
||||
if !admitted {
|
||||
break
|
||||
}
|
||||
if time.Now().After(deadline) {
|
||||
t.Fatal("exclusive operation did not queue")
|
||||
}
|
||||
time.Sleep(time.Millisecond)
|
||||
}
|
||||
flushDone := make(chan struct{})
|
||||
joins = append(joins, flushDone)
|
||||
var flushErr error
|
||||
go func() { defer close(flushDone); flushErr = s.flusher.flushOnce() }()
|
||||
select {
|
||||
case <-flushDone:
|
||||
case <-time.After(time.Second):
|
||||
t.Fatal("pressure flusher waited behind pending exclusive lock")
|
||||
}
|
||||
if flushErr != nil {
|
||||
t.Fatal(flushErr)
|
||||
}
|
||||
release()
|
||||
select {
|
||||
case <-writerDone:
|
||||
case <-time.After(3 * time.Second):
|
||||
t.Fatal("writer did not drain")
|
||||
}
|
||||
if writeErr != nil {
|
||||
t.Fatal(writeErr)
|
||||
}
|
||||
releaseCut()
|
||||
select {
|
||||
case <-exclusiveDone:
|
||||
case <-time.After(time.Second):
|
||||
t.Fatal("exclusive operation did not finish")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user