Skip to content

Commit 7f06b70

Browse files
committed
Add a Vacuum option ton SQLite connection builder
to limit duplication between aggregator & signer
1 parent f884875 commit 7f06b70

File tree

3 files changed

+48
-34
lines changed

3 files changed

+48
-34
lines changed

internal/mithril-persistence/src/sqlite/connection_builder.rs

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@ use std::ops::Not;
22
use std::path::{Path, PathBuf};
33

44
use anyhow::Context;
5-
use slog::Logger;
5+
use slog::{info, Logger};
66
use sqlite::{Connection, ConnectionThreadSafe};
77

88
use mithril_common::StdResult;
99

1010
use crate::database::{ApplicationNodeType, DatabaseVersionChecker, SqlMigration};
11+
use crate::sqlite::vacuum_database;
1112

1213
/// Builder of SQLite connection
1314
pub struct ConnectionBuilder {
@@ -31,6 +32,11 @@ pub enum ConnectionOptions {
3132
///
3233
/// This option take priority over [ConnectionOptions::EnableForeignKeys] if both are enabled.
3334
ForceDisableForeignKeys,
35+
36+
/// Run a VACUUM operation on the database after the connection is opened
37+
///
38+
/// ⚠ This operation can be very slow on large databases ⚠
39+
Vacuum,
3440
}
3541

3642
impl ConnectionBuilder {
@@ -86,6 +92,23 @@ impl ConnectionBuilder {
8692
)
8793
})?;
8894

95+
if self.options.contains(&ConnectionOptions::Vacuum) {
96+
info!(
97+
self.logger,
98+
"Vacuuming SQLite database, this may take a while...";
99+
"database" => self.connection_path.display()
100+
);
101+
102+
vacuum_database(&connection)
103+
.with_context(|| "SQLite initialization: database VACUUM error")?;
104+
105+
info!(
106+
self.logger,
107+
"SQLite database vacuumed successfully";
108+
"database" => self.connection_path.display()
109+
);
110+
}
111+
89112
if self
90113
.options
91114
.contains(&ConnectionOptions::EnableWriteAheadLog)
@@ -130,11 +153,14 @@ impl ConnectionBuilder {
130153

131154
#[cfg(test)]
132155
mod tests {
133-
use super::*;
134-
use crate::sqlite::ConnectionOptions::ForceDisableForeignKeys;
135-
use mithril_common::test_utils::TempDir;
136156
use sqlite::Value;
137157

158+
use mithril_common::test_utils::TempDir;
159+
160+
use crate::sqlite::ConnectionOptions::ForceDisableForeignKeys;
161+
162+
use super::*;
163+
138164
// see: https://www.sqlite.org/pragma.html#pragma_journal_mode
139165
const DEFAULT_SQLITE_JOURNAL_MODE: &str = "delete";
140166
// see: https://www.sqlite.org/pragma.html#pragma_synchronous

mithril-aggregator/src/dependency_injection/builder.rs

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use anyhow::Context;
22
use semver::Version;
3-
use slog::{info, Logger};
3+
use slog::Logger;
44
use std::sync::Arc;
55
use tokio::{
66
sync::{
@@ -40,10 +40,7 @@ use mithril_common::{
4040
};
4141
use mithril_persistence::{
4242
database::{repository::CardanoTransactionRepository, ApplicationNodeType, SqlMigration},
43-
sqlite::{
44-
vacuum_database, ConnectionBuilder, ConnectionOptions, SqliteConnection,
45-
SqliteConnectionPool,
46-
},
43+
sqlite::{ConnectionBuilder, ConnectionOptions, SqliteConnection, SqliteConnectionPool},
4744
store::adapter::{MemoryAdapter, SQLiteAdapter, StoreAdapter},
4845
};
4946

@@ -286,6 +283,7 @@ impl DependenciesBuilder {
286283
migrations: Vec<SqlMigration>,
287284
do_vacuum_database: bool,
288285
) -> Result<SqliteConnection> {
286+
let logger = self.get_logger()?;
289287
let connection_builder = match self.configuration.environment {
290288
ExecutionEnvironment::Production => ConnectionBuilder::open_file(
291289
&self.configuration.get_sqlite_dir().join(sqlite_file_name),
@@ -301,13 +299,21 @@ impl DependenciesBuilder {
301299
),
302300
};
303301

304-
let logger = self.get_logger()?;
305-
let connection = connection_builder
306-
.with_node_type(ApplicationNodeType::Aggregator)
307-
.with_options(&[
302+
let connection_options = {
303+
let mut options = vec![
308304
ConnectionOptions::EnableForeignKeys,
309305
ConnectionOptions::EnableWriteAheadLog,
310-
])
306+
];
307+
if do_vacuum_database {
308+
options.push(ConnectionOptions::Vacuum);
309+
}
310+
311+
options
312+
};
313+
314+
let connection = connection_builder
315+
.with_node_type(ApplicationNodeType::Aggregator)
316+
.with_options(&connection_options)
311317
.with_logger(logger.clone())
312318
.with_migrations(migrations)
313319
.build()
@@ -316,16 +322,6 @@ impl DependenciesBuilder {
316322
error: Some(e),
317323
})?;
318324

319-
if do_vacuum_database {
320-
info!(
321-
logger,
322-
"Vacuuming '{sqlite_file_name}', this may take a while..."
323-
);
324-
vacuum_database(&connection)
325-
.with_context(|| "SQLite initialization: database vacuum error")?;
326-
info!(logger, "SQLite '{sqlite_file_name}' vacuumed successfully");
327-
}
328-
329325
Ok(connection)
330326
}
331327

mithril-signer/src/runtime/signer_services.rs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use anyhow::{anyhow, Context};
22
use async_trait::async_trait;
3-
use slog::info;
43
use std::{fs, sync::Arc, time::Duration};
54

65
use mithril_common::{
@@ -25,7 +24,7 @@ use mithril_common::{
2524
};
2625
use mithril_persistence::{
2726
database::{repository::CardanoTransactionRepository, ApplicationNodeType, SqlMigration},
28-
sqlite::{vacuum_database, ConnectionBuilder, SqliteConnection, SqliteConnectionPool},
27+
sqlite::{ConnectionBuilder, ConnectionOptions, SqliteConnection, SqliteConnectionPool},
2928
store::{adapter::SQLiteAdapter, StakeStore},
3029
};
3130

@@ -173,19 +172,12 @@ impl<'a> ProductionServiceBuilder<'a> {
173172
let logger = slog_scope::logger();
174173
let connection = ConnectionBuilder::open_file(&sqlite_db_path)
175174
.with_node_type(ApplicationNodeType::Signer)
175+
.with_options(&[ConnectionOptions::Vacuum])
176176
.with_migrations(migrations)
177177
.with_logger(logger.clone())
178178
.build()
179179
.with_context(|| "Database connection initialisation error")?;
180180

181-
info!(
182-
logger,
183-
"Vacuuming '{sqlite_file_name}', this may take a while..."
184-
);
185-
vacuum_database(&connection)
186-
.with_context(|| "SQLite initialization: database vacuum error")?;
187-
info!(logger, "SQLite '{sqlite_file_name}' vacuumed successfully");
188-
189181
Ok(connection)
190182
}
191183
}

0 commit comments

Comments
 (0)