From 93742d37b7c8bbbf93ac7ec29fa204c85de1cf8a Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Mon, 26 Sep 2022 10:31:44 +0400 Subject: [PATCH] [#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 --- cmd/neofs-node/config.go | 16 ++++++++++++++-- pkg/services/netmap/executor.go | 18 ++++++++++-------- 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/cmd/neofs-node/config.go b/cmd/neofs-node/config.go index ac3ed7bd..25c66b6d 100644 --- a/cmd/neofs-node/config.go +++ b/cmd/neofs-node/config.go @@ -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 { diff --git a/pkg/services/netmap/executor.go b/pkg/services/netmap/executor.go index c4453327..b9989e91 100644 --- a/pkg/services/netmap/executor.go +++ b/pkg/services/netmap/executor.go @@ -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)