Skip to content

Commit

Permalink
feat(infra): enable configuring min & max cockroach pool conns (#922)
Browse files Browse the repository at this point in the history
<!-- Please make sure there is an issue that this PR is correlated to. -->

## Changes

<!-- If there are frontend changes, please include screenshots. -->
  • Loading branch information
NathanFlurry committed Jun 19, 2024
1 parent 4513a1f commit e8e7255
Show file tree
Hide file tree
Showing 15 changed files with 204 additions and 447 deletions.
2 changes: 2 additions & 0 deletions lib/bolt/config/src/ns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,8 @@ pub struct Turnstile {
pub struct Service {
pub count: usize,
pub resources: ServiceResources,
pub crdb_min_connections: Option<u64>,
pub crdb_max_connections: Option<u64>,
}

#[derive(Serialize, Deserialize, Clone, Debug)]
Expand Down
21 changes: 0 additions & 21 deletions lib/bolt/config/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,6 @@ pub struct ServiceConfig {

#[serde(default)]
pub resources: ServiceResourcesMap,

#[serde(default)]
pub cockroachdb: CockroachDB,
}

#[derive(Deserialize, Clone, Debug)]
Expand Down Expand Up @@ -240,24 +237,6 @@ impl Default for ServiceResourcesMap {
}
}

#[derive(Deserialize, Clone, Debug)]
#[serde(rename_all = "kebab-case", deny_unknown_fields)]
pub struct CockroachDB {
// Sets a minimum number of connections to the database. This is important to ensure that
// the initial queries are not delayed by a large surge of TCP connections immediately
// after startup.
//
// To figure out a healthy number for this value, see the `rivet_crdb_pool_conn_size`
// metric to see how many connections are being used for a given service.
pub min_connections: usize,
}

impl Default for CockroachDB {
fn default() -> Self {
Self { min_connections: 1 }
}
}

mod defaults {
pub fn singleton() -> bool {
false
Expand Down
14 changes: 10 additions & 4 deletions lib/bolt/core/src/context/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -923,10 +923,12 @@ impl ServiceContextData {
);
}

env.insert(
"CRDB_MIN_CONNECTIONS".into(),
self.config().cockroachdb.min_connections.to_string(),
);
if let Some(x) = ns_service_config.crdb_min_connections {
env.insert("CRDB_MIN_CONNECTIONS".into(), x.to_string());
}
if let Some(x) = ns_service_config.crdb_max_connections {
env.insert("CRDB_MAX_CONNECTIONS".into(), x.to_string());
}

if project_ctx.ns().prometheus.is_some() && self.depends_on_prometheus_api() {
env.insert(
Expand Down Expand Up @@ -1374,10 +1376,14 @@ impl ServiceContextData {
config::ns::ClusterKind::SingleNode { .. } => config::ns::Service {
count: 1,
resources: self.config().resources.single_node.clone(),
crdb_min_connections: None,
crdb_max_connections: None,
},
config::ns::ClusterKind::Distributed { .. } => config::ns::Service {
count: 2,
resources: self.config().resources.distributed.clone(),
crdb_min_connections: None,
crdb_max_connections: None,
},
});

Expand Down
Loading

0 comments on commit e8e7255

Please sign in to comment.