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

Commit

Permalink
update
Browse files Browse the repository at this point in the history
Signed-off-by: lowzj <[email protected]>
  • Loading branch information
lowzj committed Apr 26, 2020
1 parent 230dc89 commit a242c4e
Show file tree
Hide file tree
Showing 5 changed files with 196 additions and 90 deletions.
3 changes: 2 additions & 1 deletion dfget/config/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,8 @@ const (
ServerAliveTime = 5 * time.Minute
DefaultDownloadTimeout = 5 * time.Minute

DefaultSupernodePort = 8002
DefaultSupernodeSchema = "http"
DefaultSupernodePort = 8002
)

/* errors code */
Expand Down
83 changes: 0 additions & 83 deletions dfget/core/locator/static_locator.go

This file was deleted.

13 changes: 7 additions & 6 deletions dfget/core/locator/locator.go → dfget/locator/locator.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ type SupernodeLocator interface {

// Report records the metrics of the current supernode in order to choose a
// more appropriate supernode for the next time if necessary.
Report(ip string, metrics *SupernodeMetrics)
Report(node string, metrics *SupernodeMetrics)

// Refresh refreshes all the supernodes.
Refresh() bool
Expand All @@ -52,11 +52,12 @@ type SupernodeGroup struct {

// Supernode basic information of supernodes.
type Supernode struct {
Schema string
IP string
Port int
Weight int
Metrics *SupernodeMetrics
Schema string
IP string
Port int
Weight int
GroupName string
Metrics *SupernodeMetrics
}

// SupernodeMetrics the metrics used for locator to choose supernode.
Expand Down
123 changes: 123 additions & 0 deletions dfget/locator/static_locator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/*
* Copyright The Dragonfly Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package locator

import (
"sync/atomic"

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

type StaticLocator struct {
idx int32
Group *SupernodeGroup
}

// NewStaticLocator constructs StaticLocator which uses the nodes passed from
// configuration or CLI.
func NewStaticLocator(nodes []*config.NodeWeight) *StaticLocator {
locator := &StaticLocator{}
if nodes == nil || len(nodes) == 0 {
return locator
}
groupName := "config"
group := &SupernodeGroup{
Name: groupName,
Nodes: make([]*Supernode, len(nodes)),
}
for _, node := range nodes {
ip, port := netutils.GetIPAndPortFromNode(node.Node, config.DefaultSupernodePort)
if ip == "" {
continue
}
supernode := &Supernode{
Schema: config.DefaultSupernodeSchema,
IP: ip,
Port: port,
Weight: node.Weight,
GroupName: groupName,
}
group.Nodes = append(group.Nodes, supernode)
}
locator.Group = group
return locator
}

// NewStaticLocatorFromStr constructs StaticLocator from string list.
// The format of nodes is: ip:port=weight
func NewStaticLocatorFromStr(nodes []string) (*StaticLocator, error) {
nodeWeight, err := config.ParseNodesSlice(nodes)
if err != nil {
return nil, err
}
return NewStaticLocator(nodeWeight), nil
}

func (s *StaticLocator) Get() *Supernode {
if s.Group == nil {
return nil
}
idx := s.load()
if idx >= len(s.Group.Nodes) {
return nil
}
return s.Group.Nodes[idx]
}

func (s *StaticLocator) Next() *Supernode {
if s.Group == nil || s.load() >= len(s.Group.Nodes) {
return nil
}
idx := s.inc()
if idx >= len(s.Group.Nodes) {
return nil
}
return s.Group.Nodes[idx]
}

func (s *StaticLocator) GetGroup(name string) *SupernodeGroup {
if s.Group == nil || s.Group.Name != name {
return nil
}
return s.Group
}

func (s *StaticLocator) All() []*SupernodeGroup {
if s.Group == nil {
return nil
}
return []*SupernodeGroup{s.Group}
}

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

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

func (s *StaticLocator) load() int {
return int(atomic.LoadInt32(&s.idx))
}

func (s *StaticLocator) inc() int {
return int(atomic.AddInt32(&s.idx, 1))
}
64 changes: 64 additions & 0 deletions dfget/locator/static_locator_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright The Dragonfly Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package locator

import (
"testing"

"gopkg.in/check.v1"
)

func Test(t *testing.T) {
check.TestingT(t)
}

type StaticLocatorTestSuite struct {
workHome string
}

func init() {
check.Suite(&StaticLocatorTestSuite{})
}

func (s *StaticLocatorTestSuite) Test_NewStaticLocator(c *check.C) {

}

func (s *StaticLocatorTestSuite) Test_NewStaticLocatorFromString(c *check.C) {

}

func (s *StaticLocatorTestSuite) Test_Get(c *check.C) {
locator, _ := NewStaticLocatorFromStr([]string{"a:80=1", "b:81=1"})
sn := locator.Get()
}

func (s *StaticLocatorTestSuite) Test_Next(c *check.C) {

}

func (s *StaticLocatorTestSuite) Test_GetGroup(c *check.C) {

}

func (s *StaticLocatorTestSuite) Test_All(c *check.C) {

}

func (s *StaticLocatorTestSuite) Test_Refresh(c *check.C) {

}

0 comments on commit a242c4e

Please sign in to comment.