Coverage Report

Created: 2026-04-20 20:34

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/storage/rowset/rowset_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
#ifndef DORIS_BE_SRC_OLAP_ROWSET_ROWSET_META_H
19
#define DORIS_BE_SRC_OLAP_ROWSET_ROWSET_META_H
20
21
#include <gen_cpp/olap_file.pb.h>
22
#include <glog/logging.h>
23
24
#include <atomic>
25
#include <chrono>
26
#include <cstdint>
27
#include <memory>
28
#include <string>
29
#include <vector>
30
31
#include "common/cast_set.h"
32
#include "common/config.h"
33
#include "common/status.h"
34
#include "io/fs/encrypted_fs_factory.h"
35
#include "io/fs/file_system.h"
36
#include "runtime/memory/lru_cache_policy.h"
37
#include "storage/metadata_adder.h"
38
#include "storage/olap_common.h"
39
#include "storage/rowset/rowset_fwd.h"
40
#include "storage/storage_policy.h"
41
#include "storage/tablet/tablet_fwd.h"
42
#include "util/once.h"
43
44
namespace doris {
45
46
class RowsetMeta : public MetadataAdder<RowsetMeta> {
47
public:
48
28.0k
    RowsetMeta() = default;
49
    ~RowsetMeta();
50
51
    bool init(std::string_view pb_rowset_meta);
52
53
    bool init(const RowsetMeta* rowset_meta);
54
55
    bool init_from_pb(const RowsetMetaPB& rowset_meta_pb);
56
57
    bool init_from_json(const std::string& json_rowset_meta);
58
59
0
    bool serialize(std::string* value) { return _serialize_to_pb(value); }
60
61
    bool json_rowset_meta(std::string* json_rowset_meta);
62
63
    // If the rowset is a local rowset, return the global local file system.
64
    // Otherwise, return the remote file system corresponding to rowset's resource id.
65
    // Note that if the resource id cannot be found for the corresponding remote file system, nullptr will be returned.
66
    MOCK_FUNCTION io::FileSystemSPtr fs();
67
68
    io::FileSystemSPtr physical_fs();
69
70
    Result<const StorageResource*> remote_storage_resource();
71
72
    void set_remote_storage_resource(StorageResource resource);
73
74
33
    const std::string& resource_id() const { return _rowset_meta_pb.resource_id(); }
75
76
19
    void set_resource_id(const std::string& resource_id) {
77
19
        _rowset_meta_pb.set_resource_id(resource_id);
78
19
    }
79
80
26.7k
    bool is_local() const { return !_rowset_meta_pb.has_resource_id(); }
81
82
    bool has_variant_type_in_schema() const;
83
84
2.75M
    RowsetId rowset_id() const { return _rowset_id; }
85
86
12.6k
    void set_rowset_id(const RowsetId& rowset_id) {
87
        // rowset id is a required field, just set it to 0
88
12.6k
        _rowset_meta_pb.set_rowset_id(0);
89
12.6k
        _rowset_id = rowset_id;
90
12.6k
        _rowset_meta_pb.set_rowset_id_v2(rowset_id.to_string());
91
12.6k
    }
92
93
13.4k
    int64_t tablet_id() const { return _rowset_meta_pb.tablet_id(); }
94
95
11.7k
    void set_tablet_id(int64_t tablet_id) { _rowset_meta_pb.set_tablet_id(tablet_id); }
96
97
1
    int64_t index_id() const { return _rowset_meta_pb.index_id(); }
98
99
40
    void set_index_id(int64_t index_id) { _rowset_meta_pb.set_index_id(index_id); }
100
101
21
    TabletUid tablet_uid() const { return _rowset_meta_pb.tablet_uid(); }
102
103
11.6k
    void set_tablet_uid(TabletUid tablet_uid) {
104
11.6k
        *(_rowset_meta_pb.mutable_tablet_uid()) = tablet_uid.to_proto();
105
11.6k
    }
106
107
22
    int64_t txn_id() const { return _rowset_meta_pb.txn_id(); }
108
109
140
    void set_txn_id(int64_t txn_id) { _rowset_meta_pb.set_txn_id(txn_id); }
110
111
31
    int32_t tablet_schema_hash() const { return _rowset_meta_pb.tablet_schema_hash(); }
112
113
1.10k
    void set_tablet_schema_hash(int32_t tablet_schema_hash) {
114
1.10k
        _rowset_meta_pb.set_tablet_schema_hash(tablet_schema_hash);
115
1.10k
    }
116
117
12.5k
    RowsetTypePB rowset_type() const { return _rowset_meta_pb.rowset_type(); }
118
119
2.91k
    void set_rowset_type(RowsetTypePB rowset_type) { _rowset_meta_pb.set_rowset_type(rowset_type); }
120
121
16.1k
    RowsetStatePB rowset_state() const { return _rowset_meta_pb.rowset_state(); }
122
123
2.12k
    void set_rowset_state(RowsetStatePB rowset_state) {
124
2.12k
        _rowset_meta_pb.set_rowset_state(rowset_state);
125
2.12k
    }
126
127
2.61M
    Version version() const {
128
2.61M
        return {_rowset_meta_pb.start_version(), _rowset_meta_pb.end_version()};
129
2.61M
    }
130
131
12.4k
    void set_version(Version version) {
132
12.4k
        _rowset_meta_pb.set_start_version(version.first);
133
12.4k
        _rowset_meta_pb.set_end_version(version.second);
134
12.4k
    }
135
136
17.8k
    bool has_version() const {
137
17.8k
        return _rowset_meta_pb.has_start_version() && _rowset_meta_pb.has_end_version();
138
17.8k
    }
139
140
29.0k
    int64_t start_version() const { return _rowset_meta_pb.start_version(); }
141
142
49.7k
    int64_t end_version() const { return _rowset_meta_pb.end_version(); }
143
144
1.43k
    int64_t num_rows() const { return _rowset_meta_pb.num_rows(); }
145
146
1.51k
    void set_num_rows(int64_t num_rows) { _rowset_meta_pb.set_num_rows(num_rows); }
147
148
1.07k
    void set_num_segment_rows(const std::vector<uint32_t>& num_segment_rows) {
149
1.07k
        _rowset_meta_pb.mutable_num_segment_rows()->Assign(num_segment_rows.cbegin(),
150
1.07k
                                                           num_segment_rows.cend());
151
1.07k
    }
152
153
86
    void get_num_segment_rows(std::vector<uint32_t>* num_segment_rows) const {
154
86
        num_segment_rows->assign(_rowset_meta_pb.num_segment_rows().cbegin(),
155
86
                                 _rowset_meta_pb.num_segment_rows().cend());
156
86
    }
157
158
1.59k
    auto& get_num_segment_rows() const { return _rowset_meta_pb.num_segment_rows(); }
159
160
7.95k
    int64_t total_disk_size() const { return _rowset_meta_pb.total_disk_size(); }
161
162
8.33k
    void set_total_disk_size(int64_t total_disk_size) {
163
8.33k
        _rowset_meta_pb.set_total_disk_size(total_disk_size);
164
8.33k
    }
165
166
314
    int64_t data_disk_size() const { return _rowset_meta_pb.data_disk_size(); }
167
168
1.20k
    void set_data_disk_size(int64_t data_disk_size) {
169
1.20k
        _rowset_meta_pb.set_data_disk_size(data_disk_size);
170
1.20k
    }
171
172
305
    int64_t index_disk_size() const { return _rowset_meta_pb.index_disk_size(); }
173
174
1.20k
    void set_index_disk_size(int64_t index_disk_size) {
175
1.20k
        _rowset_meta_pb.set_index_disk_size(index_disk_size);
176
1.20k
    }
177
178
0
    void zone_maps(std::vector<ZoneMap>* zone_maps) {
179
0
        for (const ZoneMap& zone_map : _rowset_meta_pb.zone_maps()) {
180
0
            zone_maps->push_back(zone_map);
181
0
        }
182
0
    }
183
184
0
    void set_zone_maps(const std::vector<ZoneMap>& zone_maps) {
185
0
        for (const ZoneMap& zone_map : zone_maps) {
186
0
            ZoneMap* new_zone_map = _rowset_meta_pb.add_zone_maps();
187
0
            *new_zone_map = zone_map;
188
0
        }
189
0
    }
190
191
0
    void add_zone_map(const ZoneMap& zone_map) {
192
0
        ZoneMap* new_zone_map = _rowset_meta_pb.add_zone_maps();
193
0
        *new_zone_map = zone_map;
194
0
    }
195
196
5.89k
    bool has_delete_predicate() const { return _rowset_meta_pb.has_delete_predicate(); }
197
198
171
    const DeletePredicatePB& delete_predicate() const { return _rowset_meta_pb.delete_predicate(); }
199
200
0
    DeletePredicatePB* mutable_delete_predicate() {
201
0
        return _rowset_meta_pb.mutable_delete_predicate();
202
0
    }
203
204
83
    void set_delete_predicate(DeletePredicatePB delete_predicate) {
205
83
        DeletePredicatePB* new_delete_condition = _rowset_meta_pb.mutable_delete_predicate();
206
83
        *new_delete_condition = std::move(delete_predicate);
207
83
    }
208
209
51
    bool empty() const { return _rowset_meta_pb.empty(); }
210
211
1.20k
    void set_empty(bool empty) { _rowset_meta_pb.set_empty(empty); }
212
213
0
    PUniqueId load_id() const { return _rowset_meta_pb.load_id(); }
214
215
49
    void set_load_id(PUniqueId load_id) {
216
49
        PUniqueId* new_load_id = _rowset_meta_pb.mutable_load_id();
217
49
        new_load_id->set_hi(load_id.hi());
218
49
        new_load_id->set_lo(load_id.lo());
219
49
    }
220
221
1
    void set_job_id(const std::string& job_id) { _rowset_meta_pb.set_job_id(job_id); }
222
223
0
    const std::string& job_id() const { return _rowset_meta_pb.job_id(); }
224
225
0
    bool delete_flag() const { return _rowset_meta_pb.delete_flag(); }
226
227
30
    int64_t creation_time() const { return _rowset_meta_pb.creation_time(); }
228
229
1.18k
    void set_creation_time(int64_t creation_time) {
230
1.18k
        return _rowset_meta_pb.set_creation_time(creation_time);
231
1.18k
    }
232
233
717
    int64_t stale_at() const {
234
717
        int64_t stale_time = _stale_at_s.load();
235
717
        return stale_time > 0 ? stale_time : _rowset_meta_pb.creation_time();
236
717
    }
237
238
2
    bool has_stale_at() const { return _stale_at_s.load() > 0; }
239
240
632
    void set_stale_at(int64_t stale_at) { _stale_at_s.store(stale_at); }
241
242
10
    int64_t partition_id() const { return _rowset_meta_pb.partition_id(); }
243
244
1.03k
    void set_partition_id(int64_t partition_id) {
245
1.03k
        return _rowset_meta_pb.set_partition_id(partition_id);
246
1.03k
    }
247
248
84.8k
    int64_t num_segments() const { return _rowset_meta_pb.num_segments(); }
249
250
5.15k
    void set_num_segments(int64_t num_segments) { _rowset_meta_pb.set_num_segments(num_segments); }
251
252
    // Convert to RowsetMetaPB, skip_schema is only used by cloud to separate schema from rowset meta.
253
    void to_rowset_pb(RowsetMetaPB* rs_meta_pb, bool skip_schema = false) const;
254
255
    // Convert to RowsetMetaPB, skip_schema is only used by cloud to separate schema from rowset meta.
256
    RowsetMetaPB get_rowset_pb(bool skip_schema = false) const;
257
258
0
    inline DeletePredicatePB* mutable_delete_pred_pb() {
259
0
        return _rowset_meta_pb.mutable_delete_predicate();
260
0
    }
261
262
937
    bool is_singleton_delta() const {
263
937
        return has_version() && _rowset_meta_pb.start_version() == _rowset_meta_pb.end_version();
264
937
    }
265
266
    // Some time, we may check if this rowset is in rowset meta manager's meta by using RowsetMetaManager::check_rowset_meta.
267
    // But, this check behavior may cost a lot of time when it is frequent.
268
    // If we explicitly remove this rowset from rowset meta manager's meta, we can set _is_removed_from_rowset_meta to true,
269
    // And next time when we want to check if this rowset is in rowset mata manager's meta, we can
270
    // check is_remove_from_rowset_meta() first.
271
0
    void set_remove_from_rowset_meta() { _is_removed_from_rowset_meta = true; }
272
273
0
    bool is_remove_from_rowset_meta() const { return _is_removed_from_rowset_meta; }
274
275
1.53k
    SegmentsOverlapPB segments_overlap() const { return _rowset_meta_pb.segments_overlap_pb(); }
276
277
5.65k
    void set_segments_overlap(SegmentsOverlapPB segments_overlap) {
278
5.65k
        _rowset_meta_pb.set_segments_overlap_pb(segments_overlap);
279
5.65k
    }
280
281
17.1k
    static bool comparator(const RowsetMetaSharedPtr& left, const RowsetMetaSharedPtr& right) {
282
17.1k
        return left->end_version() < right->end_version();
283
17.1k
    }
284
285
    // return true if segments in this rowset has overlapping data.
286
    // this is not same as `segments_overlap()` method.
287
    // `segments_overlap()` only return the value of "segments_overlap" field in rowset meta,
288
    // but "segments_overlap" may be UNKNOWN.
289
    //
290
    // Returns true if all of the following conditions are met
291
    // 1. the rowset contains more than one segment
292
    // 2. the rowset's start version == end version (non-singleton rowset was generated by compaction process
293
    //    which always produces non-overlapped segments)
294
    // 3. segments_overlap() flag is not NONOVERLAPPING (OVERLAP_UNKNOWN and OVERLAPPING are OK)
295
30.7k
    bool is_segments_overlapping() const {
296
30.7k
        return num_segments() > 1 && is_singleton_delta() && segments_overlap() != NONOVERLAPPING;
297
30.7k
    }
298
299
0
    bool produced_by_compaction() const {
300
0
        return has_version() &&
301
0
               (start_version() < end_version() ||
302
0
                (start_version() == end_version() && segments_overlap() == NONOVERLAPPING));
303
0
    }
304
305
    // get the compaction score of this rowset.
306
    // if segments are overlapping, the score equals to the number of segments,
307
    // otherwise, score is 1.
308
28.9k
    uint32_t get_compaction_score() const {
309
28.9k
        uint32_t score = 0;
310
28.9k
        if (!is_segments_overlapping()) {
311
28.8k
            score = 1;
312
28.8k
        } else {
313
96
            auto num_seg = num_segments();
314
96
            DCHECK_GT(num_seg, 0);
315
96
            score = cast_set<uint32_t>(num_seg);
316
96
            CHECK(score > 0);
317
96
        }
318
28.9k
        return score;
319
28.9k
    }
320
321
0
    uint32_t get_merge_way_num() const {
322
0
        uint32_t way_num = 0;
323
0
        if (!is_segments_overlapping()) {
324
0
            if (num_segments() == 0) {
325
0
                way_num = 0;
326
0
            } else {
327
0
                way_num = 1;
328
0
            }
329
0
        } else {
330
0
            auto num_seg = num_segments();
331
0
            DCHECK_GT(num_seg, 0);
332
333
0
            way_num = cast_set<uint32_t>(num_seg);
334
0
            CHECK(way_num > 0);
335
0
        }
336
0
        return way_num;
337
0
    }
338
339
211
    void get_segments_key_bounds(std::vector<KeyBoundsPB>* segments_key_bounds) const {
340
228
        for (const KeyBoundsPB& key_range : _rowset_meta_pb.segments_key_bounds()) {
341
228
            segments_key_bounds->push_back(key_range);
342
228
        }
343
211
    }
344
345
3
    auto& get_segments_key_bounds() const { return _rowset_meta_pb.segments_key_bounds(); }
346
347
1.14k
    bool is_segments_key_bounds_truncated() const {
348
1.14k
        return _rowset_meta_pb.has_segments_key_bounds_truncated() &&
349
1.14k
               _rowset_meta_pb.segments_key_bounds_truncated();
350
1.14k
    }
351
352
1.14k
    void set_segments_key_bounds_truncated(bool truncated) {
353
1.14k
        _rowset_meta_pb.set_segments_key_bounds_truncated(truncated);
354
1.14k
    }
355
356
    // When true, `segments_key_bounds` holds a single aggregated
357
    // [rowset_min, rowset_max] entry instead of per-segment bounds.
358
87
    bool is_segments_key_bounds_aggregated() const {
359
87
        return _rowset_meta_pb.has_segments_key_bounds_aggregated() &&
360
87
               _rowset_meta_pb.segments_key_bounds_aggregated();
361
87
    }
362
363
1.09k
    void set_segments_key_bounds_aggregated(bool aggregated) {
364
1.09k
        _rowset_meta_pb.set_segments_key_bounds_aggregated(aggregated);
365
1.09k
    }
366
367
201
    bool get_first_segment_key_bound(KeyBoundsPB* key_bounds) {
368
        // for compatibility, old version has not segment key bounds
369
201
        if (_rowset_meta_pb.segments_key_bounds_size() == 0) {
370
0
            return false;
371
0
        }
372
201
        *key_bounds = *_rowset_meta_pb.segments_key_bounds().begin();
373
201
        return true;
374
201
    }
375
376
138
    bool get_last_segment_key_bound(KeyBoundsPB* key_bounds) {
377
138
        if (_rowset_meta_pb.segments_key_bounds_size() == 0) {
378
0
            return false;
379
0
        }
380
138
        *key_bounds = *_rowset_meta_pb.segments_key_bounds().rbegin();
381
138
        return true;
382
138
    }
383
384
    // If `aggregate_into_single` is true, collapse per-segment bounds into a single
385
    // [rowset_min, rowset_max] entry and mark this rowset as aggregated.
386
    void set_segments_key_bounds(const std::vector<KeyBoundsPB>& segments_key_bounds,
387
                                 bool aggregate_into_single = false);
388
389
0
    void add_segment_key_bounds(KeyBoundsPB segments_key_bounds) {
390
0
        *_rowset_meta_pb.add_segments_key_bounds() = std::move(segments_key_bounds);
391
0
        set_segments_overlap(OVERLAPPING);
392
0
    }
393
394
1.61k
    void set_newest_write_timestamp(int64_t timestamp) {
395
1.61k
        _rowset_meta_pb.set_newest_write_timestamp(timestamp);
396
1.61k
    }
397
398
1.06k
    int64_t newest_write_timestamp() const { return _rowset_meta_pb.newest_write_timestamp(); }
399
400
    // for cloud only
401
269
    bool has_visible_ts_ms() const { return _rowset_meta_pb.has_visible_ts_ms(); }
402
268
    int64_t visible_ts_ms() const { return _rowset_meta_pb.visible_ts_ms(); }
403
269
    std::chrono::time_point<std::chrono::system_clock> visible_timestamp() const {
404
269
        using namespace std::chrono;
405
269
        if (has_visible_ts_ms()) {
406
268
            return time_point<system_clock>(milliseconds(visible_ts_ms()));
407
268
        }
408
1
        return system_clock::from_time_t(newest_write_timestamp());
409
269
    }
410
771
    void set_visible_ts_ms(int64_t visible_ts_ms) {
411
771
        _rowset_meta_pb.set_visible_ts_ms(visible_ts_ms);
412
771
    }
413
414
    void set_tablet_schema(const TabletSchemaSPtr& tablet_schema);
415
    void set_tablet_schema(const TabletSchemaPB& tablet_schema);
416
417
34.6k
    const TabletSchemaSPtr& tablet_schema() const { return _schema; }
418
419
1
    void set_txn_expiration(int64_t expiration) { _rowset_meta_pb.set_txn_expiration(expiration); }
420
421
1
    void set_compaction_level(int64_t compaction_level) {
422
1
        _rowset_meta_pb.set_compaction_level(compaction_level);
423
1
    }
424
425
11
    int64_t compaction_level() { return _rowset_meta_pb.compaction_level(); }
426
427
    // `seg_file_size` MUST ordered by segment id
428
    void add_segments_file_size(const std::vector<size_t>& seg_file_size);
429
430
    // Return -1 if segment file size is unknown
431
    int64_t segment_file_size(int seg_id) const;
432
433
0
    const auto& segments_file_size() const { return _rowset_meta_pb.segments_file_size(); }
434
435
    // Used for partial update, when publish, partial update may add a new rowset and we should update rowset meta
436
    void merge_rowset_meta(const RowsetMeta& other);
437
438
    InvertedIndexFileInfo inverted_index_file_info(int seg_id);
439
440
0
    const auto& inverted_index_file_info() const {
441
0
        return _rowset_meta_pb.inverted_index_file_info();
442
0
    }
443
444
    void add_inverted_index_files_info(
445
            const std::vector<const InvertedIndexFileInfo*>& idx_file_info);
446
447
    int64_t get_metadata_size() const override;
448
449
    // Because the member field '_handle' is a raw pointer, use member func 'init' to replace copy ctor
450
    RowsetMeta(const RowsetMeta&) = delete;
451
    RowsetMeta operator=(const RowsetMeta&) = delete;
452
453
    void add_packed_slice_location(const std::string& segment_path,
454
                                   const std::string& packed_file_path, int64_t offset,
455
0
                                   int64_t size, int64_t packed_file_size) {
456
0
        auto* index_map = _rowset_meta_pb.mutable_packed_slice_locations();
457
0
        auto& index_pb = (*index_map)[segment_path];
458
0
        index_pb.set_packed_file_path(packed_file_path);
459
0
        index_pb.set_offset(offset);
460
0
        index_pb.set_size(size);
461
0
        index_pb.set_packed_file_size(packed_file_size);
462
0
    }
463
464
81
    int32_t schema_version() const { return _rowset_meta_pb.schema_version(); }
465
466
0
    std::string debug_string() const { return _rowset_meta_pb.ShortDebugString(); }
467
468
    // Pre-set the encryption algorithm to avoid re-entrant get_tablet calls
469
    // that can cause SingleFlight deadlock during tablet loading.
470
0
    void set_encryption_algorithm(EncryptionAlgorithmPB algorithm) {
471
0
        _determine_encryption_once.call(
472
0
                [algorithm]() -> Result<EncryptionAlgorithmPB> { return algorithm; });
473
0
    }
474
475
1
    int64_t commit_tso() const { return _rowset_meta_pb.commit_tso(); }
476
477
17
    void set_commit_tso(int64_t commit_tso) { _rowset_meta_pb.set_commit_tso(commit_tso); }
478
479
0
    void set_cloud_fields_after_visible(int64_t visible_version, int64_t version_update_time_ms) {
480
        // Update rowset meta with correct version and visible_ts
481
        // !!ATTENTION!!: this code should be updated if there are more fields
482
        // in rowset meta which will be modified in meta-service when commit_txn in the future
483
0
        set_version({visible_version, visible_version});
484
0
        if (version_update_time_ms > 0) {
485
0
            set_visible_ts_ms(version_update_time_ms);
486
0
        }
487
0
    }
488
489
private:
490
    bool _deserialize_from_pb(std::string_view value);
491
492
    bool _serialize_to_pb(std::string* value);
493
494
    void _init();
495
496
    friend bool operator==(const RowsetMeta& a, const RowsetMeta& b);
497
498
0
    friend bool operator!=(const RowsetMeta& a, const RowsetMeta& b) { return !(a == b); }
499
500
private:
501
    RowsetMetaPB _rowset_meta_pb;
502
    TabletSchemaSPtr _schema;
503
    Cache::Handle* _handle = nullptr;
504
    RowsetId _rowset_id;
505
    StorageResource _storage_resource;
506
    bool _is_removed_from_rowset_meta = false;
507
    DorisCallOnce<Result<EncryptionAlgorithmPB>> _determine_encryption_once;
508
    std::atomic<int64_t> _stale_at_s {0};
509
};
510
511
using RowsetMetaMapContainer = std::unordered_map<Version, RowsetMetaSharedPtr, HashOfVersion>;
512
513
} // namespace doris
514
515
#endif // DORIS_BE_SRC_OLAP_ROWSET_ROWSET_META_H