|
| 1 | +/* |
| 2 | + * Copyright The Dragonfly Authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package locator |
| 18 | + |
| 19 | +import ( |
| 20 | + "math/rand" |
| 21 | + "sync/atomic" |
| 22 | + "time" |
| 23 | + |
| 24 | + "github.com/dragonflyoss/Dragonfly/dfget/config" |
| 25 | + "github.com/dragonflyoss/Dragonfly/pkg/algorithm" |
| 26 | + "github.com/dragonflyoss/Dragonfly/pkg/netutils" |
| 27 | +) |
| 28 | + |
| 29 | +func init() { |
| 30 | + rand.Seed(time.Now().UnixNano()) |
| 31 | +} |
| 32 | + |
| 33 | +const staticLocatorGroupName = "config" |
| 34 | + |
| 35 | +// StaticLocator uses the nodes passed from configuration or CLI. |
| 36 | +type StaticLocator struct { |
| 37 | + idx int32 |
| 38 | + Group *SupernodeGroup |
| 39 | +} |
| 40 | + |
| 41 | +// ---------------------------------------------------------------------------- |
| 42 | +// constructors |
| 43 | + |
| 44 | +// NewStaticLocator constructs StaticLocator which uses the nodes passed from |
| 45 | +// configuration or CLI. |
| 46 | +func NewStaticLocator(nodes []*config.NodeWeight) *StaticLocator { |
| 47 | + locator := &StaticLocator{} |
| 48 | + if len(nodes) == 0 { |
| 49 | + return locator |
| 50 | + } |
| 51 | + group := &SupernodeGroup{ |
| 52 | + Name: staticLocatorGroupName, |
| 53 | + } |
| 54 | + for _, node := range nodes { |
| 55 | + ip, port := netutils.GetIPAndPortFromNode(node.Node, config.DefaultSupernodePort) |
| 56 | + if ip == "" { |
| 57 | + continue |
| 58 | + } |
| 59 | + supernode := &Supernode{ |
| 60 | + Schema: config.DefaultSupernodeSchema, |
| 61 | + IP: ip, |
| 62 | + Port: port, |
| 63 | + Weight: node.Weight, |
| 64 | + GroupName: staticLocatorGroupName, |
| 65 | + } |
| 66 | + for i := 0; i < supernode.Weight; i++ { |
| 67 | + group.Nodes = append(group.Nodes, supernode) |
| 68 | + } |
| 69 | + } |
| 70 | + shuffleNodes(group.Nodes) |
| 71 | + locator.Group = group |
| 72 | + return locator |
| 73 | +} |
| 74 | + |
| 75 | +// NewStaticLocatorFromStr constructs StaticLocator from string list. |
| 76 | +// The format of nodes is: ip:port=weight |
| 77 | +func NewStaticLocatorFromStr(nodes []string) (*StaticLocator, error) { |
| 78 | + nodeWeight, err := config.ParseNodesSlice(nodes) |
| 79 | + if err != nil { |
| 80 | + return nil, err |
| 81 | + } |
| 82 | + return NewStaticLocator(nodeWeight), nil |
| 83 | +} |
| 84 | + |
| 85 | +// ---------------------------------------------------------------------------- |
| 86 | +// implement api methods |
| 87 | + |
| 88 | +func (s *StaticLocator) Get() *Supernode { |
| 89 | + if s.Group == nil { |
| 90 | + return nil |
| 91 | + } |
| 92 | + return s.Group.GetNode(s.load()) |
| 93 | +} |
| 94 | + |
| 95 | +func (s *StaticLocator) Next() *Supernode { |
| 96 | + if s.Group == nil || s.load() >= len(s.Group.Nodes) { |
| 97 | + return nil |
| 98 | + } |
| 99 | + return s.Group.GetNode(s.inc()) |
| 100 | +} |
| 101 | + |
| 102 | +func (s *StaticLocator) GetGroup(name string) *SupernodeGroup { |
| 103 | + if s.Group == nil || s.Group.Name != name { |
| 104 | + return nil |
| 105 | + } |
| 106 | + return s.Group |
| 107 | +} |
| 108 | + |
| 109 | +func (s *StaticLocator) All() []*SupernodeGroup { |
| 110 | + if s.Group == nil { |
| 111 | + return nil |
| 112 | + } |
| 113 | + return []*SupernodeGroup{s.Group} |
| 114 | +} |
| 115 | + |
| 116 | +func (s *StaticLocator) Report(node string, metrics *SupernodeMetrics) { |
| 117 | + // unnecessary to implement this method |
| 118 | + return |
| 119 | +} |
| 120 | + |
| 121 | +func (s *StaticLocator) Refresh() bool { |
| 122 | + atomic.StoreInt32(&s.idx, 0) |
| 123 | + return true |
| 124 | +} |
| 125 | + |
| 126 | +// ---------------------------------------------------------------------------- |
| 127 | +// private methods of StaticLocator |
| 128 | + |
| 129 | +func (s *StaticLocator) load() int { |
| 130 | + return int(atomic.LoadInt32(&s.idx)) |
| 131 | +} |
| 132 | + |
| 133 | +func (s *StaticLocator) inc() int { |
| 134 | + return int(atomic.AddInt32(&s.idx, 1)) |
| 135 | +} |
| 136 | + |
| 137 | +// ---------------------------------------------------------------------------- |
| 138 | +// helper functions |
| 139 | + |
| 140 | +func shuffleNodes(nodes []*Supernode) []*Supernode { |
| 141 | + if length := len(nodes); length > 1 { |
| 142 | + algorithm.Shuffle(length, func(i, j int) { |
| 143 | + nodes[i], nodes[j] = nodes[j], nodes[i] |
| 144 | + }) |
| 145 | + } |
| 146 | + return nodes |
| 147 | +} |
0 commit comments