Coverage Report

Created: 2026-04-14 03:58

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