/root/doris/cloud/src/recycler/recycler.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/cloud.pb.h> |
21 | | #include <glog/logging.h> |
22 | | |
23 | | #include <atomic> |
24 | | #include <condition_variable> |
25 | | #include <cstdint> |
26 | | #include <deque> |
27 | | #include <functional> |
28 | | #include <memory> |
29 | | #include <string> |
30 | | #include <string_view> |
31 | | #include <thread> |
32 | | #include <utility> |
33 | | |
34 | | #include "common/bvars.h" |
35 | | #include "meta-service/txn_lazy_committer.h" |
36 | | #include "meta-store/versionstamp.h" |
37 | | #include "recycler/storage_vault_accessor.h" |
38 | | #include "recycler/white_black_list.h" |
39 | | |
40 | | namespace brpc { |
41 | | class Server; |
42 | | } // namespace brpc |
43 | | |
44 | | namespace doris::cloud { |
45 | | class TxnKv; |
46 | | class InstanceRecycler; |
47 | | class StorageVaultAccessor; |
48 | | class Checker; |
49 | | class SimpleThreadPool; |
50 | | class RecyclerMetricsContext; |
51 | | class TabletRecyclerMetricsContext; |
52 | | class SegmentRecyclerMetricsContext; |
53 | | struct RecyclerThreadPoolGroup { |
54 | 8 | RecyclerThreadPoolGroup() = default; |
55 | | RecyclerThreadPoolGroup(std::shared_ptr<SimpleThreadPool> s3_producer_pool, |
56 | | std::shared_ptr<SimpleThreadPool> recycle_tablet_pool, |
57 | | std::shared_ptr<SimpleThreadPool> group_recycle_function_pool) |
58 | | : s3_producer_pool(std::move(s3_producer_pool)), |
59 | | recycle_tablet_pool(std::move(recycle_tablet_pool)), |
60 | 10 | group_recycle_function_pool(std::move(group_recycle_function_pool)) {} |
61 | 228 | ~RecyclerThreadPoolGroup() = default; |
62 | 105 | RecyclerThreadPoolGroup(const RecyclerThreadPoolGroup&) = default; |
63 | | RecyclerThreadPoolGroup& operator=(RecyclerThreadPoolGroup& other) = default; |
64 | 10 | RecyclerThreadPoolGroup& operator=(RecyclerThreadPoolGroup&& other) = default; |
65 | 105 | RecyclerThreadPoolGroup(RecyclerThreadPoolGroup&&) = default; |
66 | | // used for accessor.delete_files, accessor.delete_directory |
67 | | std::shared_ptr<SimpleThreadPool> s3_producer_pool; |
68 | | // used for InstanceRecycler::recycle_tablet |
69 | | std::shared_ptr<SimpleThreadPool> recycle_tablet_pool; |
70 | | std::shared_ptr<SimpleThreadPool> group_recycle_function_pool; |
71 | | }; |
72 | | |
73 | | class Recycler { |
74 | | public: |
75 | | explicit Recycler(std::shared_ptr<TxnKv> txn_kv); |
76 | | ~Recycler(); |
77 | | |
78 | | // returns 0 for success otherwise error |
79 | | int start(brpc::Server* server); |
80 | | |
81 | | void stop(); |
82 | | |
83 | 297 | bool stopped() const { return stopped_.load(std::memory_order_acquire); } |
84 | | |
85 | | private: |
86 | | void recycle_callback(); |
87 | | |
88 | | void instance_scanner_callback(); |
89 | | |
90 | | void lease_recycle_jobs(); |
91 | | |
92 | | void check_recycle_tasks(); |
93 | | |
94 | | private: |
95 | | friend class RecyclerServiceImpl; |
96 | | |
97 | | std::shared_ptr<TxnKv> txn_kv_; |
98 | | std::atomic_bool stopped_ {false}; |
99 | | |
100 | | std::vector<std::thread> workers_; |
101 | | |
102 | | std::mutex mtx_; |
103 | | // notify recycle workers |
104 | | std::condition_variable pending_instance_cond_; |
105 | | std::deque<InstanceInfoPB> pending_instance_queue_; |
106 | | std::unordered_set<std::string> pending_instance_set_; |
107 | | std::unordered_map<std::string, std::shared_ptr<InstanceRecycler>> recycling_instance_map_; |
108 | | // notify instance scanner and lease thread |
109 | | std::condition_variable notifier_; |
110 | | |
111 | | std::string ip_port_; |
112 | | |
113 | | WhiteBlackList instance_filter_; |
114 | | std::unique_ptr<Checker> checker_; |
115 | | |
116 | | RecyclerThreadPoolGroup _thread_pool_group; |
117 | | |
118 | | std::shared_ptr<TxnLazyCommitter> txn_lazy_committer_; |
119 | | }; |
120 | | |
121 | | enum class RowsetRecyclingState { |
122 | | FORMAL_ROWSET, |
123 | | TMP_ROWSET, |
124 | | }; |
125 | | |
126 | | class RecyclerMetricsContext { |
127 | | public: |
128 | 1 | RecyclerMetricsContext() = default; |
129 | | |
130 | | RecyclerMetricsContext(std::string instance_id, std::string operation_type) |
131 | 376 | : operation_type(std::move(operation_type)), instance_id(std::move(instance_id)) { |
132 | 376 | start(); |
133 | 376 | } |
134 | | |
135 | 376 | ~RecyclerMetricsContext() = default; |
136 | | |
137 | | std::atomic_ullong total_need_recycle_data_size = 0; |
138 | | std::atomic_ullong total_need_recycle_num = 0; |
139 | | |
140 | | std::atomic_ullong total_recycled_data_size = 0; |
141 | | std::atomic_ullong total_recycled_num = 0; |
142 | | |
143 | | std::string operation_type; |
144 | | std::string instance_id; |
145 | | |
146 | | double start_time = 0; |
147 | | |
148 | 376 | void start() { |
149 | 376 | start_time = duration_cast<std::chrono::milliseconds>( |
150 | 376 | std::chrono::system_clock::now().time_since_epoch()) |
151 | 376 | .count(); |
152 | 376 | } |
153 | | |
154 | 184 | double duration() const { |
155 | 184 | return duration_cast<std::chrono::milliseconds>( |
156 | 184 | std::chrono::system_clock::now().time_since_epoch()) |
157 | 184 | .count() - |
158 | 184 | start_time; |
159 | 184 | } |
160 | | |
161 | 20 | void reset() { |
162 | 20 | total_need_recycle_data_size = 0; |
163 | 20 | total_need_recycle_num = 0; |
164 | 20 | total_recycled_data_size = 0; |
165 | 20 | total_recycled_num = 0; |
166 | 20 | start_time = duration_cast<std::chrono::milliseconds>( |
167 | 20 | std::chrono::system_clock::now().time_since_epoch()) |
168 | 20 | .count(); |
169 | 20 | } |
170 | | |
171 | 184 | void finish_report() { |
172 | 184 | if (!operation_type.empty()) { |
173 | 184 | double cost = duration(); |
174 | 184 | g_bvar_recycler_instance_last_round_recycle_elpased_ts.put( |
175 | 184 | {instance_id, operation_type}, cost); |
176 | 184 | g_bvar_recycler_instance_recycle_round.put({instance_id, operation_type}, 1); |
177 | 184 | LOG(INFO) << "recycle instance: " << instance_id |
178 | 184 | << ", operation type: " << operation_type << ", cost: " << cost |
179 | 184 | << " ms, total recycled num: " << total_recycled_num.load() |
180 | 184 | << ", total recycled data size: " << total_recycled_data_size.load() |
181 | 184 | << " bytes"; |
182 | 184 | if (cost != 0) { |
183 | 171 | if (total_recycled_num.load() != 0) { |
184 | 27 | g_bvar_recycler_instance_recycle_time_per_resource.put( |
185 | 27 | {instance_id, operation_type}, cost / total_recycled_num.load()); |
186 | 27 | } |
187 | 171 | g_bvar_recycler_instance_recycle_bytes_per_ms.put( |
188 | 171 | {instance_id, operation_type}, total_recycled_data_size.load() / cost); |
189 | 171 | } |
190 | 184 | } |
191 | 184 | } |
192 | | |
193 | | // `is_begin` is used to initialize total num of items need to be recycled |
194 | 24.1k | void report(bool is_begin = false) { |
195 | 24.1k | if (!operation_type.empty()) { |
196 | | // is init |
197 | 24.0k | if (is_begin) { |
198 | 0 | auto value = total_need_recycle_num.load(); |
199 | |
|
200 | 0 | g_bvar_recycler_instance_last_round_to_recycle_bytes.put( |
201 | 0 | {instance_id, operation_type}, total_need_recycle_data_size.load()); |
202 | 0 | g_bvar_recycler_instance_last_round_to_recycle_num.put( |
203 | 0 | {instance_id, operation_type}, value); |
204 | 24.0k | } else { |
205 | 24.0k | g_bvar_recycler_instance_last_round_recycled_bytes.put( |
206 | 24.0k | {instance_id, operation_type}, total_recycled_data_size.load()); |
207 | 24.0k | g_bvar_recycler_instance_recycle_total_bytes_since_started.put( |
208 | 24.0k | {instance_id, operation_type}, total_recycled_data_size.load()); |
209 | 24.0k | g_bvar_recycler_instance_last_round_recycled_num.put({instance_id, operation_type}, |
210 | 24.0k | total_recycled_num.load()); |
211 | 24.0k | g_bvar_recycler_instance_recycle_total_num_since_started.put( |
212 | 24.0k | {instance_id, operation_type}, total_recycled_num.load()); |
213 | 24.0k | } |
214 | 24.0k | } |
215 | 24.1k | } |
216 | | }; |
217 | | |
218 | | class TabletRecyclerMetricsContext : public RecyclerMetricsContext { |
219 | | public: |
220 | 105 | TabletRecyclerMetricsContext() : RecyclerMetricsContext("global_recycler", "recycle_tablet") {} |
221 | | }; |
222 | | |
223 | | class SegmentRecyclerMetricsContext : public RecyclerMetricsContext { |
224 | | public: |
225 | | SegmentRecyclerMetricsContext() |
226 | 105 | : RecyclerMetricsContext("global_recycler", "recycle_segment") {} |
227 | | }; |
228 | | |
229 | | class InstanceRecycler { |
230 | | public: |
231 | | explicit InstanceRecycler(std::shared_ptr<TxnKv> txn_kv, const InstanceInfoPB& instance, |
232 | | RecyclerThreadPoolGroup thread_pool_group, |
233 | | std::shared_ptr<TxnLazyCommitter> txn_lazy_committer); |
234 | | ~InstanceRecycler(); |
235 | | |
236 | | // returns 0 for success otherwise error |
237 | | int init(); |
238 | | |
239 | 0 | void stop() { stopped_.store(true, std::memory_order_release); } |
240 | 51 | bool stopped() const { return stopped_.load(std::memory_order_acquire); } |
241 | | |
242 | | // returns 0 for success otherwise error |
243 | | int do_recycle(); |
244 | | |
245 | | // remove all kv and data in this instance, ONLY be called when instance has been deleted |
246 | | // returns 0 for success otherwise error |
247 | | int recycle_deleted_instance(); |
248 | | |
249 | | // scan and recycle expired indexes: |
250 | | // 1. dropped table, dropped mv |
251 | | // 2. half-successtable/index when create |
252 | | // returns 0 for success otherwise error |
253 | | int recycle_indexes(); |
254 | | |
255 | | // scan and recycle expired partitions: |
256 | | // 1. dropped parttion |
257 | | // 2. half-success partition when create |
258 | | // returns 0 for success otherwise error |
259 | | int recycle_partitions(); |
260 | | |
261 | | // scan and recycle expired rowsets: |
262 | | // 1. prepare_rowset will produce recycle_rowset before uploading data to remote storage (memo) |
263 | | // 2. compaction will change the input rowsets to recycle_rowset |
264 | | // returns 0 for success otherwise error |
265 | | int recycle_rowsets(); |
266 | | |
267 | | // like `recycle_rowsets`, but for versioned rowsets. |
268 | | int recycle_versioned_rowsets(); |
269 | | |
270 | | // scan and recycle expired tmp rowsets: |
271 | | // 1. commit_rowset will produce tmp_rowset when finish upload data (load or compaction) to remote storage |
272 | | // returns 0 for success otherwise error |
273 | | int recycle_tmp_rowsets(); |
274 | | |
275 | | /** |
276 | | * recycle all tablets belonging to the index specified by `index_id` |
277 | | * |
278 | | * @param partition_id if positive, only recycle tablets in this partition belonging to the specified index |
279 | | * @return 0 for success otherwise error |
280 | | */ |
281 | | int recycle_tablets(int64_t table_id, int64_t index_id, RecyclerMetricsContext& ctx, |
282 | | int64_t partition_id = -1); |
283 | | |
284 | | /** |
285 | | * recycle all rowsets belonging to the tablet specified by `tablet_id` |
286 | | * |
287 | | * @return 0 for success otherwise error |
288 | | */ |
289 | | int recycle_tablet(int64_t tablet_id, RecyclerMetricsContext& metrics_context); |
290 | | |
291 | | /** |
292 | | * like `recycle_tablet`, but for versioned tablet |
293 | | */ |
294 | | int recycle_versioned_tablet(int64_t tablet_id, RecyclerMetricsContext& metrics_context); |
295 | | |
296 | | // scan and recycle useless partition version kv |
297 | | int recycle_versions(); |
298 | | |
299 | | // scan and abort timeout txn label |
300 | | // returns 0 for success otherwise error |
301 | | int abort_timeout_txn(); |
302 | | |
303 | | //scan and recycle expire txn label |
304 | | // returns 0 for success otherwise error |
305 | | int recycle_expired_txn_label(); |
306 | | |
307 | | // scan and recycle finished or timeout copy jobs |
308 | | // returns 0 for success otherwise error |
309 | | int recycle_copy_jobs(); |
310 | | |
311 | | // scan and recycle dropped internal stage |
312 | | // returns 0 for success otherwise error |
313 | | int recycle_stage(); |
314 | | |
315 | | // scan and recycle expired stage objects |
316 | | // returns 0 for success otherwise error |
317 | | int recycle_expired_stage_objects(); |
318 | | |
319 | | // scan and recycle operation logs |
320 | | // returns 0 for success otherwise error |
321 | | int recycle_operation_logs(); |
322 | | |
323 | | // scan and recycle expired restore jobs |
324 | | // returns 0 for success otherwise error |
325 | | int recycle_restore_jobs(); |
326 | | |
327 | | bool check_recycle_tasks(); |
328 | | |
329 | | int scan_and_statistics_indexes(); |
330 | | |
331 | | int scan_and_statistics_partitions(); |
332 | | |
333 | | int scan_and_statistics_rowsets(); |
334 | | |
335 | | int scan_and_statistics_tmp_rowsets(); |
336 | | |
337 | | int scan_and_statistics_abort_timeout_txn(); |
338 | | |
339 | | int scan_and_statistics_expired_txn_label(); |
340 | | |
341 | | int scan_and_statistics_copy_jobs(); |
342 | | |
343 | | int scan_and_statistics_stage(); |
344 | | |
345 | | int scan_and_statistics_expired_stage_objects(); |
346 | | |
347 | | int scan_and_statistics_versions(); |
348 | | |
349 | | int scan_and_statistics_restore_jobs(); |
350 | | |
351 | 5 | void TEST_add_accessor(std::string_view id, std::shared_ptr<StorageVaultAccessor> accessor) { |
352 | 5 | accessor_map_.insert({std::string(id), std::move(accessor)}); |
353 | 5 | } |
354 | | |
355 | | private: |
356 | | // returns 0 for success otherwise error |
357 | | int init_obj_store_accessors(); |
358 | | |
359 | | // returns 0 for success otherwise error |
360 | | int init_storage_vault_accessors(); |
361 | | |
362 | | /** |
363 | | * Scan key-value pairs between [`begin`, `end`), and perform `recycle_func` on each key-value pair. |
364 | | * |
365 | | * @param recycle_func defines how to recycle resources corresponding to a key-value pair. Returns 0 if the recycling is successful. |
366 | | * @param loop_done is called after `RangeGetIterator` has no next kv. Usually used to perform a batch recycling. Returns 0 if success. |
367 | | * @return 0 if all corresponding resources are recycled successfully, otherwise non-zero |
368 | | */ |
369 | | int scan_and_recycle(std::string begin, std::string_view end, |
370 | | std::function<int(std::string_view k, std::string_view v)> recycle_func, |
371 | | std::function<int()> loop_done = nullptr); |
372 | | |
373 | | // return 0 for success otherwise error |
374 | | int delete_rowset_data(const doris::RowsetMetaCloudPB& rs_meta_pb); |
375 | | |
376 | | // return 0 for success otherwise error |
377 | | // NOTE: this function ONLY be called when the file paths cannot be calculated |
378 | | int delete_rowset_data(const std::string& resource_id, int64_t tablet_id, |
379 | | const std::string& rowset_id); |
380 | | |
381 | | // return 0 for success otherwise error |
382 | | int delete_rowset_data(const std::map<std::string, doris::RowsetMetaCloudPB>& rowsets, |
383 | | RowsetRecyclingState type, RecyclerMetricsContext& metrics_context); |
384 | | |
385 | | /** |
386 | | * Get stage storage info from instance and init StorageVaultAccessor |
387 | | * @return 0 if accessor is successfully inited, 1 if stage not found, negative for error |
388 | | */ |
389 | | int init_copy_job_accessor(const std::string& stage_id, const StagePB::StageType& stage_type, |
390 | | std::shared_ptr<StorageVaultAccessor>* accessor); |
391 | | |
392 | | void register_recycle_task(const std::string& task_name, int64_t start_time); |
393 | | |
394 | | void unregister_recycle_task(const std::string& task_name); |
395 | | |
396 | | // for scan all tablets and statistics metrics |
397 | | int scan_tablets_and_statistics(int64_t tablet_id, int64_t index_id, |
398 | | RecyclerMetricsContext& metrics_context, |
399 | | int64_t partition_id = -1, bool is_empty_tablet = false); |
400 | | |
401 | | // for scan all rs of tablet and statistics metrics |
402 | | int scan_tablet_and_statistics(int64_t tablet_id, RecyclerMetricsContext& metrics_context); |
403 | | |
404 | | // Recycle operation log and the log key. |
405 | | // |
406 | | // The log_key is constructed from the log_version and instance_id. |
407 | | // Both `operation_log` and `log_key` will be removed in the same transaction, to ensure atomicity. |
408 | | int recycle_operation_log(Versionstamp log_version, OperationLogPB operation_log); |
409 | | |
410 | | // Recycle rowset meta and data, return 0 for success otherwise error |
411 | | // |
412 | | // This function will decrease the rowset ref count and remove the rowset meta and data if the ref count is 1. |
413 | | int recycle_rowset_meta_and_data(std::string_view recycle_rowset_key, |
414 | | const RowsetMetaCloudPB& rowset_meta); |
415 | | |
416 | | // Whether the instance has any snapshots, return 0 for success otherwise error. |
417 | | int has_cluster_snapshots(bool* any); |
418 | | |
419 | | private: |
420 | | std::atomic_bool stopped_ {false}; |
421 | | std::shared_ptr<TxnKv> txn_kv_; |
422 | | std::string instance_id_; |
423 | | InstanceInfoPB instance_info_; |
424 | | |
425 | | // TODO(plat1ko): Add new accessor to map in runtime for new created storage vaults |
426 | | std::unordered_map<std::string, std::shared_ptr<StorageVaultAccessor>> accessor_map_; |
427 | | using InvertedIndexInfo = |
428 | | std::pair<InvertedIndexStorageFormatPB, std::vector<std::pair<int64_t, std::string>>>; |
429 | | |
430 | | class InvertedIndexIdCache; |
431 | | std::unique_ptr<InvertedIndexIdCache> inverted_index_id_cache_; |
432 | | |
433 | | std::mutex recycled_tablets_mtx_; |
434 | | // Store recycled tablets, we can skip deleting rowset data of these tablets because these data has already been deleted. |
435 | | std::unordered_set<int64_t> recycled_tablets_; |
436 | | |
437 | | std::mutex recycle_tasks_mutex; |
438 | | // <task_name, start_time>> |
439 | | std::map<std::string, int64_t> running_recycle_tasks; |
440 | | |
441 | | RecyclerThreadPoolGroup _thread_pool_group; |
442 | | |
443 | | std::shared_ptr<TxnLazyCommitter> txn_lazy_committer_; |
444 | | |
445 | | TabletRecyclerMetricsContext tablet_metrics_context_; |
446 | | SegmentRecyclerMetricsContext segment_metrics_context_; |
447 | | }; |
448 | | |
449 | | // Helper class to check if operation logs can be recycled based on snapshots and versionstamps |
450 | | class OperationLogRecycleChecker { |
451 | | public: |
452 | | OperationLogRecycleChecker(std::string_view instance_id, TxnKv* txn_kv) |
453 | 20 | : instance_id_(instance_id), txn_kv_(txn_kv) {} |
454 | | |
455 | | // Initialize the checker by loading snapshots and setting max version stamp |
456 | | int init(); |
457 | | |
458 | | // Check if an operation log can be recycled |
459 | | bool can_recycle(const Versionstamp& log_versionstamp, int64_t log_min_timestamp) const; |
460 | | |
461 | 0 | Versionstamp max_versionstamp() const { return max_versionstamp_; } |
462 | | |
463 | | private: |
464 | | std::string_view instance_id_; |
465 | | TxnKv* txn_kv_; |
466 | | Versionstamp max_versionstamp_; |
467 | | std::map<Versionstamp, size_t> snapshot_indexes_; |
468 | | std::vector<std::pair<SnapshotPB, Versionstamp>> snapshots_; |
469 | | }; |
470 | | |
471 | | } // namespace doris::cloud |