-
Notifications
You must be signed in to change notification settings - Fork 154
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change: move external command trigger to dedicated Trigger struct (#888)
* Refactor: move RaftTypeConfig to separate file * Change: move external command trigger to dedicated Trigger struct Moved trigger election, heartbeat, snapshot and purge log from `Raft` to a new `Trigger` struct, to separate externally trigger actions from the main Raft API. --- Marked the old trigger methods in `Raft` as deprecated, and recommended using the new `Trigger` struct instead. The main motivation of these changes is to organize the Raft API in a more structured way, by extracting trigger actions into a dedicated struct, instead of mixing them together in the `Raft` API.
- Loading branch information
1 parent
7eccd06
commit 4c488a6
Showing
18 changed files
with
207 additions
and
117 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
use crate::error::Fatal; | ||
use crate::AsyncRuntime; | ||
use crate::NodeId; | ||
|
||
/// The running state of RaftCore | ||
pub(in crate::raft) enum CoreState<NID, A> | ||
where | ||
NID: NodeId, | ||
A: AsyncRuntime, | ||
{ | ||
/// The RaftCore task is still running. | ||
Running(A::JoinHandle<Result<(), Fatal<NID>>>), | ||
|
||
/// The RaftCore task has finished. The return value of the task is stored. | ||
Done(Result<(), Fatal<NID>>), | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
//! Trigger an action to RaftCore by external caller. | ||
|
||
use crate::core::raft_msg::external_command::ExternalCommand; | ||
use crate::error::Fatal; | ||
use crate::raft::RaftInner; | ||
use crate::storage::RaftLogStorage; | ||
use crate::RaftNetworkFactory; | ||
use crate::RaftTypeConfig; | ||
|
||
/// Trigger is an interface to trigger an action to RaftCore by external caller. | ||
pub struct Trigger<'r, C, N, LS> | ||
where | ||
C: RaftTypeConfig, | ||
N: RaftNetworkFactory<C>, | ||
LS: RaftLogStorage<C>, | ||
{ | ||
raft_inner: &'r RaftInner<C, N, LS>, | ||
} | ||
|
||
impl<'r, C, N, LS> Trigger<'r, C, N, LS> | ||
where | ||
C: RaftTypeConfig, | ||
N: RaftNetworkFactory<C>, | ||
LS: RaftLogStorage<C>, | ||
{ | ||
pub(in crate::raft) fn new(raft_inner: &'r RaftInner<C, N, LS>) -> Self { | ||
Self { raft_inner } | ||
} | ||
|
||
/// Trigger election at once and return at once. | ||
/// | ||
/// Returns error when RaftCore has [`Fatal`] error, e.g. shut down or having storage error. | ||
/// It is not affected by `Raft::enable_elect(false)`. | ||
pub async fn elect(&self) -> Result<(), Fatal<C::NodeId>> { | ||
self.raft_inner.send_external_command(ExternalCommand::Elect, "trigger_elect").await | ||
} | ||
|
||
/// Trigger a heartbeat at once and return at once. | ||
/// | ||
/// Returns error when RaftCore has [`Fatal`] error, e.g. shut down or having storage error. | ||
/// It is not affected by `Raft::enable_heartbeat(false)`. | ||
pub async fn heartbeat(&self) -> Result<(), Fatal<C::NodeId>> { | ||
self.raft_inner.send_external_command(ExternalCommand::Heartbeat, "trigger_heartbeat").await | ||
} | ||
|
||
/// Trigger to build a snapshot at once and return at once. | ||
/// | ||
/// Returns error when RaftCore has [`Fatal`] error, e.g. shut down or having storage error. | ||
pub async fn snapshot(&self) -> Result<(), Fatal<C::NodeId>> { | ||
self.raft_inner.send_external_command(ExternalCommand::Snapshot, "trigger_snapshot").await | ||
} | ||
|
||
/// Initiate the log purge up to and including the given `upto` log index. | ||
/// | ||
/// Logs that are not included in a snapshot will **NOT** be purged. | ||
/// In such scenario it will delete as many log as possible. | ||
/// The [`max_in_snapshot_log_to_keep`] config is not taken into account | ||
/// when purging logs. | ||
/// | ||
/// It returns error only when RaftCore has [`Fatal`] error, e.g. shut down or having storage | ||
/// error. | ||
/// | ||
/// Openraft won't purge logs at once, e.g. it may be delayed by several seconds, because if it | ||
/// is a leader and a replication task has been replicating the logs to a follower, the logs | ||
/// can't be purged until the replication task is finished. | ||
/// | ||
/// [`max_in_snapshot_log_to_keep`]: `crate::Config::max_in_snapshot_log_to_keep` | ||
pub async fn purge_log(&self, upto: u64) -> Result<(), Fatal<C::NodeId>> { | ||
self.raft_inner.send_external_command(ExternalCommand::PurgeLog { upto }, "purge_log").await | ||
} | ||
} |
Oops, something went wrong.