mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-11-20 03:13:53 +01:00
* util/http: probe an unanswering server in the background, not on a read Once the retry interval passed, the first read put the dead server first and waited out the full dial timeout before trying a live replica. Keep it last and dial it from a background probe with a short timeout instead. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * util/http: try every server of a machine that does not answer last Unreachability was kept per host:port, so a powered-off node running one volume server per disk cost one stalled read per server before each was known dead. A dial that times out or finds no route now marks the whole machine; a refused connection still marks only that port and shows the machine is up. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * util/http: probe an unanswering server with an HTTP request A bare TCP connect cleared servers that accept connections but never send response headers, and could not dial an address without an explicit port. Probe with a HEAD request through the global client instead, and have the tests wait for the probe to settle. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * util/http: keep a timed-out port last after a sibling port answers A dial timeout stored only the machine verdict, so a success on another port of that machine cleared it and the port that timed out went back to the front. Record the port as well as the machine. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * util/http: classify refused and unroutable dials with Winsock errnos on Windows Windows reports these as WSAECONNREFUSED, WSAEHOSTUNREACH and friends, which errors.Is does not match against the syscall constants. Test the refusal with a real dial to a closed port. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * util/http: wait for background probes through a WaitGroup in tests A refused probe can finish before the test reads the claimed record, so inferring completion from the record's timestamp could wait out the deadline on a probe that already settled. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * util/http: key machine verdicts apart from hosts without a port A host without an explicit port is also its machine name, so a sibling port answering cleared the timed-out host along with the machine verdict. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * util/http: group a bracketed port-less IPv6 host with its machine net.SplitHostPort rejects "[2001:db8::9]", so the machine key kept the brackets and missed the verdict stored for "[2001:db8::9]:9501". Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
136 lines
3.7 KiB
Go
136 lines
3.7 KiB
Go
package http
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"net"
|
|
"net/http"
|
|
"net/url"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
)
|
|
|
|
// unreachableRetryInterval is how long a server that failed to answer is tried
|
|
// last before it is probed again.
|
|
const unreachableRetryInterval = 30 * time.Second
|
|
|
|
// reachabilityProbeTimeout bounds the background request that checks whether
|
|
// an unanswering server is back.
|
|
const reachabilityProbeTimeout = 5 * time.Second
|
|
|
|
type unanswered struct {
|
|
at time.Time
|
|
addr string
|
|
}
|
|
|
|
// unreachable maps a host, and a machine whose dial got no answer, to its last
|
|
// failure. A dead replica sits in the location list of every volume it held,
|
|
// so this is kept per host rather than per volume.
|
|
var unreachable sync.Map
|
|
|
|
// machineKey keys a machine apart from a host without an explicit port.
|
|
type machineKey string
|
|
|
|
var probes sync.WaitGroup
|
|
|
|
// recordUnreachable keys a failure by machine when the dial never reached it,
|
|
// so every server on a powered-off node is tried last after one failure.
|
|
func recordUnreachable(host string, err error) {
|
|
failure := unanswered{at: time.Now(), addr: host}
|
|
machine := machineOf(host)
|
|
unreachable.Store(host, failure)
|
|
if hostDown(err) {
|
|
unreachable.Store(machine, failure)
|
|
} else if connRefused(err) {
|
|
unreachable.Delete(machine)
|
|
}
|
|
}
|
|
|
|
// recordReachable clears the machine verdict too, but a sibling port that
|
|
// failed on its own stays last until it answers.
|
|
func recordReachable(host string) {
|
|
unreachable.Delete(host)
|
|
unreachable.Delete(machineOf(host))
|
|
}
|
|
|
|
// hostDown reports whether a dial got no answer from the machine at all, unlike
|
|
// a refused connection, which shows the machine is up.
|
|
func hostDown(err error) bool {
|
|
var opErr *net.OpError
|
|
if !errors.As(err, &opErr) || opErr.Op != "dial" {
|
|
return false
|
|
}
|
|
return opErr.Timeout() || noRoute(err)
|
|
}
|
|
|
|
// ReachableFirst orders urls so that hosts which recently failed to answer
|
|
// come last, keeping the order within each group. Once the retry interval has
|
|
// passed, such a host is probed in the background rather than by a read, so a
|
|
// dead replica never costs a read the dial timeout again.
|
|
func ReachableFirst(urls []string) []string {
|
|
var reachable, failed []string
|
|
for _, u := range urls {
|
|
if isUnanswering(hostOf(u)) {
|
|
failed = append(failed, u)
|
|
} else {
|
|
reachable = append(reachable, u)
|
|
}
|
|
}
|
|
if failed == nil {
|
|
return urls
|
|
}
|
|
return append(reachable, failed...)
|
|
}
|
|
|
|
func isUnanswering(host string) bool {
|
|
for _, key := range [2]any{host, machineOf(host)} {
|
|
v, found := unreachable.Load(key)
|
|
if !found {
|
|
continue
|
|
}
|
|
failure := v.(unanswered)
|
|
if time.Since(failure.at) >= unreachableRetryInterval &&
|
|
unreachable.CompareAndSwap(key, failure, unanswered{at: time.Now(), addr: failure.addr}) {
|
|
probes.Go(func() { probeReachability(failure.addr) })
|
|
}
|
|
return true
|
|
}
|
|
return false
|
|
}
|
|
|
|
// probeReachability asks for response headers, not just a connection, so a
|
|
// server that accepts but never answers stays last.
|
|
func probeReachability(addr string) {
|
|
ctx, cancel := context.WithTimeout(context.Background(), reachabilityProbeTimeout)
|
|
defer cancel()
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodHead, "http://"+addr+"/status", nil)
|
|
if err == nil {
|
|
var resp *http.Response
|
|
if resp, err = GetGlobalHttpClient().Do(req); err == nil {
|
|
resp.Body.Close()
|
|
recordReachable(addr)
|
|
return
|
|
}
|
|
}
|
|
recordUnreachable(addr, err)
|
|
}
|
|
|
|
func hostOf(rawUrl string) string {
|
|
parsed, err := url.Parse(rawUrl)
|
|
if err != nil {
|
|
return ""
|
|
}
|
|
return parsed.Host
|
|
}
|
|
|
|
func machineOf(host string) machineKey {
|
|
if machine, _, err := net.SplitHostPort(host); err == nil {
|
|
return machineKey(machine)
|
|
}
|
|
if strings.HasPrefix(host, "[") && strings.HasSuffix(host, "]") {
|
|
host = host[1 : len(host)-1]
|
|
}
|
|
return machineKey(host)
|
|
}
|