mito2/worker/
handle_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//! Handling close request.
16
17use common_telemetry::info;
18use store_api::region_request::AffectedRows;
19use store_api::storage::RegionId;
20
21use crate::error::Result;
22use crate::worker::RegionWorkerLoop;
23
24impl<S> RegionWorkerLoop<S> {
25    pub(crate) async fn handle_close_request(
26        &mut self,
27        region_id: RegionId,
28    ) -> Result<AffectedRows> {
29        let Some(region) = self.regions.get_region(region_id) else {
30            return Ok(0);
31        };
32
33        info!("Try to close region {}, worker: {}", region_id, self.id);
34
35        region.stop().await;
36        self.regions.remove_region(region_id);
37        // Clean flush status.
38        self.flush_scheduler.on_region_closed(region_id);
39        // Clean compaction status.
40        self.compaction_scheduler.on_region_closed(region_id);
41
42        info!("Region {} closed, worker: {}", region_id, self.id);
43
44        self.region_count.dec();
45
46        Ok(0)
47    }
48}