Skip to content

Commit

Permalink
fix: ignore trivial error (#96)
Browse files Browse the repository at this point in the history
ignored:
1. fetch value error
2. weight not equal error

Signed-off-by: MrCroxx <mrcroxx@outlook.com>
  • Loading branch information
MrCroxx committed Aug 1, 2023
1 parent 5109637 commit d3b2972
Show file tree
Hide file tree
Showing 7 changed files with 137 additions and 70 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ check:
cargo clippy --all-targets

test:
cargo test --all
RUST_BACKTRACE=1 cargo test --all
50 changes: 44 additions & 6 deletions foyer-storage/src/device/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,58 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use std::backtrace::Backtrace;

#[derive(thiserror::Error, Debug)]
#[error("{0}")]
pub struct DeviceError(Box<DeviceErrorInner>);

#[derive(thiserror::Error, Debug)]
#[error("{source}")]
struct DeviceErrorInner {
source: DeviceErrorKind,
backtrace: Backtrace,
}

#[derive(thiserror::Error, Debug)]
pub enum Error {
pub enum DeviceErrorKind {
#[error("io error: {0}")]
Io(#[from] std::io::Error),
#[error("nix error: {0}")]
Nix(#[from] nix::errno::Errno),
#[error("other error: {0}")]
Other(String),
Other(#[from] Box<dyn std::error::Error + Send + Sync + 'static>),
}

impl From<std::io::Error> for DeviceError {
fn from(value: std::io::Error) -> Self {
value.into()
}
}

impl From<nix::errno::Errno> for DeviceError {
fn from(value: nix::errno::Errno) -> Self {
value.into()
}
}

impl Error {
pub fn io(e: std::io::Error) -> Self {
Self::Io(e)
impl From<String> for DeviceError {
fn from(value: String) -> Self {
value.into()
}
}

pub type Result<T> = core::result::Result<T, Error>;
pub type DeviceResult<T> = std::result::Result<T, DeviceError>;

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_error_size() {
assert_eq!(
std::mem::size_of::<DeviceError>(),
std::mem::size_of::<usize>()
);
}
}
18 changes: 9 additions & 9 deletions foyer-storage/src/device/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use crate::region::RegionId;
use super::{
allocator::AlignedAllocator,
asyncify,
error::{Error, Result},
error::{DeviceError, DeviceResult},
Device, IoBuf, IoBufMut,
};
use async_trait::async_trait;
Expand Down Expand Up @@ -79,7 +79,7 @@ impl Device for FsDevice {
type Config = FsDeviceConfig;
type IoBufferAllocator = AlignedAllocator;

async fn open(config: FsDeviceConfig) -> Result<Self> {
async fn open(config: FsDeviceConfig) -> DeviceResult<Self> {
Self::open(config).await
}

Expand All @@ -90,7 +90,7 @@ impl Device for FsDevice {
region: RegionId,
offset: u64,
len: usize,
) -> Result<usize> {
) -> DeviceResult<usize> {
assert!(offset as usize + len <= self.inner.config.file_capacity);

let fd = self.fd(region);
Expand All @@ -111,7 +111,7 @@ impl Device for FsDevice {
region: RegionId,
offset: u64,
len: usize,
) -> Result<usize> {
) -> DeviceResult<usize> {
assert!(offset as usize + len <= self.inner.config.file_capacity);

let fd = self.fd(region);
Expand All @@ -126,7 +126,7 @@ impl Device for FsDevice {
}

#[cfg(target_os = "linux")]
async fn flush(&self) -> Result<()> {
async fn flush(&self) -> DeviceResult<()> {
let fd = self.inner.dir.as_raw_fd();
// Commit fs cache to disk. Linux waits for I/O completions.
//
Expand All @@ -143,7 +143,7 @@ impl Device for FsDevice {
}

#[cfg(not(target_os = "linux"))]
async fn flush(&self) -> Result<()> {
async fn flush(&self) -> DeviceResult<()> {
// TODO(MrCroxx): track dirty files and call fsync(2) on them on other target os.

Ok(())
Expand Down Expand Up @@ -178,7 +178,7 @@ impl Device for FsDevice {
}

impl FsDevice {
pub async fn open(config: FsDeviceConfig) -> Result<Self> {
pub async fn open(config: FsDeviceConfig) -> DeviceResult<Self> {
config.verify();

// TODO(MrCroxx): write and read config to a manifest file for pinning
Expand Down Expand Up @@ -207,9 +207,9 @@ impl FsDevice {
#[cfg(target_os = "linux")]
opts.custom_flags(libc::O_DIRECT);

let file = opts.open(path).map_err(Error::io)?;
let file = opts.open(path)?;

Ok::<File, Error>(file)
Ok::<_, DeviceError>(file)
}
})
.collect_vec();
Expand Down
28 changes: 12 additions & 16 deletions foyer-storage/src/device/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@ pub mod error;
pub mod fs;

use async_trait::async_trait;
use error::Result;

use std::{alloc::Allocator, fmt::Debug};

use crate::region::RegionId;
use error::DeviceResult;

pub trait BufferAllocator = Allocator + Clone + Send + Sync + 'static + Debug;
pub trait IoBuf = AsRef<[u8]> + Send + Sync + 'static + Debug;
Expand All @@ -32,25 +31,25 @@ pub trait Device: Sized + Clone + Send + Sync + 'static + Debug {
type IoBufferAllocator: BufferAllocator;
type Config: Debug;

async fn open(config: Self::Config) -> Result<Self>;
async fn open(config: Self::Config) -> DeviceResult<Self>;

async fn write(
&self,
buf: impl IoBuf,
region: RegionId,
offset: u64,
len: usize,
) -> Result<usize>;
) -> DeviceResult<usize>;

async fn read(
&self,
buf: impl IoBufMut,
region: RegionId,
offset: u64,
len: usize,
) -> Result<usize>;
) -> DeviceResult<usize>;

async fn flush(&self) -> Result<()>;
async fn flush(&self) -> DeviceResult<()>;

fn capacity(&self) -> usize;

Expand All @@ -71,17 +70,14 @@ pub trait Device: Sized + Clone + Send + Sync + 'static + Debug {
}

#[tracing::instrument(level = "trace", skip(f))]
async fn asyncify<F, T>(f: F) -> error::Result<T>
async fn asyncify<F, T>(f: F) -> DeviceResult<T>
where
F: FnOnce() -> error::Result<T> + Send + 'static,
F: FnOnce() -> DeviceResult<T> + Send + 'static,
T: Send + 'static,
{
match tokio::task::spawn_blocking(f).await {
Ok(res) => res,
Err(e) => Err(error::Error::Other(format!(
"background task failed: {:?}",
e,
))),
Err(e) => Err(format!("background task failed: {:?}", e,).into()),
}
}

Expand All @@ -103,7 +99,7 @@ pub mod tests {
type Config = usize;
type IoBufferAllocator = AlignedAllocator;

async fn open(config: usize) -> Result<Self> {
async fn open(config: usize) -> DeviceResult<Self> {
Ok(Self::new(config))
}

Expand All @@ -113,7 +109,7 @@ pub mod tests {
_region: RegionId,
_offset: u64,
_len: usize,
) -> Result<usize> {
) -> DeviceResult<usize> {
Ok(0)
}

Expand All @@ -123,11 +119,11 @@ pub mod tests {
_region: RegionId,
_offset: u64,
_len: usize,
) -> Result<usize> {
) -> DeviceResult<usize> {
Ok(0)
}

async fn flush(&self) -> Result<()> {
async fn flush(&self) -> DeviceResult<()> {
Ok(())
}

Expand Down
63 changes: 38 additions & 25 deletions foyer-storage/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,42 +12,55 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use std::backtrace::Backtrace;

use crate::device::error::DeviceError;

#[derive(thiserror::Error, Debug)]
#[error("{0}")]
pub struct Error(Box<ErrorInner>);

#[derive(thiserror::Error, Debug)]
pub enum Error {
#[error("{source}")]
struct ErrorInner {
source: ErrorKind,
backtrace: Backtrace,
}

#[derive(thiserror::Error, Debug)]
pub enum ErrorKind {
#[error("device error: {0}")]
Device(#[from] super::device::error::Error),
#[error("entry too large: {len} > {capacity}")]
EntryTooLarge { len: usize, capacity: usize },
#[error("checksum mismatch, checksum: {checksum}, expected: {expected}")]
ChecksumMismatch { checksum: u64, expected: u64 },
#[error("channel full")]
ChannelFull,
#[error("event listener error: {0}")]
EventListener(Box<dyn std::error::Error + Send + Sync + 'static>),
#[error("fetch value error: {0}")]
FetchValue(anyhow::Error),
Device(#[from] DeviceError),
#[error("other error: {0}")]
Other(#[from] anyhow::Error),
}

impl Error {
pub fn device(e: super::device::error::Error) -> Self {
Self::Device(e)
}

pub fn fetch_value(e: impl Into<anyhow::Error>) -> Self {
Self::Other(e.into())
impl From<ErrorKind> for Error {
fn from(value: ErrorKind) -> Self {
value.into()
}
}

pub fn other(e: impl Into<anyhow::Error>) -> Self {
Self::Other(e.into())
impl From<DeviceError> for Error {
fn from(value: DeviceError) -> Self {
value.into()
}
}

pub fn event_listener(
e: impl Into<Box<dyn std::error::Error + Send + Sync + 'static>>,
) -> Self {
Self::EventListener(e.into())
impl From<anyhow::Error> for Error {
fn from(value: anyhow::Error) -> Self {
value.into()
}
}

pub type Result<T> = core::result::Result<T, Error>;

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_error_size() {
assert_eq!(std::mem::size_of::<Error>(), std::mem::size_of::<usize>());
}
}
2 changes: 2 additions & 0 deletions foyer-storage/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
#![feature(trait_alias)]
#![feature(get_mut_unchecked)]
#![feature(let_chains)]
#![feature(provide_any)]
#![feature(error_generic_member_access)]
#![allow(clippy::type_complexity)]

pub mod admission;
Expand Down
Loading

0 comments on commit d3b2972

Please sign in to comment.