-
Notifications
You must be signed in to change notification settings - Fork 161
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 7b51cf9
Showing
11 changed files
with
236 additions
and
75 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
use std::cmp::Ordering; | ||
use std::marker::PhantomData; | ||
|
||
use crate::vote::RaftLeaderId; | ||
use crate::RaftTypeConfig; | ||
|
||
/// Provide comparison functions for [`RaftLeaderId`] implementations. | ||
pub struct LeaderIdCompare<C>(PhantomData<C>); | ||
|
||
impl<C> LeaderIdCompare<C> | ||
where C: RaftTypeConfig | ||
{ | ||
/// 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. | ||
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()) { | ||
(None, None) => Some(Ordering::Equal), | ||
(Some(_), None) => Some(Ordering::Greater), | ||
(None, Some(_)) => Some(Ordering::Less), | ||
(Some(a), Some(b)) => { | ||
if a == b { | ||
Some(Ordering::Equal) | ||
} else { | ||
None | ||
} | ||
} | ||
}, | ||
cmp => Some(cmp), | ||
} | ||
} | ||
|
||
/// 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())); | ||
Some(res) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use std::cmp::Ordering; | ||
|
||
use crate::engine::testing::UTConfig; | ||
use crate::vote::RaftLeaderId; | ||
|
||
#[derive(Debug, PartialEq, Eq, Default, Clone, PartialOrd, derive_more::Display)] | ||
#[display("T{}-N{:?}", _0, _1)] | ||
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))] | ||
struct LeaderId(u64, Option<u64>); | ||
|
||
impl RaftLeaderId<UTConfig> for LeaderId { | ||
type Committed = u64; | ||
|
||
fn new(term: u64, node_id: u64) -> Self { | ||
Self(term, Some(node_id)) | ||
} | ||
|
||
fn term(&self) -> u64 { | ||
self.0 | ||
} | ||
|
||
fn node_id_ref(&self) -> Option<&u64> { | ||
self.1.as_ref() | ||
} | ||
|
||
fn to_committed(&self) -> Self::Committed { | ||
self.0 | ||
} | ||
} | ||
|
||
#[test] | ||
fn test_std_cmp() { | ||
use Ordering::*; | ||
|
||
use super::LeaderIdCompare as Cmp; | ||
|
||
let lid = |term, node_id| LeaderId(term, Some(node_id)); | ||
let lid_none = |term| LeaderId(term, None); | ||
|
||
// Compare term first | ||
assert_eq!(Cmp::std(&lid(2, 2), &lid(1, 2)), Some(Greater)); | ||
assert_eq!(Cmp::std(&lid(1, 2), &lid(2, 2)), Some(Less)); | ||
|
||
// Equal term, Some > None | ||
assert_eq!(Cmp::std(&lid(2, 2), &lid_none(2)), Some(Greater)); | ||
assert_eq!(Cmp::std(&lid_none(2), &lid(2, 2)), Some(Less)); | ||
|
||
// Equal | ||
assert_eq!(Cmp::std(&lid(2, 2), &lid(2, 2)), Some(Equal)); | ||
|
||
// Incomparable | ||
assert_eq!(Cmp::std(&lid(2, 2), &lid(2, 1)), None); | ||
assert_eq!(Cmp::std(&lid(2, 1), &lid(2, 2)), None); | ||
assert_eq!(Cmp::std(&lid(2, 2), &lid(2, 3)), None); | ||
} | ||
|
||
#[test] | ||
fn test_adv_cmp() { | ||
use Ordering::*; | ||
|
||
use super::LeaderIdCompare as Cmp; | ||
|
||
let lid = |term, node_id| LeaderId(term, Some(node_id)); | ||
|
||
// Compare term first | ||
assert_eq!(Cmp::adv(&lid(2, 2), &lid(1, 2)), Some(Greater)); | ||
assert_eq!(Cmp::adv(&lid(1, 2), &lid(2, 2)), Some(Less)); | ||
|
||
// Equal term | ||
assert_eq!(Cmp::adv(&lid(2, 2), &lid(2, 1)), Some(Greater)); | ||
assert_eq!(Cmp::adv(&lid(2, 1), &lid(2, 2)), Some(Less)); | ||
|
||
// Equal term, node_id | ||
assert_eq!(Cmp::adv(&lid(2, 2), &lid(2, 2)), Some(Equal)); | ||
} | ||
} |
Oops, something went wrong.