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

Commit

Permalink
feature: define the interface SupernodeLocator
Browse files Browse the repository at this point in the history
Signed-off-by: lowzj <[email protected]>
  • Loading branch information
lowzj committed Apr 30, 2020
1 parent 6cc9d86 commit 40d3766
Show file tree
Hide file tree
Showing 4 changed files with 399 additions and 1 deletion.
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
66 changes: 66 additions & 0 deletions dfget/locator/locator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* 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

// SupernodeLocator defines the way how to get available supernodes.
// Developers can implement their own locator more flexibly , not just get the
// supernode list from configuration or CLI.
type SupernodeLocator interface {
// Get returns the current selected supernode, it should be idempotent.
Get() *Supernode

// Next chooses the next available supernode for retrying or other
// purpose. The current supernode should be set as this result.
Next() *Supernode

// GetGroup returns the group.
GetGroup(name string) *SupernodeGroup

// All returns all the supernodes.
All() []*SupernodeGroup

// 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)

// Refresh refreshes all the supernodes.
Refresh() bool
}

// SupernodeGroup groups supernodes which has same attributes.
type SupernodeGroup struct {
Name string
Nodes []*Supernode

// Infos stores other information that user can customized.
Infos map[string]string
}

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

// SupernodeMetrics holds metrics used for the locator to choose supernode.
type SupernodeMetrics struct {
Metrics map[string]interface{}
}
155 changes: 155 additions & 0 deletions dfget/locator/static_locator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
/*
* 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 (
"math/rand"
"sync/atomic"
"time"

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

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

const staticLocatorGroupName = "config"

// StaticLocator uses the nodes passed from configuration or CLI.
type StaticLocator struct {
idx int32
Group *SupernodeGroup
}

// ----------------------------------------------------------------------------
// constructors

// 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
}
group := &SupernodeGroup{
Name: staticLocatorGroupName,
}
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: staticLocatorGroupName,
}
for i := 0; i < supernode.Weight; i++ {
group.Nodes = append(group.Nodes, supernode)
}
}
shuffleNodes(group.Nodes)
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
}

// ----------------------------------------------------------------------------
// implement api methods

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
}

// ----------------------------------------------------------------------------
// private methods of StaticLocator

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

func (s *StaticLocator) inc() int {
return int(atomic.AddInt32(&s.idx, 1))
}

// ----------------------------------------------------------------------------
// helper functions

func shuffleNodes(nodes []*Supernode) []*Supernode {
if length := len(nodes); length > 1 {
util.Shuffle(length, func(i, j int) {
nodes[i], nodes[j] = nodes[j], nodes[i]
})
}
return nodes
}
Loading

0 comments on commit 40d3766

Please sign in to comment.