Coverage Report

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