Skip to content

Commit

Permalink
Refactor: impl Display for Membership
Browse files Browse the repository at this point in the history
  • Loading branch information
drmingdrmer committed Mar 24, 2024
1 parent 3b90a91 commit aab7dad
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 28 deletions.
55 changes: 31 additions & 24 deletions openraft/src/membership/membership.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,50 +47,57 @@ impl<C> fmt::Display for Membership<C>
where C: RaftTypeConfig
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.summary())
}
}
write!(f, "{{voters:[",)?;

impl<C> MessageSummary<Membership<C>> for Membership<C>
where C: RaftTypeConfig
{
fn summary(&self) -> String {
let mut res = vec!["voters:[".to_string()];
for (i, c) in self.configs.iter().enumerate() {
if i > 0 {
res.push(",".to_string());
write!(f, ",",)?;
}

res.push("{".to_string());
write!(f, "{{",)?;
for (i, node_id) in c.iter().enumerate() {
if i > 0 {
res.push(",".to_string());
write!(f, ",",)?;
}
res.push(format!("{}", node_id));
write!(f, "{node_id}:")?;

let n = self.get_node(node_id).map(|x| format!("{:?}", x)).unwrap_or_else(|| "None".to_string());
res.push(format!(":{{{}}}", n));
if let Some(n) = self.get_node(node_id) {
write!(f, "{n:?}")?;
} else {
write!(f, "None")?;
}
}
res.push("}".to_string());
write!(f, "}}")?;
}
res.push("]".to_string());
write!(f, "]")?;

let all_node_ids = self.nodes.keys().cloned().collect::<BTreeSet<_>>();
let members = self.voter_ids().collect::<BTreeSet<_>>();

res.push(", learners:[".to_string());
write!(f, ", learners:[")?;

for (learner_cnt, learner_id) in all_node_ids.difference(&members).enumerate() {
if learner_cnt > 0 {
res.push(",".to_string());
write!(f, ",")?;
}

res.push(format!("{}", learner_id));

let n = self.get_node(learner_id).map(|x| format!("{:?}", x)).unwrap_or_else(|| "None".to_string());
res.push(format!(":{{{}}}", n));
write!(f, "{learner_id}:")?;
if let Some(n) = self.get_node(learner_id) {
write!(f, "{n:?}")?;
} else {
write!(f, "None")?;
}
}
res.push("]".to_string());
res.join("")
write!(f, "]}}")?;
Ok(())
}
}

impl<C> MessageSummary<Membership<C>> for Membership<C>
where C: RaftTypeConfig
{
fn summary(&self) -> String {
self.to_string()
}
}

Expand Down
6 changes: 3 additions & 3 deletions openraft/src/membership/membership_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ fn test_membership_summary() -> anyhow::Result<()> {
};

let m = Membership::<UTConfig>::new(vec![btreeset! {1,2}, btreeset! {3}], None);
assert_eq!("voters:[{1:{()},2:{()}},{3:{()}}], learners:[]", m.summary());
assert_eq!("{voters:[{1:(),2:()},{3:()}], learners:[]}", m.summary());

let m = Membership::<UTConfig>::new(vec![btreeset! {1,2}, btreeset! {3}], Some(btreeset! {4}));
assert_eq!("voters:[{1:{()},2:{()}},{3:{()}}], learners:[4:{()}]", m.summary());
assert_eq!("{voters:[{1:(),2:()},{3:()}], learners:[4:()]}", m.summary());

let m = Membership::<UTConfig<TestNode>>::new_unchecked(vec![btreeset! {1,2}, btreeset! {3}], btreemap! {
1=>node("127.0.0.1", "k1"),
Expand All @@ -51,7 +51,7 @@ fn test_membership_summary() -> anyhow::Result<()> {

});
assert_eq!(
r#"voters:[{1:{TestNode { addr: "127.0.0.1", data: {"k1": "k1"} }},2:{TestNode { addr: "127.0.0.2", data: {"k2": "k2"} }}},{3:{TestNode { addr: "127.0.0.3", data: {"k3": "k3"} }}}], learners:[4:{TestNode { addr: "127.0.0.4", data: {"k4": "k4"} }}]"#,
r#"{voters:[{1:TestNode { addr: "127.0.0.1", data: {"k1": "k1"} },2:TestNode { addr: "127.0.0.2", data: {"k2": "k2"} }},{3:TestNode { addr: "127.0.0.3", data: {"k3": "k3"} }}], learners:[4:TestNode { addr: "127.0.0.4", data: {"k4": "k4"} }]}"#,
m.summary()
);

Expand Down
2 changes: 1 addition & 1 deletion openraft/src/type_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ pub trait RaftTypeConfig:
}

#[allow(dead_code)]
/// Type alias for types used in `RaftTypeConfig`.
pub mod alias {
//! Type alias for types used in `RaftTypeConfig`.

pub type DOf<C> = <C as crate::RaftTypeConfig>::D;
pub type ROf<C> = <C as crate::RaftTypeConfig>::R;
Expand Down

0 comments on commit aab7dad

Please sign in to comment.