Skip to main content

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, req.flush_on_close)
42                .await?;
43            self.state
44                .write()
45                .unwrap()
46                .remove_physical_region(data_region_id)?;
47
48            Ok(0)
49        } else if self
50            .state
51            .read()
52            .unwrap()
53            .logical_regions()
54            .contains_key(&region_id)
55        {
56            Ok(0)
57        } else {
58            debug!("Closing a non-existent logical region {}", region_id);
59            Ok(0)
60        }
61    }
62
63    pub(crate) async fn close_physical_region(
64        &self,
65        region_id: RegionId,
66        flush_on_close: bool,
67    ) -> Result<AffectedRows> {
68        let data_region_id = utils::to_data_region_id(region_id);
69        let metadata_region_id = utils::to_metadata_region_id(region_id);
70
71        self.mito
72            .handle_request(
73                data_region_id,
74                RegionRequest::Close(RegionCloseRequest { flush_on_close }),
75            )
76            .await
77            .with_context(|_| CloseMitoRegionSnafu { region_id })?;
78        self.mito
79            .handle_request(
80                metadata_region_id,
81                RegionRequest::Close(RegionCloseRequest { flush_on_close }),
82            )
83            .await
84            .with_context(|_| CloseMitoRegionSnafu { region_id })?;
85
86        PHYSICAL_REGION_COUNT.dec();
87
88        Ok(0)
89    }
90}
91
92// Unit tests in engine.rs