Coverage Report

Created: 2026-08-20 04:53

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