Skip to content

Commit

Permalink
Bump default PG conns from 20->100; enable auto-scaling open conns fo…
Browse files Browse the repository at this point in the history
…r mercury (#12697)

* Bump default PG conns from 20->100; enable auto-scaling open conns for mercury

* Try git submodule update --recursive --remote

* Reset foundry
  • Loading branch information
samsondav authored Apr 4, 2024
1 parent b2c9c3b commit 33398b7
Show file tree
Hide file tree
Showing 16 changed files with 52 additions and 17 deletions.
9 changes: 9 additions & 0 deletions .changeset/cool-apricots-compare.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
"chainlink": patch
---

Increase default config for postgres max open conns from 20 to 100.

Also, add autoscaling for mercury jobs. The max open conns limit will be
automatically increased to the number of mercury jobs if this exceeds the
configured value.
2 changes: 1 addition & 1 deletion core/config/docs/core.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ MaxIdleConns = 10 # Default
# MaxOpenConns configures the maximum number of database connections that a Chainlink node will have open at any one time. Think of this as the maximum burst upper bound limit of database connections per Chainlink node instance. Increasing this number can help to improve performance under database-heavy workloads.
#
# Postgres has connection limits, so you must use caution when increasing this value. If you are running several instances of a Chainlink node or another application on a single database server, you might run out of Postgres connection slots if you raise this value too high.
MaxOpenConns = 20 # Default
MaxOpenConns = 100 # Default
# MigrateOnStartup controls whether a Chainlink node will attempt to automatically migrate the database on boot. If you want more control over your database migration process, set this variable to `false` and manually migrate the database using the CLI `migrate` command instead.
MigrateOnStartup = true # Default

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ DefaultLockTimeout = '15s'
DefaultQueryTimeout = '10s'
LogQueries = false
MaxIdleConns = 10
MaxOpenConns = 20
MaxOpenConns = 100
MigrateOnStartup = true

[Database.Backup]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ DefaultLockTimeout = '15s'
DefaultQueryTimeout = '10s'
LogQueries = false
MaxIdleConns = 10
MaxOpenConns = 20
MaxOpenConns = 100
MigrateOnStartup = true

[Database.Backup]
Expand Down
30 changes: 28 additions & 2 deletions core/services/pg/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,7 @@ func NewConnection(uri string, dialect dialects.DialectName, config ConnectionCo
if _, err = db.Exec(stmt); err != nil {
return nil, err
}
db.SetMaxOpenConns(config.MaxOpenConns())
db.SetMaxIdleConns(config.MaxIdleConns())
setMaxConns(db, config)

if os.Getenv("SKIP_PG_VERSION_CHECK") != "true" {
if err := checkVersion(db, MinRequiredPGVersion); err != nil {
Expand All @@ -94,6 +93,33 @@ func NewConnection(uri string, dialect dialects.DialectName, config ConnectionCo
return db, disallowReplica(db)
}

func setMaxConns(db *sqlx.DB, config ConnectionConfig) {
db.SetMaxOpenConns(config.MaxOpenConns())
db.SetMaxIdleConns(config.MaxIdleConns())

// HACK: In the case of mercury jobs, one conn is needed per job for good
// performance. Most nops will forget to increase the defaults to account
// for this so we detect it here instead.
//
// This problem will be solved by replacing mercury with parallel
// compositions (llo plugin).
//
// See: https://smartcontract-it.atlassian.net/browse/MERC-3654
var cnt int
if err := db.Get(&cnt, `SELECT COUNT(*) FROM ocr2_oracle_specs WHERE plugin_type = 'mercury'`); err != nil {
log.Printf("Error checking mercury jobs: %s", err.Error())
return
}
if cnt > config.MaxOpenConns() {
log.Printf("Detected %d mercury jobs, increasing max open connections from %d to %d", cnt, config.MaxOpenConns(), cnt)
db.SetMaxOpenConns(cnt)
}
if cnt > config.MaxIdleConns() {
log.Printf("Detected %d mercury jobs, increasing max idle connections from %d to %d", cnt, config.MaxIdleConns(), cnt)
db.SetMaxIdleConns(cnt)
}
}

type Getter interface {
Get(dest interface{}, query string, args ...interface{}) error
}
Expand Down
2 changes: 1 addition & 1 deletion core/web/resolver/testdata/config-empty-effective.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ DefaultLockTimeout = '15s'
DefaultQueryTimeout = '10s'
LogQueries = false
MaxIdleConns = 10
MaxOpenConns = 20
MaxOpenConns = 100
MigrateOnStartup = true

[Database.Backup]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ DefaultLockTimeout = '15s'
DefaultQueryTimeout = '10s'
LogQueries = false
MaxIdleConns = 10
MaxOpenConns = 20
MaxOpenConns = 100
MigrateOnStartup = true

[Database.Backup]
Expand Down
4 changes: 2 additions & 2 deletions docs/CONFIG.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ DefaultLockTimeout = '15s' # Default
DefaultQueryTimeout = '10s' # Default
LogQueries = false # Default
MaxIdleConns = 10 # Default
MaxOpenConns = 20 # Default
MaxOpenConns = 100 # Default
MigrateOnStartup = true # Default
```

Expand Down Expand Up @@ -119,7 +119,7 @@ Postgres has connection limits, so you must use caution when increasing this val

### MaxOpenConns
```toml
MaxOpenConns = 20 # Default
MaxOpenConns = 100 # Default
```
MaxOpenConns configures the maximum number of database connections that a Chainlink node will have open at any one time. Think of this as the maximum burst upper bound limit of database connections per Chainlink node instance. Increasing this number can help to improve performance under database-heavy workloads.

Expand Down
2 changes: 1 addition & 1 deletion testdata/scripts/node/validate/default.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ DefaultLockTimeout = '15s'
DefaultQueryTimeout = '10s'
LogQueries = false
MaxIdleConns = 10
MaxOpenConns = 20
MaxOpenConns = 100
MigrateOnStartup = true

[Database.Backup]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ DefaultLockTimeout = '15s'
DefaultQueryTimeout = '10s'
LogQueries = false
MaxIdleConns = 10
MaxOpenConns = 20
MaxOpenConns = 100
MigrateOnStartup = true

[Database.Backup]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ DefaultLockTimeout = '15s'
DefaultQueryTimeout = '10s'
LogQueries = false
MaxIdleConns = 10
MaxOpenConns = 20
MaxOpenConns = 100
MigrateOnStartup = true

[Database.Backup]
Expand Down
2 changes: 1 addition & 1 deletion testdata/scripts/node/validate/disk-based-logging.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ DefaultLockTimeout = '15s'
DefaultQueryTimeout = '10s'
LogQueries = false
MaxIdleConns = 10
MaxOpenConns = 20
MaxOpenConns = 100
MigrateOnStartup = true

[Database.Backup]
Expand Down
2 changes: 1 addition & 1 deletion testdata/scripts/node/validate/invalid-ocr-p2p.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ DefaultLockTimeout = '15s'
DefaultQueryTimeout = '10s'
LogQueries = false
MaxIdleConns = 10
MaxOpenConns = 20
MaxOpenConns = 100
MigrateOnStartup = true

[Database.Backup]
Expand Down
2 changes: 1 addition & 1 deletion testdata/scripts/node/validate/invalid.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ DefaultLockTimeout = '15s'
DefaultQueryTimeout = '10s'
LogQueries = false
MaxIdleConns = 10
MaxOpenConns = 20
MaxOpenConns = 100
MigrateOnStartup = true

[Database.Backup]
Expand Down
2 changes: 1 addition & 1 deletion testdata/scripts/node/validate/valid.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ DefaultLockTimeout = '15s'
DefaultQueryTimeout = '10s'
LogQueries = false
MaxIdleConns = 10
MaxOpenConns = 20
MaxOpenConns = 100
MigrateOnStartup = true

[Database.Backup]
Expand Down
2 changes: 1 addition & 1 deletion testdata/scripts/node/validate/warnings.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ DefaultLockTimeout = '15s'
DefaultQueryTimeout = '10s'
LogQueries = false
MaxIdleConns = 10
MaxOpenConns = 20
MaxOpenConns = 100
MigrateOnStartup = true

[Database.Backup]
Expand Down

0 comments on commit 33398b7

Please sign in to comment.