From bd02676dbce7ebff274ed71edbe7b8882f4ce880 Mon Sep 17 00:00:00 2001 From: junxiang Mu <1948535941@qq.com> Date: Fri, 27 Sep 2024 16:51:54 +0800 Subject: [PATCH] refact error Signed-off-by: junxiang Mu <1948535941@qq.com> --- ecstore/src/bucket_meta.rs | 2 +- ecstore/src/chunk_stream.rs | 2 +- ecstore/src/disk/endpoint.rs | 2 +- ecstore/src/disk/error.rs | 2 +- ecstore/src/disk/format.rs | 2 +- ecstore/src/disk/local.rs | 2 +- ecstore/src/disk/mod.rs | 2 +- ecstore/src/disk/remote.rs | 2 +- ecstore/src/disks_layout.rs | 2 +- ecstore/src/endpoints.rs | 2 +- ecstore/src/erasure.rs | 2 +- ecstore/src/error.rs | 82 +++++++++++++++++++++++++++++++++++ ecstore/src/lib.rs | 1 + ecstore/src/peer.rs | 2 +- ecstore/src/sets.rs | 2 +- ecstore/src/store.rs | 2 +- ecstore/src/store_api.rs | 2 +- ecstore/src/store_init.rs | 2 +- ecstore/src/utils/ellipses.rs | 2 +- ecstore/src/utils/net.rs | 2 +- rustfs/src/main.rs | 13 ++++-- 21 files changed, 110 insertions(+), 22 deletions(-) create mode 100644 ecstore/src/error.rs diff --git a/ecstore/src/bucket_meta.rs b/ecstore/src/bucket_meta.rs index 6d280449f..aedeb9cf7 100644 --- a/ecstore/src/bucket_meta.rs +++ b/ecstore/src/bucket_meta.rs @@ -2,7 +2,7 @@ use rmp_serde::Serializer; use serde::{Deserialize, Serialize}; use time::OffsetDateTime; -use common::error::Result; +use crate::error::Result; use crate::disk::BUCKET_META_PREFIX; diff --git a/ecstore/src/chunk_stream.rs b/ecstore/src/chunk_stream.rs index 531448c76..96930ef38 100644 --- a/ecstore/src/chunk_stream.rs +++ b/ecstore/src/chunk_stream.rs @@ -1,5 +1,5 @@ +use crate::error::StdError; use bytes::Bytes; -use common::error::StdError; use futures::pin_mut; use futures::stream::{Stream, StreamExt}; use std::future::Future; diff --git a/ecstore/src/disk/endpoint.rs b/ecstore/src/disk/endpoint.rs index 71c6c65fa..78ae38a26 100644 --- a/ecstore/src/disk/endpoint.rs +++ b/ecstore/src/disk/endpoint.rs @@ -1,5 +1,5 @@ +use crate::error::{Error, Result}; use crate::utils::net; -use common::error::{Error, Result}; use path_absolutize::Absolutize; use path_clean::PathClean; use std::{fmt::Display, path::Path}; diff --git a/ecstore/src/disk/error.rs b/ecstore/src/disk/error.rs index 97dd81a68..0b5e36301 100644 --- a/ecstore/src/disk/error.rs +++ b/ecstore/src/disk/error.rs @@ -1,4 +1,4 @@ -use common::error::{Error, Result}; +use crate::error::{Error, Result}; #[derive(Debug, thiserror::Error)] pub enum DiskError { diff --git a/ecstore/src/disk/format.rs b/ecstore/src/disk/format.rs index a17192394..0c37e0862 100644 --- a/ecstore/src/disk/format.rs +++ b/ecstore/src/disk/format.rs @@ -1,5 +1,5 @@ use super::error::DiskError; -use common::error::{Error, Result}; +use crate::error::{Error, Result}; use serde::{Deserialize, Serialize}; use serde_json::Error as JsonError; use uuid::Uuid; diff --git a/ecstore/src/disk/local.rs b/ecstore/src/disk/local.rs index 4f45d221b..e70d08578 100644 --- a/ecstore/src/disk/local.rs +++ b/ecstore/src/disk/local.rs @@ -4,13 +4,13 @@ use super::{ ReadOptions, RenameDataResp, VolumeInfo, WalkDirOptions, }; use crate::disk::{LocalFileReader, LocalFileWriter, STORAGE_FORMAT_FILE}; +use crate::error::{Error, Result}; use crate::{ file_meta::FileMeta, store_api::{FileInfo, RawFileInfo}, utils, }; use bytes::Bytes; -use common::error::{Error, Result}; use path_absolutize::Absolutize; use std::{ fs::Metadata, diff --git a/ecstore/src/disk/mod.rs b/ecstore/src/disk/mod.rs index 0cd267a00..1a11db382 100644 --- a/ecstore/src/disk/mod.rs +++ b/ecstore/src/disk/mod.rs @@ -14,11 +14,11 @@ const STORAGE_FORMAT_FILE: &str = "xl.meta"; use crate::{ erasure::{ReadAt, Write}, + error::{Error, Result}, file_meta::FileMeta, store_api::{FileInfo, RawFileInfo}, }; use bytes::Bytes; -use common::error::{Error, Result}; use futures::StreamExt; use protos::proto_gen::node_service::{ node_service_client::NodeServiceClient, ReadAtRequest, ReadAtResponse, WriteRequest, WriteResponse, diff --git a/ecstore/src/disk/remote.rs b/ecstore/src/disk/remote.rs index 233e724f2..afdb00d75 100644 --- a/ecstore/src/disk/remote.rs +++ b/ecstore/src/disk/remote.rs @@ -1,7 +1,6 @@ use std::path::PathBuf; use bytes::Bytes; -use common::error::{Error, Result}; use futures::lock::Mutex; use protos::{ node_service_time_out_client, @@ -17,6 +16,7 @@ use uuid::Uuid; use crate::{ disk::error::DiskError, + error::{Error, Result}, store_api::{FileInfo, RawFileInfo}, }; diff --git a/ecstore/src/disks_layout.rs b/ecstore/src/disks_layout.rs index 1d3f3e92a..b83c41e83 100644 --- a/ecstore/src/disks_layout.rs +++ b/ecstore/src/disks_layout.rs @@ -1,5 +1,5 @@ +use crate::error::{Error, Result}; use crate::utils::ellipses::*; -use common::error::{Error, Result}; use serde::Deserialize; use std::collections::HashSet; diff --git a/ecstore/src/endpoints.rs b/ecstore/src/endpoints.rs index c6151659d..9af5b963b 100644 --- a/ecstore/src/endpoints.rs +++ b/ecstore/src/endpoints.rs @@ -1,9 +1,9 @@ use crate::{ disk::endpoint::{Endpoint, EndpointType}, disks_layout::DisksLayout, + error::{Error, Result}, utils::net, }; -use common::error::{Error, Result}; use std::{ collections::{hash_map::Entry, HashMap, HashSet}, net::IpAddr, diff --git a/ecstore/src/erasure.rs b/ecstore/src/erasure.rs index af5a91d36..13cb5af18 100644 --- a/ecstore/src/erasure.rs +++ b/ecstore/src/erasure.rs @@ -1,5 +1,5 @@ +use crate::error::{Error, Result, StdError}; use bytes::Bytes; -use common::error::{Error, Result, StdError}; use futures::future::join_all; use futures::{Stream, StreamExt}; use reed_solomon_erasure::galois_8::ReedSolomon; diff --git a/ecstore/src/error.rs b/ecstore/src/error.rs new file mode 100644 index 000000000..24d2936b7 --- /dev/null +++ b/ecstore/src/error.rs @@ -0,0 +1,82 @@ +use tracing_error::{SpanTrace, SpanTraceStatus}; + +pub type StdError = Box; + +pub type Result = std::result::Result; + +#[derive(Debug)] +pub struct Error { + inner: Box, + span_trace: SpanTrace, +} + +impl Error { + /// Create a new error from a `std::error::Error`. + #[must_use] + #[track_caller] + pub fn new(source: T) -> Self { + Self::from_std_error(source.into()) + } + + /// Create a new error from a `std::error::Error`. + #[must_use] + #[track_caller] + pub fn from_std_error(inner: StdError) -> Self { + Self { + inner, + span_trace: SpanTrace::capture(), + } + } + + /// Create a new error from a string. + #[must_use] + #[track_caller] + pub fn from_string(s: impl Into) -> Self { + Self::msg(s) + } + + /// Create a new error from a string. + #[must_use] + #[track_caller] + pub fn msg(s: impl Into) -> Self { + Self::from_std_error(s.into().into()) + } + + /// Returns `true` if the inner type is the same as `T`. + #[inline] + pub fn is(&self) -> bool { + self.inner.is::() + } + + /// Returns some reference to the inner value if it is of type `T`, or + /// `None` if it isn't. + #[inline] + pub fn downcast_ref(&self) -> Option<&T> { + self.inner.downcast_ref() + } + + /// Returns some mutable reference to the inner value if it is of type `T`, or + /// `None` if it isn't. + #[inline] + pub fn downcast_mut(&mut self) -> Option<&mut T> { + self.inner.downcast_mut() + } +} + +impl From for Error { + fn from(e: T) -> Self { + Self::new(e) + } +} + +impl std::fmt::Display for Error { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{}", self.inner)?; + + if self.span_trace.status() != SpanTraceStatus::EMPTY { + write!(f, "\nspan_trace:\n{}", self.span_trace)?; + } + + Ok(()) + } +} diff --git a/ecstore/src/lib.rs b/ecstore/src/lib.rs index 119809841..85e1c9017 100644 --- a/ecstore/src/lib.rs +++ b/ecstore/src/lib.rs @@ -4,6 +4,7 @@ pub mod disk; pub mod disks_layout; pub mod endpoints; pub mod erasure; +pub mod error; mod file_meta; pub mod peer; pub mod set_disk; diff --git a/ecstore/src/peer.rs b/ecstore/src/peer.rs index 4bff296ef..9805ee969 100644 --- a/ecstore/src/peer.rs +++ b/ecstore/src/peer.rs @@ -1,5 +1,4 @@ use async_trait::async_trait; -use common::error::{Error, Result}; use futures::future::join_all; use protos::node_service_time_out_client; use protos::proto_gen::node_service::{DeleteBucketRequest, GetBucketInfoRequest, ListBucketRequest, MakeBucketRequest}; @@ -12,6 +11,7 @@ use crate::store::all_local_disk; use crate::{ disk::{self, error::DiskError, VolumeInfo}, endpoints::{EndpointServerPools, Node}, + error::{Error, Result}, store_api::{BucketInfo, BucketOptions, MakeBucketOptions}, }; diff --git a/ecstore/src/sets.rs b/ecstore/src/sets.rs index 3102db440..35d76a4a6 100644 --- a/ecstore/src/sets.rs +++ b/ecstore/src/sets.rs @@ -1,6 +1,5 @@ use std::{collections::HashMap, sync::Arc}; -use common::error::{Error, Result}; use common::globals::GLOBAL_Local_Node_Name; use futures::future::join_all; use http::HeaderMap; @@ -14,6 +13,7 @@ use crate::{ DiskStore, }, endpoints::PoolEndpoints, + error::{Error, Result}, set_disk::SetDisks, store::{GLOBAL_IsDistErasure, GLOBAL_LOCAL_DISK_SET_DRIVES}, store_api::{ diff --git a/ecstore/src/store.rs b/ecstore/src/store.rs index 6f6f2302f..e105d94e1 100644 --- a/ecstore/src/store.rs +++ b/ecstore/src/store.rs @@ -5,6 +5,7 @@ use crate::{ BUCKET_META_PREFIX, RUSTFS_META_BUCKET, }, endpoints::{EndpointServerPools, SetupType}, + error::{Error, Result}, peer::S3PeerSys, sets::Sets, store_api::{ @@ -15,7 +16,6 @@ use crate::{ store_init, utils, }; use backon::{ExponentialBuilder, Retryable}; -use common::error::{Error, Result}; use common::globals::{GLOBAL_Local_Node_Name, GLOBAL_Rustfs_Host, GLOBAL_Rustfs_Port}; use futures::future::join_all; use http::HeaderMap; diff --git a/ecstore/src/store_api.rs b/ecstore/src/store_api.rs index e6bf97fac..409c91dae 100644 --- a/ecstore/src/store_api.rs +++ b/ecstore/src/store_api.rs @@ -1,4 +1,4 @@ -use common::error::{Error, Result}; +use crate::error::{Error, Result}; use http::HeaderMap; use rmp_serde::Serializer; use s3s::dto::StreamingBlob; diff --git a/ecstore/src/store_init.rs b/ecstore/src/store_init.rs index 32a092513..6e6eb2fc1 100644 --- a/ecstore/src/store_init.rs +++ b/ecstore/src/store_init.rs @@ -5,8 +5,8 @@ use crate::{ new_disk, DiskOption, DiskStore, FORMAT_CONFIG_FILE, RUSTFS_META_BUCKET, }, endpoints::Endpoints, + error::{Error, Result}, }; -use common::error::{Error, Result}; use futures::future::join_all; use std::{ collections::{hash_map::Entry, HashMap}, diff --git a/ecstore/src/utils/ellipses.rs b/ecstore/src/utils/ellipses.rs index fcd10989c..9badb80ce 100644 --- a/ecstore/src/utils/ellipses.rs +++ b/ecstore/src/utils/ellipses.rs @@ -1,4 +1,4 @@ -use common::error::{Error, Result}; +use crate::error::{Error, Result}; use lazy_static::*; use regex::Regex; diff --git a/ecstore/src/utils/net.rs b/ecstore/src/utils/net.rs index 3f1f96dfb..91f453e86 100644 --- a/ecstore/src/utils/net.rs +++ b/ecstore/src/utils/net.rs @@ -1,4 +1,4 @@ -use common::error::{Error, Result}; +use crate::error::{Error, Result}; use lazy_static::lazy_static; use std::{ collections::HashSet, diff --git a/rustfs/src/main.rs b/rustfs/src/main.rs index 500c5fd72..43002ee2b 100644 --- a/rustfs/src/main.rs +++ b/rustfs/src/main.rs @@ -4,7 +4,7 @@ mod service; mod storage; use clap::Parser; -use common::error::Result; +use common::error::{Error, Result}; use ecstore::{ endpoints::EndpointServerPools, store::{init_local_disks, update_erasure_type, ECStore}, @@ -87,12 +87,15 @@ async fn run(opt: config::Opt) -> Result<()> { // }; // 用于rpc - let (endpoint_pools, setup_type) = EndpointServerPools::from_volumes(opt.address.clone().as_str(), opt.volumes.clone())?; + let (endpoint_pools, setup_type) = EndpointServerPools::from_volumes(opt.address.clone().as_str(), opt.volumes.clone()) + .map_err(|err| Error::from_string(err.to_string()))?; update_erasure_type(setup_type).await; // 初始化本地磁盘 - init_local_disks(endpoint_pools.clone()).await?; + init_local_disks(endpoint_pools.clone()) + .await + .map_err(|err| Error::from_string(err.to_string()))?; // Setup S3 service // 本项目使用s3s库来实现s3服务 @@ -177,7 +180,9 @@ async fn run(opt: config::Opt) -> Result<()> { warn!(" init store"); // init store - ECStore::new(opt.address.clone(), endpoint_pools.clone()).await?; + ECStore::new(opt.address.clone(), endpoint_pools.clone()) + .await + .map_err(|err| Error::from_string(err.to_string()))?; warn!(" init store success!"); tokio::select! {