diff --git a/services/horizon/cmd/db.go b/services/horizon/cmd/db.go index 0ecdf6a420..5b90289693 100644 --- a/services/horizon/cmd/db.go +++ b/services/horizon/cmd/db.go @@ -204,26 +204,30 @@ var dbReingestRangeCmd = &cobra.Command{ initRootConfig() - coreSession, err := db.Open("postgres", config.StellarCoreDatabaseURL) - if err != nil { - log.Fatalf("cannot open Core DB: %v", err) - } - horizonSession, err := db.Open("postgres", config.DatabaseURL) if err != nil { log.Fatalf("cannot open Horizon DB: %v", err) } ingestConfig := expingest.Config{ - CoreSession: coreSession, NetworkPassphrase: config.NetworkPassphrase, HistorySession: horizonSession, HistoryArchiveURL: config.HistoryArchiveURLs[0], MaxReingestRetries: int(retries), ReingesRetryBackoffSeconds: int(retryBackoffSeconds), } + if config.EnableCaptiveCoreIngestion { ingestConfig.StellarCorePath = config.StellarCoreBinaryPath + } else { + if config.StellarCoreDatabaseURL == "" { + log.Fatalf("flag --%s cannot be empty", stellarCoreDBURLFlagName) + } + coreSession, dbErr := db.Open("postgres", config.StellarCoreDatabaseURL) + if dbErr != nil { + log.Fatalf("cannot open Core DB: %v", dbErr) + } + ingestConfig.CoreSession = coreSession } if parallelWorkers < 2 { diff --git a/services/horizon/cmd/ingest.go b/services/horizon/cmd/ingest.go index efcbc0eabd..e6e6c860bb 100644 --- a/services/horizon/cmd/ingest.go +++ b/services/horizon/cmd/ingest.go @@ -24,7 +24,7 @@ var ingestVerifyFrom, ingestVerifyTo, ingestVerifyDebugServerPort uint32 var ingestVerifyState bool var ingestVerifyRangeCmdOpts = []*support.ConfigOption{ - &support.ConfigOption{ + { Name: "from", ConfigKey: &ingestVerifyFrom, OptType: types.Uint32, @@ -32,7 +32,7 @@ var ingestVerifyRangeCmdOpts = []*support.ConfigOption{ FlagDefault: uint32(0), Usage: "first ledger of the range to ingest", }, - &support.ConfigOption{ + { Name: "to", ConfigKey: &ingestVerifyTo, OptType: types.Uint32, @@ -40,7 +40,7 @@ var ingestVerifyRangeCmdOpts = []*support.ConfigOption{ FlagDefault: uint32(0), Usage: "last ledger of the range to ingest", }, - &support.ConfigOption{ + { Name: "verify-state", ConfigKey: &ingestVerifyState, OptType: types.Bool, @@ -48,7 +48,7 @@ var ingestVerifyRangeCmdOpts = []*support.ConfigOption{ FlagDefault: false, Usage: "[optional] verifies state at the last ledger of the range when true", }, - &support.ConfigOption{ + { Name: "debug-server-port", ConfigKey: &ingestVerifyDebugServerPort, OptType: types.Uint32, @@ -83,11 +83,6 @@ var ingestVerifyRangeCmd = &cobra.Command{ }() } - coreSession, err := db.Open("postgres", config.StellarCoreDatabaseURL) - if err != nil { - log.Fatalf("cannot open Core DB: %v", err) - } - horizonSession, err := db.Open("postgres", config.DatabaseURL) if err != nil { log.Fatalf("cannot open Horizon DB: %v", err) @@ -102,13 +97,22 @@ var ingestVerifyRangeCmd = &cobra.Command{ } ingestConfig := expingest.Config{ - CoreSession: coreSession, NetworkPassphrase: config.NetworkPassphrase, HistorySession: horizonSession, HistoryArchiveURL: config.HistoryArchiveURLs[0], } if config.EnableCaptiveCoreIngestion { ingestConfig.StellarCorePath = config.StellarCoreBinaryPath + } else { + if config.StellarCoreDatabaseURL == "" { + log.Fatalf("flag --%s cannot be empty", stellarCoreDBURLFlagName) + } + + coreSession, dbErr := db.Open("postgres", config.StellarCoreDatabaseURL) + if dbErr != nil { + log.Fatalf("cannot open Core DB: %v", dbErr) + } + ingestConfig.CoreSession = coreSession } system, err := expingest.NewSystem(ingestConfig) @@ -132,7 +136,7 @@ var ingestVerifyRangeCmd = &cobra.Command{ var stressTestNumTransactions, stressTestChangesPerTransaction int var stressTestCmdOpts = []*support.ConfigOption{ - &support.ConfigOption{ + { Name: "transactions", ConfigKey: &stressTestNumTransactions, OptType: types.Int, @@ -140,7 +144,7 @@ var stressTestCmdOpts = []*support.ConfigOption{ FlagDefault: int(1000), Usage: "total number of transactions to ingest (at most 1000)", }, - &support.ConfigOption{ + { Name: "changes", ConfigKey: &stressTestChangesPerTransaction, OptType: types.Int, @@ -162,11 +166,6 @@ var ingestStressTestCmd = &cobra.Command{ initRootConfig() - coreSession, err := db.Open("postgres", config.StellarCoreDatabaseURL) - if err != nil { - log.Fatalf("cannot open Core DB: %v", err) - } - horizonSession, err := db.Open("postgres", config.DatabaseURL) if err != nil { log.Fatalf("cannot open Horizon DB: %v", err) @@ -181,13 +180,23 @@ var ingestStressTestCmd = &cobra.Command{ } ingestConfig := expingest.Config{ - CoreSession: coreSession, NetworkPassphrase: config.NetworkPassphrase, HistorySession: horizonSession, HistoryArchiveURL: config.HistoryArchiveURLs[0], } + if config.EnableCaptiveCoreIngestion { ingestConfig.StellarCorePath = config.StellarCoreBinaryPath + } else { + if config.StellarCoreDatabaseURL == "" { + log.Fatalf("flag --%s cannot be empty", stellarCoreDBURLFlagName) + } + + coreSession, dbErr := db.Open("postgres", config.StellarCoreDatabaseURL) + if dbErr != nil { + log.Fatalf("cannot open Core DB: %v", dbErr) + } + ingestConfig.CoreSession = coreSession } system, err := expingest.NewSystem(ingestConfig) diff --git a/services/horizon/cmd/root.go b/services/horizon/cmd/root.go index 8ed3975f6c..2312150e3b 100644 --- a/services/horizon/cmd/root.go +++ b/services/horizon/cmd/root.go @@ -20,6 +20,12 @@ import ( "github.com/stellar/throttled" ) +const ( + maxDBPingAttempts = 30 + stellarCoreDBURLFlagName = "stellar-core-db-url" + stellarCoreURLFlagName = "stellar-core-url" +) + var ( config horizon.Config @@ -33,8 +39,6 @@ var ( } ) -const maxDBPingAttempts = 30 - // validateBothOrNeither ensures that both options are provided, if either is provided. func validateBothOrNeither(option1, option2 string) { arg1, arg2 := viper.GetString(option1), viper.GetString(option2) @@ -127,18 +131,16 @@ var configOpts = support.ConfigOptions{ ConfigKey: &config.EnableCaptiveCoreIngestion, }, &support.ConfigOption{ - Name: "stellar-core-db-url", + Name: stellarCoreDBURLFlagName, EnvVar: "STELLAR_CORE_DATABASE_URL", ConfigKey: &config.StellarCoreDatabaseURL, OptType: types.String, - Required: true, Usage: "stellar-core postgres database to connect with", }, &support.ConfigOption{ - Name: "stellar-core-url", + Name: stellarCoreURLFlagName, ConfigKey: &config.StellarCoreURL, OptType: types.String, - Required: true, Usage: "stellar-core to connect with (for http commands)", }, &support.ConfigOption{ @@ -370,6 +372,15 @@ func init() { func initApp() *horizon.App { initRootConfig() + // Validate app-specific arguments + if config.StellarCoreURL == "" { + log.Fatalf("flag --%s cannot be empty", stellarCoreURLFlagName) + } + if config.Ingest { + if config.StellarCoreDatabaseURL == "" { + log.Fatalf("flag --%s cannot be empty", stellarCoreDBURLFlagName) + } + } return horizon.NewApp(config) } diff --git a/services/horizon/internal/expingest/main.go b/services/horizon/internal/expingest/main.go index b673d41c47..b7fedeb544 100644 --- a/services/horizon/internal/expingest/main.go +++ b/services/horizon/internal/expingest/main.go @@ -149,9 +149,6 @@ func NewSystem(config Config) (System, error) { return nil, errors.Wrap(err, "error creating history archive") } - coreSession := config.CoreSession.Clone() - coreSession.Ctx = ctx - var ledgerBackend ledgerbackend.LedgerBackend if len(config.StellarCorePath) > 0 { ledgerBackend = ledgerbackend.NewCaptive( @@ -160,6 +157,8 @@ func NewSystem(config Config) (System, error) { []string{config.HistoryArchiveURL}, ) } else { + coreSession := config.CoreSession.Clone() + coreSession.Ctx = ctx ledgerBackend, err = ledgerbackend.NewDatabaseBackendFromSession(coreSession, config.NetworkPassphrase) if err != nil { cancel()