Merge pull request #687 from seaweedfs/test/weed-pick-at-start

test: pick each weed server's ports just before it starts
This commit is contained in:
Ping Qiu authored and GitHub committed 2026-11-27 18:40:59 -08:00
commit 3dcf21e6ac
2 files changed
+69 -10

No files matched your search

@@ -7,6 +7,8 @@ import (
"fmt"
"net"
"net/http"
"path/filepath"
"strings"
"testing"
"time"
)
@@ -95,3 +97,50 @@ func TestWeedStartAgainOnFreshPortsWhenAServerNeverListens(t *testing.T) {
}
t.Logf("master port %d's gRPC port %d was taken; the second start accepted writes after %v", taken, taken+10000, time.Since(start).Round(time.Second))
}
// The filer's ports are picked only after the master is up, so a listener
// that appears on the host while the master starts (another job's weed, as
// on the self-hosted runner) cannot hold the port the filer will bind: here
// the hook takes the filer's gRPC port if it was already picked when the
// master started. The cluster must come up on its first start.
func TestWeedPicksTheFilerPortsAfterTheMasterStarts(t *testing.T) {
if testing.Short() {
t.Skip("starts weed")
}
pick, started := w4PickPortPair, w4ServerStarted
t.Cleanup(func() { w4PickPortPair, w4ServerStarted = pick, started })
picked := map[string]int{}
w4PickPortPair = func(t *testing.T, used map[int]bool, role string) int {
p := w4FreePortPair(t, used)
picked[role] = p
return p
}
taken := 0
w4ServerStarted = func(role string) {
fp, ok := picked["filer"]
if role != "master" || !ok || taken != 0 {
return
}
taken = fp + 10000
ln, err := net.Listen("tcp", fmt.Sprintf("%s:%d", testListenHost(), taken))
if err != nil {
t.Fatal(err)
}
srv := &http.Server{Handler: http.NotFoundHandler()}
go func() { _ = srv.Serve(ln) }()
t.Cleanup(func() { _ = srv.Close() })
}
c := startWorkspaceWeed(t)
if taken != 0 {
t.Errorf("the filer's gRPC port %d was picked before the master started and was taken meanwhile", taken)
}
if strings.HasPrefix(filepath.Base(c.dir), "start-") {
t.Errorf("the cluster came up only on a later start (%s)", c.dir)
}
cl := c.client(t, "/swblock/vols/pick-at-start")
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if _, err := cl.Put(ctx, []byte("first start")); err != nil {
t.Fatalf("the cluster does not accept a write: %v", err)
}
}
@@ -135,19 +135,24 @@ type w4WeedOpts struct {
// 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.
// 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
if o.FilerFront != nil {
starts = 1 // a fault-injection front is wired to one cluster only
}
for attempt := 1; ; attempt++ {
at := dir
if attempt > 1 {
@@ -190,17 +195,18 @@ func startWorkspaceWeedOnce(t *testing.T, dir, weed string, o w4WeedOpts) (*w4We
t.Fatal(err)
}
used := map[int]bool{}
mp, vp, fp := w4PickPortPair(t, used, "master"), w4PickPortPair(t, used, "volume"), w4PickPortPair(t, used, "filer")
mp := w4PickPortPair(t, used, "master")
c := &w4WeedCluster{
master: fmt.Sprintf("%s:%d", testListenHost(), mp),
filerHTTP: fmt.Sprintf("%s:%d", testListenHost(), fp),
filerGRPC: fmt.Sprintf("%s:%d", testListenHost(), fp+10000),
dir: dir,
weed: weed,
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) {
@@ -225,7 +231,11 @@ func startWorkspaceWeedOnce(t *testing.T, dir, weed string, o w4WeedOpts) (*w4We
if err := w4ListeningWithin(c.master, 20*time.Second); 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)
filerArgs := []string{"filer", "-ip=" + testListenHost(), fmt.Sprintf("-port=%d", fp), "-master=" + c.master, "-defaultStoreDir=" + filepath.Join(dir, "f")}
if o.FilerBind != "" {
filerArgs = append(filerArgs, "-ip.bind="+o.FilerBind)