Skip to content

Commit bcad6b1

Browse files
authored
Merge pull request #809 from input-output-hk/greg/799/refactor_sqlite_connection
refactor sqlite connection
2 parents a35fc91 + 5269241 commit bcad6b1

File tree

14 files changed

+120
-324
lines changed

14 files changed

+120
-324
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

mithril-aggregator/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "mithril-aggregator"
3-
version = "0.2.31"
3+
version = "0.2.32"
44
description = "A Mithril Aggregator server"
55
authors = { workspace = true }
66
edition = { workspace = true }

mithril-aggregator/src/command_args.rs

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use slog_scope::{crit, debug, info};
55
use sqlite::Connection;
66
use std::{
77
error::Error,
8+
ffi::OsStr,
89
fs,
910
net::IpAddr,
1011
path::PathBuf,
@@ -34,14 +35,15 @@ use mithril_common::{
3435
};
3536

3637
use crate::{
38+
configuration::LIST_SNAPSHOTS_MAX_ITEMS,
3739
database::provider::StakePoolStore,
3840
event_store::{self, TransmitterService},
3941
http_server::routes::router,
4042
tools::{EraTools, GenesisTools, GenesisToolsDependency},
4143
AggregatorConfig, AggregatorRunner, AggregatorRuntime, CertificatePendingStore,
4244
CertificateStore, Configuration, DefaultConfiguration, DependencyManager, GenesisConfiguration,
43-
GzipSnapshotter, MithrilSignerRegisterer, MultiSignerImpl, ProtocolParametersStore,
44-
ProtocolParametersStorer, SingleSignatureStore, VerificationKeyStore,
45+
GzipSnapshotter, LocalSnapshotStore, MithrilSignerRegisterer, MultiSignerImpl,
46+
ProtocolParametersStore, ProtocolParametersStorer, SingleSignatureStore, VerificationKeyStore,
4547
};
4648

4749
const SQLITE_MONITORING_FILE: &str = "monitoring.sqlite3";
@@ -61,6 +63,12 @@ fn setup_genesis_dependencies(
6163
),
6264
)),
6365
);
66+
let sqlite_connection = Arc::new(Mutex::new(Connection::open(
67+
sqlite_db_path
68+
.as_ref()
69+
.map(|path| path.as_os_str())
70+
.unwrap_or(OsStr::new(":memory:")),
71+
)?));
6472
let immutable_file_observer = Arc::new(ImmutableFileSystemObserver::new(&config.db_directory));
6573
let beacon_provider = Arc::new(BeaconProviderImpl::new(
6674
chain_observer,
@@ -69,7 +77,7 @@ fn setup_genesis_dependencies(
6977
));
7078
let certificate_store = Arc::new(CertificateStore::new(Box::new(SQLiteAdapter::new(
7179
"certificate",
72-
sqlite_db_path.clone(),
80+
sqlite_connection.clone(),
7381
)?)));
7482
let certificate_verifier = Arc::new(MithrilCertificateVerifier::new(slog_scope::logger()));
7583
let genesis_verification_key = key_decode_hex(&config.genesis_verification_key)?;
@@ -79,22 +87,20 @@ fn setup_genesis_dependencies(
7987
let protocol_parameters_store = Arc::new(ProtocolParametersStore::new(
8088
Box::new(SQLiteAdapter::new(
8189
"protocol_parameters",
82-
sqlite_db_path.clone(),
90+
sqlite_connection.clone(),
8391
)?),
8492
config.store_retention_limit,
8593
));
8694
let verification_key_store = Arc::new(VerificationKeyStore::new(
8795
Box::new(SQLiteAdapter::new(
8896
"verification_key",
89-
sqlite_db_path.clone(),
97+
sqlite_connection.clone(),
9098
)?),
9199
config.store_retention_limit,
92100
));
93-
let stake_store = Arc::new(StakePoolStore::new(Arc::new(Mutex::new(Connection::open(
94-
sqlite_db_path.clone().unwrap(),
95-
)?))));
101+
let stake_store = Arc::new(StakePoolStore::new(sqlite_connection.clone()));
96102
let single_signature_store = Arc::new(SingleSignatureStore::new(
97-
Box::new(SQLiteAdapter::new("single_signature", sqlite_db_path)?),
103+
Box::new(SQLiteAdapter::new("single_signature", sqlite_connection)?),
98104
config.store_retention_limit,
99105
));
100106
let multi_signer = Arc::new(RwLock::new(MultiSignerImpl::new(
@@ -338,39 +344,44 @@ impl ServeCommand {
338344
.try_deserialize()
339345
.map_err(|e| format!("configuration deserialize error: {e}"))?;
340346
debug!("SERVE command"; "config" => format!("{config:?}"));
341-
let sqlite_db_path = Some(config.get_sqlite_file());
342347
check_database_migration(config.get_sqlite_file())?;
343348

344349
// Init dependencies
345-
let snapshot_store = config.build_snapshot_store()?;
350+
let sqlite_db_path = config.get_sqlite_file();
351+
let sqlite_connection = Arc::new(Mutex::new(Connection::open(sqlite_db_path)?));
352+
let snapshot_store = Arc::new(LocalSnapshotStore::new(
353+
Box::new(SQLiteAdapter::new("snapshot", sqlite_connection.clone())?),
354+
LIST_SNAPSHOTS_MAX_ITEMS,
355+
));
346356
let snapshot_uploader = config.build_snapshot_uploader()?;
347357

348358
let certificate_pending_store = Arc::new(CertificatePendingStore::new(Box::new(
349-
SQLiteAdapter::new("pending_certificate", sqlite_db_path.clone())?,
359+
SQLiteAdapter::new("pending_certificate", sqlite_connection.clone())?,
350360
)));
351361
let certificate_store = Arc::new(CertificateStore::new(Box::new(SQLiteAdapter::new(
352362
"certificate",
353-
sqlite_db_path.clone(),
363+
sqlite_connection.clone(),
354364
)?)));
355365
let verification_key_store = Arc::new(VerificationKeyStore::new(
356366
Box::new(SQLiteAdapter::new(
357367
"verification_key",
358-
sqlite_db_path.clone(),
368+
sqlite_connection.clone(),
359369
)?),
360370
config.store_retention_limit,
361371
));
362-
let stake_store = Arc::new(StakePoolStore::new(Arc::new(Mutex::new(Connection::open(
363-
sqlite_db_path.clone().unwrap(),
364-
)?))));
372+
let stake_store = Arc::new(StakePoolStore::new(sqlite_connection.clone()));
365373
let single_signature_store = Arc::new(SingleSignatureStore::new(
366374
Box::new(SQLiteAdapter::new(
367375
"single_signature",
368-
sqlite_db_path.clone(),
376+
sqlite_connection.clone(),
369377
)?),
370378
config.store_retention_limit,
371379
));
372380
let protocol_parameters_store = Arc::new(ProtocolParametersStore::new(
373-
Box::new(SQLiteAdapter::new("protocol_parameters", sqlite_db_path)?),
381+
Box::new(SQLiteAdapter::new(
382+
"protocol_parameters",
383+
sqlite_connection,
384+
)?),
374385
config.store_retention_limit,
375386
));
376387
let chain_observer = Arc::new(

mithril-aggregator/src/configuration.rs

Lines changed: 2 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,13 @@ use std::path::PathBuf;
88
use std::sync::Arc;
99

1010
use mithril_common::entities::{HexEncodedGenesisVerificationKey, ProtocolParameters};
11-
use mithril_common::store::adapter::SQLiteAdapter;
1211
use mithril_common::CardanoNetwork;
1312

14-
use crate::snapshot_stores::LocalSnapshotStore;
1513
use crate::tools::GcpFileUploader;
16-
use crate::{
17-
LocalSnapshotUploader, RemoteSnapshotStore, RemoteSnapshotUploader, SnapshotStore,
18-
SnapshotUploader,
19-
};
14+
use crate::{LocalSnapshotUploader, RemoteSnapshotUploader, SnapshotUploader};
2015

2116
// TODO: 'LIST_SNAPSHOTS_MAX_ITEMS' keep as const or in config, or add a parameter to `list_snapshots`?
22-
const LIST_SNAPSHOTS_MAX_ITEMS: usize = 20;
17+
pub const LIST_SNAPSHOTS_MAX_ITEMS: usize = 20;
2318
const SQLITE_FILE: &str = "aggregator.sqlite3";
2419

2520
/// Aggregator configuration
@@ -46,9 +41,6 @@ pub struct Configuration {
4641
/// Snapshots manifest location
4742
pub url_snapshot_manifest: String,
4843

49-
/// Type of snapshot store to use
50-
pub snapshot_store_type: SnapshotStoreType,
51-
5244
/// Type of snapshot uploader to use
5345
pub snapshot_uploader_type: SnapshotUploaderType,
5446

@@ -89,18 +81,6 @@ pub struct Configuration {
8981
pub era_reader_adapter_params: Option<String>,
9082
}
9183

92-
/// Snapshot store type enumerates the different kinds of snapshot stores.
93-
/// Local storage is mainly used by development and test environements while GCP
94-
/// is intended for production use.
95-
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
96-
#[serde(rename_all = "lowercase")]
97-
pub enum SnapshotStoreType {
98-
/// Google storage.
99-
Gcp,
100-
/// Local hard drive storage.
101-
Local,
102-
}
103-
10484
/// Uploader needed to copy the snapshot once computed.
10585
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
10686
#[serde(rename_all = "lowercase")]
@@ -117,27 +97,6 @@ impl Configuration {
11797
format!("http://{}:{}/", self.server_ip, self.server_port)
11898
}
11999

120-
/// Create a snapshot store from the configuration settings.
121-
pub fn build_snapshot_store(&self) -> Result<Arc<dyn SnapshotStore>, Box<dyn Error>> {
122-
match self.snapshot_store_type {
123-
SnapshotStoreType::Gcp => Ok(Arc::new(RemoteSnapshotStore::new(
124-
Box::new(GcpFileUploader::new(
125-
self.snapshot_bucket_name.to_owned().ok_or_else(|| {
126-
ConfigError::Message("missing snapshot bucket name".to_string())
127-
})?,
128-
)),
129-
self.url_snapshot_manifest.clone(),
130-
))),
131-
SnapshotStoreType::Local => Ok(Arc::new(LocalSnapshotStore::new(
132-
Box::new(SQLiteAdapter::new(
133-
"snapshot",
134-
Some(self.get_sqlite_file()),
135-
)?),
136-
LIST_SNAPSHOTS_MAX_ITEMS,
137-
))),
138-
}
139-
}
140-
141100
/// Create a snapshot uploader from configuration settings.
142101
pub fn build_snapshot_uploader(&self) -> Result<Arc<dyn SnapshotUploader>, Box<dyn Error>> {
143102
match self.snapshot_uploader_type {

mithril-aggregator/src/dependency.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ pub mod tests {
268268
event_store::TransmitterService, AggregatorConfig, CertificatePendingStore,
269269
CertificateStore, Configuration, DependencyManager, DumbSnapshotUploader, DumbSnapshotter,
270270
LocalSnapshotStore, MithrilSignerRegisterer, MultiSignerImpl, ProtocolParametersStore,
271-
SingleSignatureStore, SnapshotStoreType, SnapshotUploaderType, VerificationKeyStore,
271+
SingleSignatureStore, SnapshotUploaderType, VerificationKeyStore,
272272
};
273273
use mithril_common::{
274274
certificate_chain::MithrilCertificateVerifier,
@@ -301,7 +301,6 @@ pub mod tests {
301301
protocol_parameters: fake_data::protocol_parameters(),
302302
url_snapshot_manifest: "https://storage.googleapis.com/cardano-testnet/snapshots.json"
303303
.to_string(),
304-
snapshot_store_type: SnapshotStoreType::Local,
305304
snapshot_uploader_type: SnapshotUploaderType::Local,
306305
snapshot_bucket_name: None,
307306
server_ip: "0.0.0.0".to_string(),

mithril-aggregator/src/lib.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,10 @@ mod store;
3030
mod tools;
3131

3232
pub use crate::configuration::{
33-
Configuration, DefaultConfiguration, GenesisConfiguration, SnapshotStoreType,
34-
SnapshotUploaderType,
33+
Configuration, DefaultConfiguration, GenesisConfiguration, SnapshotUploaderType,
3534
};
3635
pub use crate::multi_signer::{MultiSigner, MultiSignerImpl, ProtocolError};
37-
pub use crate::snapshot_stores::{LocalSnapshotStore, RemoteSnapshotStore, SnapshotStore};
36+
pub use crate::snapshot_stores::{LocalSnapshotStore, SnapshotStore};
3837
pub use certificate_creator::{CertificateCreator, MithrilCertificateCreator};
3938
pub use command_args::MainOpts;
4039
pub use dependency::DependencyManager;

mithril-aggregator/src/snapshot_stores/mod.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
mod local_snapshot_store;
2-
mod remote_snapshot_store;
32
mod snapshot_store;
43

54
pub use local_snapshot_store::LocalSnapshotStore;
6-
pub use remote_snapshot_store::RemoteSnapshotStore;
75
pub use snapshot_store::SnapshotStore;
86
pub use snapshot_store::SnapshotStoreError;
97

0 commit comments

Comments
 (0)