Files
seaweedfs/seaweed-block/cmd/blockvolume/lost_store_exit_test.go
T
Ping QiuandClaude Opus 5.5 a5686e7fa7 Sync seaweed-block v1.1.1 into seaweed-block/ (#13411)
Brings seaweed-block main (v1.1.1 plus the docs and a test fix after it)
into seaweed-block/ as one commit. Only files under seaweed-block/ change,
and the subtree equals seaweed-block main exactly. The module's full
history stays in the seaweed-block repository.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-04 19:04:53 -08:00

195 lines
7.1 KiB
Go

package main
import (
"bytes"
"context"
"errors"
"fmt"
"net"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"sync"
"testing"
"time"
control "github.com/seaweedfs/seaweed-block/core/rpc/control"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
// storeRecordMaster answers only LifecycleService.GetReplicaStore, by mode.
type storeRecordMaster struct {
control.UnimplementedLifecycleServiceServer
mu sync.Mutex
mode string // "creating", "ended", "older", "unavailable"
}
func (m *storeRecordMaster) set(mode string) { m.mu.Lock(); m.mode = mode; m.mu.Unlock() }
func (m *storeRecordMaster) GetReplicaStore(_ context.Context, q *control.GetReplicaStoreRequest) (*control.GetReplicaStoreResponse, error) {
m.mu.Lock()
defer m.mu.Unlock()
switch m.mode {
case "creating":
return &control.GetReplicaStoreResponse{Listed: true}, nil
case "ended": // a recorded store that is never the one on disk here
return &control.GetReplicaStoreResponse{Listed: true, CreationEnded: true, StoreId: bytes.Repeat([]byte{0xab}, 16)}, nil
case "ended-unrecorded": // creation ended, no store recorded for this replica
return &control.GetReplicaStoreResponse{Listed: true, CreationEnded: true}, nil
case "older":
return nil, status.Error(codes.Unimplemented, "unknown method GetReplicaStore")
}
return nil, status.Error(codes.Unavailable, "lifecycle store unavailable")
}
// killAndJoin kills the holder and waits for it, bounded: a process that
// cannot be joined stops the test binary (no fixture cleanup or later test
// runs beside it), so the output is only read after a proven wait.
func killAndJoin(cmd *exec.Cmd, done <-chan error) {
_ = cmd.Process.Kill()
select {
case <-done:
case <-time.After(30 * time.Second):
fmt.Fprintln(os.Stderr, "FAIL: the killed blockvolume did not exit within 30 s; stopping the test binary")
os.Exit(1)
}
}
// runHolder starts the blockvolume binary and returns its exit (nil error and
// running=true when it was still running at the deadline and was killed);
// output is read only after the process has been waited for.
func runHolder(t *testing.T, bin, master, root, locks string, until func() bool, limit time.Duration) (exit int, running bool, out string) {
t.Helper()
var buf bytes.Buffer
cmd := exec.Command(bin, "--master", master, "--server-id", "s1", "--volume-id", "v1", "--replica-id", "r1",
"--data-addr", "127.0.0.1:0", "--ctrl-addr", "127.0.0.1:0", "--durable-root", root, "--durable-impl", "walstore",
"--durable-blocks", "64", "--"+volumeLockDirTestOnlyFlag, locks)
cmd.Stdout, cmd.Stderr = &buf, &buf
if err := cmd.Start(); err != nil {
t.Fatal(err)
}
done := make(chan error, 1)
go func() { done <- cmd.Wait() }()
deadline := time.After(limit)
tick := time.NewTicker(50 * time.Millisecond)
defer tick.Stop()
for {
select {
case err := <-done:
var ee *exec.ExitError
if errors.As(err, &ee) {
return ee.ExitCode(), false, buf.String()
}
return 0, false, buf.String()
case <-tick.C:
if until != nil && until() {
killAndJoin(cmd, done)
return 0, true, buf.String()
}
case <-deadline:
killAndJoin(cmd, done)
return 0, true, buf.String()
}
}
}
// A plain walstore holder asks the master for its replica's store record
// before it creates or writes anything. Composed on the real binary: while
// the volume is creating an empty state directory creates the store; once
// creation ended, a store other than the recorded one, and a wiped state
// directory, exit 78 store_lost; an older master (no GetReplicaStore) and an
// unreadable record exit 75 with an empty state directory left empty.
func TestBlockvolumeAsksTheMasterBeforeCreatingItsStore(t *testing.T) {
if testing.Short() {
t.Skip("builds and runs the blockvolume binary")
}
dir := t.TempDir()
bin := filepath.Join(dir, "blockvolume")
if runtime.GOOS == "windows" {
bin += ".exe"
}
// The build writes to a file, so its exit is its whole join (no pipe
// copy outlives it). A build that does not finish in time stops the test
// binary before the temporary output is removed: go build's own children
// may still be writing there.
buildLog, err := os.Create(filepath.Join(dir, "build.log"))
if err != nil {
t.Fatal(err)
}
build := exec.Command("go", "build", "-o", bin, "github.com/seaweedfs/seaweed-block/cmd/blockvolume")
build.Stdout, build.Stderr = buildLog, buildLog
if err := build.Start(); err != nil {
t.Fatal(err)
}
built := make(chan error, 1)
go func() { built <- build.Wait() }()
select {
case err := <-built:
_ = buildLog.Close()
if err != nil {
out, _ := os.ReadFile(buildLog.Name())
t.Fatalf("build: %v\n%s", err, out)
}
case <-time.After(5 * time.Minute):
_ = build.Process.Kill()
fmt.Fprintln(os.Stderr, "FAIL: go build of blockvolume did not finish within 5 min; stopping the test binary before its temporary output is removed")
os.Exit(1)
}
ln, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatal(err)
}
m := &storeRecordMaster{}
srv := grpc.NewServer()
control.RegisterLifecycleServiceServer(srv, m)
go func() { _ = srv.Serve(ln) }()
defer srv.Stop()
root, locks := t.TempDir(), t.TempDir()
store := filepath.Join(root, "v1.bin")
empty := func(t *testing.T) {
t.Helper()
if entries, err := os.ReadDir(root); err != nil || len(entries) != 0 {
t.Fatalf("the refused holder wrote into its state directory: %v %v", entries, err)
}
}
m.set("creating")
created := func() bool { _, err := os.Stat(store); return err == nil }
if _, running, out := runHolder(t, bin, ln.Addr().String(), root, locks, created, 60*time.Second); !running || !created() {
t.Fatalf("a creating volume did not create its store:\n%s", out)
}
m.set("ended") // the store here is not the recorded one
if code, _, out := runHolder(t, bin, ln.Addr().String(), root, locks, nil, 60*time.Second); code != exitWorkspaceSuperseded || !strings.Contains(out, "store_lost") {
t.Fatalf("a store other than the recorded one: exit %d\n%s", code, out)
}
if err := os.RemoveAll(root); err != nil {
t.Fatal(err)
}
if err := os.MkdirAll(root, 0o755); err != nil {
t.Fatal(err)
}
if code, _, out := runHolder(t, bin, ln.Addr().String(), root, locks, nil, 60*time.Second); code != exitWorkspaceSuperseded || !strings.Contains(out, "store_lost") {
t.Fatalf("a wiped state directory after creation ended: exit %d\n%s", code, out)
}
empty(t)
m.set("ended-unrecorded") // no attestation for this replica: creation ended is what counts
if code, _, out := runHolder(t, bin, ln.Addr().String(), root, locks, nil, 60*time.Second); code != exitWorkspaceSuperseded || !strings.Contains(out, "store_lost") {
t.Fatalf("a wiped state directory, creation ended with no recorded store: exit %d\n%s", code, out)
}
empty(t)
for mode, reason := range map[string]string{"older": "master_too_old", "unavailable": "store_record_unreadable"} {
m.set(mode)
if code, _, out := runHolder(t, bin, ln.Addr().String(), root, locks, nil, 60*time.Second); code != exitWorkspaceRetry || !strings.Contains(out, reason) {
t.Fatalf("%s master, empty state: exit %d, want 75 %s\n%s", mode, code, reason, out)
}
empty(t)
}
}