2021-04-22 14:56:25 +00:00
|
|
|
package common
|
2021-04-18 08:51:49 +00:00
|
|
|
|
|
|
|
import (
|
2021-06-11 15:29:49 +00:00
|
|
|
"fmt"
|
2021-04-18 08:51:49 +00:00
|
|
|
|
2023-04-12 14:35:10 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/internal/logs"
|
2023-03-07 13:38:26 +00:00
|
|
|
netmapcore "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/core/netmap"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/util/logger"
|
|
|
|
apiNetmap "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/netmap"
|
|
|
|
apireputation "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/reputation"
|
|
|
|
"git.frostfs.info/TrueCloudLab/hrw"
|
2021-04-18 08:51:49 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
// managerBuilder is implementation of reputation ManagerBuilder interface.
|
|
|
|
// It sorts nodes in NetMap with HRW algorithms and
|
|
|
|
// takes the next node after the current one as the only manager.
|
|
|
|
type managerBuilder struct {
|
|
|
|
log *logger.Logger
|
|
|
|
nmSrc netmapcore.Source
|
2021-04-29 05:30:41 +00:00
|
|
|
opts *mngOptions
|
2021-04-18 08:51:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ManagersPrm groups the required parameters of the managerBuilder's constructor.
|
|
|
|
//
|
|
|
|
// All values must comply with the requirements imposed on them.
|
|
|
|
// Passing incorrect parameter values will result in constructor
|
|
|
|
// failure (error or panic depending on the implementation).
|
|
|
|
type ManagersPrm struct {
|
|
|
|
NetMapSource netmapcore.Source
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewManagerBuilder creates a new instance of the managerBuilder.
|
|
|
|
//
|
|
|
|
// Panics if at least one value of the parameters is invalid.
|
|
|
|
//
|
|
|
|
// The created managerBuilder does not require additional
|
|
|
|
// initialization and is completely ready for work.
|
2021-06-11 15:29:49 +00:00
|
|
|
func NewManagerBuilder(prm ManagersPrm, opts ...MngOption) ManagerBuilder {
|
2021-04-18 08:51:49 +00:00
|
|
|
switch {
|
|
|
|
case prm.NetMapSource == nil:
|
2021-06-11 15:29:49 +00:00
|
|
|
panic(fmt.Sprintf("invalid NetMapSource (%T):%v", prm.NetMapSource, prm.NetMapSource))
|
2021-04-18 08:51:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
o := defaultMngOpts()
|
|
|
|
|
|
|
|
for i := range opts {
|
|
|
|
opts[i](o)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &managerBuilder{
|
|
|
|
log: o.log,
|
|
|
|
nmSrc: prm.NetMapSource,
|
2021-04-29 05:30:41 +00:00
|
|
|
opts: o,
|
2021-04-18 08:51:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-17 12:03:55 +00:00
|
|
|
// implements Server on apiNetmap.NodeInfo.
|
2022-06-08 23:18:26 +00:00
|
|
|
type nodeServer apiNetmap.NodeInfo
|
|
|
|
|
|
|
|
func (x nodeServer) PublicKey() []byte {
|
|
|
|
return (apiNetmap.NodeInfo)(x).PublicKey()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (x nodeServer) IterateAddresses(f func(string) bool) {
|
|
|
|
(apiNetmap.NodeInfo)(x).IterateNetworkEndpoints(f)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (x nodeServer) NumberOfAddresses() int {
|
|
|
|
return (apiNetmap.NodeInfo)(x).NumberOfNetworkEndpoints()
|
|
|
|
}
|
|
|
|
|
2022-09-26 12:34:01 +00:00
|
|
|
func (x nodeServer) ExternalAddresses() []string {
|
|
|
|
return (apiNetmap.NodeInfo)(x).ExternalAddresses()
|
|
|
|
}
|
|
|
|
|
2021-04-18 08:51:49 +00:00
|
|
|
// BuildManagers sorts nodes in NetMap with HRW algorithms and
|
|
|
|
// takes the next node after the current one as the only manager.
|
2022-07-04 13:24:51 +00:00
|
|
|
func (mb *managerBuilder) BuildManagers(epoch uint64, p apireputation.PeerID) ([]ServerInfo, error) {
|
2023-04-12 14:35:10 +00:00
|
|
|
mb.log.Debug(logs.CommonStartBuildingManagers,
|
2021-03-01 19:01:45 +00:00
|
|
|
zap.Uint64("epoch", epoch),
|
2022-07-04 13:24:51 +00:00
|
|
|
zap.Stringer("peer", p),
|
2021-03-01 19:01:45 +00:00
|
|
|
)
|
|
|
|
|
2021-04-18 08:51:49 +00:00
|
|
|
nm, err := mb.nmSrc.GetNetMapByEpoch(epoch)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-06-08 23:18:26 +00:00
|
|
|
nmNodes := nm.Nodes()
|
|
|
|
|
2021-04-18 08:51:49 +00:00
|
|
|
// make a copy to keep order consistency of the origin netmap after sorting
|
2022-06-08 23:18:26 +00:00
|
|
|
nodes := make([]apiNetmap.NodeInfo, len(nmNodes))
|
2021-04-18 08:51:49 +00:00
|
|
|
|
2022-06-08 23:18:26 +00:00
|
|
|
copy(nodes, nmNodes)
|
2021-04-18 08:51:49 +00:00
|
|
|
|
2023-02-27 13:39:15 +00:00
|
|
|
hrw.SortHasherSliceByValue(nodes, epoch)
|
2021-04-18 08:51:49 +00:00
|
|
|
|
|
|
|
for i := range nodes {
|
2022-07-04 13:24:51 +00:00
|
|
|
if apireputation.ComparePeerKey(p, nodes[i].PublicKey()) {
|
2021-04-18 08:51:49 +00:00
|
|
|
managerIndex := i + 1
|
|
|
|
|
|
|
|
if managerIndex == len(nodes) {
|
|
|
|
managerIndex = 0
|
|
|
|
}
|
|
|
|
|
2022-06-08 23:18:26 +00:00
|
|
|
return []ServerInfo{nodeServer(nodes[managerIndex])}, nil
|
2021-04-18 08:51:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type mngOptions struct {
|
|
|
|
log *logger.Logger
|
|
|
|
}
|
|
|
|
|
|
|
|
type MngOption func(*mngOptions)
|
|
|
|
|
|
|
|
func defaultMngOpts() *mngOptions {
|
|
|
|
return &mngOptions{
|
2022-09-28 07:41:01 +00:00
|
|
|
log: &logger.Logger{Logger: zap.L()},
|
2021-04-18 08:51:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithLogger returns MngOption to specify logging component.
|
|
|
|
func WithLogger(l *logger.Logger) MngOption {
|
|
|
|
return func(o *mngOptions) {
|
|
|
|
if l != nil {
|
|
|
|
o.log = l
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|