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, 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("MitoEngine not found".to_string()),
40            }));
41        };
42        match mito_engine
43            .get_snapshot_of_unmanifested_refs(get_file_refs.region_ids)
44            .await
45        {
46            Ok(all_file_refs) => {
47                // Return the file references
48                Some(InstructionReply::GetFileRefs(GetFileRefsReply {
49                    file_refs_manifest: all_file_refs,
50                    success: true,
51                    error: None,
52                }))
53            }
54            Err(e) => Some(InstructionReply::GetFileRefs(GetFileRefsReply {
55                file_refs_manifest: FileRefsManifest::default(),
56                success: false,
57                error: Some(format!("Failed to get file refs: {}", e.output_msg())),
58            })),
59        }
60    }
61}