Skip to content

Commit

Permalink
Change: Consolidate RaftMetrics type parameters into C
Browse files Browse the repository at this point in the history
The `RaftMetrics` type, along with related metric types, previously used
separate type parameters (`NID`, `N`) which have now been replaced with
a single generic parameter `C` bound by `RaftTypeConfig`. This
modification simplifies the type signatures and usage.

Upgrade tip:

To adapt to this change, update the metric-related type parameters from
separate `NID` and `N` to the single generic `C` constrained by
`RaftTypeConfig`:

```rust
RaftMetrics<NID, N>         --> RaftMetrics<C>
RaftDataMetrics<NID>        --> RaftDataMetrics<C>
RaftServerMetrics<NID, NID> --> RaftServerMetrics<C>
Metric<NID>                 --> Metric<C>
Wait<NID, N, A>             --> Wait<C>
```
  • Loading branch information
drmingdrmer committed Mar 16, 2024
1 parent 1876b7f commit 717ed63
Show file tree
Hide file tree
Showing 21 changed files with 157 additions and 204 deletions.
3 changes: 2 additions & 1 deletion examples/raft-kv-memstore-generic-snapshot-data/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use crate::decode;
use crate::encode;
use crate::typ;
use crate::NodeId;
use crate::TypeConfig;

pub async fn write(app: &mut App, req: String) -> String {
let res = app.raft.client_write(decode(&req)).await;
Expand Down Expand Up @@ -99,6 +100,6 @@ pub async fn init(app: &mut App) -> String {
pub async fn metrics(app: &mut App) -> String {
let metrics = app.raft.metrics().borrow().clone();

let res: Result<RaftMetrics<NodeId, BasicNode>, Infallible> = Ok(metrics);
let res: Result<RaftMetrics<TypeConfig>, Infallible> = Ok(metrics);
encode(res)
}
2 changes: 1 addition & 1 deletion examples/raft-kv-memstore-generic-snapshot-data/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ pub mod typ {
pub type RPCError<E = openraft::error::Infallible> = openraft::error::RPCError<NodeId, BasicNode, RaftError<E>>;
pub type StreamingError<E> = openraft::error::StreamingError<TypeConfig, E>;

pub type RaftMetrics = openraft::RaftMetrics<NodeId, BasicNode>;
pub type RaftMetrics = openraft::RaftMetrics<TypeConfig>;

pub type ClientWriteError = openraft::error::ClientWriteError<NodeId, BasicNode>;
pub type CheckIsLeaderError = openraft::error::CheckIsLeaderError<NodeId, BasicNode>;
Expand Down
3 changes: 2 additions & 1 deletion examples/raft-kv-memstore-opendal-snapshot-data/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use crate::decode;
use crate::encode;
use crate::typ;
use crate::NodeId;
use crate::TypeConfig;

pub async fn write(app: &mut App, req: String) -> String {
let res = app.raft.client_write(decode(&req)).await;
Expand Down Expand Up @@ -99,6 +100,6 @@ pub async fn init(app: &mut App) -> String {
pub async fn metrics(app: &mut App) -> String {
let metrics = app.raft.metrics().borrow().clone();

let res: Result<RaftMetrics<NodeId, BasicNode>, Infallible> = Ok(metrics);
let res: Result<RaftMetrics<TypeConfig>, Infallible> = Ok(metrics);
encode(res)
}
2 changes: 1 addition & 1 deletion examples/raft-kv-memstore-opendal-snapshot-data/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ pub mod typ {
pub type RPCError<E = openraft::error::Infallible> = openraft::error::RPCError<NodeId, BasicNode, RaftError<E>>;
pub type StreamingError<E> = openraft::error::StreamingError<TypeConfig, E>;

pub type RaftMetrics = openraft::RaftMetrics<NodeId, BasicNode>;
pub type RaftMetrics = openraft::RaftMetrics<TypeConfig>;

pub type ClientWriteError = openraft::error::ClientWriteError<NodeId, BasicNode>;
pub type CheckIsLeaderError = openraft::error::CheckIsLeaderError<NodeId, BasicNode>;
Expand Down
3 changes: 2 additions & 1 deletion examples/raft-kv-memstore-singlethreaded/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use crate::app::App;
use crate::decode;
use crate::encode;
use crate::NodeId;
use crate::TypeConfig;

pub async fn write(app: &mut App, req: String) -> String {
let res = app.raft.client_write(decode(&req)).await;
Expand Down Expand Up @@ -88,6 +89,6 @@ pub async fn init(app: &mut App) -> String {
pub async fn metrics(app: &mut App) -> String {
let metrics = app.raft.metrics().borrow().clone();

let res: Result<RaftMetrics<NodeId, BasicNode>, Infallible> = Ok(metrics);
let res: Result<RaftMetrics<TypeConfig>, Infallible> = Ok(metrics);
encode(res)
}
2 changes: 1 addition & 1 deletion examples/raft-kv-memstore-singlethreaded/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ pub mod typ {
pub type RaftError<E = openraft::error::Infallible> = openraft::error::RaftError<NodeId, E>;
pub type RPCError<E = openraft::error::Infallible> = openraft::error::RPCError<NodeId, BasicNode, RaftError<E>>;

pub type RaftMetrics = openraft::RaftMetrics<NodeId, BasicNode>;
pub type RaftMetrics = openraft::RaftMetrics<TypeConfig>;

pub type ClientWriteError = openraft::error::ClientWriteError<NodeId, BasicNode>;
pub type CheckIsLeaderError = openraft::error::CheckIsLeaderError<NodeId, BasicNode>;
Expand Down
4 changes: 2 additions & 2 deletions examples/raft-kv-memstore/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use std::time::Duration;
use openraft::error::ForwardToLeader;
use openraft::error::NetworkError;
use openraft::error::RemoteError;
use openraft::BasicNode;
use openraft::RaftMetrics;
use openraft::TryAsRef;
use serde::de::DeserializeOwned;
Expand All @@ -17,6 +16,7 @@ use tokio::time::timeout;
use crate::typ;
use crate::NodeId;
use crate::Request;
use crate::TypeConfig;

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Empty {}
Expand Down Expand Up @@ -103,7 +103,7 @@ impl ExampleClient {
/// Metrics contains various information about the cluster, such as current leader,
/// membership config, replication status etc.
/// See [`RaftMetrics`].
pub async fn metrics(&self) -> Result<RaftMetrics<NodeId, BasicNode>, typ::RPCError> {
pub async fn metrics(&self) -> Result<RaftMetrics<TypeConfig>, typ::RPCError> {
self.do_send_rpc_to_leader("metrics", None::<&()>).await
}

Expand Down
3 changes: 2 additions & 1 deletion examples/raft-kv-memstore/src/network/management.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use openraft::RaftMetrics;

use crate::app::App;
use crate::NodeId;
use crate::TypeConfig;

// --- Cluster management

Expand Down Expand Up @@ -49,6 +50,6 @@ pub async fn init(app: Data<App>) -> actix_web::Result<impl Responder> {
pub async fn metrics(app: Data<App>) -> actix_web::Result<impl Responder> {
let metrics = app.raft.metrics().borrow().clone();

let res: Result<RaftMetrics<NodeId, BasicNode>, Infallible> = Ok(metrics);
let res: Result<RaftMetrics<TypeConfig>, Infallible> = Ok(metrics);
Ok(Json(res))
}
3 changes: 2 additions & 1 deletion examples/raft-kv-rocksdb/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use crate::typ;
use crate::Node;
use crate::NodeId;
use crate::Request;
use crate::TypeConfig;

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Empty {}
Expand Down Expand Up @@ -102,7 +103,7 @@ impl ExampleClient {
/// Metrics contains various information about the cluster, such as current leader,
/// membership config, replication status etc.
/// See [`RaftMetrics`].
pub async fn metrics(&self) -> Result<RaftMetrics<NodeId, Node>, typ::RPCError> {
pub async fn metrics(&self) -> Result<RaftMetrics<TypeConfig>, typ::RPCError> {
self.do_send_rpc_to_leader("cluster/metrics", None::<&()>).await
}

Expand Down
3 changes: 2 additions & 1 deletion examples/raft-kv-rocksdb/src/network/management.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use crate::app::App;
use crate::Node;
use crate::NodeId;
use crate::Server;
use crate::TypeConfig;

// --- Cluster management

Expand Down Expand Up @@ -60,6 +61,6 @@ async fn init(req: Request<Arc<App>>) -> tide::Result {
async fn metrics(req: Request<Arc<App>>) -> tide::Result {
let metrics = req.state().raft.metrics().borrow().clone();

let res: Result<RaftMetrics<NodeId, Node>, Infallible> = Ok(metrics);
let res: Result<RaftMetrics<TypeConfig>, Infallible> = Ok(metrics);
Ok(Response::builder(StatusCode::Ok).body(Body::from_json(&res)?).build())
}
6 changes: 3 additions & 3 deletions openraft/src/core/raft_core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,9 +196,9 @@ where
/// A Receiver to receive callback from other components.
pub(crate) rx_notify: mpsc::UnboundedReceiver<Notify<C>>,

pub(crate) tx_metrics: watch::Sender<RaftMetrics<C::NodeId, C::Node>>,
pub(crate) tx_data_metrics: watch::Sender<RaftDataMetrics<C::NodeId>>,
pub(crate) tx_server_metrics: watch::Sender<RaftServerMetrics<C::NodeId, C::Node>>,
pub(crate) tx_metrics: watch::Sender<RaftMetrics<C>>,
pub(crate) tx_data_metrics: watch::Sender<RaftDataMetrics<C>>,
pub(crate) tx_server_metrics: watch::Sender<RaftServerMetrics<C>>,

pub(crate) command_state: CommandState,

Expand Down
2 changes: 1 addition & 1 deletion openraft/src/engine/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ mod tests {
mod startup_test;
mod trigger_purge_log_test;
}
#[cfg(test)] mod testing;
#[cfg(test)] pub(crate) mod testing;

pub(crate) use command::Command;
pub(crate) use command::Condition;
Expand Down
37 changes: 16 additions & 21 deletions openraft/src/metrics/metric.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,28 @@ 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::RaftTypeConfig;
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
pub enum Metric<C>
where C: RaftTypeConfig
{
Term(u64),
Vote(Vote<NID>),
Vote(Vote<C::NodeId>),
LastLogIndex(Option<u64>),
Applied(Option<LogId<NID>>),
Applied(Option<LogId<C::NodeId>>),
AppliedIndex(Option<u64>),
Snapshot(Option<LogId<NID>>),
Purged(Option<LogId<NID>>),
Snapshot(Option<LogId<C::NodeId>>),
Purged(Option<LogId<C::NodeId>>),
}

impl<NID> Metric<NID>
where NID: NodeId
impl<C> Metric<C>
where C: RaftTypeConfig
{
pub(crate) fn name(&self) -> &'static str {
match self {
Expand All @@ -39,18 +38,16 @@ where NID: NodeId
}
}

pub(crate) fn value(&self) -> MetricDisplay<'_, NID> {
pub(crate) fn value(&self) -> MetricDisplay<'_, C> {
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,
impl<C> PartialEq<Metric<C>> for RaftMetrics<C>
where C: RaftTypeConfig
{
fn eq(&self, other: &Metric<NID>) -> bool {
fn eq(&self, other: &Metric<C>) -> bool {
match other {
Metric::Term(v) => self.current_term == *v,
Metric::Vote(v) => &self.vote == v,
Expand All @@ -64,12 +61,10 @@ where
}

/// 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,
impl<C> PartialOrd<Metric<C>> for RaftMetrics<C>
where C: RaftTypeConfig
{
fn partial_cmp(&self, other: &Metric<NID>) -> Option<Ordering> {
fn partial_cmp(&self, other: &Metric<C>) -> Option<Ordering> {
match other {
Metric::Term(v) => Some(self.current_term.cmp(v)),
Metric::Vote(v) => self.vote.partial_cmp(v),
Expand Down
12 changes: 6 additions & 6 deletions openraft/src/metrics/metric_display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ use std::fmt::Formatter;

use crate::display_ext::DisplayOption;
use crate::metrics::Metric;
use crate::NodeId;
use crate::RaftTypeConfig;

/// Display the value of a metric.
pub(crate) struct MetricDisplay<'a, NID>
where NID: NodeId
pub(crate) struct MetricDisplay<'a, C>
where C: RaftTypeConfig
{
pub(crate) metric: &'a Metric<NID>,
pub(crate) metric: &'a Metric<C>,
}

impl<'a, NID> fmt::Display for MetricDisplay<'a, NID>
where NID: NodeId
impl<'a, C> fmt::Display for MetricDisplay<'a, C>
where C: RaftTypeConfig
{
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
match self.metric {
Expand Down
Loading

0 comments on commit 717ed63

Please sign in to comment.