-
Notifications
You must be signed in to change notification settings - Fork 385
/
placement.go
325 lines (290 loc) · 10.2 KB
/
placement.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
// Copyright (C) 2023 Storj Labs, Inc.
// See LICENSE for copying information.
package nodeselection
import (
"bytes"
"os"
"strconv"
"strings"
"github.com/jtolio/mito"
"github.com/spf13/pflag"
"github.com/zeebo/errs"
"storj.io/common/storj"
"storj.io/common/storj/location"
)
// Placement defined all the custom behavior metadata of a specific placement group.
type Placement struct {
// the unique ID of the placement
ID storj.PlacementConstraint
// binding condition for filtering out nodes
NodeFilter NodeFilter
// selector is the method how the nodes are selected from the full node space (eg. pick a subnet first, and pick a node from the subnet)
Selector NodeSelectorInit
// checked by repair job, applied to the full selection. Out of placement items will be replaced by new, selected by the Selector.
Invariant Invariant
}
// Match implements NodeFilter.
func (p Placement) Match(node *SelectedNode) bool {
return p.NodeFilter.Match(node)
}
// GetAnnotation implements NodeFilterWithAnnotation.
func (p Placement) GetAnnotation(name string) string {
switch filter := p.NodeFilter.(type) {
case NodeFilterWithAnnotation:
return filter.GetAnnotation(name)
default:
return ""
}
}
var _ NodeFilter = Placement{}
var _ NodeFilterWithAnnotation = Placement{}
// NodeSelectorInit initializes a stateful NodeSelector when node cache is refreshed.
type NodeSelectorInit func([]*SelectedNode, NodeFilter) NodeSelector
// NodeSelector pick random nodes based on a specific algorithm.
type NodeSelector func(n int, alreadySelected []storj.NodeID) ([]*SelectedNode, error)
// ErrPlacement is used for placement definition related parsing errors.
var ErrPlacement = errs.Class("placement")
// PlacementRules can crate filter based on the placement identifier.
type PlacementRules func(constraint storj.PlacementConstraint) (filter NodeFilter)
// PlacementDefinitions can include the placement definitions for each known identifier.
type PlacementDefinitions map[storj.PlacementConstraint]Placement
// ConfigurablePlacementRule is a string configuration includes all placement rules in the form of id1:def1,id2:def2...
type ConfigurablePlacementRule struct {
PlacementRules string
}
// String implements pflag.Value.
func (c *ConfigurablePlacementRule) String() string {
return c.PlacementRules
}
// Set implements pflag.Value.
func (c *ConfigurablePlacementRule) Set(s string) error {
c.PlacementRules = s
return nil
}
// Type implements pflag.Value.
func (c *ConfigurablePlacementRule) Type() string {
return "configurable-placement-rule"
}
// Parse creates the PlacementDefinitions from the string rules.
// defaultPlacement is used to create the placement if no placement has been set.
func (c ConfigurablePlacementRule) Parse(defaultPlacement func() (Placement, error)) (PlacementDefinitions, error) {
if c.PlacementRules == "" {
dp, err := defaultPlacement()
if err != nil {
return PlacementDefinitions{}, err
}
pdef := NewPlacementDefinitions(dp)
pdef.AddLegacyStaticRules()
return pdef, nil
}
rules := c.PlacementRules
if _, err := os.Stat(rules); err == nil {
ruleBytes, err := os.ReadFile(rules)
if err != nil {
return nil, ErrPlacement.New("Placement definition file couldn't be read: %s %v", rules, err)
}
rules = string(ruleBytes)
}
if strings.HasPrefix(rules, "/") || strings.HasPrefix(rules, "./") || strings.HasPrefix(rules, "../") {
return nil, ErrPlacement.New("Placement definition (%s) looks to be a path, but file doesn't exist at that place", rules)
}
d := PlacementDefinitions(map[storj.PlacementConstraint]Placement{})
d.AddLegacyStaticRules()
err := d.AddPlacementFromString(rules)
return d, err
}
var _ pflag.Value = &ConfigurablePlacementRule{}
// TestPlacementDefinitions creates placements for testing. Only 0 placement is defined with subnetfiltering.
func TestPlacementDefinitions() PlacementDefinitions {
return map[storj.PlacementConstraint]Placement{
storj.DefaultPlacement: {
ID: storj.DefaultPlacement,
NodeFilter: AnyFilter{},
Selector: AttributeGroupSelector(LastNetAttribute),
Invariant: ClumpingByAttribute(LastNetAttribute, 1),
},
}
}
// TestPlacementDefinitionsWithFraction creates placements for testing. Similar to TestPlacementDefinitions, but also selects newNodes based on fraction.
func TestPlacementDefinitionsWithFraction(newNodeFraction float64) PlacementDefinitions {
return map[storj.PlacementConstraint]Placement{
storj.DefaultPlacement: {
ID: storj.DefaultPlacement,
NodeFilter: AnyFilter{},
Selector: UnvettedSelector(newNodeFraction, AttributeGroupSelector(LastNetAttribute)),
},
}
}
// NewPlacementDefinitions creates a PlacementDefinition with a default placement.
func NewPlacementDefinitions(placements ...Placement) PlacementDefinitions {
result := map[storj.PlacementConstraint]Placement{}
for _, p := range placements {
result[p.ID] = p
}
return result
}
// AddLegacyStaticRules initializes all the placement rules defined earlier in static golang code.
func (d PlacementDefinitions) AddLegacyStaticRules() {
d[storj.EEA] = Placement{
NodeFilter: NodeFilters{NewCountryFilter(location.NewSet(EeaCountriesWithoutEu...).With(EuCountries...))},
}
d[storj.EU] = Placement{
NodeFilter: NodeFilters{NewCountryFilter(location.NewSet(EuCountries...))},
}
d[storj.US] = Placement{
NodeFilter: NodeFilters{NewCountryFilter(location.NewSet(location.UnitedStates))},
}
d[storj.DE] = Placement{
NodeFilter: NodeFilters{NewCountryFilter(location.NewSet(location.Germany))},
}
d[storj.NR] = Placement{
NodeFilter: NodeFilters{NewCountryFilter(location.NewFullSet().Without(location.Russia, location.Belarus, location.None))},
}
}
// AddPlacement registers a new placement.
func (d PlacementDefinitions) AddPlacement(id storj.PlacementConstraint, placement Placement) {
d[id] = placement
}
// AddPlacementRule registers a new placement.
func (d PlacementDefinitions) AddPlacementRule(id storj.PlacementConstraint, filter NodeFilter) {
placement := Placement{
NodeFilter: filter,
Selector: AttributeGroupSelector(LastNetAttribute),
Invariant: ClumpingByAttribute(LastNetAttribute, 1),
}
if GetAnnotation(filter, AutoExcludeSubnet) == AutoExcludeSubnetOFF {
placement.Selector = RandomSelector()
}
d[id] = placement
}
type stringNotMatch string
// AddPlacementFromString parses placement definition form string representations from id:definition;id:definition;...
func (d PlacementDefinitions) AddPlacementFromString(definitions string) error {
env := map[any]any{
"country": func(countries ...string) (NodeFilter, error) {
return NewCountryFilterFromString(countries)
},
"placement": func(ix int64) (NodeFilter, error) {
filter, found := d[storj.PlacementConstraint(ix)]
if !found {
return nil, ErrPlacement.New("Placement %d is referenced before defined. Please define it first!", ix)
}
return filter.NodeFilter, nil
},
"all": func(filters ...NodeFilter) (NodeFilters, error) {
res := NodeFilters{}
for _, filter := range filters {
res = append(res, filter)
}
return res, nil
},
mito.OpAnd: func(env map[any]any, a, b any) (any, error) {
filter1, ok1 := a.(NodeFilter)
filter2, ok2 := b.(NodeFilter)
if !ok1 || !ok2 {
return nil, ErrPlacement.New("&& is supported only between NodeFilter instances")
}
res := NodeFilters{filter1, filter2}
return res, nil
},
mito.OpOr: func(env map[any]any, a, b any) (any, error) {
filter1, ok1 := a.(NodeFilter)
filter2, ok2 := b.(NodeFilter)
if !ok1 || !ok2 {
return nil, errs.New("OR is supported only between NodeFilter instances")
}
return OrFilter{filter1, filter2}, nil
},
"tag": func(nodeIDstr string, key string, value any) (NodeFilters, error) {
nodeID, err := storj.NodeIDFromString(nodeIDstr)
if err != nil {
return nil, err
}
var rawValue []byte
match := bytes.Equal
switch v := value.(type) {
case string:
rawValue = []byte(v)
case []byte:
rawValue = v
case stringNotMatch:
match = func(a, b []byte) bool {
return !bytes.Equal(a, b)
}
rawValue = []byte(v)
default:
return nil, ErrPlacement.New("3rd argument of tag() should be string or []byte")
}
res := NodeFilters{
NewTagFilter(nodeID, key, rawValue, match),
}
return res, nil
},
"annotated": func(filter NodeFilter, kv ...Annotation) (AnnotatedNodeFilter, error) {
return AnnotatedNodeFilter{
Filter: filter,
Annotations: kv,
}, nil
},
"annotation": func(key string, value string) (Annotation, error) {
return Annotation{
Key: key,
Value: value,
}, nil
},
"exclude": func(filter NodeFilter) (NodeFilter, error) {
return NewExcludeFilter(filter), nil
},
"empty": func() string {
return ""
},
"notEmpty": func() any {
return stringNotMatch("")
},
}
for _, definition := range strings.Split(definitions, ";") {
definition = strings.TrimSpace(definition)
if definition == "" {
continue
}
idDef := strings.SplitN(definition, ":", 2)
if len(idDef) != 2 {
return ErrPlacement.New("placement definition should be in the form ID:definition (but it was %s)", definition)
}
val, err := mito.Eval(idDef[1], env)
if err != nil {
return ErrPlacement.New("Error in line '%s' when placement rule is parsed: %v", idDef[1], err)
}
id, err := strconv.Atoi(idDef[0])
if err != nil {
return ErrPlacement.Wrap(err)
}
placement := Placement{
NodeFilter: val.(NodeFilter),
}
if GetAnnotation(placement.NodeFilter, AutoExcludeSubnet) != AutoExcludeSubnetOFF {
placement.Selector = AttributeGroupSelector(LastNetAttribute)
placement.Invariant = ClumpingByAttribute(LastNetAttribute, 1)
} else {
placement.Selector = RandomSelector()
}
d[storj.PlacementConstraint(id)] = placement
}
return nil
}
// CreateFilters implements PlacementCondition.
func (d PlacementDefinitions) CreateFilters(constraint storj.PlacementConstraint) (filter NodeFilter) {
if filters, found := d[constraint]; found {
return filters.NodeFilter
}
return NodeFilters{
ExcludeAllFilter{},
}
}
// SupportedPlacements returns all the IDs, which have associated placement rules.
func (d PlacementDefinitions) SupportedPlacements() (res []storj.PlacementConstraint) {
for id := range d {
res = append(res, id)
}
return res
}