Coverage Report

Created: 2026-02-23 23:33

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