Skip to content
This repository has been archived by the owner on Feb 27, 2023. It is now read-only.

Commit

Permalink
Merge pull request #1324 from lowzj/locator-interface
Browse files Browse the repository at this point in the history
refactor: redefine the behavior of SupernodeLocator
  • Loading branch information
starnop authored May 6, 2020
2 parents 359175e + 854074c commit 0d3e9b2
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 28 deletions.
2 changes: 1 addition & 1 deletion dfget/config/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ const (
const (
DefaultYamlConfigFile = "/etc/dragonfly/dfget.yml"
DefaultIniConfigFile = "/etc/dragonfly.conf"
DefaultNode = "127.0.0.1"
DefaultLocalLimit = 20 * rate.MB
DefaultMinRate = 64 * rate.KB
DefaultClientQueueSize = 6
Expand Down Expand Up @@ -121,6 +120,7 @@ const (
DefaultDownloadTimeout = 5 * time.Minute

DefaultSupernodeSchema = "http"
DefaultSupernodeIP = "127.0.0.1"
DefaultSupernodePort = 8002
)

Expand Down
2 changes: 1 addition & 1 deletion dfget/config/supernode_value.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func NewSupernodesValue(p *[]*NodeWeight, val []*NodeWeight) *SupernodesValue {
func GetDefaultSupernodesValue() []*NodeWeight {
var result = make([]*NodeWeight, 0)
result = append(result, &NodeWeight{
Node: fmt.Sprintf("%s:%d", DefaultNode, DefaultSupernodePort),
Node: fmt.Sprintf("%s:%d", DefaultSupernodeIP, DefaultSupernodePort),
Weight: DefaultSupernodeWeight,
})
return result
Expand Down
4 changes: 0 additions & 4 deletions dfget/core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,6 @@ import (
"github.com/sirupsen/logrus"
)

func init() {
rand.Seed(time.Now().UnixNano())
}

// Start function creates a new task and starts it to download file.
func Start(cfg *config.Config) *errortypes.DfError {
var (
Expand Down
4 changes: 4 additions & 0 deletions dfget/locator/locator.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package locator
// supernode list from configuration or CLI.
type SupernodeLocator interface {
// Get returns the current selected supernode, it should be idempotent.
// It should return nil before first calling the Next method.
Get() *Supernode

// Next chooses the next available supernode for retrying or other
Expand All @@ -33,6 +34,9 @@ type SupernodeLocator interface {
// All returns all the supernodes.
All() []*SupernodeGroup

// Size returns the number of all supernodes.
Size() int

// Report records the metrics of the current supernode in order to choose a
// more appropriate supernode for the next time if necessary.
Report(node string, metrics *SupernodeMetrics)
Expand Down
21 changes: 13 additions & 8 deletions dfget/locator/static_locator.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,17 @@
package locator

import (
"math/rand"
"sync/atomic"
"time"

"github.com/dragonflyoss/Dragonfly/dfget/config"
"github.com/dragonflyoss/Dragonfly/pkg/algorithm"
"github.com/dragonflyoss/Dragonfly/pkg/netutils"
)

func init() {
rand.Seed(time.Now().UnixNano())
}

const staticLocatorGroupName = "config"

var _ SupernodeLocator = &StaticLocator{}

// StaticLocator uses the nodes passed from configuration or CLI.
type StaticLocator struct {
idx int32
Expand All @@ -44,7 +40,9 @@ type StaticLocator struct {
// NewStaticLocator constructs StaticLocator which uses the nodes passed from
// configuration or CLI.
func NewStaticLocator(nodes []*config.NodeWeight) *StaticLocator {
locator := &StaticLocator{}
locator := &StaticLocator{
idx: -1,
}
if len(nodes) == 0 {
return locator
}
Expand Down Expand Up @@ -113,13 +111,20 @@ func (s *StaticLocator) All() []*SupernodeGroup {
return []*SupernodeGroup{s.Group}
}

func (s *StaticLocator) Size() int {
if s.Group == nil {
return 0
}
return len(s.Group.Nodes)
}

func (s *StaticLocator) Report(node string, metrics *SupernodeMetrics) {
// unnecessary to implement this method
return
}

func (s *StaticLocator) Refresh() bool {
atomic.StoreInt32(&s.idx, 0)
atomic.StoreInt32(&s.idx, -1)
return true
}

Expand Down
31 changes: 17 additions & 14 deletions dfget/locator/static_locator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"github.com/go-check/check"

"github.com/dragonflyoss/Dragonfly/dfget/config"
"github.com/dragonflyoss/Dragonfly/pkg/algorithm"
)

func Test(t *testing.T) {
Expand All @@ -42,12 +41,12 @@ func (s *StaticLocatorTestSuite) Test_NewStaticLocator(c *check.C) {
rand.Seed(0)
l := NewStaticLocator(nil)
c.Assert(l, check.NotNil)
c.Assert(l.idx, check.Equals, int32(0))
c.Assert(l.idx, check.Equals, int32(-1))
c.Assert(l.Group, check.IsNil)

l = NewStaticLocator([]*config.NodeWeight{})
c.Assert(l, check.NotNil)
c.Assert(l.idx, check.Equals, int32(0))
c.Assert(l.idx, check.Equals, int32(-1))
c.Assert(l.Group, check.IsNil)

l = NewStaticLocator([]*config.NodeWeight{
Expand Down Expand Up @@ -86,6 +85,7 @@ func (s *StaticLocatorTestSuite) Test_NewStaticLocatorFromString(c *check.C) {
c.Assert(err, check.IsNil)
c.Assert(l, check.NotNil)
c.Assert(len(l.Group.Nodes), check.Equals, v.expectedLen)
c.Assert(l.Size(), check.Equals, v.expectedLen)
}
}
}
Expand All @@ -100,6 +100,9 @@ func (s *StaticLocatorTestSuite) Test_Get(c *check.C) {
for _, v := range cases {
l, _ := NewStaticLocatorFromStr(strings.Split(v.nodes, ","))
sn := l.Get()
c.Assert(sn, check.IsNil)
l.Next()
sn = l.Get()
if v.expected == nil {
c.Assert(sn, check.IsNil)
} else {
Expand All @@ -110,23 +113,23 @@ func (s *StaticLocatorTestSuite) Test_Get(c *check.C) {
}

func (s *StaticLocatorTestSuite) Test_Next(c *check.C) {
rand.Seed(0)
idx := []int{0, 1, 2}
algorithm.Shuffle(len(idx), func(i int, j int) {
idx[i], idx[j] = idx[j], idx[i]
})
cases := []struct {
nodes string
cnt int
expectedIdx int
}{
{"a:80=1", 0, 0},
{"a:80=1", 1, -1},
{"a:80=1,a:81=2", 2, idx[2]},
{"a:80=1", 0, -1},
{"a:80=1", 1, 0},
{"a:80=1,a:81=2", 2, 1},
// the weight of a:81 is 2, it will be chosen twice
{"a:80=1,a:81=2", 3, 2},
// return nil because 4 is greater than the length
{"a:80=1,a:81=2", 4, -1},
}

var sn *Supernode
for _, v := range cases {
l, _ := NewStaticLocatorFromStr(strings.Split(v.nodes, ","))
sn := l.Get()
for i := 0; i < v.cnt; i++ {
sn = l.Next()
}
Expand Down Expand Up @@ -159,10 +162,10 @@ func (s *StaticLocatorTestSuite) Test_All(c *check.C) {
func (s *StaticLocatorTestSuite) Test_Refresh(c *check.C) {
l, _ := NewStaticLocatorFromStr([]string{"a:80=1"})
_ = l.Next()
c.Assert(l.load(), check.Equals, 1)
c.Assert(l.load(), check.Equals, 0)

l.Refresh()
c.Assert(l.load(), check.Equals, 0)
c.Assert(l.load(), check.Equals, -1)
}

func create(ip string, port, weight int) *Supernode {
Expand Down

0 comments on commit 0d3e9b2

Please sign in to comment.