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 2 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
6 changes: 5 additions & 1 deletion pkg/meta/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -427,11 +427,15 @@ 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)
}
buf, err := json.Marshal(&SessionInfo{
Version: version.Version(),
HostName: host,
IPAddrs: ips,
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
44 changes: 4 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,10 +149,11 @@ 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)
}
ip := ips[0]
l, err := net.Listen("tcp", ip+":")
if err != nil {
return "", fmt.Errorf("listen: %s", err)
Expand Down
42 changes: 42 additions & 0 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package utils

import (
"errors"
"fmt"
"mime"
"net"
Expand Down Expand Up @@ -68,6 +69,47 @@ func GetLocalIp(address string) (string, error) {
return ip, nil
}

func FindLocalIPs() ([]string, error) {
ifaces, err := net.Interfaces()
if err != nil {
return nil, err
}
var ips []string
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 nil, err
SandyXSD marked this conversation as resolved.
Show resolved Hide resolved
}
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()
SandyXSD marked this conversation as resolved.
Show resolved Hide resolved
if ip == nil {
continue // not an ipv4 address
}
ips = append(ips, ip.String())
}
}
if len(ips) == 0 {
err = errors.New("are you connected to the network?")
SandyXSD marked this conversation as resolved.
Show resolved Hide resolved
}
return ips, err
}

func WithTimeout(f func() error, timeout time.Duration) error {
var done = make(chan int, 1)
var t = time.NewTimer(timeout)
Expand Down