From 7b1d4660d449d6f35230d0253e9564612cdfb7e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E7=82=8E=E6=B3=BC?= Date: Wed, 6 Apr 2022 21:50:02 +0800 Subject: [PATCH] Change: rename RaftMetrics.leader_metrics to replication - part of #229 --- openraft/src/core/mod.rs | 19 ++++++++++------- openraft/src/metrics/raft_metrics.rs | 22 ++++++++++---------- openraft/src/metrics/wait_test.rs | 2 +- openraft/tests/metrics/t30_leader_metrics.rs | 10 ++++----- 4 files changed, 29 insertions(+), 24 deletions(-) diff --git a/openraft/src/core/mod.rs b/openraft/src/core/mod.rs index 0ba4858c9..cfeb4192e 100644 --- a/openraft/src/core/mod.rs +++ b/openraft/src/core/mod.rs @@ -398,24 +398,29 @@ impl, S: RaftStorage> RaftCore>>>) { - let leader_metrics = match leader_metrics { + fn report_metrics(&self, replication: Update>>>) { + let replication = match replication { Update::Update(v) => v, - Update::AsIs => self.tx_metrics.borrow().leader_metrics.clone(), + Update::AsIs => self.tx_metrics.borrow().replication.clone(), }; let m = RaftMetrics { running_state: Ok(()), - id: self.id, - state: self.target_state, + + // --- data --- current_term: self.vote.term, last_log_index: self.last_log_id.map(|id| id.index), last_applied: self.last_applied, + snapshot: self.snapshot_last_log_id, + + // --- cluster --- + state: self.target_state, current_leader: self.current_leader(), membership_config: self.effective_membership.clone(), - snapshot: self.snapshot_last_log_id, - leader_metrics, + + // --- replication --- + replication, }; { diff --git a/openraft/src/metrics/raft_metrics.rs b/openraft/src/metrics/raft_metrics.rs index a59755a72..576215dc0 100644 --- a/openraft/src/metrics/raft_metrics.rs +++ b/openraft/src/metrics/raft_metrics.rs @@ -49,21 +49,21 @@ pub struct RaftMetrics { // --- replication --- // --- /// The metrics about the leader. It is Some() only when this node is leader. - pub leader_metrics: Option>>, + pub replication: Option>>, } impl MessageSummary for RaftMetrics { fn summary(&self) -> String { format!("Metrics{{id:{},{:?}, term:{}, last_log:{:?}, last_applied:{:?}, leader:{:?}, membership:{}, snapshot:{:?}, replication:{}", - self.id, - self.state, - self.current_term, - self.last_log_index, - self.last_applied, - self.current_leader, - self.membership_config.summary(), - self.snapshot, - self.leader_metrics.as_ref().map(|x| x.summary()).unwrap_or_default(), + self.id, + self.state, + self.current_term, + self.last_log_index, + self.last_applied, + self.current_leader, + self.membership_config.summary(), + self.snapshot, + self.replication.as_ref().map(|x| x.summary()).unwrap_or_default(), ) } } @@ -80,7 +80,7 @@ impl RaftMetrics { current_leader: None, membership_config: Arc::new(EffectiveMembership::default()), snapshot: None, - leader_metrics: None, + replication: None, } } } diff --git a/openraft/src/metrics/wait_test.rs b/openraft/src/metrics/wait_test.rs index a1a01084a..22e1656c8 100644 --- a/openraft/src/metrics/wait_test.rs +++ b/openraft/src/metrics/wait_test.rs @@ -209,7 +209,7 @@ fn init_wait_test() -> (RaftMetrics, Wait, watch::Sende )), snapshot: None, - leader_metrics: None, + replication: None, }; let (tx, rx) = watch::channel(init.clone()); let w = Wait { diff --git a/openraft/tests/metrics/t30_leader_metrics.rs b/openraft/tests/metrics/t30_leader_metrics.rs index 9c5e744e9..10b4e2d74 100644 --- a/openraft/tests/metrics/t30_leader_metrics.rs +++ b/openraft/tests/metrics/t30_leader_metrics.rs @@ -65,7 +65,7 @@ async fn leader_metrics() -> Result<()> { .wait_for_metrics( &0, |x| { - if let Some(ref q) = x.leader_metrics { + if let Some(ref q) = x.replication { q.data().replication.is_empty() } else { false @@ -113,7 +113,7 @@ async fn leader_metrics() -> Result<()> { .wait_for_metrics( &0, |x| { - if let Some(ref q) = x.leader_metrics { + if let Some(ref q) = x.replication { q.data().replication == want_repl } else { false @@ -162,7 +162,7 @@ async fn leader_metrics() -> Result<()> { .wait_for_metrics( &0, |x| { - if let Some(ref q) = x.leader_metrics { + if let Some(ref q) = x.replication { q.data().replication == want_repl } else { false @@ -190,7 +190,7 @@ async fn leader_metrics() -> Result<()> { router .wait_for_metrics( &leader, - |x| x.leader_metrics.is_none(), + |x| x.replication.is_none(), timeout(), "node 0 should close all replication", ) @@ -220,7 +220,7 @@ async fn leader_metrics() -> Result<()> { router .wait_for_metrics( &leader, - |x| x.leader_metrics.is_some(), + |x| x.replication.is_some(), timeout(), "new leader spawns replication", )