Coverage Report

Created: 2026-04-16 07:20

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