Coverage Report

Created: 2026-10-24 03:36

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