Skip to content

Commit

Permalink
feat(database): add an optional master replica max connections settin…
Browse files Browse the repository at this point in the history
…gs (#1472)

## What ❔

<!-- What are the changes this PR brings about? -->
<!-- Example: This PR adds a PR template to the repo. -->
<!-- (For bigger PRs adding more context is appreciated) -->

## Why ❔

<!-- Why are these changes done? What goal do they contribute to? What
are the principles behind them? -->
<!-- Example: PR templates ensure PR reviewers, observers, and future
iterators are in context about the evolution of repos. -->

## Checklist

<!-- Check your PR fulfills the following items. -->
<!-- For draft PRs check the boxes as you complete them. -->

- [ ] PR title corresponds to the body of PR (we generate changelog
entries from PRs).
- [ ] Tests for the changes have been added / updated.
- [ ] Documentation comments have been added / updated.
- [ ] Code has been formatted via `zk fmt` and `zk lint`.
- [ ] Spellcheck has been run via `zk spellcheck`.
- [ ] Linkcheck has been run via `zk linkcheck`.

---------

Co-authored-by: Roman Brodetski <rb@matterlabs.dev>
Co-authored-by: perekopskiy <53865202+perekopskiy@users.noreply.github.com>
  • Loading branch information
3 people authored Mar 21, 2024
1 parent b82d093 commit e5c8127
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 4 deletions.
6 changes: 6 additions & 0 deletions core/lib/config/src/configs/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ pub struct PostgresConfig {
pub prover_url: Option<String>,
/// Maximum size of the connection pool.
pub max_connections: Option<u32>,
/// Maximum size of the connection pool to master DB.
pub max_connections_master: Option<u32>,

/// Acquire timeout in seconds for a single connection attempt. There are multiple attempts (currently 3)
/// before acquire methods will return an error.
Expand Down Expand Up @@ -173,6 +175,10 @@ impl PostgresConfig {
self.max_connections.context("Max connections is absent")
}

pub fn max_connections_master(&self) -> Option<u32> {
self.max_connections_master
}

/// Returns the Postgres statement timeout.
pub fn statement_timeout(&self) -> Option<Duration> {
self.statement_timeout_sec.map(Duration::from_secs)
Expand Down
1 change: 1 addition & 0 deletions core/lib/config/src/testonly.rs
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,7 @@ impl RandomConfig for configs::database::PostgresConfig {
replica_url: g.gen(),
prover_url: g.gen(),
max_connections: g.gen(),
max_connections_master: g.gen(),
acquire_timeout_sec: g.gen(),
statement_timeout_sec: g.gen(),
long_connection_threshold_ms: g.gen(),
Expand Down
2 changes: 2 additions & 0 deletions core/lib/env_config/src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ impl FromEnv for PostgresConfig {
.ok()
.or_else(|| master_url.clone());
let max_connections = parse_optional_var("DATABASE_POOL_SIZE")?;
let max_connections_master = parse_optional_var("DATABASE_POOL_SIZE_MASTER")?;
let acquire_timeout_sec = parse_optional_var("DATABASE_ACQUIRE_TIMEOUT_SEC")?;
let statement_timeout_sec = parse_optional_var("DATABASE_STATEMENT_TIMEOUT_SEC")?;
let long_connection_threshold_ms =
Expand All @@ -49,6 +50,7 @@ impl FromEnv for PostgresConfig {
replica_url,
prover_url,
max_connections,
max_connections_master,
acquire_timeout_sec,
statement_timeout_sec,
long_connection_threshold_ms,
Expand Down
2 changes: 2 additions & 0 deletions core/lib/protobuf_config/src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ impl ProtoRepr for proto::Postgres {
replica_url: self.replica_url.clone(),
prover_url: self.prover_url.clone(),
max_connections: self.max_connections,
max_connections_master: self.max_connections_master,
acquire_timeout_sec: self.acquire_timeout_sec,
statement_timeout_sec: self.statement_timeout_sec,
long_connection_threshold_ms: self.long_connection_threshold_ms,
Expand All @@ -105,6 +106,7 @@ impl ProtoRepr for proto::Postgres {
replica_url: this.replica_url.clone(),
prover_url: this.prover_url.clone(),
max_connections: this.max_connections,
max_connections_master: this.max_connections_master,
acquire_timeout_sec: this.acquire_timeout_sec,
statement_timeout_sec: this.statement_timeout_sec,
long_connection_threshold_ms: this.long_connection_threshold_ms,
Expand Down
1 change: 1 addition & 0 deletions core/lib/protobuf_config/src/proto/database.proto
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,5 @@ message Postgres {
optional uint64 acquire_timeout_sec = 6; // optional; s
optional uint64 long_connection_threshold_ms = 7; // optional; ms
optional uint64 slow_query_threshold_ms = 8; // optional; ms
optional uint32 max_connections_master = 9; // optional
}
13 changes: 9 additions & 4 deletions core/lib/zksync_core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,10 +324,15 @@ pub async fn initialize_components(
}

let pool_size = postgres_config.max_connections()?;
let connection_pool = ConnectionPool::<Core>::builder(postgres_config.master_url()?, pool_size)
.build()
.await
.context("failed to build connection_pool")?;
let pool_size_master = postgres_config
.max_connections_master()
.unwrap_or(pool_size);

let connection_pool =
ConnectionPool::<Core>::builder(postgres_config.master_url()?, pool_size_master)
.build()
.await
.context("failed to build connection_pool")?;
// We're most interested in setting acquire / statement timeouts for the API server, which puts the most load
// on Postgres.
let replica_connection_pool =
Expand Down

0 comments on commit e5c8127

Please sign in to comment.