From df3746fa68f934ea7bd533d3a58a27ffe02e52a2 Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Wed, 13 Jan 2021 16:36:41 +0300 Subject: [PATCH] [#306] cmd/node: Switch health status on boot and shutdown Implement HealthChecker on node app structure. Set health status to ONLINE after node boot. Set health status to OFFLINE on shutdown. Signed-off-by: Leonard Lyubich --- cmd/neofs-node/config.go | 5 +++++ cmd/neofs-node/main.go | 3 +++ cmd/neofs-node/netmap.go | 3 +++ cmd/neofs-node/private.go | 9 +++++++++ 4 files changed, 20 insertions(+) diff --git a/cmd/neofs-node/config.go b/cmd/neofs-node/config.go index 27bdef53d..16f8e99e1 100644 --- a/cmd/neofs-node/config.go +++ b/cmd/neofs-node/config.go @@ -28,6 +28,7 @@ import ( nmwrapper "github.com/nspcc-dev/neofs-node/pkg/morph/client/netmap/wrapper" "github.com/nspcc-dev/neofs-node/pkg/morph/event" "github.com/nspcc-dev/neofs-node/pkg/network" + "github.com/nspcc-dev/neofs-node/pkg/services/private" tokenStorage "github.com/nspcc-dev/neofs-node/pkg/services/session/storage" "github.com/nspcc-dev/neofs-node/pkg/services/util/response" "github.com/nspcc-dev/neofs-node/pkg/util/logger" @@ -35,6 +36,7 @@ import ( "github.com/panjf2000/ants/v2" "github.com/pkg/errors" "github.com/spf13/viper" + "go.uber.org/atomic" "go.uber.org/zap" "google.golang.org/grpc" ) @@ -181,6 +183,8 @@ type cfg struct { respSvc *response.Service cfgPrivateService cfgPrivateService + + healthStatus *atomic.Int32 } type cfgGRPC struct { @@ -338,6 +342,7 @@ func initCfg(path string) *cfg { cfgObject: cfgObject{ pool: initObjectPool(viperCfg), }, + healthStatus: atomic.NewInt32(int32(private.HealthStatus_STATUS_UNDEFINED)), } initLocalStorage(c) diff --git a/cmd/neofs-node/main.go b/cmd/neofs-node/main.go index 394ef514b..b75048bbe 100644 --- a/cmd/neofs-node/main.go +++ b/cmd/neofs-node/main.go @@ -5,6 +5,7 @@ import ( "flag" "log" + "github.com/nspcc-dev/neofs-node/pkg/services/private" "github.com/nspcc-dev/neofs-node/pkg/util/grace" "go.uber.org/zap" ) @@ -54,6 +55,8 @@ func bootUp(c *cfg) { serveGRPC(c) bootstrapNode(c) startWorkers(c) + + c.setHealthStatus(private.HealthStatus_ONLINE) } func wait(c *cfg) { diff --git a/cmd/neofs-node/netmap.go b/cmd/neofs-node/netmap.go index 2942dc7c8..592d68918 100644 --- a/cmd/neofs-node/netmap.go +++ b/cmd/neofs-node/netmap.go @@ -8,6 +8,7 @@ import ( 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/nspcc-dev/neofs-node/pkg/services/private" "github.com/pkg/errors" "go.uber.org/atomic" "go.uber.org/zap" @@ -116,6 +117,8 @@ func addNewEpochNotificationHandler(c *cfg, h event.Handler) { } func goOffline(c *cfg) { + c.setHealthStatus(private.HealthStatus_OFFLINE) + err := c.cfgNetmap.wrapper.UpdatePeerState( crypto.MarshalPublicKey(&c.key.PublicKey), netmap.NodeStateOffline, diff --git a/cmd/neofs-node/private.go b/cmd/neofs-node/private.go index 5d6ec82da..4b647f2ce 100644 --- a/cmd/neofs-node/private.go +++ b/cmd/neofs-node/private.go @@ -41,6 +41,7 @@ func initPrivateService(c *cfg) { privSvc := privateSvc.New( privateSvc.WithKey(c.key), privateSvc.WithAllowedKeys(keys), + privateSvc.WithHealthChecker(c), ) var ( @@ -65,3 +66,11 @@ func initPrivateService(c *cfg) { fatalOnErr(c.cfgPrivateService.server.Serve(lis)) })) } + +func (c *cfg) setHealthStatus(st private.HealthStatus) { + c.healthStatus.Store(int32(st)) +} + +func (c *cfg) HealthStatus() private.HealthStatus { + return private.HealthStatus(c.healthStatus.Load()) +}