metric_engine/engine/
flush.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
15use snafu::ResultExt;
16use store_api::region_engine::RegionEngine;
17use store_api::region_request::{AffectedRows, RegionFlushRequest, RegionRequest};
18use store_api::storage::RegionId;
19
20use crate::engine::MetricEngineInner;
21use crate::error::{MitoFlushOperationSnafu, Result, UnsupportedRegionRequestSnafu};
22use crate::utils;
23
24impl MetricEngineInner {
25    pub async fn flush_region(
26        &self,
27        region_id: RegionId,
28        req: RegionFlushRequest,
29    ) -> Result<AffectedRows> {
30        if !self.is_physical_region(region_id) {
31            return UnsupportedRegionRequestSnafu {
32                request: RegionRequest::Flush(req),
33            }
34            .fail();
35        }
36
37        let metadata_region_id = utils::to_metadata_region_id(region_id);
38        // Flushes the metadata region as well
39        self.mito
40            .handle_request(metadata_region_id, RegionRequest::Flush(req.clone()))
41            .await
42            .context(MitoFlushOperationSnafu)
43            .map(|response| response.affected_rows)?;
44
45        let data_region_id = utils::to_data_region_id(region_id);
46        self.mito
47            .handle_request(data_region_id, RegionRequest::Flush(req.clone()))
48            .await
49            .context(MitoFlushOperationSnafu)
50            .map(|response| response.affected_rows)
51    }
52}