Skip to content
This repository has been archived by the owner on Dec 20, 2024. It is now read-only.

Commit

Permalink
Merge pull request #459 from Starnop/supernode-IP
Browse files Browse the repository at this point in the history
feature: add AdvertiseIP flag for supernode
  • Loading branch information
lowzj authored May 13, 2019
2 parents 65c6cbd + 1252f32 commit 0149ba4
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
30 changes: 30 additions & 0 deletions cmd/supernode/app/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,13 @@ import (
"path"
"reflect"

"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"

"github.com/dragonflyoss/Dragonfly/common/dflog"
errorType "github.com/dragonflyoss/Dragonfly/common/errors"
cutil "github.com/dragonflyoss/Dragonfly/common/util"
"github.com/dragonflyoss/Dragonfly/supernode/config"
"github.com/dragonflyoss/Dragonfly/supernode/daemon"
)
Expand Down Expand Up @@ -93,6 +96,9 @@ func setupFlags(cmd *cobra.Command, opt *Options) {

flagSet.IntVar(&opt.PeerDownLimit, "down-limit", opt.PeerDownLimit,
"download limit for supernode to serve download tasks")

flagSet.StringVar(&cfg.AdvertiseIP, "advertise-ip", "",
"the supernode ip that we advertise to other peer in the p2p-network")
}

// runSuperNode prepares configs, setups essential details and runs supernode daemon.
Expand All @@ -106,6 +112,16 @@ func runSuperNode() error {
return err
}

// set supernode advertise ip
if cutil.IsEmptyStr(cfg.AdvertiseIP) {
if err := setAdvertiseIP(); err != nil {
return err
}
}

// set up the CIDPrefix
cfg.SetCIDPrefix(cfg.AdvertiseIP)

logrus.Info("start to run supernode")

d, err := daemon.New(cfg)
Expand Down Expand Up @@ -150,6 +166,20 @@ func initConfig() error {
return nil
}

func setAdvertiseIP() error {
// use the first non-loop address if the AdvertiseIP is empty
ipList, err := cutil.GetAllIPs()
if err != nil {
return errors.Wrapf(errorType.ErrSystemError, "failed to get ip list: %v", err)
}
if len(ipList) != 0 {
cfg.AdvertiseIP = ipList[0]
}

logrus.Infof("success to init local ip of supernode, use ip: %s", cfg.AdvertiseIP)
return nil
}

func choosePropValue(cliProp, cfgProp *config.BaseProperties) {
if cliProp == nil || cfgProp == nil {
return
Expand Down
19 changes: 19 additions & 0 deletions common/util/net_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,25 @@ func IsValidIP(ip string) bool {
return result
}

// GetAllIPs returns all non-loopback addresses.
func GetAllIPs() (ipList []string, err error) {
// get all system's unicast interface addresses.
addrs, err := net.InterfaceAddrs()
if err != nil {
return nil, err
}

// filter all loopback addresses.
for _, v := range addrs {
if ipNet, ok := v.(*net.IPNet); ok {
if !ipNet.IP.IsLoopback() {
ipList = append(ipList, ipNet.IP.String())
}
}
}
return
}

// slice2Map translate a slice to a map with
// the value in slice as the key and true as the value.
func slice2Map(value []string) map[string]bool {
Expand Down
19 changes: 19 additions & 0 deletions supernode/config/config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package config

import (
"fmt"
"path/filepath"

"gopkg.in/yaml.v2"
Expand Down Expand Up @@ -34,6 +35,17 @@ func (c *Config) String() string {
return ""
}

// SetCIDPrefix sets a string as the prefix for supernode CID
// which used to distinguish from the other peer nodes.
func (c *Config) SetCIDPrefix(ip string) {
c.cIDPrefix = fmt.Sprintf("cdnnode:%s~", ip)
}

// GetSuperCID returns the cid string for taskID.
func (c *Config) GetSuperCID(taskID string) string {
return fmt.Sprintf("%s%s", c.cIDPrefix, taskID)
}

// NewBaseProperties create a instant with default values.
func NewBaseProperties() *BaseProperties {
home := filepath.Join(string(filepath.Separator), "home", "admin", "supernode")
Expand Down Expand Up @@ -124,4 +136,11 @@ type BaseProperties struct {
// Whether to open DEBUG level
// default: false
Debug bool `yaml:"debug"`

// AdvertiseIP is used to set the ip that we advertise to other peer in the p2p-network.
// By default, the first non-loop address is advertised.
AdvertiseIP string `yaml:"advertiseIP"`

// cIDPrefix s a prefix string used to indicate that the CID is supernode.
cIDPrefix string
}

0 comments on commit 0149ba4

Please sign in to comment.