Skip to content

Commit 63c74fe

Browse files
committed
add retries + sqlite pragmas for light client database
1 parent d4974cf commit 63c74fe

6 files changed

Lines changed: 237 additions & 176 deletions

File tree

hotshot-query-service/Cargo.toml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,12 @@ default = ["file-system-data-source", "metrics-data-source", "sql-data-source"]
2323
# Enables support for an embedded SQLite database instead of PostgreSQL.
2424
# Ideal for lightweight nodes that benefit from pruning and merklized state storage,
2525
# offering advantages over file system storage.
26-
embedded-db = ["sqlx/sqlite"]
26+
embedded-db = ["sqlx/sqlite", "sqlite-options"]
27+
28+
# Exposes shared SQLite connection helpers (e.g. `sqlite_options`) for callers that build their
29+
# own pool. Separate from `embedded-db` so clients can opt in to the helpers without flipping the
30+
# workspace into SQLite mode.
31+
sqlite-options = ["sqlx/sqlite"]
2732

2833
# Enable the availability data source backed by the local file system.
2934
file-system-data-source = ["atomic_store"]

hotshot-query-service/src/data_source/storage/sql.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -247,12 +247,7 @@ impl Default for Config {
247247
#[cfg(feature = "embedded-db")]
248248
impl Default for Config {
249249
fn default() -> Self {
250-
SqliteConnectOptions::default()
251-
.journal_mode(sqlx::sqlite::SqliteJournalMode::Wal)
252-
.busy_timeout(Duration::from_secs(30))
253-
.auto_vacuum(sqlx::sqlite::SqliteAutoVacuum::Incremental)
254-
.create_if_missing(true)
255-
.into()
250+
crate::sqlite_options::sqlite_options().into()
256251
}
257252
}
258253

hotshot-query-service/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,8 @@ pub mod metrics;
424424
pub mod migration;
425425
pub mod node;
426426
mod resolvable;
427+
#[cfg(feature = "sqlite-options")]
428+
pub mod sqlite_options;
427429
pub mod status;
428430
pub mod task;
429431
pub mod testing;
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//! Shared SQLite connection defaults.
2+
//!
3+
//! Used by both the embedded-db variant of this crate and by external clients (e.g. the light
4+
//! client) that build their own pool. Keeping the pragma choices in one place ensures every
5+
//! SQLite database in the workspace runs with the same journaling, locking, and vacuum settings.
6+
7+
use std::time::Duration;
8+
9+
use sqlx::sqlite::{SqliteAutoVacuum, SqliteConnectOptions, SqliteJournalMode};
10+
11+
/// Default [`SqliteConnectOptions`] for SQLite databases in this workspace.
12+
///
13+
/// WAL journaling is the load-bearing choice: under the default rollback journal, any writer
14+
/// holds an exclusive lock that blocks readers on other connections, which produces spurious
15+
/// `SQLITE_BUSY` ("database is locked") errors under concurrent access. WAL lets readers and
16+
/// the single writer proceed in parallel.
17+
///
18+
/// Callers add `.filename(...)` (or use `:memory:`) on top.
19+
pub fn sqlite_options() -> SqliteConnectOptions {
20+
SqliteConnectOptions::default()
21+
.journal_mode(SqliteJournalMode::Wal)
22+
.busy_timeout(Duration::from_secs(30))
23+
.auto_vacuum(SqliteAutoVacuum::Incremental)
24+
.create_if_missing(true)
25+
}

light-client/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ derive_builder = { workspace = true }
2121
derive_more = { workspace = true }
2222
espresso-types = { workspace = true }
2323
futures = { workspace = true }
24-
hotshot-query-service = { workspace = true }
24+
hotshot-query-service = { workspace = true, features = ["sqlite-options"] }
2525
hotshot-query-service-types = { workspace = true }
2626
hotshot-types = { workspace = true }
2727
jf-advz = { workspace = true }

0 commit comments

Comments
 (0)