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 | 977k | : _rowset_meta(std::move(rowset_meta)), |
37 | 977k | _tablet_path(std::move(tablet_path)), |
38 | 977k | _refs_by_reader(0) { |
39 | 977k | #ifndef BE_TEST |
40 | 977k | DCHECK(!is_local() || !_tablet_path.empty()); // local rowset MUST has tablet path |
41 | 977k | #endif |
42 | | |
43 | 977k | _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 | 977k | if (_rowset_meta->has_version() && _rowset_meta->start_version() > 0 && |
49 | 977k | _rowset_meta->rowset_state() != COMMITTED) { |
50 | 221k | _is_pending = false; |
51 | 221k | } |
52 | | |
53 | 977k | if (_is_pending) { |
54 | 755k | _is_cumulative = false; |
55 | 755k | } else { |
56 | 221k | Version version = _rowset_meta->version(); |
57 | 221k | _is_cumulative = version.first != version.second; |
58 | 221k | } |
59 | | // build schema from RowsetMeta.tablet_schema or Tablet.tablet_schema |
60 | 977k | _schema = _rowset_meta->tablet_schema() ? _rowset_meta->tablet_schema() : schema; |
61 | 977k | } |
62 | | |
63 | 5.11M | 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 | 5.11M | if (_rowset_state_machine.rowset_state() == ROWSET_LOADED) { |
67 | 4.83M | return Status::OK(); |
68 | 4.83M | } |
69 | 278k | { |
70 | | // before lock, if rowset state is ROWSET_UNLOADING, maybe it is doing do_close in release |
71 | 278k | std::lock_guard load_lock(_lock); |
72 | | // after lock, if rowset state is ROWSET_UNLOADING, it is ok to return |
73 | 287k | if (_rowset_state_machine.rowset_state() == ROWSET_UNLOADED) { |
74 | 287k | RETURN_IF_ERROR(_rowset_state_machine.on_load()); |
75 | 287k | } |
76 | 278k | } |
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 | 278k | return Status::OK(); |
83 | 278k | } |
84 | | |
85 | 441 | void Rowset::make_visible(Version version, int64_t commit_tso) { |
86 | 441 | _is_pending = false; |
87 | 441 | _rowset_meta->set_version(version); |
88 | 441 | _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 | 441 | _rowset_meta->set_creation_time(UnixSeconds()); |
92 | | |
93 | 441 | if (_rowset_meta->has_delete_predicate()) { |
94 | 0 | _rowset_meta->mutable_delete_predicate()->set_version(cast_set<int32_t>(version.first)); |
95 | 0 | } |
96 | 441 | _rowset_meta->set_commit_tso(commit_tso); |
97 | 441 | } |
98 | | |
99 | 50.3k | void Rowset::set_version(Version version) { |
100 | 50.3k | _rowset_meta->set_version(version); |
101 | 50.3k | } |
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.0k | std::string Rowset::get_rowset_info_str() { |
109 | 25.0k | std::string disk_size = PrettyPrinter::print( |
110 | 25.0k | static_cast<uint64_t>(_rowset_meta->total_disk_size()), TUnit::BYTES); |
111 | 25.0k | return fmt::format("[{}-{}] {} {} {} {} {}", start_version(), end_version(), num_segments(), |
112 | 25.0k | _rowset_meta->has_delete_predicate() ? "DELETE" : "DATA", |
113 | 25.0k | SegmentsOverlapPB_Name(_rowset_meta->segments_overlap()), |
114 | 25.0k | rowset_id().to_string(), disk_size); |
115 | 25.0k | } |
116 | | |
117 | 894k | const TabletSchemaSPtr& Rowset::tablet_schema() const { |
118 | | #ifdef BE_TEST |
119 | | // for mocking tablet schema |
120 | | return _schema; |
121 | | #endif |
122 | 894k | return _rowset_meta->tablet_schema() ? _rowset_meta->tablet_schema() : _schema; |
123 | 894k | } |
124 | | |
125 | 196k | void Rowset::clear_cache() { |
126 | 196k | { |
127 | 196k | SCOPED_SIMPLE_TRACE_IF_TIMEOUT(std::chrono::seconds(1)); |
128 | 196k | SegmentLoader::instance()->erase_segments(rowset_id(), num_segments()); |
129 | 196k | } |
130 | 196k | { |
131 | 196k | SCOPED_SIMPLE_TRACE_IF_TIMEOUT(std::chrono::seconds(1)); |
132 | 196k | clear_inverted_index_cache(); |
133 | 196k | } |
134 | 196k | if (config::enable_file_cache) { |
135 | 150k | for (int seg_id = 0; seg_id < num_segments(); ++seg_id) { |
136 | 42.5k | auto file_key = segment_v2::Segment::file_cache_key(rowset_id().to_string(), seg_id); |
137 | 42.5k | auto* file_cache = io::FileCacheFactory::instance()->get_by_path(file_key); |
138 | 42.5k | file_cache->remove_if_cached_async(file_key); |
139 | 42.5k | } |
140 | | |
141 | | // inverted index |
142 | 107k | auto file_names = get_index_file_names(); |
143 | 107k | for (const auto& file_name : file_names) { |
144 | 41.1k | auto file_key = io::BlockFileCache::hash(file_name); |
145 | 41.1k | auto* file_cache = io::FileCacheFactory::instance()->get_by_path(file_key); |
146 | 41.1k | file_cache->remove_if_cached_async(file_key); |
147 | 41.1k | } |
148 | 107k | } |
149 | 196k | } |
150 | | |
151 | 1.75M | Result<std::string> Rowset::segment_path(int64_t seg_id) { |
152 | 1.75M | if (is_local()) { |
153 | 23.2k | return local_segment_path(_tablet_path, _rowset_meta->rowset_id().to_string(), seg_id); |
154 | 23.2k | } |
155 | | |
156 | 1.73M | return _rowset_meta->remote_storage_resource().transform([=, this](auto&& storage_resource) { |
157 | 1.72M | return storage_resource->remote_segment_path(_rowset_meta->tablet_id(), |
158 | 1.72M | _rowset_meta->rowset_id().to_string(), seg_id); |
159 | 1.72M | }); |
160 | 1.75M | } |
161 | | |
162 | 125k | Status check_version_continuity(const std::vector<RowsetSharedPtr>& rowsets) { |
163 | 125k | if (rowsets.size() < 2) { |
164 | 28.0k | return Status::OK(); |
165 | 28.0k | } |
166 | 97.3k | auto prev = rowsets.begin(); |
167 | 399k | for (auto it = rowsets.begin() + 1; it != rowsets.end(); ++it) { |
168 | 301k | 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 | 301k | prev = it; |
174 | 301k | } |
175 | 97.3k | return Status::OK(); |
176 | 97.3k | } |
177 | | |
178 | 3.39k | void Rowset::merge_rowset_meta(const RowsetMeta& other) { |
179 | 3.39k | _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 | 3.39k | _schema = _rowset_meta->tablet_schema(); |
183 | 3.39k | } |
184 | | |
185 | 221k | std::vector<std::string> Rowset::get_index_file_names() { |
186 | 221k | std::vector<std::string> file_names; |
187 | 221k | auto idx_version = _schema->get_inverted_index_storage_format(); |
188 | 309k | for (int64_t seg_id = 0; seg_id < num_segments(); ++seg_id) { |
189 | 87.9k | if (idx_version == InvertedIndexStorageFormatPB::V1) { |
190 | 3.26k | for (const auto& index : _schema->inverted_indexes()) { |
191 | 475 | auto file_name = segment_v2::InvertedIndexDescriptor::get_index_file_name_v1( |
192 | 475 | rowset_id().to_string(), seg_id, index->index_id(), |
193 | 475 | index->get_index_suffix()); |
194 | 475 | file_names.emplace_back(std::move(file_name)); |
195 | 475 | } |
196 | 84.6k | } else { |
197 | 84.6k | auto file_name = segment_v2::InvertedIndexDescriptor::get_index_file_name_v2( |
198 | 84.6k | rowset_id().to_string(), seg_id); |
199 | 84.6k | file_names.emplace_back(std::move(file_name)); |
200 | 84.6k | } |
201 | 87.9k | } |
202 | 221k | return file_names; |
203 | 221k | } |
204 | | |
205 | 10.4k | int64_t Rowset::approximate_cached_data_size() { |
206 | 10.4k | if (!config::enable_file_cache) { |
207 | 0 | return 0; |
208 | 0 | } |
209 | | |
210 | 10.4k | int64_t total_cache_size = 0; |
211 | 14.2k | for (int seg_id = 0; seg_id < num_segments(); ++seg_id) { |
212 | 3.78k | auto cache_key = segment_v2::Segment::file_cache_key(rowset_id().to_string(), seg_id); |
213 | 3.78k | int64_t cache_size = |
214 | 3.78k | io::FileCacheFactory::instance()->get_cache_file_size_by_path(cache_key); |
215 | 3.78k | total_cache_size += cache_size; |
216 | 3.78k | } |
217 | 10.4k | return total_cache_size; |
218 | 10.4k | } |
219 | | |
220 | 10.4k | int64_t Rowset::approximate_cache_index_size() { |
221 | 10.4k | if (!config::enable_file_cache) { |
222 | 0 | return 0; |
223 | 0 | } |
224 | | |
225 | 10.4k | int64_t total_cache_size = 0; |
226 | 10.4k | auto file_names = get_index_file_names(); |
227 | 10.4k | for (const auto& file_name : file_names) { |
228 | 3.77k | auto cache_key = io::BlockFileCache::hash(file_name); |
229 | 3.77k | int64_t cache_size = |
230 | 3.77k | io::FileCacheFactory::instance()->get_cache_file_size_by_path(cache_key); |
231 | 3.77k | total_cache_size += cache_size; |
232 | 3.77k | } |
233 | 10.4k | return total_cache_size; |
234 | 10.4k | } |
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 |