Coverage Report

Created: 2025-04-14 12:46

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