test: start the weed cluster again on fresh ports when it never accepts a write

The harness checks each weed port pair free and releases it before weed
binds it, reserving it only inside the test process. On a runner host shared
with another job, a port can be taken in between; the master then cannot
reach the volume server's gRPC port and the cluster never accepts a write,
failing the test as an environment flake.

A start whose probe never accepts a write is now stopped (each process's log
tail logged) and the cluster is started again on fresh ports in a
subdirectory of the same root, up to 3 starts. A cluster with a
fault-injection front keeps one start.

TestWeedStartAgainOnFreshPortsWhenAPortIsTaken holds the first start's
volume gRPC port with a plain HTTP server (as another job's listener): the
first start fails as the CI runs did, and the second accepts a write. With
one start only, the test fails.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
pingqiuandClaude Opus 5.5 committed 2026-11-26 23:36:28 -08:00
1 parent dfeab5fa45
commit b78a01e239
2 files changed
+110 -3

No files matched your search

@@ -0,0 +1,55 @@
//go:build subprocess && chunkstore_integration
package main_test
import (
"context"
"fmt"
"net"
"net/http"
"testing"
"time"
)
// A weed start whose volume server's gRPC port is held by another process
// (here a plain HTTP server, as a concurrent job's listener on a shared
// runner) never accepts a write: the master's volume allocation gets an
// HTTP 404. The start is discarded and the cluster is started again on fresh
// ports, and that one accepts writes.
func TestWeedStartAgainOnFreshPortsWhenAPortIsTaken(t *testing.T) {
if testing.Short() {
t.Skip("starts weed twice")
}
pick := w4PickPortPair
t.Cleanup(func() { w4PickPortPair = pick })
taken := 0
w4PickPortPair = func(t *testing.T, used map[int]bool, role string) int {
p := w4FreePortPair(t, used)
if role == "volume" && taken == 0 {
taken = p
ln, err := net.Listen("tcp", fmt.Sprintf("%s:%d", testListenHost(), p+10000))
if err != nil {
t.Fatal(err)
}
srv := &http.Server{Handler: http.NotFoundHandler()}
go func() { _ = srv.Serve(ln) }()
t.Cleanup(func() { _ = srv.Close() })
}
return p
}
start := time.Now()
c := startWorkspaceWeed(t)
if taken == 0 {
t.Fatal("the first start did not pick a volume pair")
}
cl := c.client(t, "/swblock/vols/fresh-ports")
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if _, err := cl.Put(ctx, []byte("after the second start")); err != nil {
t.Fatalf("the cluster started again on fresh ports does not accept a write: %v", err)
}
if c.master == "" || c.dir == "" {
t.Fatal("no cluster")
}
t.Logf("volume port %d's gRPC port %d was taken; the second start accepted writes after %v", taken, taken+10000, time.Since(start).Round(time.Second))
}
@@ -94,6 +94,10 @@ func w4WaitTCP(t *testing.T, addr string, d time.Duration) {
t.Fatalf("%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())
@@ -116,17 +120,65 @@ type w4WeedOpts struct {
FilerFront func(*w4WeedCluster)
}
// weedStarts is how many times a cluster that 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 the master then
// cannot reach the volume server's gRPC port.
var weedStarts = 3
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")
}
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 {
at = filepath.Join(dir, fmt.Sprintf("start-%d", attempt))
}
c, err := startWorkspaceWeedOnce(t, at, weed, o)
if err == nil {
return c
}
if attempt >= starts {
t.Fatalf("cluster never accepted a write (start %d of %d): %v", attempt, starts, err)
}
t.Logf("weed start %d never accepted a write (%v): master %s, filer %s; starting again on fresh ports", attempt, err, c.master, c.filerGRPC)
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()
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 the probe's error when it 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, vp, fp := w4FreePortPair(t, used), w4FreePortPair(t, used), w4FreePortPair(t, used)
mp, vp, fp := w4PickPortPair(t, used, "master"), w4PickPortPair(t, used, "volume"), w4PickPortPair(t, used, "filer")
c := &w4WeedCluster{
master: fmt.Sprintf("%s:%d", testListenHost(), mp),
filerHTTP: fmt.Sprintf("%s:%d", testListenHost(), fp),
@@ -183,11 +235,11 @@ func startWorkspaceWeedWith(t *testing.T, dir string, o w4WeedOpts) *w4WeedClust
break
}
if time.Now().After(deadline) {
t.Fatalf("cluster never accepted a write: %v", err)
return c, err
}
time.Sleep(500 * time.Millisecond)
}
return c
return c, nil
}
// w4WeedProc is one weed process; a restart replaces it under the same name,