Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

meta: add IPAddrs to the session info #3464

Merged
merged 4 commits into from
Apr 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion pkg/meta/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
})
Expand Down
4 changes: 2 additions & 2 deletions pkg/meta/base_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions pkg/meta/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ type TreeSummary struct {
type SessionInfo struct {
Version string
HostName string
IPAddrs []string `json:",omitempty"`
MountPoint string
ProcessID int
}
Expand Down
53 changes: 13 additions & 40 deletions pkg/sync/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"bufio"
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"net"
Expand All @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
33 changes: 33 additions & 0 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down