mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-06 19:17:28 +01:00
- TestVolumeDeleteAnswerDoesNotWaitOnAStuckStderr: with a stderr whose writes never return, the answer must reach stdout and the command exit as with a fast stderr, within the bound plus a short drain. Fails here: no answer and no exit in 10 s (the writer is joined before stdout is written, and the error line is written to stderr before stdout). - TestVolumeDeleteNeverWritesStderrConcurrently replaces the store-call timing: a stderr not safe for concurrent use, 1 s per progress line, sees no overlapping writes (holds here and must keep holding). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
292 lines
11 KiB
Go
292 lines
11 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"io"
|
|
"strings"
|
|
"sync/atomic"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/seaweedfs/seaweed-block/core/frontend/chunkstore"
|
|
"google.golang.org/grpc/codes"
|
|
"google.golang.org/grpc/status"
|
|
)
|
|
|
|
type transientDeleteFiler struct {
|
|
*memChunkStore
|
|
seen map[string]bool
|
|
stuck bool
|
|
calls int
|
|
code codes.Code
|
|
absent bool
|
|
lost bool
|
|
}
|
|
|
|
func (f *transientDeleteFiler) DeleteEntry(ctx context.Context, dir, name string, expected map[string][]byte) error {
|
|
f.calls++
|
|
key := dir + "/" + name
|
|
if f.absent {
|
|
if err := f.memChunkStore.DeleteEntry(ctx, dir, name, expected); err != nil {
|
|
return err
|
|
}
|
|
return chunkstore.ErrNotFound
|
|
}
|
|
if f.stuck || !f.seen[key] {
|
|
f.seen[key] = true
|
|
if f.lost {
|
|
if err := f.memChunkStore.DeleteEntry(ctx, dir, name, expected); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return status.Error(f.code, "object deadline")
|
|
}
|
|
return f.memChunkStore.DeleteEntry(ctx, dir, name, expected)
|
|
}
|
|
|
|
func TestVolumeDeleteContinuesAcrossTransientObjectFailures(t *testing.T) {
|
|
for _, tc := range []struct {
|
|
name string
|
|
code codes.Code
|
|
stuck, absent, lost bool
|
|
}{
|
|
{"deadline-progress", codes.DeadlineExceeded, false, false, false},
|
|
{"unavailable-progress", codes.Unavailable, false, false, false},
|
|
{"deadline-stuck", codes.DeadlineExceeded, true, false, false},
|
|
{"unavailable-stuck", codes.Unavailable, true, false, false},
|
|
{"already-absent", codes.OK, false, true, false},
|
|
{"lost-delete-answer", codes.DeadlineExceeded, false, false, true},
|
|
} {
|
|
name, stuck := tc.name, tc.stuck
|
|
t.Run(name, func(t *testing.T) {
|
|
store := newMemChunkStore()
|
|
useMemChunkStore(t, store)
|
|
v := cliVolume(t, store, name, "daily")
|
|
f := &transientDeleteFiler{memChunkStore: store, seen: map[string]bool{}, stuck: stuck, code: tc.code, absent: tc.absent, lost: tc.lost}
|
|
dialChunkFiler = func(string) (chunkstore.Filer, func() error, error) { return f, func() error { return nil }, nil }
|
|
addr := startFakeLifecycle(t, &fakeLifecycle{})
|
|
started := time.Now()
|
|
code, out, logs := runChunk(cliArgs("volume-delete", volArgs("volume", v), []string{"--master", addr, "--request-id", "delete-progress", "--timeout", "150ms"})...)
|
|
if stuck {
|
|
e := chunkErrorOut(t, out)
|
|
if code != 1 || e.Code != "unavailable" || !e.Retryable || !strings.Contains(e.Message, "deleted 0 of") || !strings.Contains(e.Message, "/") || !strings.Contains(e.Message, "object deadline") {
|
|
t.Fatalf("stalled delete: code=%d out=%s logs=%s", code, out, logs)
|
|
}
|
|
if time.Since(started) > 3*time.Second {
|
|
t.Fatal("no-progress window was not bounded")
|
|
}
|
|
f.stuck = false
|
|
code, out, logs = runChunk(cliArgs("volume-delete", volArgs("volume", v), []string{"--master", addr, "--request-id", "delete-progress", "--timeout", "150ms"})...)
|
|
if code != 0 || !strings.Contains(out, `"state":"deleted"`) {
|
|
t.Fatalf("resume same delete: %d %s %s", code, out, logs)
|
|
}
|
|
} else if code != 0 || !strings.Contains(out, `"state":"deleted"`) || !strings.Contains(logs, "deleted 2 of 2") || (!tc.absent && !tc.lost && f.calls < 4) {
|
|
t.Fatalf("progressing delete: code=%d calls=%d out=%s logs=%s", code, f.calls, out, logs)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// A real expired child context must not expire the next sweep. Unlike the
|
|
// immediate error controls, this filer blocks an exchange until its deadline.
|
|
type waitingDeleteFiler struct {
|
|
*memChunkStore
|
|
bound time.Duration
|
|
calls int
|
|
waited bool
|
|
stall bool
|
|
removedAt time.Time
|
|
}
|
|
|
|
func (f *waitingDeleteFiler) DeleteEntry(ctx context.Context, dir, name string, expected map[string][]byte) error {
|
|
f.calls++
|
|
if f.calls == 1 {
|
|
timer := time.NewTimer(f.bound / 2)
|
|
defer timer.Stop()
|
|
select {
|
|
case <-timer.C:
|
|
case <-ctx.Done():
|
|
return ctx.Err()
|
|
}
|
|
err := f.memChunkStore.DeleteEntry(ctx, dir, name, expected)
|
|
if err == nil {
|
|
f.removedAt = time.Now()
|
|
}
|
|
return err
|
|
}
|
|
if f.stall {
|
|
return status.Error(codes.Unavailable, "stalled after first removal")
|
|
}
|
|
if !f.waited {
|
|
f.waited = true
|
|
<-ctx.Done()
|
|
return ctx.Err()
|
|
}
|
|
return f.memChunkStore.DeleteEntry(ctx, dir, name, expected)
|
|
}
|
|
|
|
func TestVolumeDeleteRefreshesAnExpiredSweepAndWaitsFromLastRemoval(t *testing.T) {
|
|
for _, stall := range []bool{false, true} {
|
|
name := "expired-sweep"
|
|
if stall {
|
|
name = "progress-then-stall"
|
|
}
|
|
t.Run(name, func(t *testing.T) {
|
|
store := newMemChunkStore()
|
|
useMemChunkStore(t, store)
|
|
v := cliVolume(t, store, name, "daily")
|
|
bound := 600 * time.Millisecond
|
|
f := &waitingDeleteFiler{memChunkStore: store, bound: bound, stall: stall}
|
|
dialChunkFiler = func(string) (chunkstore.Filer, func() error, error) { return f, func() error { return nil }, nil }
|
|
addr := startFakeLifecycle(t, &fakeLifecycle{})
|
|
code, out, logs := runChunk(cliArgs("volume-delete", volArgs("volume", v), []string{"--master", addr, "--request-id", "timed-delete", "--timeout", bound.String()})...)
|
|
if f.removedAt.IsZero() {
|
|
t.Fatalf("fixture made no progress: %d %s", code, out)
|
|
}
|
|
if !stall {
|
|
if code != 0 || !f.waited || !strings.Contains(logs, "deleted 2 of 2") {
|
|
t.Fatalf("fresh sweep: %d %s %s", code, out, logs)
|
|
}
|
|
return
|
|
}
|
|
e := chunkErrorOut(t, out)
|
|
if code != 1 || e.Code != "unavailable" || !e.Retryable || !strings.Contains(e.Message, "deleted 1 of 2") || !strings.Contains(e.Message, "stalled after first removal") {
|
|
t.Fatalf("stall after progress: %d %s", code, out)
|
|
}
|
|
if elapsed := time.Since(f.removedAt); elapsed < bound || elapsed > bound+2*time.Second {
|
|
t.Fatalf("no-progress window from last removal = %s, want [%s,%s]", elapsed, bound, bound+2*time.Second)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// blockedDeleteFiler holds every object delete until the call's deadline: a
|
|
// store that accepts the call and never answers (its only volume server gone).
|
|
type blockedDeleteFiler struct{ *memChunkStore }
|
|
|
|
func (f *blockedDeleteFiler) DeleteEntry(ctx context.Context, dir, name string, expected map[string][]byte) error {
|
|
<-ctx.Done()
|
|
return ctx.Err()
|
|
}
|
|
|
|
// A sweep stuck inside the store says so on stderr while it waits, naming
|
|
// the object and how long nothing has moved, so a stalled store is told apart
|
|
// from a slow delete before the no-progress bound ends the command.
|
|
func TestVolumeDeleteReportsAStalledStoreWhileItWaits(t *testing.T) {
|
|
store := newMemChunkStore()
|
|
useMemChunkStore(t, store)
|
|
v := cliVolume(t, store, "stalled-store", "daily")
|
|
dialChunkFiler = func(string) (chunkstore.Filer, func() error, error) {
|
|
return &blockedDeleteFiler{memChunkStore: store}, func() error { return nil }, nil
|
|
}
|
|
addr := startFakeLifecycle(t, &fakeLifecycle{})
|
|
code, out, logs := runChunk(cliArgs("volume-delete", volArgs("volume", v), []string{"--master", addr, "--request-id", "stalled", "--timeout", "800ms"})...)
|
|
if e := chunkErrorOut(t, out); code != 1 || e.Code != "unavailable" {
|
|
t.Fatalf("stalled store: code=%d out=%s logs=%s; want the bounded unavailable answer", code, out, logs)
|
|
}
|
|
var stall string
|
|
for _, l := range strings.Split(logs, "\n") {
|
|
if strings.Contains(l, "no progress for") {
|
|
stall = l
|
|
break
|
|
}
|
|
}
|
|
if stall == "" || !strings.Contains(stall, "waiting on") || !strings.Contains(stall, "deleted 0 of") {
|
|
t.Fatalf("no stall report while the sweep waited: %q (logs %s)", stall, logs)
|
|
}
|
|
}
|
|
|
|
// overlapStderr is a stderr that is NOT safe for concurrent use, as a writer
|
|
// handed to a command need not be: it flags any two overlapping Write calls.
|
|
// Each of the delete's own progress lines ("volume-delete: ...") takes delay.
|
|
type overlapStderr struct {
|
|
inFlight atomic.Int32
|
|
overlapped atomic.Bool
|
|
b bytes.Buffer
|
|
delay time.Duration
|
|
}
|
|
|
|
func (w *overlapStderr) Write(p []byte) (int, error) {
|
|
if w.inFlight.Add(1) > 1 {
|
|
w.overlapped.Store(true)
|
|
}
|
|
defer w.inFlight.Add(-1)
|
|
if bytes.HasPrefix(p, []byte("volume-delete:")) {
|
|
time.Sleep(w.delay)
|
|
}
|
|
return w.b.Write(p)
|
|
}
|
|
|
|
// stuckStderr is a stderr whose writes never return (until the test ends).
|
|
type stuckStderr struct{ release chan struct{} }
|
|
|
|
func (w *stuckStderr) Write(p []byte) (int, error) {
|
|
<-w.release
|
|
return len(p), nil
|
|
}
|
|
|
|
// runVolumeDelete runs the delete on a store that never answers, with stderr
|
|
// as given, and returns its exit status, its stdout and how long it took;
|
|
// within limit, or the test fails.
|
|
func runVolumeDelete(t *testing.T, name string, stderr io.Writer, bound, limit time.Duration) (int, string, time.Duration) {
|
|
t.Helper()
|
|
store := newMemChunkStore()
|
|
useMemChunkStore(t, store)
|
|
v := cliVolume(t, store, name, "daily")
|
|
dialChunkFiler = func(string) (chunkstore.Filer, func() error, error) {
|
|
return &blockedDeleteFiler{memChunkStore: store}, func() error { return nil }, nil
|
|
}
|
|
addr := startFakeLifecycle(t, &fakeLifecycle{})
|
|
args := append([]string{"ops", "chunk"}, cliArgs("volume-delete", volArgs("volume", v), []string{"--master", addr, "--request-id", name, "--timeout", bound.String()})...)
|
|
var out bytes.Buffer
|
|
type result struct {
|
|
code int
|
|
took time.Duration
|
|
}
|
|
got := make(chan result, 1)
|
|
started := time.Now()
|
|
go func() { c := run(args, &out, stderr); got <- result{c, time.Since(started)} }()
|
|
select {
|
|
case r := <-got:
|
|
return r.code, out.String(), r.took
|
|
case <-time.After(limit):
|
|
t.Fatalf("no answer and no exit within %s", limit)
|
|
}
|
|
return 0, "", 0
|
|
}
|
|
|
|
// Diagnostics never gate the answer: with a stderr whose writes never
|
|
// return, the answer reaches stdout and the command exits, as with a fast
|
|
// stderr, within the bound plus a short drain.
|
|
func TestVolumeDeleteAnswerDoesNotWaitOnAStuckStderr(t *testing.T) {
|
|
bound := 400 * time.Millisecond
|
|
var fastErr bytes.Buffer
|
|
fastCode, fastOut, _ := runVolumeDelete(t, "fast-stderr", &fastErr, bound, 10*time.Second)
|
|
stuck := &stuckStderr{release: make(chan struct{})}
|
|
t.Cleanup(func() { close(stuck.release) })
|
|
code, out, took := runVolumeDelete(t, "stuck-stderr", stuck, bound, 10*time.Second)
|
|
fast, got := chunkErrorOut(t, fastOut), chunkErrorOut(t, out)
|
|
if code != fastCode || got.Code != fast.Code || got.Retryable != fast.Retryable || got.Code != "unavailable" {
|
|
t.Fatalf("stuck stderr changed the answer: exit %d %+v, with a fast stderr exit %d %+v", code, got, fastCode, fast)
|
|
}
|
|
if took > bound+time.Second {
|
|
t.Fatalf("the answer and exit took %s on a stuck stderr (bound %s)", took, bound)
|
|
}
|
|
}
|
|
|
|
// The command owns its stderr: with a stderr that is not safe for concurrent
|
|
// use and takes 1 s per progress line, no two writes overlap, and the answer
|
|
// is unchanged.
|
|
func TestVolumeDeleteNeverWritesStderrConcurrently(t *testing.T) {
|
|
slow := &overlapStderr{delay: time.Second}
|
|
code, out, _ := runVolumeDelete(t, "overlap-stderr", slow, 400*time.Millisecond, 10*time.Second)
|
|
if e := chunkErrorOut(t, out); code != 1 || e.Code != "unavailable" {
|
|
t.Fatalf("answer %d %+v", code, e)
|
|
}
|
|
time.Sleep(1500 * time.Millisecond) // any write still in flight lands now
|
|
if slow.overlapped.Load() {
|
|
t.Fatal("two writes to stderr overlapped")
|
|
}
|
|
}
|