Skip to content

Commit f8dc2f2

Browse files
Alenardlachaume
andcommitted
refactor(aggregator): make cardano database builder reuse size computed from its sub-builders
Instead of computing them itself, lowering its responsabilities. Also for digests and ancillary the size computed alongside the snaspshot is re-used instead of re-doing the computation. Co-authored-by: Damien Lachaume <[email protected]>
1 parent fa5f1eb commit f8dc2f2

File tree

5 files changed

+133
-211
lines changed

5 files changed

+133
-211
lines changed

mithril-aggregator/src/artifact_builder/cardano_database.rs

Lines changed: 26 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::{path::PathBuf, sync::Arc};
1+
use std::sync::Arc;
22

33
use anyhow::{anyhow, Context};
44
use async_trait::async_trait;
@@ -14,13 +14,11 @@ use mithril_common::{
1414
};
1515

1616
use crate::artifact_builder::{AncillaryArtifactBuilder, ArtifactBuilder};
17-
use crate::tools::file_size;
1817

1918
use super::{DigestArtifactBuilder, ImmutableArtifactBuilder};
2019

2120
pub struct CardanoDatabaseArtifactBuilder {
2221
network: CardanoNetwork,
23-
db_directory: PathBuf,
2422
cardano_node_version: Version,
2523
ancillary_builder: Arc<AncillaryArtifactBuilder>,
2624
immutable_builder: Arc<ImmutableArtifactBuilder>,
@@ -30,15 +28,13 @@ pub struct CardanoDatabaseArtifactBuilder {
3028
impl CardanoDatabaseArtifactBuilder {
3129
pub fn new(
3230
network: CardanoNetwork,
33-
db_directory: PathBuf,
3431
cardano_node_version: &Version,
3532
ancillary_builder: Arc<AncillaryArtifactBuilder>,
3633
immutable_builder: Arc<ImmutableArtifactBuilder>,
3734
digest_builder: Arc<DigestArtifactBuilder>,
3835
) -> Self {
3936
Self {
4037
network,
41-
db_directory,
4238
cardano_node_version: cardano_node_version.clone(),
4339
ancillary_builder,
4440
immutable_builder,
@@ -66,55 +62,27 @@ impl ArtifactBuilder<CardanoDbBeacon, CardanoDatabaseSnapshot> for CardanoDataba
6662
SignedEntityType::CardanoDatabase(beacon.clone())
6763
)
6864
})?;
69-
let total_db_size_uncompressed = {
70-
let db_directory = self.db_directory.clone();
71-
tokio::task::spawn_blocking(move || -> StdResult<u64> {
72-
file_size::compute_size_of_path(&db_directory)
73-
})
74-
.await??
75-
};
76-
77-
let ancillary_locations = self.ancillary_builder.upload(&beacon).await?;
78-
let ancillary_builder = self.ancillary_builder.clone();
79-
let ancillary_size = {
80-
let db_directory = self.db_directory.clone();
81-
let beacon = beacon.clone();
82-
tokio::task::spawn_blocking(move || -> StdResult<u64> {
83-
ancillary_builder.compute_uncompressed_size(&db_directory, &beacon)
84-
})
85-
.await??
86-
};
8765

88-
let immutables_locations = self
66+
let ancillary_upload = self.ancillary_builder.upload(&beacon).await?;
67+
let immutables_upload = self
8968
.immutable_builder
9069
.upload(beacon.immutable_file_number)
9170
.await?;
92-
let immutable_average_size = {
93-
let db_directory = self.db_directory.clone();
94-
let immutable_file_number = beacon.immutable_file_number;
95-
let immutable_builder = self.immutable_builder.clone();
96-
tokio::task::spawn_blocking(move || -> StdResult<u64> {
97-
immutable_builder
98-
.compute_average_uncompressed_size(&db_directory, immutable_file_number)
99-
})
100-
.await??
101-
};
102-
10371
let digest_upload = self.digest_builder.upload(&beacon).await?;
10472

10573
let content = CardanoDatabaseSnapshotArtifactData {
106-
total_db_size_uncompressed,
74+
total_db_size_uncompressed: ancillary_upload.size + immutables_upload.total_size,
10775
digests: DigestsLocations {
10876
size_uncompressed: digest_upload.size,
10977
locations: digest_upload.locations,
11078
},
11179
immutables: ImmutablesLocations {
112-
average_size_uncompressed: immutable_average_size,
113-
locations: immutables_locations,
80+
average_size_uncompressed: immutables_upload.average_size,
81+
locations: immutables_upload.locations,
11482
},
11583
ancillary: AncillaryLocations {
116-
size_uncompressed: ancillary_size,
117-
locations: ancillary_locations,
84+
size_uncompressed: ancillary_upload.size,
85+
locations: ancillary_upload.locations,
11886
},
11987
};
12088

@@ -145,45 +113,22 @@ mod tests {
145113
};
146114
use mockall::{predicate, Predicate};
147115

148-
use super::*;
149-
use crate::tools::file_archiver::FileArchiver;
150116
use crate::{
151117
artifact_builder::{
152118
DigestSnapshotter, MockAncillaryFileUploader, MockImmutableFilesUploader,
153119
},
154120
immutable_file_digest_mapper::MockImmutableFileDigestMapper,
155-
services::FakeSnapshotter,
121+
services::CompressedArchiveSnapshotter,
156122
test_tools::TestLogger,
157-
tools::url_sanitizer::SanitizedUrlWithTrailingSlash,
123+
tools::{file_archiver::FileArchiver, url_sanitizer::SanitizedUrlWithTrailingSlash},
158124
};
159125

126+
use super::*;
127+
160128
fn get_test_directory(dir_name: &str) -> PathBuf {
161129
TempDir::create("cardano_database", dir_name)
162130
}
163131

164-
#[test]
165-
fn should_compute_the_size_of_the_uncompressed_database_only_immutable_ledger_and_volatile() {
166-
let test_dir = get_test_directory("should_compute_the_size_of_the_uncompressed_database_only_immutable_ledger_and_volatile");
167-
168-
let immutable_trio_file_size = 777;
169-
let ledger_file_size = 6666;
170-
let volatile_file_size = 99;
171-
DummyCardanoDbBuilder::new(test_dir.as_os_str().to_str().unwrap())
172-
.with_immutables(&[1, 2])
173-
.set_immutable_trio_file_size(immutable_trio_file_size)
174-
.with_ledger_files(&["437", "537", "637", "737"])
175-
.set_ledger_file_size(ledger_file_size)
176-
.with_volatile_files(&["blocks-0.dat", "blocks-1.dat", "blocks-2.dat"])
177-
.set_volatile_file_size(volatile_file_size)
178-
.build();
179-
let expected_total_size =
180-
(2 * immutable_trio_file_size) + (4 * ledger_file_size) + (3 * volatile_file_size);
181-
182-
let total_size = file_size::compute_size_of_path(&test_dir).unwrap();
183-
184-
assert_eq!(expected_total_size, total_size);
185-
}
186-
187132
#[tokio::test]
188133
async fn should_compute_valid_artifact() {
189134
let test_dir = get_test_directory("should_compute_valid_artifact");
@@ -203,12 +148,19 @@ mod tests {
203148
.set_volatile_file_size(volatile_file_size)
204149
.build();
205150
let expected_average_immutable_size = immutable_trio_file_size;
206-
let expected_ancillary_size =
207-
immutable_trio_file_size + ledger_file_size + volatile_file_size;
208-
let expected_total_size =
209-
4 * immutable_trio_file_size + ledger_file_size + volatile_file_size;
210-
211-
let snapshotter = Arc::new(FakeSnapshotter::new(test_dir.join("fake_snapshots")));
151+
let expected_ancillary_size = immutable_trio_file_size + ledger_file_size;
152+
let expected_total_size = 4 * immutable_trio_file_size + ledger_file_size;
153+
154+
let snapshotter = Arc::new(
155+
CompressedArchiveSnapshotter::new(
156+
cardano_db.get_dir().to_path_buf(),
157+
test_dir.join("ongoing_snapshots"),
158+
CompressionAlgorithm::Gzip,
159+
Arc::new(FileArchiver::new_for_test(test_dir.join("verification"))),
160+
TestLogger::stdout(),
161+
)
162+
.unwrap(),
163+
);
212164

213165
let ancillary_artifact_builder = {
214166
let mut ancillary_uploader = MockAncillaryFileUploader::new();
@@ -251,7 +203,7 @@ mod tests {
251203
});
252204

253205
ImmutableArtifactBuilder::new(
254-
test_dir.join("immutable"),
206+
cardano_db.get_immutable_dir().to_path_buf(),
255207
vec![Arc::new(immutable_uploader)],
256208
snapshotter,
257209
TestLogger::stdout(),
@@ -286,7 +238,6 @@ mod tests {
286238

287239
let cardano_database_artifact_builder = CardanoDatabaseArtifactBuilder::new(
288240
network,
289-
cardano_db.get_dir().to_owned(),
290241
&Version::parse("1.0.0").unwrap(),
291242
Arc::new(ancillary_artifact_builder),
292243
Arc::new(immutable_artifact_builder),

0 commit comments

Comments
 (0)