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 774
/
supernode_value.go
227 lines (190 loc) · 5.57 KB
/
supernode_value.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/*
* 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 config
import (
"encoding/json"
"fmt"
"strconv"
"strings"
"github.com/dragonflyoss/Dragonfly/pkg/algorithm"
"github.com/dragonflyoss/Dragonfly/pkg/errortypes"
"github.com/pkg/errors"
)
const weightSeparator = '='
type SupernodesValue struct {
Nodes *[]*NodeWeight
}
type NodeWeight struct {
Node string
Weight int
}
func NewSupernodesValue(p *[]*NodeWeight, val []*NodeWeight) *SupernodesValue {
ssv := new(SupernodesValue)
ssv.Nodes = p
*ssv.Nodes = val
return ssv
}
// GetDefaultSupernodesValue returns the default value of supernodes.
// default: ["127.0.0.1:8002=1"]
func GetDefaultSupernodesValue() []*NodeWeight {
var result = make([]*NodeWeight, 0)
result = append(result, &NodeWeight{
Node: fmt.Sprintf("%s:%d", DefaultSupernodeIP, DefaultSupernodePort),
Weight: DefaultSupernodeWeight,
})
return result
}
// String implements the pflag.Value interface.
func (sv *SupernodesValue) String() string {
var result []string
for _, v := range *sv.Nodes {
result = append(result, v.string())
}
return strings.Join(result, ",")
}
// Set implements the pflag.Value interface.
func (sv *SupernodesValue) Set(value string) error {
nodes, err := ParseNodesString(value)
if err != nil {
return err
}
*sv.Nodes = nodes
return nil
}
// Type implements the pflag.Value interface.
func (sv *SupernodesValue) Type() string {
return "supernodes"
}
// MarshalYAML implements the yaml.Marshaler interface.
func (nw *NodeWeight) MarshalYAML() (interface{}, error) {
return nw.string(), nil
}
// UnmarshalYAML implements the yaml.Unmarshaler interface.
func (nw *NodeWeight) UnmarshalYAML(unmarshal func(interface{}) error) error {
var value string
if err := unmarshal(&value); err != nil {
return err
}
nodeWeight, err := string2NodeWeight(value)
if err != nil {
return err
}
*nw = *nodeWeight
return nil
}
// MarshalJSON implements the json.Marshaler interface.
func (nw *NodeWeight) MarshalJSON() ([]byte, error) {
return json.Marshal(nw.string())
}
// UnmarshalJSON implements the json.Unmarshaler interface.
func (nw *NodeWeight) UnmarshalJSON(b []byte) error {
str, _ := strconv.Unquote(string(b))
nodeWeight, err := string2NodeWeight(str)
if err != nil {
return err
}
*nw = *nodeWeight
return nil
}
func (nw *NodeWeight) string() string {
return fmt.Sprintf("%s%c%d", nw.Node, weightSeparator, nw.Weight)
}
// ParseNodesString parses the value in string type to []*NodeWeight.
func ParseNodesString(value string) ([]*NodeWeight, error) {
return ParseNodesSlice(strings.Split(value, ","))
}
// ParseNodesSlice parses the value in string slice type to []*NodeWeight.
func ParseNodesSlice(value []string) ([]*NodeWeight, error) {
nodeWeightSlice := make([]*NodeWeight, 0)
weightKey := make([]int, 0)
// split node and weight
for _, v := range value {
nodeWeight, err := string2NodeWeight(v)
if err != nil {
return nil, errors.Wrapf(errortypes.ErrInvalidValue, "node: %s %v", v, err)
}
weightKey = append(weightKey, nodeWeight.Weight)
nodeWeightSlice = append(nodeWeightSlice, nodeWeight)
}
var result []*NodeWeight
// get the greatest common divisor of the weight slice and
// divide all weights by the greatest common divisor.
gcdNumber := algorithm.GCDSlice(weightKey)
for _, v := range nodeWeightSlice {
result = append(result, &NodeWeight{
Node: v.Node,
Weight: v.Weight / gcdNumber,
})
}
return result, nil
}
// NodeWeightSlice2StringSlice parses nodeWeight slice to string slice.
// It takes the NodeWeight.Node as the value and every value will be appended the corresponding NodeWeight.Weight times.
func NodeWeightSlice2StringSlice(supernodes []*NodeWeight) []string {
var nodes []string
for _, v := range supernodes {
for i := 0; i < v.Weight; i++ {
nodes = append(nodes, v.Node)
}
}
return nodes
}
func string2NodeWeight(value string) (*NodeWeight, error) {
node, weight, err := splitNodeAndWeight(value)
if err != nil {
return nil, err
}
node, err = handleDefaultPort(node)
if err != nil {
return nil, err
}
return &NodeWeight{
Node: node,
Weight: weight,
}, nil
}
// splitNodeAndWeight returns the node address and weight which parsed by the given value.
// If no weight specified, the DefaultSupernodeWeight will be returned as the weight value.
func splitNodeAndWeight(value string) (string, int, error) {
result := strings.Split(value, string(weightSeparator))
splitLength := len(result)
switch splitLength {
case 1:
return result[0], DefaultSupernodeWeight, nil
case 2:
v, err := strconv.Atoi(result[1])
if err != nil {
return "", 0, err
}
return result[0], v, nil
default:
return "", 0, errortypes.ErrInvalidValue
}
}
func handleDefaultPort(node string) (string, error) {
result := strings.Split(node, ":")
splitLength := len(result)
if splitLength == 2 {
if result[0] == "" || result[1] == "" {
return "", errortypes.ErrInvalidValue
}
return node, nil
}
if splitLength == 1 {
return fmt.Sprintf("%s:%d", node, DefaultSupernodePort), nil
}
return "", errortypes.ErrInvalidValue
}