Coverage Report

Created: 2026-09-08 11:30

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
484k
    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
887
    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.12M
    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.14M
    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.82M
    DeleteBitmapPtr delete_bitmap_ptr() { return _delete_bitmap; }
263
276k
    DeleteBitmap& delete_bitmap() { return *_delete_bitmap; }
264
265
    void remove_rowset_delete_bitmap(const RowsetId& rowset_id, const Version& version);
266
267
5.87M
    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
3.53k
    const BinlogConfig& binlog_config() const { return _binlog_config; }
275
0
    void set_binlog_config(BinlogConfig binlog_config) {
276
0
        _binlog_config = std::move(binlog_config);
277
0
    }
278
0
    TabletRolePB tablet_role() const { return _tablet_role; }
279
2.63G
    bool is_row_binlog_tablet() const {
280
2.63G
        return _tablet_role == TabletRolePB::TABLET_ROLE_ROW_BINLOG;
281
2.63G
    }
282
    void set_tablet_role(TabletRolePB tablet_role) { _tablet_role = tablet_role; }
283
5.58k
    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
848M
    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
27.4k
    int64_t time_series_compaction_goal_size_mbytes() const {
294
27.4k
        return _time_series_compaction_goal_size_mbytes;
295
27.4k
    }
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
27.4k
    int64_t time_series_compaction_file_count_threshold() const {
300
27.4k
        return _time_series_compaction_file_count_threshold;
301
27.4k
    }
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
27.4k
    int64_t time_series_compaction_time_threshold_seconds() const {
306
27.4k
        return _time_series_compaction_time_threshold_seconds;
307
27.4k
    }
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
27.4k
    int64_t time_series_compaction_empty_rowsets_threshold() const {
312
27.4k
        return _time_series_compaction_empty_rowsets_threshold;
313
27.4k
    }
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
27.5k
    int64_t time_series_compaction_level_threshold() const {
318
27.5k
        return _time_series_compaction_level_threshold;
319
27.5k
    }
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
37.5k
    int32_t vertical_compaction_num_columns_per_group() const {
325
37.5k
        return _vertical_compaction_num_columns_per_group;
326
37.5k
    }
327
328
1.31M
    int64_t ttl_seconds() const {
329
1.31M
        std::shared_lock rlock(_meta_lock);
330
1.31M
        return _ttl_seconds;
331
1.31M
    }
332
333
9
    void set_ttl_seconds(int64_t ttl_seconds) {
334
9
        std::lock_guard wlock(_meta_lock);
335
9
        _ttl_seconds = ttl_seconds;
336
9
    }
337
338
354
    int64_t avg_rs_meta_serialize_size() const { return _avg_rs_meta_serialize_size; }
339
340
445k
    EncryptionAlgorithmPB encryption_algorithm() const { return _encryption_algorithm; }
341
342
205k
    bool has_inverted_index_storage_format() const {
343
205k
        return _inverted_index_storage_format.has_value();
344
205k
    }
345
346
205k
    InvertedIndexStorageFormatPB inverted_index_storage_format() const {
347
205k
        return _inverted_index_storage_format.value_or(
348
205k
                _schema->get_inverted_index_storage_format());
349
205k
    }
350
351
private:
352
    Status _save_meta(DataDir* data_dir);
353
    void _check_mow_rowset_cache_version_size(size_t rowset_cache_version_size);
354
355
    // _del_predicates is ignored to compare.
356
    friend bool operator==(const TabletMeta& a, const TabletMeta& b);
357
    friend bool operator!=(const TabletMeta& a, const TabletMeta& b);
358
359
private:
360
    int64_t _table_id = 0;
361
    int64_t _index_id = 0;
362
    int64_t _partition_id = 0;
363
    int64_t _tablet_id = 0;
364
    int64_t _replica_id = 0;
365
    int32_t _schema_hash = 0;
366
    int32_t _shard_id = 0;
367
    int64_t _creation_time = 0;
368
    int64_t _cumulative_layer_point = 0;
369
    TabletUid _tablet_uid;
370
    TabletTypePB _tablet_type = TabletTypePB::TABLET_TYPE_DISK;
371
372
    TabletState _tablet_state = TABLET_NOTREADY;
373
    // the reference of _schema may use in tablet, so here need keep
374
    // the lifetime of tablemeta and _schema is same with tablet
375
    TabletSchemaSPtr _schema;
376
    Cache::Handle* _handle = nullptr;
377
378
    RowsetMetaMapContainer _rs_metas;
379
    // This variable _stale_rs_metas is used to record these rowsets‘ meta which are be compacted.
380
    // These stale rowsets meta are been removed when rowsets' pathVersion is expired,
381
    // this policy is judged and computed by TimestampedVersionTracker.
382
    RowsetMetaMapContainer _stale_rs_metas;
383
    bool _in_restore_mode = false;
384
    RowsetTypePB _preferred_rowset_type = BETA_ROWSET;
385
386
    // meta for cooldown
387
    int64_t _storage_policy_id = 0; // <= 0 means no storage policy
388
    UniqueId _cooldown_meta_id;
389
390
    // For unique key data model, the feature Merge-on-Write will leverage a primary
391
    // key index and a delete-bitmap to mark duplicate keys as deleted in load stage,
392
    // which can avoid the merging cost in read stage, and accelerate the aggregation
393
    // query performance significantly.
394
    bool _enable_unique_key_merge_on_write = false;
395
    std::shared_ptr<DeleteBitmap> _delete_bitmap;
396
397
    // binlog config
398
    BinlogConfig _binlog_config {};
399
    TabletRolePB _tablet_role = TabletRolePB::TABLET_ROLE_DATA;
400
    int64_t _binlog_tablet_id = 0;
401
402
    // meta for compaction
403
    std::string _compaction_policy;
404
    int64_t _time_series_compaction_goal_size_mbytes = 0;
405
    int64_t _time_series_compaction_file_count_threshold = 0;
406
    int64_t _time_series_compaction_time_threshold_seconds = 0;
407
    int64_t _time_series_compaction_empty_rowsets_threshold = 0;
408
    int64_t _time_series_compaction_level_threshold = 0;
409
    int32_t _vertical_compaction_num_columns_per_group = 5;
410
411
    int64_t _avg_rs_meta_serialize_size = 0;
412
413
    // cloud
414
    int64_t _ttl_seconds = 0;
415
416
    EncryptionAlgorithmPB _encryption_algorithm = PLAINTEXT;
417
418
    // Persisted storage format for this tablet (e.g. V2, V3). Used to derive
419
    // schema-level defaults such as external ColumnMeta usage.
420
    TStorageFormat::type _storage_format = TStorageFormat::V2;
421
    // The schema KV is shared by (index_id, schema_version). Keep the tablet's
422
    // immutable file format outside that shared identity.
423
    std::optional<InvertedIndexStorageFormatPB> _inverted_index_storage_format;
424
425
    mutable std::shared_mutex _meta_lock;
426
};
427
428
class DeleteBitmapAggCache : public LRUCachePolicy {
429
public:
430
    DeleteBitmapAggCache(size_t capacity);
431
432
    static DeleteBitmapAggCache* instance();
433
434
    static DeleteBitmapAggCache* create_instance(size_t capacity);
435
436
    DeleteBitmap snapshot(int64_t tablet_id);
437
438
    class Value : public LRUCacheValueBase {
439
    public:
440
        roaring::Roaring bitmap;
441
    };
442
};
443
444
/**
445
 * Wraps multiple bitmaps for recording rows (row id) that are deleted or
446
 * overwritten. For now, it's only used when unique key merge-on-write property
447
 * enabled.
448
 *
449
 * RowsetId and SegmentId are for locating segment, Version here is a single
450
 * uint32_t means that at which "version" of the load causes the delete or
451
 * overwrite.
452
 *
453
 * The start and end version of a load is the same, it's ok and straightforward
454
 * to use a single uint32_t.
455
 *
456
 * e.g.
457
 * There is a key "key1" in rowset id 1, version [1,1], segment id 1, row id 1.
458
 * A new load also contains "key1", the rowset id 2, version [2,2], segment id 1
459
 * the delete bitmap will be `{1,1,2} -> 1`, which means the "row id 1" in
460
 * "rowset id 1, segment id 1" is deleted/overitten by some loads at "version 2"
461
 */
462
class DeleteBitmap {
463
public:
464
    mutable std::shared_mutex lock;
465
    using SegmentId = uint32_t;
466
    using Version = uint64_t;
467
    using BitmapKey = std::tuple<RowsetId, SegmentId, Version>;
468
    using RowsetIdWithSegmentIds = std::pair<RowsetId, std::vector<SegmentId>>;
469
    std::map<BitmapKey, roaring::Roaring> delete_bitmap; // Ordered map
470
    constexpr static inline uint32_t INVALID_SEGMENT_ID = std::numeric_limits<uint32_t>::max() - 1;
471
    constexpr static inline uint32_t ROWSET_SENTINEL_MARK =
472
            std::numeric_limits<uint32_t>::max() - 1;
473
474
    // When a delete bitmap is merged into tablet's delete bitmap, the version of entries in the delete bitmap
475
    // will be replaced to the correspoding correct version. So before we finally merge a delete bitmap into
476
    // tablet's delete bitmap we can use arbitary version number in BitmapKey. Here we define some version numbers
477
    // for specific usage during this periods to avoid conflicts
478
    constexpr static inline uint64_t TEMP_VERSION_COMMON = 0;
479
480
    /**
481
     * 
482
     * @param tablet_id the tablet which this delete bitmap associates with
483
     */
484
    DeleteBitmap(int64_t tablet_id);
485
486
    /**
487
     * Copy c-tor for making delete bitmap snapshot on read path
488
     */
489
    DeleteBitmap(const DeleteBitmap& r);
490
    DeleteBitmap& operator=(const DeleteBitmap& r);
491
    /**
492
     * Move c-tor for making delete bitmap snapshot on read path
493
     */
494
    DeleteBitmap(DeleteBitmap&& r) noexcept;
495
    DeleteBitmap& operator=(DeleteBitmap&& r) noexcept;
496
497
    static DeleteBitmap from_pb(const DeleteBitmapPB& pb, int64_t tablet_id);
498
499
    DeleteBitmapPB to_pb();
500
501
    /**
502
     * Makes a snapshot of delete bitmap, read lock will be acquired in this
503
     * process
504
     */
505
    DeleteBitmap snapshot() const;
506
507
    /**
508
     * Makes a snapshot of delete bitmap on given version, read lock will be
509
     * acquired temporary in this process
510
     */
511
    DeleteBitmap snapshot(Version version) const;
512
513
    /**
514
     * Marks the specific row deleted
515
     */
516
    void add(const BitmapKey& bmk, uint32_t row_id);
517
518
    /**
519
     * Clears the deletetion mark specific row
520
     *
521
     * @return non-zero if the associated delete bitmap does not exist
522
     */
523
    int remove(const BitmapKey& bmk, uint32_t row_id);
524
525
    /**
526
     * Clears bitmaps in range [lower_key, upper_key)
527
     */
528
    void remove(const BitmapKey& lower_key, const BitmapKey& upper_key);
529
    void remove(const std::vector<std::tuple<BitmapKey, BitmapKey>>& key_ranges);
530
531
    /**
532
     * Checks if the given row is marked deleted
533
     *
534
     * @return true if marked deleted
535
     */
536
    bool contains(const BitmapKey& bmk, uint32_t row_id) const;
537
    bool contain_rowsets(const RowsetIdUnorderedSet& rowset_ids) const;
538
539
    /**
540
     * Checks if this delete bitmap is empty
541
     *
542
     * @return true if empty
543
     */
544
    bool empty() const;
545
546
    /**
547
     * return the total cardinality of the Delete Bitmap
548
     */
549
    uint64_t cardinality() const;
550
551
    /**
552
     * return the total size of the Delete Bitmap(after serialized)
553
     */
554
555
    uint64_t get_size() const;
556
557
    /**
558
     * Sets the bitmap of specific segment, it's may be insertion or replacement
559
     *
560
     * @return 1 if the insertion took place, 0 if the assignment took place
561
     */
562
    int set(const BitmapKey& bmk, const roaring::Roaring& segment_delete_bitmap);
563
564
    /**
565
     * Gets a copy of specific delete bmk
566
     *
567
     * @param segment_delete_bitmap output param
568
     * @return non-zero if the associated delete bitmap does not exist
569
     */
570
    int get(const BitmapKey& bmk, roaring::Roaring* segment_delete_bitmap) const;
571
572
    /**
573
     * Gets reference to a specific delete map, DO NOT use this function on a
574
     * mutable DeleteBitmap object
575
     * @return nullptr if the given bitmap does not exist
576
     */
577
    const roaring::Roaring* get(const BitmapKey& bmk) const;
578
579
    /**
580
     * Gets subset of delete_bitmap with given range [start, end)
581
     *
582
     * @parma start start
583
     * @parma end end
584
     * @parma subset_delete_map output param
585
     */
586
    void subset(const BitmapKey& start, const BitmapKey& end,
587
                DeleteBitmap* subset_delete_map) const;
588
    void subset(const std::vector<RowsetIdWithSegmentIds>& rowsets, int64_t start_version,
589
                int64_t end_version, DeleteBitmap* subset_delete_map) const;
590
591
    /**
592
     * Gets subset of delete_bitmap of the input rowsets
593
     * with given version range [start_version, end_version] and agg to end_version,
594
     * then merge to subset_delete_map
595
     */
596
    void subset_and_agg(const std::vector<RowsetIdWithSegmentIds>& rowsets, int64_t start_version,
597
                        int64_t end_version, DeleteBitmap* subset_delete_map) const;
598
599
    /**
600
     * Gets count of delete_bitmap with given range [start, end)
601
     *
602
     * @parma start start
603
     * @parma end end
604
     */
605
    size_t get_count_with_range(const BitmapKey& start, const BitmapKey& end) const;
606
607
    /**
608
     * Merges the given segment delete bitmap into *this
609
     *
610
     * @param bmk
611
     * @param segment_delete_bitmap
612
     */
613
    void merge(const BitmapKey& bmk, const roaring::Roaring& segment_delete_bitmap);
614
615
    /**
616
     * Merges the given delete bitmap into *this
617
     *
618
     * @param other
619
     */
620
    void merge(const DeleteBitmap& other);
621
622
    /**
623
     * Checks if the given row is marked deleted in bitmap with the condition:
624
     * all the bitmaps that
625
     * RowsetId and SegmentId are the same as the given ones,
626
     * and Version <= the given Version
627
     *
628
     * Note: aggregation cache may be used.
629
     *
630
     * @return true if marked deleted
631
     */
632
    bool contains_agg(const BitmapKey& bitmap, uint32_t row_id) const;
633
634
    bool contains_agg_with_cache_if_eligible(const BitmapKey& bmk, uint32_t row_id) const;
635
    /**
636
     * Gets aggregated delete_bitmap on rowset_id and version, the same effect:
637
     * `select sum(roaring::Roaring) where RowsetId=rowset_id and SegmentId=seg_id and Version <= version`
638
     *
639
     * @return shared_ptr to a bitmap, which may be empty
640
     */
641
    std::shared_ptr<roaring::Roaring> get_agg(const BitmapKey& bmk) const;
642
    std::shared_ptr<roaring::Roaring> get_agg_without_cache(const BitmapKey& bmk,
643
                                                            const int64_t start_version = 0) const;
644
645
    void remove_sentinel_marks();
646
647
    uint64_t get_delete_bitmap_count();
648
649
    void traverse_rowset_and_version(
650
            const std::function<int(const RowsetId& rowsetId, int64_t version)>& func) const;
651
652
    bool has_calculated_for_multi_segments(const RowsetId& rowset_id) const;
653
654
    // return the size of the map
655
    size_t remove_rowset_cache_version(const RowsetId& rowset_id);
656
657
    void clear_rowset_cache_version();
658
659
    std::set<std::string> get_rowset_cache_version();
660
661
    DeleteBitmap agg_cache_snapshot();
662
663
    void set_tablet_id(int64_t tablet_id);
664
665
    /**
666
     * Calculate diffset with given `key_set`. All entries with keys contained in this delete bitmap but not
667
     * in given key_set will be added to the output delete bitmap.
668
     *
669
     * @return Deletebitmap containning all entries in diffset
670
    */
671
    DeleteBitmap diffset(const std::set<BitmapKey>& key_set) const;
672
673
private:
674
    DeleteBitmap::Version _get_rowset_cache_version(const BitmapKey& bmk) const;
675
676
    int64_t _tablet_id;
677
    mutable std::shared_mutex _rowset_cache_version_lock;
678
    mutable std::map<RowsetId, std::map<SegmentId, Version>> _rowset_cache_version;
679
};
680
681
2.11M
inline TabletUid TabletMeta::tablet_uid() const {
682
2.11M
    return _tablet_uid;
683
2.11M
}
684
685
2.53M
inline int64_t TabletMeta::table_id() const {
686
2.53M
    return _table_id;
687
2.53M
}
688
689
2.13M
inline int64_t TabletMeta::index_id() const {
690
2.13M
    return _index_id;
691
2.13M
}
692
693
8.38M
inline int64_t TabletMeta::partition_id() const {
694
8.38M
    return _partition_id;
695
8.38M
}
696
697
1.69G
inline int64_t TabletMeta::tablet_id() const {
698
1.69G
    return _tablet_id;
699
1.69G
}
700
701
1.57M
inline int64_t TabletMeta::replica_id() const {
702
1.57M
    return _replica_id;
703
1.57M
}
704
705
1.79M
inline int32_t TabletMeta::schema_hash() const {
706
1.79M
    return _schema_hash;
707
1.79M
}
708
709
300k
inline int32_t TabletMeta::shard_id() const {
710
300k
    return _shard_id;
711
300k
}
712
713
17
inline void TabletMeta::set_shard_id(int32_t shard_id) {
714
17
    _shard_id = shard_id;
715
17
}
716
717
344k
inline int64_t TabletMeta::creation_time() const {
718
344k
    return _creation_time;
719
344k
}
720
721
inline void TabletMeta::set_creation_time(int64_t creation_time) {
722
    _creation_time = creation_time;
723
}
724
725
6.81k
inline int64_t TabletMeta::cumulative_layer_point() const {
726
6.81k
    return _cumulative_layer_point;
727
6.81k
}
728
729
0
inline void TabletMeta::set_cumulative_layer_point(int64_t new_point) {
730
0
    _cumulative_layer_point = new_point;
731
0
}
732
733
1.18M
inline size_t TabletMeta::num_rows() const {
734
1.18M
    size_t num_rows = 0;
735
2.02M
    for (const auto& [_, rs] : _rs_metas) {
736
2.02M
        num_rows += rs->num_rows();
737
2.02M
    }
738
1.18M
    return num_rows;
739
1.18M
}
740
741
17.3k
inline size_t TabletMeta::tablet_footprint() const {
742
17.3k
    size_t total_size = 0;
743
39.8k
    for (const auto& [_, rs] : _rs_metas) {
744
39.8k
        total_size += rs->total_disk_size();
745
39.8k
    }
746
17.3k
    return total_size;
747
17.3k
}
748
749
4.47M
inline size_t TabletMeta::tablet_local_size() const {
750
4.47M
    size_t total_size = 0;
751
7.70M
    for (const auto& [_, rs] : _rs_metas) {
752
7.70M
        if (rs->is_local()) {
753
7.30M
            total_size += rs->total_disk_size();
754
7.30M
        }
755
7.70M
    }
756
4.47M
    return total_size;
757
4.47M
}
758
759
4.47M
inline size_t TabletMeta::tablet_remote_size() const {
760
4.47M
    size_t total_size = 0;
761
7.70M
    for (const auto& [_, rs] : _rs_metas) {
762
7.70M
        if (!rs->is_local()) {
763
395k
            total_size += rs->total_disk_size();
764
395k
        }
765
7.70M
    }
766
4.47M
    return total_size;
767
4.47M
}
768
769
1.18M
inline size_t TabletMeta::tablet_local_index_size() const {
770
1.18M
    size_t total_size = 0;
771
2.02M
    for (const auto& [_, rs] : _rs_metas) {
772
2.02M
        if (rs->is_local()) {
773
2.02M
            total_size += rs->index_disk_size();
774
2.02M
        }
775
2.02M
    }
776
1.18M
    return total_size;
777
1.18M
}
778
779
1.18M
inline size_t TabletMeta::tablet_local_segment_size() const {
780
1.18M
    size_t total_size = 0;
781
2.02M
    for (const auto& [_, rs] : _rs_metas) {
782
2.02M
        if (rs->is_local()) {
783
2.02M
            total_size += rs->data_disk_size();
784
2.02M
        }
785
2.02M
    }
786
1.18M
    return total_size;
787
1.18M
}
788
789
1.18M
inline size_t TabletMeta::tablet_remote_index_size() const {
790
1.18M
    size_t total_size = 0;
791
2.02M
    for (const auto& [_, rs] : _rs_metas) {
792
2.02M
        if (!rs->is_local()) {
793
16
            total_size += rs->index_disk_size();
794
16
        }
795
2.02M
    }
796
1.18M
    return total_size;
797
1.18M
}
798
799
1.18M
inline size_t TabletMeta::tablet_remote_segment_size() const {
800
1.18M
    size_t total_size = 0;
801
2.02M
    for (const auto& [_, rs] : _rs_metas) {
802
2.02M
        if (!rs->is_local()) {
803
16
            total_size += rs->data_disk_size();
804
16
        }
805
2.02M
    }
806
1.18M
    return total_size;
807
1.18M
}
808
809
8.35M
inline size_t TabletMeta::version_count() const {
810
8.35M
    return _rs_metas.size();
811
8.35M
}
812
813
354
inline size_t TabletMeta::stale_version_count() const {
814
354
    return _rs_metas.size();
815
354
}
816
817
456M
inline TabletState TabletMeta::tablet_state() const {
818
456M
    return _tablet_state;
819
456M
}
820
821
12.2k
inline void TabletMeta::set_tablet_state(TabletState state) {
822
12.2k
    _tablet_state = state;
823
12.2k
}
824
825
6.81k
inline bool TabletMeta::in_restore_mode() const {
826
6.81k
    return _in_restore_mode;
827
6.81k
}
828
829
0
inline void TabletMeta::set_in_restore_mode(bool in_restore_mode) {
830
0
    _in_restore_mode = in_restore_mode;
831
0
}
832
833
1.23G
inline const TabletSchemaSPtr& TabletMeta::tablet_schema() const {
834
1.23G
    return _schema;
835
1.23G
}
836
837
65
inline TabletSchema* TabletMeta::mutable_tablet_schema() {
838
65
    return _schema.get();
839
65
}
840
841
10.7M
inline const RowsetMetaMapContainer& TabletMeta::all_rs_metas() const {
842
10.7M
    return _rs_metas;
843
10.7M
}
844
845
293k
inline RowsetMetaMapContainer& TabletMeta::all_mutable_rs_metas() {
846
293k
    return _rs_metas;
847
293k
}
848
849
738k
inline const RowsetMetaMapContainer& TabletMeta::all_stale_rs_metas() const {
850
738k
    return _stale_rs_metas;
851
738k
}
852
853
0
inline bool TabletMeta::all_beta() const {
854
0
    for (const auto& [_, rs] : _rs_metas) {
855
0
        if (rs->rowset_type() != RowsetTypePB::BETA_ROWSET) {
856
0
            return false;
857
0
        }
858
0
    }
859
0
    for (const auto& [_, rs] : _stale_rs_metas) {
860
0
        if (rs->rowset_type() != RowsetTypePB::BETA_ROWSET) {
861
0
            return false;
862
0
        }
863
0
    }
864
0
    return true;
865
0
}
866
867
std::string tablet_state_name(TabletState state);
868
869
// Only for unit test now.
870
bool operator==(const TabletMeta& a, const TabletMeta& b);
871
bool operator!=(const TabletMeta& a, const TabletMeta& b);
872
873
} // namespace doris