-
Notifications
You must be signed in to change notification settings - Fork 394
/
state.go
42 lines (35 loc) · 1.23 KB
/
state.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
// Copyright (C) 2020 Storj Labs, Inc.
// See LICENSE for copying information.
package nodeselection
import (
"github.com/zeebo/errs"
"storj.io/common/storj"
)
// ErrNotEnoughNodes is when selecting nodes failed with the given parameters.
var ErrNotEnoughNodes = errs.Class("not enough nodes")
// State includes a stateful selector (indexed nodes) for each placement.
type State map[storj.PlacementConstraint]NodeSelector
// NewState initializes the State for each placement.
func NewState(nodes []*SelectedNode, placements PlacementDefinitions) State {
state := make(State)
for id, placement := range placements {
selector := placement.Selector
if selector == nil {
selector = RandomSelector()
}
state[id] = selector(nodes, placement.NodeFilter)
}
return state
}
// Select picks the required nodes given a specific placement.
func (s State) Select(p storj.PlacementConstraint, count int, alreadySelected []storj.NodeID) ([]*SelectedNode, error) {
selector, found := s[p]
if !found {
return nil, Error.New("Placement is not defined: %d", p)
}
nodes, err := selector(count, alreadySelected)
if len(nodes) < count {
return nodes, ErrNotEnoughNodes.New("requested from cache %d, found %d", count, len(nodes))
}
return nodes, err
}