Coverage Report

Created: 2025-06-09 19:49

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