metric_engine/engine/
close.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//! Close a metric region
16
17use common_telemetry::debug;
18use snafu::ResultExt;
19use store_api::region_engine::RegionEngine;
20use store_api::region_request::{AffectedRows, RegionCloseRequest, RegionRequest};
21use store_api::storage::RegionId;
22
23use crate::engine::MetricEngineInner;
24use crate::error::{CloseMitoRegionSnafu, Result};
25use crate::metrics::PHYSICAL_REGION_COUNT;
26use crate::utils;
27
28impl MetricEngineInner {
29    pub async fn close_region(
30        &self,
31        region_id: RegionId,
32        _req: RegionCloseRequest,
33    ) -> Result<AffectedRows> {
34        let data_region_id = utils::to_data_region_id(region_id);
35        if self
36            .state
37            .read()
38            .unwrap()
39            .exist_physical_region(data_region_id)
40        {
41            self.close_physical_region(data_region_id).await?;
42            self.state
43                .write()
44                .unwrap()
45                .remove_physical_region(data_region_id)?;
46
47            Ok(0)
48        } else if self
49            .state
50            .read()
51            .unwrap()
52            .logical_regions()
53            .contains_key(&region_id)
54        {
55            Ok(0)
56        } else {
57            debug!("Closing a non-existent logical region {}", region_id);
58            Ok(0)
59        }
60    }
61
62    async fn close_physical_region(&self, region_id: RegionId) -> Result<AffectedRows> {
63        let data_region_id = utils::to_data_region_id(region_id);
64        let metadata_region_id = utils::to_metadata_region_id(region_id);
65
66        self.mito
67            .handle_request(data_region_id, RegionRequest::Close(RegionCloseRequest {}))
68            .await
69            .with_context(|_| CloseMitoRegionSnafu { region_id })?;
70        self.mito
71            .handle_request(
72                metadata_region_id,
73                RegionRequest::Close(RegionCloseRequest {}),
74            )
75            .await
76            .with_context(|_| CloseMitoRegionSnafu { region_id })?;
77
78        PHYSICAL_REGION_COUNT.dec();
79
80        Ok(0)
81    }
82}
83
84// Unit tests in engine.rs