Coverage Report

Created: 2026-08-07 11:46

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/storage/tablet/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 <butil/macros.h>
21
#include <glog/logging.h>
22
23
#include <atomic>
24
#include <cstddef>
25
#include <cstdint>
26
#include <functional>
27
#include <memory>
28
#include <mutex>
29
#include <ostream>
30
#include <set>
31
#include <shared_mutex>
32
#include <string>
33
#include <string_view>
34
#include <utility>
35
#include <vector>
36
37
#include "common/atomic_shared_ptr.h"
38
#include "common/config.h"
39
#include "common/metrics/metrics.h"
40
#include "common/status.h"
41
#include "gen_cpp/AgentService_types.h"
42
#include "storage/binlog.h"
43
#include "storage/binlog_config.h"
44
#include "storage/compaction/cumulative_compaction_binlog_policy.h"
45
#include "storage/data_dir.h"
46
#include "storage/olap_common.h"
47
#include "storage/partial_update_info.h"
48
#include "storage/rowset/rowset.h"
49
#include "storage/rowset/rowset_meta.h"
50
#include "storage/rowset/rowset_reader.h"
51
#include "storage/segment/segment.h"
52
#include "storage/segment/segment_loader.h"
53
#include "storage/tablet/base_tablet.h"
54
#include "storage/version_graph.h"
55
#include "util/once.h"
56
#include "util/slice.h"
57
58
namespace bvar {
59
template <typename T>
60
class Adder;
61
}
62
63
namespace doris {
64
65
class Tablet;
66
class CumulativeCompactionPolicy;
67
class CompactionMixin;
68
class RowsetWriter;
69
struct RowsetWriterContext;
70
class TTabletInfo;
71
class TabletMetaPB;
72
class TupleDescriptor;
73
class CalcDeleteBitmapToken;
74
enum CompressKind : int;
75
class RowsetBinlogMetasPB;
76
77
namespace io {
78
class RemoteFileSystem;
79
} // namespace io
80
class Block;
81
struct RowLocation;
82
enum KeysType : int;
83
enum SortType : int;
84
85
enum TabletStorageType { STORAGE_TYPE_LOCAL, STORAGE_TYPE_REMOTE, STORAGE_TYPE_REMOTE_AND_LOCAL };
86
87
extern bvar::Adder<uint64_t> unused_remote_rowset_num;
88
89
static inline constexpr auto TRACE_TABLET_LOCK_THRESHOLD = std::chrono::seconds(1);
90
91
struct WriteCooldownMetaExecutors {
92
    WriteCooldownMetaExecutors(size_t executor_nums = 5);
93
94
    void stop();
95
96
    void submit(TabletSharedPtr tablet);
97
165k
    size_t _get_executor_pos(int64_t tablet_id) const {
98
165k
        return std::hash<int64_t>()(tablet_id) % _executor_nums;
99
165k
    };
100
    // Each executor is a mpsc to ensure uploads of the same tablet meta are not concurrent
101
    // FIXME(AlexYue): Use mpsc instead of `ThreadPool` with 1 thread
102
    // We use PriorityThreadPool since it would call status inside it's `shutdown` function.
103
    // Consider one situation where the StackTraceCache's singleton is detructed before
104
    // this WriteCooldownMetaExecutors's singleton, then invoking the status would also call
105
    // StackTraceCache which would then result in heap use after free like #23834
106
    std::vector<std::unique_ptr<PriorityThreadPool>> _executors;
107
    std::unordered_set<int64_t> _pending_tablets;
108
    std::mutex _latch;
109
    size_t _executor_nums;
110
};
111
112
class Tablet final : public BaseTablet {
113
public:
114
    Tablet(StorageEngine& engine, TabletMetaSharedPtr tablet_meta, DataDir* data_dir,
115
           const std::string_view& cumulative_compaction_type = "");
116
117
36.4M
    DataDir* data_dir() const { return _data_dir; }
118
1.49M
    int64_t replica_id() const { return _tablet_meta->replica_id(); }
119
120
517k
    std::string tablet_path() const override { return _tablet_path; }
121
490k
    std::string get_rowset_path(const RowsetMetaSharedPtr& rowset_meta) const {
122
490k
        if (!rowset_meta->is_local()) {
123
14
            return "";
124
14
        }
125
490k
        return tablet_path();
126
490k
    }
127
128
    bool set_tablet_schema_into_rowset_meta();
129
    Status init();
130
    bool init_succeeded();
131
132
    bool is_used();
133
134
    void register_tablet_into_dir();
135
    void deregister_tablet_from_dir();
136
137
    void save_meta();
138
    // Used in clone task, to update local meta when finishing a clone job
139
    Status revise_tablet_meta(const std::vector<RowsetSharedPtr>& to_add,
140
                              const std::vector<RowsetSharedPtr>& to_delete,
141
                              bool is_incremental_clone);
142
143
    int64_t cumulative_layer_point() const;
144
    void set_cumulative_layer_point(int64_t new_point);
145
    inline int64_t cumulative_promotion_size() const;
146
    inline void set_cumulative_promotion_size(int64_t new_size);
147
148
    // Disk space occupied by tablet, contain local and remote.
149
    size_t tablet_footprint() override;
150
    // Local disk space occupied by tablet.
151
    size_t tablet_local_size();
152
    // Remote disk space occupied by tablet.
153
    size_t tablet_remote_size();
154
155
    size_t num_rows();
156
    size_t version_count() const;
157
    size_t stale_version_count() const;
158
    bool exceed_version_limit(int32_t limit) override;
159
    uint64_t segment_count() const;
160
    Version max_version() const;
161
    CumulativeCompactionPolicy* cumulative_compaction_policy();
162
163
    // properties encapsulated in TabletSchema
164
    SortType sort_type() const;
165
    size_t sort_col_num() const;
166
    size_t num_columns() const;
167
    size_t num_null_columns() const;
168
    size_t num_short_key_columns() const;
169
    size_t num_rows_per_row_block() const;
170
    double bloom_filter_fpp() const;
171
    size_t next_unique_id() const;
172
    int64_t avg_rs_meta_serialize_size() const;
173
174
    // operation in rowsets
175
    Status add_rowset(RowsetSharedPtr rowset);
176
    Status create_initial_rowset(const int64_t version);
177
178
    // MUST hold EXCLUSIVE `_meta_lock`.
179
    Status modify_rowsets(std::vector<RowsetSharedPtr>& to_add,
180
                          std::vector<RowsetSharedPtr>& to_delete, bool check_delete = false);
181
    bool rowset_exists_unlocked(const RowsetSharedPtr& rowset);
182
183
    // Add a committed rowset.
184
    Status add_inc_rowset(const RowsetSharedPtr& rowset);
185
    /// Delete stale rowset by timing. This delete policy uses now() minutes
186
    /// config::tablet_rowset_expired_stale_sweep_time_sec to compute the deadline of expired rowset
187
    /// to delete.  When rowset is deleted, it will be added to StorageEngine unused map and record
188
    /// need to delete flag.
189
    void delete_expired_stale_rowset();
190
191
    // if quiet is true, no error log will be printed if there are missing versions
192
    Status check_version_integrity(const Version& version, bool quiet = false);
193
    bool check_version_exist(const Version& version) const;
194
    void acquire_version_and_rowsets(
195
            std::vector<std::pair<Version, RowsetSharedPtr>>* version_rowsets) const;
196
197
    // If skip_missing_version is true, skip versions if they are missing.
198
    Status capture_rs_readers(const Version& spec_version, std::vector<RowSetSplits>* rs_splits,
199
                              const CaptureRowsetOps& opts) override;
200
201
    // Find the missed versions until the spec_version.
202
    //
203
    // for example:
204
    //     [0-4][5-5][8-8][9-9][14-14]
205
    // if spec_version = 12, it will return [6, 6], [7, 7], [10, 10], [11, 11], [12, 12]
206
    Versions calc_missed_versions(int64_t spec_version, Versions existing_versions) const override;
207
208
    // meta lock
209
222k
    BthreadSharedMutex& get_header_lock() { return _meta_lock; }
210
50.9k
    std::mutex& get_rowset_update_lock() { return _rowset_update_lock; }
211
48.0k
    std::mutex& get_push_lock() { return _ingest_lock; }
212
6.77M
    std::mutex& get_base_compaction_lock() { return _base_compaction_lock; }
213
9.01M
    std::mutex& get_cumulative_compaction_lock() { return _cumulative_compaction_lock; }
214
173
    std::shared_mutex& get_meta_store_lock() { return _meta_store_lock; }
215
216
95.8k
    std::shared_timed_mutex& get_migration_lock() { return _migration_lock; }
217
218
197
    std::mutex& get_build_inverted_index_lock() { return _build_inverted_index_lock; }
219
220
    // operation for compaction
221
    bool can_do_compaction(size_t path_hash, CompactionType compaction_type);
222
    bool suitable_for_compaction(
223
            CompactionType compaction_type,
224
            std::shared_ptr<CumulativeCompactionPolicy> cumulative_compaction_policy);
225
226
    uint32_t calc_compaction_score(CompactionType compaction_type);
227
228
    // This function to find max continuous version from the beginning.
229
    // For example: If there are 1, 2, 3, 5, 6, 7 versions belongs tablet, then 3 is target.
230
    // 3 will be saved in "version", and 7 will be saved in "max_version", if max_version != nullptr
231
    void max_continuous_version_from_beginning(Version* version, Version* max_version = nullptr);
232
233
0
    void set_bad(bool is_bad) { _is_bad = is_bad; }
234
235
16.9M
    int64_t last_cumu_compaction_failure_time() { return _last_cumu_compaction_failure_millis; }
236
270k
    void set_last_cumu_compaction_failure_time(int64_t millis) {
237
270k
        _last_cumu_compaction_failure_millis = millis;
238
270k
    }
239
240
6.70M
    int64_t last_base_compaction_failure_time() { return _last_base_compaction_failure_millis; }
241
144k
    void set_last_base_compaction_failure_time(int64_t millis) {
242
144k
        _last_base_compaction_failure_millis = millis;
243
144k
    }
244
245
0
    int64_t last_full_compaction_failure_time() { return _last_full_compaction_failure_millis; }
246
0
    void set_last_full_compaction_failure_time(int64_t millis) {
247
0
        _last_full_compaction_failure_millis = millis;
248
0
    }
249
250
260k
    int64_t last_cumu_compaction_success_time() { return _last_cumu_compaction_success_millis; }
251
10.5k
    void set_last_cumu_compaction_success_time(int64_t millis) {
252
10.5k
        _last_cumu_compaction_success_millis = millis;
253
10.5k
    }
254
255
260k
    int64_t last_base_compaction_success_time() { return _last_base_compaction_success_millis; }
256
6.00k
    void set_last_base_compaction_success_time(int64_t millis) {
257
6.00k
        _last_base_compaction_success_millis = millis;
258
6.00k
    }
259
260
0
    int64_t last_full_compaction_success_time() { return _last_full_compaction_success_millis; }
261
0
    void set_last_full_compaction_success_time(int64_t millis) {
262
0
        _last_full_compaction_success_millis = millis;
263
0
    }
264
265
0
    int64_t last_cumu_compaction_schedule_time() { return _last_cumu_compaction_schedule_millis; }
266
270k
    void set_last_cumu_compaction_schedule_time(int64_t millis) {
267
270k
        _last_cumu_compaction_schedule_millis = millis;
268
270k
    }
269
270
0
    int64_t last_base_compaction_schedule_time() { return _last_base_compaction_schedule_millis; }
271
72.1k
    void set_last_base_compaction_schedule_time(int64_t millis) {
272
72.1k
        _last_base_compaction_schedule_millis = millis;
273
72.1k
    }
274
275
0
    int64_t last_full_compaction_schedule_time() { return _last_full_compaction_schedule_millis; }
276
0
    void set_last_full_compaction_schedule_time(int64_t millis) {
277
0
        _last_full_compaction_schedule_millis = millis;
278
0
    }
279
280
    void delete_all_files();
281
282
    void check_tablet_path_exists();
283
284
    std::vector<RowsetSharedPtr> pick_candidate_rowsets_to_cumulative_compaction();
285
    // MUST hold shared `_meta_lock`. Use this when the caller already holds the
286
    // header lock (e.g. the time-series cumulative score path under
287
    // suitable_for_compaction) to avoid recursive shared acquisition.
288
    std::vector<RowsetSharedPtr> pick_candidate_rowsets_to_cumulative_compaction_unlocked();
289
    std::vector<RowsetSharedPtr> pick_candidate_rowsets_to_base_compaction();
290
    std::vector<RowsetSharedPtr> pick_candidate_rowsets_to_full_compaction();
291
    std::vector<RowsetSharedPtr> pick_candidate_rowsets_to_binlog_compaction();
292
    std::vector<RowsetSharedPtr> pick_candidate_rowsets_to_build_inverted_index(
293
            const std::set<int64_t>& alter_index_uids, bool is_drop_op);
294
295
    // used for single compaction to get the local versions
296
    // Single compaction does not require remote rowsets and cannot violate the cooldown semantics
297
    std::vector<Version> get_all_local_versions();
298
299
    void calculate_cumulative_point();
300
    // TODO(ygl):
301
0
    bool is_primary_replica() { return false; }
302
303
    // return true if the checkpoint is actually done
304
    bool do_tablet_meta_checkpoint();
305
306
    // Check whether the rowset is useful or not, unuseful rowset can be swept up then.
307
    // Rowset which is under tablet's management is useful, i.e. rowset is in
308
    // _rs_version_map, or _stale_rs_version_map.
309
    // Rowset whose version range is not covered by this tablet is also useful.
310
    bool rowset_meta_is_useful(RowsetMetaSharedPtr rowset_meta);
311
312
    void build_tablet_report_info(TTabletInfo* tablet_info,
313
                                  bool enable_consecutive_missing_check = false,
314
                                  bool enable_path_check = false);
315
316
    // return a json string to show the compaction status of this tablet
317
    void get_compaction_status(std::string* json_result);
318
319
    static Status prepare_compaction_and_calculate_permits(
320
            CompactionType compaction_type, const TabletSharedPtr& tablet,
321
            std::shared_ptr<CompactionMixin>& compaction, int64_t& permits);
322
323
    void execute_compaction(CompactionMixin& compaction);
324
325
    void set_cumulative_compaction_policy(
326
0
            std::shared_ptr<CumulativeCompactionPolicy> cumulative_compaction_policy) {
327
0
        _cumulative_compaction_policy = cumulative_compaction_policy;
328
0
    }
329
330
0
    std::shared_ptr<CumulativeCompactionPolicy> get_cumulative_compaction_policy() {
331
0
        return _cumulative_compaction_policy;
332
0
    }
333
334
270k
    void set_last_cumu_compaction_status(std::string status) {
335
270k
        _last_cumu_compaction_status = std::move(status);
336
270k
    }
337
338
0
    std::string get_last_cumu_compaction_status() { return _last_cumu_compaction_status; }
339
340
144k
    void set_last_base_compaction_status(std::string status) {
341
144k
        _last_base_compaction_status = std::move(status);
342
144k
    }
343
344
0
    std::string get_last_base_compaction_status() { return _last_base_compaction_status; }
345
346
0
    void set_last_full_compaction_status(std::string status) {
347
0
        _last_full_compaction_status = std::move(status);
348
0
    }
349
350
0
    std::string get_last_full_compaction_status() { return _last_full_compaction_status; }
351
352
    std::tuple<int64_t, int64_t> get_visible_version_and_time() const;
353
354
298k
    void set_visible_version(const std::shared_ptr<const VersionWithTime>& visible_version) {
355
298k
        _visible_version.store(visible_version);
356
298k
    }
357
358
0
    inline bool all_beta() const {
359
0
        std::shared_lock rdlock(_meta_lock);
360
0
        return _tablet_meta->all_beta();
361
0
    }
362
363
0
    const TabletSchemaSPtr& tablet_schema_unlocked() const { return _max_version_schema; }
364
365
    Result<std::unique_ptr<RowsetWriter>> create_rowset_writer(RowsetWriterContext& context,
366
                                                               bool vertical) override;
367
368
    Result<std::unique_ptr<RowsetWriter>> create_transient_rowset_writer(
369
            const Rowset& rowset, std::shared_ptr<PartialUpdateInfo> partial_update_info,
370
            int64_t txn_expiration = 0) override;
371
    Result<std::unique_ptr<RowsetWriter>> create_transient_rowset_writer(
372
            RowsetWriterContext& context, const RowsetId& rowset_id);
373
374
    Status create_rowset(const RowsetMetaSharedPtr& rowset_meta, RowsetSharedPtr* rowset);
375
376
    // MUST hold EXCLUSIVE `_meta_lock`
377
    void add_rowsets(const std::vector<RowsetSharedPtr>& to_add);
378
    // MUST hold EXCLUSIVE `_meta_lock`
379
    Status delete_rowsets(const std::vector<RowsetSharedPtr>& to_delete, bool move_to_stale);
380
381
    // MUST hold SHARED `_meta_lock`
382
11.8k
    const auto& rowset_map() const { return _rs_version_map; }
383
    // MUST hold SHARED `_meta_lock`
384
11.8k
    const auto& stale_rowset_map() const { return _stale_rs_version_map; }
385
    ////////////////////////////////////////////////////////////////////////////
386
    // begin cooldown functions
387
    ////////////////////////////////////////////////////////////////////////////
388
1.12M
    int64_t storage_policy_id() const { return _tablet_meta->storage_policy_id(); }
389
    void set_storage_policy_id(int64_t id) { _tablet_meta->set_storage_policy_id(id); }
390
391
4.33M
    int64_t last_failed_follow_cooldown_time() const { return _last_failed_follow_cooldown_time; }
392
393
    // Cooldown to remote fs.
394
    Status cooldown(RowsetSharedPtr rowset = nullptr);
395
396
    RowsetSharedPtr pick_cooldown_rowset();
397
398
    RowsetSharedPtr need_cooldown(int64_t* cooldown_timestamp, size_t* file_size);
399
400
    struct CooldownConf {
401
        int64_t term = -1;
402
        int64_t cooldown_replica_id = -1;
403
    };
404
405
7
    CooldownConf cooldown_conf() const {
406
7
        std::shared_lock rlock(_cooldown_conf_lock);
407
7
        return _cooldown_conf;
408
7
    }
409
410
21
    CooldownConf cooldown_conf_unlocked() const { return _cooldown_conf; }
411
412
    // Return `true` if update success
413
    bool update_cooldown_conf(int64_t cooldown_term, int64_t cooldown_replica_id);
414
415
    Status remove_all_remote_rowsets();
416
417
    void record_unused_remote_rowset(const RowsetId& rowset_id, const std::string& resource,
418
                                     int64_t num_segments);
419
420
    uint32_t calc_cold_data_compaction_score() const;
421
422
197
    std::mutex& get_cold_compaction_lock() { return _cold_compaction_lock; }
423
424
0
    std::shared_mutex& get_cooldown_conf_lock() { return _cooldown_conf_lock; }
425
426
    static void async_write_cooldown_meta(TabletSharedPtr tablet);
427
    // Return `ABORTED` if should not to retry again
428
    Status write_cooldown_meta();
429
    ////////////////////////////////////////////////////////////////////////////
430
    // end cooldown functions
431
    ////////////////////////////////////////////////////////////////////////////
432
433
    CalcDeleteBitmapExecutor* calc_delete_bitmap_executor() override;
434
    Status save_delete_bitmap(const TabletTxnInfo* txn_info, int64_t txn_id,
435
                              DeleteBitmapPtr delete_bitmap, RowsetWriter* rowset_writer,
436
                              const RowsetIdUnorderedSet& cur_rowset_ids, int64_t lock_id = -1,
437
                              int64_t next_visible_version = -1) override;
438
439
    void merge_delete_bitmap(const DeleteBitmap& delete_bitmap);
440
    bool check_all_rowset_segment();
441
442
    void update_max_version_schema(const TabletSchemaSPtr& tablet_schema);
443
444
    void set_skip_compaction(bool skip,
445
                             CompactionType compaction_type = CompactionType::CUMULATIVE_COMPACTION,
446
                             int64_t start = -1);
447
    bool should_skip_compaction(CompactionType compaction_type, int64_t now);
448
449
    std::vector<std::string> get_binlog_filepath(std::string_view binlog_version) const;
450
    std::pair<std::string, int64_t> get_binlog_info(std::string_view binlog_version) const;
451
    std::string get_rowset_binlog_meta(std::string_view binlog_version,
452
                                       std::string_view rowset_id) const;
453
    Status get_rowset_binlog_metas(const std::vector<int64_t>& binlog_versions,
454
                                   RowsetBinlogMetasPB* metas_pb);
455
    Status get_rowset_binlog_metas(Version binlog_versions, RowsetBinlogMetasPB* metas_pb);
456
    std::string get_segment_filepath(std::string_view rowset_id,
457
                                     std::string_view segment_index) const;
458
    std::string get_segment_filepath(std::string_view rowset_id, int64_t segment_index) const;
459
    bool can_add_binlog(uint64_t total_binlog_size) const;
460
    void gc_binlogs(int64_t version);
461
    Status ingest_binlog_metas(RowsetBinlogMetasPB* metas_pb);
462
463
    void report_error(const Status& st);
464
465
0
    inline int64_t get_io_error_times() const { return _io_error_times; }
466
467
1.47M
    inline bool is_io_error_too_times() const {
468
1.47M
        return config::max_tablet_io_errors > 0 && _io_error_times >= config::max_tablet_io_errors;
469
1.47M
    }
470
471
52.1k
    int64_t get_table_id() { return _tablet_meta->table_id(); }
472
473
    // binlog related functions
474
94.9k
    bool enable_binlog() const {
475
94.9k
        return config::enable_feature_binlog && _tablet_meta->binlog_config().is_enable();
476
94.9k
    }
477
95.0k
    bool enable_ccr_binlog() const {
478
95.0k
        return enable_binlog() && _tablet_meta->binlog_config().is_ccr_binlog_format();
479
95.0k
    }
480
0
    bool enable_row_binlog() const {
481
0
        return _tablet_meta->binlog_config().is_enable() &&
482
0
               _tablet_meta->binlog_config().is_row_binlog_format();
483
0
    }
484
0
    int64_t binlog_ttl_ms() const { return _tablet_meta->binlog_config().ttl_seconds(); }
485
0
    int64_t binlog_max_bytes() const { return _tablet_meta->binlog_config().max_bytes(); }
486
487
    void set_binlog_config(BinlogConfig binlog_config);
488
489
2
    void set_is_full_compaction_running(bool is_full_compaction_running) {
490
2
        _is_full_compaction_running = is_full_compaction_running;
491
2
    }
492
0
    inline bool is_full_compaction_running() const { return _is_full_compaction_running; }
493
    void clear_cache() override;
494
495
    int32_t get_compaction_score() const { return _compaction_score; }
496
497
0
    void set_compaction_score(int32_t compaction_score) { _compaction_score = compaction_score; }
498
499
68.4k
    void add_compaction_score(int32_t score) {
500
68.4k
        if (_compaction_score < 0) {
501
22.6k
            return;
502
22.6k
        }
503
45.8k
        _compaction_score += score;
504
45.8k
    }
505
506
0
    void minus_compaction_score(int32_t score) {
507
0
        if (_compaction_score < 0) {
508
0
            return;
509
0
        }
510
0
        _compaction_score -= score;
511
0
    }
512
513
    Status prepare_txn(TPartitionId partition_id, TTransactionId transaction_id,
514
                       const PUniqueId& load_id, bool ingest);
515
    // TODO: commit_txn
516
517
private:
518
    Status _init_once_action();
519
    bool _contains_rowset(const RowsetId rowset_id);
520
    Status _contains_version(const Version& version);
521
    // Returns:
522
    // version: the max continuous version from beginning
523
    // max_version: the max version of this tablet
524
    void _max_continuous_version_from_beginning_unlocked(Version* version, Version* max_version,
525
                                                         bool* has_version_cross) const;
526
    RowsetSharedPtr _rowset_with_largest_size();
527
    /// Delete stale rowset by version. This method not only delete the version in expired rowset map,
528
    /// but also delete the version in rowset meta vector.
529
    void _delete_stale_rowset_by_version(const Version& version);
530
531
    uint32_t _calc_cumulative_compaction_score(
532
            std::shared_ptr<CumulativeCompactionPolicy> cumulative_compaction_policy);
533
    uint32_t _calc_base_compaction_score() const;
534
535
    std::vector<RowsetSharedPtr> _pick_visible_rowsets_to_compaction(int64_t min_start_version,
536
                                                                     int64_t max_start_version);
537
    // MUST hold shared `_meta_lock`.
538
    std::vector<RowsetSharedPtr> _pick_visible_rowsets_to_compaction_unlocked(
539
            int64_t min_start_version, int64_t max_start_version);
540
541
    void _init_context_common_fields(RowsetWriterContext& context);
542
543
    ////////////////////////////////////////////////////////////////////////////
544
    // begin cooldown functions
545
    ////////////////////////////////////////////////////////////////////////////
546
    Status _cooldown_data(RowsetSharedPtr rowset);
547
    Status _follow_cooldowned_data();
548
    Status _read_cooldown_meta(const StorageResource& storage_resource,
549
                               TabletMetaPB* tablet_meta_pb);
550
    bool _has_data_to_cooldown();
551
    int64_t _get_newest_cooldown_time(const RowsetSharedPtr& rowset);
552
    ////////////////////////////////////////////////////////////////////////////
553
    // end cooldown functions
554
    ////////////////////////////////////////////////////////////////////////////
555
556
    void _clear_cache_by_rowset(const BetaRowsetSharedPtr& rowset);
557
    void check_table_size_correctness();
558
    std::string get_segment_path(const RowsetMetaSharedPtr& rs_meta, int64_t seg_id);
559
    int64_t get_segment_file_size(const RowsetMetaSharedPtr& rs_meta);
560
    int64_t get_inverted_index_file_size(const RowsetMetaSharedPtr& rs_meta);
561
562
public:
563
    static const int64_t K_INVALID_CUMULATIVE_POINT = -1;
564
565
private:
566
    StorageEngine& _engine;
567
    DataDir* _data_dir = nullptr;
568
569
    std::string _tablet_path;
570
571
    DorisCallOnce<Status> _init_once;
572
    // meta store lock is used for prevent 2 threads do checkpoint concurrently
573
    // it will be used in econ-mode in the future
574
    std::shared_mutex _meta_store_lock;
575
    std::mutex _ingest_lock;
576
    std::mutex _base_compaction_lock;
577
    std::mutex _cumulative_compaction_lock;
578
    std::shared_timed_mutex _migration_lock;
579
    std::mutex _build_inverted_index_lock;
580
581
    // In unique key table with MoW, we should guarantee that only one
582
    // writer can update rowset and delete bitmap at the same time.
583
    // We use a separate lock rather than _meta_lock, to avoid blocking read queries
584
    // during publish_txn, which might take hundreds of milliseconds
585
    mutable std::mutex _rowset_update_lock;
586
587
    // if this tablet is broken, set to true. default is false
588
    std::atomic<bool> _is_bad;
589
    // timestamp of last cumu compaction failure
590
    std::atomic<int64_t> _last_cumu_compaction_failure_millis;
591
    // timestamp of last base compaction failure
592
    std::atomic<int64_t> _last_base_compaction_failure_millis;
593
    // timestamp of last full compaction failure
594
    std::atomic<int64_t> _last_full_compaction_failure_millis;
595
    // timestamp of last cumu compaction success
596
    std::atomic<int64_t> _last_cumu_compaction_success_millis;
597
    // timestamp of last base compaction success
598
    std::atomic<int64_t> _last_base_compaction_success_millis;
599
    // timestamp of last full compaction success
600
    std::atomic<int64_t> _last_full_compaction_success_millis;
601
    // timestamp of last cumu compaction schedule time
602
    std::atomic<int64_t> _last_cumu_compaction_schedule_millis;
603
    // timestamp of last base compaction schedule time
604
    std::atomic<int64_t> _last_base_compaction_schedule_millis;
605
    // timestamp of last full compaction schedule time
606
    std::atomic<int64_t> _last_full_compaction_schedule_millis;
607
    std::atomic<int64_t> _cumulative_point;
608
    std::atomic<int64_t> _cumulative_promotion_size;
609
    std::atomic<int32_t> _newly_created_rowset_num;
610
    std::atomic<int64_t> _last_checkpoint_time;
611
    std::string _last_cumu_compaction_status;
612
    std::string _last_base_compaction_status;
613
    std::string _last_full_compaction_status;
614
615
    // cumulative compaction policy
616
    std::shared_ptr<CumulativeCompactionPolicy> _cumulative_compaction_policy;
617
    std::string_view _cumulative_compaction_type;
618
619
    // use a separate thread to check all tablets paths existence
620
    std::atomic<bool> _is_tablet_path_exists;
621
622
    int64_t _last_missed_version;
623
    int64_t _last_missed_time_s;
624
625
    bool _skip_cumu_compaction = false;
626
    int64_t _skip_cumu_compaction_ts;
627
628
    bool _skip_base_compaction = false;
629
    int64_t _skip_base_compaction_ts;
630
631
    bool _skip_binlog_compaction = false;
632
    int64_t _skip_binlog_compaction_ts;
633
634
    // cooldown related
635
    CooldownConf _cooldown_conf;
636
    // `_cooldown_conf_lock` is used to serialize update cooldown conf and all operations that:
637
    // 1. read cooldown conf
638
    // 2. upload rowsets to remote storage
639
    // 3. update cooldown meta id
640
    mutable std::shared_mutex _cooldown_conf_lock;
641
    // `_cold_compaction_lock` is used to serialize cold data compaction and all operations that
642
    // may delete compaction input rowsets.
643
    std::mutex _cold_compaction_lock;
644
    int64_t _last_failed_follow_cooldown_time = 0;
645
646
    int64_t _io_error_times = 0;
647
648
    // partition's visible version. it sync from fe, but not real-time.
649
    atomic_shared_ptr<const VersionWithTime> _visible_version;
650
651
    std::atomic_bool _is_full_compaction_running = false;
652
653
    int32_t _compaction_score = -1;
654
    int32_t _score_check_cnt = 0;
655
};
656
657
278k
inline CumulativeCompactionPolicy* Tablet::cumulative_compaction_policy() {
658
278k
    return _cumulative_compaction_policy.get();
659
278k
}
660
661
17.5M
inline bool Tablet::init_succeeded() {
662
17.5M
    return _init_once.has_called() && _init_once.stored_result().ok();
663
17.5M
}
664
665
19.0M
inline bool Tablet::is_used() {
666
19.0M
    return !_is_bad && _data_dir->is_used();
667
19.0M
}
668
669
292k
inline void Tablet::register_tablet_into_dir() {
670
292k
    _data_dir->register_tablet(this);
671
292k
}
672
673
6.04k
inline void Tablet::deregister_tablet_from_dir() {
674
6.04k
    _data_dir->deregister_tablet(this);
675
6.04k
}
676
677
1.44M
inline int64_t Tablet::cumulative_layer_point() const {
678
1.44M
    return _cumulative_point;
679
1.44M
}
680
681
10.4k
inline void Tablet::set_cumulative_layer_point(int64_t new_point) {
682
    // cumulative point should only be reset to -1, or be increased
683
18.4E
    CHECK(new_point == Tablet::K_INVALID_CUMULATIVE_POINT || new_point >= _cumulative_point)
684
18.4E
            << "Unexpected cumulative point: " << new_point
685
18.4E
            << ", origin: " << _cumulative_point.load();
686
10.4k
    _cumulative_point = new_point;
687
10.4k
}
688
689
269k
inline int64_t Tablet::cumulative_promotion_size() const {
690
269k
    return _cumulative_promotion_size;
691
269k
}
692
693
438k
inline void Tablet::set_cumulative_promotion_size(int64_t new_size) {
694
438k
    _cumulative_promotion_size = new_size;
695
438k
}
696
697
// TODO(lingbin): Why other methods that need to get information from _tablet_meta
698
// are not locked, here needs a comment to explain.
699
47.2k
inline size_t Tablet::tablet_footprint() {
700
47.2k
    std::shared_lock rdlock(_meta_lock);
701
47.2k
    return _tablet_meta->tablet_footprint();
702
47.2k
}
703
704
3.47M
inline size_t Tablet::tablet_local_size() {
705
3.47M
    std::shared_lock rdlock(_meta_lock);
706
3.47M
    return _tablet_meta->tablet_local_size();
707
3.47M
}
708
709
3.47M
inline size_t Tablet::tablet_remote_size() {
710
3.47M
    std::shared_lock rdlock(_meta_lock);
711
3.47M
    return _tablet_meta->tablet_remote_size();
712
3.47M
}
713
714
// TODO(lingbin): Why other methods which need to get information from _tablet_meta
715
// are not locked, here needs a comment to explain.
716
inline size_t Tablet::num_rows() {
717
    std::shared_lock rdlock(_meta_lock);
718
    return _tablet_meta->num_rows();
719
}
720
721
5.48M
inline size_t Tablet::version_count() const {
722
5.48M
    std::shared_lock rdlock(_meta_lock);
723
5.48M
    return _tablet_meta->version_count();
724
5.48M
}
725
726
47.9k
inline size_t Tablet::stale_version_count() const {
727
47.9k
    std::shared_lock rdlock(_meta_lock);
728
47.9k
    return _tablet_meta->stale_version_count();
729
47.9k
}
730
731
556k
inline Version Tablet::max_version() const {
732
556k
    std::shared_lock rdlock(_meta_lock);
733
556k
    return _tablet_meta->max_version();
734
556k
}
735
736
5.39M
inline uint64_t Tablet::segment_count() const {
737
5.39M
    std::shared_lock rdlock(_meta_lock);
738
5.39M
    uint64_t segment_nums = 0;
739
9.83M
    for (const auto& [_, rs_meta] : _tablet_meta->all_rs_metas()) {
740
9.83M
        segment_nums += rs_meta->num_segments();
741
9.83M
    }
742
5.39M
    return segment_nums;
743
5.39M
}
744
745
0
inline SortType Tablet::sort_type() const {
746
0
    return _tablet_meta->tablet_schema()->sort_type();
747
0
}
748
749
0
inline size_t Tablet::sort_col_num() const {
750
0
    return _tablet_meta->tablet_schema()->sort_col_num();
751
0
}
752
753
0
inline size_t Tablet::num_columns() const {
754
0
    return _tablet_meta->tablet_schema()->num_columns();
755
0
}
756
757
0
inline size_t Tablet::num_null_columns() const {
758
0
    return _tablet_meta->tablet_schema()->num_null_columns();
759
0
}
760
761
0
inline size_t Tablet::num_short_key_columns() const {
762
0
    return _tablet_meta->tablet_schema()->num_short_key_columns();
763
0
}
764
765
0
inline size_t Tablet::num_rows_per_row_block() const {
766
0
    return _tablet_meta->tablet_schema()->num_rows_per_row_block();
767
0
}
768
769
0
inline double Tablet::bloom_filter_fpp() const {
770
0
    return _tablet_meta->tablet_schema()->bloom_filter_fpp();
771
0
}
772
773
20
inline size_t Tablet::next_unique_id() const {
774
20
    return _tablet_meta->tablet_schema()->next_column_unique_id();
775
20
}
776
777
47.9k
inline int64_t Tablet::avg_rs_meta_serialize_size() const {
778
47.9k
    return _tablet_meta->avg_rs_meta_serialize_size();
779
47.9k
}
780
781
} // namespace doris