2020-10-08 13:14:19 +00:00
|
|
|
package netmap
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-05-11 14:58:52 +00:00
|
|
|
"errors"
|
2022-07-22 14:04:37 +00:00
|
|
|
"fmt"
|
2020-10-08 13:14:19 +00:00
|
|
|
|
2023-03-07 13:38:26 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/netmap"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/refs"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/core/version"
|
2022-12-30 14:48:50 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/services/util/response"
|
2023-03-07 13:38:26 +00:00
|
|
|
netmapSDK "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/netmap"
|
|
|
|
versionsdk "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/version"
|
2020-10-08 13:14:19 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type executorSvc struct {
|
2022-05-11 14:58:52 +00:00
|
|
|
version refs.Version
|
|
|
|
|
|
|
|
state NodeState
|
2021-02-19 08:01:37 +00:00
|
|
|
|
|
|
|
netInfo NetworkInfo
|
2022-12-30 14:48:50 +00:00
|
|
|
|
|
|
|
respSvc *response.Service
|
2020-10-08 13:14:19 +00:00
|
|
|
}
|
|
|
|
|
2021-01-25 08:02:20 +00:00
|
|
|
// NodeState encapsulates information
|
|
|
|
// about current node state.
|
|
|
|
type NodeState interface {
|
|
|
|
// Must return current node state
|
2023-02-05 15:59:38 +00:00
|
|
|
// in FrostFS API v2 NodeInfo structure.
|
2021-01-25 08:02:20 +00:00
|
|
|
LocalNodeInfo() (*netmap.NodeInfo, error)
|
2022-09-17 12:37:01 +00:00
|
|
|
|
2022-09-26 06:31:44 +00:00
|
|
|
// ReadCurrentNetMap reads current local network map of the storage node
|
|
|
|
// into the given parameter. Returns any error encountered which prevented
|
|
|
|
// the network map to be read.
|
|
|
|
ReadCurrentNetMap(*netmap.NetMap) error
|
2021-01-25 08:02:20 +00:00
|
|
|
}
|
|
|
|
|
2021-02-19 08:01:37 +00:00
|
|
|
// NetworkInfo encapsulates source of the
|
2023-02-05 15:59:38 +00:00
|
|
|
// recent information about the FrostFS network.
|
2021-02-19 08:01:37 +00:00
|
|
|
type NetworkInfo interface {
|
2023-02-05 15:59:38 +00:00
|
|
|
// Must return recent network information in FrostFS API v2 NetworkInfo structure.
|
2021-09-20 16:13:17 +00:00
|
|
|
//
|
|
|
|
// If protocol version is <=2.9, MillisecondsPerBlock and network config should be unset.
|
2022-06-08 23:18:26 +00:00
|
|
|
Dump(versionsdk.Version) (*netmapSDK.NetworkInfo, error)
|
2021-02-19 08:01:37 +00:00
|
|
|
}
|
|
|
|
|
2022-12-30 14:48:50 +00:00
|
|
|
func NewExecutionService(s NodeState, v versionsdk.Version, netInfo NetworkInfo, respSvc *response.Service) Server {
|
|
|
|
if s == nil || netInfo == nil || !version.IsValid(v) || respSvc == nil {
|
2020-10-08 13:14:19 +00:00
|
|
|
// this should never happen, otherwise it programmers bug
|
|
|
|
panic("can't create netmap execution service")
|
|
|
|
}
|
|
|
|
|
2022-05-11 14:58:52 +00:00
|
|
|
res := &executorSvc{
|
2021-01-25 08:02:20 +00:00
|
|
|
state: s,
|
2021-02-19 08:01:37 +00:00
|
|
|
netInfo: netInfo,
|
2022-12-30 14:48:50 +00:00
|
|
|
respSvc: respSvc,
|
2020-10-08 13:14:19 +00:00
|
|
|
}
|
2022-05-11 14:58:52 +00:00
|
|
|
|
|
|
|
v.WriteToV2(&res.version)
|
|
|
|
|
|
|
|
return res
|
2020-10-08 13:14:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *executorSvc) LocalNodeInfo(
|
|
|
|
_ context.Context,
|
2021-06-25 15:38:58 +00:00
|
|
|
req *netmap.LocalNodeInfoRequest) (*netmap.LocalNodeInfoResponse, error) {
|
2022-05-11 14:58:52 +00:00
|
|
|
verV2 := req.GetMetaHeader().GetVersion()
|
|
|
|
if verV2 == nil {
|
|
|
|
return nil, errors.New("missing version")
|
|
|
|
}
|
|
|
|
|
|
|
|
var ver versionsdk.Version
|
2022-07-22 14:04:37 +00:00
|
|
|
if err := ver.ReadFromV2(*verV2); err != nil {
|
|
|
|
return nil, fmt.Errorf("can't read version: %w", err)
|
|
|
|
}
|
2021-06-25 15:38:58 +00:00
|
|
|
|
2021-01-25 08:02:20 +00:00
|
|
|
ni, err := s.state.LocalNodeInfo()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-06-25 15:38:58 +00:00
|
|
|
if addrNum := ni.NumberOfAddresses(); addrNum > 0 && ver.Minor() <= 7 {
|
|
|
|
ni2 := new(netmap.NodeInfo)
|
|
|
|
ni2.SetPublicKey(ni.GetPublicKey())
|
|
|
|
ni2.SetState(ni.GetState())
|
|
|
|
ni2.SetAttributes(ni.GetAttributes())
|
|
|
|
ni.IterateAddresses(func(s string) bool {
|
|
|
|
ni2.SetAddresses(s)
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
|
|
|
|
ni = ni2
|
|
|
|
}
|
|
|
|
|
2020-10-08 13:14:19 +00:00
|
|
|
body := new(netmap.LocalNodeInfoResponseBody)
|
2022-05-11 14:58:52 +00:00
|
|
|
body.SetVersion(&s.version)
|
2021-01-25 08:02:20 +00:00
|
|
|
body.SetNodeInfo(ni)
|
2020-10-08 13:14:19 +00:00
|
|
|
|
|
|
|
resp := new(netmap.LocalNodeInfoResponse)
|
|
|
|
resp.SetBody(body)
|
|
|
|
|
2022-12-30 14:48:50 +00:00
|
|
|
s.respSvc.SetMeta(resp)
|
2020-10-08 13:14:19 +00:00
|
|
|
return resp, nil
|
|
|
|
}
|
2021-02-19 08:01:37 +00:00
|
|
|
|
|
|
|
func (s *executorSvc) NetworkInfo(
|
|
|
|
_ context.Context,
|
2021-09-20 16:13:17 +00:00
|
|
|
req *netmap.NetworkInfoRequest) (*netmap.NetworkInfoResponse, error) {
|
2022-06-08 23:18:26 +00:00
|
|
|
verV2 := req.GetMetaHeader().GetVersion()
|
|
|
|
if verV2 == nil {
|
|
|
|
return nil, errors.New("missing protocol version in meta header")
|
|
|
|
}
|
|
|
|
|
|
|
|
var ver versionsdk.Version
|
2022-07-22 14:04:37 +00:00
|
|
|
if err := ver.ReadFromV2(*verV2); err != nil {
|
|
|
|
return nil, fmt.Errorf("can't read version: %w", err)
|
|
|
|
}
|
2022-06-08 23:18:26 +00:00
|
|
|
|
|
|
|
ni, err := s.netInfo.Dump(ver)
|
2021-02-19 08:01:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-06-08 23:18:26 +00:00
|
|
|
var niV2 netmap.NetworkInfo
|
|
|
|
ni.WriteToV2(&niV2)
|
|
|
|
|
2021-02-19 08:01:37 +00:00
|
|
|
body := new(netmap.NetworkInfoResponseBody)
|
2022-06-08 23:18:26 +00:00
|
|
|
body.SetNetworkInfo(&niV2)
|
2021-02-19 08:01:37 +00:00
|
|
|
|
|
|
|
resp := new(netmap.NetworkInfoResponse)
|
|
|
|
resp.SetBody(body)
|
|
|
|
|
2022-12-30 14:48:50 +00:00
|
|
|
s.respSvc.SetMeta(resp)
|
2021-02-19 08:01:37 +00:00
|
|
|
return resp, nil
|
|
|
|
}
|
2022-09-17 12:37:01 +00:00
|
|
|
|
2023-04-26 08:24:40 +00:00
|
|
|
func (s *executorSvc) Snapshot(_ context.Context, _ *netmap.SnapshotRequest) (*netmap.SnapshotResponse, error) {
|
2022-09-26 06:31:44 +00:00
|
|
|
var nm netmap.NetMap
|
2022-09-17 12:37:01 +00:00
|
|
|
|
2022-09-26 06:31:44 +00:00
|
|
|
err := s.state.ReadCurrentNetMap(&nm)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("read current local network map: %w", err)
|
|
|
|
}
|
2022-09-17 12:37:01 +00:00
|
|
|
|
|
|
|
body := new(netmap.SnapshotResponseBody)
|
2022-09-26 06:31:44 +00:00
|
|
|
body.SetNetMap(&nm)
|
2022-09-17 12:37:01 +00:00
|
|
|
|
|
|
|
resp := new(netmap.SnapshotResponse)
|
|
|
|
resp.SetBody(body)
|
|
|
|
|
2022-12-30 14:48:50 +00:00
|
|
|
s.respSvc.SetMeta(resp)
|
2022-09-17 12:37:01 +00:00
|
|
|
return resp, nil
|
|
|
|
}
|