Coverage Report

Created: 2026-09-30 21:43

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