diff --git a/examples/raft-kv-memstore-grpc/src/pb_impl/impl_leader_id.rs b/examples/raft-kv-memstore-grpc/src/pb_impl/impl_leader_id.rs index 635006ae5..71a1bb584 100644 --- a/examples/raft-kv-memstore-grpc/src/pb_impl/impl_leader_id.rs +++ b/examples/raft-kv-memstore-grpc/src/pb_impl/impl_leader_id.rs @@ -41,7 +41,7 @@ impl RaftLeaderId for pb::LeaderId { self.term } - fn node_id_ref(&self) -> Option<&u64> { + fn node_id(&self) -> Option<&u64> { Some(&self.node_id) } diff --git a/openraft/src/core/raft_core.rs b/openraft/src/core/raft_core.rs index 05019efa1..72d93ad4d 100644 --- a/openraft/src/core/raft_core.rs +++ b/openraft/src/core/raft_core.rs @@ -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 diff --git a/openraft/src/engine/handler/establish_handler/mod.rs b/openraft/src/engine/handler/establish_handler/mod.rs index 3cf3879fb..1123012aa 100644 --- a/openraft/src/engine/handler/establish_handler/mod.rs +++ b/openraft/src/engine/handler/establish_handler/mod.rs @@ -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" ); diff --git a/openraft/src/engine/handler/vote_handler/mod.rs b/openraft/src/engine/handler/vote_handler/mod.rs index b936075cb..f20da67c8 100644 --- a/openraft/src/engine/handler/vote_handler/mod.rs +++ b/openraft/src/engine/handler/vote_handler/mod.rs @@ -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)" ); diff --git a/openraft/src/proposer/leader.rs b/openraft/src/proposer/leader.rs index f9113b674..107be2a47 100644 --- a/openraft/src/proposer/leader.rs +++ b/openraft/src/proposer/leader.rs @@ -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!( diff --git a/openraft/src/raft_state/mod.rs b/openraft/src/raft_state/mod.rs index b234f04ab..0b9fee114 100644 --- a/openraft/src/raft_state/mod.rs +++ b/openraft/src/raft_state/mod.rs @@ -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 @@ -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); } diff --git a/openraft/src/replication/mod.rs b/openraft/src/replication/mod.rs index 773a2b545..2a58b50ef 100644 --- a/openraft/src/replication/mod.rs +++ b/openraft/src/replication/mod.rs @@ -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, }; diff --git a/openraft/src/vote/leader_id/leader_id_adv.rs b/openraft/src/vote/leader_id/leader_id_adv.rs index 4ef45b9a1..e5b695d5f 100644 --- a/openraft/src/vote/leader_id/leader_id_adv.rs +++ b/openraft/src/vote/leader_id/leader_id_adv.rs @@ -55,7 +55,7 @@ where C: RaftTypeConfig self.term } - fn node_id_ref(&self) -> Option<&C::NodeId> { + fn node_id(&self) -> Option<&C::NodeId> { Some(&self.node_id) } diff --git a/openraft/src/vote/leader_id/leader_id_cmp.rs b/openraft/src/vote/leader_id/leader_id_cmp.rs index 0b8579c78..d0063b810 100644 --- a/openraft/src/vote/leader_id/leader_id_cmp.rs +++ b/openraft/src/vote/leader_id/leader_id_cmp.rs @@ -19,7 +19,7 @@ where C: RaftTypeConfig pub fn std(a: &LID, b: &LID) -> Option where LID: RaftLeaderId { 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), @@ -38,7 +38,7 @@ where C: RaftTypeConfig /// Implements [`PartialOrd`] for LeaderId to allow multiple leaders per term. pub fn adv(a: &LID, b: &LID) -> Option where LID: RaftLeaderId { - 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) } } @@ -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() } diff --git a/openraft/src/vote/leader_id/leader_id_std.rs b/openraft/src/vote/leader_id/leader_id_std.rs index d8adea127..67f1df39e 100644 --- a/openraft/src/vote/leader_id/leader_id_std.rs +++ b/openraft/src/vote/leader_id/leader_id_std.rs @@ -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() } diff --git a/openraft/src/vote/leader_id/raft_leader_id.rs b/openraft/src/vote/leader_id/raft_leader_id.rs index e0a3ad55a..55974e3ab 100644 --- a/openraft/src/vote/leader_id/raft_leader_id.rs +++ b/openraft/src/vote/leader_id/raft_leader_id.rs @@ -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. /// @@ -58,8 +58,8 @@ where Self::new(term, node_id).to_committed() } - fn node_id(&self) -> Option { - self.node_id_ref().cloned() + fn to_node_id(&self) -> Option { + self.node_id().cloned() } } diff --git a/tests/tests/fixtures/mod.rs b/tests/tests/fixtures/mod.rs index a823206d2..9c7fce9f4 100644 --- a/tests/tests/fixtures/mod.rs +++ b/tests/tests/fixtures/mod.rs @@ -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, @@ -1022,7 +1022,7 @@ impl RaftNetworkV2 for RaftRouterNetwork { mut rpc: AppendEntriesRequest, _option: RPCOption, ) -> Result, RPCError> { - 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); @@ -1092,7 +1092,7 @@ impl RaftNetworkV2 for RaftRouterNetwork { _cancel: impl Future + OptionalSend + 'static, _option: RPCOption, ) -> Result, StreamingError> { - let from_id = vote.leader_id().node_id().unwrap(); + let from_id = vote.leader_id().to_node_id().unwrap(); self.owner.count_rpc(RPCTypes::InstallSnapshot); self.owner.call_rpc_pre_hook(snapshot.clone(), from_id, self.target)?; @@ -1118,7 +1118,7 @@ impl RaftNetworkV2 for RaftRouterNetwork { rpc: VoteRequest, _option: RPCOption, ) -> Result, RPCError> { - let from_id = rpc.vote.leader_id().node_id().unwrap(); + let from_id = rpc.vote.leader_id().to_node_id().unwrap(); self.owner.count_rpc(RPCTypes::Vote); self.owner.call_rpc_pre_hook(rpc.clone(), from_id, self.target)?; @@ -1143,7 +1143,7 @@ impl RaftNetworkV2 for RaftRouterNetwork { rpc: TransferLeaderRequest, _option: RPCOption, ) -> Result<(), RPCError> { - let from_id = rpc.from_leader().leader_id().node_id().unwrap(); + let from_id = rpc.from_leader().leader_id().to_node_id().unwrap(); self.owner.count_rpc(RPCTypes::TransferLeader); self.owner.call_rpc_pre_hook(rpc.clone(), from_id, self.target)?; diff --git a/tests/tests/life_cycle/t50_single_follower_restart.rs b/tests/tests/life_cycle/t50_single_follower_restart.rs index 2e5d40da5..a96c40ad7 100644 --- a/tests/tests/life_cycle/t50_single_follower_restart.rs +++ b/tests/tests/life_cycle/t50_single_follower_restart.rs @@ -48,7 +48,7 @@ async fn single_follower_restart() -> anyhow::Result<()> { let v = sto.read_vote().await?.unwrap_or_default(); // Set a non-committed vote so that the node restarts as a follower. - sto.save_vote(&Vote::new(v.leader_id.term() + 1, v.leader_id.node_id().unwrap())).await?; + sto.save_vote(&Vote::new(v.leader_id.term() + 1, v.leader_id.to_node_id().unwrap())).await?; tracing::info!(log_index, "--- restart node-0");