From 35925cdcba040fd24103cff28f70c1bb6fd865f9 Mon Sep 17 00:00:00 2001 From: bsbds <69835502+bsbds@users.noreply.github.com> Date: Thu, 3 Aug 2023 12:47:05 +0800 Subject: [PATCH] refactor: move sync error type to `error.rs` Signed-off-by: bsbds <69835502+bsbds@users.noreply.github.com> --- curp/src/client.rs | 10 +++++----- curp/src/error.rs | 38 +++++++++++++++++++++++++++++++++++++- curp/src/rpc/mod.rs | 43 ++++--------------------------------------- 3 files changed, 46 insertions(+), 45 deletions(-) diff --git a/curp/src/client.rs b/curp/src/client.rs index 50334c524c..57c6db0c45 100644 --- a/curp/src/client.rs +++ b/curp/src/client.rs @@ -9,10 +9,10 @@ use utils::{config::ClientTimeout, parking_lot_lock::RwLockMap}; use crate::{ cmd::{Command, ProposeId}, - error::{CommandProposeError, ProposeError, RpcError}, + error::{CommandProposeError, CommandSyncError, ProposeError, RpcError, SyncError}, rpc::{ - self, connect::ConnectApi, CommandSyncError, FetchLeaderRequest, FetchReadStateRequest, - ProposeRequest, ReadState as PbReadState, SyncError, SyncResult, WaitSyncedRequest, + self, connect::ConnectApi, FetchLeaderRequest, FetchReadStateRequest, ProposeRequest, + ReadState as PbReadState, SyncResult, WaitSyncedRequest, }, LogIndex, ServerId, }; @@ -242,10 +242,10 @@ where SyncResult::Error(CommandSyncError::Sync(e)) => { return Err(ProposeError::SyncedError(e).into()); } - SyncResult::Error(CommandSyncError::ExecuteError(e)) => { + SyncResult::Error(CommandSyncError::Execute(e)) => { return Err(CommandProposeError::Execute(e)); } - SyncResult::Error(CommandSyncError::AfterSyncError(e)) => { + SyncResult::Error(CommandSyncError::AfterSync(e)) => { return Err(CommandProposeError::AfterSync(e)); } } diff --git a/curp/src/error.rs b/curp/src/error.rs index c954706f2b..2f46a7fe6f 100644 --- a/curp/src/error.rs +++ b/curp/src/error.rs @@ -4,7 +4,7 @@ use curp_external_api::cmd::Command; use serde::{Deserialize, Serialize}; use thiserror::Error; -use crate::rpc::SyncError; +use crate::{cmd::ProposeId, ServerId}; /// Server side error #[allow(clippy::module_name_repetitions)] // this-error generate code false-positive @@ -89,3 +89,39 @@ pub enum CommandProposeError { #[error("after sync error: {0}")] AfterSync(C::Error), } + +/// Wait synced error +#[derive(Clone, Error, Serialize, Deserialize, Debug)] +#[allow(clippy::module_name_repetitions)] // this-error generate code false-positive +#[non_exhaustive] +pub enum SyncError { + /// If client sent a wait synced request to a non-leader + #[error("redirect to {0:?}, term {1}")] + Redirect(Option, u64), + /// If there is no such cmd to be waited + #[error("no such command {0}")] + NoSuchCmd(ProposeId), + /// Wait timeout + #[error("timeout")] + Timeout, + /// Other error + #[error("other: {0}")] + Other(String), +} + +/// The union error which includes sync errors and user-defined errors. +#[derive(Clone, Debug, Serialize, Deserialize)] +pub(crate) enum CommandSyncError { + /// If wait sync went wrong + Sync(SyncError), + /// If the execution of the cmd went wrong + Execute(C::Error), + /// If after sync of the cmd went wrong + AfterSync(C::Error), +} + +impl From for CommandSyncError { + fn from(err: SyncError) -> Self { + Self::Sync(err) + } +} diff --git a/curp/src/rpc/mod.rs b/curp/src/rpc/mod.rs index f8f88f6e67..fb837d2bd5 100644 --- a/curp/src/rpc/mod.rs +++ b/curp/src/rpc/mod.rs @@ -1,7 +1,6 @@ use std::sync::Arc; -use serde::{de::DeserializeOwned, Deserialize, Serialize}; -use thiserror::Error; +use serde::{de::DeserializeOwned, Serialize}; pub use self::proto::protocol_server::ProtocolServer; pub(crate) use self::proto::{ @@ -19,7 +18,7 @@ pub use self::proto::{ }; use crate::{ cmd::{Command, ProposeId}, - error::ProposeError, + error::{CommandSyncError, ProposeError, SyncError}, log_entry::LogEntry, LogIndex, ServerId, }; @@ -183,11 +182,11 @@ impl WaitSyncedResponse { unreachable!("should not call after_sync when exe failed") } (Some(Err(err)), None) => { - WaitSyncedResponse::new_error(&CommandSyncError::::ExecuteError(err)) + WaitSyncedResponse::new_error(&CommandSyncError::::Execute(err)) } // The er is ignored as the propose has failed (Some(Ok(_er)), Some(Err(err))) => { - WaitSyncedResponse::new_error(&CommandSyncError::::AfterSyncError(err)) + WaitSyncedResponse::new_error(&CommandSyncError::::AfterSync(err)) } (Some(Ok(er)), Some(Ok(asr))) => WaitSyncedResponse::new_success::(&asr, &er), // The er is ignored as the propose has failed @@ -226,40 +225,6 @@ pub(crate) enum SyncResult { Error(CommandSyncError), } -/// The union error which includes sync errors and user-defined errors. -#[derive(Clone, Debug, Serialize, Deserialize)] -pub(crate) enum CommandSyncError { - /// If wait sync went wrong - Sync(SyncError), - /// If the execution of the cmd went wrong - ExecuteError(C::Error), - /// If after sync of the cmd went wrong - AfterSyncError(C::Error), -} - -impl From for CommandSyncError { - fn from(err: SyncError) -> Self { - Self::Sync(err) - } -} - -/// Wait Synced error -#[derive(Clone, Error, Serialize, Deserialize, Debug)] -pub enum SyncError { - /// If client sent a wait synced request to a non-leader - #[error("redirect to {0:?}, term {1}")] - Redirect(Option, u64), - /// If there is no such cmd to be waited - #[error("no such command {0}")] - NoSuchCmd(ProposeId), - /// Wait timeout - #[error("timeout")] - Timeout, - /// Other error - #[error("other: {0}")] - Other(String), -} - impl AppendEntriesRequest { /// Create a new `append_entries` request pub(crate) fn new(