Coverage Report

Created: 2026-03-15 08:11

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