2019-10-29 14:55:57 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2022-10-03 14:32:14 +01:00
|
|
|
"strings"
|
|
|
|
|
2019-10-29 14:55:57 +00:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/zeebo/errs"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
|
2020-03-20 13:01:12 +00:00
|
|
|
"storj.io/common/context2"
|
2020-03-23 19:18:20 +00:00
|
|
|
"storj.io/private/process"
|
2020-03-23 19:30:31 +00:00
|
|
|
"storj.io/private/version"
|
2021-04-23 14:13:51 +01:00
|
|
|
"storj.io/storj/private/revocation"
|
2019-10-29 14:55:57 +00:00
|
|
|
"storj.io/storj/satellite"
|
2020-12-22 14:56:48 +00:00
|
|
|
"storj.io/storj/satellite/accounting"
|
2019-10-29 14:55:57 +00:00
|
|
|
"storj.io/storj/satellite/accounting/live"
|
2021-05-13 09:14:18 +01:00
|
|
|
"storj.io/storj/satellite/metabase"
|
2020-01-17 22:55:53 +00:00
|
|
|
"storj.io/storj/satellite/orders"
|
2019-10-29 14:55:57 +00:00
|
|
|
"storj.io/storj/satellite/satellitedb"
|
|
|
|
)
|
|
|
|
|
|
|
|
func cmdAPIRun(cmd *cobra.Command, args []string) (err error) {
|
|
|
|
ctx, _ := process.Ctx(cmd)
|
|
|
|
log := zap.L()
|
|
|
|
|
2020-01-28 17:35:45 +00:00
|
|
|
runCfg.Debug.Address = *process.DebugAddrFlag
|
|
|
|
|
2019-10-29 14:55:57 +00:00
|
|
|
identity, err := runCfg.Identity.Load()
|
|
|
|
if err != nil {
|
2020-10-13 14:49:33 +01:00
|
|
|
log.Error("Failed to load identity.", zap.Error(err))
|
|
|
|
return errs.New("Failed to load identity: %+v", err)
|
2019-10-29 14:55:57 +00:00
|
|
|
}
|
|
|
|
|
2020-10-28 13:48:31 +00:00
|
|
|
db, err := satellitedb.Open(ctx, log.Named("db"), runCfg.Database, satellitedb.Options{
|
2020-12-04 10:24:39 +00:00
|
|
|
ApplicationName: "satellite-api",
|
2020-06-03 14:51:02 +01:00
|
|
|
APIKeysLRUOptions: runCfg.APIKeysLRUOptions(),
|
|
|
|
RevocationLRUOptions: runCfg.RevocationLRUOptions(),
|
2020-01-10 01:12:27 +00:00
|
|
|
})
|
2019-10-29 14:55:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return errs.New("Error starting master database on satellite api: %+v", err)
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
err = errs.Combine(err, db.Close())
|
|
|
|
}()
|
|
|
|
|
2022-10-05 14:57:38 +01:00
|
|
|
for _, migration := range strings.Split(runCfg.DatabaseOptions.MigrationUnsafe, ",") {
|
2022-10-03 14:32:14 +01:00
|
|
|
switch migration {
|
|
|
|
case fullMigration:
|
|
|
|
err = db.MigrateToLatest(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
case snapshotMigration:
|
2022-10-05 14:57:38 +01:00
|
|
|
log.Info("MigrationUnsafe using latest snapshot. It's not for production", zap.String("db", "master"))
|
2022-10-03 14:32:14 +01:00
|
|
|
err = db.TestingMigrateToLatest(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
case testDataCreation:
|
|
|
|
err := createTestData(ctx, db)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
case noMigration:
|
|
|
|
// noop
|
|
|
|
default:
|
|
|
|
return errs.New("unsupported migration type: %s, please try one of the: %s", migration, strings.Join(migrationTypes, ","))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-28 15:56:20 +01:00
|
|
|
metabaseDB, err := metabase.Open(ctx, log.Named("metabase"), runCfg.Config.Metainfo.DatabaseURL,
|
|
|
|
runCfg.Config.Metainfo.Metabase("satellite-api"))
|
2020-10-29 16:54:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return errs.New("Error creating metabase connection on satellite api: %+v", err)
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
err = errs.Combine(err, metabaseDB.Close())
|
|
|
|
}()
|
|
|
|
|
2022-10-05 14:57:38 +01:00
|
|
|
for _, migration := range strings.Split(runCfg.DatabaseOptions.MigrationUnsafe, ",") {
|
2022-10-03 14:32:14 +01:00
|
|
|
switch migration {
|
|
|
|
case fullMigration:
|
|
|
|
err = metabaseDB.MigrateToLatest(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
case snapshotMigration:
|
2022-10-05 14:57:38 +01:00
|
|
|
log.Info("MigrationUnsafe using latest snapshot. It's not for production", zap.String("db", "master"))
|
2022-10-03 14:32:14 +01:00
|
|
|
err = metabaseDB.TestMigrateToLatest(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
case noMigration, testDataCreation:
|
|
|
|
// noop
|
|
|
|
default:
|
|
|
|
return errs.New("unsupported migration type: %s, please try one of the: %s", migration, strings.Join(migrationTypes, ","))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-28 14:01:41 +00:00
|
|
|
revocationDB, err := revocation.OpenDBFromCfg(ctx, runCfg.Config.Server.Config)
|
2019-10-29 14:55:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return errs.New("Error creating revocation database on satellite api: %+v", err)
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
err = errs.Combine(err, revocationDB.Close())
|
|
|
|
}()
|
|
|
|
|
2021-03-24 19:22:50 +00:00
|
|
|
accountingCache, err := live.OpenCache(ctx, log.Named("live-accounting"), runCfg.LiveAccounting)
|
2019-10-29 14:55:57 +00:00
|
|
|
if err != nil {
|
2020-12-22 14:56:48 +00:00
|
|
|
if !accounting.ErrSystemOrNetError.Has(err) || accountingCache == nil {
|
|
|
|
return errs.New("Error instantiating live accounting cache: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Warn("Unable to connect to live accounting cache. Verify connection",
|
|
|
|
zap.Error(err),
|
|
|
|
)
|
2019-10-29 14:55:57 +00:00
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
err = errs.Combine(err, accountingCache.Close())
|
|
|
|
}()
|
|
|
|
|
2020-01-17 22:55:53 +00:00
|
|
|
rollupsWriteCache := orders.NewRollupsWriteCache(log.Named("orders-write-cache"), db.Orders(), runCfg.Orders.FlushBatchSize)
|
|
|
|
defer func() {
|
|
|
|
err = errs.Combine(err, rollupsWriteCache.CloseAndFlush(context2.WithoutCancellation(ctx)))
|
|
|
|
}()
|
|
|
|
|
2020-12-22 10:38:32 +00:00
|
|
|
peer, err := satellite.NewAPI(log, identity, db, metabaseDB, revocationDB, accountingCache, rollupsWriteCache, &runCfg.Config, version.Build, process.AtomicLevel(cmd))
|
2019-10-29 14:55:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-02-21 17:41:54 +00:00
|
|
|
_, err = peer.Version.Service.CheckVersion(ctx)
|
2019-10-29 14:55:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-02-18 16:16:51 +00:00
|
|
|
if err := process.InitMetricsWithHostname(ctx, log, nil); err != nil {
|
2020-04-13 10:31:17 +01:00
|
|
|
log.Warn("Failed to initialize telemetry batcher on satellite api", zap.Error(err))
|
2019-10-29 14:55:57 +00:00
|
|
|
}
|
|
|
|
|
2020-12-22 10:38:32 +00:00
|
|
|
err = metabaseDB.CheckVersion(ctx)
|
2020-10-29 16:54:35 +00:00
|
|
|
if err != nil {
|
2020-12-22 10:38:32 +00:00
|
|
|
log.Error("Failed metabase database version check.", zap.Error(err))
|
|
|
|
return errs.New("failed metabase version check: %+v", err)
|
2020-10-29 16:54:35 +00:00
|
|
|
}
|
|
|
|
|
2020-01-13 13:44:55 +00:00
|
|
|
err = db.CheckVersion(ctx)
|
2019-10-30 19:23:09 +00:00
|
|
|
if err != nil {
|
2020-10-13 14:49:33 +01:00
|
|
|
log.Error("Failed satellite database version check.", zap.Error(err))
|
2019-11-02 20:09:07 +00:00
|
|
|
return errs.New("Error checking version for satellitedb: %+v", err)
|
2019-10-30 19:23:09 +00:00
|
|
|
}
|
|
|
|
|
2019-10-29 14:55:57 +00:00
|
|
|
runError := peer.Run(ctx)
|
|
|
|
closeError := peer.Close()
|
|
|
|
return errs.Combine(runError, closeError)
|
|
|
|
}
|