datanode/heartbeat/handler/
close_region.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 common_meta::instruction::{InstructionReply, SimpleReply};
16use common_meta::RegionIdent;
17use common_telemetry::{tracing, warn};
18use futures_util::future::BoxFuture;
19use store_api::region_request::{RegionCloseRequest, RegionRequest};
20
21use crate::error;
22use crate::heartbeat::handler::HandlerContext;
23
24impl HandlerContext {
25    #[tracing::instrument(skip_all)]
26    pub(crate) fn handle_close_region_instruction(
27        self,
28        region_ident: RegionIdent,
29    ) -> BoxFuture<'static, Option<InstructionReply>> {
30        Box::pin(async move {
31            let region_id = Self::region_ident_to_region_id(&region_ident);
32            let request = RegionRequest::Close(RegionCloseRequest {});
33            let result = self.region_server.handle_request(region_id, request).await;
34
35            match result {
36                Ok(_) => Some(InstructionReply::CloseRegion(SimpleReply {
37                    result: true,
38                    error: None,
39                })),
40                Err(error::Error::RegionNotFound { .. }) => {
41                    warn!("Received a close region instruction from meta, but target region:{region_id} is not found.");
42                    Some(InstructionReply::CloseRegion(SimpleReply {
43                        result: true,
44                        error: None,
45                    }))
46                }
47                Err(err) => Some(InstructionReply::CloseRegion(SimpleReply {
48                    result: false,
49                    error: Some(format!("{err:?}")),
50                })),
51            }
52        })
53    }
54}