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, info};
18use snafu::ResultExt;
19use store_api::region_engine::{RegionEngine, RegionRole};
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        // The engine reports a physical region by its data region, so once that is closed the
72        // metadata region keeps no lease. A retry that finds the data region closed and the
73        // metadata region a follower waives the requested flush of the metadata region. The role
74        // is read before that close, so the waiver also holds for a metadata region promoted in
75        // between. The close then releases the region's WAL only up to its flushed entry id, so
76        // unflushed rows that are in a WAL are kept; a region without a WAL loses them, as on any
77        // close of a region that cannot flush.
78        let data_region_closed = !self.mito.is_region_exists(data_region_id);
79        self.mito
80            .handle_request(
81                data_region_id,
82                RegionRequest::Close(RegionCloseRequest { flush_on_close }),
83            )
84            .await
85            .context(CloseMitoRegionSnafu { region_id })?;
86        let skip_metadata_flush = flush_on_close
87            && data_region_closed
88            && self.mito.role(metadata_region_id) == Some(RegionRole::Follower);
89        if skip_metadata_flush {
90            info!(
91                "Waiving the requested flush of follower metadata region {}, its data region is closed",
92                metadata_region_id
93            );
94        }
95        self.mito
96            .handle_request(
97                metadata_region_id,
98                RegionRequest::Close(RegionCloseRequest {
99                    flush_on_close: flush_on_close && !skip_metadata_flush,
100                }),
101            )
102            .await
103            .context(CloseMitoRegionSnafu { region_id })?;
104
105        PHYSICAL_REGION_COUNT.dec();
106
107        Ok(0)
108    }
109}
110
111// Unit tests in engine.rs