Skip to content

Commit

Permalink
refactor: move sync error type to error.rs
Browse files Browse the repository at this point in the history
Signed-off-by: bsbds <69835502+bsbds@users.noreply.github.com>
  • Loading branch information
bsbds committed Aug 3, 2023
1 parent ba94974 commit 54d7f8c
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 45 deletions.
10 changes: 5 additions & 5 deletions curp/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down Expand Up @@ -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));
}
}
Expand Down
38 changes: 37 additions & 1 deletion curp/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -85,3 +85,39 @@ pub enum CommandProposeError<C: Command> {
#[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<ServerId>, 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<C: Command> {
/// 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<C: Command> From<SyncError> for CommandSyncError<C> {
fn from(err: SyncError) -> Self {
Self::Sync(err)
}
}
43 changes: 4 additions & 39 deletions curp/src/rpc/mod.rs
Original file line number Diff line number Diff line change
@@ -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::{
Expand All @@ -19,7 +18,7 @@ pub use self::proto::{
};
use crate::{
cmd::{Command, ProposeId},
error::ProposeError,
error::{CommandSyncError, ProposeError, SyncError},
log_entry::LogEntry,
LogIndex, ServerId,
};
Expand Down Expand Up @@ -183,11 +182,11 @@ impl WaitSyncedResponse {
unreachable!("should not call after_sync when exe failed")
}
(Some(Err(err)), None) => {
WaitSyncedResponse::new_error(&CommandSyncError::<C>::ExecuteError(err))
WaitSyncedResponse::new_error(&CommandSyncError::<C>::Execute(err))
}
// The er is ignored as the propose has failed
(Some(Ok(_er)), Some(Err(err))) => {
WaitSyncedResponse::new_error(&CommandSyncError::<C>::AfterSyncError(err))
WaitSyncedResponse::new_error(&CommandSyncError::<C>::AfterSync(err))
}
(Some(Ok(er)), Some(Ok(asr))) => WaitSyncedResponse::new_success::<C>(&asr, &er),
// The er is ignored as the propose has failed
Expand Down Expand Up @@ -226,40 +225,6 @@ pub(crate) enum SyncResult<C: Command> {
Error(CommandSyncError<C>),
}

/// The union error which includes sync errors and user-defined errors.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub(crate) enum CommandSyncError<C: Command> {
/// 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<C: Command> From<SyncError> for CommandSyncError<C> {
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<ServerId>, 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<C: Command + Serialize>(
Expand Down

0 comments on commit 54d7f8c

Please sign in to comment.