Skip to content

Commit

Permalink
Removed weird ProviderError variants (#14518)
Browse files Browse the repository at this point in the history
  • Loading branch information
PoulavBhowmick03 authored Feb 15, 2025
1 parent b7f173f commit 07e8360
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 45 deletions.
5 changes: 1 addition & 4 deletions crates/storage/db/src/static_file/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@ type ColumnResult<T> = ProviderResult<Option<T>>;
impl<'a> StaticFileCursor<'a> {
/// Returns a new [`StaticFileCursor`].
pub fn new(jar: &'a NippyJar<SegmentHeader>, reader: Arc<DataReader>) -> ProviderResult<Self> {
Ok(Self(
NippyJarCursor::with_reader(jar, reader)
.map_err(|err| ProviderError::NippyJar(err.to_string()))?,
))
Ok(Self(NippyJarCursor::with_reader(jar, reader).map_err(ProviderError::other)?))
}

/// Returns the current `BlockNumber` or `TxNumber` of the cursor depending on the kind of
Expand Down
19 changes: 16 additions & 3 deletions crates/storage/errors/src/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@ pub enum ProviderError {
/// RLP error.
#[error("{_0}")]
Rlp(alloy_rlp::Error),
/// Nippy jar error.
#[error("nippy jar error: {_0}")]
NippyJar(String),
/// Trie witness error.
#[error("trie witness error: {_0}")]
TrieWitnessError(String),
Expand Down Expand Up @@ -204,6 +201,22 @@ pub struct RootMismatch {
pub block_hash: BlockHash,
}

/// A Static File Write Error.
#[derive(Debug, thiserror::Error)]
#[error("{message}")]
pub struct StaticFileWriterError {
/// The error message.
pub message: String,
}

impl StaticFileWriterError {
/// Creates a new [`StaticFileWriterError`] with the given message.
#[allow(dead_code)]
pub fn new(message: impl Into<String>) -> Self {
Self { message: message.into() }
}
}

/// Consistent database view error.
#[derive(Clone, Debug, PartialEq, Eq, Display)]
pub enum ConsistentViewError {
Expand Down
22 changes: 8 additions & 14 deletions crates/storage/provider/src/providers/static_file/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,8 +303,7 @@ impl<N: NodePrimitives> StaticFileProvider<N> {
pub fn report_metrics(&self) -> ProviderResult<()> {
let Some(metrics) = &self.metrics else { return Ok(()) };

let static_files =
iter_static_files(&self.path).map_err(|e| ProviderError::NippyJar(e.to_string()))?;
let static_files = iter_static_files(&self.path).map_err(ProviderError::other)?;
for (segment, ranges) in static_files {
let mut entries = 0;
let mut size = 0;
Expand Down Expand Up @@ -429,10 +428,10 @@ impl<N: NodePrimitives> StaticFileProvider<N> {
jar.jar
} else {
NippyJar::<SegmentHeader>::load(&self.path.join(segment.filename(&fixed_block_range)))
.map_err(|e| ProviderError::NippyJar(e.to_string()))?
.map_err(ProviderError::other)?
};

jar.delete().map_err(|e| ProviderError::NippyJar(e.to_string()))?;
jar.delete().map_err(ProviderError::other)?;

let mut segment_max_block = None;
if fixed_block_range.start() > 0 {
Expand Down Expand Up @@ -461,7 +460,7 @@ impl<N: NodePrimitives> StaticFileProvider<N> {
} else {
trace!(target: "provider::static_file", ?segment, ?fixed_block_range, "Creating jar from scratch");
let path = self.path.join(segment.filename(fixed_block_range));
let jar = NippyJar::load(&path).map_err(|e| ProviderError::NippyJar(e.to_string()))?;
let jar = NippyJar::load(&path).map_err(ProviderError::other)?;
self.map.entry(key).insert(LoadedJar::new(jar)?).downgrade().into()
};

Expand Down Expand Up @@ -535,7 +534,7 @@ impl<N: NodePrimitives> StaticFileProvider<N> {
let jar = NippyJar::<SegmentHeader>::load(
&self.path.join(segment.filename(&fixed_range)),
)
.map_err(|e| ProviderError::NippyJar(e.to_string()))?;
.map_err(ProviderError::other)?;

// Updates the tx index by first removing all entries which have a higher
// block_start than our current static file.
Expand Down Expand Up @@ -600,9 +599,7 @@ impl<N: NodePrimitives> StaticFileProvider<N> {
max_block.clear();
tx_index.clear();

for (segment, ranges) in
iter_static_files(&self.path).map_err(|e| ProviderError::NippyJar(e.to_string()))?
{
for (segment, ranges) in iter_static_files(&self.path).map_err(ProviderError::other)? {
// Update last block for each segment
if let Some((block_range, _)) = ranges.last() {
max_block.insert(segment, block_range.end());
Expand Down Expand Up @@ -819,12 +816,9 @@ impl<N: NodePrimitives> StaticFileProvider<N> {
let file_path =
self.directory().join(segment.filename(&self.find_fixed_range(latest_block)));

let jar = NippyJar::<SegmentHeader>::load(&file_path)
.map_err(|e| ProviderError::NippyJar(e.to_string()))?;
let jar = NippyJar::<SegmentHeader>::load(&file_path).map_err(ProviderError::other)?;

NippyJarChecker::new(jar)
.check_consistency()
.map_err(|e| ProviderError::NippyJar(e.to_string()))?;
NippyJarChecker::new(jar).check_consistency().map_err(ProviderError::other)?;
}
Ok(())
}
Expand Down
2 changes: 1 addition & 1 deletion crates/storage/provider/src/providers/static_file/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ impl LoadedJar {
let mmap_handle = Arc::new(data_reader);
Ok(Self { jar, mmap_handle })
}
Err(e) => Err(ProviderError::NippyJar(e.to_string())),
Err(e) => Err(ProviderError::other(e)),
}
}

Expand Down
37 changes: 14 additions & 23 deletions crates/storage/provider/src/providers/static_file/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use reth_primitives::{
static_file::{SegmentHeader, SegmentRangeInclusive},
StaticFileSegment,
};
use reth_storage_errors::provider::{ProviderError, ProviderResult};
use reth_storage_errors::provider::{ProviderError, ProviderResult, StaticFileWriterError};
use std::{
borrow::Borrow,
fmt::Debug,
Expand Down Expand Up @@ -163,8 +163,7 @@ impl<N: NodePrimitives> StaticFileProviderRW<N> {
None,
) {
Ok(provider) => (
NippyJar::load(provider.data_path())
.map_err(|e| ProviderError::NippyJar(e.to_string()))?,
NippyJar::load(provider.data_path()).map_err(ProviderError::other)?,
provider.data_path().into(),
),
Err(ProviderError::MissingStaticFileBlock(_, _)) => {
Expand All @@ -180,7 +179,7 @@ impl<N: NodePrimitives> StaticFileProviderRW<N> {
// This static file has been frozen, so we should
Err(ProviderError::FinalizedStaticFile(segment, block))
}
Err(e) => Err(ProviderError::NippyJar(e.to_string())),
Err(e) => Err(ProviderError::other(e)),
}?;

if let Some(metrics) = &metrics {
Expand Down Expand Up @@ -214,7 +213,7 @@ impl<N: NodePrimitives> StaticFileProviderRW<N> {
self.user_header_mut().prune(pruned_rows);
}

self.writer.commit().map_err(|error| ProviderError::NippyJar(error.to_string()))?;
self.writer.commit().map_err(ProviderError::other)?;

// Updates the [SnapshotProvider] manager
self.update_index()?;
Expand All @@ -240,7 +239,7 @@ impl<N: NodePrimitives> StaticFileProviderRW<N> {

if self.writer.is_dirty() {
// Commits offsets and new user_header to disk
self.writer.commit().map_err(|e| ProviderError::NippyJar(e.to_string()))?;
self.writer.commit().map_err(ProviderError::other)?;

if let Some(metrics) = &self.metrics {
metrics.record_segment_operation(
Expand Down Expand Up @@ -272,9 +271,7 @@ impl<N: NodePrimitives> StaticFileProviderRW<N> {
let start = Instant::now();

// Commits offsets and new user_header to disk
self.writer
.commit_without_sync_all()
.map_err(|e| ProviderError::NippyJar(e.to_string()))?;
self.writer.commit_without_sync_all().map_err(ProviderError::other)?;

if let Some(metrics) = &self.metrics {
metrics.record_segment_operation(
Expand Down Expand Up @@ -421,9 +418,7 @@ impl<N: NodePrimitives> StaticFileProviderRW<N> {
} else {
// Update `SegmentHeader`
self.writer.user_header_mut().prune(len);
self.writer
.prune_rows(len as usize)
.map_err(|e| ProviderError::NippyJar(e.to_string()))?;
self.writer.prune_rows(len as usize).map_err(ProviderError::other)?;
break
}

Expand All @@ -433,9 +428,7 @@ impl<N: NodePrimitives> StaticFileProviderRW<N> {
self.writer.user_header_mut().prune(remaining_rows);

// Truncate data
self.writer
.prune_rows(remaining_rows as usize)
.map_err(|e| ProviderError::NippyJar(e.to_string()))?;
self.writer.prune_rows(remaining_rows as usize).map_err(ProviderError::other)?;
remaining_rows = 0;
}
}
Expand Down Expand Up @@ -476,9 +469,9 @@ impl<N: NodePrimitives> StaticFileProviderRW<N> {
self.writer.set_dirty();
self.data_path = data_path;
NippyJar::<SegmentHeader>::load(&current_path)
.map_err(|e| ProviderError::NippyJar(e.to_string()))?
.map_err(ProviderError::other)?
.delete()
.map_err(|e| ProviderError::NippyJar(e.to_string()))?;
.map_err(ProviderError::other)?;
Ok(())
}

Expand All @@ -487,9 +480,7 @@ impl<N: NodePrimitives> StaticFileProviderRW<N> {
self.buf.clear();
column.to_compact(&mut self.buf);

self.writer
.append_column(Some(Ok(&self.buf)))
.map_err(|e| ProviderError::NippyJar(e.to_string()))?;
self.writer.append_column(Some(Ok(&self.buf))).map_err(ProviderError::other)?;
Ok(())
}

Expand Down Expand Up @@ -762,9 +753,9 @@ impl<N: NodePrimitives> StaticFileProviderRW<N> {
/// Returns Error if there is a pruning instruction that needs to be applied.
fn ensure_no_queued_prune(&self) -> ProviderResult<()> {
if self.prune_on_commit.is_some() {
return Err(ProviderError::NippyJar(
"Pruning should be committed before appending or pruning more data".to_string(),
))
return Err(ProviderError::other(StaticFileWriterError::new(
"Pruning should be committed before appending or pruning more data",
)));
}
Ok(())
}
Expand Down

0 comments on commit 07e8360

Please sign in to comment.