Skip to content

Commit

Permalink
feat: add leader_status for RegionRoute (#2670)
Browse files Browse the repository at this point in the history
  • Loading branch information
WenyXu authored Oct 31, 2023
1 parent d77003f commit e3320c5
Show file tree
Hide file tree
Showing 7 changed files with 94 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 @@ -157,6 +157,7 @@ fn create_region_routes() -> Vec<RegionRoute> {
addr: String::new(),
}),
follower_peers: vec![],
leader_status: None,
});
}

Expand Down
1 change: 1 addition & 0 deletions src/common/meta/src/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -783,6 +783,7 @@ mod tests {
},
leader_peer: Some(Peer::new(datanode, "a2")),
follower_peers: vec![],
leader_status: None,
}
}

Expand Down
82 changes: 82 additions & 0 deletions src/common/meta/src/rpc/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ impl TableRoute {
region,
leader_peer,
follower_peers,
leader_status: None,
});
}

Expand Down Expand Up @@ -196,6 +197,45 @@ pub struct RegionRoute {
pub region: Region,
pub leader_peer: Option<Peer>,
pub follower_peers: Vec<Peer>,
/// `None` by default.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub leader_status: Option<RegionStatus>,
}

/// 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<RegionRoute>);
Expand Down Expand Up @@ -311,6 +351,48 @@ impl From<PbPartition> 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 {
Expand Down
1 change: 1 addition & 0 deletions src/frontend/src/instance/standalone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ impl TableMetadataAllocator for StandaloneTableMetadataCreator {
region,
leader_peer: Some(peer),
follower_peers: vec![],
leader_status: None,
}
})
.collect::<Vec<_>>();
Expand Down
1 change: 1 addition & 0 deletions src/meta-srv/src/table_meta_alloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Vec<_>>();
Expand Down
2 changes: 2 additions & 0 deletions src/meta-srv/src/test_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}

Expand Down Expand Up @@ -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:
Expand Down
6 changes: 6 additions & 0 deletions src/operator/src/tests/partition_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -156,6 +158,7 @@ pub(crate) async fn create_partition_rule_manager(
},
leader_peer: Some(Peer::new(1, "")),
follower_peers: vec![],
leader_status: None,
},
],
)
Expand Down Expand Up @@ -185,6 +188,7 @@ pub(crate) async fn create_partition_rule_manager(
},
leader_peer: None,
follower_peers: vec![],
leader_status: None,
},
RegionRoute {
region: Region {
Expand All @@ -205,6 +209,7 @@ pub(crate) async fn create_partition_rule_manager(
},
leader_peer: None,
follower_peers: vec![],
leader_status: None,
},
RegionRoute {
region: Region {
Expand All @@ -222,6 +227,7 @@ pub(crate) async fn create_partition_rule_manager(
},
leader_peer: None,
follower_peers: vec![],
leader_status: None,
},
],
)
Expand Down

0 comments on commit e3320c5

Please sign in to comment.