Coverage Report

Created: 2026-05-28 07:32

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