Coverage Report

Created: 2026-06-04 05:19

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