From de43d48be4feccc889f39f9d71acde97db1673e7 Mon Sep 17 00:00:00 2001 From: pingqiu Date: Tue, 1 Dec 2026 21:42:59 -0800 Subject: [PATCH] test: a holder's harness-chosen port is drawn below the host's ephemeral range, and a start that lost its port is retried once (#927) handoffAddress drew from [10000, 20000) whatever the host's ephemeral range; it now cuts the range at the host's ip_local_port_range low end (handoffRange), as the weed port pairs already do. F1's holder start (f1Up) answers errBindTaken when the holder exits before adopting with "address already in use"; startWithBindRetry then draws the holder's four ports again and starts it once more (v1, whose ports the cluster spec names, is not retried). Any other failure, or a second taken port, fails the test as before. Red first: TestHandoffRangeStaysBelowTheEphemeralRange (a host whose ephemeral range starts at 15000 must get [10000, 15000)) and TestStartWithBindRetry (a taken port, then ok: one retry with fresh ports) fail with the previous behaviour and pass here. Refs #927. Co-Authored-By: Claude Opus 5.5 --- .../planned_handoff_integration_test.go | 14 +++- .../snapshot_ladder_f1_integration_test.go | 59 +++++++++++++- .../cmd/blockvolume/test_port_handoff_test.go | 77 +++++++++++++++++++ 3 files changed, 144 insertions(+), 6 deletions(-) create mode 100644 seaweed-block/cmd/blockvolume/test_port_handoff_test.go diff --git a/seaweed-block/cmd/blockvolume/planned_handoff_integration_test.go b/seaweed-block/cmd/blockvolume/planned_handoff_integration_test.go index d52e19f41f..99e943d1ca 100644 --- a/seaweed-block/cmd/blockvolume/planned_handoff_integration_test.go +++ b/seaweed-block/cmd/blockvolume/planned_handoff_integration_test.go @@ -507,9 +507,10 @@ var handoffPorts struct { } // handoffAddress returns a free loopback address in 10000-19999: below the -// kernel's ephemeral range (32768 and up on Linux), where an outgoing -// connection can take a port while a restarted daemon waits to bind it -// again, and below the 20000-49999 range startWorkspaceWeed draws from. +// kernel's ephemeral range (32768 and up on Linux; the range is cut at the +// host's own ephemeral low end when that is lower: handoffRange), where an +// outgoing connection can take a port while a restarted daemon waits to bind +// it again, and below the 20000-49999 range startWorkspaceWeed draws from. func handoffAddress(t *testing.T) string { t.Helper() handoffPorts.Lock() @@ -517,8 +518,13 @@ func handoffAddress(t *testing.T) string { if handoffPorts.used == nil { handoffPorts.used = map[int]bool{} } + eph, _ := ephemeralPorts() + rlo, rhi, ok := handoffRange(eph) + if !ok { + t.Fatalf("no handoff port range below this host's ephemeral range (from %d)", eph) + } for i := 0; i < 500; i++ { - lo, hi := testPortSpan(10000, 20000) + lo, hi := testPortSpan(rlo, rhi) p := lo + rand.Intn(hi-lo) if handoffPorts.used[p] || !claimHostPort(p) { continue diff --git a/seaweed-block/cmd/blockvolume/snapshot_ladder_f1_integration_test.go b/seaweed-block/cmd/blockvolume/snapshot_ladder_f1_integration_test.go index c485f06a99..3edfb6244d 100644 --- a/seaweed-block/cmd/blockvolume/snapshot_ladder_f1_integration_test.go +++ b/seaweed-block/cmd/blockvolume/snapshot_ladder_f1_integration_test.go @@ -260,15 +260,70 @@ func f1StartEnv(t *testing.T, cfg f1Config) (*handoffEnv, *daemon) { // f1Up starts the blockvolume of key and waits for its volume to be primary // and its frontend ready. +// +// A harness-chosen port can be taken between handoffAddress's free check and +// the holder's bind. When the holder exits before adopting with "address +// already in use", its four ports are drawn again and it starts once more +// (startWithBindRetry). v1's ports are the harness's own and named in the +// cluster spec, so v1 is not retried. func f1Up(t *testing.T, env *handoffEnv, key string) *daemon { t.Helper() + var d *daemon + start := func() error { + b := env.hosts[key] + d = env.h.start(t, "blockvolume-"+key, env.h.volume, env.volArgsFor(b)...) + return f1WaitAdopted(d, 60*time.Second, fmt.Sprintf("blockvolume: volume %s adopted as PRIMARY at %s@", b.volume, b.replica)) + } + fresh := func() { + b := env.hosts[key] + b.data, b.ctrl, b.status, b.nvme = handoffAddress(t), handoffAddress(t), handoffAddress(t), handoffAddress(t) + env.hosts[key] = b + } + var retried bool + var err error + if key == "v1" { + err = start() + } else { + retried, err = startWithBindRetry(start, fresh) + } + if retried { + t.Logf("%s: a harness-chosen port was taken before the holder bound it; started again on new ports", key) + } + if err != nil { + t.Fatal(err) + } b := env.hosts[key] - d := env.h.start(t, "blockvolume-"+key, env.h.volume, env.volArgsFor(b)...) - d.waitLog(t, 60*time.Second, fmt.Sprintf("blockvolume: volume %s adopted as PRIMARY at %s@", b.volume, b.replica)) poll(t, 60*time.Second, key+" never reported its frontend ready", func() bool { return volumeFrontendReady(b.status, b.volume) }) return d } +// f1WaitAdopted waits until d logs s. A holder that exits first because a +// listener's port was taken answers errBindTaken (wrapped, with its log's +// end); any other exit or the bound answers an error naming it. +func f1WaitAdopted(d *daemon, timeout time.Duration, s string) error { + end := time.Now().Add(timeout) + for { + if d.line(s) != "" { + return nil + } + select { + case <-d.done: + if d.line(s) != "" { + return nil + } + if d.line("address already in use") != "" { + return fmt.Errorf("%s exited (%v) before logging %q: %w; its log ends:\n%s", d.name, d.err, s, errBindTaken, d.tail(40)) + } + return fmt.Errorf("%s exited (%v) before logging %q; its log ends:\n%s", d.name, d.err, s, d.tail(40)) + default: + } + if time.Now().After(end) { + return fmt.Errorf("%s did not log %q within %s; its log ends:\n%s", d.name, s, timeout, d.tail(40)) + } + time.Sleep(100 * time.Millisecond) + } +} + // f1StartSources starts the N source volumes. func f1StartSources(t *testing.T, env *handoffEnv, n int) []f1Volume { t.Helper() diff --git a/seaweed-block/cmd/blockvolume/test_port_handoff_test.go b/seaweed-block/cmd/blockvolume/test_port_handoff_test.go new file mode 100644 index 0000000000..10496bbaf4 --- /dev/null +++ b/seaweed-block/cmd/blockvolume/test_port_handoff_test.go @@ -0,0 +1,77 @@ +package main_test + +import ( + "errors" + "testing" +) + +// handoffRange is the range [lo, hi) a harness-chosen daemon port is drawn +// from: [10000, 20000), cut at the host's ephemeral range when that starts +// lower, so an outgoing connection's source port cannot take a port between +// the free check and the daemon's bind. ok is false when nothing is left. +func handoffRange(ephemeralLo int) (lo, hi int, ok bool) { + lo, hi = 10000, 20000 + if ephemeralLo < hi { + hi = ephemeralLo + } + return lo, hi, hi > lo +} + +// errBindTaken is a start that failed because a harness-chosen port was +// taken before the daemon bound it. +var errBindTaken = errors.New("a harness-chosen port was taken before the daemon bound it") + +// startWithBindRetry starts a daemon and waits for it; when the start +// reports errBindTaken, it asks for fresh ports and starts once more. Any +// other answer, and a second errBindTaken, is returned as it is. +func startWithBindRetry(start func() error, freshPorts func()) (retried bool, err error) { + err = start() + if !errors.Is(err, errBindTaken) { + return false, err + } + freshPorts() + return true, start() +} + +func TestHandoffRangeStaysBelowTheEphemeralRange(t *testing.T) { + for _, c := range []struct { + eph, lo, hi int + ok bool + }{ + {32768, 10000, 20000, true}, // the Linux default: the whole range + {20000, 10000, 20000, true}, + {15000, 10000, 15000, true}, // a host whose ephemeral range starts low: cut there + {10001, 10000, 10001, true}, + {10000, 10000, 10000, false}, // nothing left + {1024, 10000, 1024, false}, + } { + lo, hi, ok := handoffRange(c.eph) + if lo != c.lo || hi != c.hi || ok != c.ok { + t.Fatalf("ephemeral from %d: [%d, %d) ok=%v; want [%d, %d) ok=%v", c.eph, lo, hi, ok, c.lo, c.hi, c.ok) + } + } +} + +func TestStartWithBindRetry(t *testing.T) { + other := errors.New("exited before logging adoption") + for _, c := range []struct { + name string + answers []error + retried bool + want error + starts int + freshCnt int + }{ + {"ok at once", []error{nil}, false, nil, 1, 0}, + {"another failure is not retried", []error{other}, false, other, 1, 0}, + {"a taken port, then ok", []error{errBindTaken, nil}, true, nil, 2, 1}, + {"a taken port twice: the second answer stands", []error{errBindTaken, errBindTaken}, true, errBindTaken, 2, 1}, + {"a taken port, then another failure", []error{errBindTaken, other}, true, other, 2, 1}, + } { + starts, fresh := 0, 0 + retried, err := startWithBindRetry(func() error { e := c.answers[starts]; starts++; return e }, func() { fresh++ }) + if retried != c.retried || !errors.Is(err, c.want) || (c.want == nil && err != nil) || starts != c.starts || fresh != c.freshCnt { + t.Fatalf("%s: retried=%v err=%v starts=%d fresh=%d", c.name, retried, err, starts, fresh) + } + } +}