datanode/heartbeat/handler/
open_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, OpenRegion, SimpleReply};
16use common_meta::wal_options_allocator::prepare_wal_options;
17use futures_util::future::BoxFuture;
18use store_api::path_utils::region_dir;
19use store_api::region_request::{RegionOpenRequest, RegionRequest};
20
21use crate::heartbeat::handler::HandlerContext;
22
23impl HandlerContext {
24    pub(crate) fn handle_open_region_instruction(
25        self,
26        OpenRegion {
27            region_ident,
28            region_storage_path,
29            mut region_options,
30            region_wal_options,
31            skip_wal_replay,
32        }: OpenRegion,
33    ) -> BoxFuture<'static, Option<InstructionReply>> {
34        Box::pin(async move {
35            let region_id = Self::region_ident_to_region_id(&region_ident);
36            prepare_wal_options(&mut region_options, region_id, &region_wal_options);
37            let request = RegionRequest::Open(RegionOpenRequest {
38                engine: region_ident.engine,
39                region_dir: region_dir(&region_storage_path, region_id),
40                options: region_options,
41                skip_wal_replay,
42            });
43            let result = self.region_server.handle_request(region_id, request).await;
44            let success = result.is_ok();
45            let error = result.as_ref().map_err(|e| format!("{e:?}")).err();
46            Some(InstructionReply::OpenRegion(SimpleReply {
47                result: success,
48                error,
49            }))
50        })
51    }
52}