-
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.
Merge pull request #967 from drmingdrmer/55-wait
Feature: add `Wait::eq()` and `ge()` to await a metics
- Loading branch information
Showing
5 changed files
with
221 additions
and
52 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
use std::cmp::Ordering; | ||
|
||
use crate::metrics::metric_display::MetricDisplay; | ||
use crate::LogId; | ||
use crate::LogIdOptionExt; | ||
use crate::Node; | ||
use crate::NodeId; | ||
use crate::RaftMetrics; | ||
use crate::Vote; | ||
|
||
/// A metric entry of a Raft node. | ||
/// | ||
/// This is used to specify which metric to observe. | ||
#[derive(Debug)] | ||
pub enum Metric<NID> | ||
where NID: NodeId | ||
{ | ||
Term(u64), | ||
Vote(Vote<NID>), | ||
LastLogIndex(Option<u64>), | ||
Applied(Option<LogId<NID>>), | ||
AppliedIndex(Option<u64>), | ||
Snapshot(Option<LogId<NID>>), | ||
Purged(Option<LogId<NID>>), | ||
} | ||
|
||
impl<NID> Metric<NID> | ||
where NID: NodeId | ||
{ | ||
pub(crate) fn name(&self) -> &'static str { | ||
match self { | ||
Metric::Term(_) => "term", | ||
Metric::Vote(_) => "vote", | ||
Metric::LastLogIndex(_) => "last_log_index", | ||
Metric::Applied(_) => "applied", | ||
Metric::AppliedIndex(_) => "applied_index", | ||
Metric::Snapshot(_) => "snapshot", | ||
Metric::Purged(_) => "purged", | ||
} | ||
} | ||
|
||
pub(crate) fn value(&self) -> MetricDisplay<'_, NID> { | ||
MetricDisplay { metric: self } | ||
} | ||
} | ||
|
||
/// Metric can be compared with RaftMetrics by comparing the corresponding field of RaftMetrics. | ||
impl<NID, N> PartialEq<Metric<NID>> for RaftMetrics<NID, N> | ||
where | ||
NID: NodeId, | ||
N: Node, | ||
{ | ||
fn eq(&self, other: &Metric<NID>) -> bool { | ||
match other { | ||
Metric::Term(v) => self.current_term == *v, | ||
Metric::Vote(v) => &self.vote == v, | ||
Metric::LastLogIndex(v) => self.last_log_index == *v, | ||
Metric::Applied(v) => &self.last_applied == v, | ||
Metric::AppliedIndex(v) => self.last_applied.index() == *v, | ||
Metric::Snapshot(v) => &self.snapshot == v, | ||
Metric::Purged(v) => &self.purged == v, | ||
} | ||
} | ||
} | ||
|
||
/// Metric can be compared with RaftMetrics by comparing the corresponding field of RaftMetrics. | ||
impl<NID, N> PartialOrd<Metric<NID>> for RaftMetrics<NID, N> | ||
where | ||
NID: NodeId, | ||
N: Node, | ||
{ | ||
fn partial_cmp(&self, other: &Metric<NID>) -> Option<Ordering> { | ||
match other { | ||
Metric::Term(v) => Some(self.current_term.cmp(v)), | ||
Metric::Vote(v) => self.vote.partial_cmp(v), | ||
Metric::LastLogIndex(v) => Some(self.last_log_index.cmp(v)), | ||
Metric::Applied(v) => Some(self.last_applied.cmp(v)), | ||
Metric::AppliedIndex(v) => Some(self.last_applied.index().cmp(v)), | ||
Metric::Snapshot(v) => Some(self.snapshot.cmp(v)), | ||
Metric::Purged(v) => Some(self.purged.cmp(v)), | ||
} | ||
} | ||
} |
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,29 @@ | ||
use std::fmt; | ||
use std::fmt::Formatter; | ||
|
||
use crate::display_ext::DisplayOption; | ||
use crate::metrics::Metric; | ||
use crate::NodeId; | ||
|
||
/// Display the value of a metric. | ||
pub(crate) struct MetricDisplay<'a, NID> | ||
where NID: NodeId | ||
{ | ||
pub(crate) metric: &'a Metric<NID>, | ||
} | ||
|
||
impl<'a, NID> fmt::Display for MetricDisplay<'a, NID> | ||
where NID: NodeId | ||
{ | ||
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { | ||
match self.metric { | ||
Metric::Term(v) => write!(f, "{}", v), | ||
Metric::Vote(v) => write!(f, "{}", v), | ||
Metric::LastLogIndex(v) => write!(f, "{}", DisplayOption(v)), | ||
Metric::Applied(v) => write!(f, "{}", DisplayOption(v)), | ||
Metric::AppliedIndex(v) => write!(f, "{}", DisplayOption(v)), | ||
Metric::Snapshot(v) => write!(f, "{}", DisplayOption(v)), | ||
Metric::Purged(v) => write!(f, "{}", DisplayOption(v)), | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
use std::fmt; | ||
|
||
use crate::metrics::metric_display::MetricDisplay; | ||
use crate::metrics::Metric; | ||
use crate::NodeId; | ||
|
||
/// A condition that the application wait for. | ||
#[derive(Debug)] | ||
pub(crate) enum Condition<NID> | ||
where NID: NodeId | ||
{ | ||
GE(Metric<NID>), | ||
EQ(Metric<NID>), | ||
} | ||
|
||
impl<NID> Condition<NID> | ||
where NID: NodeId | ||
{ | ||
/// Build a new condition which the application will await to meet or exceed. | ||
pub(crate) fn ge(v: Metric<NID>) -> Self { | ||
Self::GE(v) | ||
} | ||
|
||
/// Build a new condition which the application will await to meet. | ||
pub(crate) fn eq(v: Metric<NID>) -> Self { | ||
Self::EQ(v) | ||
} | ||
|
||
pub(crate) fn name(&self) -> &'static str { | ||
match self { | ||
Condition::GE(v) => v.name(), | ||
Condition::EQ(v) => v.name(), | ||
} | ||
} | ||
|
||
pub(crate) fn op(&self) -> &'static str { | ||
match self { | ||
Condition::GE(_) => ">=", | ||
Condition::EQ(_) => "==", | ||
} | ||
} | ||
|
||
pub(crate) fn value(&self) -> MetricDisplay<'_, NID> { | ||
match self { | ||
Condition::GE(v) => v.value(), | ||
Condition::EQ(v) => v.value(), | ||
} | ||
} | ||
} | ||
|
||
impl<NID> fmt::Display for Condition<NID> | ||
where NID: NodeId | ||
{ | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
write!(f, "{}{}{}", self.name(), self.op(), self.value()) | ||
} | ||
} |