2021-10-25 13:27:59 +01:00
|
|
|
// Copyright (C) 2020 Storj Labs, Inc.
|
2020-05-15 15:32:28 +01:00
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
2023-07-07 09:31:58 +01:00
|
|
|
package nodeselection
|
2020-05-15 15:32:28 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/zeebo/errs"
|
|
|
|
|
|
|
|
"storj.io/common/storj"
|
|
|
|
)
|
|
|
|
|
2023-08-17 10:53:05 +01:00
|
|
|
const (
|
|
|
|
// AutoExcludeSubnet is placement annotation key to turn off subnet restrictions.
|
|
|
|
AutoExcludeSubnet = "autoExcludeSubnet"
|
|
|
|
|
|
|
|
// AutoExcludeSubnetOFF is the value of AutoExcludeSubnet to disable subnet restrictions.
|
|
|
|
AutoExcludeSubnetOFF = "off"
|
|
|
|
)
|
|
|
|
|
2023-08-21 11:11:22 +01:00
|
|
|
// AllowSameSubnet is a short to check if Subnet exclusion is disabled == allow pick nodes from the same subnet.
|
|
|
|
func AllowSameSubnet(filter NodeFilter) bool {
|
|
|
|
return GetAnnotation(filter, AutoExcludeSubnet) == AutoExcludeSubnetOFF
|
|
|
|
}
|
|
|
|
|
2020-05-15 15:32:28 +01:00
|
|
|
// ErrNotEnoughNodes is when selecting nodes failed with the given parameters.
|
|
|
|
var ErrNotEnoughNodes = errs.Class("not enough nodes")
|
|
|
|
|
|
|
|
// State defines a node selector state that allows for selection.
|
|
|
|
type State struct {
|
|
|
|
mu sync.RWMutex
|
|
|
|
|
|
|
|
// netByID returns subnet based on storj.NodeID
|
|
|
|
netByID map[storj.NodeID]string
|
2023-08-01 12:50:22 +01:00
|
|
|
|
|
|
|
// byNetwork contains selectors for distinct selection.
|
|
|
|
byNetwork struct {
|
2020-05-15 15:32:28 +01:00
|
|
|
Reputable SelectBySubnet
|
|
|
|
New SelectBySubnet
|
|
|
|
}
|
|
|
|
|
2023-08-01 12:50:22 +01:00
|
|
|
byID struct {
|
|
|
|
Reputable SelectByID
|
|
|
|
New SelectByID
|
|
|
|
}
|
2020-05-15 15:32:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Selector defines interface for selecting nodes.
|
|
|
|
type Selector interface {
|
2021-10-25 13:27:59 +01:00
|
|
|
// Select selects up-to n nodes which are included by the criteria.
|
|
|
|
// empty criteria includes all the nodes
|
2023-06-30 11:13:18 +01:00
|
|
|
Select(n int, nodeFilter NodeFilter) []*SelectedNode
|
2020-05-15 15:32:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewState returns a state based on the input.
|
2023-06-30 11:35:07 +01:00
|
|
|
func NewState(reputableNodes, newNodes []*SelectedNode) *State {
|
2020-05-15 15:32:28 +01:00
|
|
|
state := &State{}
|
|
|
|
|
|
|
|
state.netByID = map[storj.NodeID]string{}
|
|
|
|
for _, node := range reputableNodes {
|
|
|
|
state.netByID[node.ID] = node.LastNet
|
|
|
|
}
|
|
|
|
for _, node := range newNodes {
|
|
|
|
state.netByID[node.ID] = node.LastNet
|
|
|
|
}
|
|
|
|
|
2023-08-01 12:50:22 +01:00
|
|
|
state.byNetwork.Reputable = SelectBySubnetFromNodes(reputableNodes)
|
|
|
|
state.byNetwork.New = SelectBySubnetFromNodes(newNodes)
|
|
|
|
|
|
|
|
state.byID.Reputable = SelectByID(reputableNodes)
|
|
|
|
state.byID.New = SelectByID(newNodes)
|
2020-05-15 15:32:28 +01:00
|
|
|
|
|
|
|
return state
|
|
|
|
}
|
|
|
|
|
2023-08-01 12:50:22 +01:00
|
|
|
// SelectionType defines how to select nodes randomly.
|
|
|
|
type SelectionType int8
|
|
|
|
|
|
|
|
const (
|
|
|
|
// SelectionTypeByNetwork chooses subnets randomly, and one node from each subnet.
|
|
|
|
SelectionTypeByNetwork = iota
|
|
|
|
|
|
|
|
// SelectionTypeByID chooses nodes randomly.
|
|
|
|
SelectionTypeByID
|
|
|
|
)
|
|
|
|
|
2020-05-15 15:32:28 +01:00
|
|
|
// Request contains arguments for State.Request.
|
|
|
|
type Request struct {
|
2023-08-01 12:50:22 +01:00
|
|
|
Count int
|
|
|
|
NewFraction float64
|
|
|
|
NodeFilters NodeFilters
|
|
|
|
SelectionType SelectionType
|
2020-05-15 15:32:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Select selects requestedCount nodes where there will be newFraction nodes.
|
2023-06-30 11:35:07 +01:00
|
|
|
func (state *State) Select(ctx context.Context, request Request) (_ []*SelectedNode, err error) {
|
2020-05-15 15:32:28 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
state.mu.RLock()
|
|
|
|
defer state.mu.RUnlock()
|
|
|
|
|
|
|
|
totalCount := request.Count
|
|
|
|
newCount := int(float64(totalCount) * request.NewFraction)
|
|
|
|
|
2023-06-30 11:35:07 +01:00
|
|
|
var selected []*SelectedNode
|
2020-05-15 15:32:28 +01:00
|
|
|
|
|
|
|
var reputableNodes Selector
|
|
|
|
var newNodes Selector
|
|
|
|
|
2023-08-01 12:50:22 +01:00
|
|
|
switch request.SelectionType {
|
|
|
|
case SelectionTypeByNetwork:
|
|
|
|
reputableNodes = state.byNetwork.Reputable
|
|
|
|
newNodes = state.byNetwork.New
|
|
|
|
case SelectionTypeByID:
|
|
|
|
reputableNodes = state.byID.Reputable
|
|
|
|
newNodes = state.byID.New
|
|
|
|
default:
|
|
|
|
return nil, errs.New("Unsupported selection type: %d", request.SelectionType)
|
|
|
|
}
|
2020-05-15 15:32:28 +01:00
|
|
|
|
|
|
|
// Get a random selection of new nodes out of the cache first so that if there aren't
|
|
|
|
// enough new nodes on the network, we can fall back to using reputable nodes instead.
|
|
|
|
selected = append(selected,
|
2023-06-30 11:13:18 +01:00
|
|
|
newNodes.Select(newCount, request.NodeFilters)...)
|
2020-05-15 15:32:28 +01:00
|
|
|
|
|
|
|
// Get all the remaining reputable nodes.
|
|
|
|
reputableCount := totalCount - len(selected)
|
2023-08-17 10:53:05 +01:00
|
|
|
|
|
|
|
filters := request.NodeFilters
|
|
|
|
if GetAnnotation(filters, AutoExcludeSubnet) != AutoExcludeSubnetOFF {
|
|
|
|
filters = append(append(NodeFilters{}, request.NodeFilters...), ExcludedNodeNetworks(selected))
|
|
|
|
}
|
|
|
|
|
|
|
|
selected = append(selected, reputableNodes.Select(reputableCount, filters)...)
|
2020-05-15 15:32:28 +01:00
|
|
|
|
|
|
|
if len(selected) < totalCount {
|
|
|
|
return selected, ErrNotEnoughNodes.New("requested from cache %d, found %d", totalCount, len(selected))
|
|
|
|
}
|
|
|
|
return selected, nil
|
|
|
|
}
|
|
|
|
|
2023-06-30 11:13:18 +01:00
|
|
|
// ExcludeNetworksBasedOnNodes will create a NodeFilter which exclude all nodes which shares subnet with the specified ones.
|
|
|
|
func (state *State) ExcludeNetworksBasedOnNodes(ds []storj.NodeID) NodeFilter {
|
|
|
|
uniqueExcludedNet := make(map[string]struct{}, len(ds))
|
|
|
|
for _, id := range ds {
|
|
|
|
net := state.netByID[id]
|
|
|
|
uniqueExcludedNet[net] = struct{}{}
|
|
|
|
}
|
|
|
|
excludedNet := make([]string, len(uniqueExcludedNet))
|
|
|
|
i := 0
|
|
|
|
for net := range uniqueExcludedNet {
|
|
|
|
excludedNet[i] = net
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
return ExcludedNetworks(excludedNet)
|
|
|
|
}
|