[#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 (
"context"
"errors"
"fmt"
"net"
"path/filepath"
@ -140,8 +141,19 @@ type cfg struct {
netMap atomicstd.Value // type netmap.NetMap
}
func (c *cfg) ProcessCurrentNetMap(f func(netmap.NetMap)) {
f(c.netMap.Load().(netmap.NetMap))
// ReadCurrentNetMap reads network map which has been cached at the
// 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 {

View file

@ -27,9 +27,10 @@ type NodeState interface {
// in NeoFS API v2 NodeInfo structure.
LocalNodeInfo() (*netmap.NodeInfo, error)
// ProcessCurrentNetMap passes current local network map of the storage node
// into the given handler.
ProcessCurrentNetMap(func(netmapSDK.NetMap))
// 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
}
// 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) {
var nmV2 netmap.NetMap
var nm netmap.NetMap
s.state.ProcessCurrentNetMap(func(netMap netmapSDK.NetMap) {
netMap.WriteToV2(&nmV2)
})
err := s.state.ReadCurrentNetMap(&nm)
if err != nil {
return nil, fmt.Errorf("read current local network map: %w", err)
}
body := new(netmap.SnapshotResponseBody)
body.SetNetMap(&nmV2)
body.SetNetMap(&nm)
resp := new(netmap.SnapshotResponse)
resp.SetBody(body)