Coverage Report

Created: 2025-04-27 02:50

/root/doris/be/src/runtime/snapshot_loader.h
Line
Count
Source (jump to first uncovered line)
1
// Licensed to the Apache Software Foundation (ASF) under one
2
// or more contributor license agreements.  See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership.  The ASF licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License.  You may obtain a copy of the License at
8
//
9
//   http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied.  See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
18
#pragma once
19
20
#include <gen_cpp/Types_types.h>
21
22
#include <cstdint>
23
#include <map>
24
#include <memory>
25
#include <string>
26
#include <vector>
27
28
#include "common/status.h"
29
#include "olap/tablet_fwd.h"
30
#include "runtime/workload_management/resource_context.h"
31
32
namespace doris {
33
namespace io {
34
class RemoteFileSystem;
35
} // namespace io
36
37
class DataDir;
38
class TRemoteTabletSnapshot;
39
class StorageEngine;
40
41
struct FileStat {
42
    std::string name;
43
    std::string md5;
44
    int64_t size;
45
};
46
class ExecEnv;
47
48
/*
49
 * Upload:
50
 * upload() will upload the specified snapshot
51
 * to remote storage via broker.
52
 * Each call of upload() is responsible for several tablet snapshots.
53
 *
54
 * It will try to get the existing files in remote storage,
55
 * and only upload the incremental part of files.
56
 *
57
 * Download:
58
 * download() will download the remote tablet snapshot files
59
 * to local snapshot dir via broker.
60
 * It will also only download files which does not exist in local dir.
61
 *
62
 * Move:
63
 * move() is the final step of restore process. it will replace the
64
 * old tablet data dir with the newly downloaded snapshot dir.
65
 * and reload the tablet header to take this tablet on line.
66
 *
67
 */
68
class SnapshotLoader {
69
    friend class SnapshotHttpDownloader;
70
71
public:
72
    SnapshotLoader(StorageEngine& engine, ExecEnv* env, int64_t job_id, int64_t task_id,
73
                   const TNetworkAddress& broker_addr = {},
74
                   const std::map<std::string, std::string>& broker_prop = {});
75
76
    ~SnapshotLoader();
77
78
    Status init(TStorageBackendType::type type, const std::string& location);
79
80
    Status upload(const std::map<std::string, std::string>& src_to_dest_path,
81
                  std::map<int64_t, std::vector<std::string>>* tablet_files);
82
83
    Status download(const std::map<std::string, std::string>& src_to_dest_path,
84
                    std::vector<int64_t>* downloaded_tablet_ids);
85
86
    Status remote_http_download(const std::vector<TRemoteTabletSnapshot>& remote_tablets,
87
                                std::vector<int64_t>* downloaded_tablet_ids);
88
89
    Status move(const std::string& snapshot_path, TabletSharedPtr tablet, bool overwrite);
90
91
2
    int64_t get_http_download_files_num() const { return _http_download_files_num; }
92
93
0
    std::shared_ptr<ResourceContext> resource_ctx() { return _resource_ctx; }
94
95
private:
96
    Status _get_tablet_id_and_schema_hash_from_file_path(const std::string& src_path,
97
                                                         int64_t* tablet_id, int32_t* schema_hash);
98
99
    Status _check_local_snapshot_paths(const std::map<std::string, std::string>& src_to_dest_path,
100
                                       bool check_src);
101
102
    Status _get_existing_files_from_local(const std::string& local_path,
103
                                          std::vector<std::string>* local_files);
104
105
    Status _replace_tablet_id(const std::string& file_name, int64_t tablet_id,
106
                              std::string* new_file_name);
107
108
    Status _get_tablet_id_from_remote_path(const std::string& remote_path, int64_t* tablet_id);
109
110
    Status _report_every(int report_threshold, int* counter, int finished_num, int total_num,
111
                         TTaskType::type type);
112
113
    Status _list_with_checksum(const std::string& dir, std::map<std::string, FileStat>* md5_files);
114
115
2
    void _set_http_download_files_num(int64_t num) { _http_download_files_num = num; }
116
117
private:
118
    StorageEngine& _engine;
119
    ExecEnv* _env = nullptr;
120
    int64_t _job_id;
121
    int64_t _task_id;
122
    const TNetworkAddress _broker_addr;
123
    const std::map<std::string, std::string> _prop;
124
    std::shared_ptr<io::RemoteFileSystem> _remote_fs;
125
    // for test remote_http_download
126
    size_t _http_download_files_num;
127
    std::shared_ptr<ResourceContext> _resource_ctx;
128
};
129
130
} // end namespace doris