Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

metrics, pprof: support reloading services with SIGHUP #3016

Merged
merged 4 commits into from
Nov 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ attribute, which is used for container domain name in NNS contracts (#2954)
- New `peapod-to-fstree` tool providing peapod-to-fstree data migration (#3013)
- Reloading node attributes with SIGHUP (#3005)
- Reloading pool sizes (#3018)
- Reloading pprof/metrics services with SIGHUP (#3016)

### Fixed
- Do not search for tombstones when handling their expiration, use local indexes instead (#2929)
Expand Down
52 changes: 46 additions & 6 deletions cmd/neofs-node/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
shardconfig "github.com/nspcc-dev/neofs-node/cmd/neofs-node/config/engine/shard"
fstreeconfig "github.com/nspcc-dev/neofs-node/cmd/neofs-node/config/engine/shard/blobstor/fstree"
loggerconfig "github.com/nspcc-dev/neofs-node/cmd/neofs-node/config/logger"
metricsconfig "github.com/nspcc-dev/neofs-node/cmd/neofs-node/config/metrics"
morphconfig "github.com/nspcc-dev/neofs-node/cmd/neofs-node/config/morph"
nodeconfig "github.com/nspcc-dev/neofs-node/cmd/neofs-node/config/node"
objectconfig "github.com/nspcc-dev/neofs-node/cmd/neofs-node/config/object"
Expand Down Expand Up @@ -73,6 +72,11 @@
// for each contract listener.
const notificationHandlerPoolSize = 10

const (
metricName = "prometheus"
profilerName = "pprof"
)

// applicationConfiguration reads and stores component-specific configuration
// values. It should not store any application helpers structs (pointers to shared
// structs).
Expand Down Expand Up @@ -291,7 +295,8 @@
closers []func()
// services that are useful for debug (e.g. when a regular closer does not
// close), must be close at the very end of application life cycle
veryLastClosers []func()
veryLastClosersLock sync.RWMutex
veryLastClosers map[string]func()

apiVersion version.Version
healthStatus atomic.Int32
Expand Down Expand Up @@ -639,10 +644,10 @@

c.ownerIDFromKey = user.NewFromECDSAPublicKey(key.PrivateKey.PublicKey)

if metricsconfig.Enabled(c.cfgReader) {
c.metricsCollector = metrics.NewNodeMetrics(misc.Version)
c.basics.networkState.metrics = c.metricsCollector
}
c.metricsCollector = metrics.NewNodeMetrics(misc.Version)
c.basics.networkState.metrics = c.metricsCollector

c.veryLastClosers = make(map[string]func())

Check warning on line 650 in cmd/neofs-node/config.go

View check run for this annotation

Codecov / codecov/patch

cmd/neofs-node/config.go#L647-L650

Added lines #L647 - L650 were not covered by tests

c.onShutdown(c.clientCache.CloseAll) // clean up connections
c.onShutdown(c.bgClientCache.CloseAll) // clean up connections
Expand Down Expand Up @@ -867,6 +872,9 @@
case <-ch:
c.log.Info("SIGHUP has been received, rereading configuration...")

oldMetrics := writeMetricConfig(c.cfgReader)
oldProfiler := writeProfilerConfig(c.cfgReader)

Check warning on line 877 in cmd/neofs-node/config.go

View check run for this annotation

Codecov / codecov/patch

cmd/neofs-node/config.go#L875-L877

Added lines #L875 - L877 were not covered by tests
err := c.readConfig(c.cfgReader)
if err != nil {
c.log.Error("configuration reading", zap.Error(err))
Expand All @@ -877,6 +885,11 @@

c.reloadObjectPoolSizes()

// Prometheus and pprof

// nolint:contextcheck
c.reloadMetricsAndPprof(oldMetrics, oldProfiler)

Check warning on line 892 in cmd/neofs-node/config.go

View check run for this annotation

Codecov / codecov/patch

cmd/neofs-node/config.go#L888-L892

Added lines #L888 - L892 were not covered by tests
// Logger

err = c.internals.logLevel.UnmarshalText([]byte(c.logger.level))
Expand Down Expand Up @@ -961,3 +974,30 @@

return nil
}

func (c *cfg) reloadMetricsAndPprof(oldMetrics metricConfig, oldProfiler profilerConfig) {
c.veryLastClosersLock.Lock()
defer c.veryLastClosersLock.Unlock()

// Metrics

if oldMetrics.isUpdated(c.cfgReader) {
if closer, ok := c.veryLastClosers[metricName]; ok {
roman-khimov marked this conversation as resolved.
Show resolved Hide resolved
closer()
}
delete(c.veryLastClosers, metricName)

preRunAndLog(c, metricName, initMetrics(c))

Check warning on line 990 in cmd/neofs-node/config.go

View check run for this annotation

Codecov / codecov/patch

cmd/neofs-node/config.go#L978-L990

Added lines #L978 - L990 were not covered by tests
}

//Profiler

if oldProfiler.isUpdated(c.cfgReader) {
if closer, ok := c.veryLastClosers[profilerName]; ok {
closer()
}
delete(c.veryLastClosers, profilerName)

preRunAndLog(c, profilerName, initProfiler(c))

Check warning on line 1001 in cmd/neofs-node/config.go

View check run for this annotation

Codecov / codecov/patch

cmd/neofs-node/config.go#L995-L1001

Added lines #L995 - L1001 were not covered by tests
}
}
4 changes: 1 addition & 3 deletions cmd/neofs-node/control.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,7 @@
func (c *cfg) setHealthStatus(st control.HealthStatus) {
c.healthStatus.Store(int32(st))

if c.metricsCollector != nil {
c.metricsCollector.SetHealth(int32(st))
}
c.metricsCollector.SetHealth(int32(st))

Check warning on line 70 in cmd/neofs-node/control.go

View check run for this annotation

Codecov / codecov/patch

cmd/neofs-node/control.go#L70

Added line #L70 was not covered by tests
}

func (c *cfg) HealthStatus() control.HealthStatus {
Expand Down
13 changes: 8 additions & 5 deletions cmd/neofs-node/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@

c := initCfg(appCfg)

preRunAndLog(c, "prometheus", initMetrics(c))
preRunAndLog(c, metricName, initMetrics(c))

Check warning on line 58 in cmd/neofs-node/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/neofs-node/main.go#L58

Added line #L58 was not covered by tests

preRunAndLog(c, "pprof", initProfiler(c))
preRunAndLog(c, profilerName, initProfiler(c))

Check warning on line 60 in cmd/neofs-node/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/neofs-node/main.go#L60

Added line #L60 was not covered by tests

initApp(c)

Expand Down Expand Up @@ -91,13 +91,13 @@
c.log.Info(fmt.Sprintf("%s service is initialized", name))
c.wg.Add(1)
go func() {
runAndLog(c, name, true, func(c *cfg) {
runAndLog(c, name, false, func(c *cfg) {

Check warning on line 94 in cmd/neofs-node/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/neofs-node/main.go#L94

Added line #L94 was not covered by tests
fatalOnErr(srv.Serve(ln))
c.wg.Done()
})
}()

c.veryLastClosers = append(c.veryLastClosers, func() {
c.veryLastClosers[name] = func() {

Check warning on line 100 in cmd/neofs-node/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/neofs-node/main.go#L100

Added line #L100 was not covered by tests
c.log.Debug(fmt.Sprintf("shutting down %s service", name))

err := srv.Shutdown()
Expand All @@ -108,7 +108,7 @@
}

c.log.Debug(fmt.Sprintf("%s service has been stopped", name))
})
}
}

func initAndLog(c *cfg, name string, initializer func(*cfg)) {
Expand Down Expand Up @@ -184,9 +184,12 @@
for _, closer := range c.closers {
closer()
}

c.veryLastClosersLock.RLock()

Check warning on line 188 in cmd/neofs-node/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/neofs-node/main.go#L188

Added line #L188 was not covered by tests
for _, lastCloser := range c.veryLastClosers {
lastCloser()
}
c.veryLastClosersLock.RUnlock()

Check warning on line 192 in cmd/neofs-node/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/neofs-node/main.go#L192

Added line #L192 was not covered by tests

c.log.Debug("waiting for all processes to stop")

Expand Down
23 changes: 23 additions & 0 deletions cmd/neofs-node/metrics.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package main

import (
"time"

"github.com/nspcc-dev/neofs-node/cmd/neofs-node/config"
metricsconfig "github.com/nspcc-dev/neofs-node/cmd/neofs-node/config/metrics"
httputil "github.com/nspcc-dev/neofs-node/pkg/util/http"
"github.com/prometheus/client_golang/prometheus/promhttp"
Expand All @@ -25,3 +28,23 @@

return srv
}

type metricConfig struct {
enabled bool
shutdownTimeout time.Duration
address string
}

func writeMetricConfig(c *config.Config) metricConfig {
return metricConfig{
enabled: metricsconfig.Enabled(c),
shutdownTimeout: metricsconfig.ShutdownTimeout(c),
address: metricsconfig.Address(c),
}

Check warning on line 43 in cmd/neofs-node/metrics.go

View check run for this annotation

Codecov / codecov/patch

cmd/neofs-node/metrics.go#L38-L43

Added lines #L38 - L43 were not covered by tests
}

func (m1 metricConfig) isUpdated(c *config.Config) bool {
return m1.enabled != metricsconfig.Enabled(c) ||
m1.shutdownTimeout != metricsconfig.ShutdownTimeout(c) ||
m1.address != metricsconfig.Address(c)

Check warning on line 49 in cmd/neofs-node/metrics.go

View check run for this annotation

Codecov / codecov/patch

cmd/neofs-node/metrics.go#L46-L49

Added lines #L46 - L49 were not covered by tests
}
5 changes: 2 additions & 3 deletions cmd/neofs-node/netmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,8 @@

func (s *networkState) setCurrentEpoch(v uint64) {
s.epoch.Store(v)
if s.metrics != nil {
s.metrics.SetEpoch(v)
}

s.metrics.SetEpoch(v)

Check warning on line 52 in cmd/neofs-node/netmap.go

View check run for this annotation

Codecov / codecov/patch

cmd/neofs-node/netmap.go#L51-L52

Added lines #L51 - L52 were not covered by tests
}

func (s *networkState) setNodeInfo(ni *netmapSDK.NodeInfo) {
Expand Down
5 changes: 1 addition & 4 deletions cmd/neofs-node/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,10 +347,7 @@
respSvc,
)

var firstSvc objectService.ServiceServer = signSvc
if c.metricsCollector != nil {
firstSvc = objectService.NewMetricCollector(signSvc, c.metricsCollector)
}
firstSvc := objectService.NewMetricCollector(signSvc, c.metricsCollector)

Check warning on line 350 in cmd/neofs-node/object.go

View check run for this annotation

Codecov / codecov/patch

cmd/neofs-node/object.go#L350

Added line #L350 was not covered by tests

server := objectTransportGRPC.New(firstSvc, mNumber, objNode, neofsecdsa.SignerRFC6979(c.shared.basics.key.PrivateKey))

Expand Down
23 changes: 23 additions & 0 deletions cmd/neofs-node/pprof.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package main

import (
"time"

"github.com/nspcc-dev/neofs-node/cmd/neofs-node/config"
profilerconfig "github.com/nspcc-dev/neofs-node/cmd/neofs-node/config/profiler"
httputil "github.com/nspcc-dev/neofs-node/pkg/util/http"
)
Expand All @@ -24,3 +27,23 @@

return srv
}

type profilerConfig struct {
enabled bool
shutdownTimeout time.Duration
address string
}

func writeProfilerConfig(c *config.Config) profilerConfig {
return profilerConfig{
enabled: profilerconfig.Enabled(c),
shutdownTimeout: profilerconfig.ShutdownTimeout(c),
address: profilerconfig.Address(c),
}

Check warning on line 42 in cmd/neofs-node/pprof.go

View check run for this annotation

Codecov / codecov/patch

cmd/neofs-node/pprof.go#L37-L42

Added lines #L37 - L42 were not covered by tests
}

func (m1 profilerConfig) isUpdated(c *config.Config) bool {
return m1.enabled != profilerconfig.Enabled(c) ||
m1.shutdownTimeout != profilerconfig.ShutdownTimeout(c) ||
m1.address != profilerconfig.Address(c)

Check warning on line 48 in cmd/neofs-node/pprof.go

View check run for this annotation

Codecov / codecov/patch

cmd/neofs-node/pprof.go#L45-L48

Added lines #L45 - L48 were not covered by tests
}
4 changes: 1 addition & 3 deletions cmd/neofs-node/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,7 @@
opts = append(opts, engine.WithContainersSource(cntClient.AsContainerSource(c.shared.basics.cCli)))
}

if c.metricsCollector != nil {
opts = append(opts, engine.WithMetrics(c.metricsCollector))
}
opts = append(opts, engine.WithMetrics(c.metricsCollector))

Check warning on line 96 in cmd/neofs-node/storage.go

View check run for this annotation

Codecov / codecov/patch

cmd/neofs-node/storage.go#L96

Added line #L96 was not covered by tests

return opts
}
Expand Down
Loading