Coverage Report

Created: 2025-09-23 19:04

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