/root/doris/cloud/src/recycler/checker.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 "resource-manager/resource_manager.h" |
21 | | #if defined(USE_LIBCPP) && _LIBCPP_ABI_VERSION <= 1 |
22 | | #define _LIBCPP_ABI_INCOMPLETE_TYPES_IN_DEQUE |
23 | | #endif |
24 | | #include <atomic> |
25 | | #include <condition_variable> |
26 | | #include <deque> |
27 | | #include <functional> |
28 | | #include <thread> |
29 | | #include <tuple> |
30 | | #include <unordered_map> |
31 | | #include <unordered_set> |
32 | | |
33 | | #include "recycler/storage_vault_accessor.h" |
34 | | #include "recycler/white_black_list.h" |
35 | | #include "snapshot/snapshot_manager.h" |
36 | | |
37 | | namespace doris { |
38 | | class RowsetMetaCloudPB; |
39 | | } // namespace doris |
40 | | |
41 | | namespace doris::cloud { |
42 | | class StorageVaultAccessor; |
43 | | class InstanceChecker; |
44 | | class TxnKv; |
45 | | class InstanceInfoPB; |
46 | | class ObjectStoreInfoPB; |
47 | | class HdfsVaultInfo; |
48 | | struct SharedResourceOwner { |
49 | | std::string instance_id; |
50 | | std::string resource_id; |
51 | | }; |
52 | | |
53 | | // resource id -> all instance/resource pairs that use the same physical storage. |
54 | | using SharedResourceOwners = std::unordered_map<std::string, std::vector<SharedResourceOwner>>; |
55 | | |
56 | | // Tells which instance/resource pairs write into the same physical storage. |
57 | | // Resource IDs are local to an instance, so the physical storage location is |
58 | | // matched instead: descendants can write into inherited resources even after |
59 | | // decoupling removes their source_instance_id. |
60 | | // |
61 | | // One index is shared by all checkers of a scheduling round. It is loaded |
62 | | // lazily on the first query, and the result (success or failure) is cached |
63 | | // until the round ends. A new round must create a new index to see changes. |
64 | | class SharedStorageIndex { |
65 | | public: |
66 | | // If `instances` is given, it is used as the full instance list instead of |
67 | | // scanning all instances from the kv store. |
68 | | explicit SharedStorageIndex( |
69 | | std::shared_ptr<TxnKv> txn_kv, |
70 | | std::optional<std::vector<InstanceInfoPB>> instances = std::nullopt); |
71 | | |
72 | | // For every resource of `instance_id`, fill in all owners sharing its physical storage. |
73 | | // A resource without identifiable storage location is only owned by itself. |
74 | | // Return 0 on success, -1 if the index failed to load or the instance is unknown. |
75 | | // Never fills partial owners on failure. |
76 | | int get_shared_resource_owners(std::string_view instance_id, SharedResourceOwners* owners); |
77 | | |
78 | | private: |
79 | | // Physical storage location. All fields must match exactly, overlapping |
80 | | // prefixes are not merged. Credentials are deliberately not part of it. |
81 | | struct StorageLocation { |
82 | | std::string type; // "s3" or "hdfs" |
83 | | std::string endpoint; // S3 endpoint, or HDFS fs_name |
84 | | std::string bucket; // empty for HDFS |
85 | | std::string prefix; |
86 | | |
87 | 0 | bool operator<(const StorageLocation& other) const { |
88 | 0 | return std::tie(type, endpoint, bucket, prefix) < |
89 | 0 | std::tie(other.type, other.endpoint, other.bucket, other.prefix); |
90 | 0 | } |
91 | | }; |
92 | | |
93 | | // Resources without connection info have no location, so that they never |
94 | | // match unrelated resources. |
95 | | using ResourceLocations = std::map<std::string, std::optional<StorageLocation>>; |
96 | | |
97 | | static std::optional<StorageLocation> s3_location(const ObjectStoreInfoPB& obj_info); |
98 | | static std::optional<StorageLocation> hdfs_location(const HdfsVaultInfo& hdfs_info); |
99 | | |
100 | | void ensure_loaded(); |
101 | | int load_all_instances(); |
102 | | int load_instance(const InstanceInfoPB& instance); |
103 | | int load_storage_vaults(const std::string& instance_id); |
104 | | void add_resource(const std::string& instance_id, const std::string& resource_id, |
105 | | std::optional<StorageLocation> location); |
106 | | |
107 | | std::shared_ptr<TxnKv> txn_kv_; |
108 | | // Consumed by the first load. |
109 | | std::optional<std::vector<InstanceInfoPB>> preloaded_instances_; |
110 | | |
111 | | std::once_flag load_once_; |
112 | | int load_status_ = -1; |
113 | | // instance_id -> (resource_id -> location) |
114 | | std::unordered_map<std::string, ResourceLocations> instance_resource_locations_; |
115 | | // location -> all instance/resource pairs located there |
116 | | std::map<StorageLocation, std::vector<SharedResourceOwner>> location_to_owners_; |
117 | | }; |
118 | | |
119 | | class Checker { |
120 | | public: |
121 | | explicit Checker(std::shared_ptr<TxnKv> txn_kv); |
122 | | ~Checker(); |
123 | | |
124 | | int start(); |
125 | | |
126 | | void stop(); |
127 | 326 | bool stopped() const { return stopped_.load(std::memory_order_acquire); } |
128 | | |
129 | | private: |
130 | | void lease_check_jobs(); |
131 | | void inspect_instance_check_interval(); |
132 | | void do_inspect(const InstanceInfoPB& instance); |
133 | | |
134 | | private: |
135 | | friend class RecyclerServiceImpl; |
136 | | |
137 | | std::shared_ptr<TxnKv> txn_kv_; |
138 | | std::shared_ptr<ResourceManager> resource_mgr_; |
139 | | std::atomic_bool stopped_ {false}; |
140 | | std::string ip_port_; |
141 | | std::vector<std::thread> workers_; |
142 | | |
143 | | std::mutex mtx_; |
144 | | // notify check workers |
145 | | std::condition_variable pending_instance_cond_; |
146 | | struct PendingInstance { |
147 | | InstanceInfoPB instance; |
148 | | std::shared_ptr<SharedStorageIndex> shared_storage_index; |
149 | | }; |
150 | | std::deque<PendingInstance> pending_instance_queue_; |
151 | | // instance_id -> enqueue_timestamp |
152 | | std::unordered_map<std::string, long> pending_instance_map_; |
153 | | std::unordered_map<std::string, std::shared_ptr<InstanceChecker>> working_instance_map_; |
154 | | // notify instance scanner and lease thread |
155 | | std::condition_variable notifier_; |
156 | | |
157 | | WhiteBlackList instance_filter_; |
158 | | }; |
159 | | |
160 | | class InstanceChecker { |
161 | | public: |
162 | | explicit InstanceChecker(std::shared_ptr<TxnKv> txn_kv, const std::string& instance_id, |
163 | | std::shared_ptr<ResourceManager> resource_mgr = nullptr, |
164 | | std::shared_ptr<SharedStorageIndex> shared_storage_index = nullptr); |
165 | | // Return 0 if success, otherwise error |
166 | | int init(const InstanceInfoPB& instance); |
167 | | // Return 0 if success. |
168 | | // Return 1 if data leak is identified. |
169 | | // Return negative if a temporary error occurred during the check process. |
170 | | int do_inverted_check(); |
171 | | |
172 | | // Return 0 if success. |
173 | | // Return 1 if data loss is identified. |
174 | | // Return negative if a temporary error occurred during the check process. |
175 | | int do_check(); |
176 | | |
177 | | // Return 0 if success. |
178 | | // Return 1 if delete bitmap leak is identified. |
179 | | // Return negative if a temporary error occurred during the check process. |
180 | | int do_delete_bitmap_inverted_check(); |
181 | | |
182 | | // version = 1 : https://github.com/apache/doris/pull/40204 |
183 | | // checks if https://github.com/apache/doris/pull/40204 works as expected |
184 | | // the stale delete bitmap will be cleared in MS when BE delete expired stale rowsets |
185 | | // NOTE: stale rowsets will be lost after BE restarts, so there may be some stale delete bitmaps |
186 | | // which will not be cleared. |
187 | | // version = 2 : https://github.com/apache/doris/pull/49822 |
188 | | int do_delete_bitmap_storage_optimize_check(int version = 2); |
189 | | |
190 | | int do_mow_job_key_check(); |
191 | | |
192 | | int do_tablet_stats_key_check(); |
193 | | |
194 | | int do_restore_job_check(); |
195 | | |
196 | | int do_txn_key_check(); |
197 | | |
198 | | // check table and partition version key |
199 | | // table version should be greater than the versions of all its partitions |
200 | | // Return 0 if success, otherwise error |
201 | | int do_version_key_check(); |
202 | | |
203 | | // Return 0 if success. |
204 | | // Return 1 if meta rowset key leak or loss is identified. |
205 | | // Return negative if a temporary error occurred during the check process. |
206 | | int do_meta_rowset_key_check(); |
207 | | |
208 | | // Return 0 if success. |
209 | | // Return 1 if snapshot key and file leak or loss is identified. |
210 | | // Return negative if a temporary error occurred during the check process. |
211 | | int do_snapshots_check(); |
212 | | |
213 | | // Return 0 if success. |
214 | | // Return 1 if mvcc meta key and data leak or loss is identified. |
215 | | // Return negative if a temporary error occurred during the check process. |
216 | | int do_mvcc_meta_key_check(); |
217 | | |
218 | | // Return 0 if success. |
219 | | // Return 1 if packed file metadata leak or loss is identified. |
220 | | // Return negative if a temporary error occurred during the check process. |
221 | | int do_packed_file_check(); |
222 | | |
223 | | StorageVaultAccessor* get_accessor(const std::string& id); |
224 | | |
225 | 0 | ResourceManager* resource_mgr() const { return resource_mgr_.get(); } |
226 | | |
227 | 0 | SharedStorageIndex* shared_storage_index() const { return shared_storage_index_.get(); } |
228 | | |
229 | | void get_all_accessor(std::vector<StorageVaultAccessor*>* accessors); |
230 | | |
231 | 0 | std::string_view instance_id() const { return instance_id_; } |
232 | | |
233 | 0 | void TEST_add_accessor(std::string_view id, std::shared_ptr<StorageVaultAccessor> accessor) { |
234 | 0 | accessor_map_.insert({std::string(id), std::move(accessor)}); |
235 | 0 | } |
236 | | |
237 | | // If there are multiple buckets, return the minimum lifecycle; if there are no buckets (i.e. |
238 | | // all accessors are HdfsAccessor), return INT64_MAX. |
239 | | // Return 0 if success, otherwise error |
240 | | int get_bucket_lifecycle(int64_t* lifecycle_days); |
241 | 0 | void stop() { stopped_.store(true, std::memory_order_release); } |
242 | 5.81k | bool stopped() const { return stopped_.load(std::memory_order_acquire); } |
243 | | |
244 | | private: |
245 | | struct RowsetIndexesFormatV1 { |
246 | | std::string rowset_id; |
247 | | std::unordered_set<int64_t> segment_ids; |
248 | | std::unordered_set<std::string> index_ids; |
249 | | }; |
250 | | |
251 | | struct RowsetIndexesFormatV2 { |
252 | | std::string rowset_id; |
253 | | std::unordered_set<int64_t> segment_ids; |
254 | | }; |
255 | | |
256 | | private: |
257 | | // returns 0 for success otherwise error |
258 | | int init_obj_store_accessors(const InstanceInfoPB& instance); |
259 | | |
260 | | // returns 0 for success otherwise error |
261 | | int init_storage_vault_accessors(const InstanceInfoPB& instance); |
262 | | |
263 | | int traverse_mow_tablet(const std::function<int(int64_t, bool)>& check_func); |
264 | | int traverse_rowset_delete_bitmaps( |
265 | | int64_t tablet_id, std::string rowset_id, |
266 | | const std::function<int(int64_t, std::string_view, int64_t, int64_t)>& callback); |
267 | | int collect_tablet_rowsets( |
268 | | int64_t tablet_id, |
269 | | const std::function<void(const doris::RowsetMetaCloudPB&)>& collect_cb); |
270 | | int collect_unexpired_job_tmp_rowsets( |
271 | | std::unordered_map<int64_t, std::unordered_set<std::string>>& tmp_rowsets); |
272 | | int get_pending_delete_bitmap_keys(int64_t tablet_id, |
273 | | std::unordered_set<std::string>& pending_delete_bitmaps); |
274 | | int check_delete_bitmap_storage_optimize_v2(int64_t tablet_id, bool has_sequence_col, |
275 | | int64_t& abnormal_rowsets_num); |
276 | | |
277 | | int check_inverted_index_file_storage_format_v1(int64_t tablet_id, const std::string& file_path, |
278 | | const std::string& rowset_info, |
279 | | RowsetIndexesFormatV1& rowset_index_cache_v1); |
280 | | |
281 | | int check_inverted_index_file_storage_format_v2(int64_t tablet_id, const std::string& file_path, |
282 | | const std::string& rowset_info, |
283 | | RowsetIndexesFormatV2& rowset_index_cache_v2); |
284 | | |
285 | | // Return 0 if success. |
286 | | // Return 1 if key loss is abnormal. |
287 | | // Return negative if a temporary error occurred during the check process. |
288 | | int check_stats_tablet_key(std::string_view key, std::string_view value); |
289 | | |
290 | | // Return 0 if success. |
291 | | // Return 1 if key loss is identified. |
292 | | // Return negative if a temporary error occurred during the check process. |
293 | | int check_stats_tablet_key_exists(std::string_view key, std::string_view value); |
294 | | |
295 | | // Return 0 if success. |
296 | | // Return 1 if key leak is identified. |
297 | | // Return negative if a temporary error occurred during the check process. |
298 | | int check_stats_tablet_key_leaked(std::string_view key, std::string_view value); |
299 | | int check_txn_info_key(std::string_view key, std::string_view value); |
300 | | |
301 | | int check_txn_label_key(std::string_view key, std::string_view value); |
302 | | |
303 | | int check_txn_index_key(std::string_view key, std::string_view value); |
304 | | |
305 | | int check_txn_running_key(std::string_view key, std::string_view value); |
306 | | |
307 | | // Only check whether the meta rowset key is leak |
308 | | // in do_inverted_check() function, check whether the key is lost by comparing data file with key |
309 | | // Return 0 if success. |
310 | | // Return 1 if meta rowset key leak is identified. |
311 | | // Return negative if a temporary error occurred during the check process. |
312 | | int check_meta_rowset_key(std::string_view key, std::string_view value); |
313 | | |
314 | | // if TxnInfoKey's finish time > current time, it should not find tmp rowset |
315 | | // Return 0 if success. |
316 | | // Return 1 if meta tmp rowset key is abnormal. |
317 | | // Return negative if a temporary error occurred during the check process. |
318 | | int check_meta_tmp_rowset_key(std::string_view key, std::string_view value); |
319 | | |
320 | | /** |
321 | | * It is used to scan the key in the range from start_key to end_key |
322 | | * and then perform handle operations on each group of kv |
323 | | * |
324 | | * @param start_key Range begining. Note that this function will modify the `start_key` |
325 | | * @param end_key Range ending |
326 | | * @param handle_kv Operations on kv |
327 | | * @return code int 0 for success to scan and hanle, 1 for success to scan but handle abnormally, -1 for failed to handle |
328 | | */ |
329 | | int scan_and_handle_kv(std::string& start_key, const std::string& end_key, |
330 | | std::function<int(std::string_view, std::string_view)> handle_kv); |
331 | | |
332 | | std::atomic_bool stopped_ {false}; |
333 | | std::shared_ptr<TxnKv> txn_kv_; |
334 | | std::string instance_id_; |
335 | | // id -> accessor |
336 | | std::unordered_map<std::string, std::shared_ptr<StorageVaultAccessor>> accessor_map_; |
337 | | std::shared_ptr<SnapshotManager> snapshot_manager_; |
338 | | std::shared_ptr<ResourceManager> resource_mgr_; |
339 | | std::shared_ptr<SharedStorageIndex> shared_storage_index_; |
340 | | }; |
341 | | |
342 | | } // namespace doris::cloud |