metric_engine/engine/
drop.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//! Drop a metric region
16
17use common_telemetry::{debug, info};
18use snafu::ResultExt;
19use store_api::region_engine::RegionEngine;
20use store_api::region_request::{AffectedRows, RegionDropRequest, RegionRequest};
21use store_api::storage::RegionId;
22
23use crate::engine::MetricEngineInner;
24use crate::error::{
25    CloseMitoRegionSnafu, LogicalRegionNotFoundSnafu, PhysicalRegionBusySnafu, Result,
26};
27use crate::metrics::PHYSICAL_REGION_COUNT;
28use crate::utils;
29
30impl MetricEngineInner {
31    pub async fn drop_region(
32        &self,
33        region_id: RegionId,
34        req: RegionDropRequest,
35    ) -> Result<AffectedRows> {
36        let data_region_id = utils::to_data_region_id(region_id);
37        let fast_path = req.fast_path;
38        let force = req.force;
39
40        // enclose the guard in a block to prevent the guard from polluting the async context
41        let (is_physical_region, is_physical_region_busy) = {
42            if let Some(state) = self
43                .state
44                .read()
45                .unwrap()
46                .physical_region_states()
47                .get(&data_region_id)
48            {
49                debug!(
50                    "Physical region {} is busy, there are still some logical regions: {:?}",
51                    data_region_id,
52                    state
53                        .logical_regions()
54                        .iter()
55                        .map(|id| id.to_string())
56                        .collect::<Vec<_>>()
57                );
58                (true, !state.logical_regions().is_empty())
59            } else {
60                // the second argument is not used, just pass in a dummy value
61                (false, true)
62            }
63        };
64
65        if is_physical_region {
66            // check if there is no logical region relates to this physical region
67            if is_physical_region_busy && !force {
68                // reject if there is any present logical region
69                return Err(PhysicalRegionBusySnafu {
70                    region_id: data_region_id,
71                }
72                .build());
73            }
74            if is_physical_region_busy && force {
75                info!("Dropping physical region {} with force", data_region_id);
76            }
77            return self
78                .drop_physical_region(data_region_id, req.partial_drop)
79                .await;
80        }
81
82        if fast_path {
83            // for fast path, we don't delete the metadata in the metadata region.
84            // it only remove the logical region from the engine state.
85            //
86            // The drop database procedure will ensure the metadata region and data region are dropped eventually.
87            self.state
88                .write()
89                .unwrap()
90                .remove_logical_region(region_id)?;
91
92            Ok(0)
93        } else {
94            let metadata_region_id = self
95                .state
96                .read()
97                .unwrap()
98                .logical_regions()
99                .get(&region_id)
100                .copied();
101            if let Some(metadata_region_id) = metadata_region_id {
102                self.drop_logical_region(region_id, metadata_region_id)
103                    .await
104            } else {
105                Err(LogicalRegionNotFoundSnafu { region_id }.build())
106            }
107        }
108    }
109
110    async fn drop_physical_region(
111        &self,
112        region_id: RegionId,
113        partial_drop: bool,
114    ) -> Result<AffectedRows> {
115        let data_region_id = utils::to_data_region_id(region_id);
116        let metadata_region_id = utils::to_metadata_region_id(region_id);
117
118        // Drop mito regions.
119        // Since the physical regions are going to be dropped, we don't need to
120        // update the contents in metadata region.
121        self.mito
122            .handle_request(
123                data_region_id,
124                RegionRequest::Drop(RegionDropRequest {
125                    fast_path: false,
126                    force: false,
127                    partial_drop,
128                }),
129            )
130            .await
131            .with_context(|_| CloseMitoRegionSnafu { region_id })?;
132        self.mito
133            .handle_request(
134                metadata_region_id,
135                RegionRequest::Drop(RegionDropRequest {
136                    fast_path: false,
137                    force: false,
138                    partial_drop,
139                }),
140            )
141            .await
142            .with_context(|_| CloseMitoRegionSnafu { region_id })?;
143
144        PHYSICAL_REGION_COUNT.dec();
145
146        // Update engine state
147        self.state
148            .write()
149            .unwrap()
150            .remove_physical_region(data_region_id)?;
151
152        Ok(0)
153    }
154
155    async fn drop_logical_region(
156        &self,
157        logical_region_id: RegionId,
158        physical_region_id: RegionId,
159    ) -> Result<AffectedRows> {
160        // Update metadata
161        self.metadata_region
162            .remove_logical_region(physical_region_id, logical_region_id)
163            .await?;
164
165        // Update engine state
166        self.state
167            .write()
168            .unwrap()
169            .remove_logical_region(logical_region_id)?;
170
171        Ok(0)
172    }
173}