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