Coverage Report

Created: 2026-09-01 20:48

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