From a8481223d638f95eb8e44362af14b9e4b10c2b25 Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Wed, 21 Oct 2020 18:12:31 +0300 Subject: [PATCH] [#60] cmd/neofs-node: Implement primary network State instance Define networkState structure and implement netmap.State interface on it. Set epoch number on NewEpoch notification from chain. Provide WithNetworkState option to Put service constructor. Signed-off-by: Leonard Lyubich --- cmd/neofs-node/config.go | 2 ++ cmd/neofs-node/netmap.go | 26 ++++++++++++++++++++++++++ cmd/neofs-node/object.go | 1 + 3 files changed, 29 insertions(+) diff --git a/cmd/neofs-node/config.go b/cmd/neofs-node/config.go index 8d7e2ced1..582bafd7d 100644 --- a/cmd/neofs-node/config.go +++ b/cmd/neofs-node/config.go @@ -163,6 +163,8 @@ type cfgNetmap struct { parsers map[event.Type]event.Parser subscribers map[event.Type][]event.Handler + + state *networkState } type BootstrapType uint32 diff --git a/cmd/neofs-node/netmap.go b/cmd/neofs-node/netmap.go index 5f79fdfdb..25036e9ad 100644 --- a/cmd/neofs-node/netmap.go +++ b/cmd/neofs-node/netmap.go @@ -5,11 +5,32 @@ import ( netmapGRPC "github.com/nspcc-dev/neofs-api-go/v2/netmap/grpc" crypto "github.com/nspcc-dev/neofs-crypto" "github.com/nspcc-dev/neofs-node/pkg/morph/event" + netmapEvent "github.com/nspcc-dev/neofs-node/pkg/morph/event/netmap" netmapTransportGRPC "github.com/nspcc-dev/neofs-node/pkg/network/transport/netmap/grpc" netmapService "github.com/nspcc-dev/neofs-node/pkg/services/netmap" "github.com/pkg/errors" + "go.uber.org/atomic" ) +// primary solution of local network state dump. +type networkState struct { + epoch *atomic.Uint64 +} + +func newNetworkState() *networkState { + return &networkState{ + epoch: atomic.NewUint64(0), + } +} + +func (s *networkState) CurrentEpoch() uint64 { + return s.epoch.Load() +} + +func (s *networkState) setCurrentEpoch(v uint64) { + s.epoch.Store(v) +} + func initNetmapService(c *cfg) { peerInfo := new(netmap.NodeInfo) peerInfo.SetAddress(c.localAddr.String()) @@ -29,6 +50,11 @@ func initNetmapService(c *cfg) { ), ), ) + + c.cfgNetmap.state = newNetworkState() + addNewEpochNotificationHandler(c, func(ev event.Event) { + c.cfgNetmap.state.setCurrentEpoch(ev.(netmapEvent.NewEpoch).EpochNumber()) + }) } func bootstrapNode(c *cfg) { diff --git a/cmd/neofs-node/object.go b/cmd/neofs-node/object.go index 88dbdc378..76b4dfa31 100644 --- a/cmd/neofs-node/object.go +++ b/cmd/neofs-node/object.go @@ -233,6 +233,7 @@ func initObjectService(c *cfg) { putsvc.WithFormatValidatorOpts( objectCore.WithDeleteHandler(objGC), ), + putsvc.WithNetworkState(c.cfgNetmap.state), ) sPutV2 := putsvcV2.NewService(