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 "storage/partial_update_info.h" |
23 | | #include "storage/rowset/rowset.h" |
24 | | #include "storage/tablet/base_tablet.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 | | bool operator==(const WarmUpState& other) const { |
39 | | return trigger_source == other.trigger_source && progress == other.progress; |
40 | | } |
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 | | int64_t bthread_schedule_delay_ns {0}; |
58 | | int64_t meta_lock_wait_ns {0}; // _meta_lock (BthreadSharedMutex) wait across all acquisitions |
59 | | int64_t sync_meta_lock_wait_ns { |
60 | | 0}; // _sync_meta_lock (bthread::Mutex) wait across all acquisitions |
61 | | }; |
62 | | |
63 | | struct SyncOptions { |
64 | | bool warmup_delta_data = false; |
65 | | bool sync_delete_bitmap = true; |
66 | | bool full_sync = false; |
67 | | bool merge_schema = false; |
68 | | int64_t query_version = -1; |
69 | | }; |
70 | | |
71 | | struct RecycledRowsets { |
72 | | RowsetId rowset_id; |
73 | | int64_t num_segments; |
74 | | std::vector<int64_t> segment_ids; |
75 | | std::vector<std::string> index_file_names; |
76 | | }; |
77 | | |
78 | | class CloudTablet final : public BaseTablet { |
79 | | public: |
80 | | CloudTablet(CloudStorageEngine& engine, TabletMetaSharedPtr tablet_meta); |
81 | | |
82 | | ~CloudTablet() override; |
83 | | |
84 | | bool exceed_version_limit(int32_t limit) override; |
85 | | |
86 | | Result<std::unique_ptr<RowsetWriter>> create_rowset_writer(RowsetWriterContext& context, |
87 | | bool vertical) override; |
88 | | |
89 | | Status capture_rs_readers(const Version& spec_version, std::vector<RowSetSplits>* rs_splits, |
90 | | const CaptureRowsetOps& opts) override; |
91 | | |
92 | | [[nodiscard]] Result<std::vector<Version>> capture_consistent_versions_unlocked( |
93 | | const Version& version_range, const CaptureRowsetOps& options) const override; |
94 | | |
95 | | // Capture versions with cache preference optimization. |
96 | | // This method prioritizes using cached/warmed-up rowsets when building version paths, |
97 | | // avoiding cold data reads when possible. It uses capture_consistent_versions_prefer_cache |
98 | | // to find a consistent version path that prefers already warmed-up rowsets. |
99 | | Result<std::vector<Version>> capture_versions_prefer_cache(const Version& spec_version) const; |
100 | | |
101 | | // Capture versions with query freshness tolerance. |
102 | | // This method finds a consistent version path where all rowsets are warmed up, |
103 | | // but allows fallback to normal capture if there are newer rowsets that should be |
104 | | // visible (based on freshness tolerance) but haven't been warmed up yet. |
105 | | // For merge-on-write tables, uses special validation to ensure data correctness. |
106 | | // |
107 | | // IMPORTANT: The returned version may be smaller than the requested version if newer |
108 | | // data hasn't been warmed up yet. This can cause different tablets in the same query |
109 | | // to read from different versions, potentially leading to inconsistent query results. |
110 | | // |
111 | | // @param options.query_freshness_tolerance_ms: Time tolerance in milliseconds. Rowsets that |
112 | | // became visible within this time range (after current_time - query_freshness_tolerance_ms) |
113 | | // can be skipped if not warmed up. However, if older rowsets (before this time point) |
114 | | // are not warmed up, the method will fallback to normal capture. |
115 | | Result<std::vector<Version>> capture_versions_with_freshness_tolerance( |
116 | | const Version& spec_version, const CaptureRowsetOps& options) const; |
117 | | |
118 | 538k | size_t tablet_footprint() override { |
119 | 538k | return _approximate_data_size.load(std::memory_order_relaxed); |
120 | 538k | } |
121 | | |
122 | | std::string tablet_path() const override; |
123 | | |
124 | | // clang-format off |
125 | 3.90M | int64_t fetch_add_approximate_num_rowsets (int64_t x) { return _approximate_num_rowsets .fetch_add(x, std::memory_order_relaxed); } |
126 | 178k | int64_t fetch_add_approximate_num_segments(int64_t x) { return _approximate_num_segments.fetch_add(x, std::memory_order_relaxed); } |
127 | 178k | int64_t fetch_add_approximate_num_rows (int64_t x) { return _approximate_num_rows .fetch_add(x, std::memory_order_relaxed); } |
128 | 178k | int64_t fetch_add_approximate_data_size (int64_t x) { return _approximate_data_size .fetch_add(x, std::memory_order_relaxed); } |
129 | 205k | int64_t fetch_add_approximate_cumu_num_rowsets (int64_t x) { return _approximate_cumu_num_rowsets.fetch_add(x, std::memory_order_relaxed); } |
130 | 181k | int64_t fetch_add_approximate_cumu_num_deltas (int64_t x) { return _approximate_cumu_num_deltas.fetch_add(x, std::memory_order_relaxed); } |
131 | | // clang-format on |
132 | | |
133 | | // meta lock must be held when calling this function |
134 | | void reset_approximate_stats(int64_t num_rowsets, int64_t num_segments, int64_t num_rows, |
135 | | int64_t data_size); |
136 | | |
137 | | // return a json string to show the compaction status of this tablet |
138 | | void get_compaction_status(std::string* json_result); |
139 | | |
140 | | // Synchronize the rowsets from meta service. |
141 | | // If tablet state is not `TABLET_RUNNING`, sync tablet meta and all visible rowsets. |
142 | | // If `query_version` > 0 and local max_version of the tablet >= `query_version`, do nothing. |
143 | | // If 'need_download_data_async' is true, it means that we need to download the new version |
144 | | // rowsets datum async. |
145 | | Status sync_rowsets(const SyncOptions& options = {}, SyncRowsetStats* stats = nullptr); |
146 | | |
147 | | // Synchronize the tablet meta from meta service. |
148 | | Status sync_meta(); |
149 | | |
150 | | // If `version_overlap` is true, function will delete rowsets with overlapped version in this tablet. |
151 | | // If 'warmup_delta_data' is true, download the new version rowset data in background. |
152 | | // MUST hold EXCLUSIVE `_meta_lock`. |
153 | | // If 'need_download_data_async' is true, it means that we need to download the new version |
154 | | // rowsets datum async. |
155 | | void add_rowsets(std::vector<RowsetSharedPtr> to_add, bool version_overlap, |
156 | | std::unique_lock<BthreadSharedMutex>& meta_lock, |
157 | | bool warmup_delta_data = false); |
158 | | |
159 | | // MUST hold EXCLUSIVE `_meta_lock`. |
160 | | void delete_rowsets(const std::vector<RowsetSharedPtr>& to_delete, |
161 | | std::unique_lock<BthreadSharedMutex>& meta_lock); |
162 | | |
163 | | // Like delete_rowsets, but also removes edges from the version graph. |
164 | | // Used by schema change to prevent the greedy capture algorithm from |
165 | | // preferring stale compaction rowsets over individual SC output rowsets. |
166 | | // MUST hold EXCLUSIVE `_meta_lock`. |
167 | | void delete_rowsets_for_schema_change(const std::vector<RowsetSharedPtr>& to_delete, |
168 | | std::unique_lock<BthreadSharedMutex>& meta_lock, |
169 | | bool recycle_deleted_rowsets = true); |
170 | | |
171 | | // Replace local rowsets in [2, alter_version] with schema change output rowsets. |
172 | | // Existing SC output rowsets are kept; other local/double-write/compaction rowsets |
173 | | // in this version range are removed from both _rs_version_map and version graph. |
174 | | // recycle_deleted_rowsets should only be true for the real tablet; temporary |
175 | | // schema-change delete-bitmap tablets only need to normalize their local graph. |
176 | | // MUST hold EXCLUSIVE `_meta_lock`. |
177 | | void replace_rowsets_with_schema_change_output( |
178 | | const std::vector<RowsetSharedPtr>& output_rowsets, int64_t alter_version, |
179 | | std::unique_lock<BthreadSharedMutex>& meta_lock, const char* stage, |
180 | | bool recycle_deleted_rowsets); |
181 | | |
182 | | // When the tablet is dropped, we need to recycle cached data: |
183 | | // 1. The data in file cache |
184 | | // 2. The memory in tablet cache |
185 | | void clear_cache() override; |
186 | | |
187 | | // Return number of deleted stale rowsets |
188 | | uint64_t delete_expired_stale_rowsets(); |
189 | | |
190 | 685k | bool has_stale_rowsets() const { return !_stale_rs_version_map.empty(); } |
191 | | |
192 | | int64_t get_cloud_base_compaction_score() const; |
193 | | int64_t get_cloud_cumu_compaction_score() const; |
194 | | |
195 | 710k | int64_t max_version_unlocked() const override { return _max_version; } |
196 | 963k | int64_t base_compaction_cnt() const { return _base_compaction_cnt; } |
197 | 962k | int64_t cumulative_compaction_cnt() const { return _cumulative_compaction_cnt; } |
198 | 354k | int64_t full_compaction_cnt() const { return _full_compaction_cnt; } |
199 | 819k | int64_t cumulative_layer_point() const { |
200 | 819k | return _cumulative_point.load(std::memory_order_relaxed); |
201 | 819k | } |
202 | | |
203 | 354k | void set_base_compaction_cnt(int64_t cnt) { _base_compaction_cnt = cnt; } |
204 | 365k | void set_cumulative_compaction_cnt(int64_t cnt) { _cumulative_compaction_cnt = cnt; } |
205 | 354k | void set_full_compaction_cnt(int64_t cnt) { _full_compaction_cnt = cnt; } |
206 | | void set_cumulative_layer_point(int64_t new_point); |
207 | | |
208 | 543M | int64_t last_cumu_compaction_failure_time() { return _last_cumu_compaction_failure_millis; } |
209 | 1.14k | void set_last_cumu_compaction_failure_time(int64_t millis) { |
210 | 1.14k | _last_cumu_compaction_failure_millis = millis; |
211 | 1.14k | } |
212 | | |
213 | 71.1M | int64_t last_base_compaction_failure_time() { return _last_base_compaction_failure_millis; } |
214 | 7.60k | void set_last_base_compaction_failure_time(int64_t millis) { |
215 | 7.60k | _last_base_compaction_failure_millis = millis; |
216 | 7.60k | } |
217 | | |
218 | 0 | int64_t last_full_compaction_failure_time() { return _last_full_compaction_failure_millis; } |
219 | 0 | void set_last_full_compaction_failure_time(int64_t millis) { |
220 | 0 | _last_full_compaction_failure_millis = millis; |
221 | 0 | } |
222 | | |
223 | 39.7k | int64_t last_cumu_compaction_success_time() { return _last_cumu_compaction_success_millis; } |
224 | 23.0k | void set_last_cumu_compaction_success_time(int64_t millis) { |
225 | 23.0k | _last_cumu_compaction_success_millis = millis; |
226 | 23.0k | } |
227 | | |
228 | 11.9k | int64_t last_base_compaction_success_time() { return _last_base_compaction_success_millis; } |
229 | 12.0k | void set_last_base_compaction_success_time(int64_t millis) { |
230 | 12.0k | _last_base_compaction_success_millis = millis; |
231 | 12.0k | } |
232 | | |
233 | 11.7k | int64_t last_full_compaction_success_time() { return _last_full_compaction_success_millis; } |
234 | 11.8k | void set_last_full_compaction_success_time(int64_t millis) { |
235 | 11.8k | _last_full_compaction_success_millis = millis; |
236 | 11.8k | } |
237 | | |
238 | 0 | int64_t last_cumu_compaction_schedule_time() { return _last_cumu_compaction_schedule_millis; } |
239 | 373 | void set_last_cumu_compaction_schedule_time(int64_t millis) { |
240 | 373 | _last_cumu_compaction_schedule_millis = millis; |
241 | 373 | } |
242 | | |
243 | 0 | int64_t last_base_compaction_schedule_time() { return _last_base_compaction_schedule_millis; } |
244 | 2 | void set_last_base_compaction_schedule_time(int64_t millis) { |
245 | 2 | _last_base_compaction_schedule_millis = millis; |
246 | 2 | } |
247 | | |
248 | 0 | int64_t last_full_compaction_schedule_time() { return _last_full_compaction_schedule_millis; } |
249 | 58 | void set_last_full_compaction_schedule_time(int64_t millis) { |
250 | 58 | _last_full_compaction_schedule_millis = millis; |
251 | 58 | } |
252 | | |
253 | 168k | void set_last_cumu_compaction_status(std::string status) { |
254 | 168k | _last_cumu_compaction_status = std::move(status); |
255 | 168k | } |
256 | | |
257 | 0 | std::string get_last_cumu_compaction_status() { return _last_cumu_compaction_status; } |
258 | | |
259 | 3.97k | void set_last_base_compaction_status(std::string status) { |
260 | 3.97k | _last_base_compaction_status = std::move(status); |
261 | 3.97k | } |
262 | | |
263 | 0 | std::string get_last_base_compaction_status() { return _last_base_compaction_status; } |
264 | | |
265 | 116 | void set_last_full_compaction_status(std::string status) { |
266 | 116 | _last_full_compaction_status = std::move(status); |
267 | 116 | } |
268 | | |
269 | 0 | std::string get_last_full_compaction_status() { return _last_full_compaction_status; } |
270 | | |
271 | 229k | int64_t alter_version() const { return _alter_version; } |
272 | 83.9k | void set_alter_version(int64_t alter_version) { _alter_version = alter_version; } |
273 | | |
274 | | struct LastActiveClusterInfo { |
275 | | std::string cluster_id; |
276 | | int64_t time_ms; |
277 | | int64_t epoch; |
278 | | }; |
279 | | |
280 | 38.5M | LastActiveClusterInfo last_active_cluster_info() const { |
281 | 38.5M | std::shared_lock lock(_cluster_info_mutex); |
282 | 38.5M | return {.cluster_id = _last_active_cluster_id, |
283 | 38.5M | .time_ms = _last_active_time_ms, |
284 | 38.5M | .epoch = _last_active_epoch}; |
285 | 38.5M | } |
286 | | std::string last_active_cluster_id() const { return last_active_cluster_info().cluster_id; } |
287 | | int64_t last_active_time_ms() const { return last_active_cluster_info().time_ms; } |
288 | | int64_t last_active_epoch() const { return last_active_cluster_info().epoch; } |
289 | | void set_last_active_cluster_info(const std::string& cluster_id, int64_t time_ms, |
290 | 365k | int64_t epoch = 0) { |
291 | 365k | std::unique_lock lock(_cluster_info_mutex); |
292 | 365k | _last_active_cluster_id = cluster_id; |
293 | 365k | _last_active_time_ms = time_ms; |
294 | 365k | _last_active_epoch = epoch; |
295 | 365k | } |
296 | | bool update_last_active_cluster_info(const std::string& cluster_id, int64_t time_ms, |
297 | 59.8k | int64_t epoch) { |
298 | 59.8k | std::unique_lock lock(_cluster_info_mutex); |
299 | 59.8k | if (epoch <= _last_active_epoch) { |
300 | 19 | return false; |
301 | 19 | } |
302 | 59.8k | _last_active_cluster_id = cluster_id; |
303 | 59.8k | _last_active_time_ms = time_ms; |
304 | 59.8k | _last_active_epoch = epoch; |
305 | 59.8k | return true; |
306 | 59.8k | } |
307 | | |
308 | | // MUST hold SHARED `_meta_lock`. |
309 | | std::vector<RowsetSharedPtr> pick_candidate_rowsets_to_base_compaction_unlocked(); |
310 | | |
311 | 936k | inline Version max_version() const { |
312 | 936k | std::shared_lock rdlock(_meta_lock); |
313 | 936k | return _tablet_meta->max_version(); |
314 | 936k | } |
315 | | |
316 | 133k | int64_t base_size() const { return _base_size; } |
317 | | |
318 | | // MUST hold SHARED `_meta_lock`. |
319 | | std::vector<RowsetSharedPtr> pick_candidate_rowsets_to_full_compaction_unlocked(); |
320 | | Result<RowsetSharedPtr> pick_a_rowset_for_index_change(int schema_version, |
321 | | bool& is_base_rowset); |
322 | | Status check_rowset_schema_for_build_index(std::vector<TColumn>& columns, int schema_version); |
323 | | |
324 | 0 | std::mutex& get_base_compaction_lock() { return _base_compaction_lock; } |
325 | 0 | std::mutex& get_cumulative_compaction_lock() { return _cumulative_compaction_lock; } |
326 | | |
327 | | Result<std::unique_ptr<RowsetWriter>> create_transient_rowset_writer( |
328 | | const Rowset& rowset, std::shared_ptr<PartialUpdateInfo> partial_update_info, |
329 | | int64_t txn_expiration = 0) override; |
330 | | |
331 | | CalcDeleteBitmapExecutor* calc_delete_bitmap_executor() override; |
332 | | |
333 | | Status save_delete_bitmap(const TabletTxnInfo* txn_info, int64_t txn_id, |
334 | | DeleteBitmapPtr delete_bitmap, RowsetWriter* rowset_writer, |
335 | | const RowsetIdUnorderedSet& cur_rowset_ids, int64_t lock_id = -1, |
336 | | int64_t next_visible_version = -1) override; |
337 | | |
338 | | Status save_delete_bitmap_to_ms(int64_t cur_version, int64_t txn_id, |
339 | | DeleteBitmapPtr delete_bitmap, int64_t lock_id, |
340 | | int64_t next_visible_version, RowsetSharedPtr rowset); |
341 | | |
342 | | Status calc_delete_bitmap_for_compaction(const std::vector<RowsetSharedPtr>& input_rowsets, |
343 | | const RowsetSharedPtr& output_rowset, |
344 | | const RowIdConversion& rowid_conversion, |
345 | | ReaderType compaction_type, int64_t merged_rows, |
346 | | int64_t filtered_rows, int64_t initiator, |
347 | | DeleteBitmapPtr& output_rowset_delete_bitmap, |
348 | | bool allow_delete_in_cumu_compaction, |
349 | | int64_t& get_delete_bitmap_lock_start_time); |
350 | | |
351 | | // Find the missed versions until the spec_version. |
352 | | // |
353 | | // for example: |
354 | | // [0-4][5-5][8-8][9-9][14-14] |
355 | | // if spec_version = 12, it will return [6-7],[10-12] |
356 | | Versions calc_missed_versions(int64_t spec_version, Versions existing_versions) const override; |
357 | | |
358 | 51.7k | std::mutex& get_rowset_update_lock() { return _rowset_update_lock; } |
359 | | |
360 | 160k | bthread::Mutex& get_sync_meta_lock() { return _sync_meta_lock; } |
361 | | |
362 | 120k | const auto& rowset_map() const { return _rs_version_map; } |
363 | | |
364 | | int64_t last_sync_time_s = 0; |
365 | | int64_t last_load_time_ms = 0; |
366 | | int64_t last_base_compaction_success_time_ms = 0; |
367 | | int64_t last_cumu_compaction_success_time_ms = 0; |
368 | | int64_t last_cumu_no_suitable_version_ms = 0; |
369 | | int64_t last_access_time_ms = 0; |
370 | | |
371 | | std::atomic<int64_t> local_read_time_us = 0; |
372 | | std::atomic<int64_t> remote_read_time_us = 0; |
373 | | std::atomic<int64_t> exec_compaction_time_us = 0; |
374 | | |
375 | | void build_tablet_report_info(TTabletInfo* tablet_info); |
376 | | |
377 | | // check that if the delete bitmap in delete bitmap cache has the same cardinality with the expected_delete_bitmap's |
378 | | Status check_delete_bitmap_cache(int64_t txn_id, DeleteBitmap* expected_delete_bitmap) override; |
379 | | |
380 | | void agg_delete_bitmap_for_compaction(int64_t start_version, int64_t end_version, |
381 | | const std::vector<RowsetSharedPtr>& pre_rowsets, |
382 | | DeleteBitmapPtr& new_delete_bitmap, |
383 | | std::map<std::string, int64_t>& pre_rowset_to_versions); |
384 | | |
385 | | bool need_remove_unused_rowsets(); |
386 | | |
387 | | void add_unused_rowsets(const std::vector<RowsetSharedPtr>& rowsets); |
388 | | void remove_unused_rowsets(); |
389 | | |
390 | | // For each given rowset not in active use, clears its file cache and returns its |
391 | | // ID, segment count, and index file names as RecycledRowsets entries. |
392 | | static std::vector<RecycledRowsets> recycle_cached_data( |
393 | | const std::vector<RowsetSharedPtr>& rowsets); |
394 | | |
395 | | // Add warmup state management |
396 | | WarmUpState get_rowset_warmup_state(RowsetId rowset_id); |
397 | | bool add_rowset_warmup_state( |
398 | | const RowsetMeta& rowset, WarmUpTriggerSource source, |
399 | | std::chrono::steady_clock::time_point start_tp = std::chrono::steady_clock::now()); |
400 | | bool update_rowset_warmup_state_inverted_idx_num(WarmUpTriggerSource source, RowsetId rowset_id, |
401 | | int64_t delta); |
402 | | bool update_rowset_warmup_state_inverted_idx_num_unlocked(WarmUpTriggerSource source, |
403 | | RowsetId rowset_id, int64_t delta); |
404 | | WarmUpState complete_rowset_segment_warmup(WarmUpTriggerSource trigger_source, |
405 | | RowsetId rowset_id, Status status, |
406 | | int64_t segment_num, int64_t inverted_idx_num); |
407 | | |
408 | | bool is_rowset_warmed_up(const RowsetId& rowset_id) const; |
409 | | |
410 | | void add_warmed_up_rowset(const RowsetId& rowset_id); |
411 | | // Test helper: add a rowset to the warmup state map with DOING progress, |
412 | | // so that is_rowset_warmed_up() returns false for it. |
413 | | void add_not_warmed_up_rowset(const RowsetId& rowset_id); |
414 | | |
415 | | // Try to apply visible pending rowsets to tablet meta in version order |
416 | | // This should be called after receiving FE notification or when new rowsets are added |
417 | | // @return Status::OK() if successfully applied, error otherwise |
418 | | void apply_visible_pending_rowsets(); |
419 | | |
420 | | void try_make_committed_rs_visible(int64_t txn_id, int64_t visible_version, |
421 | | int64_t version_update_time_ms); |
422 | | void try_make_committed_rs_visible_for_mow(int64_t txn_id, int64_t visible_version, |
423 | | int64_t version_update_time_ms); |
424 | | |
425 | | void clear_unused_visible_pending_rowsets(); |
426 | | |
427 | 5 | std::string rowset_warmup_digest() const { |
428 | 5 | std::string res; |
429 | 104 | auto add_log = [&](const RowsetSharedPtr& rs) { |
430 | 104 | auto tmp = fmt::format("{}{}", rs->rowset_id().to_string(), rs->version().to_string()); |
431 | 104 | if (_rowset_warm_up_states.contains(rs->rowset_id())) { |
432 | 104 | tmp += fmt::format( |
433 | 104 | ", progress={}, segments_warmed_up={}/{}, inverted_idx_warmed_up={}/{}", |
434 | 104 | _rowset_warm_up_states.at(rs->rowset_id()).state.progress, |
435 | 104 | _rowset_warm_up_states.at(rs->rowset_id()).num_segments_warmed_up, |
436 | 104 | _rowset_warm_up_states.at(rs->rowset_id()).num_segments, |
437 | 104 | _rowset_warm_up_states.at(rs->rowset_id()).num_inverted_idx_warmed_up, |
438 | 104 | _rowset_warm_up_states.at(rs->rowset_id()).num_inverted_idx); |
439 | 104 | } |
440 | 104 | res += fmt::format("[{}],", tmp); |
441 | 104 | }; |
442 | 5 | traverse_rowsets_unlocked(add_log, true); |
443 | 5 | return res; |
444 | 5 | } |
445 | | |
446 | | private: |
447 | | // FIXME(plat1ko): No need to record base size if rowsets are ordered by version |
448 | | void update_base_size(const Rowset& rs); |
449 | | |
450 | | Status sync_if_not_running(SyncRowsetStats* stats = nullptr); |
451 | | |
452 | | bool add_rowset_warmup_state_unlocked( |
453 | | const RowsetMeta& rowset, WarmUpTriggerSource source, |
454 | | std::chrono::steady_clock::time_point start_tp = std::chrono::steady_clock::now()); |
455 | | |
456 | | // used by capture_rs_reader_xxx functions |
457 | | bool rowset_is_warmed_up_unlocked(int64_t start_version, int64_t end_version) const; |
458 | | |
459 | | // Check if a rowset should be visible but not warmed up within freshness tolerance |
460 | | bool _check_rowset_should_be_visible_but_not_warmed_up( |
461 | | const RowsetMetaSharedPtr& rs_meta, int64_t path_max_version, |
462 | | std::chrono::system_clock::time_point freshness_limit_tp) const; |
463 | | |
464 | | // Submit a segment download task for warming up |
465 | | void _submit_segment_download_task(const RowsetSharedPtr& rs, io::Path segment_path, |
466 | | int64_t segment_file_size, int64_t expiration_time); |
467 | | |
468 | | // Submit an inverted index download task for warming up |
469 | | void _submit_inverted_index_download_task(const RowsetSharedPtr& rs, |
470 | | const StorageResource* storage_resource, |
471 | | const io::Path& idx_path, int64_t idx_size, |
472 | | int64_t expiration_time); |
473 | | |
474 | | // Add rowsets directly with warmup |
475 | | void _add_rowsets_directly(std::vector<RowsetSharedPtr>& rowsets, bool warmup_delta_data); |
476 | | |
477 | | CloudStorageEngine& _engine; |
478 | | |
479 | | // this mutex MUST ONLY be used when sync meta |
480 | | bthread::Mutex _sync_meta_lock; |
481 | | // ATTENTION: lock order should be: _sync_meta_lock -> _meta_lock |
482 | | |
483 | | std::atomic<int64_t> _cumulative_point {-1}; |
484 | | std::atomic<int64_t> _approximate_num_rowsets {-1}; |
485 | | std::atomic<int64_t> _approximate_num_segments {-1}; |
486 | | std::atomic<int64_t> _approximate_num_rows {-1}; |
487 | | std::atomic<int64_t> _approximate_data_size {-1}; |
488 | | std::atomic<int64_t> _approximate_cumu_num_rowsets {-1}; |
489 | | // Number of sorted arrays (e.g. for rowset with N segments, if rowset is overlapping, delta is N, otherwise 1) after cumu point |
490 | | std::atomic<int64_t> _approximate_cumu_num_deltas {-1}; |
491 | | |
492 | | // timestamp of last cumu compaction failure |
493 | | std::atomic<int64_t> _last_cumu_compaction_failure_millis; |
494 | | // timestamp of last base compaction failure |
495 | | std::atomic<int64_t> _last_base_compaction_failure_millis; |
496 | | // timestamp of last full compaction failure |
497 | | std::atomic<int64_t> _last_full_compaction_failure_millis; |
498 | | // timestamp of last cumu compaction success |
499 | | std::atomic<int64_t> _last_cumu_compaction_success_millis; |
500 | | // timestamp of last base compaction success |
501 | | std::atomic<int64_t> _last_base_compaction_success_millis; |
502 | | // timestamp of last full compaction success |
503 | | std::atomic<int64_t> _last_full_compaction_success_millis; |
504 | | // timestamp of last cumu compaction schedule time |
505 | | std::atomic<int64_t> _last_cumu_compaction_schedule_millis; |
506 | | // timestamp of last base compaction schedule time |
507 | | std::atomic<int64_t> _last_base_compaction_schedule_millis; |
508 | | // timestamp of last full compaction schedule time |
509 | | std::atomic<int64_t> _last_full_compaction_schedule_millis; |
510 | | std::string _last_cumu_compaction_status; |
511 | | std::string _last_base_compaction_status; |
512 | | std::string _last_full_compaction_status; |
513 | | int64_t _base_compaction_cnt = 0; |
514 | | int64_t _cumulative_compaction_cnt = 0; |
515 | | int64_t _full_compaction_cnt = 0; |
516 | | int64_t _max_version = -1; |
517 | | int64_t _base_size = 0; |
518 | | int64_t _alter_version = -1; |
519 | | |
520 | | std::mutex _base_compaction_lock; |
521 | | std::mutex _cumulative_compaction_lock; |
522 | | |
523 | | // To avoid multiple calc delete bitmap tasks on same (txn_id, tablet_id) with different |
524 | | // signatures being executed concurrently, we use _rowset_update_lock to serialize them |
525 | | mutable std::mutex _rowset_update_lock; |
526 | | |
527 | | // unused_rowsets, [start_version, end_version] |
528 | | std::mutex _gc_mutex; |
529 | | std::unordered_map<RowsetId, RowsetSharedPtr> _unused_rowsets; |
530 | | std::vector<std::pair<std::vector<RowsetId>, DeleteBitmapKeyRanges>> _unused_delete_bitmap; |
531 | | |
532 | | // for warm up states management |
533 | | struct RowsetWarmUpInfo { |
534 | | WarmUpState state; |
535 | | int64_t num_segments = 0; |
536 | | int64_t num_inverted_idx = 0; |
537 | | int64_t num_segments_warmed_up = 0; |
538 | | int64_t num_inverted_idx_warmed_up = 0; |
539 | | std::chrono::steady_clock::time_point start_tp; |
540 | | |
541 | 59.3k | void done(int64_t input_num_segments, int64_t input_num_inverted_idx) { |
542 | 59.3k | num_segments_warmed_up += input_num_segments; |
543 | 59.3k | num_inverted_idx_warmed_up += input_num_inverted_idx; |
544 | 59.3k | update_state(); |
545 | 59.3k | } |
546 | | |
547 | 59.3k | bool has_finished() const { |
548 | 59.3k | return (num_segments_warmed_up >= num_segments) && |
549 | 59.3k | (num_inverted_idx_warmed_up >= num_inverted_idx); |
550 | 59.3k | } |
551 | | |
552 | | void update_state(); |
553 | | }; |
554 | | std::unordered_map<RowsetId, RowsetWarmUpInfo> _rowset_warm_up_states; |
555 | | |
556 | | mutable std::shared_mutex _warmed_up_rowsets_mutex; |
557 | | std::unordered_set<RowsetId> _warmed_up_rowsets; |
558 | | |
559 | | // Cluster info for compaction read-write separation |
560 | | mutable std::shared_mutex _cluster_info_mutex; |
561 | | std::string _last_active_cluster_id; |
562 | | int64_t _last_active_time_ms {0}; |
563 | | int64_t _last_active_epoch {0}; |
564 | | |
565 | | // Map: version -> <rowset_meta, expiration_time> |
566 | | // Stores rowsets that have been notified by FE but not yet added to tablet meta |
567 | | // due to out-of-order notification or version discontinuity |
568 | | struct VisiblePendingRowset { |
569 | | const bool is_empty_rowset; |
570 | | const int64_t expiration_time; // seconds since epoch |
571 | | RowsetMetaSharedPtr rowset_meta; |
572 | | |
573 | | VisiblePendingRowset(RowsetMetaSharedPtr rowset_meta_, int64_t expiration_time_, |
574 | | bool is_empty_rowset_ = false) |
575 | 120k | : is_empty_rowset(is_empty_rowset_), |
576 | 120k | expiration_time(expiration_time_), |
577 | 120k | rowset_meta(std::move(rowset_meta_)) {} |
578 | | }; |
579 | | mutable std::mutex _visible_pending_rs_lock; |
580 | | std::map<int64_t, VisiblePendingRowset> _visible_pending_rs_map; |
581 | | }; |
582 | | |
583 | | using CloudTabletSPtr = std::shared_ptr<CloudTablet>; |
584 | | |
585 | | } // namespace doris |