From c5760214095946c95c25dfeb9f55ba4e48591197 Mon Sep 17 00:00:00 2001 From: WenyXu Date: Wed, 30 Aug 2023 03:29:43 +0000 Subject: [PATCH] chore: apply suggestions from CR --- src/meta-srv/src/error.rs | 5 +-- .../region_failover/failover_start.rs | 41 ++++++++----------- 2 files changed, 18 insertions(+), 28 deletions(-) diff --git a/src/meta-srv/src/error.rs b/src/meta-srv/src/error.rs index b92dfff1cc13..00855b43fafd 100644 --- a/src/meta-srv/src/error.rs +++ b/src/meta-srv/src/error.rs @@ -26,10 +26,7 @@ use crate::pubsub::Message; #[derive(Debug, Snafu)] #[snafu(visibility(pub))] pub enum Error { - #[snafu(display( - "Failed to found failed region: {} in route, it may be already failover by another procedure", - region - ))] + #[snafu(display("Failed to find the failed region: {} in table route", region))] FailedRegionNotFound { location: Location, region: String }, #[snafu(display("Failed to list catalogs: {}", source))] diff --git a/src/meta-srv/src/procedure/region_failover/failover_start.rs b/src/meta-srv/src/procedure/region_failover/failover_start.rs index e0fd24c15132..cd7cd27cdd4d 100644 --- a/src/meta-srv/src/procedure/region_failover/failover_start.rs +++ b/src/meta-srv/src/procedure/region_failover/failover_start.rs @@ -13,15 +13,15 @@ // limitations under the License. use async_trait::async_trait; -use common_catalog::format_full_table_name; use common_error::ext::ErrorExt; use common_error::status_code::StatusCode; use common_meta::ident::TableIdent; +use common_meta::key::datanode_table::DatanodeTableKey; use common_meta::peer::Peer; use common_meta::RegionIdent; use common_telemetry::info; use serde::{Deserialize, Serialize}; -use snafu::{ensure, OptionExt, ResultExt}; +use snafu::{ensure, ResultExt}; use super::deactivate_region::DeactivateRegion; use super::{RegionFailoverContext, State}; @@ -45,30 +45,23 @@ impl RegionFailoverStart { failed_region: &RegionIdent, ) -> Result { let table_id = failed_region.table_ident.table_id; - let route = ctx + let datanode_id = failed_region.datanode_id; + + let value = ctx .table_metadata_manager - .table_route_manager() - .get(table_id) + .datanode_table_manager() + .get(&DatanodeTableKey::new(datanode_id, table_id)) .await - .context(error::TableMetadataManagerSnafu)? - .with_context(|| error::TableRouteNotFoundSnafu { - table_name: format_full_table_name( - &failed_region.table_ident.catalog, - &failed_region.table_ident.schema, - &failed_region.table_ident.table, - ), - })?; - - let failed_region = route.region_routes.iter().any(|route| { - route.region.id.region_number() == failed_region.region_number - && route - .leader_peer - .as_ref() - .map(|route| route.id == failed_region.datanode_id) - .unwrap_or_default() - }); - - Ok(failed_region) + .context(error::TableMetadataManagerSnafu)?; + + Ok(value + .map(|value| { + value + .regions + .iter() + .any(|region| *region == failed_region.region_number) + }) + .unwrap_or_default()) } async fn choose_candidate(