be/src/exec/scan/parallel_scanner_builder.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 "exec/scan/parallel_scanner_builder.h" |
19 | | |
20 | | #include <cstddef> |
21 | | |
22 | | #include "cloud/cloud_storage_engine.h" |
23 | | #include "cloud/cloud_tablet_hotspot.h" |
24 | | #include "cloud/config.h" |
25 | | #include "common/status.h" |
26 | | #include "exec/operator/olap_scan_operator.h" |
27 | | #include "exec/scan/olap_scanner.h" |
28 | | #include "storage/rowset/beta_rowset.h" |
29 | | #include "storage/segment/segment_loader.h" |
30 | | #include "storage/tablet/base_tablet.h" |
31 | | |
32 | | namespace doris { |
33 | | |
34 | | namespace { |
35 | | |
36 | | io::FileCacheStatistics take_initial_file_cache_stats( |
37 | 660k | std::unordered_map<int64_t, io::FileCacheStatistics>* preload_stats, int64_t tablet_id) { |
38 | 660k | auto it = preload_stats->find(tablet_id); |
39 | 660k | if (it == preload_stats->end()) { |
40 | 55.1k | return {}; |
41 | 55.1k | } |
42 | 605k | auto stats = std::move(it->second); |
43 | 605k | preload_stats->erase(it); |
44 | 605k | return stats; |
45 | 660k | } |
46 | | |
47 | | } // namespace |
48 | | |
49 | 170k | Status ParallelScannerBuilder::build_scanners(std::list<ScannerSPtr>& scanners) { |
50 | 170k | RETURN_IF_ERROR(_load()); |
51 | 170k | if (_scan_parallelism_by_per_segment) { |
52 | 24 | return _build_scanners_by_per_segment(scanners); |
53 | 172k | } else if (_is_dup_mow_key) { |
54 | | // Default strategy for DUP/MOW tables: split by rowids within segments |
55 | 172k | return _build_scanners_by_rowid(scanners); |
56 | 18.4E | } else { |
57 | | // TODO: support to split by key range |
58 | 18.4E | return Status::NotSupported("split by key range not supported yet."); |
59 | 18.4E | } |
60 | 170k | } |
61 | | |
62 | 172k | Status ParallelScannerBuilder::_build_scanners_by_rowid(std::list<ScannerSPtr>& scanners) { |
63 | 172k | DCHECK_GE(_rows_per_scanner, _min_rows_per_scanner); |
64 | | |
65 | 815k | for (auto&& [tablet, version] : _tablets) { |
66 | 815k | DCHECK(_all_read_sources.contains(tablet->tablet_id())); |
67 | 815k | auto& entire_read_source = _all_read_sources[tablet->tablet_id()]; |
68 | | |
69 | 815k | if (config::is_cloud_mode()) { |
70 | | // FIXME(plat1ko): Avoid pointer cast |
71 | 453k | ExecEnv::GetInstance()->storage_engine().to_cloud().tablet_hotspot().count(*tablet); |
72 | 453k | } |
73 | | |
74 | | // `rs_splits` in `entire read source` will be devided into several partitial read sources |
75 | | // to build several parallel scanners, based on segment rows number. All the partitial read sources |
76 | | // share the same delete predicates from their corresponding entire read source. |
77 | 815k | TabletReadSource partitial_read_source; |
78 | 815k | int64_t rows_collected = 0; |
79 | 3.42M | for (auto& rs_split : entire_read_source.rs_splits) { |
80 | 3.42M | auto reader = rs_split.rs_reader; |
81 | 3.42M | auto rowset = reader->rowset(); |
82 | 3.42M | const auto rowset_id = rowset->rowset_id(); |
83 | | |
84 | 3.42M | const auto& segments_rows = _all_segments_rows[rowset_id]; |
85 | | |
86 | 3.42M | if (rowset->num_rows() == 0) { |
87 | 2.10M | continue; |
88 | 2.10M | } |
89 | | |
90 | 1.31M | int64_t segment_start = 0; |
91 | 1.31M | auto split = RowSetSplits(reader->clone()); |
92 | | |
93 | 2.63M | for (size_t i = 0; i != segments_rows.size(); ++i) { |
94 | 1.31M | const size_t rows_of_segment = segments_rows[i]; |
95 | 1.31M | RowRanges row_ranges; |
96 | 1.31M | int64_t offset_in_segment = 0; |
97 | | |
98 | | // try to split large segments into RowRanges |
99 | 2.68M | while (offset_in_segment < rows_of_segment) { |
100 | 1.36M | const int64_t remaining_rows = rows_of_segment - offset_in_segment; |
101 | 1.36M | auto rows_need = _rows_per_scanner - rows_collected; |
102 | | |
103 | | // 0.9: try to avoid splitting the segments into excessively small parts. |
104 | 1.36M | if (rows_need >= remaining_rows * 9 / 10) { |
105 | 1.31M | rows_need = remaining_rows; |
106 | 1.31M | } |
107 | 1.36M | DCHECK_LE(rows_need, remaining_rows); |
108 | | |
109 | | // RowRange stands for range: [From, To), From is inclusive, To is exclusive. |
110 | 1.36M | row_ranges.add({offset_in_segment, |
111 | 1.36M | offset_in_segment + static_cast<int64_t>(rows_need)}); |
112 | 1.36M | rows_collected += rows_need; |
113 | 1.36M | offset_in_segment += rows_need; |
114 | | |
115 | | // If collected enough rows, build a new scanner |
116 | 1.36M | if (rows_collected >= _rows_per_scanner) { |
117 | 55.7k | split.segment_offsets.first = segment_start, |
118 | 55.7k | split.segment_offsets.second = i + 1; |
119 | 55.7k | split.segment_row_ranges.emplace_back(std::move(row_ranges)); |
120 | | |
121 | 55.7k | DCHECK_EQ(split.segment_offsets.second - split.segment_offsets.first, |
122 | 55.7k | split.segment_row_ranges.size()); |
123 | | |
124 | 55.7k | partitial_read_source.rs_splits.emplace_back(std::move(split)); |
125 | | |
126 | 55.7k | scanners.emplace_back(_build_scanner( |
127 | 55.7k | tablet, version, _key_ranges, |
128 | 55.7k | {.rs_splits = std::move(partitial_read_source.rs_splits), |
129 | 55.7k | .delete_predicates = entire_read_source.delete_predicates, |
130 | 55.7k | .delete_bitmap = entire_read_source.delete_bitmap}, |
131 | 55.7k | take_initial_file_cache_stats(&_tablet_preload_file_cache_stats, |
132 | 55.7k | tablet->tablet_id()))); |
133 | | |
134 | 55.7k | partitial_read_source = {}; |
135 | 55.7k | split = RowSetSplits(reader->clone()); |
136 | 55.7k | row_ranges = RowRanges(); |
137 | | |
138 | 55.7k | segment_start = offset_in_segment < rows_of_segment ? i : i + 1; |
139 | 55.7k | rows_collected = 0; |
140 | 55.7k | } |
141 | 1.36M | } |
142 | | |
143 | | // The non-empty `row_ranges` means there are some rows left in this segment not added into `split`. |
144 | 1.31M | if (!row_ranges.is_empty()) { |
145 | 1.31M | DCHECK_GT(rows_collected, 0); |
146 | 1.31M | DCHECK_EQ(row_ranges.to(), rows_of_segment); |
147 | 1.31M | split.segment_row_ranges.emplace_back(std::move(row_ranges)); |
148 | 1.31M | } |
149 | 1.31M | } |
150 | | |
151 | 1.31M | DCHECK_LE(rows_collected, _rows_per_scanner); |
152 | 1.31M | if (rows_collected > 0) { |
153 | 1.31M | split.segment_offsets.first = segment_start; |
154 | 1.31M | split.segment_offsets.second = segments_rows.size(); |
155 | 1.31M | DCHECK_GT(split.segment_offsets.second, split.segment_offsets.first); |
156 | 1.31M | DCHECK_EQ(split.segment_row_ranges.size(), |
157 | 1.31M | split.segment_offsets.second - split.segment_offsets.first); |
158 | 1.31M | partitial_read_source.rs_splits.emplace_back(std::move(split)); |
159 | 1.31M | } |
160 | 1.31M | } // end `for (auto& rowset : rowsets)` |
161 | | |
162 | 815k | DCHECK_LE(rows_collected, _rows_per_scanner); |
163 | 815k | if (rows_collected > 0) { |
164 | 605k | DCHECK_GT(partitial_read_source.rs_splits.size(), 0); |
165 | 605k | #ifndef NDEBUG |
166 | 1.22M | for (auto& split : partitial_read_source.rs_splits) { |
167 | 1.22M | DCHECK(split.rs_reader != nullptr); |
168 | 1.22M | DCHECK_LT(split.segment_offsets.first, split.segment_offsets.second); |
169 | 1.22M | DCHECK_EQ(split.segment_row_ranges.size(), |
170 | 1.22M | split.segment_offsets.second - split.segment_offsets.first); |
171 | 1.22M | } |
172 | 605k | #endif |
173 | 605k | scanners.emplace_back( |
174 | 605k | _build_scanner(tablet, version, _key_ranges, |
175 | 605k | {.rs_splits = std::move(partitial_read_source.rs_splits), |
176 | 605k | .delete_predicates = entire_read_source.delete_predicates, |
177 | 605k | .delete_bitmap = entire_read_source.delete_bitmap}, |
178 | 605k | take_initial_file_cache_stats(&_tablet_preload_file_cache_stats, |
179 | 605k | tablet->tablet_id()))); |
180 | 605k | } |
181 | 815k | } |
182 | | |
183 | 172k | return Status::OK(); |
184 | 172k | } |
185 | | |
186 | | // Build scanners so that each segment is exclusively scanned by a single scanner. |
187 | | // This guarantees the number of scanners equals the number of segments across all rowsets |
188 | | // for the involved tablets. It preserves delete predicates and key ranges, and clones |
189 | | // RowsetReader per scanner to avoid sharing between scanners. |
190 | 24 | Status ParallelScannerBuilder::_build_scanners_by_per_segment(std::list<ScannerSPtr>& scanners) { |
191 | 24 | DCHECK_GE(_rows_per_scanner, _min_rows_per_scanner); |
192 | | |
193 | 120 | for (auto&& [tablet, version] : _tablets) { |
194 | 120 | DCHECK(_all_read_sources.contains(tablet->tablet_id())); |
195 | 120 | auto& entire_read_source = _all_read_sources[tablet->tablet_id()]; |
196 | | |
197 | 120 | if (config::is_cloud_mode()) { |
198 | | // FIXME(plat1ko): Avoid pointer cast |
199 | 120 | ExecEnv::GetInstance()->storage_engine().to_cloud().tablet_hotspot().count(*tablet); |
200 | 120 | } |
201 | | |
202 | | // For each RowSet split in the read source, split by segment id and build |
203 | | // one scanner per segment. Keep delete predicates shared. |
204 | 240 | for (auto& rs_split : entire_read_source.rs_splits) { |
205 | 240 | auto reader = rs_split.rs_reader; |
206 | 240 | auto rowset = reader->rowset(); |
207 | 240 | const auto rowset_id = rowset->rowset_id(); |
208 | 240 | const auto& segments_rows = _all_segments_rows[rowset_id]; |
209 | 240 | if (segments_rows.empty() || rowset->num_rows() == 0) { |
210 | 182 | continue; |
211 | 182 | } |
212 | | |
213 | | // Build scanners for [i, i+1) segment range, without row-range slicing. |
214 | 116 | for (int64_t i = 0; i < rowset->num_segments(); ++i) { |
215 | 58 | RowSetSplits split(reader->clone()); |
216 | 58 | split.segment_offsets.first = i; |
217 | 58 | split.segment_offsets.second = i + 1; |
218 | | // No row-ranges slicing; scan whole segment i. |
219 | 58 | DCHECK_GE(split.segment_offsets.second, split.segment_offsets.first + 1); |
220 | | |
221 | 58 | TabletReadSource partitial_read_source; |
222 | 58 | partitial_read_source.rs_splits.emplace_back(std::move(split)); |
223 | | |
224 | 58 | scanners.emplace_back(_build_scanner( |
225 | 58 | tablet, version, _key_ranges, |
226 | 58 | {.rs_splits = std::move(partitial_read_source.rs_splits), |
227 | 58 | .delete_predicates = entire_read_source.delete_predicates, |
228 | 58 | .delete_bitmap = entire_read_source.delete_bitmap}, |
229 | 58 | take_initial_file_cache_stats(&_tablet_preload_file_cache_stats, |
230 | 58 | tablet->tablet_id()))); |
231 | 58 | } |
232 | 58 | } |
233 | 120 | } |
234 | | |
235 | 24 | return Status::OK(); |
236 | 24 | } |
237 | | |
238 | | /** |
239 | | * Load rowsets of each tablet with specified version, segments of each rowset. |
240 | | */ |
241 | 170k | Status ParallelScannerBuilder::_load() { |
242 | 170k | _total_rows = 0; |
243 | 170k | size_t idx = 0; |
244 | 170k | bool enable_segment_cache = _state->query_options().__isset.enable_segment_cache |
245 | 170k | ? _state->query_options().enable_segment_cache |
246 | 170k | : true; |
247 | 813k | for (auto&& [tablet, version] : _tablets) { |
248 | 813k | const auto tablet_id = tablet->tablet_id(); |
249 | 813k | _all_read_sources[tablet_id] = _read_sources[idx]; |
250 | 813k | const auto& read_source = _all_read_sources[tablet_id]; |
251 | 3.41M | for (auto& rs_split : read_source.rs_splits) { |
252 | 3.41M | auto rowset = rs_split.rs_reader->rowset(); |
253 | 3.41M | RETURN_IF_ERROR(rowset->load()); |
254 | 3.41M | const auto rowset_id = rowset->rowset_id(); |
255 | | |
256 | 3.41M | auto beta_rowset = std::dynamic_pointer_cast<BetaRowset>(rowset); |
257 | 3.41M | std::vector<uint32_t> segment_rows; |
258 | 3.41M | OlapReaderStatistics preload_stats; |
259 | 3.41M | RETURN_IF_ERROR(beta_rowset->get_segment_num_rows(&segment_rows, enable_segment_cache, |
260 | 3.41M | &preload_stats)); |
261 | 3.41M | _tablet_preload_file_cache_stats[tablet_id].merge_from(preload_stats.file_cache_stats); |
262 | 3.41M | auto segment_count = rowset->num_segments(); |
263 | 4.72M | for (int64_t i = 0; i != segment_count; i++) { |
264 | 1.31M | _all_segments_rows[rowset_id].emplace_back(segment_rows[i]); |
265 | 1.31M | } |
266 | 3.41M | _total_rows += rowset->num_rows(); |
267 | 3.41M | } |
268 | 813k | idx++; |
269 | 813k | } |
270 | | |
271 | 170k | _rows_per_scanner = _total_rows / _max_scanners_count; |
272 | 170k | _rows_per_scanner = std::max<size_t>(_rows_per_scanner, _min_rows_per_scanner); |
273 | | |
274 | 170k | return Status::OK(); |
275 | 170k | } |
276 | | |
277 | | std::shared_ptr<OlapScanner> ParallelScannerBuilder::_build_scanner( |
278 | | BaseTabletSPtr tablet, int64_t version, const std::vector<OlapScanRange*>& key_ranges, |
279 | 659k | TabletReadSource&& read_source, io::FileCacheStatistics&& initial_file_cache_stats) { |
280 | 659k | OlapScanner::Params params { |
281 | 659k | .state = _state, |
282 | 659k | .profile = _scanner_profile.get(), |
283 | 659k | .key_ranges = key_ranges, |
284 | 659k | .tablet = std::move(tablet), |
285 | 659k | .version = version, |
286 | 659k | .read_source = std::move(read_source), |
287 | 659k | .initial_file_cache_stats = std::move(initial_file_cache_stats), |
288 | 659k | .limit = _limit, |
289 | 659k | .aggregation = _is_preaggregation, |
290 | 659k | .read_row_binlog = false, |
291 | 659k | .binlog_scan_type = TBinlogScanType::NONE, |
292 | 659k | .start_tso = std::nullopt, |
293 | 659k | .end_tso = std::nullopt, |
294 | 659k | }; |
295 | 659k | return OlapScanner::create_shared(_parent, std::move(params)); |
296 | 659k | } |
297 | | |
298 | | } // namespace doris |