Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

services/horizon/internal: Create separate db connections for ingestion #2560

Merged
merged 3 commits into from
May 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions services/horizon/internal/expingest/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ const (
// everything else).
CurrentVersion = 10

// MaxDBConnections is the size of the postgres connection pool dedicated to Horizon ingestion
MaxDBConnections = 2

defaultCoreCursorName = "HORIZON"
stateVerificationErrorThreshold = 3
)
Expand Down
61 changes: 48 additions & 13 deletions services/horizon/internal/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,33 +19,68 @@ import (
"github.com/stellar/go/support/log"
)

func mustInitHorizonDB(app *App) {
session, err := db.Open("postgres", app.config.DatabaseURL)
func mustNewDBSession(databaseURL string, maxIdle, maxOpen int) *db.Session {
session, err := db.Open("postgres", databaseURL)
if err != nil {
log.Fatalf("cannot open Horizon DB: %v", err)
}

session.DB.SetMaxIdleConns(app.config.HorizonDBMaxIdleConnections)
session.DB.SetMaxOpenConns(app.config.HorizonDBMaxOpenConnections)
app.historyQ = &history.Q{session}
session.DB.SetMaxIdleConns(maxIdle)
session.DB.SetMaxOpenConns(maxOpen)
return session
}

func mustInitHorizonDB(app *App) {
maxIdle := app.config.HorizonDBMaxIdleConnections
maxOpen := app.config.HorizonDBMaxOpenConnections
if app.config.Ingest || app.config.IngestInMemoryOnly {
maxIdle -= expingest.MaxDBConnections
maxOpen -= expingest.MaxDBConnections
if maxIdle <= 0 {
log.Fatalf("max idle connections to horizon db must be greater than %d", expingest.MaxDBConnections)
}
if maxOpen <= 0 {
log.Fatalf("max open connections to horizon db must be greater than %d", expingest.MaxDBConnections)
}
}

app.historyQ = &history.Q{mustNewDBSession(
app.config.DatabaseURL,
maxIdle,
maxOpen,
)}
}

func mustInitCoreDB(app *App) {
session, err := db.Open("postgres", app.config.StellarCoreDatabaseURL)
if err != nil {
log.Fatalf("cannot open Core DB: %v", err)
maxIdle := app.config.CoreDBMaxIdleConnections
maxOpen := app.config.CoreDBMaxOpenConnections
if app.config.Ingest || app.config.IngestInMemoryOnly {
maxIdle -= expingest.MaxDBConnections
maxOpen -= expingest.MaxDBConnections
if maxIdle <= 0 {
log.Fatalf("max idle connections to stellar-core db must be greater than %d", expingest.MaxDBConnections)
}
if maxOpen <= 0 {
log.Fatalf("max open connections to stellar-core db must be greater than %d", expingest.MaxDBConnections)
}
}

session.DB.SetMaxIdleConns(app.config.CoreDBMaxIdleConnections)
session.DB.SetMaxOpenConns(app.config.CoreDBMaxOpenConnections)
app.coreQ = &core.Q{session}
app.coreQ = &core.Q{mustNewDBSession(
app.config.StellarCoreDatabaseURL,
maxIdle,
maxOpen,
)}
}

func initExpIngester(app *App, orderBookGraph *orderbook.OrderBookGraph) {
var err error
app.expingester, err = expingest.NewSystem(expingest.Config{
CoreSession: app.CoreSession(context.Background()),
HistorySession: app.HorizonSession(context.Background()),
CoreSession: mustNewDBSession(
app.config.StellarCoreDatabaseURL, expingest.MaxDBConnections, expingest.MaxDBConnections,
),
HistorySession: mustNewDBSession(
app.config.DatabaseURL, expingest.MaxDBConnections, expingest.MaxDBConnections,
),
NetworkPassphrase: app.config.NetworkPassphrase,
// TODO:
// Use the first archive for now. We don't have a mechanism to
Expand Down