Skip to content

Commit

Permalink
feat: introduce the peer state
Browse files Browse the repository at this point in the history
  • Loading branch information
WenyXu committed Oct 24, 2023
1 parent 01fdbf3 commit acebc70
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/cmd/src/cli/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ fn create_region_routes() -> Vec<RegionRoute> {
leader_peer: Some(Peer {
id: rng.gen_range(0..10),
addr: String::new(),
..Default::default()
}),
follower_peers: vec![],
});
Expand Down
69 changes: 69 additions & 0 deletions src/common/meta/src/peer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,58 @@ 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<PeerState>,
}

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<PbPeer> for Peer {
fn from(p: PbPeer) -> Self {
Self {
id: p.id,
addr: p.addr,
state: None,
}
}
}
Expand All @@ -47,6 +87,7 @@ impl Peer {
Self {
id,
addr: addr.into(),
state: None,
}
}
}
Expand All @@ -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());
}
}
1 change: 1 addition & 0 deletions src/meta-srv/src/table_routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ pub(crate) mod tests {
leader_peer: Some(Peer {
id: peer,
addr: String::new(),
state: None,
}),
follower_peers: vec![],
};
Expand Down

0 comments on commit acebc70

Please sign in to comment.