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 f18e3cf commit 9bc0aef
Show file tree
Hide file tree
Showing 13 changed files with 22 additions and 22 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
6 changes: 3 additions & 3 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 @@ -58,8 +58,8 @@ where
Self::new(term, node_id).to_committed()
}

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

Expand Down
10 changes: 5 additions & 5 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 Expand Up @@ -1092,7 +1092,7 @@ impl RaftNetworkV2<MemConfig> for RaftRouterNetwork {
_cancel: impl Future<Output = ReplicationClosed> + OptionalSend + 'static,
_option: RPCOption,
) -> Result<SnapshotResponse<MemConfig>, StreamingError<MemConfig>> {
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)?;
Expand All @@ -1118,7 +1118,7 @@ impl RaftNetworkV2<MemConfig> for RaftRouterNetwork {
rpc: VoteRequest<MemConfig>,
_option: RPCOption,
) -> Result<VoteResponse<MemConfig>, RPCError<MemConfig>> {
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)?;
Expand All @@ -1143,7 +1143,7 @@ impl RaftNetworkV2<MemConfig> for RaftRouterNetwork {
rpc: TransferLeaderRequest<MemConfig>,
_option: RPCOption,
) -> Result<(), RPCError<MemConfig>> {
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)?;
Expand Down
2 changes: 1 addition & 1 deletion tests/tests/life_cycle/t50_single_follower_restart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down

0 comments on commit 9bc0aef

Please sign in to comment.