diff --git a/openraft/src/engine/engine_impl.rs b/openraft/src/engine/engine_impl.rs index e0bf990f8..3c587761f 100644 --- a/openraft/src/engine/engine_impl.rs +++ b/openraft/src/engine/engine_impl.rs @@ -77,7 +77,7 @@ where C: RaftTypeConfig pub(crate) seen_greater_log: bool, /// The internal server state used by Engine. - pub(crate) internal_server_state: InternalServerState>, + pub(crate) internal_server_state: InternalServerState, /// Output entry for the runtime. pub(crate) output: EngineOutput, diff --git a/openraft/src/engine/handler/leader_handler/mod.rs b/openraft/src/engine/handler/leader_handler/mod.rs index 42196b7a4..7dbe95610 100644 --- a/openraft/src/engine/handler/leader_handler/mod.rs +++ b/openraft/src/engine/handler/leader_handler/mod.rs @@ -7,7 +7,6 @@ use crate::engine::EngineOutput; use crate::entry::RaftPayload; use crate::internal_server_state::LeaderQuorumSet; use crate::leader::Leading; -use crate::type_config::alias::InstantOf; use crate::RaftLogId; use crate::RaftState; use crate::RaftTypeConfig; @@ -24,7 +23,7 @@ pub(crate) struct LeaderHandler<'x, C> where C: RaftTypeConfig { pub(crate) config: &'x mut EngineConfig, - pub(crate) leader: &'x mut Leading, InstantOf>, + pub(crate) leader: &'x mut Leading>, pub(crate) state: &'x mut RaftState, pub(crate) output: &'x mut EngineOutput, } diff --git a/openraft/src/engine/handler/replication_handler/mod.rs b/openraft/src/engine/handler/replication_handler/mod.rs index 86894c850..4fbac7192 100644 --- a/openraft/src/engine/handler/replication_handler/mod.rs +++ b/openraft/src/engine/handler/replication_handler/mod.rs @@ -41,7 +41,7 @@ pub(crate) struct ReplicationHandler<'x, C> where C: RaftTypeConfig { pub(crate) config: &'x mut EngineConfig, - pub(crate) leader: &'x mut Leading, InstantOf>, + pub(crate) leader: &'x mut Leading>, pub(crate) state: &'x mut RaftState, pub(crate) output: &'x mut EngineOutput, } diff --git a/openraft/src/engine/handler/vote_handler/mod.rs b/openraft/src/engine/handler/vote_handler/mod.rs index a975c3b99..b6adfa5bf 100644 --- a/openraft/src/engine/handler/vote_handler/mod.rs +++ b/openraft/src/engine/handler/vote_handler/mod.rs @@ -31,7 +31,7 @@ where C: RaftTypeConfig pub(crate) config: &'st EngineConfig, pub(crate) state: &'st mut RaftState, pub(crate) output: &'st mut EngineOutput, - pub(crate) internal_server_state: &'st mut InternalServerState>, + pub(crate) internal_server_state: &'st mut InternalServerState, } impl<'st, C> VoteHandler<'st, C> diff --git a/openraft/src/internal_server_state.rs b/openraft/src/internal_server_state.rs index bbf9499fd..b2a996bad 100644 --- a/openraft/src/internal_server_state.rs +++ b/openraft/src/internal_server_state.rs @@ -1,8 +1,7 @@ use crate::leader::voting::Voting; use crate::leader::Leading; use crate::quorum::Joint; -use crate::Instant; -use crate::NodeId; +use crate::RaftTypeConfig; /// The quorum set type used by `Leader`. pub(crate) type LeaderQuorumSet = Joint, Vec>>; @@ -23,15 +22,13 @@ pub(crate) type LeaderQuorumSet = Joint, Vec>>; #[derive(PartialEq, Eq)] // TODO(9): Make InternalServerState an Option, separate Leading(Proposer) role and // Following(Acceptor) role -pub(crate) enum InternalServerState -where - NID: NodeId, - I: Instant, +pub(crate) enum InternalServerState +where C: RaftTypeConfig { /// Leader or candidate. /// /// `vote.committed==true` means it is a leader. - Leading(Box, I>>), + Leading(Box>>), /// Follower or learner. /// @@ -39,36 +36,32 @@ where Following, } -impl Default for InternalServerState -where - NID: NodeId, - I: Instant, +impl Default for InternalServerState +where C: RaftTypeConfig { fn default() -> Self { Self::Following } } -impl InternalServerState -where - NID: NodeId, - I: Instant, +impl InternalServerState +where C: RaftTypeConfig { - pub(crate) fn voting_mut(&mut self) -> Option<&mut Voting, I>> { + pub(crate) fn voting_mut(&mut self) -> Option<&mut Voting>> { match self { InternalServerState::Leading(l) => l.voting_mut(), InternalServerState::Following => None, } } - pub(crate) fn leading(&self) -> Option<&Leading, I>> { + pub(crate) fn leading(&self) -> Option<&Leading>> { match self { InternalServerState::Leading(l) => Some(l), InternalServerState::Following => None, } } - pub(crate) fn leading_mut(&mut self) -> Option<&mut Leading, I>> { + pub(crate) fn leading_mut(&mut self) -> Option<&mut Leading>> { match self { InternalServerState::Leading(l) => Some(l), InternalServerState::Following => None, diff --git a/openraft/src/leader/leader.rs b/openraft/src/leader/leader.rs index 9bf4d971b..c31840d53 100644 --- a/openraft/src/leader/leader.rs +++ b/openraft/src/leader/leader.rs @@ -5,10 +5,11 @@ use crate::progress::entry::ProgressEntry; use crate::progress::Progress; use crate::progress::VecProgress; use crate::quorum::QuorumSet; +use crate::type_config::alias::InstantOf; +use crate::type_config::alias::LogIdOf; use crate::Instant; -use crate::LogId; use crate::LogIdOptionExt; -use crate::NodeId; +use crate::RaftTypeConfig; use crate::Vote; /// Leading state data. @@ -27,37 +28,38 @@ use crate::Vote; /// But instead it will be able to upgrade its `leader_id` without losing leadership. #[derive(Clone, Debug)] #[derive(PartialEq, Eq)] -pub(crate) struct Leading, I: Instant> { +pub(crate) struct Leading> +where C: RaftTypeConfig +{ /// The vote this leader works in. - pub(crate) vote: Vote, + pub(crate) vote: Vote, quorum_set: QS, /// Voting state, i.e., there is a Candidate running. - voting: Option>, + voting: Option>, /// Tracks the replication progress and committed index - pub(crate) progress: VecProgress, Option>, QS>, + pub(crate) progress: VecProgress, Option>, QS>, /// Tracks the clock time acknowledged by other nodes. /// /// See [`docs::leader_lease`] for more details. /// /// [`docs::leader_lease`]: `crate::docs::protocol::replication::leader_lease` - pub(crate) clock_progress: VecProgress, Option, QS>, + pub(crate) clock_progress: VecProgress>, Option>, QS>, } -impl Leading +impl Leading where - NID: NodeId, - QS: QuorumSet + Clone + fmt::Debug + 'static, - I: Instant, + C: RaftTypeConfig, + QS: QuorumSet + Clone + fmt::Debug + 'static, { pub(crate) fn new( - vote: Vote, + vote: Vote, quorum_set: QS, - learner_ids: impl Iterator, - last_log_id: Option>, + learner_ids: impl Iterator, + last_log_id: Option>, ) -> Self { let learner_ids = learner_ids.collect::>(); @@ -75,22 +77,26 @@ where } #[allow(dead_code)] - pub(crate) fn voting(&self) -> Option<&Voting> { + pub(crate) fn voting(&self) -> Option<&Voting> { self.voting.as_ref() } #[allow(dead_code)] - pub(crate) fn voting_mut(&mut self) -> Option<&mut Voting> { + pub(crate) fn voting_mut(&mut self) -> Option<&mut Voting> { self.voting.as_mut() } - pub(crate) fn initialize_voting(&mut self, last_log_id: Option>, now: I) -> &mut Voting { + pub(crate) fn initialize_voting( + &mut self, + last_log_id: Option>, + now: InstantOf, + ) -> &mut Voting { self.voting = Some(Voting::new(now, self.vote, last_log_id, self.quorum_set.clone())); self.voting.as_mut().unwrap() } /// Finish the voting process and return the state. - pub(crate) fn finish_voting(&mut self) -> Voting { + pub(crate) fn finish_voting(&mut self) -> Voting { // it has to be in voting progress self.voting.take().unwrap() } @@ -107,7 +113,7 @@ where /// Note that the leader may not be in the QuorumSet at all. /// In such a case, the update operation will be just ignored, /// and the quorum-acked-time is totally determined by remove voters. - pub(crate) fn last_quorum_acked_time(&mut self) -> Option { + pub(crate) fn last_quorum_acked_time(&mut self) -> Option> { // For `Leading`, the vote is always the leader's vote. // Thus vote.voted_for() is this node. @@ -142,12 +148,8 @@ mod tests { #[test] fn test_leading_last_quorum_acked_time_leader_is_voter() { - let mut leading = Leading::, InstantOf>::new( - Vote::new_committed(2, 1), - vec![1, 2, 3], - vec![4].into_iter(), - None, - ); + let mut leading = + Leading::>::new(Vote::new_committed(2, 1), vec![1, 2, 3], vec![4].into_iter(), None); let now1 = InstantOf::::now(); @@ -158,12 +160,8 @@ mod tests { #[test] fn test_leading_last_quorum_acked_time_leader_is_learner() { - let mut leading = Leading::, InstantOf>::new( - Vote::new_committed(2, 4), - vec![1, 2, 3], - vec![4].into_iter(), - None, - ); + let mut leading = + Leading::>::new(Vote::new_committed(2, 4), vec![1, 2, 3], vec![4].into_iter(), None); let t2 = InstantOf::::now(); let _ = leading.clock_progress.increase_to(&2, Some(t2)); @@ -178,12 +176,8 @@ mod tests { #[test] fn test_leading_last_quorum_acked_time_leader_is_not_member() { - let mut leading = Leading::, InstantOf>::new( - Vote::new_committed(2, 5), - vec![1, 2, 3], - vec![4].into_iter(), - None, - ); + let mut leading = + Leading::>::new(Vote::new_committed(2, 5), vec![1, 2, 3], vec![4].into_iter(), None); let t2 = InstantOf::::now(); let _ = leading.clock_progress.increase_to(&2, Some(t2)); diff --git a/openraft/src/leader/voting.rs b/openraft/src/leader/voting.rs index 33fb0178b..ed5e1722d 100644 --- a/openraft/src/leader/voting.rs +++ b/openraft/src/leader/voting.rs @@ -4,37 +4,35 @@ use crate::display_ext::DisplayOptionExt; use crate::progress::Progress; use crate::progress::VecProgress; use crate::quorum::QuorumSet; -use crate::Instant; -use crate::LogId; -use crate::NodeId; +use crate::type_config::alias::InstantOf; +use crate::type_config::alias::LogIdOf; +use crate::RaftTypeConfig; use crate::Vote; /// Voting state. #[derive(Clone, Debug)] #[derive(PartialEq, Eq)] -pub(crate) struct Voting +pub(crate) struct Voting where - NID: NodeId, - QS: QuorumSet, - I: Instant, + C: RaftTypeConfig, + QS: QuorumSet, { /// When the voting is started. - starting_time: I, + starting_time: InstantOf, /// The vote. - vote: Vote, + vote: Vote, - last_log_id: Option>, + last_log_id: Option>, /// Which nodes have granted the the vote at certain time point. - progress: VecProgress, + progress: VecProgress, } -impl fmt::Display for Voting +impl fmt::Display for Voting where - NID: NodeId, - QS: QuorumSet + fmt::Debug + 'static, - I: Instant, + C: RaftTypeConfig, + QS: QuorumSet + fmt::Debug + 'static, { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!( @@ -48,13 +46,17 @@ where } } -impl Voting +impl Voting where - NID: NodeId, - QS: QuorumSet + fmt::Debug + 'static, - I: Instant, + C: RaftTypeConfig, + QS: QuorumSet + fmt::Debug + 'static, { - pub(crate) fn new(starting_time: I, vote: Vote, last_log_id: Option>, quorum_set: QS) -> Self { + pub(crate) fn new( + starting_time: InstantOf, + vote: Vote, + last_log_id: Option>, + quorum_set: QS, + ) -> Self { Self { starting_time, vote, @@ -63,16 +65,16 @@ where } } - pub(crate) fn vote_ref(&self) -> &Vote { + pub(crate) fn vote_ref(&self) -> &Vote { &self.vote } - pub(crate) fn progress(&self) -> &VecProgress { + pub(crate) fn progress(&self) -> &VecProgress { &self.progress } /// Grant the vote by a node. - pub(crate) fn grant_by(&mut self, target: &NID) -> bool { + pub(crate) fn grant_by(&mut self, target: &C::NodeId) -> bool { let granted = *self.progress.update(target, true).expect("target not in quorum set"); tracing::info!(voting = debug(&self), "{}", func_name!()); @@ -82,7 +84,7 @@ where /// Return the node ids that has granted this vote. #[allow(dead_code)] - pub(crate) fn granters(&self) -> impl Iterator + '_ { + pub(crate) fn granters(&self) -> impl Iterator + '_ { self.progress().iter().filter(|(_, granted)| *granted).map(|(target, _)| *target) } }