Coverage Report

Created: 2026-06-02 13:54

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
               const TTabletSchema* row_binlog_schema = nullptr);
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
291k
    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
209
    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 binlog_size() const;
178
179
    size_t tablet_local_index_size() const;
180
    size_t tablet_local_segment_size() const;
181
    size_t tablet_remote_index_size() const;
182
    size_t tablet_remote_segment_size() const;
183
184
    size_t version_count() const;
185
    size_t stale_version_count() const;
186
    size_t binlog_file_num() const;
187
    size_t version_count_cross_with_range(const Version& range) const;
188
    Version max_version() const;
189
190
    TabletState tablet_state() const;
191
    void set_tablet_state(TabletState state);
192
193
    bool in_restore_mode() const;
194
    void set_in_restore_mode(bool in_restore_mode);
195
196
    const TabletSchemaSPtr& tablet_schema() const;
197
198
    TabletSchema* mutable_tablet_schema();
199
200
    const RowsetMetaMapContainer& all_rs_metas() const;
201
    RowsetMetaMapContainer& all_mutable_rs_metas();
202
    Status add_rs_meta(const RowsetMetaSharedPtr& rs_meta);
203
    void delete_rs_meta_by_version(const Version& version,
204
                                   std::vector<RowsetMetaSharedPtr>* deleted_rs_metas);
205
    // If same_version is true, the rowset in "to_delete" will not be added
206
    // to _stale_rs_meta, but to be deleted from rs_meta directly.
207
    void modify_rs_metas(const std::vector<RowsetMetaSharedPtr>& to_add,
208
                         const std::vector<RowsetMetaSharedPtr>& to_delete,
209
                         bool same_version = false);
210
    void modify_row_binlog_rs_metas(const std::vector<RowsetMetaSharedPtr>& to_add,
211
                                    const std::vector<RowsetMetaSharedPtr>& to_delete);
212
    void revise_rs_metas(std::vector<RowsetMetaSharedPtr>&& rs_metas);
213
    void revise_row_binlog_rs_metas(std::vector<RowsetMetaSharedPtr>&& rs_metas);
214
    void revise_delete_bitmap_unlocked(const DeleteBitmap& delete_bitmap);
215
    // Revise delete vector used by binlog<row> rowsets.
216
    void revise_binlog_delvec_unlocked(const DeleteBitmap& binlog_delvec);
217
218
    const RowsetMetaMapContainer& all_stale_rs_metas() const;
219
    RowsetMetaSharedPtr acquire_rs_meta_by_version(const Version& version) const;
220
    void delete_stale_rs_meta_by_version(const Version& version);
221
    RowsetMetaSharedPtr acquire_stale_rs_meta_by_version(const Version& version) const;
222
223
    Status set_partition_id(int64_t partition_id);
224
225
48.2k
    RowsetTypePB preferred_rowset_type() const { return _preferred_rowset_type; }
226
227
7.22k
    void set_preferred_rowset_type(RowsetTypePB preferred_rowset_type) {
228
7.22k
        _preferred_rowset_type = preferred_rowset_type;
229
7.22k
    }
230
231
    // used for after tablet cloned to clear stale rowset
232
    void clear_stale_rowset();
233
234
    void clear_rowsets();
235
236
    // MUST hold EXCLUSIVE `_meta_lock` in belonged Tablet
237
    // `to_add` MUST NOT have overlapped version with `_rs_metas` in tablet meta.
238
    void add_rowsets_unchecked(const std::vector<RowsetSharedPtr>& to_add);
239
240
    bool all_beta() const;
241
242
2.18M
    int64_t storage_policy_id() const { return _storage_policy_id; }
243
244
4
    void set_storage_policy_id(int64_t id) {
245
4
        VLOG_NOTICE << "set tablet_id : " << _table_id << " storage policy from "
246
4
                    << _storage_policy_id << " to " << id;
247
4
        _storage_policy_id = id;
248
4
    }
249
250
3.00M
    UniqueId cooldown_meta_id() const { return _cooldown_meta_id; }
251
5
    void set_cooldown_meta_id(UniqueId uid) { _cooldown_meta_id = uid; }
252
253
    static void init_column_from_tcolumn(uint32_t unique_id, const TColumn& tcolumn,
254
                                         ColumnPB* column);
255
256
    struct SchemaCreateOptions {
257
        const std::unordered_map<uint32_t, uint32_t>& col_ordinal_to_unique_id;
258
        TCompressionType::type compression_type;
259
        TInvertedIndexFileStorageFormat::type inverted_index_file_storage_format;
260
        uint32_t next_unique_id;
261
    };
262
263
    static void init_schema_from_thrift(const TTabletSchema& tablet_schema,
264
                                        const SchemaCreateOptions& schema_create_options,
265
                                        TabletSchemaPB* tablet_schema_pb);
266
267
976k
    DeleteBitmapPtr delete_bitmap_ptr() { return _delete_bitmap; }
268
163k
    DeleteBitmap& delete_bitmap() { return *_delete_bitmap; }
269
270
0
    DeleteBitmapPtr binlog_delvec_ptr() { return _binlog_delvec; }
271
8.73k
    DeleteBitmap& binlog_delvec() { return *_binlog_delvec; }
272
273
    void remove_rowset_delete_bitmap(const RowsetId& rowset_id, const Version& version);
274
275
3.29M
    bool enable_unique_key_merge_on_write() const { return _enable_unique_key_merge_on_write; }
276
#ifdef BE_TEST
277
    void set_enable_unique_key_merge_on_write(bool value) {
278
        _enable_unique_key_merge_on_write = value;
279
    }
280
#endif
281
    // TODO(Drogon): thread safety
282
412M
    const BinlogConfig& binlog_config() const { return _binlog_config; }
283
0
    void set_binlog_config(BinlogConfig binlog_config) {
284
0
        _binlog_config = std::move(binlog_config);
285
0
    }
286
287
9
    const TabletSchemaSPtr& row_binlog_schema() const { return _row_binlog_schema; }
288
9
    int32_t row_binlog_schema_hash() const { return _row_binlog_schema_hash; }
289
    const RowsetMetaMapContainer& all_row_binlog_rs_metas() const;
290
    RowsetMetaSharedPtr acquire_row_binlog_rs_meta_by_version(const Version& version) const;
291
    Status add_row_binlog_rs_meta(const RowsetMetaSharedPtr& rs_meta);
292
293
13
    void set_compaction_policy(std::string compaction_policy) {
294
13
        _compaction_policy = compaction_policy;
295
13
    }
296
55.4M
    std::string compaction_policy() const { return _compaction_policy; }
297
12
    void set_time_series_compaction_goal_size_mbytes(int64_t goal_size_mbytes) {
298
12
        _time_series_compaction_goal_size_mbytes = goal_size_mbytes;
299
12
    }
300
50.3k
    int64_t time_series_compaction_goal_size_mbytes() const {
301
50.3k
        return _time_series_compaction_goal_size_mbytes;
302
50.3k
    }
303
12
    void set_time_series_compaction_file_count_threshold(int64_t file_count_threshold) {
304
12
        _time_series_compaction_file_count_threshold = file_count_threshold;
305
12
    }
306
50.3k
    int64_t time_series_compaction_file_count_threshold() const {
307
50.3k
        return _time_series_compaction_file_count_threshold;
308
50.3k
    }
309
13
    void set_time_series_compaction_time_threshold_seconds(int64_t time_threshold) {
310
13
        _time_series_compaction_time_threshold_seconds = time_threshold;
311
13
    }
312
50.3k
    int64_t time_series_compaction_time_threshold_seconds() const {
313
50.3k
        return _time_series_compaction_time_threshold_seconds;
314
50.3k
    }
315
0
    void set_time_series_compaction_empty_rowsets_threshold(int64_t empty_rowsets_threshold) {
316
0
        _time_series_compaction_empty_rowsets_threshold = empty_rowsets_threshold;
317
0
    }
318
50.2k
    int64_t time_series_compaction_empty_rowsets_threshold() const {
319
50.2k
        return _time_series_compaction_empty_rowsets_threshold;
320
50.2k
    }
321
0
    void set_time_series_compaction_level_threshold(int64_t level_threshold) {
322
0
        _time_series_compaction_level_threshold = level_threshold;
323
0
    }
324
50.3k
    int64_t time_series_compaction_level_threshold() const {
325
50.3k
        return _time_series_compaction_level_threshold;
326
50.3k
    }
327
328
0
    void set_vertical_compaction_num_columns_per_group(int32_t num) {
329
0
        _vertical_compaction_num_columns_per_group = num;
330
0
    }
331
55.4k
    int32_t vertical_compaction_num_columns_per_group() const {
332
55.4k
        return _vertical_compaction_num_columns_per_group;
333
55.4k
    }
334
335
636k
    int64_t ttl_seconds() const {
336
636k
        std::shared_lock rlock(_meta_lock);
337
636k
        return _ttl_seconds;
338
636k
    }
339
340
    void set_ttl_seconds(int64_t ttl_seconds) {
341
        std::lock_guard wlock(_meta_lock);
342
        _ttl_seconds = ttl_seconds;
343
    }
344
345
36.6k
    int64_t avg_rs_meta_serialize_size() const { return _avg_rs_meta_serialize_size; }
346
347
181k
    EncryptionAlgorithmPB encryption_algorithm() const { return _encryption_algorithm; }
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
    std::shared_ptr<DeleteBitmap> _binlog_delvec;
395
396
    int32_t _row_binlog_schema_hash = 0;
397
    TabletSchemaSPtr _row_binlog_schema;
398
    RowsetMetaMapContainer _row_binlog_rs_metas;
399
400
    // binlog config
401
    BinlogConfig _binlog_config {};
402
403
    // meta for compaction
404
    std::string _compaction_policy;
405
    int64_t _time_series_compaction_goal_size_mbytes = 0;
406
    int64_t _time_series_compaction_file_count_threshold = 0;
407
    int64_t _time_series_compaction_time_threshold_seconds = 0;
408
    int64_t _time_series_compaction_empty_rowsets_threshold = 0;
409
    int64_t _time_series_compaction_level_threshold = 0;
410
    int32_t _vertical_compaction_num_columns_per_group = 5;
411
412
    int64_t _avg_rs_meta_serialize_size = 0;
413
414
    // cloud
415
    int64_t _ttl_seconds = 0;
416
417
    EncryptionAlgorithmPB _encryption_algorithm = PLAINTEXT;
418
419
    // Persisted storage format for this tablet (e.g. V2, V3). Used to derive
420
    // schema-level defaults such as external ColumnMeta usage.
421
    TStorageFormat::type _storage_format = TStorageFormat::V2;
422
423
    mutable std::shared_mutex _meta_lock;
424
};
425
426
class DeleteBitmapAggCache : public LRUCachePolicy {
427
public:
428
    DeleteBitmapAggCache(size_t capacity);
429
430
    static DeleteBitmapAggCache* instance();
431
432
    static DeleteBitmapAggCache* create_instance(size_t capacity);
433
434
    DeleteBitmap snapshot(int64_t tablet_id);
435
436
    class Value : public LRUCacheValueBase {
437
    public:
438
        roaring::Roaring bitmap;
439
    };
440
};
441
442
/**
443
 * Wraps multiple bitmaps for recording rows (row id) that are deleted or
444
 * overwritten. For now, it's only used when unique key merge-on-write property
445
 * enabled.
446
 *
447
 * RowsetId and SegmentId are for locating segment, Version here is a single
448
 * uint32_t means that at which "version" of the load causes the delete or
449
 * overwrite.
450
 *
451
 * The start and end version of a load is the same, it's ok and straightforward
452
 * to use a single uint32_t.
453
 *
454
 * e.g.
455
 * There is a key "key1" in rowset id 1, version [1,1], segment id 1, row id 1.
456
 * A new load also contains "key1", the rowset id 2, version [2,2], segment id 1
457
 * the delete bitmap will be `{1,1,2} -> 1`, which means the "row id 1" in
458
 * "rowset id 1, segment id 1" is deleted/overitten by some loads at "version 2"
459
 */
460
class DeleteBitmap {
461
public:
462
    mutable std::shared_mutex lock;
463
    using SegmentId = uint32_t;
464
    using Version = uint64_t;
465
    using BitmapKey = std::tuple<RowsetId, SegmentId, Version>;
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(std::vector<std::pair<RowsetId, int64_t>>& rowset_ids, 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(std::vector<std::pair<RowsetId, int64_t>>& rowset_ids,
594
                        int64_t start_version, int64_t end_version,
595
                        DeleteBitmap* subset_delete_map) const;
596
597
    /**
598
     * Gets count of delete_bitmap with given range [start, end)
599
     *
600
     * @parma start start
601
     * @parma end end
602
     */
603
    size_t get_count_with_range(const BitmapKey& start, const BitmapKey& end) const;
604
605
    /**
606
     * Merges the given segment delete bitmap into *this
607
     *
608
     * @param bmk
609
     * @param segment_delete_bitmap
610
     */
611
    void merge(const BitmapKey& bmk, const roaring::Roaring& segment_delete_bitmap);
612
613
    /**
614
     * Merges the given delete bitmap into *this
615
     *
616
     * @param other
617
     */
618
    void merge(const DeleteBitmap& other);
619
620
    /**
621
     * Checks if the given row is marked deleted in bitmap with the condition:
622
     * all the bitmaps that
623
     * RowsetId and SegmentId are the same as the given ones,
624
     * and Version <= the given Version
625
     *
626
     * Note: aggregation cache may be used.
627
     *
628
     * @return true if marked deleted
629
     */
630
    bool contains_agg(const BitmapKey& bitmap, uint32_t row_id) const;
631
632
    bool contains_agg_with_cache_if_eligible(const BitmapKey& bmk, uint32_t row_id) const;
633
    /**
634
     * Gets aggregated delete_bitmap on rowset_id and version, the same effect:
635
     * `select sum(roaring::Roaring) where RowsetId=rowset_id and SegmentId=seg_id and Version <= version`
636
     *
637
     * @return shared_ptr to a bitmap, which may be empty
638
     */
639
    std::shared_ptr<roaring::Roaring> get_agg(const BitmapKey& bmk) const;
640
    std::shared_ptr<roaring::Roaring> get_agg_without_cache(const BitmapKey& bmk,
641
                                                            const int64_t start_version = 0) const;
642
643
    void remove_sentinel_marks();
644
645
    uint64_t get_delete_bitmap_count();
646
647
    void traverse_rowset_and_version(
648
            const std::function<int(const RowsetId& rowsetId, int64_t version)>& func) const;
649
650
    bool has_calculated_for_multi_segments(const RowsetId& rowset_id) const;
651
652
    // return the size of the map
653
    size_t remove_rowset_cache_version(const RowsetId& rowset_id);
654
655
    void clear_rowset_cache_version();
656
657
    std::set<std::string> get_rowset_cache_version();
658
659
    DeleteBitmap agg_cache_snapshot();
660
661
    void set_tablet_id(int64_t tablet_id);
662
663
    /**
664
     * Calculate diffset with given `key_set`. All entries with keys contained in this delete bitmap but not
665
     * in given key_set will be added to the output delete bitmap.
666
     *
667
     * @return Deletebitmap containning all entries in diffset
668
    */
669
    DeleteBitmap diffset(const std::set<BitmapKey>& key_set) const;
670
671
private:
672
    DeleteBitmap::Version _get_rowset_cache_version(const BitmapKey& bmk) const;
673
674
    int64_t _tablet_id;
675
    mutable std::shared_mutex _rowset_cache_version_lock;
676
    mutable std::map<RowsetId, std::map<SegmentId, Version>> _rowset_cache_version;
677
};
678
679
2.43M
inline TabletUid TabletMeta::tablet_uid() const {
680
2.43M
    return _tablet_uid;
681
2.43M
}
682
683
702k
inline int64_t TabletMeta::table_id() const {
684
702k
    return _table_id;
685
702k
}
686
687
528k
inline int64_t TabletMeta::index_id() const {
688
528k
    return _index_id;
689
528k
}
690
691
4.98M
inline int64_t TabletMeta::partition_id() const {
692
4.98M
    return _partition_id;
693
4.98M
}
694
695
154M
inline int64_t TabletMeta::tablet_id() const {
696
154M
    return _tablet_id;
697
154M
}
698
699
1.40M
inline int64_t TabletMeta::replica_id() const {
700
1.40M
    return _replica_id;
701
1.40M
}
702
703
1.87M
inline int32_t TabletMeta::schema_hash() const {
704
1.87M
    return _schema_hash;
705
1.87M
}
706
707
310k
inline int32_t TabletMeta::shard_id() const {
708
310k
    return _shard_id;
709
310k
}
710
711
189
inline void TabletMeta::set_shard_id(int32_t shard_id) {
712
189
    _shard_id = shard_id;
713
189
}
714
715
60.5k
inline int64_t TabletMeta::creation_time() const {
716
60.5k
    return _creation_time;
717
60.5k
}
718
719
inline void TabletMeta::set_creation_time(int64_t creation_time) {
720
    _creation_time = creation_time;
721
}
722
723
28.8k
inline int64_t TabletMeta::cumulative_layer_point() const {
724
28.8k
    return _cumulative_layer_point;
725
28.8k
}
726
727
0
inline void TabletMeta::set_cumulative_layer_point(int64_t new_point) {
728
0
    _cumulative_layer_point = new_point;
729
0
}
730
731
1.20M
inline size_t TabletMeta::num_rows() const {
732
1.20M
    size_t num_rows = 0;
733
2.14M
    for (const auto& [_, rs] : _rs_metas) {
734
2.14M
        num_rows += rs->num_rows();
735
2.14M
    }
736
1.20M
    return num_rows;
737
1.20M
}
738
739
31.4k
inline size_t TabletMeta::tablet_footprint() const {
740
31.4k
    size_t total_size = 0;
741
98.4k
    for (const auto& [_, rs] : _rs_metas) {
742
98.4k
        total_size += rs->total_disk_size();
743
98.4k
    }
744
31.4k
    return total_size;
745
31.4k
}
746
747
4.34M
inline size_t TabletMeta::tablet_local_size() const {
748
4.34M
    size_t total_size = 0;
749
7.72M
    for (const auto& [_, rs] : _rs_metas) {
750
7.72M
        if (rs->is_local()) {
751
7.69M
            total_size += rs->total_disk_size();
752
7.69M
        }
753
7.72M
    }
754
    // if we need to split data and binlog or not
755
4.34M
    total_size += binlog_size();
756
4.34M
    return total_size;
757
4.34M
}
758
759
4.34M
inline size_t TabletMeta::tablet_remote_size() const {
760
4.34M
    size_t total_size = 0;
761
7.72M
    for (const auto& [_, rs] : _rs_metas) {
762
7.72M
        if (!rs->is_local()) {
763
30.6k
            total_size += rs->total_disk_size();
764
30.6k
        }
765
7.72M
    }
766
4.34M
    return total_size;
767
4.34M
}
768
769
4.34M
inline size_t TabletMeta::binlog_size() const {
770
4.34M
    size_t total_size = 0;
771
4.34M
    for (auto& [_, rs] : _row_binlog_rs_metas) {
772
1.64k
        if (rs->is_local()) {
773
1.64k
            total_size += rs->data_disk_size();
774
1.64k
        }
775
1.64k
    }
776
4.34M
    return total_size;
777
4.34M
}
778
779
1.20M
inline size_t TabletMeta::tablet_local_index_size() const {
780
1.20M
    size_t total_size = 0;
781
2.14M
    for (const auto& [_, rs] : _rs_metas) {
782
2.14M
        if (rs->is_local()) {
783
2.14M
            total_size += rs->index_disk_size();
784
2.14M
        }
785
2.14M
    }
786
1.20M
    return total_size;
787
1.20M
}
788
789
1.20M
inline size_t TabletMeta::tablet_local_segment_size() const {
790
1.20M
    size_t total_size = 0;
791
2.14M
    for (const auto& [_, rs] : _rs_metas) {
792
2.14M
        if (rs->is_local()) {
793
2.14M
            total_size += rs->data_disk_size();
794
2.14M
        }
795
2.14M
    }
796
1.20M
    return total_size;
797
1.20M
}
798
799
1.20M
inline size_t TabletMeta::tablet_remote_index_size() const {
800
1.20M
    size_t total_size = 0;
801
2.14M
    for (const auto& [_, rs] : _rs_metas) {
802
2.14M
        if (!rs->is_local()) {
803
16
            total_size += rs->index_disk_size();
804
16
        }
805
2.14M
    }
806
1.20M
    return total_size;
807
1.20M
}
808
809
1.20M
inline size_t TabletMeta::tablet_remote_segment_size() const {
810
1.20M
    size_t total_size = 0;
811
2.14M
    for (const auto& [_, rs] : _rs_metas) {
812
2.14M
        if (!rs->is_local()) {
813
16
            total_size += rs->data_disk_size();
814
16
        }
815
2.14M
    }
816
1.20M
    return total_size;
817
1.20M
}
818
819
6.38M
inline size_t TabletMeta::version_count() const {
820
6.38M
    return _rs_metas.size();
821
6.38M
}
822
823
36.6k
inline size_t TabletMeta::stale_version_count() const {
824
36.6k
    return _rs_metas.size();
825
36.6k
}
826
827
242
inline size_t TabletMeta::binlog_file_num() const {
828
242
    return _row_binlog_rs_metas.size();
829
242
}
830
831
72.0M
inline TabletState TabletMeta::tablet_state() const {
832
72.0M
    return _tablet_state;
833
72.0M
}
834
835
6.00k
inline void TabletMeta::set_tablet_state(TabletState state) {
836
6.00k
    _tablet_state = state;
837
6.00k
}
838
839
28.9k
inline bool TabletMeta::in_restore_mode() const {
840
28.9k
    return _in_restore_mode;
841
28.9k
}
842
843
0
inline void TabletMeta::set_in_restore_mode(bool in_restore_mode) {
844
0
    _in_restore_mode = in_restore_mode;
845
0
}
846
847
746M
inline const TabletSchemaSPtr& TabletMeta::tablet_schema() const {
848
746M
    return _schema;
849
746M
}
850
851
3
inline TabletSchema* TabletMeta::mutable_tablet_schema() {
852
3
    return _schema.get();
853
3
}
854
855
9.90M
inline const RowsetMetaMapContainer& TabletMeta::all_rs_metas() const {
856
9.90M
    return _rs_metas;
857
9.90M
}
858
859
266k
inline RowsetMetaMapContainer& TabletMeta::all_mutable_rs_metas() {
860
266k
    return _rs_metas;
861
266k
}
862
863
579k
inline const RowsetMetaMapContainer& TabletMeta::all_stale_rs_metas() const {
864
579k
    return _stale_rs_metas;
865
579k
}
866
867
846k
inline const RowsetMetaMapContainer& TabletMeta::all_row_binlog_rs_metas() const {
868
846k
    return _row_binlog_rs_metas;
869
846k
}
870
871
0
inline bool TabletMeta::all_beta() const {
872
0
    for (const auto& [_, rs] : _rs_metas) {
873
0
        if (rs->rowset_type() != RowsetTypePB::BETA_ROWSET) {
874
0
            return false;
875
0
        }
876
0
    }
877
0
    for (const auto& [_, rs] : _stale_rs_metas) {
878
0
        if (rs->rowset_type() != RowsetTypePB::BETA_ROWSET) {
879
0
            return false;
880
0
        }
881
0
    }
882
0
    for (const auto& [_, rs] : _row_binlog_rs_metas) {
883
0
        if (rs->rowset_type() != RowsetTypePB::BETA_ROWSET) {
884
0
            return false;
885
0
        }
886
0
    }
887
0
    return true;
888
0
}
889
890
std::string tablet_state_name(TabletState state);
891
892
// Only for unit test now.
893
bool operator==(const TabletMeta& a, const TabletMeta& b);
894
bool operator!=(const TabletMeta& a, const TabletMeta& b);
895
896
} // namespace doris