Coverage Report

Created: 2025-04-10 14:17

/root/doris/be/src/runtime/snapshot_loader.h
Line
Count
Source
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
31
namespace doris {
32
namespace io {
33
class RemoteFileSystem;
34
} // namespace io
35
36
class DataDir;
37
class TRemoteTabletSnapshot;
38
class StorageEngine;
39
40
struct FileStat {
41
    std::string name;
42
    std::string md5;
43
    int64_t size;
44
};
45
class ExecEnv;
46
47
/*
48
 * Upload:
49
 * upload() will upload the specified snapshot
50
 * to remote storage via broker.
51
 * Each call of upload() is responsible for several tablet snapshots.
52
 *
53
 * It will try to get the existing files in remote storage,
54
 * and only upload the incremental part of files.
55
 *
56
 * Download:
57
 * download() will download the remote tablet snapshot files
58
 * to local snapshot dir via broker.
59
 * It will also only download files which does not exist in local dir.
60
 *
61
 * Move:
62
 * move() is the final step of restore process. it will replace the
63
 * old tablet data dir with the newly downloaded snapshot dir.
64
 * and reload the tablet header to take this tablet on line.
65
 *
66
 */
67
class SnapshotLoader {
68
    friend class SnapshotHttpDownloader;
69
70
public:
71
    SnapshotLoader(StorageEngine& engine, ExecEnv* env, int64_t job_id, int64_t task_id,
72
                   const TNetworkAddress& broker_addr = {},
73
                   const std::map<std::string, std::string>& broker_prop = {});
74
75
    ~SnapshotLoader();
76
77
    Status init(TStorageBackendType::type type, const std::string& location);
78
79
    Status upload(const std::map<std::string, std::string>& src_to_dest_path,
80
                  std::map<int64_t, std::vector<std::string>>* tablet_files);
81
82
    Status download(const std::map<std::string, std::string>& src_to_dest_path,
83
                    std::vector<int64_t>* downloaded_tablet_ids);
84
85
    Status remote_http_download(const std::vector<TRemoteTabletSnapshot>& remote_tablets,
86
                                std::vector<int64_t>* downloaded_tablet_ids);
87
88
    Status move(const std::string& snapshot_path, TabletSharedPtr tablet, bool overwrite);
89
90
1
    int64_t get_http_download_files_num() const { return _http_download_files_num; }
91
92
private:
93
    Status _get_tablet_id_and_schema_hash_from_file_path(const std::string& src_path,
94
                                                         int64_t* tablet_id, int32_t* schema_hash);
95
96
    Status _check_local_snapshot_paths(const std::map<std::string, std::string>& src_to_dest_path,
97
                                       bool check_src);
98
99
    Status _get_existing_files_from_local(const std::string& local_path,
100
                                          std::vector<std::string>* local_files);
101
102
    Status _replace_tablet_id(const std::string& file_name, int64_t tablet_id,
103
                              std::string* new_file_name);
104
105
    Status _get_tablet_id_from_remote_path(const std::string& remote_path, int64_t* tablet_id);
106
107
    Status _report_every(int report_threshold, int* counter, int finished_num, int total_num,
108
                         TTaskType::type type);
109
110
    Status _list_with_checksum(const std::string& dir, std::map<std::string, FileStat>* md5_files);
111
112
1
    void _set_http_download_files_num(int64_t num) { _http_download_files_num = num; }
113
114
private:
115
    StorageEngine& _engine;
116
    ExecEnv* _env = nullptr;
117
    int64_t _job_id;
118
    int64_t _task_id;
119
    const TNetworkAddress _broker_addr;
120
    const std::map<std::string, std::string> _prop;
121
    std::shared_ptr<io::RemoteFileSystem> _remote_fs;
122
    // for test remote_http_download
123
    size_t _http_download_files_num;
124
};
125
126
} // end namespace doris