Skip to content

Commit c3f9220

Browse files
Frank ChinonsoFrank Chinonso
authored andcommitted
feat(store): make sqlite pool size configurable
Add a new sqlite pool-size option in shared storage CLI options and thread it through store state initialization so operators can tune database concurrency instead of relying on a hardcoded pool of 16.
1 parent 909de78 commit c3f9220

5 files changed

Lines changed: 61 additions & 9 deletions

File tree

crates/db/src/lib.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ mod conv;
22
mod errors;
33
mod manager;
44

5+
use std::num::NonZeroUsize;
56
use std::path::Path;
67

78
pub use conv::{DatabaseTypeConversionError, SqlTypeConvert};
@@ -22,8 +23,18 @@ pub struct Db {
2223
impl Db {
2324
/// Creates a new database instance with the provided connection pool.
2425
pub fn new(database_filepath: &Path) -> Result<Self, DatabaseError> {
26+
Self::new_with_pool_size(database_filepath, NonZeroUsize::new(16).expect("non-zero"))
27+
}
28+
29+
/// Creates a new database instance with a configurable SQLite connection pool size.
30+
pub fn new_with_pool_size(
31+
database_filepath: &Path,
32+
sqlite_pool_size: NonZeroUsize,
33+
) -> Result<Self, DatabaseError> {
2534
let manager = ConnectionManager::new(database_filepath.to_str().unwrap());
26-
let pool = deadpool_diesel::Pool::builder(manager).max_size(16).build()?;
35+
let pool = deadpool_diesel::Pool::builder(manager)
36+
.max_size(sqlite_pool_size.get())
37+
.build()?;
2738
Ok(Self { pool })
2839
}
2940

crates/store/src/db/mod.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use std::collections::{BTreeMap, BTreeSet, HashSet};
22
use std::mem::size_of;
3+
use std::num::NonZeroUsize;
34
use std::ops::{Deref, DerefMut, RangeInclusive};
45
use std::path::PathBuf;
56
use std::sync::Arc;
@@ -280,8 +281,11 @@ impl Db {
280281

281282
/// Open a connection to the DB and apply any pending migrations.
282283
#[instrument(target = COMPONENT, skip_all)]
283-
pub async fn load(database_filepath: PathBuf) -> Result<Self, DatabaseError> {
284-
let db = miden_node_db::Db::new(&database_filepath)?;
284+
pub async fn load(
285+
database_filepath: PathBuf,
286+
sqlite_pool_size: NonZeroUsize,
287+
) -> Result<Self, DatabaseError> {
288+
let db = miden_node_db::Db::new_with_pool_size(&database_filepath, sqlite_pool_size)?;
285289
info!(
286290
target: COMPONENT,
287291
sqlite= %database_filepath.display(),

crates/store/src/db/tests.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1532,7 +1532,12 @@ async fn reconstruct_storage_map_from_db_pages_until_latest() {
15321532
let block2 = BlockNumber::from(2);
15331533
let block3 = BlockNumber::from(3);
15341534

1535-
let db = crate::db::Db::load(db_path).await.unwrap();
1535+
let db = crate::db::Db::load(
1536+
db_path,
1537+
miden_node_utils::clap::StorageOptions::default().sqlite_pool_size,
1538+
)
1539+
.await
1540+
.unwrap();
15361541
let slot_name_for_db = slot_name.clone();
15371542
db.query("insert paged values", move |db_conn| {
15381543
db_conn.transaction(|db_conn| {
@@ -1600,7 +1605,12 @@ async fn reconstruct_storage_map_from_db_returns_limit_exceeded_for_single_block
16001605

16011606
let block5 = BlockNumber::from(5);
16021607

1603-
let db = crate::db::Db::load(db_path).await.unwrap();
1608+
let db = crate::db::Db::load(
1609+
db_path,
1610+
miden_node_utils::clap::StorageOptions::default().sqlite_pool_size,
1611+
)
1612+
.await
1613+
.unwrap();
16041614
let slot_name_for_db = slot_name.clone();
16051615
db.query("insert entries in single block", move |db_conn| {
16061616
db_conn.transaction(|db_conn| {

crates/store/src/state/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ impl State {
163163
);
164164

165165
let database_filepath = data_directory.database_path();
166-
let mut db = Db::load(database_filepath.clone())
166+
let mut db = Db::load(database_filepath.clone(), storage_options.sqlite_pool_size)
167167
.await
168168
.map_err(StateInitializationError::DatabaseLoadError)?;
169169

crates/utils/src/clap.rs

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Public module for share clap pieces to reduce duplication
22
3-
use std::num::{NonZeroU32, NonZeroU64};
3+
use std::num::{NonZeroU32, NonZeroU64, NonZeroUsize};
44
use std::time::Duration;
55

66
#[cfg(feature = "rocksdb")]
@@ -14,6 +14,7 @@ const DEFAULT_MAX_CONNECTION_AGE: Duration = Duration::from_mins(30);
1414
const DEFAULT_REPLENISH_N_PER_SECOND_PER_IP: NonZeroU64 = NonZeroU64::new(16).unwrap();
1515
const DEFAULT_BURST_SIZE: NonZeroU32 = NonZeroU32::new(128).unwrap();
1616
const DEFAULT_MAX_CONCURRENT_CONNECTIONS: u64 = 1_000;
17+
const DEFAULT_SQLITE_POOL_SIZE: NonZeroUsize = NonZeroUsize::new(16).unwrap();
1718

1819
// Formats a Duration into a human-readable string for display in clap help text
1920
// and yields a &'static str by _leaking_ the string deliberately.
@@ -141,8 +142,17 @@ impl GrpcOptionsExternal {
141142
/// Collection of per usage storage backend configurations.
142143
///
143144
/// Note: Currently only contains `rocksdb` related configuration.
144-
#[derive(clap::Args, Clone, Debug, Default, PartialEq, Eq)]
145+
#[derive(clap::Args, Clone, Debug, PartialEq, Eq)]
145146
pub struct StorageOptions {
147+
/// Maximum number of SQLite connections in the async pool.
148+
#[arg(
149+
long = "sqlite.pool_size",
150+
env = "MIDEN_NODE_SQLITE_POOL_SIZE",
151+
default_value_t = DEFAULT_SQLITE_POOL_SIZE,
152+
value_name = "NUM"
153+
)]
154+
pub sqlite_pool_size: NonZeroUsize,
155+
146156
#[cfg(feature = "rocksdb")]
147157
#[clap(flatten)]
148158
pub account_tree: AccountTreeRocksDbOptions,
@@ -168,9 +178,26 @@ impl StorageOptions {
168178
cache_size_in_bytes: DEFAULT_ROCKSDB_CACHE_SIZE,
169179
durability_mode: None,
170180
};
171-
Self { account_tree, nullifier_tree }
181+
Self { sqlite_pool_size: DEFAULT_SQLITE_POOL_SIZE, account_tree, nullifier_tree }
172182
}
173183
#[cfg(not(feature = "rocksdb"))]
174184
Self::default()
175185
}
176186
}
187+
188+
impl Default for StorageOptions {
189+
fn default() -> Self {
190+
#[cfg(feature = "rocksdb")]
191+
{
192+
Self {
193+
sqlite_pool_size: DEFAULT_SQLITE_POOL_SIZE,
194+
account_tree: AccountTreeRocksDbOptions::default(),
195+
nullifier_tree: NullifierTreeRocksDbOptions::default(),
196+
}
197+
}
198+
#[cfg(not(feature = "rocksdb"))]
199+
{
200+
Self { sqlite_pool_size: DEFAULT_SQLITE_POOL_SIZE }
201+
}
202+
}
203+
}

0 commit comments

Comments
 (0)