[#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 <leonard@nspcc.ru>
This commit is contained in:
Leonard Lyubich 2021-01-13 16:36:41 +03:00 committed by Alex Vanin
parent 44a0fb5a69
commit df3746fa68
4 changed files with 20 additions and 0 deletions

View file

@ -28,6 +28,7 @@ import (
nmwrapper "github.com/nspcc-dev/neofs-node/pkg/morph/client/netmap/wrapper" 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/morph/event"
"github.com/nspcc-dev/neofs-node/pkg/network" "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" 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/services/util/response"
"github.com/nspcc-dev/neofs-node/pkg/util/logger" "github.com/nspcc-dev/neofs-node/pkg/util/logger"
@ -35,6 +36,7 @@ import (
"github.com/panjf2000/ants/v2" "github.com/panjf2000/ants/v2"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/spf13/viper" "github.com/spf13/viper"
"go.uber.org/atomic"
"go.uber.org/zap" "go.uber.org/zap"
"google.golang.org/grpc" "google.golang.org/grpc"
) )
@ -181,6 +183,8 @@ type cfg struct {
respSvc *response.Service respSvc *response.Service
cfgPrivateService cfgPrivateService cfgPrivateService cfgPrivateService
healthStatus *atomic.Int32
} }
type cfgGRPC struct { type cfgGRPC struct {
@ -338,6 +342,7 @@ func initCfg(path string) *cfg {
cfgObject: cfgObject{ cfgObject: cfgObject{
pool: initObjectPool(viperCfg), pool: initObjectPool(viperCfg),
}, },
healthStatus: atomic.NewInt32(int32(private.HealthStatus_STATUS_UNDEFINED)),
} }
initLocalStorage(c) initLocalStorage(c)

View file

@ -5,6 +5,7 @@ import (
"flag" "flag"
"log" "log"
"github.com/nspcc-dev/neofs-node/pkg/services/private"
"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"
) )
@ -54,6 +55,8 @@ func bootUp(c *cfg) {
serveGRPC(c) serveGRPC(c)
bootstrapNode(c) bootstrapNode(c)
startWorkers(c) startWorkers(c)
c.setHealthStatus(private.HealthStatus_ONLINE)
} }
func wait(c *cfg) { func wait(c *cfg) {

View file

@ -8,6 +8,7 @@ import (
netmapEvent "github.com/nspcc-dev/neofs-node/pkg/morph/event/netmap" netmapEvent "github.com/nspcc-dev/neofs-node/pkg/morph/event/netmap"
netmapTransportGRPC "github.com/nspcc-dev/neofs-node/pkg/network/transport/netmap/grpc" netmapTransportGRPC "github.com/nspcc-dev/neofs-node/pkg/network/transport/netmap/grpc"
netmapService "github.com/nspcc-dev/neofs-node/pkg/services/netmap" netmapService "github.com/nspcc-dev/neofs-node/pkg/services/netmap"
"github.com/nspcc-dev/neofs-node/pkg/services/private"
"github.com/pkg/errors" "github.com/pkg/errors"
"go.uber.org/atomic" "go.uber.org/atomic"
"go.uber.org/zap" "go.uber.org/zap"
@ -116,6 +117,8 @@ func addNewEpochNotificationHandler(c *cfg, h event.Handler) {
} }
func goOffline(c *cfg) { func goOffline(c *cfg) {
c.setHealthStatus(private.HealthStatus_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,

View file

@ -41,6 +41,7 @@ func initPrivateService(c *cfg) {
privSvc := privateSvc.New( privSvc := privateSvc.New(
privateSvc.WithKey(c.key), privateSvc.WithKey(c.key),
privateSvc.WithAllowedKeys(keys), privateSvc.WithAllowedKeys(keys),
privateSvc.WithHealthChecker(c),
) )
var ( var (
@ -65,3 +66,11 @@ func initPrivateService(c *cfg) {
fatalOnErr(c.cfgPrivateService.server.Serve(lis)) 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())
}