Coverage Report

Created: 2026-09-18 10:56

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/storage/tablet/tablet_meta.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 <gen_cpp/AgentService_types.h>
21
#include <gen_cpp/FrontendService_types.h>
22
#include <gen_cpp/olap_file.pb.h>
23
#include <stdint.h>
24
25
#include <atomic>
26
#include <cstddef>
27
#include <limits>
28
#include <map>
29
#include <memory>
30
#include <mutex>
31
#include <optional>
32
#include <ostream>
33
#include <roaring/roaring.hh>
34
#include <shared_mutex>
35
#include <string>
36
#include <tuple>
37
#include <unordered_map>
38
#include <utility>
39
#include <vector>
40
41
#include "common/logging.h"
42
#include "common/status.h"
43
#include "io/fs/file_system.h"
44
#include "runtime/memory/lru_cache_policy.h"
45
#include "storage/binlog_config.h"
46
#include "storage/metadata_adder.h"
47
#include "storage/olap_common.h"
48
#include "storage/rowset/rowset_meta.h"
49
#include "storage/tablet/tablet_schema.h"
50
#include "util/lru_cache.h"
51
#include "util/uid_util.h"
52
53
namespace json2pb {
54
struct Pb2JsonOptions;
55
} // namespace json2pb
56
57
namespace doris {
58
class TColumn;
59
60
// Lifecycle states that a Tablet can be in. Legal state transitions for a
61
// Tablet object:
62
//
63
//   NOTREADY -> RUNNING -> TOMBSTONED -> STOPPED -> SHUTDOWN
64
//      |           |            |          ^^^
65
//      |           |            +----------++|
66
//      |           +------------------------+|
67
//      +-------------------------------------+
68
69
enum TabletState {
70
    // Tablet is under alter table, rollup, clone
71
    TABLET_NOTREADY,
72
73
    TABLET_RUNNING,
74
75
    // Tablet integrity has been violated, such as missing versions.
76
    // In this state, tablet will not accept any incoming request.
77
    // Report this state to FE, scheduling BE to drop tablet.
78
    TABLET_TOMBSTONED,
79
80
    // Tablet is shutting down, files in disk still remained.
81
    TABLET_STOPPED,
82
83
    // Files have been removed, tablet has been shutdown completely.
84
    TABLET_SHUTDOWN
85
};
86
87
class DataDir;
88
class TabletMeta;
89
class DeleteBitmap;
90
class TBinlogConfig;
91
92
// Class encapsulates meta of tablet.
93
// The concurrency control is handled in Tablet Class, not in this class.
94
class TabletMeta : public MetadataAdder<TabletMeta> {
95
public:
96
    static TabletMetaSharedPtr create(
97
            const TCreateTabletReq& request, const TabletUid& tablet_uid, uint64_t shard_id,
98
            uint32_t next_unique_id,
99
            const std::unordered_map<uint32_t, uint32_t>& col_ordinal_to_unique_id);
100
101
    TabletMeta();
102
    ~TabletMeta() override;
103
    TabletMeta(int64_t table_id, int64_t partition_id, int64_t tablet_id, int64_t replica_id,
104
               int32_t schema_hash, int32_t shard_id, const TTabletSchema& tablet_schema,
105
               uint32_t next_unique_id,
106
               const std::unordered_map<uint32_t, uint32_t>& col_ordinal_to_unique_id,
107
               TabletUid tablet_uid, TTabletType::type tabletType,
108
               TCompressionType::type compression_type, int64_t storage_policy_id = 0,
109
               bool enable_unique_key_merge_on_write = false,
110
               std::optional<TBinlogConfig> binlog_config = {},
111
               std::string compaction_policy = "size_based",
112
               int64_t time_series_compaction_goal_size_mbytes = 1024,
113
               int64_t time_series_compaction_file_count_threshold = 1000,
114
               int64_t time_series_compaction_time_threshold_seconds = 3600,
115
               int64_t time_series_compaction_empty_rowsets_threshold = 5,
116
               int64_t time_series_compaction_level_threshold = 1,
117
               TInvertedIndexFileStorageFormat::type inverted_index_file_storage_format =
118
                       TInvertedIndexFileStorageFormat::V2,
119
               TEncryptionAlgorithm::type tde_algorithm = TEncryptionAlgorithm::PLAINTEXT,
120
               TStorageFormat::type storage_format = TStorageFormat::V2,
121
               int32_t vertical_compaction_num_columns_per_group = 5,
122
               TTabletRole::type tablet_role = TTabletRole::TABLET_ROLE_DATA);
123
    // If need add a filed in TableMeta, filed init copy in copy construct function
124
    TabletMeta(const TabletMeta& tablet_meta);
125
    TabletMeta(TabletMeta&& tablet_meta) = delete;
126
127
// UT
128
#ifdef BE_TEST
129
    TabletMeta(TabletSchemaSPtr tablet_schema) : _schema(tablet_schema) {}
130
#endif
131
132
    // Function create_from_file is used to be compatible with previous tablet_meta.
133
    // Previous tablet_meta is a physical file in tablet dir, which is not stored in rocksdb.
134
    Status create_from_file(const std::string& file_path);
135
    // Used to create tablet meta from memory buffer.
136
    Status create_from_buffer(const uint8_t* buffer, size_t buffer_size);
137
    static Status load_from_file(const std::string& file_path, TabletMetaPB* tablet_meta_pb);
138
    Status save(const std::string& file_path);
139
    Status save_as_json(const std::string& file_path);
140
    static Status save(const std::string& file_path, const TabletMetaPB& tablet_meta_pb);
141
    static std::string construct_header_file_path(const std::string& schema_hash_path,
142
                                                  int64_t tablet_id);
143
    Status save_meta(DataDir* data_dir);
144
145
    void serialize(std::string* meta_binary);
146
    Status deserialize(std::string_view meta_binary);
147
    void init_from_pb(const TabletMetaPB& tablet_meta_pb);
148
149
    void to_meta_pb(TabletMetaPB* tablet_meta_pb, bool cloud_get_rowset_meta);
150
    void to_json(std::string* json_string, json2pb::Pb2JsonOptions& options);
151
481k
    size_t tablet_columns_num() const { return _schema->num_columns(); }
152
153
0
    TabletTypePB tablet_type() const { return _tablet_type; }
154
    TabletUid tablet_uid() const;
155
102
    void set_tablet_uid(TabletUid uid) { _tablet_uid = uid; }
156
    int64_t table_id() const;
157
    int64_t index_id() const;
158
    int64_t partition_id() const;
159
    int64_t tablet_id() const;
160
    int64_t replica_id() const;
161
0
    void set_replica_id(int64_t replica_id) { _replica_id = replica_id; }
162
    int32_t schema_hash() const;
163
    int32_t shard_id() const;
164
    void set_shard_id(int32_t shard_id);
165
    int64_t creation_time() const;
166
    void set_creation_time(int64_t creation_time);
167
    int64_t cumulative_layer_point() const;
168
    void set_cumulative_layer_point(int64_t new_point);
169
170
    size_t num_rows() const;
171
    // Disk space occupied by tablet, contain local and remote.
172
    size_t tablet_footprint() const;
173
    // Local disk space occupied by tablet.
174
    size_t tablet_local_size() const;
175
    // Remote disk space occupied by tablet.
176
    size_t tablet_remote_size() const;
177
    size_t tablet_local_index_size() const;
178
    size_t tablet_local_segment_size() const;
179
    size_t tablet_remote_index_size() const;
180
    size_t tablet_remote_segment_size() const;
181
182
    size_t version_count() const;
183
    size_t stale_version_count() const;
184
    size_t version_count_cross_with_range(const Version& range) const;
185
    Version max_version() const;
186
187
    TabletState tablet_state() const;
188
    void set_tablet_state(TabletState state);
189
190
    bool in_restore_mode() const;
191
    void set_in_restore_mode(bool in_restore_mode);
192
193
    const TabletSchemaSPtr& tablet_schema() const;
194
195
    TabletSchema* mutable_tablet_schema();
196
197
    const RowsetMetaMapContainer& all_rs_metas() const;
198
    RowsetMetaMapContainer& all_mutable_rs_metas();
199
    Status add_rs_meta(const RowsetMetaSharedPtr& rs_meta);
200
    void delete_rs_meta_by_version(const Version& version,
201
                                   std::vector<RowsetMetaSharedPtr>* deleted_rs_metas);
202
    // If same_version is true, the rowset in "to_delete" will not be added
203
    // to _stale_rs_meta, but to be deleted from rs_meta directly.
204
    void modify_rs_metas(const std::vector<RowsetMetaSharedPtr>& to_add,
205
                         const std::vector<RowsetMetaSharedPtr>& to_delete,
206
                         bool same_version = false);
207
    void revise_rs_metas(std::vector<RowsetMetaSharedPtr>&& rs_metas);
208
    void revise_delete_bitmap_unlocked(const DeleteBitmap& delete_bitmap);
209
210
    const RowsetMetaMapContainer& all_stale_rs_metas() const;
211
    RowsetMetaSharedPtr acquire_rs_meta_by_version(const Version& version) const;
212
    void delete_stale_rs_meta_by_version(const Version& version);
213
    RowsetMetaSharedPtr acquire_stale_rs_meta_by_version(const Version& version) const;
214
215
    Status set_partition_id(int64_t partition_id);
216
217
770
    RowsetTypePB preferred_rowset_type() const { return _preferred_rowset_type; }
218
219
113
    void set_preferred_rowset_type(RowsetTypePB preferred_rowset_type) {
220
113
        _preferred_rowset_type = preferred_rowset_type;
221
113
    }
222
223
    // used for after tablet cloned to clear stale rowset
224
    void clear_stale_rowset();
225
226
    // Clear stale rowset metadata without changing the delete bitmap cache.
227
    void clear_stale_rs_metas();
228
229
    void clear_rowsets();
230
231
    // MUST hold EXCLUSIVE `_meta_lock` in belonged Tablet
232
    // `to_add` MUST NOT have overlapped version with `_rs_metas` in tablet meta.
233
    void add_rowsets_unchecked(const std::vector<RowsetSharedPtr>& to_add);
234
235
    bool all_beta() const;
236
237
2.28M
    int64_t storage_policy_id() const { return _storage_policy_id; }
238
239
4
    void set_storage_policy_id(int64_t id) {
240
4
        VLOG_NOTICE << "set tablet_id : " << _table_id << " storage policy from "
241
4
                    << _storage_policy_id << " to " << id;
242
4
        _storage_policy_id = id;
243
4
    }
244
245
3.19M
    UniqueId cooldown_meta_id() const { return _cooldown_meta_id; }
246
5
    void set_cooldown_meta_id(UniqueId uid) { _cooldown_meta_id = uid; }
247
248
    static void init_column_from_tcolumn(uint32_t unique_id, const TColumn& tcolumn,
249
                                         ColumnPB* column);
250
251
    struct SchemaCreateOptions {
252
        const std::unordered_map<uint32_t, uint32_t>& col_ordinal_to_unique_id;
253
        TCompressionType::type compression_type;
254
        TInvertedIndexFileStorageFormat::type inverted_index_file_storage_format;
255
        uint32_t next_unique_id;
256
    };
257
258
    static void init_schema_from_thrift(const TTabletSchema& tablet_schema,
259
                                        const SchemaCreateOptions& schema_create_options,
260
                                        TabletSchemaPB* tablet_schema_pb);
261
262
2.74M
    DeleteBitmapPtr delete_bitmap_ptr() { return _delete_bitmap; }
263
274k
    DeleteBitmap& delete_bitmap() { return *_delete_bitmap; }
264
265
    void remove_rowset_delete_bitmap(const RowsetId& rowset_id, const Version& version);
266
267
5.80M
    bool enable_unique_key_merge_on_write() const { return _enable_unique_key_merge_on_write; }
268
#ifdef BE_TEST
269
    void set_enable_unique_key_merge_on_write(bool value) {
270
        _enable_unique_key_merge_on_write = value;
271
    }
272
#endif
273
    // TODO(Drogon): thread safety
274
242k
    const BinlogConfig& binlog_config() const { return _binlog_config; }
275
2
    void set_binlog_config(BinlogConfig binlog_config) {
276
2
        _binlog_config = std::move(binlog_config);
277
2
    }
278
0
    TabletRolePB tablet_role() const { return _tablet_role; }
279
2.81G
    bool is_row_binlog_tablet() const {
280
2.81G
        return _tablet_role == TabletRolePB::TABLET_ROLE_ROW_BINLOG;
281
2.81G
    }
282
    void set_tablet_role(TabletRolePB tablet_role) { _tablet_role = tablet_role; }
283
2.91k
    int64_t binlog_tablet_id() const { return _binlog_tablet_id; }
284
4
    void set_binlog_tablet_id(int64_t binlog_tablet_id) { _binlog_tablet_id = binlog_tablet_id; }
285
286
37
    void set_compaction_policy(std::string compaction_policy) {
287
37
        _compaction_policy = compaction_policy;
288
37
    }
289
940M
    std::string compaction_policy() const { return _compaction_policy; }
290
14
    void set_time_series_compaction_goal_size_mbytes(int64_t goal_size_mbytes) {
291
14
        _time_series_compaction_goal_size_mbytes = goal_size_mbytes;
292
14
    }
293
245k
    int64_t time_series_compaction_goal_size_mbytes() const {
294
245k
        return _time_series_compaction_goal_size_mbytes;
295
245k
    }
296
14
    void set_time_series_compaction_file_count_threshold(int64_t file_count_threshold) {
297
14
        _time_series_compaction_file_count_threshold = file_count_threshold;
298
14
    }
299
245k
    int64_t time_series_compaction_file_count_threshold() const {
300
245k
        return _time_series_compaction_file_count_threshold;
301
245k
    }
302
15
    void set_time_series_compaction_time_threshold_seconds(int64_t time_threshold) {
303
15
        _time_series_compaction_time_threshold_seconds = time_threshold;
304
15
    }
305
245k
    int64_t time_series_compaction_time_threshold_seconds() const {
306
245k
        return _time_series_compaction_time_threshold_seconds;
307
245k
    }
308
2
    void set_time_series_compaction_empty_rowsets_threshold(int64_t empty_rowsets_threshold) {
309
2
        _time_series_compaction_empty_rowsets_threshold = empty_rowsets_threshold;
310
2
    }
311
245k
    int64_t time_series_compaction_empty_rowsets_threshold() const {
312
245k
        return _time_series_compaction_empty_rowsets_threshold;
313
245k
    }
314
4
    void set_time_series_compaction_level_threshold(int64_t level_threshold) {
315
4
        _time_series_compaction_level_threshold = level_threshold;
316
4
    }
317
245k
    int64_t time_series_compaction_level_threshold() const {
318
245k
        return _time_series_compaction_level_threshold;
319
245k
    }
320
321
2
    void set_vertical_compaction_num_columns_per_group(int32_t num) {
322
2
        _vertical_compaction_num_columns_per_group = num;
323
2
    }
324
255k
    int32_t vertical_compaction_num_columns_per_group() const {
325
255k
        return _vertical_compaction_num_columns_per_group;
326
255k
    }
327
328
1.54M
    int64_t ttl_seconds() const {
329
1.54M
        std::shared_lock rlock(_meta_lock);
330
1.54M
        return _ttl_seconds;
331
1.54M
    }
332
333
33
    void set_ttl_seconds(int64_t ttl_seconds) {
334
33
        std::lock_guard wlock(_meta_lock);
335
33
        _ttl_seconds = ttl_seconds;
336
33
    }
337
338
    // Absolute timestamp (seconds since epoch) at which this tablet's data stops being kept
339
    // in the file cache TTL queue, or 0 when the tablet has no TTL or the deadline has
340
    // already passed. The deadline is anchored at the tablet creation time, so every tablet
341
    // of a table shares one deadline regardless of when each rowset was written.
342
    //
343
    // This is the single definition of that deadline. The load, compaction, schema change,
344
    // query and warm up paths all stamp the cache blocks they create with this value, and
345
    // BlockFileCacheTtlMgr expires those blocks by the very same value, so a block's
346
    // recorded expiration time always agrees with the sweep that acts on it.
347
    int64_t file_cache_ttl_expiration_time() const;
348
349
280
    int64_t avg_rs_meta_serialize_size() const { return _avg_rs_meta_serialize_size; }
350
351
441k
    EncryptionAlgorithmPB encryption_algorithm() const { return _encryption_algorithm; }
352
353
204k
    bool has_inverted_index_storage_format() const {
354
204k
        return _inverted_index_storage_format.has_value();
355
204k
    }
356
357
205k
    InvertedIndexStorageFormatPB inverted_index_storage_format() const {
358
205k
        return _inverted_index_storage_format.value_or(
359
205k
                _schema->get_inverted_index_storage_format());
360
205k
    }
361
362
private:
363
    Status _save_meta(DataDir* data_dir);
364
    void _check_mow_rowset_cache_version_size(size_t rowset_cache_version_size);
365
366
    // _del_predicates is ignored to compare.
367
    friend bool operator==(const TabletMeta& a, const TabletMeta& b);
368
    friend bool operator!=(const TabletMeta& a, const TabletMeta& b);
369
370
private:
371
    int64_t _table_id = 0;
372
    int64_t _index_id = 0;
373
    int64_t _partition_id = 0;
374
    int64_t _tablet_id = 0;
375
    int64_t _replica_id = 0;
376
    int32_t _schema_hash = 0;
377
    int32_t _shard_id = 0;
378
    int64_t _creation_time = 0;
379
    int64_t _cumulative_layer_point = 0;
380
    TabletUid _tablet_uid;
381
    TabletTypePB _tablet_type = TabletTypePB::TABLET_TYPE_DISK;
382
383
    TabletState _tablet_state = TABLET_NOTREADY;
384
    // the reference of _schema may use in tablet, so here need keep
385
    // the lifetime of tablemeta and _schema is same with tablet
386
    TabletSchemaSPtr _schema;
387
    Cache::Handle* _handle = nullptr;
388
389
    RowsetMetaMapContainer _rs_metas;
390
    // This variable _stale_rs_metas is used to record these rowsets‘ meta which are be compacted.
391
    // These stale rowsets meta are been removed when rowsets' pathVersion is expired,
392
    // this policy is judged and computed by TimestampedVersionTracker.
393
    RowsetMetaMapContainer _stale_rs_metas;
394
    bool _in_restore_mode = false;
395
    RowsetTypePB _preferred_rowset_type = BETA_ROWSET;
396
397
    // meta for cooldown
398
    int64_t _storage_policy_id = 0; // <= 0 means no storage policy
399
    UniqueId _cooldown_meta_id;
400
401
    // For unique key data model, the feature Merge-on-Write will leverage a primary
402
    // key index and a delete-bitmap to mark duplicate keys as deleted in load stage,
403
    // which can avoid the merging cost in read stage, and accelerate the aggregation
404
    // query performance significantly.
405
    bool _enable_unique_key_merge_on_write = false;
406
    std::shared_ptr<DeleteBitmap> _delete_bitmap;
407
408
    // binlog config
409
    BinlogConfig _binlog_config {};
410
    TabletRolePB _tablet_role = TabletRolePB::TABLET_ROLE_DATA;
411
    int64_t _binlog_tablet_id = 0;
412
413
    // meta for compaction
414
    std::string _compaction_policy;
415
    int64_t _time_series_compaction_goal_size_mbytes = 0;
416
    int64_t _time_series_compaction_file_count_threshold = 0;
417
    int64_t _time_series_compaction_time_threshold_seconds = 0;
418
    int64_t _time_series_compaction_empty_rowsets_threshold = 0;
419
    int64_t _time_series_compaction_level_threshold = 0;
420
    int32_t _vertical_compaction_num_columns_per_group = 5;
421
422
    int64_t _avg_rs_meta_serialize_size = 0;
423
424
    // cloud
425
    int64_t _ttl_seconds = 0;
426
427
    EncryptionAlgorithmPB _encryption_algorithm = PLAINTEXT;
428
429
    // Persisted storage format for this tablet (e.g. V2, V3). Used to derive
430
    // schema-level defaults such as external ColumnMeta usage.
431
    TStorageFormat::type _storage_format = TStorageFormat::V2;
432
    // The schema KV is shared by (index_id, schema_version). Keep the tablet's
433
    // immutable file format outside that shared identity.
434
    std::optional<InvertedIndexStorageFormatPB> _inverted_index_storage_format;
435
436
    mutable std::shared_mutex _meta_lock;
437
};
438
439
class DeleteBitmapAggCache : public LRUCachePolicy {
440
public:
441
    DeleteBitmapAggCache(size_t capacity);
442
443
    static DeleteBitmapAggCache* instance();
444
445
    static DeleteBitmapAggCache* create_instance(size_t capacity);
446
447
    DeleteBitmap snapshot(int64_t tablet_id);
448
449
    class Value : public LRUCacheValueBase {
450
    public:
451
        roaring::Roaring bitmap;
452
    };
453
};
454
455
/**
456
 * Wraps multiple bitmaps for recording rows (row id) that are deleted or
457
 * overwritten. For now, it's only used when unique key merge-on-write property
458
 * enabled.
459
 *
460
 * RowsetId and SegmentId are for locating segment, Version here is a single
461
 * uint32_t means that at which "version" of the load causes the delete or
462
 * overwrite.
463
 *
464
 * The start and end version of a load is the same, it's ok and straightforward
465
 * to use a single uint32_t.
466
 *
467
 * e.g.
468
 * There is a key "key1" in rowset id 1, version [1,1], segment id 1, row id 1.
469
 * A new load also contains "key1", the rowset id 2, version [2,2], segment id 1
470
 * the delete bitmap will be `{1,1,2} -> 1`, which means the "row id 1" in
471
 * "rowset id 1, segment id 1" is deleted/overitten by some loads at "version 2"
472
 */
473
class DeleteBitmap {
474
public:
475
    mutable std::shared_mutex lock;
476
    using SegmentId = uint32_t;
477
    using Version = uint64_t;
478
    using BitmapKey = std::tuple<RowsetId, SegmentId, Version>;
479
    using RowsetIdWithSegmentIds = std::pair<RowsetId, std::vector<SegmentId>>;
480
    std::map<BitmapKey, roaring::Roaring> delete_bitmap; // Ordered map
481
    constexpr static inline uint32_t INVALID_SEGMENT_ID = std::numeric_limits<uint32_t>::max() - 1;
482
    constexpr static inline uint32_t ROWSET_SENTINEL_MARK =
483
            std::numeric_limits<uint32_t>::max() - 1;
484
485
    // When a delete bitmap is merged into tablet's delete bitmap, the version of entries in the delete bitmap
486
    // will be replaced to the correspoding correct version. So before we finally merge a delete bitmap into
487
    // tablet's delete bitmap we can use arbitary version number in BitmapKey. Here we define some version numbers
488
    // for specific usage during this periods to avoid conflicts
489
    constexpr static inline uint64_t TEMP_VERSION_COMMON = 0;
490
491
    /**
492
     * 
493
     * @param tablet_id the tablet which this delete bitmap associates with
494
     */
495
    DeleteBitmap(int64_t tablet_id);
496
497
    /**
498
     * Copy c-tor for making delete bitmap snapshot on read path
499
     */
500
    DeleteBitmap(const DeleteBitmap& r);
501
    DeleteBitmap& operator=(const DeleteBitmap& r);
502
    /**
503
     * Move c-tor for making delete bitmap snapshot on read path
504
     */
505
    DeleteBitmap(DeleteBitmap&& r) noexcept;
506
    DeleteBitmap& operator=(DeleteBitmap&& r) noexcept;
507
508
    static DeleteBitmap from_pb(const DeleteBitmapPB& pb, int64_t tablet_id);
509
510
    DeleteBitmapPB to_pb();
511
512
    /**
513
     * Makes a snapshot of delete bitmap, read lock will be acquired in this
514
     * process
515
     */
516
    DeleteBitmap snapshot() const;
517
518
    /**
519
     * Makes a snapshot of delete bitmap on given version, read lock will be
520
     * acquired temporary in this process
521
     */
522
    DeleteBitmap snapshot(Version version) const;
523
524
    /**
525
     * Marks the specific row deleted
526
     */
527
    void add(const BitmapKey& bmk, uint32_t row_id);
528
529
    /**
530
     * Clears the deletetion mark specific row
531
     *
532
     * @return non-zero if the associated delete bitmap does not exist
533
     */
534
    int remove(const BitmapKey& bmk, uint32_t row_id);
535
536
    /**
537
     * Clears bitmaps in range [lower_key, upper_key)
538
     */
539
    void remove(const BitmapKey& lower_key, const BitmapKey& upper_key);
540
    void remove(const std::vector<std::tuple<BitmapKey, BitmapKey>>& key_ranges);
541
542
    /**
543
     * Checks if the given row is marked deleted
544
     *
545
     * @return true if marked deleted
546
     */
547
    bool contains(const BitmapKey& bmk, uint32_t row_id) const;
548
    bool contain_rowsets(const RowsetIdUnorderedSet& rowset_ids) const;
549
550
    /**
551
     * Checks if this delete bitmap is empty
552
     *
553
     * @return true if empty
554
     */
555
    bool empty() const;
556
557
    /**
558
     * return the total cardinality of the Delete Bitmap
559
     */
560
    uint64_t cardinality() const;
561
562
    /**
563
     * return the total size of the Delete Bitmap(after serialized)
564
     */
565
566
    uint64_t get_size() const;
567
568
    /**
569
     * Sets the bitmap of specific segment, it's may be insertion or replacement
570
     *
571
     * @return 1 if the insertion took place, 0 if the assignment took place
572
     */
573
    int set(const BitmapKey& bmk, const roaring::Roaring& segment_delete_bitmap);
574
575
    /**
576
     * Gets a copy of specific delete bmk
577
     *
578
     * @param segment_delete_bitmap output param
579
     * @return non-zero if the associated delete bitmap does not exist
580
     */
581
    int get(const BitmapKey& bmk, roaring::Roaring* segment_delete_bitmap) const;
582
583
    /**
584
     * Gets reference to a specific delete map, DO NOT use this function on a
585
     * mutable DeleteBitmap object
586
     * @return nullptr if the given bitmap does not exist
587
     */
588
    const roaring::Roaring* get(const BitmapKey& bmk) const;
589
590
    /**
591
     * Gets subset of delete_bitmap with given range [start, end)
592
     *
593
     * @parma start start
594
     * @parma end end
595
     * @parma subset_delete_map output param
596
     */
597
    void subset(const BitmapKey& start, const BitmapKey& end,
598
                DeleteBitmap* subset_delete_map) const;
599
    void subset(const std::vector<RowsetIdWithSegmentIds>& rowsets, int64_t start_version,
600
                int64_t end_version, DeleteBitmap* subset_delete_map) const;
601
602
    /**
603
     * Gets subset of delete_bitmap of the input rowsets
604
     * with given version range [start_version, end_version] and agg to end_version,
605
     * then merge to subset_delete_map
606
     */
607
    void subset_and_agg(const std::vector<RowsetIdWithSegmentIds>& rowsets, int64_t start_version,
608
                        int64_t end_version, DeleteBitmap* subset_delete_map) const;
609
610
    /**
611
     * Gets count of delete_bitmap with given range [start, end)
612
     *
613
     * @parma start start
614
     * @parma end end
615
     */
616
    size_t get_count_with_range(const BitmapKey& start, const BitmapKey& end) const;
617
618
    /**
619
     * Merges the given segment delete bitmap into *this
620
     *
621
     * @param bmk
622
     * @param segment_delete_bitmap
623
     */
624
    void merge(const BitmapKey& bmk, const roaring::Roaring& segment_delete_bitmap);
625
626
    /**
627
     * Merges the given delete bitmap into *this
628
     *
629
     * @param other
630
     */
631
    void merge(const DeleteBitmap& other);
632
633
    /**
634
     * Checks if the given row is marked deleted in bitmap with the condition:
635
     * all the bitmaps that
636
     * RowsetId and SegmentId are the same as the given ones,
637
     * and Version <= the given Version
638
     *
639
     * Note: aggregation cache may be used.
640
     *
641
     * @return true if marked deleted
642
     */
643
    bool contains_agg(const BitmapKey& bitmap, uint32_t row_id) const;
644
645
    bool contains_agg_with_cache_if_eligible(const BitmapKey& bmk, uint32_t row_id) const;
646
    /**
647
     * Gets aggregated delete_bitmap on rowset_id and version, the same effect:
648
     * `select sum(roaring::Roaring) where RowsetId=rowset_id and SegmentId=seg_id and Version <= version`
649
     *
650
     * @return shared_ptr to a bitmap, which may be empty
651
     */
652
    std::shared_ptr<roaring::Roaring> get_agg(const BitmapKey& bmk) const;
653
    std::shared_ptr<roaring::Roaring> get_agg_without_cache(const BitmapKey& bmk,
654
                                                            const int64_t start_version = 0) const;
655
656
    void remove_sentinel_marks();
657
658
    uint64_t get_delete_bitmap_count();
659
660
    void traverse_rowset_and_version(
661
            const std::function<int(const RowsetId& rowsetId, int64_t version)>& func) const;
662
663
    bool has_calculated_for_multi_segments(const RowsetId& rowset_id) const;
664
665
    // return the size of the map
666
    size_t remove_rowset_cache_version(const RowsetId& rowset_id);
667
668
    void clear_rowset_cache_version();
669
670
    std::set<std::string> get_rowset_cache_version();
671
672
    DeleteBitmap agg_cache_snapshot();
673
674
    void set_tablet_id(int64_t tablet_id);
675
676
    /**
677
     * Calculate diffset with given `key_set`. All entries with keys contained in this delete bitmap but not
678
     * in given key_set will be added to the output delete bitmap.
679
     *
680
     * @return Deletebitmap containning all entries in diffset
681
    */
682
    DeleteBitmap diffset(const std::set<BitmapKey>& key_set) const;
683
684
private:
685
    DeleteBitmap::Version _get_rowset_cache_version(const BitmapKey& bmk) const;
686
687
    int64_t _tablet_id;
688
    mutable std::shared_mutex _rowset_cache_version_lock;
689
    mutable std::map<RowsetId, std::map<SegmentId, Version>> _rowset_cache_version;
690
};
691
692
2.31M
inline TabletUid TabletMeta::tablet_uid() const {
693
2.31M
    return _tablet_uid;
694
2.31M
}
695
696
2.21M
inline int64_t TabletMeta::table_id() const {
697
2.21M
    return _table_id;
698
2.21M
}
699
700
2.28M
inline int64_t TabletMeta::index_id() const {
701
2.28M
    return _index_id;
702
2.28M
}
703
704
8.73M
inline int64_t TabletMeta::partition_id() const {
705
8.73M
    return _partition_id;
706
8.73M
}
707
708
1.88G
inline int64_t TabletMeta::tablet_id() const {
709
1.88G
    return _tablet_id;
710
1.88G
}
711
712
1.73M
inline int64_t TabletMeta::replica_id() const {
713
1.73M
    return _replica_id;
714
1.73M
}
715
716
1.87M
inline int32_t TabletMeta::schema_hash() const {
717
1.87M
    return _schema_hash;
718
1.87M
}
719
720
294k
inline int32_t TabletMeta::shard_id() const {
721
294k
    return _shard_id;
722
294k
}
723
724
17
inline void TabletMeta::set_shard_id(int32_t shard_id) {
725
17
    _shard_id = shard_id;
726
17
}
727
728
1.50M
inline int64_t TabletMeta::creation_time() const {
729
1.50M
    return _creation_time;
730
1.50M
}
731
732
inline void TabletMeta::set_creation_time(int64_t creation_time) {
733
    _creation_time = creation_time;
734
}
735
736
5.85k
inline int64_t TabletMeta::cumulative_layer_point() const {
737
5.85k
    return _cumulative_layer_point;
738
5.85k
}
739
740
0
inline void TabletMeta::set_cumulative_layer_point(int64_t new_point) {
741
0
    _cumulative_layer_point = new_point;
742
0
}
743
744
1.29M
inline size_t TabletMeta::num_rows() const {
745
1.29M
    size_t num_rows = 0;
746
2.20M
    for (const auto& [_, rs] : _rs_metas) {
747
2.20M
        num_rows += rs->num_rows();
748
2.20M
    }
749
1.29M
    return num_rows;
750
1.29M
}
751
752
9.49k
inline size_t TabletMeta::tablet_footprint() const {
753
9.49k
    size_t total_size = 0;
754
24.0k
    for (const auto& [_, rs] : _rs_metas) {
755
24.0k
        total_size += rs->total_disk_size();
756
24.0k
    }
757
9.49k
    return total_size;
758
9.49k
}
759
760
4.49M
inline size_t TabletMeta::tablet_local_size() const {
761
4.49M
    size_t total_size = 0;
762
7.73M
    for (const auto& [_, rs] : _rs_metas) {
763
7.73M
        if (rs->is_local()) {
764
7.33M
            total_size += rs->total_disk_size();
765
7.33M
        }
766
7.73M
    }
767
4.49M
    return total_size;
768
4.49M
}
769
770
4.49M
inline size_t TabletMeta::tablet_remote_size() const {
771
4.49M
    size_t total_size = 0;
772
7.73M
    for (const auto& [_, rs] : _rs_metas) {
773
7.73M
        if (!rs->is_local()) {
774
399k
            total_size += rs->total_disk_size();
775
399k
        }
776
7.73M
    }
777
4.49M
    return total_size;
778
4.49M
}
779
780
1.29M
inline size_t TabletMeta::tablet_local_index_size() const {
781
1.29M
    size_t total_size = 0;
782
2.20M
    for (const auto& [_, rs] : _rs_metas) {
783
2.20M
        if (rs->is_local()) {
784
2.20M
            total_size += rs->index_disk_size();
785
2.20M
        }
786
2.20M
    }
787
1.29M
    return total_size;
788
1.29M
}
789
790
1.29M
inline size_t TabletMeta::tablet_local_segment_size() const {
791
1.29M
    size_t total_size = 0;
792
2.20M
    for (const auto& [_, rs] : _rs_metas) {
793
2.20M
        if (rs->is_local()) {
794
2.20M
            total_size += rs->data_disk_size();
795
2.20M
        }
796
2.20M
    }
797
1.29M
    return total_size;
798
1.29M
}
799
800
1.29M
inline size_t TabletMeta::tablet_remote_index_size() const {
801
1.29M
    size_t total_size = 0;
802
2.20M
    for (const auto& [_, rs] : _rs_metas) {
803
2.20M
        if (!rs->is_local()) {
804
18
            total_size += rs->index_disk_size();
805
18
        }
806
2.20M
    }
807
1.29M
    return total_size;
808
1.29M
}
809
810
1.29M
inline size_t TabletMeta::tablet_remote_segment_size() const {
811
1.29M
    size_t total_size = 0;
812
2.20M
    for (const auto& [_, rs] : _rs_metas) {
813
2.20M
        if (!rs->is_local()) {
814
18
            total_size += rs->data_disk_size();
815
18
        }
816
2.20M
    }
817
1.29M
    return total_size;
818
1.29M
}
819
820
8.92M
inline size_t TabletMeta::version_count() const {
821
8.92M
    return _rs_metas.size();
822
8.92M
}
823
824
280
inline size_t TabletMeta::stale_version_count() const {
825
280
    return _rs_metas.size();
826
280
}
827
828
504M
inline TabletState TabletMeta::tablet_state() const {
829
504M
    return _tablet_state;
830
504M
}
831
832
12.3k
inline void TabletMeta::set_tablet_state(TabletState state) {
833
12.3k
    _tablet_state = state;
834
12.3k
}
835
836
5.85k
inline bool TabletMeta::in_restore_mode() const {
837
5.85k
    return _in_restore_mode;
838
5.85k
}
839
840
0
inline void TabletMeta::set_in_restore_mode(bool in_restore_mode) {
841
0
    _in_restore_mode = in_restore_mode;
842
0
}
843
844
1.27G
inline const TabletSchemaSPtr& TabletMeta::tablet_schema() const {
845
1.27G
    return _schema;
846
1.27G
}
847
848
65
inline TabletSchema* TabletMeta::mutable_tablet_schema() {
849
65
    return _schema.get();
850
65
}
851
852
11.3M
inline const RowsetMetaMapContainer& TabletMeta::all_rs_metas() const {
853
11.3M
    return _rs_metas;
854
11.3M
}
855
856
287k
inline RowsetMetaMapContainer& TabletMeta::all_mutable_rs_metas() {
857
287k
    return _rs_metas;
858
287k
}
859
860
725k
inline const RowsetMetaMapContainer& TabletMeta::all_stale_rs_metas() const {
861
725k
    return _stale_rs_metas;
862
725k
}
863
864
0
inline bool TabletMeta::all_beta() const {
865
0
    for (const auto& [_, rs] : _rs_metas) {
866
0
        if (rs->rowset_type() != RowsetTypePB::BETA_ROWSET) {
867
0
            return false;
868
0
        }
869
0
    }
870
0
    for (const auto& [_, rs] : _stale_rs_metas) {
871
0
        if (rs->rowset_type() != RowsetTypePB::BETA_ROWSET) {
872
0
            return false;
873
0
        }
874
0
    }
875
0
    return true;
876
0
}
877
878
std::string tablet_state_name(TabletState state);
879
880
// Only for unit test now.
881
bool operator==(const TabletMeta& a, const TabletMeta& b);
882
bool operator!=(const TabletMeta& a, const TabletMeta& b);
883
884
} // namespace doris