Coverage Report

Created: 2026-03-13 09:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/storage/rowset/rowset_meta.cpp
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
#include "storage/rowset/rowset_meta.h"
19
20
#include <gen_cpp/olap_file.pb.h>
21
#include <glog/logging.h>
22
23
#include <memory>
24
#include <random>
25
26
#include "cloud/cloud_storage_engine.h"
27
#include "common/logging.h"
28
#include "common/status.h"
29
#include "cpp/sync_point.h"
30
#include "exec/common/variant_util.h"
31
#include "google/protobuf/util/message_differencer.h"
32
#include "io/fs/encrypted_fs_factory.h"
33
#include "io/fs/file_system.h"
34
#include "io/fs/file_writer.h"
35
#include "io/fs/local_file_system.h"
36
#include "io/fs/packed_file_manager.h"
37
#include "io/fs/packed_file_system.h"
38
#include "json2pb/json_to_pb.h"
39
#include "json2pb/pb_to_json.h"
40
#include "runtime/exec_env.h"
41
#include "storage/olap_common.h"
42
#include "storage/storage_policy.h"
43
#include "storage/tablet/base_tablet.h"
44
#include "storage/tablet/tablet_fwd.h"
45
#include "storage/tablet/tablet_schema.h"
46
#include "storage/tablet/tablet_schema_cache.h"
47
#include "util/lru_cache.h"
48
49
namespace doris {
50
51
#include "common/compile_check_begin.h"
52
53
736k
RowsetMeta::~RowsetMeta() {
54
736k
    if (_handle) {
55
713k
        TabletSchemaCache::instance()->release(_handle);
56
713k
    }
57
736k
}
58
59
23.7k
bool RowsetMeta::init(std::string_view pb_rowset_meta) {
60
23.7k
    bool ret = _deserialize_from_pb(pb_rowset_meta);
61
23.7k
    if (!ret) {
62
1
        return false;
63
1
    }
64
23.7k
    _init();
65
23.7k
    return true;
66
23.7k
}
67
68
22.9k
bool RowsetMeta::init(const RowsetMeta* rowset_meta) {
69
22.9k
    RowsetMetaPB rowset_meta_pb;
70
22.9k
    rowset_meta->to_rowset_pb(&rowset_meta_pb);
71
22.9k
    return init_from_pb(rowset_meta_pb);
72
22.9k
}
73
74
837k
bool RowsetMeta::init_from_pb(const RowsetMetaPB& rowset_meta_pb) {
75
837k
    if (rowset_meta_pb.has_tablet_schema()) {
76
822k
        set_tablet_schema(rowset_meta_pb.tablet_schema());
77
822k
    }
78
    // Release ownership of TabletSchemaPB from `rowset_meta_pb` and then set it back to `rowset_meta_pb`,
79
    // this won't break const semantics of `rowset_meta_pb`, because `rowset_meta_pb` is not changed
80
    // before and after call this method.
81
837k
    auto& mut_rowset_meta_pb = const_cast<RowsetMetaPB&>(rowset_meta_pb);
82
837k
    auto* schema = mut_rowset_meta_pb.release_tablet_schema();
83
837k
    _rowset_meta_pb = mut_rowset_meta_pb;
84
837k
    mut_rowset_meta_pb.set_allocated_tablet_schema(schema);
85
837k
    _init();
86
837k
    return true;
87
837k
}
88
89
58
bool RowsetMeta::init_from_json(const std::string& json_rowset_meta) {
90
58
    bool ret = json2pb::JsonToProtoMessage(json_rowset_meta, &_rowset_meta_pb);
91
58
    if (!ret) {
92
1
        return false;
93
1
    }
94
57
    _init();
95
57
    return true;
96
58
}
97
98
0
bool RowsetMeta::json_rowset_meta(std::string* json_rowset_meta) {
99
0
    json2pb::Pb2JsonOptions json_options;
100
0
    json_options.pretty_json = true;
101
0
    bool ret = json2pb::ProtoMessageToJson(_rowset_meta_pb, json_rowset_meta, json_options);
102
0
    return ret;
103
0
}
104
105
683k
io::FileSystemSPtr RowsetMeta::physical_fs() {
106
683k
    if (is_local()) {
107
511k
        return io::global_local_filesystem();
108
511k
    }
109
110
172k
    auto storage_resource = remote_storage_resource();
111
172k
    if (storage_resource) {
112
172k
        return storage_resource.value()->fs;
113
172k
    } else {
114
62
        LOG(WARNING) << storage_resource.error();
115
62
        return nullptr;
116
62
    }
117
172k
}
118
119
674k
io::FileSystemSPtr RowsetMeta::fs() {
120
674k
    auto fs = physical_fs();
121
122
674k
#ifndef BE_TEST
123
674k
    auto algorithm = _determine_encryption_once.call([this]() -> Result<EncryptionAlgorithmPB> {
124
115k
        auto maybe_tablet = ExecEnv::get_tablet(tablet_id());
125
115k
        if (!maybe_tablet) {
126
0
            LOG(WARNING) << "get tablet failed: " << maybe_tablet.error();
127
0
            return ResultError(maybe_tablet.error());
128
0
        }
129
115k
        auto tablet = maybe_tablet.value();
130
115k
        return tablet->tablet_meta()->encryption_algorithm();
131
115k
    });
132
674k
    if (!algorithm.has_value()) {
133
        // TODO: return a Result<FileSystemSPtr> in this method?
134
0
        return nullptr;
135
0
    }
136
137
    // Apply packed file system first if enabled and index_map is not empty
138
674k
    io::FileSystemSPtr wrapped = fs;
139
674k
    if (_rowset_meta_pb.packed_slice_locations_size() > 0) {
140
155k
        std::unordered_map<std::string, io::PackedSliceLocation> index_map;
141
169k
        for (const auto& [path, index_pb] : _rowset_meta_pb.packed_slice_locations()) {
142
169k
            io::PackedSliceLocation index;
143
169k
            index.packed_file_path = index_pb.packed_file_path();
144
169k
            index.offset = index_pb.offset();
145
169k
            index.size = index_pb.size();
146
169k
            index.packed_file_size =
147
18.4E
                    index_pb.has_packed_file_size() ? index_pb.packed_file_size() : -1;
148
169k
            index.tablet_id = tablet_id();
149
169k
            index.rowset_id = _rowset_id.to_string();
150
169k
            index.resource_id = wrapped->id();
151
169k
            index_map[path] = index;
152
169k
        }
153
155k
        if (!index_map.empty()) {
154
155k
            io::PackedAppendContext append_info;
155
155k
            append_info.tablet_id = tablet_id();
156
155k
            append_info.rowset_id = _rowset_id.to_string();
157
155k
            append_info.txn_id = txn_id();
158
155k
            wrapped = std::make_shared<io::PackedFileSystem>(wrapped, index_map, append_info);
159
155k
        }
160
155k
    }
161
162
    // Then apply encryption on top
163
674k
    wrapped = io::make_file_system(wrapped, algorithm.value());
164
674k
    return wrapped;
165
#else
166
    return fs;
167
#endif
168
674k
}
169
170
422k
Result<const StorageResource*> RowsetMeta::remote_storage_resource() {
171
422k
    if (is_local()) {
172
395
        return ResultError(Status::InternalError(
173
395
                "local rowset has no storage resource. tablet_id={} rowset_id={}", tablet_id(),
174
395
                _rowset_id.to_string()));
175
395
    }
176
177
422k
    if (!_storage_resource.fs) {
178
80.2k
        if (auto storage_resource = get_storage_resource(resource_id())) {
179
80.0k
            _storage_resource = std::move(storage_resource->first);
180
80.0k
        } else {
181
459
            if (config::is_cloud_mode()) {
182
                // When creating a new cluster or creating a storage resource, BE may not sync storage resource,
183
                // at the moment a query is coming, the BetaRowsetReader call loadSegment and use this method
184
                // to get the storage resource, so we need to sync storage resource here.
185
459
                ExecEnv::GetInstance()->storage_engine().to_cloud().sync_storage_vault();
186
459
                if (auto retry_resource = get_storage_resource(resource_id())) {
187
0
                    _storage_resource = std::move(retry_resource->first);
188
0
                    return &_storage_resource;
189
0
                }
190
459
            }
191
210
            return ResultError(Status::InternalError("cannot find storage resource. resource_id={}",
192
210
                                                     resource_id()));
193
210
        }
194
80.2k
    }
195
421k
    return &_storage_resource;
196
422k
}
197
198
201k
void RowsetMeta::set_remote_storage_resource(StorageResource resource) {
199
201k
    _storage_resource = std::move(resource);
200
201k
    _rowset_meta_pb.set_resource_id(_storage_resource.fs->id());
201
201k
}
202
203
442k
bool RowsetMeta::has_variant_type_in_schema() const {
204
442k
    return _schema && _schema->num_variant_columns() > 0;
205
442k
}
206
207
443k
void RowsetMeta::to_rowset_pb(RowsetMetaPB* rs_meta_pb, bool skip_schema) const {
208
443k
    *rs_meta_pb = _rowset_meta_pb;
209
443k
    if (_schema) [[likely]] {
210
421k
        rs_meta_pb->set_schema_version(_schema->schema_version());
211
421k
        if (!skip_schema) {
212
            // For cloud, separate tablet schema from rowset meta to reduce persistent size.
213
265k
            _schema->to_schema_pb(rs_meta_pb->mutable_tablet_schema());
214
265k
        }
215
421k
    }
216
443k
    rs_meta_pb->set_has_variant_type_in_schema(has_variant_type_in_schema());
217
443k
}
218
219
275k
RowsetMetaPB RowsetMeta::get_rowset_pb(bool skip_schema) const {
220
275k
    RowsetMetaPB rowset_meta_pb;
221
275k
    to_rowset_pb(&rowset_meta_pb, skip_schema);
222
275k
    return rowset_meta_pb;
223
275k
}
224
225
592k
void RowsetMeta::set_tablet_schema(const TabletSchemaSPtr& tablet_schema) {
226
592k
    if (_handle) {
227
229k
        TabletSchemaCache::instance()->release(_handle);
228
229k
    }
229
592k
    auto pair = TabletSchemaCache::instance()->insert(tablet_schema->to_key());
230
592k
    _handle = pair.first;
231
592k
    _schema = pair.second;
232
592k
}
233
234
846k
void RowsetMeta::set_tablet_schema(const TabletSchemaPB& tablet_schema) {
235
846k
    if (_handle) {
236
0
        TabletSchemaCache::instance()->release(_handle);
237
0
    }
238
846k
    auto pair = TabletSchemaCache::instance()->insert(
239
846k
            TabletSchema::deterministic_string_serialize(tablet_schema));
240
846k
    _handle = pair.first;
241
846k
    _schema = pair.second;
242
846k
}
243
244
23.7k
bool RowsetMeta::_deserialize_from_pb(std::string_view value) {
245
23.7k
    if (!_rowset_meta_pb.ParseFromArray(value.data(), cast_set<int32_t>(value.size()))) {
246
1
        _rowset_meta_pb.Clear();
247
1
        return false;
248
1
    }
249
23.7k
    if (_rowset_meta_pb.has_tablet_schema()) {
250
23.7k
        set_tablet_schema(_rowset_meta_pb.tablet_schema());
251
23.7k
        _rowset_meta_pb.set_allocated_tablet_schema(nullptr);
252
23.7k
    }
253
23.7k
    return true;
254
23.7k
}
255
256
0
bool RowsetMeta::_serialize_to_pb(std::string* value) {
257
0
    if (value == nullptr) {
258
0
        return false;
259
0
    }
260
0
    RowsetMetaPB rowset_meta_pb = _rowset_meta_pb;
261
0
    if (_schema) {
262
0
        _schema->to_schema_pb(rowset_meta_pb.mutable_tablet_schema());
263
0
    }
264
0
    return rowset_meta_pb.SerializeToString(value);
265
0
}
266
267
861k
void RowsetMeta::_init() {
268
861k
    if (_rowset_meta_pb.rowset_id() > 0) {
269
3.56k
        _rowset_id.init(_rowset_meta_pb.rowset_id());
270
857k
    } else {
271
857k
        _rowset_id.init(_rowset_meta_pb.rowset_id_v2());
272
857k
    }
273
861k
    update_metadata_size();
274
861k
}
275
276
205k
void RowsetMeta::add_segments_file_size(const std::vector<size_t>& seg_file_size) {
277
205k
    _rowset_meta_pb.set_enable_segments_file_size(true);
278
205k
    for (auto fsize : seg_file_size) {
279
64.2k
        _rowset_meta_pb.add_segments_file_size(fsize);
280
64.2k
    }
281
205k
}
282
283
718k
int64_t RowsetMeta::segment_file_size(int seg_id) const {
284
18.4E
    DCHECK(_rowset_meta_pb.segments_file_size().empty() ||
285
18.4E
           _rowset_meta_pb.segments_file_size_size() > seg_id)
286
18.4E
            << _rowset_meta_pb.segments_file_size_size() << ' ' << seg_id;
287
718k
    return _rowset_meta_pb.enable_segments_file_size()
288
718k
                   ? (_rowset_meta_pb.segments_file_size_size() > seg_id
289
192k
                              ? _rowset_meta_pb.segments_file_size(seg_id)
290
18.4E
                              : -1)
291
718k
                   : -1;
292
718k
}
293
294
253k
void RowsetMeta::set_segments_key_bounds(const std::vector<KeyBoundsPB>& segments_key_bounds) {
295
253k
    for (const KeyBoundsPB& key_bounds : segments_key_bounds) {
296
98.0k
        KeyBoundsPB* new_key_bounds = _rowset_meta_pb.add_segments_key_bounds();
297
98.0k
        *new_key_bounds = key_bounds;
298
98.0k
    }
299
300
253k
    int32_t truncation_threshold = config::segments_key_bounds_truncation_threshold;
301
253k
    if (config::random_segments_key_bounds_truncation) {
302
0
        std::mt19937 generator(std::random_device {}());
303
0
        std::uniform_int_distribution<int> distribution(-10, 40);
304
0
        truncation_threshold = distribution(generator);
305
0
    }
306
253k
    bool really_do_truncation {false};
307
253k
    if (truncation_threshold > 0) {
308
252k
        for (auto& segment_key_bounds : *_rowset_meta_pb.mutable_segments_key_bounds()) {
309
93.1k
            if (segment_key_bounds.min_key().size() > truncation_threshold) {
310
91.8k
                really_do_truncation = true;
311
91.8k
                segment_key_bounds.mutable_min_key()->resize(truncation_threshold);
312
91.8k
            }
313
93.1k
            if (segment_key_bounds.max_key().size() > truncation_threshold) {
314
92.5k
                really_do_truncation = true;
315
92.5k
                segment_key_bounds.mutable_max_key()->resize(truncation_threshold);
316
92.5k
            }
317
93.1k
        }
318
252k
    }
319
253k
    set_segments_key_bounds_truncated(really_do_truncation || is_segments_key_bounds_truncated());
320
253k
}
321
322
1.61k
void RowsetMeta::merge_rowset_meta(const RowsetMeta& other) {
323
1.61k
    set_num_segments(num_segments() + other.num_segments());
324
1.61k
    set_num_rows(num_rows() + other.num_rows());
325
1.61k
    set_data_disk_size(data_disk_size() + other.data_disk_size());
326
1.61k
    set_total_disk_size(total_disk_size() + other.total_disk_size());
327
1.61k
    set_index_disk_size(index_disk_size() + other.index_disk_size());
328
1.61k
    set_total_disk_size(data_disk_size() + index_disk_size());
329
1.61k
    set_segments_key_bounds_truncated(is_segments_key_bounds_truncated() ||
330
1.61k
                                      other.is_segments_key_bounds_truncated());
331
1.61k
    if (_rowset_meta_pb.num_segment_rows_size() > 0) {
332
1.53k
        if (other.num_segments() > 0) {
333
21
            if (other._rowset_meta_pb.num_segment_rows_size() > 0) {
334
21
                for (auto row_count : other._rowset_meta_pb.num_segment_rows()) {
335
21
                    _rowset_meta_pb.add_num_segment_rows(row_count);
336
21
                }
337
21
            } else {
338
                // This may happen when a partial update load commits in high version doirs_be
339
                // and publishes with new segments in low version doris_be. In this case, just clear
340
                // all num_segment_rows.
341
0
                _rowset_meta_pb.clear_num_segment_rows();
342
0
            }
343
21
        }
344
1.53k
    }
345
1.61k
    for (auto&& key_bound : other.get_segments_key_bounds()) {
346
21
        add_segment_key_bounds(key_bound);
347
21
    }
348
1.61k
    if (_rowset_meta_pb.enable_segments_file_size() &&
349
1.61k
        other._rowset_meta_pb.enable_segments_file_size()) {
350
1.38k
        for (auto fsize : other.segments_file_size()) {
351
21
            _rowset_meta_pb.add_segments_file_size(fsize);
352
21
        }
353
1.38k
    }
354
1.61k
    if (_rowset_meta_pb.enable_inverted_index_file_info() &&
355
1.61k
        other._rowset_meta_pb.enable_inverted_index_file_info()) {
356
178
        for (auto finfo : other.inverted_index_file_info()) {
357
0
            InvertedIndexFileInfo* new_file_info = _rowset_meta_pb.add_inverted_index_file_info();
358
0
            *new_file_info = finfo;
359
0
        }
360
178
    }
361
    // In partial update the rowset schema maybe updated when table contains variant type, so we need the newest schema to be updated
362
    // Otherwise the schema is stale and lead to wrong data read
363
1.61k
    TEST_SYNC_POINT_RETURN_WITH_VOID("RowsetMeta::merge_rowset_meta:skip_schema_merge");
364
1.61k
    if (tablet_schema()->num_variant_columns() > 0) {
365
        // merge extracted columns
366
47
        TabletSchemaSPtr merged_schema;
367
47
        static_cast<void>(variant_util::get_least_common_schema(
368
47
                {tablet_schema(), other.tablet_schema()}, nullptr, merged_schema));
369
47
        if (*_schema != *merged_schema) {
370
0
            set_tablet_schema(merged_schema);
371
0
        }
372
47
    }
373
1.61k
    if (rowset_state() == RowsetStatePB::BEGIN_PARTIAL_UPDATE) {
374
1.38k
        set_rowset_state(RowsetStatePB::COMMITTED);
375
1.38k
    }
376
377
1.61k
    update_metadata_size();
378
1.61k
}
379
380
862k
int64_t RowsetMeta::get_metadata_size() const {
381
862k
    return sizeof(RowsetMeta) + _rowset_meta_pb.ByteSizeLong();
382
862k
}
383
384
675k
InvertedIndexFileInfo RowsetMeta::inverted_index_file_info(int seg_id) {
385
675k
    return _rowset_meta_pb.enable_inverted_index_file_info()
386
675k
                   ? (_rowset_meta_pb.inverted_index_file_info_size() > seg_id
387
18.6k
                              ? _rowset_meta_pb.inverted_index_file_info(seg_id)
388
18.6k
                              : InvertedIndexFileInfo())
389
675k
                   : InvertedIndexFileInfo();
390
675k
}
391
392
void RowsetMeta::add_inverted_index_files_info(
393
21.0k
        const std::vector<const InvertedIndexFileInfo*>& idx_file_info) {
394
21.0k
    _rowset_meta_pb.set_enable_inverted_index_file_info(true);
395
21.0k
    for (auto finfo : idx_file_info) {
396
6.01k
        auto* new_file_info = _rowset_meta_pb.add_inverted_index_file_info();
397
6.01k
        *new_file_info = *finfo;
398
6.01k
    }
399
21.0k
}
400
401
0
bool operator==(const RowsetMeta& a, const RowsetMeta& b) {
402
0
    if (a._rowset_id != b._rowset_id) return false;
403
0
    if (a._is_removed_from_rowset_meta != b._is_removed_from_rowset_meta) return false;
404
0
    if (!google::protobuf::util::MessageDifferencer::Equals(a._rowset_meta_pb, b._rowset_meta_pb))
405
0
        return false;
406
0
    return true;
407
0
}
408
409
#include "common/compile_check_end.h"
410
411
} // namespace doris