mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-06 11:07:31 +01:00
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>
385 lines
12 KiB
Go
385 lines
12 KiB
Go
//go:build subprocess && chunkstore_integration
|
|
|
|
package main_test
|
|
|
|
// The process helper follows the existing chunkstore integration harness.
|
|
// Only real loopback SeaweedFS processes and per-test temporary state are used.
|
|
import (
|
|
"context"
|
|
crand "crypto/rand"
|
|
"encoding/hex"
|
|
"fmt"
|
|
"github.com/seaweedfs/seaweed-block/core/frontend/chunkstore"
|
|
"github.com/seaweedfs/seaweed-block/core/frontend/chunkstore/layout"
|
|
"github.com/seaweedfs/seaweed-block/internal/weedtest"
|
|
"math"
|
|
"net"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
type w4WeedCluster struct {
|
|
master, filerHTTP, filerGRPC string
|
|
dir string
|
|
weed string
|
|
procs map[string]*w4WeedProc
|
|
reported sync.Once // its one failure report (registerWeedReport)
|
|
discarded bool // a failed start attempt, already reported
|
|
}
|
|
|
|
// testListenHost is the address the test harness binds and advertises for
|
|
// its weed cluster, blockmaster and blockvolume ports: 127.0.0.1 unless
|
|
// SWBLOCK_TEST_LISTEN_HOST names another (a lab run whose processes span two
|
|
// hosts). Unset, every test behaves exactly as before.
|
|
func testListenHost() string {
|
|
if h := os.Getenv("SWBLOCK_TEST_LISTEN_HOST"); h != "" {
|
|
return h
|
|
}
|
|
return "127.0.0.1"
|
|
}
|
|
|
|
func w4FreePortPair(t *testing.T, used map[int]bool) int {
|
|
t.Helper()
|
|
p, ok := weedtest.FreeWeedPair(testListenHost(), used)
|
|
if !ok {
|
|
t.Fatal("no free port pair")
|
|
}
|
|
return p
|
|
}
|
|
|
|
func w4WaitTCP(t *testing.T, addr string, d time.Duration) {
|
|
t.Helper()
|
|
if err := w4ListeningWithin(addr, d); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
// w4ListeningWithin waits until addr accepts a TCP connection.
|
|
func w4ListeningWithin(addr string, d time.Duration) error {
|
|
deadline := time.Now().Add(d)
|
|
for time.Now().Before(deadline) {
|
|
if c, err := net.DialTimeout("tcp", addr, 200*time.Millisecond); err == nil {
|
|
c.Close()
|
|
return nil
|
|
}
|
|
time.Sleep(100 * time.Millisecond)
|
|
}
|
|
return fmt.Errorf("%s not listening after %v", addr, d)
|
|
}
|
|
|
|
// w4PickPortPair picks each weed server's port pair (HTTP p, gRPC p+10000);
|
|
// a test replaces it to make a start collide with another listener.
|
|
var w4PickPortPair = func(t *testing.T, used map[int]bool, _ string) int { return w4FreePortPair(t, used) }
|
|
|
|
func startWorkspaceWeed(t *testing.T) *w4WeedCluster {
|
|
t.Helper()
|
|
return startWorkspaceWeedAt(t, t.TempDir())
|
|
}
|
|
|
|
// A caller-owned root lets process qualification retain its filer evidence
|
|
// when external-service cleanup is uncertain. Existing tests keep TempDir.
|
|
func startWorkspaceWeedAt(t *testing.T, dir string) *w4WeedCluster {
|
|
t.Helper()
|
|
return startWorkspaceWeedWith(t, dir, w4WeedOpts{VolumeSizeLimitMB: 256, VolumeMax: 32})
|
|
}
|
|
|
|
// w4WeedOpts sizes the weed volume server: VolumeMax volumes of
|
|
// VolumeSizeLimitMB each (the default holds about 8 GiB of chunks).
|
|
type w4WeedOpts struct {
|
|
VolumeSizeLimitMB, VolumeMax int
|
|
// A fault-injection front keeps the advertised filer endpoint unchanged.
|
|
// The one real filer binds a private loopback address; no second filer is registered.
|
|
FilerBind string
|
|
FilerFront func(*w4WeedCluster)
|
|
}
|
|
|
|
// weedStarts is how many times a cluster that does not start (a server
|
|
// never listens, or the cluster never accepts a write) is started again on
|
|
// fresh ports. A port pair is checked free and then released before weed
|
|
// binds it; another process on the host (a concurrent job on a shared
|
|
// runner) can take it in between, and a server then cannot reach another's
|
|
// gRPC port. Each server's pair is picked just before that server starts,
|
|
// which keeps that window short.
|
|
var weedStarts = 3
|
|
|
|
// w4ServerStarted, when set, runs after each weed server of a start is
|
|
// launched (role "master", "volume", "filer"); a test uses it to act
|
|
// between two servers' starts.
|
|
var w4ServerStarted func(role string)
|
|
|
|
func startWorkspaceWeedWith(t *testing.T, dir string, o w4WeedOpts) *w4WeedCluster {
|
|
t.Helper()
|
|
weed := os.Getenv("CHUNKSTORE_WEED")
|
|
if weed == "" {
|
|
t.Skip("CHUNKSTORE_WEED not set")
|
|
}
|
|
// A fault-injection front is built again for each start, on that
|
|
// start's ports (o.FilerFront runs once the filer listens).
|
|
starts := weedStarts
|
|
for attempt := 1; ; attempt++ {
|
|
at := dir
|
|
if attempt > 1 {
|
|
at = filepath.Join(dir, fmt.Sprintf("start-%d", attempt))
|
|
}
|
|
c, err := startWorkspaceWeedOnce(t, at, weed, o)
|
|
if err == nil {
|
|
return c
|
|
}
|
|
// The evidence is taken now, before the servers are stopped.
|
|
if attempt >= starts {
|
|
t.Fatal(weedStartFailure(attempt, starts, err, c.servers()))
|
|
}
|
|
t.Logf("master %s, filer %s; starting again on fresh ports: %s", c.master, c.filerGRPC, weedStartFailure(attempt, starts, err, c.servers()))
|
|
c.discard(t)
|
|
}
|
|
}
|
|
|
|
// discard stops a cluster that failed its start and logs each process's
|
|
// log tail; its own cleanups then find the processes already gone.
|
|
func (c *w4WeedCluster) discard(t *testing.T) {
|
|
t.Helper()
|
|
c.discarded = true
|
|
for name, p := range c.procs {
|
|
if !c.stopProcWithin(name, 5*time.Second) {
|
|
t.Errorf("weed %s did not exit after kill", name)
|
|
}
|
|
b, _ := os.ReadFile(p.log.Name())
|
|
if len(b) > 4000 {
|
|
b = b[len(b)-4000:]
|
|
}
|
|
t.Logf("---- discarded start: %s log tail ----\n%s", name, b)
|
|
}
|
|
}
|
|
|
|
// startWorkspaceWeedOnce starts one cluster on freshly checked ports in dir
|
|
// and returns an error when a server never listens or the cluster never
|
|
// accepts a write.
|
|
func startWorkspaceWeedOnce(t *testing.T, dir, weed string, o w4WeedOpts) (*w4WeedCluster, error) {
|
|
t.Helper()
|
|
if err := os.MkdirAll(dir, 0700); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
used := map[int]bool{}
|
|
mp := w4PickPortPair(t, used, "master")
|
|
c := &w4WeedCluster{
|
|
master: fmt.Sprintf("%s:%d", testListenHost(), mp),
|
|
dir: dir,
|
|
weed: weed,
|
|
}
|
|
c.procs = map[string]*w4WeedProc{}
|
|
run := func(name string, args ...string) {
|
|
c.startProc(t, name, args...)
|
|
if w4ServerStarted != nil {
|
|
defer w4ServerStarted(name)
|
|
}
|
|
t.Cleanup(func() {
|
|
p := c.procs[name]
|
|
if !c.stopProcWithin(name, 5*time.Second) {
|
|
t.Errorf("weed %s did not exit after kill", name)
|
|
}
|
|
p.log.Close()
|
|
if t.Failed() {
|
|
b, _ := os.ReadFile(p.log.Name())
|
|
if len(b) > 4000 {
|
|
b = b[len(b)-4000:]
|
|
}
|
|
t.Logf("---- %s log tail ----\n%s", name, b)
|
|
}
|
|
})
|
|
registerWeedReport(t, &c.reported, func() bool { return c.discarded }, c.servers)
|
|
}
|
|
for _, d := range []string{"m", "v", "f"} {
|
|
if err := os.MkdirAll(filepath.Join(dir, d), 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
run("master", "master", "-ip="+testListenHost(), fmt.Sprintf("-port=%d", mp), "-mdir="+filepath.Join(dir, "m"), fmt.Sprintf("-volumeSizeLimitMB=%d", o.VolumeSizeLimitMB), "-defaultReplication=000")
|
|
if err := w4ListeningWithin(c.master, 20*time.Second); err != nil {
|
|
return c, err
|
|
}
|
|
if err := c.exitedEarly("master"); err != nil {
|
|
return c, err
|
|
}
|
|
vp := w4PickPortPair(t, used, "volume")
|
|
run("volume", "volume", "-ip="+testListenHost(), fmt.Sprintf("-port=%d", vp), "-dir="+filepath.Join(dir, "v"), "-master="+c.master, fmt.Sprintf("-max=%d", o.VolumeMax))
|
|
fp := w4PickPortPair(t, used, "filer")
|
|
c.filerHTTP = fmt.Sprintf("%s:%d", testListenHost(), fp)
|
|
c.filerGRPC = fmt.Sprintf("%s:%d", testListenHost(), fp+10000)
|
|
// Each cluster's filer has its own filer group: a filer of another
|
|
// cluster that reaches this cluster's master (a port pair two concurrent
|
|
// starts picked alike) registers in its own group, so neither cluster's
|
|
// single-filer census counts it.
|
|
filerArgs := []string{"filer", "-ip=" + testListenHost(), fmt.Sprintf("-port=%d", fp), "-master=" + c.master, "-defaultStoreDir=" + filepath.Join(dir, "f"), "-filerGroup=" + w4FilerGroup(t)}
|
|
if o.FilerBind != "" {
|
|
filerArgs = append(filerArgs, "-ip.bind="+o.FilerBind)
|
|
c.filerHTTP = net.JoinHostPort(o.FilerBind, fmt.Sprint(fp))
|
|
c.filerGRPC = net.JoinHostPort(o.FilerBind, fmt.Sprint(fp+10000))
|
|
}
|
|
run("filer", filerArgs...)
|
|
if err := w4ListeningWithin(c.filerGRPC, 30*time.Second); err != nil {
|
|
return c, err
|
|
}
|
|
if o.FilerFront != nil {
|
|
o.FilerFront(c)
|
|
}
|
|
|
|
// Wait until an assign succeeds (volume registered, volumes growable).
|
|
cl := c.client(t, "/swblock/vols/probe")
|
|
deadline := time.Now().Add(60 * time.Second)
|
|
for {
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
_, err := cl.Put(ctx, []byte("probe"))
|
|
cancel()
|
|
if err == nil {
|
|
break
|
|
}
|
|
if time.Now().After(deadline) {
|
|
return c, err
|
|
}
|
|
time.Sleep(500 * time.Millisecond)
|
|
}
|
|
// A master that could not bind its port has exited by now; the port
|
|
// answered for another cluster's master, which the servers above then
|
|
// joined. Start again on fresh ports.
|
|
if err := c.exitedEarly("master"); err != nil {
|
|
return c, err
|
|
}
|
|
return c, nil
|
|
}
|
|
|
|
// exitedEarly reports a server of this start that has already exited: its
|
|
// port answered, but for another process.
|
|
func (c *w4WeedCluster) exitedEarly(name string) error {
|
|
p := c.procs[name]
|
|
if p == nil || p.done == nil {
|
|
return nil
|
|
}
|
|
select {
|
|
case <-p.done:
|
|
return fmt.Errorf("weed %s exited (%v) although its port answered: another process holds that port", name, *p.exit)
|
|
default:
|
|
return nil
|
|
}
|
|
}
|
|
|
|
// w4FilerGroup is a filer group no other cluster on the host uses.
|
|
func w4FilerGroup(t *testing.T) string {
|
|
t.Helper()
|
|
b := make([]byte, 6)
|
|
if _, err := crand.Read(b); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return "swt" + hex.EncodeToString(b)
|
|
}
|
|
|
|
// w4WeedProc is one weed process; a restart replaces it under the same name,
|
|
// arguments and log, so the cluster's cleanup always ends the current one.
|
|
type w4WeedProc struct {
|
|
args []string
|
|
log *os.File
|
|
cmd *exec.Cmd
|
|
done chan struct{}
|
|
exit *error // this start's exit status, set before done closes (a fresh one per start)
|
|
}
|
|
|
|
func (c *w4WeedCluster) startProc(t *testing.T, name string, args ...string) {
|
|
t.Helper()
|
|
p := c.procs[name]
|
|
if p == nil {
|
|
logf, err := os.Create(filepath.Join(c.dir, name+".log"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
p = &w4WeedProc{args: args, log: logf}
|
|
c.procs[name] = p
|
|
}
|
|
cmd := exec.Command(c.weed, p.args...)
|
|
cmd.Stdout, cmd.Stderr = p.log, p.log
|
|
cmd.Dir = c.dir
|
|
if err := cmd.Start(); err != nil {
|
|
t.Fatalf("start %s: %v", name, err)
|
|
}
|
|
p.cmd, p.done, p.exit = cmd, make(chan struct{}), new(error)
|
|
go func(done chan struct{}, exit *error) { *exit = cmd.Wait(); close(done) }(p.done, p.exit)
|
|
}
|
|
|
|
// servers is the cluster's servers for weedState and weedFailureReport.
|
|
func (c *w4WeedCluster) servers() []weedServer {
|
|
out := make([]weedServer, 0, len(c.procs))
|
|
for name, p := range c.procs {
|
|
s := weedServer{name: name, log: p.log.Name()}
|
|
if p.done == nil { // its launch failed: it never ran
|
|
s.exited, s.err = true, fmt.Errorf("not started")
|
|
out = append(out, s)
|
|
continue
|
|
}
|
|
select {
|
|
case <-p.done:
|
|
s.exited, s.err = true, *p.exit
|
|
default:
|
|
}
|
|
out = append(out, s)
|
|
}
|
|
return out
|
|
}
|
|
|
|
// stopProcWithin kills name's current process and reports whether it exited.
|
|
func (c *w4WeedCluster) stopProcWithin(name string, d time.Duration) bool {
|
|
p := c.procs[name]
|
|
if p == nil || p.cmd == nil {
|
|
return true
|
|
}
|
|
_ = p.cmd.Process.Kill()
|
|
select {
|
|
case <-p.done:
|
|
return true
|
|
case <-time.After(d):
|
|
return false
|
|
}
|
|
}
|
|
|
|
// stopProc and restartProc take one weed process (master, volume, filer) down
|
|
// and bring it back with the same ports and data directory.
|
|
func (c *w4WeedCluster) stopProc(t *testing.T, name string) {
|
|
t.Helper()
|
|
if !c.stopProcWithin(name, 10*time.Second) {
|
|
t.Fatalf("weed %s did not exit after kill", name)
|
|
}
|
|
}
|
|
|
|
func (c *w4WeedCluster) restartProc(t *testing.T, name string) {
|
|
t.Helper()
|
|
c.startProc(t, name)
|
|
}
|
|
|
|
func (c *w4WeedCluster) client(t *testing.T, assignPath string) *chunkstore.SWFS {
|
|
t.Helper()
|
|
cl, err := chunkstore.DialSWFS(chunkstore.SWFSConfig{FilerGRPC: c.filerGRPC, Collection: "swblock", AssignPath: assignPath})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Cleanup(func() { _ = cl.Close() })
|
|
return cl
|
|
}
|
|
|
|
// remote reads ref's HEAD under base as the chunk engine sees it, through the
|
|
// layout remote of an observer that holds nothing: its epoch, holder and
|
|
// claim match no HEAD, so it never rolls a session back or writes.
|
|
func (c *w4WeedCluster) remote(t *testing.T, base string, ref chunkstore.VolumeRef) *chunkstore.LayoutRemote {
|
|
t.Helper()
|
|
cl := c.client(t, base)
|
|
cat, err := chunkstore.NewBCatalog(cl, cl, base)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
r, err := chunkstore.NewLayoutRemote(cat, ref, layout.HolderID{Epoch: math.MaxUint64, Holder: "test-observer", ClaimID: "observer"})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return r
|
|
}
|