Coverage Report

Created: 2026-11-03 12:08

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