diff --git a/Cargo.lock b/Cargo.lock index 38828faaaf..65039d5370 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -10204,7 +10204,6 @@ dependencies = [ "reth-ethereum-engine-primitives", "reth-ethereum-primitives", "reth-execution-types", - "reth-fs-util", "reth-metrics", "reth-nippy-jar", "reth-node-types", diff --git a/crates/storage/db/src/static_file/mod.rs b/crates/storage/db/src/static_file/mod.rs index 270a0f0e2a..46357b69d1 100644 --- a/crates/storage/db/src/static_file/mod.rs +++ b/crates/storage/db/src/static_file/mod.rs @@ -1,6 +1,7 @@ //! reth's static file database table import and access -use std::{collections::HashMap, path::Path}; +use alloy_primitives::map::HashMap; +use std::path::Path; mod cursor; pub use cursor::StaticFileCursor; @@ -44,8 +45,8 @@ pub fn iter_static_files(path: &Path) -> Result NippyJar { let config_path = path.with_extension(CONFIG_FILE_EXTENSION); let config_file = File::open(&config_path) .inspect_err(|e| { - warn!( ?path, %e, "Failed to load static file jar"); + warn!(?path, %e, "Failed to load static file jar"); }) .map_err(|err| reth_fs_util::FsPathError::open(err, config_path))?; - let mut obj = Self::load_from_reader(config_file)?; + let mut obj = Self::load_from_reader(io::BufReader::new(config_file))?; obj.path = path.to_path_buf(); Ok(obj) } @@ -418,10 +418,15 @@ impl DataReader { &self.data_mmap[range] } - /// Returns total size of data + /// Returns total size of data file. pub fn size(&self) -> usize { self.data_mmap.len() } + + /// Returns total size of offsets file. + pub fn offsets_size(&self) -> usize { + self.offset_mmap.len() + } } #[cfg(test)] diff --git a/crates/storage/provider/Cargo.toml b/crates/storage/provider/Cargo.toml index 22520d82f8..0199b6d2fc 100644 --- a/crates/storage/provider/Cargo.toml +++ b/crates/storage/provider/Cargo.toml @@ -17,7 +17,6 @@ reth-chainspec.workspace = true reth-execution-types.workspace = true reth-ethereum-primitives = { workspace = true, features = ["reth-codec"] } reth-primitives-traits = { workspace = true, features = ["reth-codec", "secp256k1"] } -reth-fs-util.workspace = true reth-errors.workspace = true reth-storage-errors.workspace = true reth-storage-api = { workspace = true, features = ["std", "db-api"] } diff --git a/crates/storage/provider/src/providers/static_file/jar.rs b/crates/storage/provider/src/providers/static_file/jar.rs index 5548cb5c1f..04809742c8 100644 --- a/crates/storage/provider/src/providers/static_file/jar.rs +++ b/crates/storage/provider/src/providers/static_file/jar.rs @@ -85,6 +85,11 @@ impl<'a, N: NodePrimitives> StaticFileJarProvider<'a, N> { self.metrics = Some(metrics); self } + + /// Returns the total size of the data and offsets files (from the in-memory mmap). + pub fn size(&self) -> usize { + self.jar.value().size() + } } impl> HeaderProvider for StaticFileJarProvider<'_, N> { diff --git a/crates/storage/provider/src/providers/static_file/manager.rs b/crates/storage/provider/src/providers/static_file/manager.rs index 17c5df8245..9ce7a6e372 100644 --- a/crates/storage/provider/src/providers/static_file/manager.rs +++ b/crates/storage/provider/src/providers/static_file/manager.rs @@ -474,6 +474,9 @@ impl StaticFileProviderInner { impl StaticFileProvider { /// Reports metrics for the static files. + /// + /// This uses the in-memory index to get file sizes from mmap handles instead of reading + /// filesystem metadata. pub fn report_metrics(&self) -> ProviderResult<()> { let Some(metrics) = &self.metrics else { return Ok(()) }; @@ -491,29 +494,7 @@ impl StaticFileProvider { })?; entries += jar_provider.rows(); - - let data_path = jar_provider.data_path().to_path_buf(); - let index_path = jar_provider.index_path(); - let offsets_path = jar_provider.offsets_path(); - let config_path = jar_provider.config_path(); - - // can release jar early - drop(jar_provider); - - let data_size = reth_fs_util::metadata(data_path) - .map(|metadata| metadata.len()) - .unwrap_or_default(); - let index_size = reth_fs_util::metadata(index_path) - .map(|metadata| metadata.len()) - .unwrap_or_default(); - let offsets_size = reth_fs_util::metadata(offsets_path) - .map(|metadata| metadata.len()) - .unwrap_or_default(); - let config_size = reth_fs_util::metadata(config_path) - .map(|metadata| metadata.len()) - .unwrap_or_default(); - - size += data_size + index_size + offsets_size + config_size; + size += jar_provider.size() as u64; } metrics.record_segment(segment, size, headers.len(), entries); diff --git a/crates/storage/provider/src/providers/static_file/mod.rs b/crates/storage/provider/src/providers/static_file/mod.rs index 356238d288..a20dd6a3ff 100644 --- a/crates/storage/provider/src/providers/static_file/mod.rs +++ b/crates/storage/provider/src/providers/static_file/mod.rs @@ -44,6 +44,11 @@ impl LoadedJar { const fn segment(&self) -> StaticFileSegment { self.jar.user_header().segment() } + + /// Returns the total size of the data and offsets files (from the in-memory mmap). + fn size(&self) -> usize { + self.mmap_handle.size() + self.mmap_handle.offsets_size() + } } impl Deref for LoadedJar {