2020-07-10 14:17:51 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-01-12 11:05:08 +00:00
|
|
|
"context"
|
2020-09-16 07:45:08 +00:00
|
|
|
"flag"
|
2021-05-31 05:11:23 +00:00
|
|
|
"fmt"
|
2020-08-21 15:01:59 +00:00
|
|
|
"log"
|
2021-07-06 13:24:18 +00:00
|
|
|
"os"
|
2021-06-28 14:01:31 +00:00
|
|
|
"os/signal"
|
|
|
|
"syscall"
|
2020-08-21 15:01:59 +00:00
|
|
|
|
2022-09-20 13:20:45 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/cmd/neofs-node/config"
|
2021-05-11 09:25:14 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/misc"
|
2021-01-15 10:28:37 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/services/control"
|
2021-01-12 11:05:08 +00:00
|
|
|
"go.uber.org/zap"
|
2020-07-10 14:17:51 +00:00
|
|
|
)
|
|
|
|
|
2021-07-06 13:24:18 +00:00
|
|
|
const (
|
2022-10-17 12:03:55 +00:00
|
|
|
// SuccessReturnCode returns when application closed without panic.
|
2021-07-06 13:24:18 +00:00
|
|
|
SuccessReturnCode = 0
|
|
|
|
)
|
|
|
|
|
2021-05-31 05:11:23 +00:00
|
|
|
// prints err to standard logger and calls os.Exit(1).
|
2020-08-21 15:01:59 +00:00
|
|
|
func fatalOnErr(err error) {
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-31 05:11:23 +00:00
|
|
|
// prints err with details to standard logger and calls os.Exit(1).
|
|
|
|
func fatalOnErrDetails(details string, err error) {
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(fmt.Errorf("%s: %w", details, err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-10 14:17:51 +00:00
|
|
|
func main() {
|
2020-09-16 07:45:08 +00:00
|
|
|
configFile := flag.String("config", "", "path to config")
|
2021-12-03 03:42:35 +00:00
|
|
|
versionFlag := flag.Bool("version", false, "neofs node version")
|
2022-09-20 13:29:09 +00:00
|
|
|
dryRunFlag := flag.Bool("check", false, "validate configuration and exit")
|
2020-09-16 07:45:08 +00:00
|
|
|
flag.Parse()
|
|
|
|
|
2021-07-06 13:24:18 +00:00
|
|
|
if *versionFlag {
|
2022-07-08 12:45:21 +00:00
|
|
|
fmt.Print(misc.BuildInfo("NeoFS Storage node"))
|
2021-07-06 13:24:18 +00:00
|
|
|
|
|
|
|
os.Exit(SuccessReturnCode)
|
|
|
|
}
|
|
|
|
|
2022-09-20 13:20:45 +00:00
|
|
|
appCfg := config.New(config.Prm{}, config.WithConfigFile(*configFile))
|
|
|
|
|
|
|
|
err := validateConfig(appCfg)
|
|
|
|
fatalOnErr(err)
|
|
|
|
|
2022-09-20 13:29:09 +00:00
|
|
|
if *dryRunFlag {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-09-20 13:20:45 +00:00
|
|
|
c := initCfg(appCfg)
|
2020-08-21 15:01:59 +00:00
|
|
|
|
2021-01-11 15:17:06 +00:00
|
|
|
initApp(c)
|
2020-08-24 09:40:32 +00:00
|
|
|
|
2021-01-15 10:28:37 +00:00
|
|
|
c.setHealthStatus(control.HealthStatus_STARTING)
|
|
|
|
|
2020-08-24 09:40:32 +00:00
|
|
|
bootUp(c)
|
|
|
|
|
2021-01-15 10:28:37 +00:00
|
|
|
c.setHealthStatus(control.HealthStatus_READY)
|
|
|
|
|
2020-08-24 09:40:32 +00:00
|
|
|
wait(c)
|
|
|
|
|
2021-01-15 10:28:37 +00:00
|
|
|
c.setHealthStatus(control.HealthStatus_SHUTTING_DOWN)
|
|
|
|
|
2020-08-24 09:40:32 +00:00
|
|
|
shutdown(c)
|
|
|
|
}
|
|
|
|
|
2022-05-25 16:18:36 +00:00
|
|
|
func initAndLog(c *cfg, name string, initializer func(*cfg)) {
|
|
|
|
c.log.Info(fmt.Sprintf("initializing %s service...", name))
|
|
|
|
initializer(c)
|
|
|
|
c.log.Info(fmt.Sprintf("%s service has been successfully initialized", name))
|
|
|
|
}
|
|
|
|
|
2021-01-11 15:17:06 +00:00
|
|
|
func initApp(c *cfg) {
|
2022-09-26 13:26:41 +00:00
|
|
|
initLocalStorage(c)
|
|
|
|
|
2022-09-26 22:02:20 +00:00
|
|
|
c.ctx, c.ctxCancel = signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
|
2020-08-21 15:01:59 +00:00
|
|
|
|
2022-06-29 15:33:02 +00:00
|
|
|
initAndLog(c, "storage engine", func(c *cfg) {
|
|
|
|
fatalOnErr(c.cfgObject.cfgLocalStorage.localStorage.Open())
|
|
|
|
fatalOnErr(c.cfgObject.cfgLocalStorage.localStorage.Init())
|
|
|
|
})
|
|
|
|
|
2022-05-25 16:18:36 +00:00
|
|
|
initAndLog(c, "gRPC", initGRPC)
|
|
|
|
initAndLog(c, "netmap", initNetmapService)
|
|
|
|
initAndLog(c, "accounting", initAccountingService)
|
|
|
|
initAndLog(c, "container", initContainerService)
|
|
|
|
initAndLog(c, "session", initSessionService)
|
|
|
|
initAndLog(c, "reputation", initReputationService)
|
|
|
|
initAndLog(c, "notification", initNotifications)
|
|
|
|
initAndLog(c, "object", initObjectService)
|
2022-07-13 09:26:40 +00:00
|
|
|
initAndLog(c, "pprof", initProfiler)
|
|
|
|
initAndLog(c, "prometheus", initMetrics)
|
2022-04-22 13:30:20 +00:00
|
|
|
initAndLog(c, "tree", initTreeService)
|
2022-05-17 09:09:18 +00:00
|
|
|
initAndLog(c, "control", initControlService)
|
2022-05-25 16:18:36 +00:00
|
|
|
|
|
|
|
initAndLog(c, "morph notifications", listenMorphNotifications)
|
2022-09-26 22:02:20 +00:00
|
|
|
|
|
|
|
c.workers = append(c.workers, newWorkerFromFunc(c.configWatcher))
|
2022-05-25 16:18:36 +00:00
|
|
|
}
|
2020-10-21 09:26:16 +00:00
|
|
|
|
2022-05-25 16:18:36 +00:00
|
|
|
func runAndLog(c *cfg, name string, logSuccess bool, starter func(*cfg)) {
|
|
|
|
c.log.Info(fmt.Sprintf("starting %s service...", name))
|
|
|
|
starter(c)
|
2020-11-30 15:35:37 +00:00
|
|
|
|
2022-05-25 16:18:36 +00:00
|
|
|
if logSuccess {
|
|
|
|
c.log.Info(fmt.Sprintf("%s service started successfully", name))
|
|
|
|
}
|
2020-08-24 09:40:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func bootUp(c *cfg) {
|
2022-05-25 16:18:36 +00:00
|
|
|
runAndLog(c, "NATS", true, connectNats)
|
|
|
|
runAndLog(c, "gRPC", false, serveGRPC)
|
|
|
|
runAndLog(c, "notary", true, makeAndWaitNotaryDeposit)
|
|
|
|
|
2021-09-09 11:57:55 +00:00
|
|
|
bootstrapNode(c)
|
2020-10-03 09:57:02 +00:00
|
|
|
startWorkers(c)
|
2020-08-24 09:40:32 +00:00
|
|
|
}
|
2020-07-10 14:17:51 +00:00
|
|
|
|
2020-08-24 09:40:32 +00:00
|
|
|
func wait(c *cfg) {
|
2021-05-11 09:25:14 +00:00
|
|
|
c.log.Info("application started",
|
2022-07-13 14:45:33 +00:00
|
|
|
zap.String("version", misc.Version))
|
2020-09-25 12:34:17 +00:00
|
|
|
|
2021-01-12 11:05:08 +00:00
|
|
|
select {
|
|
|
|
case <-c.ctx.Done(): // graceful shutdown
|
|
|
|
case err := <-c.internalErr: // internal application error
|
|
|
|
close(c.internalErr)
|
|
|
|
c.ctxCancel()
|
|
|
|
|
|
|
|
c.log.Warn("internal application error",
|
|
|
|
zap.String("message", err.Error()))
|
|
|
|
}
|
2020-08-24 09:40:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func shutdown(c *cfg) {
|
2021-01-18 08:56:14 +00:00
|
|
|
for _, closer := range c.closers {
|
|
|
|
closer()
|
|
|
|
}
|
2020-08-22 11:03:45 +00:00
|
|
|
|
2021-08-04 14:44:37 +00:00
|
|
|
c.log.Debug("waiting for all processes to stop")
|
|
|
|
|
2020-08-22 11:03:45 +00:00
|
|
|
c.wg.Wait()
|
2020-07-10 14:17:51 +00:00
|
|
|
}
|
2021-01-18 08:56:14 +00:00
|
|
|
|
|
|
|
func (c *cfg) onShutdown(f func()) {
|
|
|
|
c.closers = append(c.closers, f)
|
|
|
|
}
|