Skip to content

Commit

Permalink
Use strum_macros to automate generation of error reasons
Browse files Browse the repository at this point in the history
Signed-off-by: Simon Beal <[email protected]>
  • Loading branch information
muddyfish committed Dec 6, 2024
1 parent 3705db8 commit 9766e08
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 19 deletions.
16 changes: 15 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions mountpoint-s3/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }
async-stream = "0.3.6"
humansize = "2.1.3"
base64ct = "1.6.0"
strum_macros = "0.26.4"

[target.'cfg(target_os = "linux")'.dependencies]
procfs = { version = "0.17.0", default-features = false }
Expand Down
13 changes: 4 additions & 9 deletions mountpoint-s3/src/data_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ mod in_memory_data_cache;
mod multilevel_cache;

use async_trait::async_trait;
use strum_macros::IntoStaticStr;
use thiserror::Error;

pub use crate::checksums::ChecksummedBytes;
Expand All @@ -26,7 +27,8 @@ use crate::object::ObjectId;
pub type BlockIndex = u64;

/// Errors returned by operations on a [DataCache]
#[derive(Debug, Error)]
#[derive(Debug, Error, IntoStaticStr)]
#[strum(serialize_all = "snake_case")]
pub enum DataCacheError {
#[error("IO error when reading or writing from cache: {0}")]
IoFailure(#[source] anyhow::Error),
Expand All @@ -44,14 +46,7 @@ pub enum DataCacheError {

impl DataCacheError {
fn get_reason(&self) -> &'static str {
match self {
DataCacheError::IoFailure(_) => "io_failure",
DataCacheError::InvalidBlockHeader(_) => "invalid_block_header",
DataCacheError::InvalidBlockChecksum => "invalid_block_checksum",
DataCacheError::InvalidBlockContent => "invalid_block_content",
DataCacheError::InvalidBlockOffset => "invalid_block_offset",
DataCacheError::EvictionFailure => "eviction_failure",
}
self.into()
}
}

Expand Down
16 changes: 7 additions & 9 deletions mountpoint-s3/src/data_cache/express_data_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,12 +174,10 @@ where
let crc32c_b64 = checksum
.checksum_crc32c
.ok_or_else(|| DataCacheError::InvalidBlockChecksum)?;
let crc32c = crc32c_from_base64(&crc32c_b64)
.map_err(|_| DataCacheError::InvalidBlockChecksum)?;
let crc32c = crc32c_from_base64(&crc32c_b64).map_err(|_| DataCacheError::InvalidBlockChecksum)?;

let block_metadata = BlockMetadata::new(block_idx, block_offset, cache_key, &self.bucket_name, crc32c);
block_metadata
.validate_object_metadata(&object_metadata)?;
block_metadata.validate_object_metadata(&object_metadata)?;

Ok(Some(ChecksummedBytes::new_from_inner_data(buffer, crc32c)))
}
Expand All @@ -203,9 +201,7 @@ where

let object_key = get_s3_key(&self.prefix, &cache_key, block_idx);

let (data, checksum) = bytes
.into_inner()
.map_err(|_| DataCacheError::InvalidBlockContent)?;
let (data, checksum) = bytes.into_inner().map_err(|_| DataCacheError::InvalidBlockContent)?;
let block_metadata = BlockMetadata::new(block_idx, block_offset, &cache_key, &self.bucket_name, checksum);

self.client
Expand Down Expand Up @@ -248,7 +244,8 @@ where
}
Err(err) => {
metrics::counter!("express_data_cache.block_hit").increment(0);
metrics::counter!("express_data_cache.block_err", "reason" => err.get_reason(), "type" => "read").increment(1);
metrics::counter!("express_data_cache.block_err", "reason" => err.get_reason(), "type" => "read")
.increment(1);
(Err(err), "error")
}
};
Expand All @@ -275,7 +272,8 @@ where
(Ok(()), "ok")
}
Err(err) => {
metrics::counter!("express_data_cache.block_err", "reason" => err.get_reason(), "type" => "write").increment(1);
metrics::counter!("express_data_cache.block_err", "reason" => err.get_reason(), "type" => "write")
.increment(1);
(Err(err), "error")
}
};
Expand Down

0 comments on commit 9766e08

Please sign in to comment.