From e62b039cd71bfe21f3cfd559ee65b717a62dc957 Mon Sep 17 00:00:00 2001 From: pingqiu Date: Fri, 20 Nov 2026 07:54:17 -0800 Subject: [PATCH] test(durable): witness StopIO return before final HEAD publication --- .../durable/precopy_stop_boundary_test.go | 90 +++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 seaweed-block/core/frontend/durable/precopy_stop_boundary_test.go diff --git a/seaweed-block/core/frontend/durable/precopy_stop_boundary_test.go b/seaweed-block/core/frontend/durable/precopy_stop_boundary_test.go new file mode 100644 index 0000000000..fbd8bb9a41 --- /dev/null +++ b/seaweed-block/core/frontend/durable/precopy_stop_boundary_test.go @@ -0,0 +1,90 @@ +package durable_test + +import ( + "context" + "strings" + "sync" + "testing" + "time" + + "github.com/seaweedfs/seaweed-block/core/storage" +) + +type stopBoundaryRemote struct { + storage.ChunkRemote + entered chan time.Time + release chan struct{} +} + +func (r *stopBoundaryRemote) CASHead(ctx context.Context, expected storage.ChunkHeadVersion, next storage.ChunkHead) (uint64, error) { + if next.State == storage.ChunkHeadDetached { + r.entered <- time.Now() + select { + case <-r.release: + case <-ctx.Done(): + return 0, ctx.Err() + } + } + return r.ChunkRemote.CASHead(ctx, expected, next) +} + +func TestPrecopyStopIOReturnTimestampPrecedesFinalPublication(t *testing.T) { + r := &stopBoundaryRemote{ChunkRemote: storage.NewMemChunkRemote(), entered: make(chan time.Time, 1), release: make(chan struct{})} + p := precopyProvider(t, r, &parkView{stubView: v1View()}) + openRecovered(t, p) + b, err := p.Open(context.Background(), "v1") + if err != nil { + t.Fatal(err) + } + if _, err := b.Write(context.Background(), 4096, block(0x67)); err != nil { + t.Fatal(err) + } + logs := &lockedLog{} + captureWriteLogs(t, logs) + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + done := make(chan error, 1) + var once sync.Once + release := func() { once.Do(func() { close(r.release) }) } + t.Cleanup(func() { + release() + cancel() + select { + case <-done: + case <-time.After(5 * time.Second): + t.Error("detach did not join") + } + }) + started := time.Now() + go func() { _, _, err := p.DetachVolume(ctx, "v1"); done <- err }() + var beforePublication time.Time + select { + case beforePublication = <-r.entered: + case <-ctx.Done(): + t.Fatal("final publication never entered") + } + release() + select { + case err := <-done: + done <- err + if err != nil { + t.Fatal(err) + } + case <-ctx.Done(): + t.Fatal("detach did not finish") + } + count := 0 + for _, line := range strings.Split(logs.String(), "\n") { + _, stamp, ok := strings.Cut(line, "volume=v1 StopIO returned at ") + if !ok { + continue + } + count++ + at, err := time.Parse(time.RFC3339Nano, stamp) + if err != nil || at.Before(started) || at.After(beforePublication) { + t.Fatalf("StopIO timestamp is not its return boundary: stamp=%q started=%s finalCAS=%s err=%v", stamp, started, beforePublication, err) + } + } + if count != 1 { + t.Fatalf("StopIO returned timestamp count=%d, want 1", count) + } +}