[#313] cmd/node: Update netmap status in the right way

Update node network status via update status by getting the latest network
map and finding the node in it. This step is performed at the start of the
application and upon notification of a new epoch.

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
Leonard Lyubich 2021-01-15 13:02:59 +03:00 committed by Alex Vanin
parent 83d4420a30
commit 71fcbd3ed4
2 changed files with 50 additions and 7 deletions

View file

@ -5,7 +5,6 @@ import (
"flag" "flag"
"log" "log"
"github.com/nspcc-dev/neofs-node/pkg/services/control"
"github.com/nspcc-dev/neofs-node/pkg/util/grace" "github.com/nspcc-dev/neofs-node/pkg/util/grace"
"go.uber.org/zap" "go.uber.org/zap"
) )
@ -55,8 +54,6 @@ func bootUp(c *cfg) {
serveGRPC(c) serveGRPC(c)
bootstrapNode(c) bootstrapNode(c)
startWorkers(c) startWorkers(c)
c.setNetmapStatus(control.NetmapStatus_ONLINE)
} }
func wait(c *cfg) { func wait(c *cfg) {

View file

@ -1,6 +1,8 @@
package main package main
import ( import (
"bytes"
"github.com/nspcc-dev/neofs-api-go/pkg/netmap" "github.com/nspcc-dev/neofs-api-go/pkg/netmap"
netmapGRPC "github.com/nspcc-dev/neofs-api-go/v2/netmap/grpc" netmapGRPC "github.com/nspcc-dev/neofs-api-go/v2/netmap/grpc"
crypto "github.com/nspcc-dev/neofs-crypto" crypto "github.com/nspcc-dev/neofs-crypto"
@ -72,6 +74,22 @@ func initNetmapService(c *cfg) {
} }
}) })
} }
addNewEpochNotificationHandler(c, func(ev event.Event) {
e := ev.(netmapEvent.NewEpoch).EpochNumber()
netStatus, err := c.netmapStatus(e)
if err != nil {
c.log.Error("could not update network status on new epoch",
zap.Uint64("epoch", e),
zap.String("error", err.Error()),
)
return
}
c.setNetmapStatus(netStatus)
})
} }
func bootstrapNode(c *cfg) { func bootstrapNode(c *cfg) {
@ -105,20 +123,48 @@ func initState(c *cfg) {
epoch, err := c.cfgNetmap.wrapper.Epoch() epoch, err := c.cfgNetmap.wrapper.Epoch()
fatalOnErr(errors.Wrap(err, "could not initialize current epoch number")) fatalOnErr(errors.Wrap(err, "could not initialize current epoch number"))
c.log.Info("initial epoch number", netStatus, err := c.netmapStatus(epoch)
zap.Uint64("value", epoch), fatalOnErr(errors.Wrap(err, "could not init network status"))
c.setNetmapStatus(netStatus)
c.log.Info("initial network state",
zap.Uint64("epoch", epoch),
zap.Stringer("status", netStatus),
) )
c.cfgNetmap.state.setCurrentEpoch(epoch) c.cfgNetmap.state.setCurrentEpoch(epoch)
} }
func (c *cfg) netmapStatus(epoch uint64) (control.NetmapStatus, error) {
// calculate current network state
nm, err := c.cfgNetmap.wrapper.GetNetMapByEpoch(epoch)
if err != nil {
return control.NetmapStatus_STATUS_UNDEFINED, err
}
if c.inNetmap(nm) {
return control.NetmapStatus_ONLINE, nil
}
return control.NetmapStatus_OFFLINE, nil
}
func (c *cfg) inNetmap(nm *netmap.Netmap) bool {
for _, n := range nm.Nodes {
if bytes.Equal(n.PublicKey(), crypto.MarshalPublicKey(&c.key.PublicKey)) {
return true
}
}
return false
}
func addNewEpochNotificationHandler(c *cfg, h event.Handler) { func addNewEpochNotificationHandler(c *cfg, h event.Handler) {
addNetmapNotificationHandler(c, newEpochNotification, h) addNetmapNotificationHandler(c, newEpochNotification, h)
} }
func goOffline(c *cfg) { func goOffline(c *cfg) {
c.setNetmapStatus(control.NetmapStatus_OFFLINE)
err := c.cfgNetmap.wrapper.UpdatePeerState( err := c.cfgNetmap.wrapper.UpdatePeerState(
crypto.MarshalPublicKey(&c.key.PublicKey), crypto.MarshalPublicKey(&c.key.PublicKey),
netmap.NodeStateOffline, netmap.NodeStateOffline,