This repository has been archived by the owner on Feb 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 773
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: define the interface SupernodeLocator
Signed-off-by: lowzj <[email protected]>
- Loading branch information
Showing
4 changed files
with
399 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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{} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.