Skip to content

Commit

Permalink
Refactor: remove OrdBy
Browse files Browse the repository at this point in the history
Just use `as_ref_vote()` to build a `RefVote` for comparing `Vote`.
  • Loading branch information
drmingdrmer committed Jan 27, 2025
1 parent c6a0174 commit 427ac06
Show file tree
Hide file tree
Showing 11 changed files with 14 additions and 72 deletions.
2 changes: 0 additions & 2 deletions openraft/src/base/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
//! Basic types used in the Raft implementation.
pub(crate) mod ord_by;

pub use serde_able::OptionalSerde;
pub use threaded::BoxAny;
pub use threaded::BoxAsyncOnceMut;
Expand Down
36 changes: 0 additions & 36 deletions openraft/src/base/ord_by.rs

This file was deleted.

3 changes: 1 addition & 2 deletions openraft/src/engine/engine_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use std::time::Duration;

use validit::Valid;

use crate::base::ord_by::OrdBy;
use crate::core::raft_msg::AppendEntriesTx;
use crate::core::raft_msg::ResultSender;
use crate::core::sm;
Expand Down Expand Up @@ -755,7 +754,7 @@ where C: RaftTypeConfig
};

debug_assert!(
leader.committed_vote_ref().ord_by() >= self.state.vote_ref().ord_by(),
leader.committed_vote_ref().as_ref_vote() >= self.state.vote_ref().as_ref_vote(),
"leader.vote({}) >= state.vote({})",
leader.committed_vote_ref(),
self.state.vote_ref()
Expand Down
3 changes: 1 addition & 2 deletions openraft/src/engine/handler/establish_handler/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::base::ord_by::OrdBy;
use crate::engine::EngineConfig;
use crate::proposer::Candidate;
use crate::proposer::Leader;
Expand Down Expand Up @@ -33,7 +32,7 @@ where C: RaftTypeConfig

if let Some(l) = self.leader.as_ref() {
#[allow(clippy::neg_cmp_op_on_partial_ord)]
if !(vote.ord_by() > l.committed_vote_ref().ord_by()) {
if !(vote.as_ref_vote() > l.committed_vote_ref().as_ref_vote()) {
tracing::warn!(
"vote is not greater than current existing leader vote. Do not establish new leader and quit"
);
Expand Down
5 changes: 2 additions & 3 deletions openraft/src/engine/handler/vote_handler/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::fmt::Debug;
use std::time::Duration;

use crate::base::ord_by::OrdBy;
use crate::core::raft_msg::ResultSender;
use crate::engine::handler::leader_handler::LeaderHandler;
use crate::engine::handler::replication_handler::ReplicationHandler;
Expand Down Expand Up @@ -106,7 +105,7 @@ where C: RaftTypeConfig
// Partial ord compare:
// Vote does not have to be total ord.
// `!(a >= b)` does not imply `a < b`.
if vote.ord_by() >= self.state.vote_ref().ord_by() {
if vote.as_ref_vote() >= self.state.vote_ref().as_ref_vote() {
// Ok
} else {
tracing::info!("vote {} is rejected by local vote: {}", vote, self.state.vote_ref());
Expand All @@ -126,7 +125,7 @@ where C: RaftTypeConfig
Duration::default()
};

if vote.ord_by() > self.state.vote_ref().ord_by() {
if vote.as_ref_vote() > self.state.vote_ref().as_ref_vote() {
tracing::info!("vote is changing from {} to {}", self.state.vote_ref(), vote);

self.state.vote.update(C::now(), leader_lease, vote.clone());
Expand Down
4 changes: 2 additions & 2 deletions openraft/src/metrics/metric.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use std::cmp::Ordering;

use crate::base::ord_by::OrdBy;
use crate::metrics::metric_display::MetricDisplay;
use crate::type_config::alias::LogIdOf;
use crate::type_config::alias::VoteOf;
use crate::vote::raft_vote::RaftVoteExt;
use crate::LogIdOptionExt;
use crate::RaftMetrics;
use crate::RaftTypeConfig;
Expand Down Expand Up @@ -68,7 +68,7 @@ where C: RaftTypeConfig
fn partial_cmp(&self, other: &Metric<C>) -> Option<Ordering> {
match other {
Metric::Term(v) => Some(self.current_term.cmp(v)),
Metric::Vote(v) => self.vote.ord_by().partial_cmp(&v.ord_by()),
Metric::Vote(v) => self.vote.as_ref_vote().partial_cmp(&v.as_ref_vote()),
Metric::LastLogIndex(v) => Some(self.last_log_index.cmp(v)),
Metric::Applied(v) => Some(self.last_applied.cmp(v)),
Metric::AppliedIndex(v) => Some(self.last_applied.index().cmp(v)),
Expand Down
4 changes: 2 additions & 2 deletions openraft/src/network/snapshot_transport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ mod tokio_rt {
use super::Chunked;
use super::SnapshotTransport;
use super::Streaming;
use crate::base::ord_by::OrdBy;
use crate::error::Fatal;
use crate::error::InstallSnapshotError;
use crate::error::RPCError;
Expand All @@ -31,6 +30,7 @@ mod tokio_rt {
use crate::storage::Snapshot;
use crate::type_config::alias::VoteOf;
use crate::type_config::TypeConfigExt;
use crate::vote::raft_vote::RaftVoteExt;
use crate::ErrorSubject;
use crate::ErrorVerb;
use crate::OptionalSend;
Expand Down Expand Up @@ -149,7 +149,7 @@ mod tokio_rt {
}
};

if resp.vote.ord_by() > vote.ord_by() {
if resp.vote.as_ref_vote() > vote.as_ref_vote() {
// Unfinished, return a response with a higher vote.
// The caller checks the vote and return a HigherVote error.
return Ok(SnapshotResponse::new(resp.vote));
Expand Down
6 changes: 3 additions & 3 deletions openraft/src/raft/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ use tracing::Level;
use crate::async_runtime::watch::WatchReceiver;
use crate::async_runtime::MpscUnboundedSender;
use crate::async_runtime::OneshotSender;
use crate::base::ord_by::OrdBy;
use crate::base::BoxAsyncOnceMut;
use crate::base::BoxFuture;
use crate::base::BoxOnce;
Expand Down Expand Up @@ -91,6 +90,7 @@ use crate::type_config::alias::SnapshotDataOf;
use crate::type_config::alias::VoteOf;
use crate::type_config::alias::WatchReceiverOf;
use crate::type_config::TypeConfigExt;
use crate::vote::raft_vote::RaftVoteExt;
use crate::LogIdOptionExt;
use crate::LogIndexOptionExt;
use crate::OptionalSend;
Expand Down Expand Up @@ -472,7 +472,7 @@ where C: RaftTypeConfig
// It is not mandatory because it is just a read operation
// but prevent unnecessary snapshot transfer early.
{
if req_vote.ord_by() >= my_vote.ord_by() {
if req_vote.as_ref_vote() >= my_vote.as_ref_vote() {
// Ok
} else {
tracing::info!("vote {} is rejected by local vote: {}", req_vote, my_vote);
Expand Down Expand Up @@ -691,7 +691,7 @@ where C: RaftTypeConfig

// Condition failed to become Leader
#[allow(clippy::neg_cmp_op_on_partial_ord)]
let fail = |m: &RaftMetrics<C>| !(req.from_leader.ord_by() >= m.vote.ord_by());
let fail = |m: &RaftMetrics<C>| !(req.from_leader.as_ref_vote() >= m.vote.as_ref_vote());

let timeout = Some(Duration::from_millis(self.inner.config.election_timeout_min));
let metrics_res =
Expand Down
3 changes: 1 addition & 2 deletions openraft/src/raft_state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ pub(crate) use log_state_reader::LogStateReader;
pub use membership_state::MembershipState;
pub(crate) use vote_state_reader::VoteStateReader;

use crate::base::ord_by::OrdBy;
use crate::display_ext::DisplayOptionExt;
use crate::proposer::Leader;
use crate::proposer::LeaderQuorumSet;
Expand Down Expand Up @@ -238,7 +237,7 @@ where C: RaftTypeConfig
let new_vote = accepted.to_vote();
let current_vote = curr_accepted.clone().map(|io_id| io_id.to_vote());
assert!(
Some(new_vote.ord_by()) >= current_vote.as_ref().map(|x| x.ord_by()),
Some(new_vote.as_ref_vote()) >= current_vote.as_ref().map(|x| x.as_ref_vote()),
"new accepted.committed_vote {} must be >= current accepted.committed_vote: {}",
new_vote,
current_vote.display(),
Expand Down
5 changes: 2 additions & 3 deletions openraft/src/replication/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ use tracing_futures::Instrument;
use crate::async_runtime::MpscUnboundedReceiver;
use crate::async_runtime::MpscUnboundedSender;
use crate::async_runtime::MpscUnboundedWeakSender;
use crate::base::ord_by::OrdBy;
use crate::config::Config;
use crate::core::notification::Notification;
use crate::core::sm::handle::SnapshotReader;
Expand Down Expand Up @@ -484,7 +483,7 @@ where
}
AppendEntriesResponse::HigherVote(vote) => {
debug_assert!(
vote.ord_by() > self.session_id.vote().ord_by(),
vote.as_ref_vote() > self.session_id.vote().as_ref_vote(),
"higher vote({}) should be greater than leader's vote({})",
vote,
self.session_id.vote(),
Expand Down Expand Up @@ -803,7 +802,7 @@ where

// Handle response conditions.
let sender_vote = self.session_id.vote();
if resp.vote.ord_by() > sender_vote.ord_by() {
if resp.vote.as_ref_vote() > sender_vote.as_ref_vote() {
return Err(ReplicationError::HigherVote(HigherVote {
higher: resp.vote,
sender_vote,
Expand Down
15 changes: 0 additions & 15 deletions openraft/src/vote/raft_vote.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::fmt::Debug;
use std::fmt::Display;

use crate::base::ord_by::OrdBy;
use crate::base::OptionalFeatures;
use crate::type_config::alias::CommittedLeaderIdOf;
use crate::vote::committed::CommittedVote;
Expand Down Expand Up @@ -107,17 +106,3 @@ where
T: RaftVote<C>,
{
}

impl<C, T> OrdBy<C> for T
where
C: RaftTypeConfig,
T: RaftVote<C>,
{
type By<'k>
= RefVote<'k, C>
where Self: 'k;

fn ord_by(&self) -> Self::By<'_> {
self.as_ref_vote()
}
}

0 comments on commit 427ac06

Please sign in to comment.