ir: Change log level on SIGHUP #258
4 changed files with 127 additions and 61 deletions
|
@ -5,6 +5,8 @@ Changelog for FrostFS Node
|
||||||
|
|
||||||
### Added
|
### Added
|
||||||
- Support impersonate bearer token (#229)
|
- Support impersonate bearer token (#229)
|
||||||
|
- Change log level on SIGHUP for ir (#125)
|
||||||
|
|
||||||
### Changed
|
### Changed
|
||||||
### Fixed
|
### Fixed
|
||||||
- Take network settings into account during netmap contract update (#100)
|
- Take network settings into account during netmap contract update (#100)
|
||||||
|
|
73
cmd/frostfs-ir/config.go
Normal file
73
cmd/frostfs-ir/config.go
Normal file
|
@ -0,0 +1,73 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"os/signal"
|
||||||
|
"syscall"
|
||||||
|
|
||||||
|
configViper "git.frostfs.info/TrueCloudLab/frostfs-node/cmd/internal/common/config"
|
||||||
|
"git.frostfs.info/TrueCloudLab/frostfs-node/internal/logs"
|
||||||
|
"github.com/spf13/viper"
|
||||||
|
"go.uber.org/zap"
|
||||||
|
)
|
||||||
|
|
||||||
|
func newConfig() (*viper.Viper, error) {
|
||||||
|
var err error
|
||||||
|
var dv = viper.New()
|
||||||
|
|
||||||
|
defaultConfiguration(dv)
|
||||||
|
|
||||||
|
_, err = configViper.CreateViper(configViper.WithConfigFile(*configFile),
|
||||||
|
configViper.WithConfigDir(*configDir), configViper.WithEnvPrefix(EnvPrefix),
|
||||||
|
configViper.WithViper(dv))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return dv, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func reloadConfig() error {
|
||||||
|
err := configViper.ReloadViper(configViper.WithConfigFile(*configFile),
|
||||||
|
configViper.WithConfigDir(*configDir), configViper.WithEnvPrefix(EnvPrefix),
|
||||||
|
configViper.WithViper(cfg))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
err = logPrm.SetLevelString(cfg.GetString("logger.level"))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return logPrm.Reload()
|
||||||
|
}
|
||||||
|
|
||||||
|
func watchForSignal(cancel func()) {
|
||||||
|
ch := make(chan os.Signal, 1)
|
||||||
|
signal.Notify(ch, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM)
|
||||||
|
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case err := <-intErr:
|
||||||
|
log.Info(logs.FrostFSIRInternalError, zap.String("msg", err.Error()))
|
||||||
|
cancel()
|
||||||
|
shutdown()
|
||||||
|
return
|
||||||
|
case sig := <-ch:
|
||||||
|
switch sig {
|
||||||
|
case syscall.SIGHUP:
|
||||||
|
log.Info(logs.FrostFSNodeSIGHUPHasBeenReceivedRereadingConfiguration)
|
||||||
|
err := reloadConfig()
|
||||||
|
if err != nil {
|
||||||
|
log.Error(logs.FrostFSNodeConfigurationReading, zap.Error(err))
|
||||||
|
}
|
||||||
|
log.Info(logs.FrostFSNodeConfigurationHasBeenReloadedSuccessfully)
|
||||||
|
case syscall.SIGTERM, syscall.SIGINT:
|
||||||
|
log.Info(logs.FrostFSNodeTerminationSignalHasBeenReceivedStopping)
|
||||||
|
cancel()
|
||||||
|
shutdown()
|
||||||
|
log.Info(logs.FrostFSNodeTerminationSignalProcessingIsComplete)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -3,28 +3,9 @@ package main
|
||||||
import (
|
import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
configViper "git.frostfs.info/TrueCloudLab/frostfs-node/cmd/internal/common/config"
|
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
)
|
)
|
||||||
|
|
||||||
func newConfig(path, directory string) (*viper.Viper, error) {
|
|
||||||
const envPrefix = "FROSTFS_IR"
|
|
||||||
|
|
||||||
var err error
|
|
||||||
var dv = viper.New()
|
|
||||||
|
|
||||||
defaultConfiguration(dv)
|
|
||||||
|
|
||||||
_, _, err = configViper.CreateViper(configViper.WithConfigFile(path),
|
|
||||||
configViper.WithConfigDir(directory), configViper.WithEnvPrefix(envPrefix),
|
|
||||||
configViper.WithViper(dv))
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return dv, err
|
|
||||||
}
|
|
||||||
|
|
||||||
func defaultConfiguration(cfg *viper.Viper) {
|
func defaultConfiguration(cfg *viper.Viper) {
|
||||||
cfg.SetDefault("logger.level", "info")
|
cfg.SetDefault("logger.level", "info")
|
||||||
|
|
||||||
|
|
|
@ -6,8 +6,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"sync"
|
||||||
"syscall"
|
|
||||||
|
|
||||||
"git.frostfs.info/TrueCloudLab/frostfs-node/internal/logs"
|
"git.frostfs.info/TrueCloudLab/frostfs-node/internal/logs"
|
||||||
"git.frostfs.info/TrueCloudLab/frostfs-node/misc"
|
"git.frostfs.info/TrueCloudLab/frostfs-node/misc"
|
||||||
|
@ -25,6 +24,20 @@ const (
|
||||||
|
|
||||||
// SuccessReturnCode returns when application closed without panic.
|
// SuccessReturnCode returns when application closed without panic.
|
||||||
SuccessReturnCode = 0
|
SuccessReturnCode = 0
|
||||||
|
|
||||||
|
EnvPrefix = "FROSTFS_IR"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
wg = new(sync.WaitGroup)
|
||||||
|
intErr = make(chan error) // internal inner ring errors
|
||||||
|
logPrm = new(logger.Prm)
|
||||||
|
innerRing *innerring.Server
|
||||||
|
httpServers []*httputil.Server
|
||||||
|
log *logger.Logger
|
||||||
|
cfg *viper.Viper
|
||||||
|
configFile *string
|
||||||
|
configDir *string
|
||||||
)
|
)
|
||||||
|
|
||||||
func exitErr(err error) {
|
func exitErr(err error) {
|
||||||
|
@ -35,8 +48,8 @@ func exitErr(err error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
configFile := flag.String("config", "", "path to config")
|
configFile = flag.String("config", "", "path to config")
|
||||||
configDir := flag.String("config-dir", "", "path to config directory")
|
configDir = flag.String("config-dir", "", "path to config directory")
|
||||||
versionFlag := flag.Bool("version", false, "frostfs-ir node version")
|
versionFlag := flag.Bool("version", false, "frostfs-ir node version")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
|
@ -46,35 +59,32 @@ func main() {
|
||||||
os.Exit(SuccessReturnCode)
|
os.Exit(SuccessReturnCode)
|
||||||
}
|
}
|
||||||
|
|
||||||
cfg, err := newConfig(*configFile, *configDir)
|
var err error
|
||||||
|
cfg, err = newConfig()
|
||||||
exitErr(err)
|
exitErr(err)
|
||||||
|
|
||||||
var logPrm logger.Prm
|
|
||||||
|
|
||||||
err = logPrm.SetLevelString(
|
err = logPrm.SetLevelString(
|
||||||
cfg.GetString("logger.level"),
|
cfg.GetString("logger.level"),
|
||||||
)
|
)
|
||||||
exitErr(err)
|
exitErr(err)
|
||||||
|
|
||||||
log, err := logger.NewLogger(&logPrm)
|
log, err = logger.NewLogger(logPrm)
|
||||||
exitErr(err)
|
exitErr(err)
|
||||||
|
|
||||||
ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP)
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
defer cancel()
|
|
||||||
|
|
||||||
intErr := make(chan error) // internal inner ring errors
|
initHTTPServers(cfg)
|
||||||
|
|
||||||
httpServers := initHTTPServers(cfg, log)
|
innerRing, err = innerring.New(ctx, log, cfg, intErr)
|
||||||
|
|
||||||
innerRing, err := innerring.New(ctx, log, cfg, intErr)
|
|
||||||
exitErr(err)
|
exitErr(err)
|
||||||
|
|
||||||
// start HTTP servers
|
// start HTTP servers
|
||||||
for i := range httpServers {
|
for _, srv := range httpServers {
|
||||||
srv := httpServers[i]
|
wg.Add(1)
|
||||||
go func() {
|
go func(srv *httputil.Server) {
|
||||||
exitErr(srv.Serve())
|
exitErr(srv.Serve())
|
||||||
}()
|
wg.Done()
|
||||||
|
}(srv)
|
||||||
}
|
}
|
||||||
|
|
||||||
// start inner ring
|
// start inner ring
|
||||||
|
@ -84,32 +94,16 @@ func main() {
|
||||||
log.Info(logs.CommonApplicationStarted,
|
log.Info(logs.CommonApplicationStarted,
|
||||||
zap.String("version", misc.Version))
|
zap.String("version", misc.Version))
|
||||||
|
|
||||||
select {
|
watchForSignal(cancel)
|
||||||
case <-ctx.Done():
|
|
||||||
case err := <-intErr:
|
|
||||||
log.Info(logs.FrostFSIRInternalError, zap.String("msg", err.Error()))
|
|
||||||
}
|
|
||||||
|
|
||||||
innerRing.Stop()
|
<-ctx.Done() // graceful shutdown
|
||||||
|
log.Debug(logs.FrostFSNodeWaitingForAllProcessesToStop)
|
||||||
// shut down HTTP servers
|
wg.Wait()
|
||||||
for i := range httpServers {
|
|
||||||
srv := httpServers[i]
|
|
||||||
|
|
||||||
go func() {
|
|
||||||
err := srv.Shutdown()
|
|
||||||
if err != nil {
|
|
||||||
log.Debug(logs.FrostFSIRCouldNotShutdownHTTPServer,
|
|
||||||
zap.String("error", err.Error()),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
}
|
|
||||||
|
|
||||||
log.Info(logs.FrostFSIRApplicationStopped)
|
log.Info(logs.FrostFSIRApplicationStopped)
|
||||||
}
|
}
|
||||||
|
|
||||||
func initHTTPServers(cfg *viper.Viper, log *logger.Logger) []*httputil.Server {
|
func initHTTPServers(cfg *viper.Viper) {
|
||||||
items := []struct {
|
items := []struct {
|
||||||
cfgPrefix string
|
cfgPrefix string
|
||||||
handler func() http.Handler
|
handler func() http.Handler
|
||||||
|
@ -118,7 +112,7 @@ func initHTTPServers(cfg *viper.Viper, log *logger.Logger) []*httputil.Server {
|
||||||
{"prometheus", promhttp.Handler},
|
{"prometheus", promhttp.Handler},
|
||||||
}
|
}
|
||||||
|
|
||||||
httpServers := make([]*httputil.Server, 0, len(items))
|
httpServers = make([]*httputil.Server, 0, len(items))
|
||||||
|
|
||||||
for _, item := range items {
|
for _, item := range items {
|
||||||
if !cfg.GetBool(item.cfgPrefix + ".enabled") {
|
if !cfg.GetBool(item.cfgPrefix + ".enabled") {
|
||||||
|
@ -141,6 +135,22 @@ func initHTTPServers(cfg *viper.Viper, log *logger.Logger) []*httputil.Server {
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return httpServers
|
|
||||||
|
func shutdown() {
|
||||||
|
innerRing.Stop()
|
||||||
|
|
||||||
|
// shut down HTTP servers
|
||||||
|
for _, srv := range httpServers {
|
||||||
|
wg.Add(1)
|
||||||
|
go func(srv *httputil.Server) {
|
||||||
|
err := srv.Shutdown()
|
||||||
|
if err != nil {
|
||||||
|
log.Debug(logs.FrostFSIRCouldNotShutdownHTTPServer,
|
||||||
|
zap.String("error", err.Error()),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
wg.Done()
|
||||||
|
}(srv)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue