Coverage Report

Created: 2026-04-10 16:11

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/storage/rowset/rowset.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.h"
19
20
#include <gen_cpp/olap_file.pb.h>
21
22
#include "common/cast_set.h"
23
#include "common/config.h"
24
#include "io/cache/block_file_cache_factory.h"
25
#include "storage/index/inverted/inverted_index_desc.h"
26
#include "storage/olap_define.h"
27
#include "storage/segment/segment_loader.h"
28
#include "storage/tablet/tablet_schema.h"
29
#include "util/time.h"
30
#include "util/trace.h"
31
32
namespace doris {
33
34
Rowset::Rowset(const TabletSchemaSPtr& schema, RowsetMetaSharedPtr rowset_meta,
35
               std::string tablet_path)
36
989k
        : _rowset_meta(std::move(rowset_meta)),
37
989k
          _tablet_path(std::move(tablet_path)),
38
989k
          _refs_by_reader(0) {
39
989k
#ifndef BE_TEST
40
989k
    DCHECK(!is_local() || !_tablet_path.empty()); // local rowset MUST has tablet path
41
989k
#endif
42
43
989k
    _is_pending = true;
44
45
    // Generally speaking, as long as a rowset has a version, it can be considered not to be in a pending state.
46
    // However, if the rowset was created through ingesting binlogs, it will have a version but should still be
47
    // considered in a pending state because the ingesting txn has not yet been committed.
48
989k
    if (_rowset_meta->has_version() && _rowset_meta->start_version() > 0 &&
49
989k
        _rowset_meta->rowset_state() != COMMITTED) {
50
324k
        _is_pending = false;
51
324k
    }
52
53
989k
    if (_is_pending) {
54
665k
        _is_cumulative = false;
55
665k
    } else {
56
324k
        Version version = _rowset_meta->version();
57
324k
        _is_cumulative = version.first != version.second;
58
324k
    }
59
    // build schema from RowsetMeta.tablet_schema or Tablet.tablet_schema
60
989k
    _schema = _rowset_meta->tablet_schema() ? _rowset_meta->tablet_schema() : schema;
61
989k
}
62
63
6.71M
Status Rowset::load(bool use_cache) {
64
    // if the state is ROWSET_UNLOADING it means close() is called
65
    // and the rowset is already loaded, and the resource is not closed yet.
66
6.71M
    if (_rowset_state_machine.rowset_state() == ROWSET_LOADED) {
67
6.42M
        return Status::OK();
68
6.42M
    }
69
295k
    {
70
        // before lock, if rowset state is ROWSET_UNLOADING, maybe it is doing do_close in release
71
295k
        std::lock_guard load_lock(_lock);
72
        // after lock, if rowset state is ROWSET_UNLOADING, it is ok to return
73
301k
        if (_rowset_state_machine.rowset_state() == ROWSET_UNLOADED) {
74
301k
            RETURN_IF_ERROR(_rowset_state_machine.on_load());
75
301k
        }
76
295k
    }
77
    // load is done
78
18.4E
    VLOG_CRITICAL << "rowset is loaded. " << rowset_id()
79
18.4E
                  << ", rowset version:" << rowset_meta()->version()
80
18.4E
                  << ", state from ROWSET_UNLOADED to ROWSET_LOADED. tabletid:"
81
18.4E
                  << _rowset_meta->tablet_id();
82
295k
    return Status::OK();
83
295k
}
84
85
14.8k
void Rowset::make_visible(Version version, int64_t commit_tso) {
86
14.8k
    _is_pending = false;
87
14.8k
    _rowset_meta->set_version(version);
88
14.8k
    _rowset_meta->set_rowset_state(VISIBLE);
89
    // update create time to the visible time,
90
    // it's used to skip recently published version during compaction
91
14.8k
    _rowset_meta->set_creation_time(UnixSeconds());
92
93
14.8k
    if (_rowset_meta->has_delete_predicate()) {
94
22
        _rowset_meta->mutable_delete_predicate()->set_version(cast_set<int32_t>(version.first));
95
22
    }
96
14.8k
    _rowset_meta->set_commit_tso(commit_tso);
97
14.8k
}
98
99
20.4k
void Rowset::set_version(Version version) {
100
20.4k
    _rowset_meta->set_version(version);
101
20.4k
}
102
103
0
bool Rowset::check_rowset_segment() {
104
0
    std::lock_guard load_lock(_lock);
105
0
    return check_current_rowset_segment();
106
0
}
107
108
25.7k
std::string Rowset::get_rowset_info_str() {
109
25.7k
    std::string disk_size = PrettyPrinter::print(
110
25.7k
            static_cast<uint64_t>(_rowset_meta->total_disk_size()), TUnit::BYTES);
111
25.7k
    return fmt::format("[{}-{}] {} {} {} {} {}", start_version(), end_version(), num_segments(),
112
25.7k
                       _rowset_meta->has_delete_predicate() ? "DELETE" : "DATA",
113
25.7k
                       SegmentsOverlapPB_Name(_rowset_meta->segments_overlap()),
114
25.7k
                       rowset_id().to_string(), disk_size);
115
25.7k
}
116
117
903k
const TabletSchemaSPtr& Rowset::tablet_schema() const {
118
#ifdef BE_TEST
119
    // for mocking tablet schema
120
    return _schema;
121
#endif
122
18.4E
    return _rowset_meta->tablet_schema() ? _rowset_meta->tablet_schema() : _schema;
123
903k
}
124
125
173k
void Rowset::clear_cache() {
126
173k
    {
127
173k
        SCOPED_SIMPLE_TRACE_IF_TIMEOUT(std::chrono::seconds(1));
128
173k
        SegmentLoader::instance()->erase_segments(rowset_id(), num_segments());
129
173k
    }
130
173k
    {
131
173k
        SCOPED_SIMPLE_TRACE_IF_TIMEOUT(std::chrono::seconds(1));
132
173k
        clear_inverted_index_cache();
133
173k
    }
134
173k
    if (config::enable_file_cache) {
135
182k
        for (int seg_id = 0; seg_id < num_segments(); ++seg_id) {
136
48.4k
            auto file_key = segment_v2::Segment::file_cache_key(rowset_id().to_string(), seg_id);
137
48.4k
            auto* file_cache = io::FileCacheFactory::instance()->get_by_path(file_key);
138
48.4k
            file_cache->remove_if_cached_async(file_key);
139
48.4k
        }
140
141
        // inverted index
142
134k
        auto file_names = get_index_file_names();
143
134k
        for (const auto& file_name : file_names) {
144
47.0k
            auto file_key = io::BlockFileCache::hash(file_name);
145
47.0k
            auto* file_cache = io::FileCacheFactory::instance()->get_by_path(file_key);
146
47.0k
            file_cache->remove_if_cached_async(file_key);
147
47.0k
        }
148
134k
    }
149
173k
}
150
151
703k
Result<std::string> Rowset::segment_path(int64_t seg_id) {
152
703k
    if (is_local()) {
153
512k
        return local_segment_path(_tablet_path, _rowset_meta->rowset_id().to_string(), seg_id);
154
512k
    }
155
156
191k
    return _rowset_meta->remote_storage_resource().transform([=, this](auto&& storage_resource) {
157
191k
        return storage_resource->remote_segment_path(_rowset_meta->tablet_id(),
158
191k
                                                     _rowset_meta->rowset_id().to_string(), seg_id);
159
191k
    });
160
703k
}
161
162
155k
Status check_version_continuity(const std::vector<RowsetSharedPtr>& rowsets) {
163
155k
    if (rowsets.size() < 2) {
164
58.4k
        return Status::OK();
165
58.4k
    }
166
97.0k
    auto prev = rowsets.begin();
167
392k
    for (auto it = rowsets.begin() + 1; it != rowsets.end(); ++it) {
168
295k
        if ((*prev)->end_version() + 1 != (*it)->start_version()) {
169
0
            return Status::InternalError("versions are not continuity: prev={} cur={}",
170
0
                                         (*prev)->version().to_string(),
171
0
                                         (*it)->version().to_string());
172
0
        }
173
295k
        prev = it;
174
295k
    }
175
97.0k
    return Status::OK();
176
97.0k
}
177
178
1.62k
void Rowset::merge_rowset_meta(const RowsetMeta& other) {
179
1.62k
    _rowset_meta->merge_rowset_meta(other);
180
    // rowset->meta_meta()->tablet_schema() maybe updated so make sure _schema is
181
    // consistent with rowset meta
182
1.62k
    _schema = _rowset_meta->tablet_schema();
183
1.62k
}
184
185
248k
std::vector<std::string> Rowset::get_index_file_names() {
186
248k
    std::vector<std::string> file_names;
187
248k
    auto idx_version = _schema->get_inverted_index_storage_format();
188
342k
    for (int64_t seg_id = 0; seg_id < num_segments(); ++seg_id) {
189
93.6k
        if (idx_version == InvertedIndexStorageFormatPB::V1) {
190
3.26k
            for (const auto& index : _schema->inverted_indexes()) {
191
471
                auto file_name = segment_v2::InvertedIndexDescriptor::get_index_file_name_v1(
192
471
                        rowset_id().to_string(), seg_id, index->index_id(),
193
471
                        index->get_index_suffix());
194
471
                file_names.emplace_back(std::move(file_name));
195
471
            }
196
90.4k
        } else {
197
90.4k
            auto file_name = segment_v2::InvertedIndexDescriptor::get_index_file_name_v2(
198
90.4k
                    rowset_id().to_string(), seg_id);
199
90.4k
            file_names.emplace_back(std::move(file_name));
200
90.4k
        }
201
93.6k
    }
202
248k
    return file_names;
203
248k
}
204
205
10.6k
int64_t Rowset::approximate_cached_data_size() {
206
10.6k
    if (!config::enable_file_cache) {
207
0
        return 0;
208
0
    }
209
210
10.6k
    int64_t total_cache_size = 0;
211
14.4k
    for (int seg_id = 0; seg_id < num_segments(); ++seg_id) {
212
3.82k
        auto cache_key = segment_v2::Segment::file_cache_key(rowset_id().to_string(), seg_id);
213
3.82k
        int64_t cache_size =
214
3.82k
                io::FileCacheFactory::instance()->get_cache_file_size_by_path(cache_key);
215
3.82k
        total_cache_size += cache_size;
216
3.82k
    }
217
10.6k
    return total_cache_size;
218
10.6k
}
219
220
10.6k
int64_t Rowset::approximate_cache_index_size() {
221
10.6k
    if (!config::enable_file_cache) {
222
0
        return 0;
223
0
    }
224
225
10.6k
    int64_t total_cache_size = 0;
226
10.6k
    auto file_names = get_index_file_names();
227
10.6k
    for (const auto& file_name : file_names) {
228
3.81k
        auto cache_key = io::BlockFileCache::hash(file_name);
229
3.81k
        int64_t cache_size =
230
3.81k
                io::FileCacheFactory::instance()->get_cache_file_size_by_path(cache_key);
231
3.81k
        total_cache_size += cache_size;
232
3.81k
    }
233
10.6k
    return total_cache_size;
234
10.6k
}
235
236
356
std::chrono::time_point<std::chrono::system_clock> Rowset::visible_timestamp() const {
237
356
    return _rowset_meta->visible_timestamp();
238
356
}
239
240
} // namespace doris