Skip to content

Commit

Permalink
[nspcc-dev#1770] node: Support configuration reread on SIGHUP
Browse files Browse the repository at this point in the history
Signed-off-by: Pavel Karpy <carpawell@nspcc.ru>
  • Loading branch information
carpawell authored and aprasolova committed Oct 19, 2022
1 parent c52d49d commit 4e3bbbe
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 8 deletions.
59 changes: 52 additions & 7 deletions cmd/neofs-node/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ import (
"fmt"
"io/fs"
"net"
"os"
"os/signal"
"sync"
atomicstd "sync/atomic"
"syscall"
"time"

"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
Expand Down Expand Up @@ -562,8 +565,13 @@ func (c *cfg) engineOpts() []engine.Option {
return opts
}

func (c *cfg) shardOpts() [][]shard.Option {
oo := make([][]shard.Option, 0, len(c.EngineCfg.shards))
type shardOptsWithMetaPath struct {
metaPath string
shOpts []shard.Option
}

func (c *cfg) shardOpts() []shardOptsWithMetaPath {
shards := make([]shardOptsWithMetaPath, 0, len(c.EngineCfg.shards))

for _, shCfg := range c.EngineCfg.shards {
var writeCacheOpts []writecache.Option
Expand Down Expand Up @@ -626,7 +634,9 @@ func (c *cfg) shardOpts() [][]shard.Option {
}
}

oo = append(oo, []shard.Option{
var sh shardOptsWithMetaPath
sh.metaPath = shCfg.metaCfg.path
sh.shOpts = []shard.Option{
shard.WithLogger(c.log),
shard.WithRefillMetabase(shCfg.refillMetabase),
shard.WithMode(shCfg.mode),
Expand Down Expand Up @@ -660,10 +670,12 @@ func (c *cfg) shardOpts() [][]shard.Option {

return pool
}),
})
}

shards = append(shards, sh)
}

return oo
return shards
}

func (c *cfg) LocalAddress() network.AddressGroup {
Expand All @@ -690,8 +702,8 @@ func initLocalStorage(c *cfg) {
tombstone.WithTombstoneSource(tombstoneSrc),
)

for _, opts := range c.shardOpts() {
id, err := ls.AddShard(append(opts, shard.WithTombstoneSource(tombstoneSource))...)
for _, optsWithMeta := range c.shardOpts() {
id, err := ls.AddShard(append(optsWithMeta.shOpts, shard.WithTombstoneSource(tombstoneSource))...)
fatalOnErr(err)

c.log.Info("shard attached to engine",
Expand Down Expand Up @@ -773,3 +785,36 @@ func (c *cfg) needBootstrap() bool {
func (c *cfg) ObjectServiceLoad() float64 {
return float64(c.cfgObject.pool.putRemote.Running()) / float64(c.cfgObject.pool.putRemoteCapacity)
}

func (c *cfg) configWatcher(ctx context.Context) {
ch := make(chan os.Signal, 1)
signal.Notify(ch, syscall.SIGHUP)

for {
select {
case <-ch:
c.log.Info("SIGHUP has been received, rereading configuration...")

err := c.readConfig(c.appCfg)
if err != nil {
c.log.Error("configuration reading", zap.Error(err))
continue
}

var rcfg engine.ReConfiguration
for _, optsWithMeta := range c.shardOpts() {
rcfg.AddShard(optsWithMeta.metaPath, optsWithMeta.shOpts)
}

err = c.cfgObject.cfgLocalStorage.localStorage.Reload(rcfg)
if err != nil {
c.log.Error("storage engine configuration update", zap.Error(err))
continue
}

c.log.Info("configuration has been reloaded successfully")
case <-ctx.Done():
return
}
}
}
4 changes: 3 additions & 1 deletion cmd/neofs-node/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func initAndLog(c *cfg, name string, initializer func(*cfg)) {
func initApp(c *cfg) {
initLocalStorage(c)

c.ctx, c.ctxCancel = signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP)
c.ctx, c.ctxCancel = signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)

initAndLog(c, "storage engine", func(c *cfg) {
fatalOnErr(c.cfgObject.cfgLocalStorage.localStorage.Open())
Expand All @@ -102,6 +102,8 @@ func initApp(c *cfg) {
initAndLog(c, "control", initControlService)

initAndLog(c, "morph notifications", listenMorphNotifications)

c.workers = append(c.workers, newWorkerFromFunc(c.configWatcher))
}

func runAndLog(c *cfg, name string, logSuccess bool, starter func(*cfg)) {
Expand Down

0 comments on commit 4e3bbbe

Please sign in to comment.