Skip to content

Commit

Permalink
Refactor: rename RaftLeaderId::node_id_ref() to node_id()
Browse files Browse the repository at this point in the history
  • Loading branch information
drmingdrmer committed Jan 3, 2025
1 parent 959a663 commit c55c962
Show file tree
Hide file tree
Showing 12 changed files with 17 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ impl RaftLeaderId<TypeConfig> for pb::LeaderId {
self.term
}

fn node_id_ref(&self) -> Option<&u64> {
fn node_id(&self) -> Option<&u64> {
Some(&self.node_id)
}

Expand Down
2 changes: 1 addition & 1 deletion openraft/src/core/raft_core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -727,7 +727,7 @@ where
}

// Safe unwrap(): vote that is committed has to already have voted for some node.
let id = vote.leader_id().node_id_ref().cloned().unwrap();
let id = vote.leader_id().node_id().cloned().unwrap();

// TODO: `is_voter()` is slow, maybe cache `current_leader`,
// e.g., only update it when membership or vote changes
Expand Down
2 changes: 1 addition & 1 deletion openraft/src/engine/handler/establish_handler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ where C: RaftTypeConfig
let vote = candidate.vote_ref().clone();

debug_assert_eq!(
vote.leader_id().node_id_ref(),
vote.leader_id().node_id(),
Some(&self.config.id),
"it can only commit its own vote"
);
Expand Down
2 changes: 1 addition & 1 deletion openraft/src/engine/handler/vote_handler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ where C: RaftTypeConfig
/// This node then becomes raft-follower or raft-learner.
pub(crate) fn become_following(&mut self) {
debug_assert!(
self.state.vote_ref().leader_id().node_id_ref() != Some(&self.config.id)
self.state.vote_ref().leader_id().node_id() != Some(&self.config.id)
|| !self.state.membership_state.effective().membership().is_voter(&self.config.id),
"It must hold: vote is not mine, or I am not a voter(leader just left the cluster)"
);
Expand Down
2 changes: 1 addition & 1 deletion openraft/src/proposer/leader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ where
// Thus vote.voted_for() is this node.

// Safe unwrap: voted_for() is always non-None in Openraft
let node_id = self.committed_vote.clone().into_vote().leader_id().node_id_ref().cloned().unwrap();
let node_id = self.committed_vote.clone().into_vote().leader_id().node_id().cloned().unwrap();
let now = C::now();

tracing::debug!(
Expand Down
4 changes: 2 additions & 2 deletions openraft/src/raft_state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ where C: RaftTypeConfig
///
/// [Determine Server State]: crate::docs::data::vote#vote-and-membership-define-the-server-state
pub(crate) fn is_leading(&self, id: &C::NodeId) -> bool {
self.membership_state.contains(id) && self.vote.leader_id().node_id_ref() == Some(id)
self.membership_state.contains(id) && self.vote.leader_id().node_id() == Some(id)
}

/// The node is leader
Expand Down Expand Up @@ -408,7 +408,7 @@ where C: RaftTypeConfig

if vote.is_committed() {
// Safe unwrap(): vote that is committed has to already have voted for some node.
let id = vote.leader_id().node_id_ref().cloned().unwrap();
let id = vote.leader_id().node_id().cloned().unwrap();

return self.new_forward_to_leader(id);
}
Expand Down
2 changes: 1 addition & 1 deletion openraft/src/replication/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@ where
let append_res = res.map_err(|_e| {
let to = Timeout {
action: RPCTypes::AppendEntries,
id: self.session_id.vote().leader_id().node_id_ref().cloned().unwrap(),
id: self.session_id.vote().leader_id().node_id().cloned().unwrap(),
target: self.target.clone(),
timeout: the_timeout,
};
Expand Down
2 changes: 1 addition & 1 deletion openraft/src/vote/leader_id/leader_id_adv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ where C: RaftTypeConfig<LeaderId = Self>
self.term
}

fn node_id_ref(&self) -> Option<&C::NodeId> {
fn node_id(&self) -> Option<&C::NodeId> {
Some(&self.node_id)
}

Expand Down
6 changes: 3 additions & 3 deletions openraft/src/vote/leader_id/leader_id_cmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ where C: RaftTypeConfig
pub fn std<LID>(a: &LID, b: &LID) -> Option<Ordering>
where LID: RaftLeaderId<C> {
match a.term().cmp(&b.term()) {
Ordering::Equal => match (a.node_id_ref(), b.node_id_ref()) {
Ordering::Equal => match (a.node_id(), b.node_id()) {
(None, None) => Some(Ordering::Equal),
(Some(_), None) => Some(Ordering::Greater),
(None, Some(_)) => Some(Ordering::Less),
Expand All @@ -38,7 +38,7 @@ where C: RaftTypeConfig
/// Implements [`PartialOrd`] for LeaderId to allow multiple leaders per term.
pub fn adv<LID>(a: &LID, b: &LID) -> Option<Ordering>
where LID: RaftLeaderId<C> {
let res = (a.term(), a.node_id_ref()).cmp(&(b.term(), b.node_id_ref()));
let res = (a.term(), a.node_id()).cmp(&(b.term(), b.node_id()));
Some(res)
}
}
Expand Down Expand Up @@ -66,7 +66,7 @@ mod tests {
self.0
}

fn node_id_ref(&self) -> Option<&u64> {
fn node_id(&self) -> Option<&u64> {
self.1.as_ref()
}

Expand Down
2 changes: 1 addition & 1 deletion openraft/src/vote/leader_id/leader_id_std.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ where C: RaftTypeConfig
self.term
}

fn node_id_ref(&self) -> Option<&C::NodeId> {
fn node_id(&self) -> Option<&C::NodeId> {
self.voted_for.as_ref()
}

Expand Down
4 changes: 2 additions & 2 deletions openraft/src/vote/leader_id/raft_leader_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ where
fn term(&self) -> C::Term;

/// Get the node ID of this leader, if one is set
fn node_id_ref(&self) -> Option<&C::NodeId>;
fn node_id(&self) -> Option<&C::NodeId>;

/// Convert this leader ID to a committed leader ID.
///
Expand All @@ -59,7 +59,7 @@ where
}

fn node_id(&self) -> Option<C::NodeId> {
self.node_id_ref().cloned()
self.node_id().cloned()

Check failure on line 62 in openraft/src/vote/leader_id/raft_leader_id.rs

View workflow job for this annotation

GitHub Actions / cluster-benchmark (cluster_benchmark)

multiple applicable items in scope

Check failure on line 62 in openraft/src/vote/leader_id/raft_leader_id.rs

View workflow job for this annotation

GitHub Actions / lint

multiple applicable items in scope

Check failure on line 62 in openraft/src/vote/leader_id/raft_leader_id.rs

View workflow job for this annotation

GitHub Actions / openraft-feature-test (nightly)

multiple applicable items in scope

Check failure on line 62 in openraft/src/vote/leader_id/raft_leader_id.rs

View workflow job for this annotation

GitHub Actions / openraft-feature-test (nightly, serde)

multiple applicable items in scope

Check failure on line 62 in openraft/src/vote/leader_id/raft_leader_id.rs

View workflow job for this annotation

GitHub Actions / rt-monoio

multiple applicable items in scope

Check failure on line 62 in openraft/src/vote/leader_id/raft_leader_id.rs

View workflow job for this annotation

GitHub Actions / openraft-feature-test (nightly, serde,singlethreaded)

multiple applicable items in scope

Check failure on line 62 in openraft/src/vote/leader_id/raft_leader_id.rs

View workflow job for this annotation

GitHub Actions / stores (stores/memstore)

multiple applicable items in scope

Check failure on line 62 in openraft/src/vote/leader_id/raft_leader_id.rs

View workflow job for this annotation

GitHub Actions / openraft-test-bench (nightly)

multiple applicable items in scope

Check failure on line 62 in openraft/src/vote/leader_id/raft_leader_id.rs

View workflow job for this annotation

GitHub Actions / test-crate-openraft (stable)

multiple applicable items in scope

Check failure on line 62 in openraft/src/vote/leader_id/raft_leader_id.rs

View workflow job for this annotation

GitHub Actions / test-crate-tests (nightly, 0, single-term-leader)

multiple applicable items in scope

Check failure on line 62 in openraft/src/vote/leader_id/raft_leader_id.rs

View workflow job for this annotation

GitHub Actions / test-crate-tests (stable, 0)

multiple applicable items in scope

Check failure on line 62 in openraft/src/vote/leader_id/raft_leader_id.rs

View workflow job for this annotation

GitHub Actions / test-crate-openraft (nightly)

multiple applicable items in scope

Check failure on line 62 in openraft/src/vote/leader_id/raft_leader_id.rs

View workflow job for this annotation

GitHub Actions / test-crate-tests (nightly, 30)

multiple applicable items in scope

Check failure on line 62 in openraft/src/vote/leader_id/raft_leader_id.rs

View workflow job for this annotation

GitHub Actions / tests-feature-test (nightly)

multiple applicable items in scope

Check failure on line 62 in openraft/src/vote/leader_id/raft_leader_id.rs

View workflow job for this annotation

GitHub Actions / tests-feature-test (nightly, single-term-leader)

multiple applicable items in scope

Check failure on line 62 in openraft/src/vote/leader_id/raft_leader_id.rs

View workflow job for this annotation

GitHub Actions / Build (nightly, bench,serde,bt,singlethreaded)

multiple applicable items in scope
}
}

Expand Down
4 changes: 2 additions & 2 deletions tests/tests/fixtures/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -892,7 +892,7 @@ impl TypedRaftRouter {

if let Some(voted_for) = &expect_voted_for {
assert_eq!(
vote.leader_id().node_id_ref(),
vote.leader_id().node_id(),
Some(voted_for),
"expected node {} to have voted for {}, got {:?}",
id,
Expand Down Expand Up @@ -1022,7 +1022,7 @@ impl RaftNetworkV2<MemConfig> for RaftRouterNetwork {
mut rpc: AppendEntriesRequest<MemConfig>,
_option: RPCOption,
) -> Result<AppendEntriesResponse<MemConfig>, RPCError<MemConfig>> {
let from_id = rpc.vote.leader_id().node_id_ref().cloned().unwrap();
let from_id = rpc.vote.leader_id().node_id().cloned().unwrap();

tracing::debug!("append_entries to id={} {}", self.target, rpc);
self.owner.count_rpc(RPCTypes::AppendEntries);
Expand Down

0 comments on commit c55c962

Please sign in to comment.