[#1793] node/netmap: Change interface of the latest network map reader

Replace `ProcessCurrentNetMap` method of `NodeState` interface with
`ReadCurrentNetMap` one with two changes:
 * Replace network map type from NeoFS SDK package with the
   protocol-generated message. This replaces all the business logic to
   the application layer.
 * Support error return. This allows to cover problem node states.

Return an error from `NodeState.ReadCurrentNetMap` method implemeted
through `atomic.Value` if `Store` method has not been called yet.

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
Leonard Lyubich 2022-09-26 10:31:44 +04:00 committed by fyrchik
parent 485a5418d2
commit 93742d37b7
2 changed files with 24 additions and 10 deletions

View file

@ -2,6 +2,7 @@ package main
import ( import (
"context" "context"
"errors"
"fmt" "fmt"
"net" "net"
"path/filepath" "path/filepath"
@ -140,8 +141,19 @@ type cfg struct {
netMap atomicstd.Value // type netmap.NetMap netMap atomicstd.Value // type netmap.NetMap
} }
func (c *cfg) ProcessCurrentNetMap(f func(netmap.NetMap)) { // ReadCurrentNetMap reads network map which has been cached at the
f(c.netMap.Load().(netmap.NetMap)) // latest epoch. Returns an error if value has not been cached yet.
//
// Provides interface for NetmapService server.
func (c *cfg) ReadCurrentNetMap(msg *netmapV2.NetMap) error {
val := c.netMap.Load()
if val == nil {
return errors.New("missing local network map")
}
val.(netmap.NetMap).WriteToV2(msg)
return nil
} }
type cfgGRPC struct { type cfgGRPC struct {

View file

@ -27,9 +27,10 @@ type NodeState interface {
// in NeoFS API v2 NodeInfo structure. // in NeoFS API v2 NodeInfo structure.
LocalNodeInfo() (*netmap.NodeInfo, error) LocalNodeInfo() (*netmap.NodeInfo, error)
// ProcessCurrentNetMap passes current local network map of the storage node // ReadCurrentNetMap reads current local network map of the storage node
// into the given handler. // into the given parameter. Returns any error encountered which prevented
ProcessCurrentNetMap(func(netmapSDK.NetMap)) // the network map to be read.
ReadCurrentNetMap(*netmap.NetMap) error
} }
// NetworkInfo encapsulates source of the // NetworkInfo encapsulates source of the
@ -129,14 +130,15 @@ func (s *executorSvc) NetworkInfo(
} }
func (s *executorSvc) Snapshot(_ context.Context, req *netmap.SnapshotRequest) (*netmap.SnapshotResponse, error) { func (s *executorSvc) Snapshot(_ context.Context, req *netmap.SnapshotRequest) (*netmap.SnapshotResponse, error) {
var nmV2 netmap.NetMap var nm netmap.NetMap
s.state.ProcessCurrentNetMap(func(netMap netmapSDK.NetMap) { err := s.state.ReadCurrentNetMap(&nm)
netMap.WriteToV2(&nmV2) if err != nil {
}) return nil, fmt.Errorf("read current local network map: %w", err)
}
body := new(netmap.SnapshotResponseBody) body := new(netmap.SnapshotResponseBody)
body.SetNetMap(&nmV2) body.SetNetMap(&nm)
resp := new(netmap.SnapshotResponse) resp := new(netmap.SnapshotResponse)
resp.SetBody(body) resp.SetBody(body)