From 3cf54c25d0653b1fdba7b4d459bcc468140becc9 Mon Sep 17 00:00:00 2001 From: Sandy Xu Date: Wed, 12 Apr 2023 16:55:14 +0800 Subject: [PATCH] meta: add IPAddrs to the session info (#3464) --- pkg/meta/base.go | 14 +++++++++++- pkg/meta/base_test.go | 4 ++-- pkg/meta/interface.go | 1 + pkg/sync/cluster.go | 53 +++++++++++-------------------------------- pkg/utils/utils.go | 33 +++++++++++++++++++++++++++ 5 files changed, 62 insertions(+), 43 deletions(-) diff --git a/pkg/meta/base.go b/pkg/meta/base.go index c17efdd11701..69b50fbd6cb9 100644 --- a/pkg/meta/base.go +++ b/pkg/meta/base.go @@ -427,11 +427,23 @@ func (m *baseMeta) newSessionInfo() []byte { host, err := os.Hostname() if err != nil { logger.Warnf("Failed to get hostname: %s", err) - host = "" + } + ips, err := utils.FindLocalIPs() + if err != nil { + logger.Warnf("Failed to get local IP: %s", err) + } + addrs := make([]string, 0, len(ips)) + for _, i := range ips { + if ip := i.String(); ip[0] == '?' { + logger.Warnf("Invalid IP address: %s", ip) + } else { + addrs = append(addrs, ip) + } } buf, err := json.Marshal(&SessionInfo{ Version: version.Version(), HostName: host, + IPAddrs: addrs, MountPoint: m.conf.MountPoint, ProcessID: os.Getpid(), }) diff --git a/pkg/meta/base_test.go b/pkg/meta/base_test.go index 26e7eb7f2577..8e0207ae7b8a 100644 --- a/pkg/meta/base_test.go +++ b/pkg/meta/base_test.go @@ -1362,8 +1362,8 @@ func testCloseSession(t *testing.T, m Meta) { if err != nil { t.Fatalf("get session: %s", err) } - var empty SessionInfo - if s.SessionInfo != empty { + if s.SessionInfo.Version != "" || s.SessionInfo.HostName != "" || s.SessionInfo.IPAddrs != nil || + s.SessionInfo.MountPoint != "" || s.SessionInfo.ProcessID != 0 { t.Fatalf("incorrect session info %+v", s.SessionInfo) } if len(s.Flocks) != 0 || len(s.Plocks) != 0 || len(s.Sustained) != 0 { diff --git a/pkg/meta/interface.go b/pkg/meta/interface.go index 894ef1ffe247..6e96ee1b7970 100644 --- a/pkg/meta/interface.go +++ b/pkg/meta/interface.go @@ -246,6 +246,7 @@ type TreeSummary struct { type SessionInfo struct { Version string HostName string + IPAddrs []string `json:",omitempty"` MountPoint string ProcessID int } diff --git a/pkg/sync/cluster.go b/pkg/sync/cluster.go index 096a83a78c3c..4ac2a3748717 100644 --- a/pkg/sync/cluster.go +++ b/pkg/sync/cluster.go @@ -20,7 +20,6 @@ import ( "bufio" "bytes" "encoding/json" - "errors" "fmt" "io" "net" @@ -35,6 +34,7 @@ import ( "github.com/oliverisaac/shellescape" "github.com/juicedata/juicefs/pkg/object" + "github.com/juicedata/juicefs/pkg/utils" ) // Stat has the counters to represent the progress. @@ -97,43 +97,6 @@ func sendStats(addr string) { } } -func findLocalIP() (string, error) { - ifaces, err := net.Interfaces() - if err != nil { - return "", err - } - for _, iface := range ifaces { - if iface.Flags&net.FlagUp == 0 { - continue // interface down - } - if iface.Flags&net.FlagLoopback != 0 { - continue // loopback interface - } - addrs, err := iface.Addrs() - if err != nil { - return "", err - } - for _, addr := range addrs { - var ip net.IP - switch v := addr.(type) { - case *net.IPNet: - ip = v.IP - case *net.IPAddr: - ip = v.IP - } - if ip == nil || ip.IsLoopback() { - continue - } - ip = ip.To4() - if ip == nil { - continue // not an ipv4 address - } - return ip.String(), nil - } - } - return "", errors.New("are you connected to the network?") -} - func startManager(tasks <-chan object.Object) (string, error) { http.HandleFunc("/fetch", func(w http.ResponseWriter, req *http.Request) { var objs []object.Object @@ -186,9 +149,19 @@ func startManager(tasks <-chan object.Object) (string, error) { logger.Debugf("receive stats %+v from %s", r, req.RemoteAddr) _, _ = w.Write([]byte("OK")) }) - ip, err := findLocalIP() + ips, err := utils.FindLocalIPs() if err != nil { - return "", fmt.Errorf("find local ip: %s", err) + return "", fmt.Errorf("find local ips: %s", err) + } + var ip string + for _, i := range ips { + if i = i.To4(); i != nil { + ip = i.String() + break + } + } + if ip == "" { + return "", fmt.Errorf("no local ip found") } l, err := net.Listen("tcp", ip+":") if err != nil { diff --git a/pkg/utils/utils.go b/pkg/utils/utils.go index aa2eb3c0f79d..f7db8957ba77 100644 --- a/pkg/utils/utils.go +++ b/pkg/utils/utils.go @@ -68,6 +68,39 @@ func GetLocalIp(address string) (string, error) { return ip, nil } +func FindLocalIPs() ([]net.IP, error) { + ifaces, err := net.Interfaces() + if err != nil { + return nil, err + } + var ips []net.IP + for _, iface := range ifaces { + if iface.Flags&net.FlagUp == 0 { + continue // interface down + } + if iface.Flags&net.FlagLoopback != 0 { + continue // loopback interface + } + addrs, err := iface.Addrs() + if err != nil { + continue + } + for _, addr := range addrs { + var ip net.IP + switch v := addr.(type) { + case *net.IPNet: + ip = v.IP + case *net.IPAddr: + ip = v.IP + } + if len(ip) > 0 && !ip.IsLoopback() { + ips = append(ips, ip) + } + } + } + return ips, nil +} + func WithTimeout(f func() error, timeout time.Duration) error { var done = make(chan int, 1) var t = time.NewTimer(timeout)