Coverage Report

Created: 2025-10-01 20:59

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/root/doris/be/src/olap/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 "olap/binlog_config.h"
45
#include "olap/lru_cache.h"
46
#include "olap/metadata_adder.h"
47
#include "olap/olap_common.h"
48
#include "olap/rowset/rowset_meta.h"
49
#include "olap/tablet_schema.h"
50
#include "runtime/memory/lru_cache_policy.h"
51
#include "util/uid_util.h"
52
53
namespace json2pb {
54
#include "common/compile_check_begin.h"
55
struct Pb2JsonOptions;
56
} // namespace json2pb
57
58
namespace doris {
59
class TColumn;
60
61
// Lifecycle states that a Tablet can be in. Legal state transitions for a
62
// Tablet object:
63
//
64
//   NOTREADY -> RUNNING -> TOMBSTONED -> STOPPED -> SHUTDOWN
65
//      |           |            |          ^^^
66
//      |           |            +----------++|
67
//      |           +------------------------+|
68
//      +-------------------------------------+
69
70
enum TabletState {
71
    // Tablet is under alter table, rollup, clone
72
    TABLET_NOTREADY,
73
74
    TABLET_RUNNING,
75
76
    // Tablet integrity has been violated, such as missing versions.
77
    // In this state, tablet will not accept any incoming request.
78
    // Report this state to FE, scheduling BE to drop tablet.
79
    TABLET_TOMBSTONED,
80
81
    // Tablet is shutting down, files in disk still remained.
82
    TABLET_STOPPED,
83
84
    // Files have been removed, tablet has been shutdown completely.
85
    TABLET_SHUTDOWN
86
};
87
88
class DataDir;
89
class TabletMeta;
90
class DeleteBitmap;
91
class TBinlogConfig;
92
93
// Class encapsulates meta of tablet.
94
// The concurrency control is handled in Tablet Class, not in this class.
95
class TabletMeta : public MetadataAdder<TabletMeta> {
96
public:
97
    static TabletMetaSharedPtr create(
98
            const TCreateTabletReq& request, const TabletUid& tablet_uid, uint64_t shard_id,
99
            uint32_t next_unique_id,
100
            const std::unordered_map<uint32_t, uint32_t>& col_ordinal_to_unique_id);
101
102
    TabletMeta();
103
    ~TabletMeta() override;
104
    TabletMeta(int64_t table_id, int64_t partition_id, int64_t tablet_id, int64_t replica_id,
105
               int32_t schema_hash, int32_t shard_id, const TTabletSchema& tablet_schema,
106
               uint32_t next_unique_id,
107
               const std::unordered_map<uint32_t, uint32_t>& col_ordinal_to_unique_id,
108
               TabletUid tablet_uid, TTabletType::type tabletType,
109
               TCompressionType::type compression_type, int64_t storage_policy_id = 0,
110
               bool enable_unique_key_merge_on_write = false,
111
               std::optional<TBinlogConfig> binlog_config = {},
112
               std::string compaction_policy = "size_based",
113
               int64_t time_series_compaction_goal_size_mbytes = 1024,
114
               int64_t time_series_compaction_file_count_threshold = 2000,
115
               int64_t time_series_compaction_time_threshold_seconds = 3600,
116
               int64_t time_series_compaction_empty_rowsets_threshold = 5,
117
               int64_t time_series_compaction_level_threshold = 1,
118
               TInvertedIndexFileStorageFormat::type inverted_index_file_storage_format =
119
                       TInvertedIndexFileStorageFormat::V2,
120
               TEncryptionAlgorithm::type tde_algorithm = TEncryptionAlgorithm::PLAINTEXT);
121
    // If need add a filed in TableMeta, filed init copy in copy construct function
122
    TabletMeta(const TabletMeta& tablet_meta);
123
    TabletMeta(TabletMeta&& tablet_meta) = delete;
124
125
// UT
126
#ifdef BE_TEST
127
48
    TabletMeta(TabletSchemaSPtr tablet_schema) : _schema(tablet_schema) {}
128
#endif
129
130
    // Function create_from_file is used to be compatible with previous tablet_meta.
131
    // Previous tablet_meta is a physical file in tablet dir, which is not stored in rocksdb.
132
    Status create_from_file(const std::string& file_path);
133
    // Used to create tablet meta from memory buffer.
134
    Status create_from_buffer(const uint8_t* buffer, size_t buffer_size);
135
    static Status load_from_file(const std::string& file_path, TabletMetaPB* tablet_meta_pb);
136
    Status save(const std::string& file_path);
137
    Status save_as_json(const std::string& file_path);
138
    static Status save(const std::string& file_path, const TabletMetaPB& tablet_meta_pb);
139
    static std::string construct_header_file_path(const std::string& schema_hash_path,
140
                                                  int64_t tablet_id);
141
    Status save_meta(DataDir* data_dir);
142
143
    void serialize(std::string* meta_binary);
144
    Status deserialize(std::string_view meta_binary);
145
    void init_from_pb(const TabletMetaPB& tablet_meta_pb);
146
147
    void to_meta_pb(TabletMetaPB* tablet_meta_pb);
148
    void to_json(std::string* json_string, json2pb::Pb2JsonOptions& options);
149
559
    size_t tablet_columns_num() const { return _schema->num_columns(); }
150
151
0
    TabletTypePB tablet_type() const { return _tablet_type; }
152
    TabletUid tablet_uid() const;
153
33
    void set_tablet_uid(TabletUid uid) { _tablet_uid = uid; }
154
    int64_t table_id() const;
155
    int64_t index_id() const;
156
    int64_t partition_id() const;
157
    int64_t tablet_id() const;
158
    int64_t replica_id() const;
159
0
    void set_replica_id(int64_t replica_id) { _replica_id = replica_id; }
160
    int32_t schema_hash() const;
161
    int32_t shard_id() const;
162
    void set_shard_id(int32_t shard_id);
163
    int64_t creation_time() const;
164
    void set_creation_time(int64_t creation_time);
165
    int64_t cumulative_layer_point() const;
166
    void set_cumulative_layer_point(int64_t new_point);
167
168
    size_t num_rows() const;
169
    // Disk space occupied by tablet, contain local and remote.
170
    size_t tablet_footprint() const;
171
    // Local disk space occupied by tablet.
172
    size_t tablet_local_size() const;
173
    // Remote disk space occupied by tablet.
174
    size_t tablet_remote_size() const;
175
176
    size_t tablet_local_index_size() const;
177
    size_t tablet_local_segment_size() const;
178
    size_t tablet_remote_index_size() const;
179
    size_t tablet_remote_segment_size() const;
180
181
    size_t version_count() const;
182
    size_t stale_version_count() const;
183
    size_t version_count_cross_with_range(const Version& range) const;
184
    Version max_version() const;
185
186
    TabletState tablet_state() const;
187
    void set_tablet_state(TabletState state);
188
189
    bool in_restore_mode() const;
190
    void set_in_restore_mode(bool in_restore_mode);
191
192
    const TabletSchemaSPtr& tablet_schema() const;
193
194
    TabletSchema* mutable_tablet_schema();
195
196
    const RowsetMetaMapContainer& all_rs_metas() const;
197
    RowsetMetaMapContainer& all_mutable_rs_metas();
198
    Status add_rs_meta(const RowsetMetaSharedPtr& rs_meta);
199
    void delete_rs_meta_by_version(const Version& version,
200
                                   std::vector<RowsetMetaSharedPtr>* deleted_rs_metas);
201
    // If same_version is true, the rowset in "to_delete" will not be added
202
    // to _stale_rs_meta, but to be deleted from rs_meta directly.
203
    void modify_rs_metas(const std::vector<RowsetMetaSharedPtr>& to_add,
204
                         const std::vector<RowsetMetaSharedPtr>& to_delete,
205
                         bool same_version = false);
206
    void revise_rs_metas(std::vector<RowsetMetaSharedPtr>&& rs_metas);
207
    void revise_delete_bitmap_unlocked(const DeleteBitmap& delete_bitmap);
208
209
    const RowsetMetaMapContainer& all_stale_rs_metas() const;
210
    RowsetMetaSharedPtr acquire_rs_meta_by_version(const Version& version) const;
211
    void delete_stale_rs_meta_by_version(const Version& version);
212
    RowsetMetaSharedPtr acquire_stale_rs_meta_by_version(const Version& version) const;
213
214
    Status set_partition_id(int64_t partition_id);
215
216
387
    RowsetTypePB preferred_rowset_type() const { return _preferred_rowset_type; }
217
218
52
    void set_preferred_rowset_type(RowsetTypePB preferred_rowset_type) {
219
52
        _preferred_rowset_type = preferred_rowset_type;
220
52
    }
221
222
    // used for after tablet cloned to clear stale rowset
223
    void clear_stale_rowset();
224
225
    void clear_rowsets();
226
227
    // MUST hold EXCLUSIVE `_meta_lock` in belonged Tablet
228
    // `to_add` MUST NOT have overlapped version with `_rs_metas` in tablet meta.
229
    void add_rowsets_unchecked(const std::vector<RowsetSharedPtr>& to_add);
230
231
    bool all_beta() const;
232
233
22
    int64_t storage_policy_id() const { return _storage_policy_id; }
234
235
4
    void set_storage_policy_id(int64_t id) {
236
4
        VLOG_NOTICE << "set tablet_id : " << _table_id << " storage policy from "
237
0
                    << _storage_policy_id << " to " << id;
238
4
        _storage_policy_id = id;
239
4
    }
240
241
18
    UniqueId cooldown_meta_id() const { return _cooldown_meta_id; }
242
5
    void set_cooldown_meta_id(UniqueId uid) { _cooldown_meta_id = uid; }
243
244
    static void init_column_from_tcolumn(uint32_t unique_id, const TColumn& tcolumn,
245
                                         ColumnPB* column);
246
247
2
    DeleteBitmapPtr delete_bitmap_ptr() { return _delete_bitmap; }
248
19
    DeleteBitmap& delete_bitmap() { return *_delete_bitmap; }
249
    void remove_rowset_delete_bitmap(const RowsetId& rowset_id, const Version& version);
250
251
1.21k
    bool enable_unique_key_merge_on_write() const { return _enable_unique_key_merge_on_write; }
252
#ifdef BE_TEST
253
33
    void set_enable_unique_key_merge_on_write(bool value) {
254
33
        _enable_unique_key_merge_on_write = value;
255
33
    }
256
#endif
257
    // TODO(Drogon): thread safety
258
0
    const BinlogConfig& binlog_config() const { return _binlog_config; }
259
0
    void set_binlog_config(BinlogConfig binlog_config) {
260
0
        _binlog_config = std::move(binlog_config);
261
0
    }
262
263
12
    void set_compaction_policy(std::string compaction_policy) {
264
12
        _compaction_policy = compaction_policy;
265
12
    }
266
1.66k
    std::string compaction_policy() const { return _compaction_policy; }
267
12
    void set_time_series_compaction_goal_size_mbytes(int64_t goal_size_mbytes) {
268
12
        _time_series_compaction_goal_size_mbytes = goal_size_mbytes;
269
12
    }
270
823
    int64_t time_series_compaction_goal_size_mbytes() const {
271
823
        return _time_series_compaction_goal_size_mbytes;
272
823
    }
273
12
    void set_time_series_compaction_file_count_threshold(int64_t file_count_threshold) {
274
12
        _time_series_compaction_file_count_threshold = file_count_threshold;
275
12
    }
276
823
    int64_t time_series_compaction_file_count_threshold() const {
277
823
        return _time_series_compaction_file_count_threshold;
278
823
    }
279
13
    void set_time_series_compaction_time_threshold_seconds(int64_t time_threshold) {
280
13
        _time_series_compaction_time_threshold_seconds = time_threshold;
281
13
    }
282
823
    int64_t time_series_compaction_time_threshold_seconds() const {
283
823
        return _time_series_compaction_time_threshold_seconds;
284
823
    }
285
0
    void set_time_series_compaction_empty_rowsets_threshold(int64_t empty_rowsets_threshold) {
286
0
        _time_series_compaction_empty_rowsets_threshold = empty_rowsets_threshold;
287
0
    }
288
818
    int64_t time_series_compaction_empty_rowsets_threshold() const {
289
818
        return _time_series_compaction_empty_rowsets_threshold;
290
818
    }
291
0
    void set_time_series_compaction_level_threshold(int64_t level_threshold) {
292
0
        _time_series_compaction_level_threshold = level_threshold;
293
0
    }
294
843
    int64_t time_series_compaction_level_threshold() const {
295
843
        return _time_series_compaction_level_threshold;
296
843
    }
297
298
337
    int64_t ttl_seconds() const {
299
337
        std::shared_lock rlock(_meta_lock);
300
337
        return _ttl_seconds;
301
337
    }
302
303
0
    void set_ttl_seconds(int64_t ttl_seconds) {
304
0
        std::lock_guard wlock(_meta_lock);
305
0
        _ttl_seconds = ttl_seconds;
306
0
    }
307
308
28
    int64_t avg_rs_meta_serialize_size() const { return _avg_rs_meta_serialize_size; }
309
310
387
    EncryptionAlgorithmPB encryption_algorithm() const { return _encryption_algorithm; }
311
312
private:
313
    Status _save_meta(DataDir* data_dir);
314
    void _check_mow_rowset_cache_version_size(size_t rowset_cache_version_size);
315
316
    // _del_predicates is ignored to compare.
317
    friend bool operator==(const TabletMeta& a, const TabletMeta& b);
318
    friend bool operator!=(const TabletMeta& a, const TabletMeta& b);
319
320
private:
321
    int64_t _table_id = 0;
322
    int64_t _index_id = 0;
323
    int64_t _partition_id = 0;
324
    int64_t _tablet_id = 0;
325
    int64_t _replica_id = 0;
326
    int32_t _schema_hash = 0;
327
    int32_t _shard_id = 0;
328
    int64_t _creation_time = 0;
329
    int64_t _cumulative_layer_point = 0;
330
    TabletUid _tablet_uid;
331
    TabletTypePB _tablet_type = TabletTypePB::TABLET_TYPE_DISK;
332
333
    TabletState _tablet_state = TABLET_NOTREADY;
334
    // the reference of _schema may use in tablet, so here need keep
335
    // the lifetime of tablemeta and _schema is same with tablet
336
    TabletSchemaSPtr _schema;
337
    Cache::Handle* _handle = nullptr;
338
339
    RowsetMetaMapContainer _rs_metas;
340
    // This variable _stale_rs_metas is used to record these rowsets‘ meta which are be compacted.
341
    // These stale rowsets meta are been removed when rowsets' pathVersion is expired,
342
    // this policy is judged and computed by TimestampedVersionTracker.
343
    RowsetMetaMapContainer _stale_rs_metas;
344
    bool _in_restore_mode = false;
345
    RowsetTypePB _preferred_rowset_type = BETA_ROWSET;
346
347
    // meta for cooldown
348
    int64_t _storage_policy_id = 0; // <= 0 means no storage policy
349
    UniqueId _cooldown_meta_id;
350
351
    // For unique key data model, the feature Merge-on-Write will leverage a primary
352
    // key index and a delete-bitmap to mark duplicate keys as deleted in load stage,
353
    // which can avoid the merging cost in read stage, and accelerate the aggregation
354
    // query performance significantly.
355
    bool _enable_unique_key_merge_on_write = false;
356
    std::shared_ptr<DeleteBitmap> _delete_bitmap;
357
358
    // binlog config
359
    BinlogConfig _binlog_config {};
360
361
    // meta for compaction
362
    std::string _compaction_policy;
363
    int64_t _time_series_compaction_goal_size_mbytes = 0;
364
    int64_t _time_series_compaction_file_count_threshold = 0;
365
    int64_t _time_series_compaction_time_threshold_seconds = 0;
366
    int64_t _time_series_compaction_empty_rowsets_threshold = 0;
367
    int64_t _time_series_compaction_level_threshold = 0;
368
369
    int64_t _avg_rs_meta_serialize_size = 0;
370
371
    // cloud
372
    int64_t _ttl_seconds = 0;
373
374
    EncryptionAlgorithmPB _encryption_algorithm = PLAINTEXT;
375
376
    mutable std::shared_mutex _meta_lock;
377
};
378
379
class DeleteBitmapAggCache : public LRUCachePolicy {
380
public:
381
    DeleteBitmapAggCache(size_t capacity);
382
383
    static DeleteBitmapAggCache* instance();
384
385
    static DeleteBitmapAggCache* create_instance(size_t capacity);
386
387
    DeleteBitmap snapshot(int64_t tablet_id);
388
389
    class Value : public LRUCacheValueBase {
390
    public:
391
        roaring::Roaring bitmap;
392
    };
393
};
394
395
/**
396
 * Wraps multiple bitmaps for recording rows (row id) that are deleted or
397
 * overwritten. For now, it's only used when unique key merge-on-write property
398
 * enabled.
399
 *
400
 * RowsetId and SegmentId are for locating segment, Version here is a single
401
 * uint32_t means that at which "version" of the load causes the delete or
402
 * overwrite.
403
 *
404
 * The start and end version of a load is the same, it's ok and straightforward
405
 * to use a single uint32_t.
406
 *
407
 * e.g.
408
 * There is a key "key1" in rowset id 1, version [1,1], segment id 1, row id 1.
409
 * A new load also contains "key1", the rowset id 2, version [2,2], segment id 1
410
 * the delete bitmap will be `{1,1,2} -> 1`, which means the "row id 1" in
411
 * "rowset id 1, segment id 1" is deleted/overitten by some loads at "version 2"
412
 */
413
class DeleteBitmap {
414
public:
415
    mutable std::shared_mutex lock;
416
    using SegmentId = uint32_t;
417
    using Version = uint64_t;
418
    using BitmapKey = std::tuple<RowsetId, SegmentId, Version>;
419
    std::map<BitmapKey, roaring::Roaring> delete_bitmap; // Ordered map
420
    constexpr static inline uint32_t INVALID_SEGMENT_ID = std::numeric_limits<uint32_t>::max() - 1;
421
    constexpr static inline uint32_t ROWSET_SENTINEL_MARK =
422
            std::numeric_limits<uint32_t>::max() - 1;
423
424
    // When a delete bitmap is merged into tablet's delete bitmap, the version of entries in the delete bitmap
425
    // will be replaced to the correspoding correct version. So before we finally merge a delete bitmap into
426
    // tablet's delete bitmap we can use arbitary version number in BitmapKey. Here we define some version numbers
427
    // for specific usage during this periods to avoid conflicts
428
    constexpr static inline uint64_t TEMP_VERSION_COMMON = 0;
429
430
    /**
431
     * 
432
     * @param tablet_id the tablet which this delete bitmap associates with
433
     */
434
    DeleteBitmap(int64_t tablet_id);
435
436
    /**
437
     * Copy c-tor for making delete bitmap snapshot on read path
438
     */
439
    DeleteBitmap(const DeleteBitmap& r);
440
    DeleteBitmap& operator=(const DeleteBitmap& r);
441
    /**
442
     * Move c-tor for making delete bitmap snapshot on read path
443
     */
444
    DeleteBitmap(DeleteBitmap&& r) noexcept;
445
    DeleteBitmap& operator=(DeleteBitmap&& r) noexcept;
446
447
    /**
448
     * Makes a snapshot of delete bitmap, read lock will be acquired in this
449
     * process
450
     */
451
    DeleteBitmap snapshot() const;
452
453
    /**
454
     * Makes a snapshot of delete bitmap on given version, read lock will be
455
     * acquired temporary in this process
456
     */
457
    DeleteBitmap snapshot(Version version) const;
458
459
    /**
460
     * Marks the specific row deleted
461
     */
462
    void add(const BitmapKey& bmk, uint32_t row_id);
463
464
    /**
465
     * Clears the deletetion mark specific row
466
     *
467
     * @return non-zero if the associated delete bitmap does not exist
468
     */
469
    int remove(const BitmapKey& bmk, uint32_t row_id);
470
471
    /**
472
     * Clears bitmaps in range [lower_key, upper_key)
473
     */
474
    void remove(const BitmapKey& lower_key, const BitmapKey& upper_key);
475
    void remove(const std::vector<std::tuple<BitmapKey, BitmapKey>>& key_ranges);
476
477
    /**
478
     * Checks if the given row is marked deleted
479
     *
480
     * @return true if marked deleted
481
     */
482
    bool contains(const BitmapKey& bmk, uint32_t row_id) const;
483
484
    /**
485
     * Checks if this delete bitmap is empty
486
     *
487
     * @return true if empty
488
     */
489
    bool empty() const;
490
491
    /**
492
     * return the total cardinality of the Delete Bitmap
493
     */
494
    uint64_t cardinality() const;
495
496
    /**
497
     * return the total size of the Delete Bitmap(after serialized)
498
     */
499
500
    uint64_t get_size() const;
501
502
    /**
503
     * Sets the bitmap of specific segment, it's may be insertion or replacement
504
     *
505
     * @return 1 if the insertion took place, 0 if the assignment took place
506
     */
507
    int set(const BitmapKey& bmk, const roaring::Roaring& segment_delete_bitmap);
508
509
    /**
510
     * Gets a copy of specific delete bmk
511
     *
512
     * @param segment_delete_bitmap output param
513
     * @return non-zero if the associated delete bitmap does not exist
514
     */
515
    int get(const BitmapKey& bmk, roaring::Roaring* segment_delete_bitmap) const;
516
517
    /**
518
     * Gets reference to a specific delete map, DO NOT use this function on a
519
     * mutable DeleteBitmap object
520
     * @return nullptr if the given bitmap does not exist
521
     */
522
    const roaring::Roaring* get(const BitmapKey& bmk) const;
523
524
    /**
525
     * Gets subset of delete_bitmap with given range [start, end)
526
     *
527
     * @parma start start
528
     * @parma end end
529
     * @parma subset_delete_map output param
530
     */
531
    void subset(const BitmapKey& start, const BitmapKey& end,
532
                DeleteBitmap* subset_delete_map) const;
533
    void subset(std::vector<std::pair<RowsetId, int64_t>>& rowset_ids, int64_t start_version,
534
                int64_t end_version, DeleteBitmap* subset_delete_map) const;
535
536
    /**
537
     * Gets subset of delete_bitmap of the input rowsets
538
     * with given version range [start_version, end_version] and agg to end_version,
539
     * then merge to subset_delete_map
540
     */
541
    void subset_and_agg(std::vector<std::pair<RowsetId, int64_t>>& rowset_ids,
542
                        int64_t start_version, int64_t end_version,
543
                        DeleteBitmap* subset_delete_map) const;
544
545
    /**
546
     * Gets count of delete_bitmap with given range [start, end)
547
     *
548
     * @parma start start
549
     * @parma end end
550
     */
551
    size_t get_count_with_range(const BitmapKey& start, const BitmapKey& end) const;
552
553
    /**
554
     * Merges the given segment delete bitmap into *this
555
     *
556
     * @param bmk
557
     * @param segment_delete_bitmap
558
     */
559
    void merge(const BitmapKey& bmk, const roaring::Roaring& segment_delete_bitmap);
560
561
    /**
562
     * Merges the given delete bitmap into *this
563
     *
564
     * @param other
565
     */
566
    void merge(const DeleteBitmap& other);
567
568
    /**
569
     * Checks if the given row is marked deleted in bitmap with the condition:
570
     * all the bitmaps that
571
     * RowsetId and SegmentId are the same as the given ones,
572
     * and Version <= the given Version
573
     *
574
     * Note: aggregation cache may be used.
575
     *
576
     * @return true if marked deleted
577
     */
578
    bool contains_agg(const BitmapKey& bitmap, uint32_t row_id) const;
579
580
    bool contains_agg_with_cache_if_eligible(const BitmapKey& bmk, uint32_t row_id) const;
581
    /**
582
     * Gets aggregated delete_bitmap on rowset_id and version, the same effect:
583
     * `select sum(roaring::Roaring) where RowsetId=rowset_id and SegmentId=seg_id and Version <= version`
584
     *
585
     * @return shared_ptr to a bitmap, which may be empty
586
     */
587
    std::shared_ptr<roaring::Roaring> get_agg(const BitmapKey& bmk) const;
588
    std::shared_ptr<roaring::Roaring> get_agg_without_cache(const BitmapKey& bmk,
589
                                                            const int64_t start_version = 0) const;
590
591
    void remove_sentinel_marks();
592
593
    uint64_t get_delete_bitmap_count();
594
595
    void traverse_rowset_and_version(
596
            const std::function<int(const RowsetId& rowsetId, int64_t version)>& func) const;
597
598
    bool has_calculated_for_multi_segments(const RowsetId& rowset_id) const;
599
600
    // return the size of the map
601
    size_t remove_rowset_cache_version(const RowsetId& rowset_id);
602
603
    void clear_rowset_cache_version();
604
605
    std::set<std::string> get_rowset_cache_version();
606
607
    DeleteBitmap agg_cache_snapshot();
608
609
    void set_tablet_id(int64_t tablet_id);
610
611
private:
612
    DeleteBitmap::Version _get_rowset_cache_version(const BitmapKey& bmk) const;
613
614
    int64_t _tablet_id;
615
    mutable std::shared_mutex _rowset_cache_version_lock;
616
    mutable std::map<RowsetId, std::map<SegmentId, Version>> _rowset_cache_version;
617
};
618
619
13.4k
inline TabletUid TabletMeta::tablet_uid() const {
620
13.4k
    return _tablet_uid;
621
13.4k
}
622
623
828
inline int64_t TabletMeta::table_id() const {
624
828
    return _table_id;
625
828
}
626
627
842
inline int64_t TabletMeta::index_id() const {
628
842
    return _index_id;
629
842
}
630
631
2.32k
inline int64_t TabletMeta::partition_id() const {
632
2.32k
    return _partition_id;
633
2.32k
}
634
635
19.9k
inline int64_t TabletMeta::tablet_id() const {
636
19.9k
    return _tablet_id;
637
19.9k
}
638
639
1.09k
inline int64_t TabletMeta::replica_id() const {
640
1.09k
    return _replica_id;
641
1.09k
}
642
643
2.89k
inline int32_t TabletMeta::schema_hash() const {
644
2.89k
    return _schema_hash;
645
2.89k
}
646
647
1.55k
inline int32_t TabletMeta::shard_id() const {
648
1.55k
    return _shard_id;
649
1.55k
}
650
651
17
inline void TabletMeta::set_shard_id(int32_t shard_id) {
652
17
    _shard_id = shard_id;
653
17
}
654
655
814
inline int64_t TabletMeta::creation_time() const {
656
814
    return _creation_time;
657
814
}
658
659
0
inline void TabletMeta::set_creation_time(int64_t creation_time) {
660
0
    _creation_time = creation_time;
661
0
}
662
663
814
inline int64_t TabletMeta::cumulative_layer_point() const {
664
814
    return _cumulative_layer_point;
665
814
}
666
667
0
inline void TabletMeta::set_cumulative_layer_point(int64_t new_point) {
668
0
    _cumulative_layer_point = new_point;
669
0
}
670
671
19
inline size_t TabletMeta::num_rows() const {
672
19
    size_t num_rows = 0;
673
32
    for (const auto& [_, rs] : _rs_metas) {
674
32
        num_rows += rs->num_rows();
675
32
    }
676
19
    return num_rows;
677
19
}
678
679
8
inline size_t TabletMeta::tablet_footprint() const {
680
8
    size_t total_size = 0;
681
26
    for (const auto& [_, rs] : _rs_metas) {
682
26
        total_size += rs->total_disk_size();
683
26
    }
684
8
    return total_size;
685
8
}
686
687
0
inline size_t TabletMeta::tablet_local_size() const {
688
0
    size_t total_size = 0;
689
0
    for (const auto& [_, rs] : _rs_metas) {
690
0
        if (rs->is_local()) {
691
0
            total_size += rs->total_disk_size();
692
0
        }
693
0
    }
694
0
    return total_size;
695
0
}
696
697
0
inline size_t TabletMeta::tablet_remote_size() const {
698
0
    size_t total_size = 0;
699
0
    for (const auto& [_, rs] : _rs_metas) {
700
0
        if (!rs->is_local()) {
701
0
            total_size += rs->total_disk_size();
702
0
        }
703
0
    }
704
0
    return total_size;
705
0
}
706
707
0
inline size_t TabletMeta::tablet_local_index_size() const {
708
0
    size_t total_size = 0;
709
0
    for (const auto& [_, rs] : _rs_metas) {
710
0
        if (rs->is_local()) {
711
0
            total_size += rs->index_disk_size();
712
0
        }
713
0
    }
714
0
    return total_size;
715
0
}
716
717
0
inline size_t TabletMeta::tablet_local_segment_size() const {
718
0
    size_t total_size = 0;
719
0
    for (const auto& [_, rs] : _rs_metas) {
720
0
        if (rs->is_local()) {
721
0
            total_size += rs->data_disk_size();
722
0
        }
723
0
    }
724
0
    return total_size;
725
0
}
726
727
0
inline size_t TabletMeta::tablet_remote_index_size() const {
728
0
    size_t total_size = 0;
729
0
    for (const auto& [_, rs] : _rs_metas) {
730
0
        if (!rs->is_local()) {
731
0
            total_size += rs->index_disk_size();
732
0
        }
733
0
    }
734
0
    return total_size;
735
0
}
736
737
0
inline size_t TabletMeta::tablet_remote_segment_size() const {
738
0
    size_t total_size = 0;
739
0
    for (const auto& [_, rs] : _rs_metas) {
740
0
        if (!rs->is_local()) {
741
0
            total_size += rs->data_disk_size();
742
0
        }
743
0
    }
744
0
    return total_size;
745
0
}
746
747
56
inline size_t TabletMeta::version_count() const {
748
56
    return _rs_metas.size();
749
56
}
750
751
28
inline size_t TabletMeta::stale_version_count() const {
752
28
    return _rs_metas.size();
753
28
}
754
755
10.6k
inline TabletState TabletMeta::tablet_state() const {
756
10.6k
    return _tablet_state;
757
10.6k
}
758
759
257
inline void TabletMeta::set_tablet_state(TabletState state) {
760
257
    _tablet_state = state;
761
257
}
762
763
814
inline bool TabletMeta::in_restore_mode() const {
764
814
    return _in_restore_mode;
765
814
}
766
767
0
inline void TabletMeta::set_in_restore_mode(bool in_restore_mode) {
768
0
    _in_restore_mode = in_restore_mode;
769
0
}
770
771
3.25k
inline const TabletSchemaSPtr& TabletMeta::tablet_schema() const {
772
3.25k
    return _schema;
773
3.25k
}
774
775
0
inline TabletSchema* TabletMeta::mutable_tablet_schema() {
776
0
    return _schema.get();
777
0
}
778
779
2.34k
inline const RowsetMetaMapContainer& TabletMeta::all_rs_metas() const {
780
2.34k
    return _rs_metas;
781
2.34k
}
782
783
0
inline RowsetMetaMapContainer& TabletMeta::all_mutable_rs_metas() {
784
0
    return _rs_metas;
785
0
}
786
787
1.20k
inline const RowsetMetaMapContainer& TabletMeta::all_stale_rs_metas() const {
788
1.20k
    return _stale_rs_metas;
789
1.20k
}
790
791
0
inline bool TabletMeta::all_beta() const {
792
0
    for (const auto& [_, rs] : _rs_metas) {
793
0
        if (rs->rowset_type() != RowsetTypePB::BETA_ROWSET) {
794
0
            return false;
795
0
        }
796
0
    }
797
0
    for (const auto& [_, rs] : _stale_rs_metas) {
798
0
        if (rs->rowset_type() != RowsetTypePB::BETA_ROWSET) {
799
0
            return false;
800
0
        }
801
0
    }
802
0
    return true;
803
0
}
804
805
std::string tablet_state_name(TabletState state);
806
807
// Only for unit test now.
808
bool operator==(const TabletMeta& a, const TabletMeta& b);
809
bool operator!=(const TabletMeta& a, const TabletMeta& b);
810
811
#include "common/compile_check_end.h"
812
} // namespace doris