2021-10-27 10:00:35 +00:00
|
|
|
package netmap
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2023-03-07 11:20:03 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/netmap"
|
|
|
|
"git.frostfs.info/TrueCloudLab/hrw"
|
2021-10-27 10:00:35 +00:00
|
|
|
)
|
|
|
|
|
2022-12-29 10:46:18 +00:00
|
|
|
// NetMap represents FrostFS network map. It includes information about all
|
|
|
|
// storage nodes registered in FrostFS the network.
|
2022-09-17 10:07:56 +00:00
|
|
|
//
|
2023-03-07 11:20:03 +00:00
|
|
|
// NetMap is mutually compatible with git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/netmap.NetMap
|
2022-09-17 10:07:56 +00:00
|
|
|
// message. See ReadFromV2 / WriteToV2 methods.
|
|
|
|
//
|
|
|
|
// Instances can be created using built-in var declaration.
|
2022-06-07 08:25:34 +00:00
|
|
|
type NetMap struct {
|
2022-09-17 10:07:56 +00:00
|
|
|
epoch uint64
|
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
nodes []NodeInfo
|
2022-06-07 02:12:39 +00:00
|
|
|
}
|
|
|
|
|
2022-09-17 10:07:56 +00:00
|
|
|
// ReadFromV2 reads NetMap from the netmap.NetMap message. Checks if the
|
2022-12-29 10:46:18 +00:00
|
|
|
// message conforms to FrostFS API V2 protocol.
|
2022-09-17 10:07:56 +00:00
|
|
|
//
|
|
|
|
// See also WriteToV2.
|
|
|
|
func (m *NetMap) ReadFromV2(msg netmap.NetMap) error {
|
|
|
|
var err error
|
|
|
|
nodes := msg.Nodes()
|
|
|
|
|
|
|
|
if nodes == nil {
|
|
|
|
m.nodes = nil
|
|
|
|
} else {
|
|
|
|
m.nodes = make([]NodeInfo, len(nodes))
|
|
|
|
|
|
|
|
for i := range nodes {
|
|
|
|
err = m.nodes[i].ReadFromV2(nodes[i])
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("invalid node info: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
m.epoch = msg.Epoch()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// WriteToV2 writes NetMap to the netmap.NetMap message. The message
|
|
|
|
// MUST NOT be nil.
|
|
|
|
//
|
|
|
|
// See also ReadFromV2.
|
|
|
|
func (m NetMap) WriteToV2(msg *netmap.NetMap) {
|
|
|
|
var nodes []netmap.NodeInfo
|
|
|
|
|
|
|
|
if m.nodes != nil {
|
|
|
|
nodes = make([]netmap.NodeInfo, len(m.nodes))
|
|
|
|
|
|
|
|
for i := range m.nodes {
|
|
|
|
m.nodes[i].WriteToV2(&nodes[i])
|
|
|
|
}
|
|
|
|
|
|
|
|
msg.SetNodes(nodes)
|
|
|
|
}
|
|
|
|
|
|
|
|
msg.SetEpoch(m.epoch)
|
|
|
|
}
|
|
|
|
|
2022-12-29 10:46:18 +00:00
|
|
|
// SetNodes sets information list about all storage nodes from the FrostFS 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-09-17 10:07:56 +00:00
|
|
|
// SetEpoch specifies revision number of the NetMap.
|
|
|
|
//
|
|
|
|
// See also Epoch.
|
|
|
|
func (m *NetMap) SetEpoch(epoch uint64) {
|
|
|
|
m.epoch = epoch
|
|
|
|
}
|
|
|
|
|
|
|
|
// Epoch returns epoch set using SetEpoch.
|
|
|
|
//
|
|
|
|
// Zero NetMap has zero revision.
|
|
|
|
func (m NetMap) Epoch() uint64 {
|
|
|
|
return m.epoch
|
|
|
|
}
|
|
|
|
|
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])
|
2023-02-28 11:04:02 +00:00
|
|
|
hrw.SortHasherSliceByWeightValue(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
|
|
|
|
2023-06-05 10:38:15 +00:00
|
|
|
// Note that the cached selectors are not used when the policy contains the UNIQUE flag.
|
|
|
|
// This is necessary because each selection vector affects potentially the subsequent vectors
|
|
|
|
// and thus we call getSelection in such case, in order to take into account nodes previously
|
|
|
|
// marked as used by earlier replicas.
|
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
|
|
|
|
2023-04-18 09:56:23 +00:00
|
|
|
nodes, err := c.getSelection(s)
|
2021-10-27 10:00:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
result[i] = flattenNodes(nodes)
|
|
|
|
}
|
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
for i := range p.selectors {
|
2023-06-05 10:38:15 +00:00
|
|
|
if p.unique {
|
|
|
|
nodes, err := c.getSelection(p.selectors[i])
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
result[i] = append(result[i], flattenNodes(nodes)...)
|
|
|
|
} else {
|
|
|
|
result[i] = append(result[i], flattenNodes(c.selections[p.selectors[i].GetName()])...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.unique {
|
|
|
|
c.addUsedNodes(result[i]...)
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-06-05 10:38:15 +00:00
|
|
|
if p.unique {
|
|
|
|
nodes, err := c.getSelection(*c.processedSelectors[sName])
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
result[i] = append(result[i], flattenNodes(nodes)...)
|
|
|
|
c.addUsedNodes(result[i]...)
|
|
|
|
} else {
|
|
|
|
nodes, ok := c.selections[sName]
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("selector not found: REPLICA '%s'", sName)
|
|
|
|
}
|
|
|
|
result[i] = append(result[i], flattenNodes(nodes)...)
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-06-07 02:12:39 +00:00
|
|
|
return result, nil
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|