-
Notifications
You must be signed in to change notification settings - Fork 160
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor: update gRPC example to adapt to
LeaderId
changes
- Update the `raft-kv-memstore-grpc` example to use the protobuf-defined `LeaderId`. - Automatically implement `CommittedLeaderId` for all types. - Add `openraft::vote::LeaderIdCompare` to provide comparison functions for both single-leader-per-term and multi-leader-per-term implementations.
- Loading branch information
1 parent
6908a3c
commit 959a663
Showing
15 changed files
with
245 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
examples/raft-kv-memstore-grpc/src/pb_impl/impl_leader_id.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
//! Implement [`RaftLeaderId`] for protobuf defined LeaderId, so that it can be used in OpenRaft | ||
use std::cmp::Ordering; | ||
use std::fmt; | ||
|
||
use openraft::vote::LeaderIdCompare; | ||
use openraft::vote::RaftLeaderId; | ||
|
||
use crate::protobuf as pb; | ||
use crate::TypeConfig; | ||
|
||
/// Implements PartialOrd for LeaderId to enforce the standard Raft behavior of at most one leader | ||
/// per term. | ||
/// | ||
/// In standard Raft, each term can have at most one leader. This is enforced by making leader IDs | ||
/// with the same term incomparable (returning None), unless they refer to the same node. | ||
/// | ||
/// This differs from the [`PartialOrd`] default implementation which would allow multiple leaders | ||
/// in the same term by comparing node IDs. | ||
impl PartialOrd for pb::LeaderId { | ||
#[inline] | ||
fn partial_cmp(&self, other: &Self) -> Option<Ordering> { | ||
LeaderIdCompare::std(self, other) | ||
} | ||
} | ||
|
||
impl fmt::Display for pb::LeaderId { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
write!(f, "T{}-N{}", self.term, self.node_id) | ||
} | ||
} | ||
|
||
impl RaftLeaderId<TypeConfig> for pb::LeaderId { | ||
type Committed = u64; | ||
|
||
fn new(term: u64, node_id: u64) -> Self { | ||
Self { term, node_id } | ||
} | ||
|
||
fn term(&self) -> u64 { | ||
self.term | ||
} | ||
|
||
fn node_id_ref(&self) -> Option<&u64> { | ||
Some(&self.node_id) | ||
} | ||
|
||
fn to_committed(&self) -> Self::Committed { | ||
self.term | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
//! Implements traits for protobuf types | ||
mod impl_leader_id; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.