mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-04 18:17:16 +01:00
test: F1 retries only an exact listener-bind EADDRINUSE on the attempt's own drawn address, and keeps the first attempt's log (#927 review)
P1: f1WaitAdopted took any log line containing "address already in use" as a taken port. It now passes the attempt's four drawn addresses to bindTakenOn, which accepts only Go's listener error ending a line, "listen tcp <addr>: bind: address already in use", with <addr> one of them; a different port or host, a dial, a quoted copy, text after it, udp or the bare words are not a taken port, so the start fails as before. P2: the retry starts as blockvolume-<key>-attempt2, so the first attempt's log is not truncated, and startWithBindRetry returns the first attempt's error, which the test logs (address and log path) before the retry's answer. Red: TestBindTakenOnOnlyThisAttemptsListener fails with the previous substring rule; TestStartWithBindRetry checks attempt numbers and the kept first error. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
de43d48be4
commit
dccff2f073
2 files changed
+115
-42
No files matched your search
@@ -269,25 +269,29 @@ func f1StartEnv(t *testing.T, cfg f1Config) (*handoffEnv, *daemon) {
|
||||
func f1Up(t *testing.T, env *handoffEnv, key string) *daemon {
|
||||
t.Helper()
|
||||
var d *daemon
|
||||
start := func() error {
|
||||
start := func(attempt int) 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))
|
||||
name := "blockvolume-" + key
|
||||
if attempt > 1 {
|
||||
name = fmt.Sprintf("%s-attempt%d", name, attempt) // its own log: the first attempt's stays whole
|
||||
}
|
||||
d = env.h.start(t, name, 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),
|
||||
[]string{b.data, b.ctrl, b.status, b.nvme})
|
||||
}
|
||||
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
|
||||
var first, err error
|
||||
if key == "v1" {
|
||||
err = start()
|
||||
err = start(1)
|
||||
} else {
|
||||
retried, err = startWithBindRetry(start, fresh)
|
||||
first, 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 first != nil {
|
||||
t.Logf("%s: started again on new ports after its first attempt lost a drawn port: %v", key, first)
|
||||
}
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
@@ -297,10 +301,11 @@ func f1Up(t *testing.T, env *handoffEnv, key string) *daemon {
|
||||
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 {
|
||||
// f1WaitAdopted waits until d logs s. A holder that exits first because the
|
||||
// bind of one of drawn (this attempt's own addresses) found it in use
|
||||
// answers errBindTaken (wrapped, naming the address and the log's end);
|
||||
// any other exit or the bound answers an error naming it.
|
||||
func f1WaitAdopted(d *daemon, timeout time.Duration, s string, drawn []string) error {
|
||||
end := time.Now().Add(timeout)
|
||||
for {
|
||||
if d.line(s) != "" {
|
||||
@@ -311,8 +316,10 @@ func f1WaitAdopted(d *daemon, timeout time.Duration, s string) error {
|
||||
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))
|
||||
if data, err := os.ReadFile(d.log); err == nil {
|
||||
if addr, ok := bindTakenOn(string(data), drawn); ok {
|
||||
return fmt.Errorf("%s exited (%v) before logging %q: %w (%s; log %s); its log ends:\n%s", d.name, d.err, s, errBindTaken, addr, d.log, 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:
|
||||
|
||||
@@ -2,6 +2,8 @@ package main_test
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"regexp"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
@@ -17,20 +19,48 @@ func handoffRange(ephemeralLo int) (lo, hi int, ok bool) {
|
||||
return lo, hi, hi > lo
|
||||
}
|
||||
|
||||
// errBindTaken is a start that failed because a harness-chosen port was
|
||||
// taken before the daemon bound it.
|
||||
// errBindTaken is a start that failed because one of the addresses the
|
||||
// harness drew for it 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
|
||||
// bindTakenRe is Go's net error for a listener whose bind found the address
|
||||
// in use, ending its log line: "listen tcp <addr>: bind: address already in
|
||||
// use". A dial, a quoted copy inside other text, or anything after it on the
|
||||
// line does not match.
|
||||
var bindTakenRe = regexp.MustCompile(`(?:^|[\s:])listen tcp (\S+): bind: address already in use$`)
|
||||
|
||||
// bindTakenOn reports the drawn address whose listener bind failed with
|
||||
// EADDRINUSE in log, if any: only an exact listener-bind error, and only on
|
||||
// one of drawn (this attempt's own addresses). Anything else is not a taken
|
||||
// port, so the caller fails as it would without a retry.
|
||||
func bindTakenOn(log string, drawn []string) (string, bool) {
|
||||
for _, l := range strings.Split(log, "\n") {
|
||||
m := bindTakenRe.FindStringSubmatch(strings.TrimRight(l, "\r"))
|
||||
if m == nil {
|
||||
continue
|
||||
}
|
||||
for _, a := range drawn {
|
||||
if m[1] == a {
|
||||
return a, true
|
||||
}
|
||||
}
|
||||
}
|
||||
return "", false
|
||||
}
|
||||
|
||||
// startWithBindRetry starts a daemon and waits for it; when the start
|
||||
// reports errBindTaken, it asks for fresh ports and starts once more. first
|
||||
// is the first attempt's error when it was retried (kept for the report);
|
||||
// err is the answer that stands. Any other answer, and a second
|
||||
// errBindTaken, is returned as it is.
|
||||
func startWithBindRetry(start func(attempt int) error, freshPorts func()) (first, err error) {
|
||||
err = start(1)
|
||||
if !errors.Is(err, errBindTaken) {
|
||||
return nil, err
|
||||
}
|
||||
first = err
|
||||
freshPorts()
|
||||
return true, start()
|
||||
return first, start(2)
|
||||
}
|
||||
|
||||
func TestHandoffRangeStaysBelowTheEphemeralRange(t *testing.T) {
|
||||
@@ -52,26 +82,62 @@ func TestHandoffRangeStaysBelowTheEphemeralRange(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestStartWithBindRetry(t *testing.T) {
|
||||
other := errors.New("exited before logging adoption")
|
||||
func TestBindTakenOnOnlyThisAttemptsListener(t *testing.T) {
|
||||
drawn := []string{"127.0.0.1:11230", "127.0.0.1:11231", "127.0.0.1:11232", "127.0.0.1:11233"}
|
||||
for _, c := range []struct {
|
||||
name string
|
||||
answers []error
|
||||
retried bool
|
||||
want error
|
||||
starts int
|
||||
freshCnt int
|
||||
name, log string
|
||||
addr string
|
||||
taken bool
|
||||
}{
|
||||
{"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},
|
||||
{"a drawn listener's bind", "2026/12/02 05:01:02 blockvolume: status listener: listen tcp 127.0.0.1:11230: bind: address already in use", "127.0.0.1:11230", true},
|
||||
{"the bare net error", "listen tcp 127.0.0.1:11233: bind: address already in use", "127.0.0.1:11233", true},
|
||||
{"a CRLF line", "listen tcp 127.0.0.1:11231: bind: address already in use\r", "127.0.0.1:11231", true},
|
||||
{"another port", "listen tcp 127.0.0.1:11299: bind: address already in use", "", false},
|
||||
{"another host, same port", "listen tcp 0.0.0.0:11230: bind: address already in use", "", false},
|
||||
{"a dial", "dial tcp 127.0.0.1:11230: connect: address already in use", "", false},
|
||||
{"quoted inside other text", `retry: err="listen tcp 127.0.0.1:11230: bind: address already in use"`, "", false},
|
||||
{"more after it", "listen tcp 127.0.0.1:11230: bind: address already in use; retrying", "", false},
|
||||
{"the words alone", "address already in use", "", false},
|
||||
{"udp", "listen udp 127.0.0.1:11230: bind: address already in use", "", false},
|
||||
{"nothing", "blockvolume: volume v2 adopted as PRIMARY at r2@1", "", false},
|
||||
} {
|
||||
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)
|
||||
addr, taken := bindTakenOn("first line\n"+c.log+"\nlast line", drawn)
|
||||
if addr != c.addr || taken != c.taken {
|
||||
t.Fatalf("%s: (%q, %v); want (%q, %v)", c.name, addr, taken, c.addr, c.taken)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestStartWithBindRetry(t *testing.T) {
|
||||
other := errors.New("exited before logging adoption")
|
||||
taken := func(n int) error { return errors.Join(errBindTaken, errors.New("attempt "+string(rune('0'+n)))) }
|
||||
for _, c := range []struct {
|
||||
name string
|
||||
answers []error
|
||||
first bool
|
||||
want error
|
||||
attempts []int
|
||||
freshCnt int
|
||||
}{
|
||||
{"ok at once", []error{nil}, false, nil, []int{1}, 0},
|
||||
{"another failure is not retried", []error{other}, false, other, []int{1}, 0},
|
||||
{"a taken port, then ok", []error{taken(1), nil}, true, nil, []int{1, 2}, 1},
|
||||
{"a taken port twice: the second answer stands", []error{taken(1), taken(2)}, true, errBindTaken, []int{1, 2}, 1},
|
||||
{"a taken port, then another failure", []error{taken(1), other}, true, other, []int{1, 2}, 1},
|
||||
} {
|
||||
var attempts []int
|
||||
fresh := 0
|
||||
first, err := startWithBindRetry(func(a int) error { attempts = append(attempts, a); return c.answers[a-1] }, func() { fresh++ })
|
||||
if (first != nil) != c.first || (c.first && !errors.Is(first, errBindTaken)) {
|
||||
t.Fatalf("%s: first=%v", c.name, first)
|
||||
}
|
||||
if !errors.Is(err, c.want) || (c.want == nil && err != nil) || fresh != c.freshCnt || len(attempts) != len(c.attempts) {
|
||||
t.Fatalf("%s: err=%v attempts=%v fresh=%d", c.name, err, attempts, fresh)
|
||||
}
|
||||
for i, a := range c.attempts {
|
||||
if attempts[i] != a {
|
||||
t.Fatalf("%s: attempts %v, want %v", c.name, attempts, c.attempts)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user