From 2d4c0a0f4a727694e9b106521e1965e6e11e9ec7 Mon Sep 17 00:00:00 2001 From: Ekaterina Lebedeva Date: Tue, 5 Dec 2023 17:05:34 +0300 Subject: [PATCH] [#552] Add systemd notifications to ir service Signed-off-by: Ekaterina Lebedeva --- config/example/ir.yaml | 3 +++ pkg/innerring/innerring.go | 14 ++++++++++++++ pkg/innerring/state.go | 20 ++++++++++++++++++++ 3 files changed, 37 insertions(+) diff --git a/config/example/ir.yaml b/config/example/ir.yaml index a01f3d0bb..401328e72 100644 --- a/config/example/ir.yaml +++ b/config/example/ir.yaml @@ -119,3 +119,6 @@ prometheus: enabled: true address: localhost:9090 # Endpoint for application prometheus metrics; disabled by default shutdown_timeout: 30s # Timeout for metrics HTTP server graceful shutdown + +systemdnotify: + enabled: true diff --git a/pkg/innerring/innerring.go b/pkg/innerring/innerring.go index 49b770811..5d7dc5a52 100644 --- a/pkg/innerring/innerring.go +++ b/pkg/innerring/innerring.go @@ -24,6 +24,7 @@ import ( control "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/services/control/ir" "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/util/logger" "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/util/precision" + "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/util/sdnotify" "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/util/state" "github.com/nspcc-dev/neo-go/pkg/core/block" "github.com/nspcc-dev/neo-go/pkg/core/transaction" @@ -73,6 +74,7 @@ type ( predefinedValidators keys.PublicKeys initialEpochTickDelta uint32 withoutMainNet bool + sdNotify bool // runtime processors netmapProcessor *netmap.Processor @@ -336,6 +338,11 @@ func New(ctx context.Context, log *logger.Logger, cfg *viper.Viper, errChan chan irMetrics: metrics, } + server.sdNotify, err = server.initSdNotify(cfg) + if err != nil { + return nil, err + } + server.setHealthStatus(control.HealthStatus_HEALTH_STATUS_UNDEFINED) // parse notary support @@ -404,6 +411,13 @@ func New(ctx context.Context, log *logger.Logger, cfg *viper.Viper, errChan chan return server, nil } +func (s *Server) initSdNotify(cfg *viper.Viper) (bool, error) { + if cfg.GetBool("systemdnotify.enabled") { + return true, sdnotify.InitSocket() + } + return false, nil +} + func createListener(ctx context.Context, cli *client.Client, p *chainParams) (event.Listener, error) { var ( sub subscriber.Subscriber diff --git a/pkg/innerring/state.go b/pkg/innerring/state.go index f5a03fa29..1616dbb9f 100644 --- a/pkg/innerring/state.go +++ b/pkg/innerring/state.go @@ -7,6 +7,7 @@ import ( "git.frostfs.info/TrueCloudLab/frostfs-node/internal/logs" "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/innerring/processors/governance" control "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/services/control/ir" + "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/util/sdnotify" "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/util/state" "github.com/nspcc-dev/neo-go/pkg/util" "github.com/spf13/viper" @@ -154,6 +155,7 @@ func (s *Server) ResetEpochTimer(h uint32) error { func (s *Server) setHealthStatus(hs control.HealthStatus) { s.healthStatus.Store(int32(hs)) + s.notifySystemd(hs) if s.irMetrics != nil { s.irMetrics.SetHealth(int32(hs)) } @@ -173,3 +175,21 @@ func initPersistentStateStorage(cfg *viper.Viper) (*state.PersistentStorage, err return persistStorage, nil } + +func (s *Server) notifySystemd(st control.HealthStatus) { + if !s.sdNotify { + return + } + var err error + switch st { + case control.HealthStatus_READY: + err = sdnotify.FlagAndStatus(sdnotify.ReadyEnabled) + case control.HealthStatus_SHUTTING_DOWN: + err = sdnotify.FlagAndStatus(sdnotify.StoppingEnabled) + default: + err = sdnotify.Status(fmt.Sprintf("%v", st)) + } + if err != nil { + s.log.Error(logs.FailedToReportStatusToSystemd, zap.Error(err)) + } +}