/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 <map> |
29 | | #include <memory> |
30 | | #include <string> |
31 | | #include <string_view> |
32 | | #include <thread> |
33 | | #include <unordered_map> |
34 | | #include <unordered_set> |
35 | | #include <utility> |
36 | | #include <vector> |
37 | | |
38 | | #include "common/bvars.h" |
39 | | #include "meta-service/delete_bitmap_lock_white_list.h" |
40 | | #include "meta-service/txn_lazy_committer.h" |
41 | | #include "meta-store/versionstamp.h" |
42 | | #include "recycler/snapshot_chain_compactor.h" |
43 | | #include "recycler/snapshot_data_migrator.h" |
44 | | #include "recycler/storage_vault_accessor.h" |
45 | | #include "snapshot/snapshot_manager.h" |
46 | | |
47 | | namespace brpc { |
48 | | class Server; |
49 | | } // namespace brpc |
50 | | |
51 | | namespace doris::cloud { |
52 | | class TxnKv; |
53 | | class InstanceRecycler; |
54 | | class StorageVaultAccessor; |
55 | | class Checker; |
56 | | class SimpleThreadPool; |
57 | | class RecyclerMetricsContext; |
58 | | class TabletRecyclerMetricsContext; |
59 | | class SegmentRecyclerMetricsContext; |
60 | | |
61 | | int64_t calculate_tmp_rowset_expired_time( |
62 | | const std::string& instance_id_, const doris::RowsetMetaCloudPB& tmp_rowset_meta_pb, |
63 | | int64_t* earlest_ts /* tmp_rowset earliest expiration ts */); |
64 | | |
65 | | struct RecyclerThreadPoolGroup { |
66 | 11 | RecyclerThreadPoolGroup() = default; |
67 | | RecyclerThreadPoolGroup(std::shared_ptr<SimpleThreadPool> s3_producer_pool, |
68 | | std::shared_ptr<SimpleThreadPool> recycle_tablet_pool, |
69 | | std::shared_ptr<SimpleThreadPool> group_recycle_function_pool) |
70 | | : s3_producer_pool(std::move(s3_producer_pool)), |
71 | | recycle_tablet_pool(std::move(recycle_tablet_pool)), |
72 | 13 | group_recycle_function_pool(std::move(group_recycle_function_pool)) {} |
73 | 306 | ~RecyclerThreadPoolGroup() = default; |
74 | 141 | RecyclerThreadPoolGroup(const RecyclerThreadPoolGroup&) = default; |
75 | | RecyclerThreadPoolGroup& operator=(RecyclerThreadPoolGroup& other) = default; |
76 | 14 | RecyclerThreadPoolGroup& operator=(RecyclerThreadPoolGroup&& other) = default; |
77 | 141 | RecyclerThreadPoolGroup(RecyclerThreadPoolGroup&&) = default; |
78 | | // used for accessor.delete_files, accessor.delete_directory |
79 | | std::shared_ptr<SimpleThreadPool> s3_producer_pool; |
80 | | // used for InstanceRecycler::recycle_tablet |
81 | | std::shared_ptr<SimpleThreadPool> recycle_tablet_pool; |
82 | | std::shared_ptr<SimpleThreadPool> group_recycle_function_pool; |
83 | | }; |
84 | | |
85 | | class Recycler { |
86 | | public: |
87 | | explicit Recycler(std::shared_ptr<TxnKv> txn_kv); |
88 | | ~Recycler(); |
89 | | |
90 | | // returns 0 for success otherwise error |
91 | | int start(brpc::Server* server); |
92 | | |
93 | | void stop(); |
94 | | |
95 | 4.02k | bool stopped() const { return stopped_.load(std::memory_order_acquire); } |
96 | | |
97 | 0 | RecyclerThreadPoolGroup& thread_pool_group() { return _thread_pool_group; } |
98 | | |
99 | | private: |
100 | | void recycle_callback(); |
101 | | |
102 | | void instance_scanner_callback(); |
103 | | |
104 | | void lease_recycle_jobs(); |
105 | | |
106 | | void check_recycle_tasks(); |
107 | | |
108 | | private: |
109 | | friend class RecyclerServiceImpl; |
110 | | |
111 | | std::shared_ptr<TxnKv> txn_kv_; |
112 | | std::atomic_bool stopped_ {false}; |
113 | | |
114 | | std::vector<std::thread> workers_; |
115 | | |
116 | | std::mutex mtx_; |
117 | | // notify recycle workers |
118 | | std::condition_variable pending_instance_cond_; |
119 | | std::deque<InstanceInfoPB> pending_instance_queue_; |
120 | | std::unordered_set<std::string> pending_instance_set_; |
121 | | std::unordered_map<std::string, std::shared_ptr<InstanceRecycler>> recycling_instance_map_; |
122 | | // notify instance scanner and lease thread |
123 | | std::condition_variable notifier_; |
124 | | |
125 | | std::string ip_port_; |
126 | | |
127 | | std::unique_ptr<Checker> checker_; |
128 | | |
129 | | RecyclerThreadPoolGroup _thread_pool_group; |
130 | | |
131 | | std::shared_ptr<TxnLazyCommitter> txn_lazy_committer_; |
132 | | std::shared_ptr<SnapshotManager> snapshot_manager_; |
133 | | std::shared_ptr<SnapshotDataMigrator> snapshot_data_migrator_; |
134 | | std::shared_ptr<SnapshotChainCompactor> snapshot_chain_compactor_; |
135 | | }; |
136 | | |
137 | | enum class RowsetRecyclingState { |
138 | | FORMAL_ROWSET, |
139 | | TMP_ROWSET, |
140 | | }; |
141 | | |
142 | | // Represents a single rowset deletion task for batch delete |
143 | | struct RowsetDeleteTask { |
144 | | RowsetMetaCloudPB rowset_meta; |
145 | | std::string recycle_rowset_key; // Primary key marking "pending recycle" |
146 | | std::string non_versioned_rowset_key; // Legacy non-versioned rowset meta key |
147 | | std::string versioned_rowset_key; // Versioned meta rowset key |
148 | | Versionstamp versionstamp; |
149 | | std::string rowset_ref_count_key; |
150 | | }; |
151 | | |
152 | | class RecyclerMetricsContext { |
153 | | public: |
154 | 10 | RecyclerMetricsContext() = default; |
155 | | |
156 | | RecyclerMetricsContext(std::string instance_id, std::string operation_type) |
157 | 571 | : operation_type(std::move(operation_type)), instance_id(std::move(instance_id)) { |
158 | 571 | start(); |
159 | 571 | } |
160 | | |
161 | 580 | ~RecyclerMetricsContext() = default; |
162 | | |
163 | | std::atomic_ullong total_need_recycle_data_size = 0; |
164 | | std::atomic_ullong total_need_recycle_num = 0; |
165 | | |
166 | | std::atomic_ullong total_recycled_data_size = 0; |
167 | | std::atomic_ullong total_recycled_num = 0; |
168 | | |
169 | | std::string operation_type; |
170 | | std::string instance_id; |
171 | | |
172 | | double start_time = 0; |
173 | | |
174 | 571 | void start() { |
175 | 571 | start_time = duration_cast<std::chrono::milliseconds>( |
176 | 571 | std::chrono::system_clock::now().time_since_epoch()) |
177 | 571 | .count(); |
178 | 571 | } |
179 | | |
180 | 294 | double duration() const { |
181 | 294 | return duration_cast<std::chrono::milliseconds>( |
182 | 294 | std::chrono::system_clock::now().time_since_epoch()) |
183 | 294 | .count() - |
184 | 294 | start_time; |
185 | 294 | } |
186 | | |
187 | 20 | void reset() { |
188 | 20 | total_need_recycle_data_size = 0; |
189 | 20 | total_need_recycle_num = 0; |
190 | 20 | total_recycled_data_size = 0; |
191 | 20 | total_recycled_num = 0; |
192 | 20 | start_time = duration_cast<std::chrono::milliseconds>( |
193 | 20 | std::chrono::system_clock::now().time_since_epoch()) |
194 | 20 | .count(); |
195 | 20 | } |
196 | | |
197 | 294 | void finish_report() { |
198 | 294 | if (!operation_type.empty()) { |
199 | 294 | double cost = duration(); |
200 | 294 | g_bvar_recycler_instance_last_round_recycle_elpased_ts.put( |
201 | 294 | {instance_id, operation_type}, cost); |
202 | 294 | g_bvar_recycler_instance_recycle_round.put({instance_id, operation_type}, 1); |
203 | 294 | g_bvar_recycler_instance_recycle_total_bytes_since_started.put( |
204 | 294 | {instance_id, operation_type}, total_recycled_data_size.load()); |
205 | 294 | g_bvar_recycler_instance_recycle_total_num_since_started.put( |
206 | 294 | {instance_id, operation_type}, total_recycled_num.load()); |
207 | 294 | LOG(INFO) << "recycle instance: " << instance_id |
208 | 294 | << ", operation type: " << operation_type << ", cost: " << cost |
209 | 294 | << " ms, total recycled num: " << total_recycled_num.load() |
210 | 294 | << ", total recycled data size: " << total_recycled_data_size.load() |
211 | 294 | << " bytes"; |
212 | 294 | if (cost != 0) { |
213 | 273 | if (total_recycled_num.load() != 0) { |
214 | 72 | g_bvar_recycler_instance_recycle_time_per_resource.put( |
215 | 72 | {instance_id, operation_type}, cost / total_recycled_num.load()); |
216 | 72 | } |
217 | 273 | g_bvar_recycler_instance_recycle_bytes_per_ms.put( |
218 | 273 | {instance_id, operation_type}, total_recycled_data_size.load() / cost); |
219 | 273 | } |
220 | 294 | } |
221 | 294 | } |
222 | | |
223 | | // `is_begin` is used to initialize total num of items need to be recycled |
224 | 1.16k | void report(bool is_begin = false) { |
225 | 1.16k | if (!operation_type.empty()) { |
226 | | // is init |
227 | 1.13k | if (is_begin) { |
228 | 3 | auto value = total_need_recycle_num.load(); |
229 | | |
230 | 3 | g_bvar_recycler_instance_last_round_to_recycle_bytes.put( |
231 | 3 | {instance_id, operation_type}, total_need_recycle_data_size.load()); |
232 | 3 | g_bvar_recycler_instance_last_round_to_recycle_num.put( |
233 | 3 | {instance_id, operation_type}, value); |
234 | 1.13k | } else { |
235 | 1.13k | g_bvar_recycler_instance_last_round_recycled_bytes.put( |
236 | 1.13k | {instance_id, operation_type}, total_recycled_data_size.load()); |
237 | 1.13k | g_bvar_recycler_instance_last_round_recycled_num.put({instance_id, operation_type}, |
238 | 1.13k | total_recycled_num.load()); |
239 | 1.13k | } |
240 | 1.13k | } |
241 | 1.16k | } |
242 | | }; |
243 | | |
244 | | class TabletRecyclerMetricsContext : public RecyclerMetricsContext { |
245 | | public: |
246 | 141 | TabletRecyclerMetricsContext() : RecyclerMetricsContext("global_recycler", "recycle_tablet") {} |
247 | | }; |
248 | | |
249 | | class SegmentRecyclerMetricsContext : public RecyclerMetricsContext { |
250 | | public: |
251 | | SegmentRecyclerMetricsContext() |
252 | 141 | : RecyclerMetricsContext("global_recycler", "recycle_segment") {} |
253 | | }; |
254 | | |
255 | | struct OplogRecycleStats; |
256 | | |
257 | | class InstanceRecycler { |
258 | | public: |
259 | | struct PackedFileRecycleStats { |
260 | | int64_t num_scanned = 0; // packed-file kv scanned |
261 | | int64_t num_corrected = 0; // packed-file kv corrected |
262 | | int64_t num_deleted = 0; // packed-file kv deleted |
263 | | int64_t num_failed = 0; // packed-file kv failed |
264 | | int64_t bytes_deleted = 0; // packed-file kv bytes deleted from txn-kv |
265 | | int64_t num_object_deleted = 0; // packed-file objects deleted from storage (vault/HDFS) |
266 | | int64_t bytes_object_deleted = 0; // bytes deleted from storage objects |
267 | | int64_t rowset_scan_count = 0; // rowset metas scanned during correction |
268 | | }; |
269 | | |
270 | | explicit InstanceRecycler(std::shared_ptr<TxnKv> txn_kv, const InstanceInfoPB& instance, |
271 | | RecyclerThreadPoolGroup thread_pool_group, |
272 | | std::shared_ptr<TxnLazyCommitter> txn_lazy_committer); |
273 | | ~InstanceRecycler(); |
274 | | |
275 | 0 | std::string_view instance_id() const { return instance_id_; } |
276 | 5 | const InstanceInfoPB& instance_info() const { return instance_info_; } |
277 | | |
278 | | // returns 0 for success otherwise error |
279 | | int init(); |
280 | | |
281 | 0 | void stop() { stopped_.store(true, std::memory_order_release); } |
282 | 79 | bool stopped() const { return stopped_.load(std::memory_order_acquire); } |
283 | | |
284 | | // returns 0 for success otherwise error |
285 | | int do_recycle(); |
286 | | |
287 | | // remove all kv and data in this instance, ONLY be called when instance has been deleted |
288 | | // returns 0 for success otherwise error |
289 | | int recycle_deleted_instance(); |
290 | | |
291 | | // scan and recycle expired indexes: |
292 | | // 1. dropped table, dropped mv |
293 | | // 2. half-successtable/index when create |
294 | | // returns 0 for success otherwise error |
295 | | int recycle_indexes(); |
296 | | |
297 | | // scan and recycle expired partitions: |
298 | | // 1. dropped parttion |
299 | | // 2. half-success partition when create |
300 | | // returns 0 for success otherwise error |
301 | | int recycle_partitions(); |
302 | | |
303 | | // scan and recycle expired rowsets: |
304 | | // 1. prepare_rowset will produce recycle_rowset before uploading data to remote storage (memo) |
305 | | // 2. compaction will change the input rowsets to recycle_rowset |
306 | | // returns 0 for success otherwise error |
307 | | int recycle_rowsets(); |
308 | | |
309 | | // like `recycle_rowsets`, but for versioned rowsets. |
310 | | int recycle_versioned_rowsets(); |
311 | | |
312 | | // scan and recycle expired tmp rowsets: |
313 | | // 1. commit_rowset will produce tmp_rowset when finish upload data (load or compaction) to remote storage |
314 | | // returns 0 for success otherwise error |
315 | | int recycle_tmp_rowsets(); |
316 | | |
317 | | /** |
318 | | * recycle all tablets belonging to the index specified by `index_id` |
319 | | * |
320 | | * @param partition_id if positive, only recycle tablets in this partition belonging to the specified index |
321 | | * @return 0 for success otherwise error |
322 | | */ |
323 | | int recycle_tablets(int64_t table_id, int64_t index_id, RecyclerMetricsContext& ctx, |
324 | | int64_t partition_id = -1); |
325 | | |
326 | | /** |
327 | | * recycle all rowsets belonging to the tablet specified by `tablet_id` |
328 | | * |
329 | | * @return 0 for success otherwise error |
330 | | */ |
331 | | int recycle_tablet(int64_t tablet_id, RecyclerMetricsContext& metrics_context); |
332 | | |
333 | | /** |
334 | | * like `recycle_tablet`, but for versioned tablet |
335 | | */ |
336 | | int recycle_versioned_tablet(int64_t tablet_id, RecyclerMetricsContext& metrics_context); |
337 | | |
338 | | // scan and recycle useless partition version kv |
339 | | int recycle_versions(); |
340 | | |
341 | | // scan and recycle the orphan partitions |
342 | | int recycle_orphan_partitions(); |
343 | | |
344 | | // scan and abort timeout txn label |
345 | | // returns 0 for success otherwise error |
346 | | int abort_timeout_txn(); |
347 | | |
348 | | //scan and recycle expire txn label |
349 | | // returns 0 for success otherwise error |
350 | | int recycle_expired_txn_label(); |
351 | | |
352 | | // scan and recycle finished or timeout copy jobs |
353 | | // returns 0 for success otherwise error |
354 | | int recycle_copy_jobs(); |
355 | | |
356 | | // scan and recycle dropped internal stage |
357 | | // returns 0 for success otherwise error |
358 | | int recycle_stage(); |
359 | | |
360 | | // scan and recycle expired stage objects |
361 | | // returns 0 for success otherwise error |
362 | | int recycle_expired_stage_objects(); |
363 | | |
364 | | // scan and recycle operation logs |
365 | | // returns 0 for success otherwise error |
366 | | int recycle_operation_logs(); |
367 | | |
368 | | // scan and recycle expired restore jobs |
369 | | // returns 0 for success otherwise error |
370 | | int recycle_restore_jobs(); |
371 | | |
372 | | /** |
373 | | * Scan packed-file metadata, correct reference counters, and recycle unused packed files. |
374 | | * |
375 | | * @return 0 on success, non-zero error code otherwise |
376 | | */ |
377 | | int recycle_packed_files(); |
378 | | |
379 | | // scan and recycle snapshots |
380 | | // returns 0 for success otherwise error |
381 | | int recycle_cluster_snapshots(); |
382 | | |
383 | | // scan and recycle ref rowsets for deleted instance |
384 | | // returns 0 for success otherwise error |
385 | | int recycle_ref_rowsets(bool* has_unrecycled_rowsets); |
386 | | |
387 | | bool check_recycle_tasks(); |
388 | | |
389 | | int scan_and_statistics_indexes(); |
390 | | |
391 | | int scan_and_statistics_partitions(); |
392 | | |
393 | | int scan_and_statistics_rowsets(); |
394 | | |
395 | | int scan_and_statistics_tmp_rowsets(); |
396 | | |
397 | | int scan_and_statistics_abort_timeout_txn(); |
398 | | |
399 | | int scan_and_statistics_expired_txn_label(); |
400 | | |
401 | | int scan_and_statistics_copy_jobs(); |
402 | | |
403 | | int scan_and_statistics_stage(); |
404 | | |
405 | | int scan_and_statistics_expired_stage_objects(); |
406 | | |
407 | | int scan_and_statistics_versions(); |
408 | | |
409 | | int scan_and_statistics_restore_jobs(); |
410 | | |
411 | | void scan_and_statistics_operation_logs(); |
412 | | |
413 | | /** |
414 | | * Decode the key of a packed-file metadata record into the persisted object path. |
415 | | * |
416 | | * @param key raw key persisted in txn-kv |
417 | | * @param packed_path output object storage path referenced by the key |
418 | | * @return true if decoding succeeds, false otherwise |
419 | | */ |
420 | | static bool decode_packed_file_key(std::string_view key, std::string* packed_path); |
421 | | |
422 | 29 | void TEST_add_accessor(std::string_view id, std::shared_ptr<StorageVaultAccessor> accessor) { |
423 | 29 | accessor_map_.insert({std::string(id), std::move(accessor)}); |
424 | 29 | } |
425 | | |
426 | | // Recycle snapshot meta and data, return 0 for success otherwise error. |
427 | | int recycle_snapshot_meta_and_data(const std::string& instance_id, |
428 | | const std::string& resource_id, |
429 | | Versionstamp snapshot_version, |
430 | | const SnapshotPB& snapshot_pb); |
431 | | |
432 | | private: |
433 | | // returns 0 for success otherwise error |
434 | | int init_obj_store_accessors(); |
435 | | |
436 | | // returns 0 for success otherwise error |
437 | | int init_storage_vault_accessors(); |
438 | | |
439 | | /** |
440 | | * Scan key-value pairs between [`begin`, `end`) with multiple rounds of range get(`RangeGetIterator`), |
441 | | * and perform `recycle_func` on each key-value pair. |
442 | | * |
443 | | * @param recycle_func defines how to recycle resources corresponding to a key-value pair. |
444 | | * The scan will stop if recycle_func() returns non-zero. |
445 | | * recycle_func() returns 0 if the recycling is successful or the scan can continue with ignorable errors. |
446 | | * @param loop_done is called after a round (`RangeGetIterator`) in the scan has no next kv. Usually used to perform a batch recycling. |
447 | | * The scan will stop if loop_done() returns non-zero. |
448 | | * loop_done() returns 0 if the recycling is successful or the scan can continue with ignorable errors. |
449 | | * @return 0 if all corresponding resources are recycled successfully, otherwise non-zero |
450 | | */ |
451 | | int scan_and_recycle(std::string begin, std::string_view end, |
452 | | std::function<int(std::string_view k, std::string_view v)> recycle_func, |
453 | | std::function<int()> loop_done = nullptr, |
454 | | std::function<bool(std::string*)> next_begin_getter = nullptr); |
455 | | |
456 | | static int next_recycle_rowset_tablet_key(const std::string& instance_id, int64_t tablet_id, |
457 | | std::string* next_key); |
458 | | |
459 | | int scan_recycle_rowsets_by_tablet( |
460 | | std::string begin, std::string_view end, |
461 | | std::function<int(std::string_view k, std::string_view v)> recycle_func, |
462 | | std::function<int()> loop_done = nullptr); |
463 | | |
464 | | // return 0 for success otherwise error |
465 | | int delete_rowset_data(const doris::RowsetMetaCloudPB& rs_meta_pb); |
466 | | |
467 | | // return 0 for success otherwise error |
468 | | // NOTE: this function ONLY be called when the file paths cannot be calculated |
469 | | int delete_rowset_data(const std::string& resource_id, int64_t tablet_id, |
470 | | const std::string& rowset_id); |
471 | | |
472 | | // return 0 for success otherwise error |
473 | | int delete_rowset_data(const std::map<std::string, doris::RowsetMetaCloudPB>& rowsets, |
474 | | RowsetRecyclingState type, RecyclerMetricsContext& metrics_context); |
475 | | |
476 | | // Decrement packed file ref counts for rowset segments. |
477 | | // Returns 0 for success, -1 for error. |
478 | | int decrement_packed_file_ref_counts(const doris::RowsetMetaCloudPB& rs_meta_pb); |
479 | | |
480 | | enum class DeleteBitmapStorageType { |
481 | | NOT_FOUND, |
482 | | IN_FDB, |
483 | | STANDALONE_FILE, |
484 | | PACKED_FILE, |
485 | | }; |
486 | | |
487 | | // Process delete bitmap storage and decrement packed file ref count when needed. |
488 | | // Returns 0 for success, -1 for error. |
489 | | // out_storage_type: if not null, will be set to the delete bitmap storage type. |
490 | | int decrement_delete_bitmap_packed_file_ref_counts(int64_t tablet_id, |
491 | | const std::string& rowset_id, |
492 | | DeleteBitmapStorageType* out_storage_type); |
493 | | |
494 | | int delete_packed_file_and_kv(const std::string& packed_file_path, |
495 | | const std::string& packed_key, |
496 | | const cloud::PackedFileInfoPB& packed_info); |
497 | | |
498 | | /** |
499 | | * Get stage storage info from instance and init StorageVaultAccessor |
500 | | * @return 0 if accessor is successfully inited, 1 if stage not found, negative for error |
501 | | */ |
502 | | int init_copy_job_accessor(const std::string& stage_id, const StagePB::StageType& stage_type, |
503 | | std::shared_ptr<StorageVaultAccessor>* accessor); |
504 | | |
505 | | void register_recycle_task(const std::string& task_name, int64_t start_time); |
506 | | |
507 | | void unregister_recycle_task(const std::string& task_name); |
508 | | |
509 | | // for scan all tablets and statistics metrics |
510 | | int scan_tablets_and_statistics(int64_t tablet_id, int64_t index_id, |
511 | | RecyclerMetricsContext& metrics_context, |
512 | | int64_t partition_id = -1, bool is_empty_tablet = false); |
513 | | |
514 | | // for scan all rs of tablet and statistics metrics |
515 | | int scan_tablet_and_statistics(int64_t tablet_id, RecyclerMetricsContext& metrics_context); |
516 | | |
517 | | // Recycle operation log and the log keys. The log keys are specified by `raw_keys`. |
518 | | // |
519 | | // Both `operation_log` and `raw_keys` will be removed in the same transaction, to ensure atomicity. |
520 | | int recycle_operation_log(Versionstamp log_version, const std::vector<std::string>& raw_keys, |
521 | | OperationLogPB operation_log, |
522 | | OplogRecycleStats* oplog_stats = nullptr); |
523 | | |
524 | | // Recycle rowset meta and data, return 0 for success otherwise error |
525 | | // |
526 | | // This function will decrease the rowset ref count and remove the rowset meta and data if the ref count is 1. |
527 | | int recycle_rowset_meta_and_data(const RowsetDeleteTask& task); |
528 | | |
529 | | // Classify rowset task by ref_count, return 0 to add to batch delete, 1 if handled (ref>1), -1 on error |
530 | | int classify_rowset_task_by_ref_count(RowsetDeleteTask& task, |
531 | | std::vector<RowsetDeleteTask>& batch_delete_tasks); |
532 | | |
533 | | // Cleanup metadata for deleted rowsets, return 0 for success otherwise error |
534 | | int cleanup_rowset_metadata(const std::vector<RowsetDeleteTask>& tasks); |
535 | | |
536 | | // Whether the instance has any snapshots, return 0 for success otherwise error. |
537 | | int has_cluster_snapshots(bool* any); |
538 | | |
539 | | // Whether need to recycle versioned keys |
540 | | bool should_recycle_versioned_keys() const; |
541 | | |
542 | | /** |
543 | | * Parse the path of a packed-file fragment and output the owning tablet and rowset identifiers. |
544 | | * |
545 | | * @param path packed-file fragment path to decode |
546 | | * @param tablet_id output tablet identifier extracted from the path |
547 | | * @param rowset_id output rowset identifier extracted from the path |
548 | | * @return true if both identifiers are successfully parsed, false otherwise |
549 | | */ |
550 | | static bool parse_packed_slice_path(std::string_view path, int64_t* tablet_id, |
551 | | std::string* rowset_id); |
552 | | // Check whether a rowset referenced by a packed file still exists in metadata. |
553 | | // @param stats optional recycle statistics collector. |
554 | | int check_rowset_exists(int64_t tablet_id, const std::string& rowset_id, bool* exists, |
555 | | PackedFileRecycleStats* stats = nullptr); |
556 | | int check_recycle_and_tmp_rowset_exists(int64_t tablet_id, const std::string& rowset_id, |
557 | | int64_t txn_id, bool* recycle_exists, bool* tmp_exists); |
558 | | /** |
559 | | * Resolve which storage accessor should be used for a packed file. |
560 | | * |
561 | | * @param hint preferred storage resource identifier persisted with the file |
562 | | * @return pair of the resolved resource identifier and accessor; the accessor can be null if unavailable |
563 | | */ |
564 | | std::pair<std::string, std::shared_ptr<StorageVaultAccessor>> resolve_packed_file_accessor( |
565 | | const std::string& hint); |
566 | | // Recompute packed-file counters and lifecycle state after validating contained fragments. |
567 | | // @param stats optional recycle statistics collector. |
568 | | int correct_packed_file_info(cloud::PackedFileInfoPB* packed_info, bool* changed, |
569 | | const std::string& packed_file_path, |
570 | | PackedFileRecycleStats* stats = nullptr); |
571 | | // Correct and recycle a single packed-file record, updating metadata and accounting statistics. |
572 | | // @param stats optional recycle statistics collector. |
573 | | int process_single_packed_file(const std::string& packed_key, |
574 | | const std::string& packed_file_path, |
575 | | PackedFileRecycleStats* stats); |
576 | | // Process a packed-file KV while scanning and aggregate recycling statistics. |
577 | | int handle_packed_file_kv(std::string_view key, std::string_view value, |
578 | | PackedFileRecycleStats* stats, int* ret); |
579 | | |
580 | | // Abort the transaction/job associated with a rowset that is about to be recycled. |
581 | | // This function is called during rowset recycling to prevent data loss by ensuring that |
582 | | // the transaction/job cannot be committed after its rowset data has been deleted. |
583 | | // |
584 | | // Scenario: |
585 | | // When recycler detects an expired prepared rowset (e.g., from a failed load transaction/job), |
586 | | // it needs to recycle the rowset data. However, if the transaction/job is still active and gets |
587 | | // committed after the data is deleted, it would lead to data loss - the transaction/job would |
588 | | // reference non-existent data. |
589 | | // |
590 | | // Solution: |
591 | | // Before recycling the rowset data, this function aborts the associated transaction/job to ensure |
592 | | // it cannot be committed. This guarantees that: |
593 | | // 1. The transaction/job state is marked as ABORTED |
594 | | // 2. Any subsequent commit_rowset/commit_txn attempts will fail |
595 | | // 3. The rowset data can be safely deleted without risk of data loss |
596 | | // |
597 | | // Parameters: |
598 | | // txn_id: The transaction/job ID associated with the rowset to be recycled |
599 | | // |
600 | | // Returns: |
601 | | // 0 on success, -1 on failure |
602 | | int abort_txn_for_related_rowset(int64_t txn_id); |
603 | | int abort_job_for_related_rowset(const RowsetMetaCloudPB& rowset_meta); |
604 | | |
605 | | template <typename T> |
606 | | int batch_abort_txn_or_job_for_recycle(const std::vector<std::string>& keys, |
607 | | bool skip_base_version); |
608 | | |
609 | | private: |
610 | | std::atomic_bool stopped_ {false}; |
611 | | std::shared_ptr<TxnKv> txn_kv_; |
612 | | std::string instance_id_; |
613 | | InstanceInfoPB instance_info_; |
614 | | |
615 | | // TODO(plat1ko): Add new accessor to map in runtime for new created storage vaults |
616 | | std::unordered_map<std::string, std::shared_ptr<StorageVaultAccessor>> accessor_map_; |
617 | | using InvertedIndexInfo = |
618 | | std::pair<InvertedIndexStorageFormatPB, std::vector<std::pair<int64_t, std::string>>>; |
619 | | |
620 | | class InvertedIndexIdCache; |
621 | | std::unique_ptr<InvertedIndexIdCache> inverted_index_id_cache_; |
622 | | |
623 | | std::mutex recycled_tablets_mtx_; |
624 | | // Store recycled tablets, we can skip deleting rowset data of these tablets because these data has already been deleted. |
625 | | std::unordered_set<int64_t> recycled_tablets_; |
626 | | |
627 | | std::mutex recycle_tasks_mutex; |
628 | | // <task_name, start_time>> |
629 | | std::map<std::string, int64_t> running_recycle_tasks; |
630 | | |
631 | | RecyclerThreadPoolGroup _thread_pool_group; |
632 | | |
633 | | std::shared_ptr<TxnLazyCommitter> txn_lazy_committer_; |
634 | | std::shared_ptr<SnapshotManager> snapshot_manager_; |
635 | | std::shared_ptr<DeleteBitmapLockWhiteList> delete_bitmap_lock_white_list_; |
636 | | std::shared_ptr<ResourceManager> resource_mgr_; |
637 | | |
638 | | TabletRecyclerMetricsContext tablet_metrics_context_; |
639 | | SegmentRecyclerMetricsContext segment_metrics_context_; |
640 | | }; |
641 | | |
642 | | struct OperationLogReferenceInfo { |
643 | | bool referenced_by_instance = false; |
644 | | bool referenced_by_snapshot = false; |
645 | | Versionstamp referenced_snapshot_timestamp; |
646 | | }; |
647 | | |
648 | | struct OplogRecycleStats { |
649 | | // Total oplog count scanned per round |
650 | | std::atomic<int64_t> total_num {0}; |
651 | | // Oplogs not recycled this round (per round, written to mBvarStatus) |
652 | | std::atomic<int64_t> not_recycled_num {0}; |
653 | | // Recycle failures (per round, accumulated to mBvarIntAdder at end) |
654 | | std::atomic<int64_t> failed_num {0}; |
655 | | // Per-oplog-type recycled counts (incremented after successful commit) |
656 | | std::atomic<int64_t> recycled_commit_partition {0}; |
657 | | std::atomic<int64_t> recycled_drop_partition {0}; |
658 | | std::atomic<int64_t> recycled_commit_index {0}; |
659 | | std::atomic<int64_t> recycled_drop_index {0}; |
660 | | std::atomic<int64_t> recycled_update_tablet {0}; |
661 | | std::atomic<int64_t> recycled_compaction {0}; |
662 | | std::atomic<int64_t> recycled_schema_change {0}; |
663 | | std::atomic<int64_t> recycled_commit_txn {0}; |
664 | | }; |
665 | | |
666 | | // Helper class to check if operation logs can be recycled based on snapshots and versionstamps |
667 | | class OperationLogRecycleChecker { |
668 | | public: |
669 | | OperationLogRecycleChecker(std::string_view instance_id, TxnKv* txn_kv, |
670 | | const InstanceInfoPB& instance_info) |
671 | 35 | : instance_id_(instance_id), txn_kv_(txn_kv), instance_info_(instance_info) {} |
672 | | |
673 | | // Initialize the checker by loading snapshots and setting max version stamp |
674 | | int init(); |
675 | | |
676 | | // Check if an operation log can be recycled |
677 | | bool can_recycle(const Versionstamp& log_versionstamp, int64_t log_min_timestamp, |
678 | | OperationLogReferenceInfo* reference_info) const; |
679 | | |
680 | 0 | Versionstamp max_versionstamp() const { return max_versionstamp_; } |
681 | | |
682 | 28 | const std::vector<std::pair<SnapshotPB, Versionstamp>>& get_snapshots() const { |
683 | 28 | return snapshots_; |
684 | 28 | } |
685 | | |
686 | | private: |
687 | | std::string_view instance_id_; |
688 | | TxnKv* txn_kv_; |
689 | | const InstanceInfoPB& instance_info_; |
690 | | Versionstamp max_versionstamp_; |
691 | | Versionstamp source_snapshot_versionstamp_; |
692 | | std::map<Versionstamp, size_t> snapshot_indexes_; |
693 | | std::vector<std::pair<SnapshotPB, Versionstamp>> snapshots_; |
694 | | }; |
695 | | |
696 | | class SnapshotDataSizeCalculator { |
697 | | public: |
698 | | SnapshotDataSizeCalculator(std::string_view instance_id, std::shared_ptr<TxnKv> txn_kv) |
699 | 29 | : instance_id_(instance_id), txn_kv_(std::move(txn_kv)) {} |
700 | | |
701 | | void init(const std::vector<std::pair<SnapshotPB, Versionstamp>>& snapshots); |
702 | | |
703 | | int calculate_operation_log_data_size(const std::string_view& log_key, |
704 | | OperationLogPB& operation_log, |
705 | | OperationLogReferenceInfo& reference_info); |
706 | | |
707 | | int save_snapshot_data_size_with_retry(); |
708 | | |
709 | | private: |
710 | | int get_all_index_partitions(int64_t db_id, int64_t table_id, int64_t index_id, |
711 | | std::vector<int64_t>* partition_ids); |
712 | | int get_index_partition_data_size(int64_t db_id, int64_t table_id, int64_t index_id, |
713 | | int64_t partition_id, int64_t* data_size); |
714 | | int save_operation_log(const std::string_view& log_key, OperationLogPB& operation_log); |
715 | | int save_snapshot_data_size(); |
716 | | |
717 | | std::string_view instance_id_; |
718 | | std::shared_ptr<TxnKv> txn_kv_; |
719 | | |
720 | | int64_t instance_retained_data_size_ = 0; |
721 | | std::map<Versionstamp, int64_t> retained_data_size_; |
722 | | std::set<std::string> calculated_partitions_; |
723 | | }; |
724 | | |
725 | | } // namespace doris::cloud |