2021-10-27 10:00:35 +00:00
|
|
|
package netmap
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/nspcc-dev/hrw"
|
2022-06-07 02:12:39 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/v2/netmap"
|
2021-10-27 10:00:35 +00:00
|
|
|
)
|
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
// NetMap represents NeoFS network map. It includes information about all
|
|
|
|
// storage nodes registered in NeoFS the network.
|
|
|
|
type NetMap struct {
|
|
|
|
nodes []NodeInfo
|
2022-06-07 02:12:39 +00:00
|
|
|
}
|
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
// SetNodes sets information list about all storage nodes from the NeoFS network.
|
2022-06-07 02:12:39 +00:00
|
|
|
//
|
2022-06-07 08:25:34 +00:00
|
|
|
// Argument MUST NOT be mutated, make a copy first.
|
|
|
|
//
|
|
|
|
// See also Nodes.
|
|
|
|
func (m *NetMap) SetNodes(nodes []NodeInfo) {
|
|
|
|
m.nodes = nodes
|
2022-06-07 02:12:39 +00:00
|
|
|
}
|
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
// Nodes returns nodes set using SetNodes.
|
|
|
|
//
|
|
|
|
// Return value MUST not be mutated, make a copy first.
|
|
|
|
func (m NetMap) Nodes() []NodeInfo {
|
|
|
|
return m.nodes
|
2022-06-07 02:12:39 +00:00
|
|
|
}
|
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
// nodes is a slice of NodeInfo instances needed for HRW sorting.
|
2022-06-07 02:12:39 +00:00
|
|
|
type nodes []NodeInfo
|
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
// assert nodes type provides hrw.Hasher required for HRW sorting.
|
|
|
|
var _ hrw.Hasher = nodes{}
|
|
|
|
|
2022-06-07 02:12:39 +00:00
|
|
|
// Hash is a function from hrw.Hasher interface. It is implemented
|
|
|
|
// to support weighted hrw sorting of buckets. Each bucket is already sorted by hrw,
|
|
|
|
// thus giving us needed "randomness".
|
|
|
|
func (n nodes) Hash() uint64 {
|
|
|
|
if len(n) > 0 {
|
|
|
|
return n[0].Hash()
|
|
|
|
}
|
2022-06-14 14:27:51 +00:00
|
|
|
|
2022-06-07 02:12:39 +00:00
|
|
|
return 0
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
|
2022-06-07 02:12:39 +00:00
|
|
|
// weights returns slice of nodes weights W.
|
|
|
|
func (n nodes) weights(wf weightFunc) []float64 {
|
|
|
|
w := make([]float64, 0, len(n))
|
|
|
|
for i := range n {
|
|
|
|
w = append(w, wf(n[i]))
|
|
|
|
}
|
|
|
|
|
|
|
|
return w
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
|
2022-06-07 02:12:39 +00:00
|
|
|
func flattenNodes(ns []nodes) nodes {
|
2022-06-07 03:40:59 +00:00
|
|
|
var sz, i int
|
|
|
|
|
|
|
|
for i = range ns {
|
|
|
|
sz += len(ns[i])
|
|
|
|
}
|
|
|
|
|
2022-06-07 02:12:39 +00:00
|
|
|
result := make(nodes, 0, sz)
|
2022-06-07 03:40:59 +00:00
|
|
|
|
2021-10-27 10:00:35 +00:00
|
|
|
for i := range ns {
|
|
|
|
result = append(result, ns[i]...)
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
// PlacementVectors sorts container nodes returned by ContainerNodes method
|
|
|
|
// and returns placement vectors for the entity identified by the given pivot.
|
2022-06-14 14:27:51 +00:00
|
|
|
// For example, in order to build node list to store the object, binary-encoded
|
|
|
|
// object identifier can be used as pivot. Result is deterministic for
|
2022-06-07 08:25:34 +00:00
|
|
|
// the fixed NetMap and parameters.
|
|
|
|
func (m NetMap) PlacementVectors(vectors [][]NodeInfo, pivot []byte) ([][]NodeInfo, error) {
|
2021-10-27 10:00:35 +00:00
|
|
|
h := hrw.Hash(pivot)
|
2022-06-07 06:01:25 +00:00
|
|
|
wf := defaultWeightFunc(m.nodes)
|
2022-06-07 02:12:39 +00:00
|
|
|
result := make([][]NodeInfo, len(vectors))
|
2021-10-27 10:00:35 +00:00
|
|
|
|
2022-06-07 02:12:39 +00:00
|
|
|
for i := range vectors {
|
|
|
|
result[i] = make([]NodeInfo, len(vectors[i]))
|
|
|
|
copy(result[i], vectors[i])
|
|
|
|
hrw.SortSliceByWeightValue(result[i], nodes(result[i]).weights(wf), h)
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
// ContainerNodes returns two-dimensional list of nodes as a result of applying
|
|
|
|
// given PlacementPolicy to the NetMap. Each line of the list corresponds to a
|
|
|
|
// replica descriptor. Line order corresponds to order of ReplicaDescriptor list
|
|
|
|
// in the policy. Nodes are pre-filtered according to the Filter list from
|
|
|
|
// the policy, and then selected by Selector list. Result is deterministic for
|
|
|
|
// the fixed NetMap and parameters.
|
|
|
|
//
|
|
|
|
// Result can be used in PlacementVectors.
|
|
|
|
func (m NetMap) ContainerNodes(p PlacementPolicy, pivot []byte) ([][]NodeInfo, error) {
|
2022-05-20 12:45:12 +00:00
|
|
|
c := newContext(m)
|
2021-10-27 10:00:35 +00:00
|
|
|
c.setPivot(pivot)
|
2022-06-07 08:25:34 +00:00
|
|
|
c.setCBF(p.backupFactor)
|
2021-10-27 10:00:35 +00:00
|
|
|
|
|
|
|
if err := c.processFilters(p); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := c.processSelectors(p); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
result := make([][]NodeInfo, len(p.replicas))
|
2021-10-27 10:00:35 +00:00
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
for i := range p.replicas {
|
|
|
|
sName := p.replicas[i].GetSelector()
|
|
|
|
if sName == "" {
|
|
|
|
if len(p.selectors) == 0 {
|
|
|
|
var s netmap.Selector
|
|
|
|
s.SetCount(p.replicas[i].GetCount())
|
|
|
|
s.SetFilter(mainFilterName)
|
2021-10-27 10:00:35 +00:00
|
|
|
|
|
|
|
nodes, err := c.getSelection(p, s)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
result[i] = flattenNodes(nodes)
|
|
|
|
}
|
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
for i := range p.selectors {
|
|
|
|
result[i] = append(result[i], flattenNodes(c.selections[p.selectors[i].GetName()])...)
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
nodes, ok := c.selections[sName]
|
2021-10-27 10:00:35 +00:00
|
|
|
if !ok {
|
2022-06-07 08:25:34 +00:00
|
|
|
return nil, fmt.Errorf("selector not found: REPLICA '%s'", sName)
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
result[i] = append(result[i], flattenNodes(nodes)...)
|
|
|
|
}
|
|
|
|
|
2022-06-07 02:12:39 +00:00
|
|
|
return result, nil
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|