From acebc7021890deb31a6da3c1f162099858261620 Mon Sep 17 00:00:00 2001 From: WenyXu Date: Tue, 24 Oct 2023 09:25:43 +0000 Subject: [PATCH] feat: introduce the peer state --- src/cmd/src/cli/bench.rs | 1 + src/common/meta/src/peer.rs | 69 ++++++++++++++++++++++++++++++++ src/meta-srv/src/table_routes.rs | 1 + 3 files changed, 71 insertions(+) diff --git a/src/cmd/src/cli/bench.rs b/src/cmd/src/cli/bench.rs index 54373cfa7ac9..cee84059184f 100644 --- a/src/cmd/src/cli/bench.rs +++ b/src/cmd/src/cli/bench.rs @@ -155,6 +155,7 @@ fn create_region_routes() -> Vec { leader_peer: Some(Peer { id: rng.gen_range(0..10), addr: String::new(), + ..Default::default() }), follower_peers: vec![], }); diff --git a/src/common/meta/src/peer.rs b/src/common/meta/src/peer.rs index 483250cbf1df..16acdce407d0 100644 --- a/src/common/meta/src/peer.rs +++ b/src/common/meta/src/peer.rs @@ -17,11 +17,50 @@ use std::fmt::{Display, Formatter}; use api::v1::meta::Peer as PbPeer; use serde::{Deserialize, Serialize}; +/// The State of the [Peer]. +#[derive(Debug, Clone, Hash, Eq, PartialEq, Deserialize, Serialize)] +pub enum PeerState { + /// The following cases in which the [Peer] will be downgraded. + /// + /// - The [Peer] or [Region][crate::rpc::router::Region] on the [Peer] is unavailable(e.g., Crashed, Network disconnected). + /// - The [Region][crate::rpc::router::Region] on the [Peer] was planned to migrate to another [Peer]. + Downgraded, +} + #[derive(Debug, Default, Clone, Hash, Eq, PartialEq, Deserialize, Serialize)] pub struct Peer { /// Node identifier. Unique in a cluster. pub id: u64, pub addr: String, + /// `None` by default. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub state: Option, +} + +impl Peer { + /// Returns true if the [Peer] is downgraded. + /// + /// The following cases in which the [Peer] will be downgraded. + /// + /// - The [Peer] or [Region][crate::rpc::router::Region] on the [Peer] is unavailable(e.g., Crashed, Network disconnected). + /// - The [Region][crate::rpc::router::Region] on the [Peer] was planned to migrate to another [Peer]. + /// + pub fn is_downgraded(&self) -> bool { + matches!(self.state, Some(PeerState::Downgraded)) + } + + /// Marks the [Peer] as downgraded. + /// + /// We should downgrade a [Peer] before deactivating a [Region][crate::rpc::router::Region] on it: + /// + /// - During the [Region][crate::rpc::router::Region] Failover Procedure. + /// - Migrating a [Region][crate::rpc::router::Region]. + /// + /// **Notes:** Meta Server will stop renewing the lease for a [Region][crate::rpc::router::Region] on the downgraded [Peer]. + /// + pub fn downgrade(&mut self) { + self.state = Some(PeerState::Downgraded) + } } impl From for Peer { @@ -29,6 +68,7 @@ impl From for Peer { Self { id: p.id, addr: p.addr, + state: None, } } } @@ -47,6 +87,7 @@ impl Peer { Self { id, addr: addr.into(), + state: None, } } } @@ -56,3 +97,31 @@ impl Display for Peer { write!(f, "peer-{}({})", self.id, self.addr) } } + +#[cfg(test)] +mod tests { + + use super::Peer; + + #[test] + fn test_peer_serde() { + let peer = Peer::new(2, "test"); + + let serialized = r#"{"id":2,"addr":"test"}"#; + + let decoded: Peer = serde_json::from_str(serialized).unwrap(); + + assert_eq!(peer, decoded); + } + + #[test] + fn test_peer_is_downgraded() { + let mut peer = Peer::new(2, "test"); + + assert!(!peer.is_downgraded()); + + peer.downgrade(); + + assert!(peer.is_downgraded()); + } +} diff --git a/src/meta-srv/src/table_routes.rs b/src/meta-srv/src/table_routes.rs index 593b6c384610..226c8dfcffb3 100644 --- a/src/meta-srv/src/table_routes.rs +++ b/src/meta-srv/src/table_routes.rs @@ -118,6 +118,7 @@ pub(crate) mod tests { leader_peer: Some(Peer { id: peer, addr: String::new(), + state: None, }), follower_peers: vec![], };