Coverage Report

Created: 2025-09-18 20:22

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
13.4k
        : _rowset_meta(std::move(rowset_meta)),
39
13.4k
          _tablet_path(std::move(tablet_path)),
40
13.4k
          _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
13.4k
    _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
13.4k
    if (_rowset_meta->has_version() && _rowset_meta->start_version() > 0 &&
51
13.4k
        _rowset_meta->rowset_state() != COMMITTED) {
52
12.6k
        _is_pending = false;
53
12.6k
    }
54
55
13.4k
    if (_is_pending) {
56
787
        _is_cumulative = false;
57
12.6k
    } else {
58
12.6k
        Version version = _rowset_meta->version();
59
12.6k
        _is_cumulative = version.first != version.second;
60
12.6k
    }
61
    // build schema from RowsetMeta.tablet_schema or Tablet.tablet_schema
62
13.4k
    _schema = _rowset_meta->tablet_schema() ? _rowset_meta->tablet_schema() : schema;
63
13.4k
}
64
65
1.19k
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
1.19k
    if (_rowset_state_machine.rowset_state() == ROWSET_LOADED) {
69
728
        return Status::OK();
70
728
    }
71
470
    {
72
        // before lock, if rowset state is ROWSET_UNLOADING, maybe it is doing do_close in release
73
470
        std::lock_guard load_lock(_lock);
74
        // after lock, if rowset state is ROWSET_UNLOADING, it is ok to return
75
470
        if (_rowset_state_machine.rowset_state() == ROWSET_UNLOADED) {
76
470
            RETURN_IF_ERROR(_rowset_state_machine.on_load());
77
470
        }
78
470
    }
79
    // load is done
80
470
    VLOG_CRITICAL << "rowset is loaded. " << rowset_id()
81
0
                  << ", rowset version:" << rowset_meta()->version()
82
0
                  << ", state from ROWSET_UNLOADED to ROWSET_LOADED. tabletid:"
83
0
                  << _rowset_meta->tablet_id();
84
470
    return Status::OK();
85
470
}
86
87
16
void Rowset::make_visible(Version version) {
88
16
    _is_pending = false;
89
16
    _rowset_meta->set_version(version);
90
16
    _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
16
    _rowset_meta->set_creation_time(UnixSeconds());
94
95
16
    if (_rowset_meta->has_delete_predicate()) {
96
0
        _rowset_meta->mutable_delete_predicate()->set_version(cast_set<int32_t>(version.first));
97
0
    }
98
16
}
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
688
std::string Rowset::get_rowset_info_str() {
110
688
    std::string disk_size = PrettyPrinter::print(
111
688
            static_cast<uint64_t>(_rowset_meta->total_disk_size()), TUnit::BYTES);
112
688
    return fmt::format("[{}-{}] {} {} {} {} {}", start_version(), end_version(), num_segments(),
113
688
                       _rowset_meta->has_delete_predicate() ? "DELETE" : "DATA",
114
688
                       SegmentsOverlapPB_Name(_rowset_meta->segments_overlap()),
115
688
                       rowset_id().to_string(), disk_size);
116
688
}
117
118
2.90k
const TabletSchemaSPtr& Rowset::tablet_schema() const {
119
2.90k
#ifdef BE_TEST
120
    // for mocking tablet schema
121
2.90k
    return _schema;
122
0
#endif
123
0
    return _rowset_meta->tablet_schema() ? _rowset_meta->tablet_schema() : _schema;
124
2.90k
}
125
126
21.3k
void Rowset::clear_cache() {
127
21.3k
    {
128
21.3k
        SCOPED_SIMPLE_TRACE_IF_TIMEOUT(std::chrono::seconds(1));
129
21.3k
        SegmentLoader::instance()->erase_segments(rowset_id(), num_segments());
130
21.3k
    }
131
21.3k
    {
132
21.3k
        SCOPED_SIMPLE_TRACE_IF_TIMEOUT(std::chrono::seconds(1));
133
21.3k
        clear_inverted_index_cache();
134
21.3k
    }
135
21.3k
    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
21.3k
}
151
152
9.19k
Result<std::string> Rowset::segment_path(int64_t seg_id) {
153
9.19k
    if (is_local()) {
154
9.18k
        return local_segment_path(_tablet_path, _rowset_meta->rowset_id().to_string(), seg_id);
155
9.18k
    }
156
157
4
    return _rowset_meta->remote_storage_resource().transform([=, this](auto&& storage_resource) {
158
4
        return storage_resource->remote_segment_path(_rowset_meta->tablet_id(),
159
4
                                                     _rowset_meta->rowset_id().to_string(), seg_id);
160
4
    });
161
9.19k
}
162
163
2
Status check_version_continuity(const std::vector<RowsetSharedPtr>& rowsets) {
164
2
    if (rowsets.size() < 2) {
165
0
        return Status::OK();
166
0
    }
167
2
    auto prev = rowsets.begin();
168
48
    for (auto it = rowsets.begin() + 1; it != rowsets.end(); ++it) {
169
46
        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
46
        prev = it;
175
46
    }
176
2
    return Status::OK();
177
2
}
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
2
std::vector<std::string> Rowset::get_index_file_names() {
187
2
    std::vector<std::string> file_names;
188
2
    auto idx_version = _schema->get_inverted_index_storage_format();
189
6
    for (int64_t seg_id = 0; seg_id < num_segments(); ++seg_id) {
190
4
        if (idx_version == InvertedIndexStorageFormatPB::V1) {
191
4
            for (const auto& index : _schema->inverted_indexes()) {
192
4
                auto file_name = segment_v2::InvertedIndexDescriptor::get_index_file_name_v1(
193
4
                        rowset_id().to_string(), seg_id, index->index_id(),
194
4
                        index->get_index_suffix());
195
4
                file_names.emplace_back(std::move(file_name));
196
4
            }
197
2
        } else {
198
2
            auto file_name = segment_v2::InvertedIndexDescriptor::get_index_file_name_v2(
199
2
                    rowset_id().to_string(), seg_id);
200
2
            file_names.emplace_back(std::move(file_name));
201
2
        }
202
4
    }
203
2
    return file_names;
204
2
}
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
210
std::chrono::time_point<std::chrono::system_clock> Rowset::visible_timestamp() const {
238
210
    return _rowset_meta->visible_timestamp();
239
210
}
240
241
#include "common/compile_check_end.h"
242
243
} // namespace doris