From e3320c531d8593ffb91e5e0fc87de16af395f9f2 Mon Sep 17 00:00:00 2001 From: Weny Xu Date: Tue, 31 Oct 2023 18:34:56 +0900 Subject: [PATCH] feat: add leader_status for RegionRoute (#2670) --- src/cmd/src/cli/bench.rs | 1 + src/common/meta/src/key.rs | 1 + src/common/meta/src/rpc/router.rs | 82 +++++++++++++++++++++ src/frontend/src/instance/standalone.rs | 1 + src/meta-srv/src/table_meta_alloc.rs | 1 + src/meta-srv/src/test_util.rs | 2 + src/operator/src/tests/partition_manager.rs | 6 ++ 7 files changed, 94 insertions(+) diff --git a/src/cmd/src/cli/bench.rs b/src/cmd/src/cli/bench.rs index 54373cfa7ac9..787ea4353512 100644 --- a/src/cmd/src/cli/bench.rs +++ b/src/cmd/src/cli/bench.rs @@ -157,6 +157,7 @@ fn create_region_routes() -> Vec { addr: String::new(), }), follower_peers: vec![], + leader_status: None, }); } diff --git a/src/common/meta/src/key.rs b/src/common/meta/src/key.rs index d1a980d7023e..6bb2c1bdabc5 100644 --- a/src/common/meta/src/key.rs +++ b/src/common/meta/src/key.rs @@ -783,6 +783,7 @@ mod tests { }, leader_peer: Some(Peer::new(datanode, "a2")), follower_peers: vec![], + leader_status: None, } } diff --git a/src/common/meta/src/rpc/router.rs b/src/common/meta/src/rpc/router.rs index 449628aa4421..ffc311fbe511 100644 --- a/src/common/meta/src/rpc/router.rs +++ b/src/common/meta/src/rpc/router.rs @@ -148,6 +148,7 @@ impl TableRoute { region, leader_peer, follower_peers, + leader_status: None, }); } @@ -196,6 +197,45 @@ pub struct RegionRoute { pub region: Region, pub leader_peer: Option, pub follower_peers: Vec, + /// `None` by default. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub leader_status: Option, +} + +/// The Status of the [Region]. +#[derive(Debug, Clone, Deserialize, Serialize, PartialEq)] +pub enum RegionStatus { + /// The following cases in which the [Region] will be downgraded. + /// + /// - The [Region] is unavailable(e.g., Crashed, Network disconnected). + /// - The [Region] was planned to migrate to another [Peer]. + Downgraded, +} + +impl RegionRoute { + /// Returns true if the Leader [Region] is downgraded. + /// + /// The following cases in which the [Region] will be downgraded. + /// + /// - The [Region] is unavailable(e.g., Crashed, Network disconnected). + /// - The [Region] was planned to migrate to another [Peer]. + /// + pub fn is_leader_downgraded(&self) -> bool { + matches!(self.leader_status, Some(RegionStatus::Downgraded)) + } + + /// Marks the Leader [Region] as downgraded. + /// + /// We should downgrade a [Region] before deactivating it: + /// + /// - During the [Region] Failover Procedure. + /// - Migrating a [Region]. + /// + /// **Notes:** Meta Server will stop renewing the lease for the downgraded [Region]. + /// + pub fn downgrade_leader(&mut self) { + self.leader_status = Some(RegionStatus::Downgraded) + } } pub struct RegionRoutes(pub Vec); @@ -311,6 +351,48 @@ impl From for Partition { mod tests { use super::*; + #[test] + fn test_leader_is_downgraded() { + let mut region_route = RegionRoute { + region: Region { + id: 2.into(), + name: "r2".to_string(), + partition: None, + attrs: BTreeMap::new(), + }, + leader_peer: Some(Peer::new(1, "a1")), + follower_peers: vec![Peer::new(2, "a2"), Peer::new(3, "a3")], + leader_status: None, + }; + + assert!(!region_route.is_leader_downgraded()); + + region_route.downgrade_leader(); + + assert!(region_route.is_leader_downgraded()); + } + + #[test] + fn test_region_route_decode() { + let region_route = RegionRoute { + region: Region { + id: 2.into(), + name: "r2".to_string(), + partition: None, + attrs: BTreeMap::new(), + }, + leader_peer: Some(Peer::new(1, "a1")), + follower_peers: vec![Peer::new(2, "a2"), Peer::new(3, "a3")], + leader_status: None, + }; + + let input = r#"{"region":{"id":2,"name":"r2","partition":null,"attrs":{}},"leader_peer":{"id":1,"addr":"a1"},"follower_peers":[{"id":2,"addr":"a2"},{"id":3,"addr":"a3"}]}"#; + + let decoded: RegionRoute = serde_json::from_str(input).unwrap(); + + assert_eq!(decoded, region_route); + } + #[test] fn test_de_serialize_partition() { let p = Partition { diff --git a/src/frontend/src/instance/standalone.rs b/src/frontend/src/instance/standalone.rs index 12228d133689..f7d121e5f4a6 100644 --- a/src/frontend/src/instance/standalone.rs +++ b/src/frontend/src/instance/standalone.rs @@ -128,6 +128,7 @@ impl TableMetadataAllocator for StandaloneTableMetadataCreator { region, leader_peer: Some(peer), follower_peers: vec![], + leader_status: None, } }) .collect::>(); diff --git a/src/meta-srv/src/table_meta_alloc.rs b/src/meta-srv/src/table_meta_alloc.rs index abbc729b8d8e..cfefbba0c282 100644 --- a/src/meta-srv/src/table_meta_alloc.rs +++ b/src/meta-srv/src/table_meta_alloc.rs @@ -118,6 +118,7 @@ async fn handle_create_region_routes( region, leader_peer: Some(peer.into()), follower_peers: vec![], // follower_peers is not supported at the moment + leader_status: None, } }) .collect::>(); diff --git a/src/meta-srv/src/test_util.rs b/src/meta-srv/src/test_util.rs index 1de7ae77cf94..63d68c9b7b9f 100644 --- a/src/meta-srv/src/test_util.rs +++ b/src/meta-srv/src/test_util.rs @@ -48,6 +48,7 @@ pub(crate) fn new_region_route(region_id: u64, peers: &[Peer], leader_node: u64) region, leader_peer, follower_peers: vec![], + leader_status: None, } } @@ -133,6 +134,7 @@ pub(crate) async fn prepare_table_region_and_info_value( addr: String::new(), }), follower_peers: vec![], + leader_status: None, }; // Region distribution: diff --git a/src/operator/src/tests/partition_manager.rs b/src/operator/src/tests/partition_manager.rs index 58421634ae2a..e5e24b9b9596 100644 --- a/src/operator/src/tests/partition_manager.rs +++ b/src/operator/src/tests/partition_manager.rs @@ -122,6 +122,7 @@ pub(crate) async fn create_partition_rule_manager( }, leader_peer: Some(Peer::new(3, "")), follower_peers: vec![], + leader_status: None, }, RegionRoute { region: Region { @@ -139,6 +140,7 @@ pub(crate) async fn create_partition_rule_manager( }, leader_peer: Some(Peer::new(2, "")), follower_peers: vec![], + leader_status: None, }, RegionRoute { region: Region { @@ -156,6 +158,7 @@ pub(crate) async fn create_partition_rule_manager( }, leader_peer: Some(Peer::new(1, "")), follower_peers: vec![], + leader_status: None, }, ], ) @@ -185,6 +188,7 @@ pub(crate) async fn create_partition_rule_manager( }, leader_peer: None, follower_peers: vec![], + leader_status: None, }, RegionRoute { region: Region { @@ -205,6 +209,7 @@ pub(crate) async fn create_partition_rule_manager( }, leader_peer: None, follower_peers: vec![], + leader_status: None, }, RegionRoute { region: Region { @@ -222,6 +227,7 @@ pub(crate) async fn create_partition_rule_manager( }, leader_peer: None, follower_peers: vec![], + leader_status: None, }, ], )