Coverage Report

Created: 2025-04-14 12:06

/root/doris/be/src/cloud/cloud_tablet.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 <memory>
21
22
#include "olap/base_tablet.h"
23
#include "olap/partial_update_info.h"
24
25
namespace doris {
26
27
class CloudStorageEngine;
28
29
struct SyncRowsetStats {
30
    int64_t get_remote_rowsets_num {0};
31
    int64_t get_remote_rowsets_rpc_ms {0};
32
33
    int64_t get_local_delete_bitmap_rowsets_num {0};
34
    int64_t get_remote_delete_bitmap_rowsets_num {0};
35
    int64_t get_remote_delete_bitmap_key_count {0};
36
    int64_t get_remote_delete_bitmap_bytes {0};
37
    int64_t get_remote_delete_bitmap_rpc_ms {0};
38
};
39
40
struct SyncOptions {
41
    bool warmup_delta_data = false;
42
    bool sync_delete_bitmap = true;
43
    bool full_sync = false;
44
    bool merge_schema = false;
45
    int64_t query_version = -1;
46
};
47
48
class CloudTablet final : public BaseTablet {
49
public:
50
    CloudTablet(CloudStorageEngine& engine, TabletMetaSharedPtr tablet_meta);
51
52
    ~CloudTablet() override;
53
54
    bool exceed_version_limit(int32_t limit) override;
55
56
    Result<std::unique_ptr<RowsetWriter>> create_rowset_writer(RowsetWriterContext& context,
57
                                                               bool vertical) override;
58
59
    Status capture_rs_readers(const Version& spec_version, std::vector<RowSetSplits>* rs_splits,
60
                              bool skip_missing_version) override;
61
62
    Status capture_consistent_rowsets_unlocked(
63
            const Version& spec_version, std::vector<RowsetSharedPtr>* rowsets) const override;
64
65
0
    size_t tablet_footprint() override {
66
0
        return _approximate_data_size.load(std::memory_order_relaxed);
67
0
    }
68
69
    // clang-format off
70
0
    int64_t fetch_add_approximate_num_rowsets (int64_t x) { return _approximate_num_rowsets .fetch_add(x, std::memory_order_relaxed); }
71
0
    int64_t fetch_add_approximate_num_segments(int64_t x) { return _approximate_num_segments.fetch_add(x, std::memory_order_relaxed); }
72
0
    int64_t fetch_add_approximate_num_rows    (int64_t x) { return _approximate_num_rows    .fetch_add(x, std::memory_order_relaxed); }
73
0
    int64_t fetch_add_approximate_data_size   (int64_t x) { return _approximate_data_size   .fetch_add(x, std::memory_order_relaxed); }
74
0
    int64_t fetch_add_approximate_cumu_num_rowsets (int64_t x) { return _approximate_cumu_num_rowsets.fetch_add(x, std::memory_order_relaxed); }
75
0
    int64_t fetch_add_approximate_cumu_num_deltas   (int64_t x) { return _approximate_cumu_num_deltas.fetch_add(x, std::memory_order_relaxed); }
76
    // clang-format on
77
78
    // meta lock must be held when calling this function
79
    void reset_approximate_stats(int64_t num_rowsets, int64_t num_segments, int64_t num_rows,
80
                                 int64_t data_size);
81
82
    // return a json string to show the compaction status of this tablet
83
    void get_compaction_status(std::string* json_result);
84
85
    // Synchronize the rowsets from meta service.
86
    // If tablet state is not `TABLET_RUNNING`, sync tablet meta and all visible rowsets.
87
    // If `query_version` > 0 and local max_version of the tablet >= `query_version`, do nothing.
88
    // If 'need_download_data_async' is true, it means that we need to download the new version
89
    // rowsets datum async.
90
    Status sync_rowsets(const SyncOptions& options = {}, SyncRowsetStats* stats = nullptr);
91
92
    // Synchronize the tablet meta from meta service.
93
    Status sync_meta();
94
95
    // If `version_overlap` is true, function will delete rowsets with overlapped version in this tablet.
96
    // If 'warmup_delta_data' is true, download the new version rowset data in background.
97
    // MUST hold EXCLUSIVE `_meta_lock`.
98
    // If 'need_download_data_async' is true, it means that we need to download the new version
99
    // rowsets datum async.
100
    void add_rowsets(std::vector<RowsetSharedPtr> to_add, bool version_overlap,
101
                     std::unique_lock<std::shared_mutex>& meta_lock,
102
                     bool warmup_delta_data = false);
103
104
    // MUST hold EXCLUSIVE `_meta_lock`.
105
    void delete_rowsets(const std::vector<RowsetSharedPtr>& to_delete,
106
                        std::unique_lock<std::shared_mutex>& meta_lock);
107
108
    // When the tablet is dropped, we need to recycle cached data:
109
    // 1. The data in file cache
110
    // 2. The memory in tablet cache
111
    void clear_cache() override;
112
113
    // Return number of deleted stale rowsets
114
    uint64_t delete_expired_stale_rowsets();
115
116
0
    bool has_stale_rowsets() const { return !_stale_rs_version_map.empty(); }
117
118
    int64_t get_cloud_base_compaction_score() const;
119
    int64_t get_cloud_cumu_compaction_score() const;
120
121
0
    int64_t max_version_unlocked() const override { return _max_version; }
122
0
    int64_t base_compaction_cnt() const { return _base_compaction_cnt; }
123
0
    int64_t cumulative_compaction_cnt() const { return _cumulative_compaction_cnt; }
124
0
    int64_t cumulative_layer_point() const {
125
0
        return _cumulative_point.load(std::memory_order_relaxed);
126
0
    }
127
128
0
    void set_base_compaction_cnt(int64_t cnt) { _base_compaction_cnt = cnt; }
129
0
    void set_cumulative_compaction_cnt(int64_t cnt) { _cumulative_compaction_cnt = cnt; }
130
    void set_cumulative_layer_point(int64_t new_point);
131
132
0
    int64_t last_cumu_compaction_failure_time() { return _last_cumu_compaction_failure_millis; }
133
0
    void set_last_cumu_compaction_failure_time(int64_t millis) {
134
0
        _last_cumu_compaction_failure_millis = millis;
135
0
    }
136
137
0
    int64_t last_base_compaction_failure_time() { return _last_base_compaction_failure_millis; }
138
0
    void set_last_base_compaction_failure_time(int64_t millis) {
139
0
        _last_base_compaction_failure_millis = millis;
140
0
    }
141
142
0
    int64_t last_full_compaction_failure_time() { return _last_full_compaction_failure_millis; }
143
0
    void set_last_full_compaction_failure_time(int64_t millis) {
144
0
        _last_full_compaction_failure_millis = millis;
145
0
    }
146
147
0
    int64_t last_cumu_compaction_success_time() { return _last_cumu_compaction_success_millis; }
148
0
    void set_last_cumu_compaction_success_time(int64_t millis) {
149
0
        _last_cumu_compaction_success_millis = millis;
150
0
    }
151
152
0
    int64_t last_base_compaction_success_time() { return _last_base_compaction_success_millis; }
153
0
    void set_last_base_compaction_success_time(int64_t millis) {
154
0
        _last_base_compaction_success_millis = millis;
155
0
    }
156
157
0
    int64_t last_full_compaction_success_time() { return _last_full_compaction_success_millis; }
158
0
    void set_last_full_compaction_success_time(int64_t millis) {
159
0
        _last_full_compaction_success_millis = millis;
160
0
    }
161
162
0
    int64_t last_base_compaction_schedule_time() { return _last_base_compaction_schedule_millis; }
163
0
    void set_last_base_compaction_schedule_time(int64_t millis) {
164
0
        _last_base_compaction_schedule_millis = millis;
165
0
    }
166
167
0
    int64_t alter_version() const { return _alter_version; }
168
0
    void set_alter_version(int64_t alter_version) { _alter_version = alter_version; }
169
170
    std::vector<RowsetSharedPtr> pick_candidate_rowsets_to_base_compaction();
171
172
0
    inline Version max_version() const {
173
0
        std::shared_lock rdlock(_meta_lock);
174
0
        return _tablet_meta->max_version();
175
0
    }
176
177
0
    int64_t base_size() const { return _base_size; }
178
179
    std::vector<RowsetSharedPtr> pick_candidate_rowsets_to_full_compaction();
180
181
0
    std::mutex& get_base_compaction_lock() { return _base_compaction_lock; }
182
0
    std::mutex& get_cumulative_compaction_lock() { return _cumulative_compaction_lock; }
183
184
    Result<std::unique_ptr<RowsetWriter>> create_transient_rowset_writer(
185
            const Rowset& rowset, std::shared_ptr<PartialUpdateInfo> partial_update_info,
186
            int64_t txn_expiration = 0) override;
187
188
    CalcDeleteBitmapExecutor* calc_delete_bitmap_executor() override;
189
190
    Status save_delete_bitmap(const TabletTxnInfo* txn_info, int64_t txn_id,
191
                              DeleteBitmapPtr delete_bitmap, RowsetWriter* rowset_writer,
192
                              const RowsetIdUnorderedSet& cur_rowset_ids, int64_t lock_id = -1,
193
                              int64_t next_visible_version = -1) override;
194
195
    Status save_delete_bitmap_to_ms(int64_t cur_version, int64_t txn_id,
196
                                    DeleteBitmapPtr delete_bitmap, int64_t lock_id,
197
                                    int64_t next_visible_version);
198
199
    Status calc_delete_bitmap_for_compaction(const std::vector<RowsetSharedPtr>& input_rowsets,
200
                                             const RowsetSharedPtr& output_rowset,
201
                                             const RowIdConversion& rowid_conversion,
202
                                             ReaderType compaction_type, int64_t merged_rows,
203
                                             int64_t filtered_rows, int64_t initiator,
204
                                             DeleteBitmapPtr& output_rowset_delete_bitmap,
205
                                             bool allow_delete_in_cumu_compaction);
206
207
    // Find the missed versions until the spec_version.
208
    //
209
    // for example:
210
    //     [0-4][5-5][8-8][9-9][14-14]
211
    // if spec_version = 12, it will return [6-7],[10-12]
212
    Versions calc_missed_versions(int64_t spec_version, Versions existing_versions) const override;
213
214
0
    std::mutex& get_rowset_update_lock() { return _rowset_update_lock; }
215
216
0
    const auto& rowset_map() const { return _rs_version_map; }
217
218
    // Merge all rowset schemas within a CloudTablet
219
    Status merge_rowsets_schema();
220
221
    int64_t last_sync_time_s = 0;
222
    int64_t last_load_time_ms = 0;
223
    int64_t last_base_compaction_success_time_ms = 0;
224
    int64_t last_cumu_compaction_success_time_ms = 0;
225
    int64_t last_cumu_no_suitable_version_ms = 0;
226
    int64_t last_access_time_ms = 0;
227
228
    // Return merged extended schema
229
    TabletSchemaSPtr merged_tablet_schema() const override;
230
231
    void build_tablet_report_info(TTabletInfo* tablet_info);
232
233
    static void recycle_cached_data(const std::vector<RowsetSharedPtr>& rowsets);
234
235
    // check that if the delete bitmap in delete bitmap cache has the same cardinality with the expected_delete_bitmap's
236
    Status check_delete_bitmap_cache(int64_t txn_id, DeleteBitmap* expected_delete_bitmap) override;
237
238
private:
239
    // FIXME(plat1ko): No need to record base size if rowsets are ordered by version
240
    void update_base_size(const Rowset& rs);
241
242
    Status sync_if_not_running(SyncRowsetStats* stats = nullptr);
243
244
    CloudStorageEngine& _engine;
245
246
    // this mutex MUST ONLY be used when sync meta
247
    bthread::Mutex _sync_meta_lock;
248
249
    std::atomic<int64_t> _cumulative_point {-1};
250
    std::atomic<int64_t> _approximate_num_rowsets {-1};
251
    std::atomic<int64_t> _approximate_num_segments {-1};
252
    std::atomic<int64_t> _approximate_num_rows {-1};
253
    std::atomic<int64_t> _approximate_data_size {-1};
254
    std::atomic<int64_t> _approximate_cumu_num_rowsets {-1};
255
    // Number of sorted arrays (e.g. for rowset with N segments, if rowset is overlapping, delta is N, otherwise 1) after cumu point
256
    std::atomic<int64_t> _approximate_cumu_num_deltas {-1};
257
258
    // timestamp of last cumu compaction failure
259
    std::atomic<int64_t> _last_cumu_compaction_failure_millis;
260
    // timestamp of last base compaction failure
261
    std::atomic<int64_t> _last_base_compaction_failure_millis;
262
    // timestamp of last full compaction failure
263
    std::atomic<int64_t> _last_full_compaction_failure_millis;
264
    // timestamp of last cumu compaction success
265
    std::atomic<int64_t> _last_cumu_compaction_success_millis;
266
    // timestamp of last base compaction success
267
    std::atomic<int64_t> _last_base_compaction_success_millis;
268
    // timestamp of last full compaction success
269
    std::atomic<int64_t> _last_full_compaction_success_millis;
270
    // timestamp of last base compaction schedule time
271
    std::atomic<int64_t> _last_base_compaction_schedule_millis;
272
273
    int64_t _base_compaction_cnt = 0;
274
    int64_t _cumulative_compaction_cnt = 0;
275
    int64_t _max_version = -1;
276
    int64_t _base_size = 0;
277
    int64_t _alter_version = -1;
278
279
    std::mutex _base_compaction_lock;
280
    std::mutex _cumulative_compaction_lock;
281
    mutable std::mutex _rowset_update_lock;
282
283
    // Schema will be merged from all rowsets when sync_rowsets
284
    TabletSchemaSPtr _merged_tablet_schema;
285
};
286
287
using CloudTabletSPtr = std::shared_ptr<CloudTablet>;
288
289
} // namespace doris