Skip to main content

datanode/heartbeat/handler/
file_ref.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_error::ext::ErrorExt;
16use common_meta::instruction::{GetFileRefs, GetFileRefsReply, InstructionError, InstructionReply};
17use store_api::storage::FileRefsManifest;
18
19use crate::heartbeat::handler::{HandlerContext, InstructionHandler};
20
21pub struct GetFileRefsHandler;
22
23#[async_trait::async_trait]
24impl InstructionHandler for GetFileRefsHandler {
25    type Instruction = GetFileRefs;
26
27    async fn handle(
28        &self,
29        ctx: &HandlerContext,
30        get_file_refs: Self::Instruction,
31    ) -> Option<InstructionReply> {
32        let region_server = &ctx.region_server;
33
34        // Get the MitoEngine
35        let Some(mito_engine) = region_server.mito_engine() else {
36            return Some(InstructionReply::GetFileRefs(GetFileRefsReply {
37                file_refs_manifest: FileRefsManifest::default(),
38                success: false,
39                error: Some(InstructionError::legacy_internal_retryable(
40                    "MitoEngine not found",
41                )),
42            }));
43        };
44        match mito_engine
45            .get_snapshot_of_file_refs(get_file_refs.query_regions, get_file_refs.related_regions)
46            .await
47        {
48            Ok(all_file_refs) => {
49                // Return the file references
50                Some(InstructionReply::GetFileRefs(GetFileRefsReply {
51                    file_refs_manifest: all_file_refs,
52                    success: true,
53                    error: None,
54                }))
55            }
56            Err(e) => Some(InstructionReply::GetFileRefs(GetFileRefsReply {
57                file_refs_manifest: FileRefsManifest::default(),
58                success: false,
59                error: Some(InstructionError::new(
60                    e.status_code(),
61                    format!("Failed to get file refs: {}", e.output_msg()),
62                    e.retry_hint(),
63                )),
64            })),
65        }
66    }
67}