Skip to main content

common_meta/ddl/drop_table/
metadata.rs

1// Copyright 2023 Greptime Team
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#[cfg(feature = "enterprise")]
16use common_catalog::consts::FILE_ENGINE;
17use common_catalog::format_full_table_name;
18use snafu::OptionExt;
19use store_api::metric_engine_consts::METRIC_ENGINE_NAME;
20
21use crate::ddl::drop_table::DropTableProcedure;
22use crate::ddl::utils::extract_region_wal_options;
23#[cfg(feature = "enterprise")]
24use crate::ddl::utils::is_metric_engine_logical_table;
25use crate::error::{self, Result};
26#[cfg(feature = "enterprise")]
27use crate::key::table_route::TableRouteValue;
28
29impl DropTableProcedure {
30    #[cfg(feature = "enterprise")]
31    fn apply_soft_drop_fallbacks(
32        &mut self,
33        table_info_value: &crate::key::table_info::TableInfoValue,
34        table_route_value: &TableRouteValue,
35        is_physical: bool,
36    ) {
37        // TODO(hl): support soft-dropping logical tables.
38        if self.data.soft_drop_enabled
39            && is_metric_engine_logical_table(&table_info_value.table_info, table_route_value)
40        {
41            self.data.soft_drop_enabled = false;
42        }
43
44        if is_physical
45            && self.data.soft_drop_enabled
46            && table_info_value.table_info.meta.engine == FILE_ENGINE
47        {
48            self.data.soft_drop_enabled = false;
49        }
50    }
51
52    /// Fetches the table info and physical table route.
53    pub(crate) async fn fill_table_metadata(&mut self) -> Result<()> {
54        let task = &self.data.task;
55        let (physical_table_id, physical_table_route_value) = self
56            .context
57            .table_metadata_manager
58            .table_route_manager()
59            .get_physical_table_route(task.table_id)
60            .await?;
61
62        let table_info_value = self
63            .context
64            .table_metadata_manager
65            .table_info_manager()
66            .get(task.table_id)
67            .await?
68            .with_context(|| error::TableInfoNotFoundSnafu {
69                table: format_full_table_name(&task.catalog, &task.schema, &task.table),
70            })?
71            .into_inner();
72        #[cfg(feature = "enterprise")]
73        let table_route_value = TableRouteValue::new(
74            self.data.table_id(),
75            physical_table_id,
76            physical_table_route_value.region_routes.clone(),
77        );
78        #[cfg(feature = "enterprise")]
79        self.apply_soft_drop_fallbacks(
80            &table_info_value,
81            &table_route_value,
82            physical_table_id == self.data.table_id(),
83        );
84
85        if physical_table_id == self.data.table_id() {
86            let engine = table_info_value.table_info.meta.engine;
87            // rollback only if dropping the metric physical table fails
88            self.data.allow_rollback = engine.as_str() == METRIC_ENGINE_NAME;
89
90            // Deletes topic-region mapping if dropping physical table
91            let datanode_table_values = self
92                .context
93                .table_metadata_manager
94                .datanode_table_manager()
95                .regions(physical_table_id, &physical_table_route_value)
96                .await?;
97            self.data.region_wal_options = extract_region_wal_options(&datanode_table_values)?;
98        }
99
100        self.data.physical_region_routes = physical_table_route_value.region_routes;
101        self.data.physical_table_id = Some(physical_table_id);
102
103        Ok(())
104    }
105}