Coverage Report

Created: 2025-11-20 15:16

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/root/doris/be/src/cloud/cloud_tablet.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 <memory>
21
22
#include "olap/base_tablet.h"
23
#include "olap/partial_update_info.h"
24
#include "olap/rowset/rowset.h"
25
26
namespace doris {
27
28
class CloudStorageEngine;
29
30
enum class WarmUpTriggerSource : int { NONE, SYNC_ROWSET, EVENT_DRIVEN, JOB };
31
32
enum class WarmUpProgress : int { NONE, DOING, DONE };
33
34
struct WarmUpState {
35
    WarmUpTriggerSource trigger_source {WarmUpTriggerSource::NONE};
36
    WarmUpProgress progress {WarmUpProgress::NONE};
37
38
55
    bool operator==(const WarmUpState& other) const {
39
55
        return trigger_source == other.trigger_source && progress == other.progress;
40
55
    }
41
};
42
43
struct SyncRowsetStats {
44
    int64_t get_remote_rowsets_num {0};
45
    int64_t get_remote_rowsets_rpc_ns {0};
46
47
    int64_t get_local_delete_bitmap_rowsets_num {0};
48
    int64_t get_remote_delete_bitmap_rowsets_num {0};
49
    int64_t get_remote_delete_bitmap_key_count {0};
50
    int64_t get_remote_delete_bitmap_bytes {0};
51
    int64_t get_remote_delete_bitmap_rpc_ns {0};
52
53
    int64_t get_remote_tablet_meta_rpc_ns {0};
54
    int64_t tablet_meta_cache_hit {0};
55
    int64_t tablet_meta_cache_miss {0};
56
};
57
58
struct SyncOptions {
59
    bool warmup_delta_data = false;
60
    bool sync_delete_bitmap = true;
61
    bool full_sync = false;
62
    bool merge_schema = false;
63
    int64_t query_version = -1;
64
};
65
66
struct RecycledRowsets {
67
    RowsetId rowset_id;
68
    int64_t num_segments;
69
    std::vector<std::string> index_file_names;
70
};
71
72
class CloudTablet final : public BaseTablet {
73
public:
74
    CloudTablet(CloudStorageEngine& engine, TabletMetaSharedPtr tablet_meta);
75
76
    ~CloudTablet() override;
77
78
    bool exceed_version_limit(int32_t limit) override;
79
80
    Result<std::unique_ptr<RowsetWriter>> create_rowset_writer(RowsetWriterContext& context,
81
                                                               bool vertical) override;
82
83
    Status capture_rs_readers(const Version& spec_version, std::vector<RowSetSplits>* rs_splits,
84
                              const CaptureRowsetOps& opts) override;
85
86
    [[nodiscard]] Result<std::vector<Version>> capture_consistent_versions_unlocked(
87
            const Version& version_range, const CaptureRowsetOps& options) const override;
88
89
    // Capture versions with cache preference optimization.
90
    // This method prioritizes using cached/warmed-up rowsets when building version paths,
91
    // avoiding cold data reads when possible. It uses capture_consistent_versions_prefer_cache
92
    // to find a consistent version path that prefers already warmed-up rowsets.
93
    Result<std::vector<Version>> capture_versions_prefer_cache(const Version& spec_version) const;
94
95
    // Capture versions with query freshness tolerance.
96
    // This method finds a consistent version path where all rowsets are warmed up,
97
    // but allows fallback to normal capture if there are newer rowsets that should be
98
    // visible (based on freshness tolerance) but haven't been warmed up yet.
99
    // For merge-on-write tables, uses special validation to ensure data correctness.
100
    //
101
    // IMPORTANT: The returned version may be smaller than the requested version if newer
102
    // data hasn't been warmed up yet. This can cause different tablets in the same query
103
    // to read from different versions, potentially leading to inconsistent query results.
104
    //
105
    // @param options.query_freshness_tolerance_ms: Time tolerance in milliseconds. Rowsets that
106
    //        became visible within this time range (after current_time - query_freshness_tolerance_ms)
107
    //        can be skipped if not warmed up. However, if older rowsets (before this time point)
108
    //        are not warmed up, the method will fallback to normal capture.
109
    Result<std::vector<Version>> capture_versions_with_freshness_tolerance(
110
            const Version& spec_version, const CaptureRowsetOps& options) const;
111
112
0
    size_t tablet_footprint() override {
113
0
        return _approximate_data_size.load(std::memory_order_relaxed);
114
0
    }
115
116
    std::string tablet_path() const override;
117
118
    // clang-format off
119
2
    int64_t fetch_add_approximate_num_rowsets (int64_t x) { return _approximate_num_rowsets .fetch_add(x, std::memory_order_relaxed); }
120
0
    int64_t fetch_add_approximate_num_segments(int64_t x) { return _approximate_num_segments.fetch_add(x, std::memory_order_relaxed); }
121
0
    int64_t fetch_add_approximate_num_rows    (int64_t x) { return _approximate_num_rows    .fetch_add(x, std::memory_order_relaxed); }
122
0
    int64_t fetch_add_approximate_data_size   (int64_t x) { return _approximate_data_size   .fetch_add(x, std::memory_order_relaxed); }
123
2
    int64_t fetch_add_approximate_cumu_num_rowsets (int64_t x) { return _approximate_cumu_num_rowsets.fetch_add(x, std::memory_order_relaxed); }
124
0
    int64_t fetch_add_approximate_cumu_num_deltas   (int64_t x) { return _approximate_cumu_num_deltas.fetch_add(x, std::memory_order_relaxed); }
125
    // clang-format on
126
127
    // meta lock must be held when calling this function
128
    void reset_approximate_stats(int64_t num_rowsets, int64_t num_segments, int64_t num_rows,
129
                                 int64_t data_size);
130
131
    // return a json string to show the compaction status of this tablet
132
    void get_compaction_status(std::string* json_result);
133
134
    // Synchronize the rowsets from meta service.
135
    // If tablet state is not `TABLET_RUNNING`, sync tablet meta and all visible rowsets.
136
    // If `query_version` > 0 and local max_version of the tablet >= `query_version`, do nothing.
137
    // If 'need_download_data_async' is true, it means that we need to download the new version
138
    // rowsets datum async.
139
    Status sync_rowsets(const SyncOptions& options = {}, SyncRowsetStats* stats = nullptr);
140
141
    // Synchronize the tablet meta from meta service.
142
    Status sync_meta();
143
144
    // If `version_overlap` is true, function will delete rowsets with overlapped version in this tablet.
145
    // If 'warmup_delta_data' is true, download the new version rowset data in background.
146
    // MUST hold EXCLUSIVE `_meta_lock`.
147
    // If 'need_download_data_async' is true, it means that we need to download the new version
148
    // rowsets datum async.
149
    void add_rowsets(std::vector<RowsetSharedPtr> to_add, bool version_overlap,
150
                     std::unique_lock<std::shared_mutex>& meta_lock,
151
                     bool warmup_delta_data = false);
152
153
    // MUST hold EXCLUSIVE `_meta_lock`.
154
    void delete_rowsets(const std::vector<RowsetSharedPtr>& to_delete,
155
                        std::unique_lock<std::shared_mutex>& meta_lock);
156
157
    // When the tablet is dropped, we need to recycle cached data:
158
    // 1. The data in file cache
159
    // 2. The memory in tablet cache
160
    void clear_cache() override;
161
162
    // Return number of deleted stale rowsets
163
    uint64_t delete_expired_stale_rowsets();
164
165
0
    bool has_stale_rowsets() const { return !_stale_rs_version_map.empty(); }
166
167
    int64_t get_cloud_base_compaction_score() const;
168
    int64_t get_cloud_cumu_compaction_score() const;
169
170
2
    int64_t max_version_unlocked() const override { return _max_version; }
171
3
    int64_t base_compaction_cnt() const { return _base_compaction_cnt; }
172
3
    int64_t cumulative_compaction_cnt() const { return _cumulative_compaction_cnt; }
173
0
    int64_t full_compaction_cnt() const { return _full_compaction_cnt; }
174
6
    int64_t cumulative_layer_point() const {
175
6
        return _cumulative_point.load(std::memory_order_relaxed);
176
6
    }
177
178
0
    void set_base_compaction_cnt(int64_t cnt) { _base_compaction_cnt = cnt; }
179
0
    void set_cumulative_compaction_cnt(int64_t cnt) { _cumulative_compaction_cnt = cnt; }
180
0
    void set_full_compaction_cnt(int64_t cnt) { _full_compaction_cnt = cnt; }
181
    void set_cumulative_layer_point(int64_t new_point);
182
183
1
    int64_t last_cumu_compaction_failure_time() { return _last_cumu_compaction_failure_millis; }
184
3
    void set_last_cumu_compaction_failure_time(int64_t millis) {
185
3
        _last_cumu_compaction_failure_millis = millis;
186
3
    }
187
188
2
    int64_t last_base_compaction_failure_time() { return _last_base_compaction_failure_millis; }
189
3
    void set_last_base_compaction_failure_time(int64_t millis) {
190
3
        _last_base_compaction_failure_millis = millis;
191
3
    }
192
193
0
    int64_t last_full_compaction_failure_time() { return _last_full_compaction_failure_millis; }
194
0
    void set_last_full_compaction_failure_time(int64_t millis) {
195
0
        _last_full_compaction_failure_millis = millis;
196
0
    }
197
198
0
    int64_t last_cumu_compaction_success_time() { return _last_cumu_compaction_success_millis; }
199
0
    void set_last_cumu_compaction_success_time(int64_t millis) {
200
0
        _last_cumu_compaction_success_millis = millis;
201
0
    }
202
203
0
    int64_t last_base_compaction_success_time() { return _last_base_compaction_success_millis; }
204
0
    void set_last_base_compaction_success_time(int64_t millis) {
205
0
        _last_base_compaction_success_millis = millis;
206
0
    }
207
208
0
    int64_t last_full_compaction_success_time() { return _last_full_compaction_success_millis; }
209
0
    void set_last_full_compaction_success_time(int64_t millis) {
210
0
        _last_full_compaction_success_millis = millis;
211
0
    }
212
213
0
    int64_t last_cumu_compaction_schedule_time() { return _last_cumu_compaction_schedule_millis; }
214
0
    void set_last_cumu_compaction_schedule_time(int64_t millis) {
215
0
        _last_cumu_compaction_schedule_millis = millis;
216
0
    }
217
218
0
    int64_t last_base_compaction_schedule_time() { return _last_base_compaction_schedule_millis; }
219
0
    void set_last_base_compaction_schedule_time(int64_t millis) {
220
0
        _last_base_compaction_schedule_millis = millis;
221
0
    }
222
223
0
    int64_t last_full_compaction_schedule_time() { return _last_full_compaction_schedule_millis; }
224
0
    void set_last_full_compaction_schedule_time(int64_t millis) {
225
0
        _last_full_compaction_schedule_millis = millis;
226
0
    }
227
228
0
    void set_last_cumu_compaction_status(std::string status) {
229
0
        _last_cumu_compaction_status = std::move(status);
230
0
    }
231
232
0
    std::string get_last_cumu_compaction_status() { return _last_cumu_compaction_status; }
233
234
0
    void set_last_base_compaction_status(std::string status) {
235
0
        _last_base_compaction_status = std::move(status);
236
0
    }
237
238
0
    std::string get_last_base_compaction_status() { return _last_base_compaction_status; }
239
240
0
    void set_last_full_compaction_status(std::string status) {
241
0
        _last_full_compaction_status = std::move(status);
242
0
    }
243
244
0
    std::string get_last_full_compaction_status() { return _last_full_compaction_status; }
245
246
0
    int64_t alter_version() const { return _alter_version; }
247
2
    void set_alter_version(int64_t alter_version) { _alter_version = alter_version; }
248
249
    std::vector<RowsetSharedPtr> pick_candidate_rowsets_to_base_compaction();
250
251
7
    inline Version max_version() const {
252
7
        std::shared_lock rdlock(_meta_lock);
253
7
        return _tablet_meta->max_version();
254
7
    }
255
256
7
    int64_t base_size() const { return _base_size; }
257
258
    std::vector<RowsetSharedPtr> pick_candidate_rowsets_to_full_compaction();
259
    Result<RowsetSharedPtr> pick_a_rowset_for_index_change(int schema_version,
260
                                                           bool& is_base_rowset);
261
    Status check_rowset_schema_for_build_index(std::vector<TColumn>& columns, int schema_version);
262
263
0
    std::mutex& get_base_compaction_lock() { return _base_compaction_lock; }
264
0
    std::mutex& get_cumulative_compaction_lock() { return _cumulative_compaction_lock; }
265
266
    Result<std::unique_ptr<RowsetWriter>> create_transient_rowset_writer(
267
            const Rowset& rowset, std::shared_ptr<PartialUpdateInfo> partial_update_info,
268
            int64_t txn_expiration = 0) override;
269
270
    CalcDeleteBitmapExecutor* calc_delete_bitmap_executor() override;
271
272
    Status save_delete_bitmap(const TabletTxnInfo* txn_info, int64_t txn_id,
273
                              DeleteBitmapPtr delete_bitmap, RowsetWriter* rowset_writer,
274
                              const RowsetIdUnorderedSet& cur_rowset_ids, int64_t lock_id = -1,
275
                              int64_t next_visible_version = -1) override;
276
277
    Status save_delete_bitmap_to_ms(int64_t cur_version, int64_t txn_id,
278
                                    DeleteBitmapPtr delete_bitmap, int64_t lock_id,
279
                                    int64_t next_visible_version, RowsetSharedPtr rowset);
280
281
    Status calc_delete_bitmap_for_compaction(const std::vector<RowsetSharedPtr>& input_rowsets,
282
                                             const RowsetSharedPtr& output_rowset,
283
                                             const RowIdConversion& rowid_conversion,
284
                                             ReaderType compaction_type, int64_t merged_rows,
285
                                             int64_t filtered_rows, int64_t initiator,
286
                                             DeleteBitmapPtr& output_rowset_delete_bitmap,
287
                                             bool allow_delete_in_cumu_compaction,
288
                                             int64_t& get_delete_bitmap_lock_start_time);
289
290
    // Find the missed versions until the spec_version.
291
    //
292
    // for example:
293
    //     [0-4][5-5][8-8][9-9][14-14]
294
    // if spec_version = 12, it will return [6-7],[10-12]
295
    Versions calc_missed_versions(int64_t spec_version, Versions existing_versions) const override;
296
297
0
    std::mutex& get_rowset_update_lock() { return _rowset_update_lock; }
298
299
0
    bthread::Mutex& get_sync_meta_lock() { return _sync_meta_lock; }
300
301
94
    const auto& rowset_map() const { return _rs_version_map; }
302
303
    int64_t last_sync_time_s = 0;
304
    int64_t last_load_time_ms = 0;
305
    int64_t last_base_compaction_success_time_ms = 0;
306
    int64_t last_cumu_compaction_success_time_ms = 0;
307
    int64_t last_cumu_no_suitable_version_ms = 0;
308
    int64_t last_access_time_ms = 0;
309
310
    std::atomic<int64_t> local_read_time_us = 0;
311
    std::atomic<int64_t> remote_read_time_us = 0;
312
    std::atomic<int64_t> exec_compaction_time_us = 0;
313
314
    void build_tablet_report_info(TTabletInfo* tablet_info);
315
316
    // check that if the delete bitmap in delete bitmap cache has the same cardinality with the expected_delete_bitmap's
317
    Status check_delete_bitmap_cache(int64_t txn_id, DeleteBitmap* expected_delete_bitmap) override;
318
319
    void agg_delete_bitmap_for_compaction(int64_t start_version, int64_t end_version,
320
                                          const std::vector<RowsetSharedPtr>& pre_rowsets,
321
                                          DeleteBitmapPtr& new_delete_bitmap,
322
                                          std::map<std::string, int64_t>& pre_rowset_to_versions);
323
324
    bool need_remove_unused_rowsets();
325
326
    void add_unused_rowsets(const std::vector<RowsetSharedPtr>& rowsets);
327
    void remove_unused_rowsets();
328
329
    // For each given rowset not in active use, clears its file cache and returns its
330
    // ID, segment count, and index file names as RecycledRowsets entries.
331
    static std::vector<RecycledRowsets> recycle_cached_data(
332
            const std::vector<RowsetSharedPtr>& rowsets);
333
334
    // Add warmup state management
335
    WarmUpState get_rowset_warmup_state(RowsetId rowset_id);
336
    bool add_rowset_warmup_state(
337
            const RowsetMeta& rowset, WarmUpTriggerSource source,
338
            std::chrono::steady_clock::time_point start_tp = std::chrono::steady_clock::now());
339
    bool update_rowset_warmup_state_inverted_idx_num(WarmUpTriggerSource source, RowsetId rowset_id,
340
                                                     int64_t delta);
341
    bool update_rowset_warmup_state_inverted_idx_num_unlocked(WarmUpTriggerSource source,
342
                                                              RowsetId rowset_id, int64_t delta);
343
    WarmUpState complete_rowset_segment_warmup(WarmUpTriggerSource trigger_source,
344
                                               RowsetId rowset_id, Status status,
345
                                               int64_t segment_num, int64_t inverted_idx_num);
346
347
    bool is_rowset_warmed_up(const RowsetId& rowset_id) const;
348
349
    void add_warmed_up_rowset(const RowsetId& rowset_id);
350
351
5
    std::string rowset_warmup_digest() const {
352
5
        std::string res;
353
104
        auto add_log = [&](const RowsetSharedPtr& rs) {
354
104
            auto tmp = fmt::format("{}{}", rs->rowset_id().to_string(), rs->version().to_string());
355
104
            if (_rowset_warm_up_states.contains(rs->rowset_id())) {
356
87
                tmp += fmt::format(
357
87
                        ", progress={}, segments_warmed_up={}/{}, inverted_idx_warmed_up={}/{}",
358
87
                        _rowset_warm_up_states.at(rs->rowset_id()).state.progress,
359
87
                        _rowset_warm_up_states.at(rs->rowset_id()).num_segments_warmed_up,
360
87
                        _rowset_warm_up_states.at(rs->rowset_id()).num_segments,
361
87
                        _rowset_warm_up_states.at(rs->rowset_id()).num_inverted_idx_warmed_up,
362
87
                        _rowset_warm_up_states.at(rs->rowset_id()).num_inverted_idx);
363
87
            }
364
104
            res += fmt::format("[{}],", tmp);
365
104
        };
366
5
        traverse_rowsets_unlocked(add_log, true);
367
5
        return res;
368
5
    }
369
370
private:
371
    // FIXME(plat1ko): No need to record base size if rowsets are ordered by version
372
    void update_base_size(const Rowset& rs);
373
374
    Status sync_if_not_running(SyncRowsetStats* stats = nullptr);
375
376
    bool add_rowset_warmup_state_unlocked(
377
            const RowsetMeta& rowset, WarmUpTriggerSource source,
378
            std::chrono::steady_clock::time_point start_tp = std::chrono::steady_clock::now());
379
380
    // used by capture_rs_reader_xxx functions
381
    bool rowset_is_warmed_up_unlocked(int64_t start_version, int64_t end_version) const;
382
383
    CloudStorageEngine& _engine;
384
385
    // this mutex MUST ONLY be used when sync meta
386
    bthread::Mutex _sync_meta_lock;
387
    // ATTENTION: lock order should be: _sync_meta_lock -> _meta_lock
388
389
    std::atomic<int64_t> _cumulative_point {-1};
390
    std::atomic<int64_t> _approximate_num_rowsets {-1};
391
    std::atomic<int64_t> _approximate_num_segments {-1};
392
    std::atomic<int64_t> _approximate_num_rows {-1};
393
    std::atomic<int64_t> _approximate_data_size {-1};
394
    std::atomic<int64_t> _approximate_cumu_num_rowsets {-1};
395
    // Number of sorted arrays (e.g. for rowset with N segments, if rowset is overlapping, delta is N, otherwise 1) after cumu point
396
    std::atomic<int64_t> _approximate_cumu_num_deltas {-1};
397
398
    // timestamp of last cumu compaction failure
399
    std::atomic<int64_t> _last_cumu_compaction_failure_millis;
400
    // timestamp of last base compaction failure
401
    std::atomic<int64_t> _last_base_compaction_failure_millis;
402
    // timestamp of last full compaction failure
403
    std::atomic<int64_t> _last_full_compaction_failure_millis;
404
    // timestamp of last cumu compaction success
405
    std::atomic<int64_t> _last_cumu_compaction_success_millis;
406
    // timestamp of last base compaction success
407
    std::atomic<int64_t> _last_base_compaction_success_millis;
408
    // timestamp of last full compaction success
409
    std::atomic<int64_t> _last_full_compaction_success_millis;
410
    // timestamp of last cumu compaction schedule time
411
    std::atomic<int64_t> _last_cumu_compaction_schedule_millis;
412
    // timestamp of last base compaction schedule time
413
    std::atomic<int64_t> _last_base_compaction_schedule_millis;
414
    // timestamp of last full compaction schedule time
415
    std::atomic<int64_t> _last_full_compaction_schedule_millis;
416
417
    std::string _last_cumu_compaction_status;
418
    std::string _last_base_compaction_status;
419
    std::string _last_full_compaction_status;
420
421
    int64_t _base_compaction_cnt = 0;
422
    int64_t _cumulative_compaction_cnt = 0;
423
    int64_t _full_compaction_cnt = 0;
424
    int64_t _max_version = -1;
425
    int64_t _base_size = 0;
426
    int64_t _alter_version = -1;
427
428
    std::mutex _base_compaction_lock;
429
    std::mutex _cumulative_compaction_lock;
430
431
    // To avoid multiple calc delete bitmap tasks on same (txn_id, tablet_id) with different
432
    // signatures being executed concurrently, we use _rowset_update_lock to serialize them
433
    mutable std::mutex _rowset_update_lock;
434
435
    // unused_rowsets, [start_version, end_version]
436
    std::mutex _gc_mutex;
437
    std::unordered_map<RowsetId, RowsetSharedPtr> _unused_rowsets;
438
    std::vector<std::pair<std::vector<RowsetId>, DeleteBitmapKeyRanges>> _unused_delete_bitmap;
439
440
    // for warm up states management
441
    struct RowsetWarmUpInfo {
442
        WarmUpState state;
443
        int64_t num_segments = 0;
444
        int64_t num_inverted_idx = 0;
445
        int64_t num_segments_warmed_up = 0;
446
        int64_t num_inverted_idx_warmed_up = 0;
447
        std::chrono::steady_clock::time_point start_tp;
448
449
18
        void done(int64_t num_segments, int64_t num_inverted_idx) {
450
18
            num_segments_warmed_up += num_segments;
451
18
            num_inverted_idx_warmed_up += num_inverted_idx;
452
18
            update_state();
453
18
        }
454
455
51
        bool has_finished() const {
456
51
            return (num_segments_warmed_up >= num_segments) &&
457
51
                   (num_inverted_idx_warmed_up >= num_inverted_idx);
458
51
        }
459
460
        void update_state();
461
    };
462
    std::unordered_map<RowsetId, RowsetWarmUpInfo> _rowset_warm_up_states;
463
464
    mutable std::shared_mutex _warmed_up_rowsets_mutex;
465
    std::unordered_set<RowsetId> _warmed_up_rowsets;
466
};
467
468
using CloudTabletSPtr = std::shared_ptr<CloudTablet>;
469
470
} // namespace doris