Skip to content

Commit d105c6c

Browse files
committed
fix(cardano-chain-follower): move mmap file to cat-types
Signed-off-by: bkioshn <[email protected]>
1 parent 6b68b3c commit d105c6c

File tree

10 files changed

+219
-220
lines changed

10 files changed

+219
-220
lines changed

.config/dictionaries/project.dic

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ maindbname
141141
mapref
142142
mdlint
143143
mdns
144+
MEMMAP
144145
memx
145146
Metadatum
146147
mgrybyk

rust/cardano-chain-follower/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ mithril-client = { version = "0.10.4", default-features = false, features = [
2020
"num-integer-backend",
2121
] }
2222
cardano-blockchain-types = { version = "0.0.1", git = "https://github.com/input-output-hk/catalyst-libs.git", tag = "r20250114-00" }
23-
catalyst-types = { version = "0.0.1", git = "https://github.com/input-output-hk/catalyst-libs.git", tag = "r20250108-00" }
23+
catalyst-types = { version = "0.0.1", path = "../catalyst-types" }
2424

2525
thiserror = "1.0.69"
2626
tokio = { version = "1.42.0", features = [

rust/cardano-chain-follower/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ mod mithril_snapshot_data;
1414
mod mithril_snapshot_iterator;
1515
mod mithril_snapshot_sync;
1616
mod mithril_turbo_downloader;
17-
mod mmap_file;
1817
mod snapshot_id;
1918
mod stats;
2019
pub mod turbo_downloader;

rust/cardano-chain-follower/src/mithril_turbo_downloader.rs

Lines changed: 19 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use std::{
1414

1515
use anyhow::{anyhow, bail};
1616
use async_trait::async_trait;
17-
use catalyst_types::conversion::from_saturating;
17+
use catalyst_types::{conversion::from_saturating, mmap_file::MemoryMapFile};
1818
use dashmap::DashSet;
1919
use memx::memcmp;
2020
use mithril_client::{
@@ -28,7 +28,6 @@ use zstd::Decoder;
2828
use crate::{
2929
mithril_snapshot_config::MithrilSnapshotConfig,
3030
mithril_snapshot_data::latest_mithril_snapshot_data,
31-
mmap_file::MemoryMapFile,
3231
stats::{self},
3332
turbo_downloader::ParallelDownloadProcessor,
3433
};
@@ -134,7 +133,8 @@ impl Inner {
134133
self.ext_size.fetch_add(entry_size, Ordering::SeqCst);
135134

136135
// Try and deduplicate the file if we can, otherwise just extract it.
137-
if let Ok(prev_mmap) = self.can_deduplicate(&rel_file, entry_size, prev_file.as_ref()) {
136+
if let Ok(prev_mmap) = Self::can_deduplicate(&rel_file, entry_size, prev_file.as_ref())
137+
{
138138
let expected_file_size = from_saturating(entry_size);
139139
let mut buf: Vec<u8> = Vec::with_capacity(expected_file_size);
140140
if entry.read_to_end(&mut buf)? != expected_file_size {
@@ -145,31 +145,20 @@ impl Inner {
145145
buf.len()
146146
);
147147
}
148+
// Got the full file and its the expected size. Is it different?
149+
if memcmp(prev_mmap.file_as_slice(), buf.as_slice()) == cmp::Ordering::Equal {
150+
// Same so lets Hardlink it, and throw away the temp buffer.
148151

149-
let mut prev_mmap_option = Some(prev_mmap);
150-
// Take the value inside the option out
151-
if let Some(prev_mmap) = prev_mmap_option.take() {
152-
if memcmp(prev_mmap.file_as_slice(), buf.as_slice()) == cmp::Ordering::Equal {
153-
// Same so let's hardlink it, and throw away the temp buffer.
154-
// Make sure our big mmap get dropped.
155-
drop(prev_mmap);
156-
stats::set_mmap_drop(self.cfg.chain);
157-
158-
// File is the same, so dedup it.
159-
if self.cfg.dedup_tmp(&abs_file, &latest_snapshot).is_ok() {
160-
self.dedup_size.fetch_add(entry_size, Ordering::SeqCst);
161-
changed_file!(self, rel_file, abs_file, entry_size);
162-
drop(buf);
163-
continue;
164-
}
165-
}
166-
}
167-
168-
// If the `prev_mmap` is not yet drop, drop it now.
169-
// Need to do this way because drop is moved into the if block.
170-
if let Some(prev_mmap) = prev_mmap_option {
152+
// Make sure our big mmap get dropped.
171153
drop(prev_mmap);
172-
stats::set_mmap_drop(self.cfg.chain);
154+
155+
// File is the same, so dedup it.
156+
if self.cfg.dedup_tmp(&abs_file, &latest_snapshot).is_ok() {
157+
self.dedup_size.fetch_add(entry_size, Ordering::SeqCst);
158+
changed_file!(self, rel_file, abs_file, entry_size);
159+
drop(buf);
160+
continue;
161+
}
173162
}
174163

175164
if let Err(error) = std::fs::write(&abs_file, buf) {
@@ -233,7 +222,7 @@ impl Inner {
233222

234223
/// Check if a given path from the archive is able to be deduplicated.
235224
fn can_deduplicate(
236-
&self, rel_file: &Path, file_size: u64, prev_file: Option<&PathBuf>,
225+
rel_file: &Path, file_size: u64, prev_file: Option<&PathBuf>,
237226
) -> MithrilResult<MemoryMapFile> {
238227
// Can't dedup if the current file is not de-dupable (must be immutable)
239228
if rel_file.starts_with("immutable") {
@@ -243,7 +232,7 @@ impl Inner {
243232
// If the current file is not exactly the same as the previous file size, we
244233
// can't dedup.
245234
if file_size == current_size {
246-
if let Ok(pref_file_loaded) = self.mmap_open_sync(prev_file) {
235+
if let Ok(pref_file_loaded) = Self::mmap_open_sync(prev_file) {
247236
if pref_file_loaded.size() == file_size {
248237
return Ok(pref_file_loaded);
249238
}
@@ -256,12 +245,9 @@ impl Inner {
256245
}
257246

258247
/// Open a file using mmap for performance.
259-
fn mmap_open_sync(&self, path: &Path) -> MithrilResult<MemoryMapFile> {
248+
fn mmap_open_sync(path: &Path) -> MithrilResult<MemoryMapFile> {
260249
match MemoryMapFile::try_from(path) {
261-
Ok(mmap_file) => {
262-
stats::update_mmap_count_and_size(self.cfg.chain, mmap_file.size());
263-
Ok(mmap_file)
264-
},
250+
Ok(mmap_file) => Ok(mmap_file),
265251
Err(error) => {
266252
error!(error=%error, file=%path.to_string_lossy(), "Failed to open file");
267253
Err(error.into())

rust/cardano-chain-follower/src/mmap_file.rs

Lines changed: 0 additions & 45 deletions
This file was deleted.

rust/cardano-chain-follower/src/stats/mmap_file.rs

Lines changed: 0 additions & 88 deletions
This file was deleted.

rust/cardano-chain-follower/src/stats/mod.rs

Lines changed: 1 addition & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,15 @@
33
pub(crate) mod follower;
44
pub(crate) mod live_chain;
55
pub(crate) mod mithril;
6-
pub(crate) mod mmap_file;
76
pub(crate) mod rollback;
87
pub(crate) mod thread;
98

109
use std::sync::{Arc, LazyLock, RwLock};
1110

1211
use cardano_blockchain_types::{Network, Slot};
12+
use catalyst_types::mmap_file::MemMapFileStat;
1313
use chrono::Utc;
1414
use dashmap::DashMap;
15-
use mmap_file::MemMapFileStat;
1615
use rollback::{rollbacks, rollbacks_reset, RollbackType};
1716
use serde::Serialize;
1817
use strum::IntoEnumIterator;
@@ -575,56 +574,6 @@ pub fn thread_stat_name(chain: Network) -> Vec<String> {
575574
.collect()
576575
}
577576

578-
// ----------------- MMap File STATISTICS-------------------
579-
580-
/// Update the mmap file counter and total size.
581-
pub(crate) fn update_mmap_count_and_size(chain: Network, size: u64) {
582-
// This will actually always succeed.
583-
let Some(stats) = lookup_stats(chain) else {
584-
return;
585-
};
586-
587-
let Ok(chain_stats) = stats.write() else {
588-
// Worst case if this fails (it never should) is we stop updating stats.
589-
error!("Stats RwLock should never be able to error.");
590-
return;
591-
};
592-
593-
chain_stats.mmap_file_stat.incr_file_counter();
594-
chain_stats.mmap_file_stat.update_total_size(size);
595-
}
596-
597-
/// Set the mmap file to be dropped.
598-
pub(crate) fn set_mmap_drop(chain: Network) {
599-
// This will actually always succeed.
600-
let Some(stats) = lookup_stats(chain) else {
601-
return;
602-
};
603-
604-
let Ok(chain_stats) = stats.write() else {
605-
// Worst case if this fails (it never should) is we stop updating stats.
606-
error!("Stats RwLock should never be able to error.");
607-
return;
608-
};
609-
610-
chain_stats.mmap_file_stat.set_is_drop();
611-
}
612-
613-
/// Get the mmap file statistic.
614-
#[allow(dead_code)]
615-
pub(crate) fn mmap_file_stat(chain: Network) -> Option<MemMapFileStat> {
616-
// This will actually always succeed.
617-
let stats = lookup_stats(chain)?;
618-
619-
let Ok(chain_stats) = stats.write() else {
620-
// Worst case if this fails (it never should) is we stop updating stats.
621-
error!("Stats RwLock should never be able to error.");
622-
return None;
623-
};
624-
625-
Some(chain_stats.mmap_file_stat.clone())
626-
}
627-
628577
#[cfg(test)]
629578
#[allow(clippy::unwrap_used)]
630579
mod tests {

rust/catalyst-types/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ serde = { version = "1.0.217", features = ["derive"] }
3030
thiserror = "2.0.9"
3131
base64-url = "3.0.0"
3232
uuid = { version = "1.11.0", features = ["v4", "v7", "serde"] }
33+
fmmap = { version = "0.3.3", features = ["sync", "tokio-async"] }
34+
once_cell = "1.20.2"
3335

3436
[dev-dependencies]
3537
ed25519-dalek = { version = "2.1.1", features = ["rand_core"] }

rust/catalyst-types/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ pub mod hashes;
55
pub mod kid_uri;
66
pub mod problem_report;
77
pub mod uuid;
8+
pub mod mmap_file;

0 commit comments

Comments
 (0)