be/src/io/fs/buffered_reader.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 "io/fs/buffered_reader.h" |
19 | | |
20 | | #include <bvar/reducer.h> |
21 | | #include <bvar/window.h> |
22 | | #include <string.h> |
23 | | |
24 | | #include <algorithm> |
25 | | #include <chrono> |
26 | | #include <cstdint> |
27 | | #include <memory> |
28 | | |
29 | | #include "common/cast_set.h" |
30 | | #include "common/compiler_util.h" // IWYU pragma: keep |
31 | | #include "common/config.h" |
32 | | #include "common/status.h" |
33 | | #include "core/custom_allocator.h" |
34 | | #include "runtime/exec_env.h" |
35 | | #include "runtime/file_scan_profile.h" |
36 | | #include "runtime/runtime_profile.h" |
37 | | #include "runtime/thread_context.h" |
38 | | #include "runtime/workload_group/workload_group.h" |
39 | | #include "runtime/workload_management/io_throttle.h" |
40 | | #include "runtime/workload_management/resource_context.h" |
41 | | #include "util/slice.h" |
42 | | #include "util/threadpool.h" |
43 | | namespace doris { |
44 | | |
45 | | namespace io { |
46 | | struct IOContext; |
47 | | |
48 | | // add bvar to capture the download bytes per second by buffered reader |
49 | | bvar::Adder<uint64_t> g_bytes_downloaded("buffered_reader", "bytes_downloaded"); |
50 | | bvar::PerSecond<bvar::Adder<uint64_t>> g_bytes_downloaded_per_second("buffered_reader", |
51 | | "bytes_downloaded_per_second", |
52 | | &g_bytes_downloaded, 60); |
53 | | |
54 | | Status MergeRangeFileReader::read_at_impl(size_t offset, Slice result, size_t* bytes_read, |
55 | 854 | const IOContext* io_ctx) { |
56 | 854 | _statistics.request_io++; |
57 | 854 | *bytes_read = 0; |
58 | 854 | if (result.size == 0) { |
59 | 0 | return Status::OK(); |
60 | 0 | } |
61 | 854 | const int range_index = _search_read_range(offset, offset + result.size); |
62 | 854 | if (range_index < 0) { |
63 | 3 | SCOPED_RAW_TIMER(&_statistics.read_time); |
64 | 3 | Status st = _reader->read_at(offset, result, bytes_read, io_ctx); |
65 | 3 | _statistics.merged_io++; |
66 | 3 | _statistics.request_bytes += *bytes_read; |
67 | 3 | _statistics.merged_bytes += *bytes_read; |
68 | 3 | return st; |
69 | 3 | } |
70 | 851 | if (offset + result.size > _random_access_ranges[range_index].end_offset) { |
71 | | // return _reader->read_at(offset, result, bytes_read, io_ctx); |
72 | 0 | return Status::IOError("Range in RandomAccessReader should be read sequentially"); |
73 | 0 | } |
74 | | |
75 | 851 | size_t has_read = 0; |
76 | 851 | RangeCachedData& cached_data = _range_cached_data[range_index]; |
77 | 851 | cached_data.has_read = true; |
78 | 851 | if (cached_data.contains(offset)) { |
79 | | // has cached data in box |
80 | 523 | _read_in_box(cached_data, offset, result, &has_read); |
81 | 523 | _statistics.request_bytes += has_read; |
82 | 523 | if (has_read == result.size) { |
83 | | // all data is read in cache |
84 | 523 | *bytes_read = has_read; |
85 | 523 | return Status::OK(); |
86 | 523 | } |
87 | 523 | } else if (!cached_data.empty()) { |
88 | | // the data in range may be skipped or ignored |
89 | 0 | for (int16_t box_index : cached_data.ref_box) { |
90 | 0 | _dec_box_ref(box_index); |
91 | 0 | } |
92 | 0 | cached_data.reset(); |
93 | 0 | } |
94 | | |
95 | 328 | size_t to_read = result.size - has_read; |
96 | 328 | if (to_read >= SMALL_IO || to_read >= _remaining) { |
97 | 0 | SCOPED_RAW_TIMER(&_statistics.read_time); |
98 | 0 | size_t read_size = 0; |
99 | 0 | RETURN_IF_ERROR(_reader->read_at(offset + has_read, Slice(result.data + has_read, to_read), |
100 | 0 | &read_size, io_ctx)); |
101 | 0 | *bytes_read = has_read + read_size; |
102 | 0 | _statistics.merged_io++; |
103 | 0 | _statistics.request_bytes += read_size; |
104 | 0 | _statistics.merged_bytes += read_size; |
105 | 0 | return Status::OK(); |
106 | 0 | } |
107 | | |
108 | | // merge small IO |
109 | 328 | size_t merge_start = offset + has_read; |
110 | 328 | const size_t merge_end = merge_start + _merged_read_slice_size; |
111 | | // <slice_size, is_content> |
112 | 328 | std::vector<std::pair<size_t, bool>> merged_slice; |
113 | 328 | size_t content_size = 0; |
114 | 328 | size_t hollow_size = 0; |
115 | 328 | if (merge_start > _random_access_ranges[range_index].end_offset) { |
116 | 0 | return Status::IOError("Fail to merge small IO"); |
117 | 0 | } |
118 | 328 | int merge_index = range_index; |
119 | 974 | while (merge_start < merge_end && merge_index < _random_access_ranges.size()) { |
120 | 671 | size_t content_max = _remaining - content_size; |
121 | 671 | if (content_max == 0) { |
122 | 0 | break; |
123 | 0 | } |
124 | 671 | if (merge_index != range_index && _range_cached_data[merge_index].has_read) { |
125 | | // don't read or merge twice |
126 | 0 | break; |
127 | 0 | } |
128 | 671 | if (_random_access_ranges[merge_index].end_offset > merge_end) { |
129 | 0 | size_t add_content = std::min(merge_end - merge_start, content_max); |
130 | 0 | content_size += add_content; |
131 | 0 | merge_start += add_content; |
132 | 0 | merged_slice.emplace_back(add_content, true); |
133 | 0 | break; |
134 | 0 | } |
135 | 671 | size_t add_content = |
136 | 671 | std::min(_random_access_ranges[merge_index].end_offset - merge_start, content_max); |
137 | 671 | content_size += add_content; |
138 | 671 | merge_start += add_content; |
139 | 671 | merged_slice.emplace_back(add_content, true); |
140 | 671 | if (merge_start != _random_access_ranges[merge_index].end_offset) { |
141 | 0 | break; |
142 | 0 | } |
143 | 671 | if (merge_index < _random_access_ranges.size() - 1 && merge_start < merge_end) { |
144 | 368 | size_t gap = _random_access_ranges[merge_index + 1].start_offset - |
145 | 368 | _random_access_ranges[merge_index].end_offset; |
146 | 368 | if ((content_size + hollow_size) > SMALL_IO && gap >= SMALL_IO) { |
147 | | // too large gap |
148 | 0 | break; |
149 | 0 | } |
150 | 368 | if (gap < merge_end - merge_start && content_size < _remaining && |
151 | 368 | !_range_cached_data[merge_index + 1].has_read) { |
152 | 343 | hollow_size += gap; |
153 | 343 | merge_start = _random_access_ranges[merge_index + 1].start_offset; |
154 | 343 | merged_slice.emplace_back(gap, false); |
155 | 343 | } else { |
156 | | // there's no enough memory to read hollow data |
157 | 25 | break; |
158 | 25 | } |
159 | 368 | } |
160 | 646 | merge_index++; |
161 | 646 | } |
162 | 328 | content_size = 0; |
163 | 328 | hollow_size = 0; |
164 | 328 | std::vector<std::pair<double, size_t>> ratio_and_size; |
165 | | // Calculate the read amplified ratio for each merge operation and the size of the merged data. |
166 | | // Find the largest size of the merged data whose amplified ratio is less than config::max_amplified_read_ratio |
167 | 1.01k | for (const std::pair<size_t, bool>& slice : merged_slice) { |
168 | 1.01k | if (slice.second) { |
169 | 671 | content_size += slice.first; |
170 | 671 | if (slice.first > 0) { |
171 | 671 | ratio_and_size.emplace_back((double)hollow_size / (double)content_size, |
172 | 671 | content_size + hollow_size); |
173 | 671 | } |
174 | 671 | } else { |
175 | 343 | hollow_size += slice.first; |
176 | 343 | } |
177 | 1.01k | } |
178 | 328 | size_t best_merged_size = 0; |
179 | 999 | for (int i = 0; i < ratio_and_size.size(); ++i) { |
180 | 671 | const std::pair<double, size_t>& rs = ratio_and_size[i]; |
181 | 671 | size_t equivalent_size = rs.second / (i + 1); |
182 | 671 | if (rs.second > best_merged_size) { |
183 | 671 | if (rs.first <= _max_amplified_ratio || |
184 | 671 | (_max_amplified_ratio < 1 && equivalent_size <= _equivalent_io_size)) { |
185 | 671 | best_merged_size = rs.second; |
186 | 671 | } |
187 | 671 | } |
188 | 671 | } |
189 | | |
190 | 328 | if (best_merged_size == to_read) { |
191 | | // read directly to avoid copy operation |
192 | 152 | SCOPED_RAW_TIMER(&_statistics.read_time); |
193 | 152 | size_t read_size = 0; |
194 | 152 | RETURN_IF_ERROR(_reader->read_at(offset + has_read, Slice(result.data + has_read, to_read), |
195 | 152 | &read_size, io_ctx)); |
196 | 152 | *bytes_read = has_read + read_size; |
197 | 152 | _statistics.merged_io++; |
198 | 152 | _statistics.request_bytes += read_size; |
199 | 152 | _statistics.merged_bytes += read_size; |
200 | 152 | return Status::OK(); |
201 | 152 | } |
202 | | |
203 | 176 | merge_start = offset + has_read; |
204 | 176 | size_t merge_read_size = 0; |
205 | 176 | RETURN_IF_ERROR( |
206 | 176 | _fill_box(range_index, merge_start, best_merged_size, &merge_read_size, io_ctx)); |
207 | 176 | if (cached_data.start_offset != merge_start) { |
208 | 0 | return Status::IOError("Wrong start offset in merged IO"); |
209 | 0 | } |
210 | | |
211 | | // read from cached data |
212 | 176 | size_t box_read_size = 0; |
213 | 176 | _read_in_box(cached_data, merge_start, Slice(result.data + has_read, to_read), &box_read_size); |
214 | 176 | *bytes_read = has_read + box_read_size; |
215 | 176 | _statistics.request_bytes += box_read_size; |
216 | 176 | if (*bytes_read < result.size && box_read_size < merge_read_size) { |
217 | 0 | return Status::IOError("Can't read enough bytes in merged IO"); |
218 | 0 | } |
219 | 176 | return Status::OK(); |
220 | 176 | } |
221 | | |
222 | 854 | int MergeRangeFileReader::_search_read_range(size_t start_offset, size_t end_offset) { |
223 | 854 | if (_random_access_ranges.empty()) { |
224 | 0 | return -1; |
225 | 0 | } |
226 | 854 | int left = 0, right = cast_set<int>(_random_access_ranges.size()) - 1; |
227 | 2.10k | do { |
228 | 2.10k | int mid = left + (right - left) / 2; |
229 | 2.10k | const PrefetchRange& range = _random_access_ranges[mid]; |
230 | 2.10k | if (range.start_offset <= start_offset && start_offset < range.end_offset) { |
231 | 851 | if (range.start_offset <= end_offset && end_offset <= range.end_offset) { |
232 | 851 | return mid; |
233 | 851 | } else { |
234 | 0 | return -1; |
235 | 0 | } |
236 | 1.25k | } else if (range.start_offset > start_offset) { |
237 | 528 | right = mid - 1; |
238 | 726 | } else { |
239 | 726 | left = mid + 1; |
240 | 726 | } |
241 | 2.10k | } while (left <= right); |
242 | 3 | return -1; |
243 | 854 | } |
244 | | |
245 | 492 | void MergeRangeFileReader::_clean_cached_data(RangeCachedData& cached_data) { |
246 | 492 | if (!cached_data.empty()) { |
247 | 0 | for (int i = 0; i < cached_data.ref_box.size(); ++i) { |
248 | 0 | DCHECK_GT(cached_data.box_end_offset[i], cached_data.box_start_offset[i]); |
249 | 0 | int16_t box_index = cached_data.ref_box[i]; |
250 | 0 | DCHECK_GT(_box_ref[box_index], 0); |
251 | 0 | _box_ref[box_index]--; |
252 | 0 | } |
253 | 0 | } |
254 | 492 | cached_data.reset(); |
255 | 492 | } |
256 | | |
257 | 556 | void MergeRangeFileReader::_dec_box_ref(int16_t box_index) { |
258 | 556 | if (--_box_ref[box_index] == 0) { |
259 | 233 | _remaining += BOX_SIZE; |
260 | 233 | } |
261 | 556 | if (box_index == _last_box_ref) { |
262 | 162 | _last_box_ref = -1; |
263 | 162 | _last_box_usage = 0; |
264 | 162 | } |
265 | 556 | } |
266 | | |
267 | | void MergeRangeFileReader::_read_in_box(RangeCachedData& cached_data, size_t offset, Slice result, |
268 | 699 | size_t* bytes_read) { |
269 | 699 | SCOPED_RAW_TIMER(&_statistics.copy_time); |
270 | 708 | auto handle_in_box = [&](size_t remaining, char* copy_out) { |
271 | 708 | size_t to_handle = remaining; |
272 | 708 | int cleaned_box = 0; |
273 | 1.47k | for (int i = 0; i < cached_data.ref_box.size() && remaining > 0; ++i) { |
274 | 768 | int16_t box_index = cached_data.ref_box[i]; |
275 | 768 | size_t box_to_handle = std::min(remaining, (size_t)(cached_data.box_end_offset[i] - |
276 | 768 | cached_data.box_start_offset[i])); |
277 | 768 | if (copy_out != nullptr) { |
278 | 759 | } |
279 | 768 | if (copy_out != nullptr) { |
280 | 759 | memcpy(copy_out + to_handle - remaining, |
281 | 759 | _boxes[box_index].data() + cached_data.box_start_offset[i], box_to_handle); |
282 | 759 | } |
283 | 768 | remaining -= box_to_handle; |
284 | 768 | cached_data.box_start_offset[i] += box_to_handle; |
285 | 768 | if (cached_data.box_start_offset[i] == cached_data.box_end_offset[i]) { |
286 | 556 | cleaned_box++; |
287 | 556 | _dec_box_ref(box_index); |
288 | 556 | } |
289 | 768 | } |
290 | 708 | DCHECK_EQ(remaining, 0); |
291 | 708 | if (cleaned_box > 0) { |
292 | 526 | cached_data.ref_box.erase(cached_data.ref_box.begin(), |
293 | 526 | cached_data.ref_box.begin() + cleaned_box); |
294 | 526 | cached_data.box_start_offset.erase(cached_data.box_start_offset.begin(), |
295 | 526 | cached_data.box_start_offset.begin() + cleaned_box); |
296 | 526 | cached_data.box_end_offset.erase(cached_data.box_end_offset.begin(), |
297 | 526 | cached_data.box_end_offset.begin() + cleaned_box); |
298 | 526 | } |
299 | 708 | cached_data.start_offset += to_handle; |
300 | 708 | if (cached_data.start_offset == cached_data.end_offset) { |
301 | 492 | _clean_cached_data(cached_data); |
302 | 492 | } |
303 | 708 | }; |
304 | | |
305 | 699 | if (offset > cached_data.start_offset) { |
306 | | // the data in range may be skipped |
307 | 9 | size_t to_skip = offset - cached_data.start_offset; |
308 | 9 | handle_in_box(to_skip, nullptr); |
309 | 9 | } |
310 | | |
311 | 699 | size_t to_read = std::min(cached_data.end_offset - cached_data.start_offset, result.size); |
312 | 699 | handle_in_box(to_read, result.data); |
313 | 699 | *bytes_read = to_read; |
314 | 699 | } |
315 | | |
316 | | Status MergeRangeFileReader::_fill_box(int range_index, size_t start_offset, size_t to_read, |
317 | 176 | size_t* bytes_read, const IOContext* io_ctx) { |
318 | 176 | if (!_read_slice) { |
319 | 161 | _read_slice = std::make_unique<OwnedSlice>(_merged_read_slice_size); |
320 | 161 | } |
321 | | |
322 | 176 | *bytes_read = 0; |
323 | 176 | { |
324 | 176 | SCOPED_RAW_TIMER(&_statistics.read_time); |
325 | 176 | RETURN_IF_ERROR(_reader->read_at(start_offset, Slice(_read_slice->data(), to_read), |
326 | 176 | bytes_read, io_ctx)); |
327 | 176 | _statistics.merged_io++; |
328 | 176 | _statistics.merged_bytes += *bytes_read; |
329 | 176 | } |
330 | | |
331 | 176 | SCOPED_RAW_TIMER(&_statistics.copy_time); |
332 | 176 | size_t copy_start = start_offset; |
333 | 176 | const size_t copy_end = start_offset + *bytes_read; |
334 | | // copy data into small boxes |
335 | | // tuple(box_index, box_start_offset, file_start_offset, file_end_offset) |
336 | 176 | std::vector<std::tuple<int16_t, uint32_t, size_t, size_t>> filled_boxes; |
337 | | |
338 | 583 | auto fill_box = [&](int16_t fill_box_ref, uint32_t box_usage, size_t box_copy_end) { |
339 | 583 | size_t copy_size = std::min(box_copy_end - copy_start, BOX_SIZE - box_usage); |
340 | 583 | memcpy(_boxes[fill_box_ref].data() + box_usage, |
341 | 583 | _read_slice->data() + copy_start - start_offset, copy_size); |
342 | 583 | filled_boxes.emplace_back(fill_box_ref, box_usage, copy_start, copy_start + copy_size); |
343 | 583 | copy_start += copy_size; |
344 | 583 | _last_box_ref = fill_box_ref; |
345 | 583 | _last_box_usage = box_usage + cast_set<int>(copy_size); |
346 | 583 | _box_ref[fill_box_ref]++; |
347 | 583 | if (box_usage == 0) { |
348 | 256 | _remaining -= BOX_SIZE; |
349 | 256 | } |
350 | 583 | }; |
351 | | |
352 | 176 | for (int fill_range_index = range_index; |
353 | 695 | fill_range_index < _random_access_ranges.size() && copy_start < copy_end; |
354 | 519 | ++fill_range_index) { |
355 | 519 | RangeCachedData& fill_range_cache = _range_cached_data[fill_range_index]; |
356 | 519 | DCHECK(fill_range_cache.empty()); |
357 | 519 | fill_range_cache.reset(); |
358 | 519 | const PrefetchRange& fill_range = _random_access_ranges[fill_range_index]; |
359 | 519 | if (fill_range.start_offset > copy_start) { |
360 | | // don't copy hollow data |
361 | 27 | size_t hollow_size = fill_range.start_offset - copy_start; |
362 | 27 | DCHECK_GT(copy_end - copy_start, hollow_size); |
363 | 27 | copy_start += hollow_size; |
364 | 27 | } |
365 | | |
366 | 519 | const size_t range_copy_end = std::min(copy_end, fill_range.end_offset); |
367 | | // reuse the remaining capacity of last box |
368 | 519 | if (_last_box_ref >= 0 && _last_box_usage < BOX_SIZE) { |
369 | 327 | fill_box(_last_box_ref, _last_box_usage, range_copy_end); |
370 | 327 | } |
371 | | // reuse the former released box |
372 | 1.83k | for (int16_t i = 0; i < _boxes.size() && copy_start < range_copy_end; ++i) { |
373 | 1.31k | if (_box_ref[i] == 0) { |
374 | 6 | fill_box(i, 0, range_copy_end); |
375 | 6 | } |
376 | 1.31k | } |
377 | | // apply for new box to copy data |
378 | 769 | while (copy_start < range_copy_end && _boxes.size() < NUM_BOX) { |
379 | 250 | _boxes.emplace_back(BOX_SIZE); |
380 | 250 | _box_ref.emplace_back(0); |
381 | 250 | fill_box(cast_set<int16_t>(_boxes.size()) - 1, 0, range_copy_end); |
382 | 250 | } |
383 | 519 | DCHECK_EQ(copy_start, range_copy_end); |
384 | | |
385 | 519 | if (!filled_boxes.empty()) { |
386 | 519 | fill_range_cache.start_offset = std::get<2>(filled_boxes[0]); |
387 | 519 | fill_range_cache.end_offset = std::get<3>(filled_boxes.back()); |
388 | 583 | for (auto& tuple : filled_boxes) { |
389 | 583 | fill_range_cache.ref_box.emplace_back(std::get<0>(tuple)); |
390 | 583 | fill_range_cache.box_start_offset.emplace_back(std::get<1>(tuple)); |
391 | 583 | fill_range_cache.box_end_offset.emplace_back( |
392 | 583 | std::get<1>(tuple) + std::get<3>(tuple) - std::get<2>(tuple)); |
393 | 583 | } |
394 | 519 | filled_boxes.clear(); |
395 | 519 | } |
396 | 519 | } |
397 | 176 | return Status::OK(); |
398 | 176 | } |
399 | | |
400 | | // there exists occasions where the buffer is already closed but |
401 | | // some prior tasks are still queued in thread pool, so we have to check whether |
402 | | // the buffer is closed each time the condition variable is notified. |
403 | 23 | void PrefetchBuffer::reset_offset(size_t offset) { |
404 | 23 | { |
405 | 23 | std::unique_lock lck {_lock}; |
406 | 23 | if (!_prefetched.wait_for( |
407 | 23 | lck, std::chrono::milliseconds(config::buffered_reader_read_timeout_ms), |
408 | 23 | [this]() { return _buffer_status != BufferStatus::PENDING; })) { |
409 | 0 | _prefetch_status = Status::TimedOut("time out when reset prefetch buffer"); |
410 | 0 | return; |
411 | 0 | } |
412 | 23 | if (UNLIKELY(_buffer_status == BufferStatus::CLOSED)) { |
413 | 0 | _prefetched.notify_all(); |
414 | 0 | return; |
415 | 0 | } |
416 | 23 | _buffer_status = BufferStatus::RESET; |
417 | 23 | _offset = offset; |
418 | 23 | _prefetched.notify_all(); |
419 | 23 | } |
420 | 23 | if (UNLIKELY(offset >= _file_range.end_offset)) { |
421 | 18 | _len = 0; |
422 | 18 | _exceed = true; |
423 | 18 | return; |
424 | 18 | } else { |
425 | 5 | _exceed = false; |
426 | 5 | } |
427 | | // Lazy-allocate the backing buffer in the calling (query) thread, which has a |
428 | | // MemTrackerLimiter attached. The prefetch thread pool threads are "Orphan" threads |
429 | | // without a tracker, so allocation must not happen there. |
430 | 5 | if (_buf.empty()) { |
431 | 5 | _buf.resize(_size); |
432 | 5 | } |
433 | 5 | _prefetch_status = ExecEnv::GetInstance()->buffered_reader_prefetch_thread_pool()->submit_func( |
434 | 5 | [buffer_ptr = shared_from_this()]() { buffer_ptr->prefetch_buffer(); }); |
435 | 5 | } |
436 | | |
437 | | // only this function would run concurrently in another thread |
438 | 5 | void PrefetchBuffer::prefetch_buffer() { |
439 | 5 | { |
440 | 5 | std::unique_lock lck {_lock}; |
441 | 5 | if (!_prefetched.wait_for( |
442 | 5 | lck, std::chrono::milliseconds(config::buffered_reader_read_timeout_ms), |
443 | 5 | [this]() { |
444 | 5 | return _buffer_status == BufferStatus::RESET || |
445 | 5 | _buffer_status == BufferStatus::CLOSED; |
446 | 5 | })) { |
447 | 0 | _prefetch_status = Status::TimedOut("time out when invoking prefetch buffer"); |
448 | 0 | return; |
449 | 0 | } |
450 | | // in case buffer is already closed |
451 | 5 | if (UNLIKELY(_buffer_status == BufferStatus::CLOSED)) { |
452 | 0 | _prefetched.notify_all(); |
453 | 0 | return; |
454 | 0 | } |
455 | 5 | _buffer_status = BufferStatus::PENDING; |
456 | 5 | _prefetched.notify_all(); |
457 | 5 | } |
458 | | |
459 | 0 | int read_range_index = search_read_range(_offset); |
460 | 5 | size_t buf_size; |
461 | 5 | if (read_range_index == -1) { |
462 | 5 | buf_size = |
463 | 5 | _file_range.end_offset - _offset > _size ? _size : _file_range.end_offset - _offset; |
464 | 5 | } else { |
465 | 0 | buf_size = merge_small_ranges(_offset, read_range_index); |
466 | 0 | } |
467 | | |
468 | 5 | _len = 0; |
469 | 5 | Status s; |
470 | | |
471 | 5 | { |
472 | 5 | SCOPED_RAW_TIMER(&_statis.read_time); |
473 | 5 | s = _reader->read_at(_offset, Slice {_buf.data(), buf_size}, &_len, _io_ctx); |
474 | 5 | } |
475 | 5 | if (UNLIKELY(s.ok() && buf_size != _len)) { |
476 | | // This indicates that the data size returned by S3 object storage is smaller than what we requested, |
477 | | // which seems to be a violation of the S3 protocol since our request range was valid. |
478 | | // We currently consider this situation a bug and will treat this task as a failure. |
479 | 0 | s = Status::InternalError("Data size returned by S3 is smaller than requested"); |
480 | 0 | LOG(WARNING) << "Data size returned by S3 is smaller than requested" << _reader->path() |
481 | 0 | << " request bytes " << buf_size << " returned size " << _len; |
482 | 0 | } |
483 | 5 | g_bytes_downloaded << _len; |
484 | 5 | _statis.prefetch_request_io += 1; |
485 | 5 | _statis.prefetch_request_bytes += _len; |
486 | 5 | std::unique_lock lck {_lock}; |
487 | 5 | if (!_prefetched.wait_for(lck, |
488 | 5 | std::chrono::milliseconds(config::buffered_reader_read_timeout_ms), |
489 | 5 | [this]() { return _buffer_status == BufferStatus::PENDING; })) { |
490 | 0 | _prefetch_status = Status::TimedOut("time out when invoking prefetch buffer"); |
491 | 0 | return; |
492 | 0 | } |
493 | 5 | if (!s.ok() && _offset < _reader->size()) { |
494 | | // We should print the error msg since this buffer might not be accessed by the consumer |
495 | | // which would result in the status being missed |
496 | 1 | LOG_WARNING("prefetch path {} failed, offset {}, error {}", _reader->path().native(), |
497 | 1 | _offset, s.to_string()); |
498 | 1 | _prefetch_status = std::move(s); |
499 | 1 | } |
500 | 5 | _buffer_status = BufferStatus::PREFETCHED; |
501 | 5 | _prefetched.notify_all(); |
502 | | // eof would come up with len == 0, it would be handled by read_buffer |
503 | 5 | } |
504 | | |
505 | 5 | int PrefetchBuffer::search_read_range(size_t off) const { |
506 | 5 | if (_random_access_ranges == nullptr || _random_access_ranges->empty()) { |
507 | 5 | return -1; |
508 | 5 | } |
509 | 0 | const std::vector<PrefetchRange>& random_access_ranges = *_random_access_ranges; |
510 | 0 | int left = 0, right = cast_set<int>(random_access_ranges.size()) - 1; |
511 | 0 | do { |
512 | 0 | int mid = left + (right - left) / 2; |
513 | 0 | const PrefetchRange& range = random_access_ranges[mid]; |
514 | 0 | if (range.start_offset <= off && range.end_offset > off) { |
515 | 0 | return mid; |
516 | 0 | } else if (range.start_offset > off) { |
517 | 0 | right = mid; |
518 | 0 | } else { |
519 | 0 | left = mid + 1; |
520 | 0 | } |
521 | 0 | } while (left < right); |
522 | 0 | if (random_access_ranges[right].start_offset > off) { |
523 | 0 | return right; |
524 | 0 | } else { |
525 | 0 | return -1; |
526 | 0 | } |
527 | 0 | } |
528 | | |
529 | 0 | size_t PrefetchBuffer::merge_small_ranges(size_t off, int range_index) const { |
530 | 0 | if (_random_access_ranges == nullptr || _random_access_ranges->empty()) { |
531 | 0 | return _size; |
532 | 0 | } |
533 | 0 | int64_t remaining = _size; |
534 | 0 | const std::vector<PrefetchRange>& random_access_ranges = *_random_access_ranges; |
535 | 0 | while (remaining > 0 && range_index < random_access_ranges.size()) { |
536 | 0 | const PrefetchRange& range = random_access_ranges[range_index]; |
537 | 0 | if (range.start_offset <= off && range.end_offset > off) { |
538 | 0 | remaining -= range.end_offset - off; |
539 | 0 | off = range.end_offset; |
540 | 0 | range_index++; |
541 | 0 | } else if (range.start_offset > off) { |
542 | | // merge small range |
543 | 0 | size_t hollow = range.start_offset - off; |
544 | 0 | if (hollow < remaining) { |
545 | 0 | remaining -= hollow; |
546 | 0 | off = range.start_offset; |
547 | 0 | } else { |
548 | 0 | break; |
549 | 0 | } |
550 | 0 | } else { |
551 | 0 | DCHECK(false); |
552 | 0 | } |
553 | 0 | } |
554 | 0 | if (remaining < 0 || remaining == _size) { |
555 | 0 | remaining = 0; |
556 | 0 | } |
557 | 0 | return _size - remaining; |
558 | 0 | } |
559 | | |
560 | | Status PrefetchBuffer::read_buffer(size_t off, const char* out, size_t buf_len, |
561 | 12 | size_t* bytes_read) { |
562 | 12 | if (UNLIKELY(off >= _file_range.end_offset)) { |
563 | | // Reader can read out of [start_offset, end_offset) by synchronous method. |
564 | 0 | return _reader->read_at(off, Slice {out, buf_len}, bytes_read, _io_ctx); |
565 | 0 | } |
566 | 12 | if (_exceed) { |
567 | 0 | reset_offset((off / _size) * _size); |
568 | 0 | return read_buffer(off, out, buf_len, bytes_read); |
569 | 0 | } |
570 | 12 | { |
571 | 12 | std::unique_lock lck {_lock}; |
572 | | // buffer must be prefetched or it's closed |
573 | 12 | if (!_prefetched.wait_for( |
574 | 12 | lck, std::chrono::milliseconds(config::buffered_reader_read_timeout_ms), |
575 | 22 | [this]() { |
576 | 22 | return _buffer_status == BufferStatus::PREFETCHED || |
577 | 22 | _buffer_status == BufferStatus::CLOSED; |
578 | 22 | })) { |
579 | 1 | _prefetch_status = Status::TimedOut("time out when read prefetch buffer"); |
580 | 1 | return _prefetch_status; |
581 | 1 | } |
582 | 11 | if (UNLIKELY(BufferStatus::CLOSED == _buffer_status)) { |
583 | 0 | return Status::OK(); |
584 | 0 | } |
585 | 11 | } |
586 | 11 | RETURN_IF_ERROR(_prefetch_status); |
587 | | // there is only parquet would do not sequence read |
588 | | // it would read the end of the file first |
589 | 11 | if (UNLIKELY(!contains(off))) { |
590 | 0 | reset_offset((off / _size) * _size); |
591 | 0 | return read_buffer(off, out, buf_len, bytes_read); |
592 | 0 | } |
593 | 11 | if (UNLIKELY(0 == _len || _offset + _len < off)) { |
594 | 0 | return Status::OK(); |
595 | 0 | } |
596 | | |
597 | 11 | { |
598 | 11 | LIMIT_REMOTE_SCAN_IO(bytes_read); |
599 | | // [0]: maximum len trying to read, [1] maximum length buffer can provide, [2] actual len buffer has |
600 | 11 | size_t read_len = std::min({buf_len, _offset + _size - off, _offset + _len - off}); |
601 | 11 | { |
602 | 11 | SCOPED_RAW_TIMER(&_statis.copy_time); |
603 | 11 | memcpy((void*)out, _buf.data() + (off - _offset), read_len); |
604 | 11 | } |
605 | 11 | *bytes_read = read_len; |
606 | 11 | _statis.request_io += 1; |
607 | 11 | _statis.request_bytes += read_len; |
608 | 11 | } |
609 | 11 | if (off + *bytes_read == _offset + _len) { |
610 | 3 | reset_offset(_offset + _whole_buffer_size); |
611 | 3 | } |
612 | 11 | return Status::OK(); |
613 | 11 | } |
614 | | |
615 | 20 | void PrefetchBuffer::close() { |
616 | 20 | std::unique_lock lck {_lock}; |
617 | | // in case _reader still tries to write to the buf after we close the buffer |
618 | 20 | if (!_prefetched.wait_for(lck, |
619 | 20 | std::chrono::milliseconds(config::buffered_reader_read_timeout_ms), |
620 | 21 | [this]() { return _buffer_status != BufferStatus::PENDING; })) { |
621 | 1 | _prefetch_status = Status::TimedOut("time out when close prefetch buffer"); |
622 | 1 | return; |
623 | 1 | } |
624 | 19 | _buffer_status = BufferStatus::CLOSED; |
625 | 19 | _prefetched.notify_all(); |
626 | | // Explicitly release the backing buffer here, in the calling (query) thread which has a |
627 | | // MemTrackerLimiter. The destructor may run in the thread pool's Orphan thread (when the |
628 | | // last shared_ptr ref is released after the prefetch lambda completes), so we must not |
629 | | // rely on ~PODArray() to release memory — that would trigger memory_orphan_check(). |
630 | 19 | PODArray<char>().swap(_buf); |
631 | 19 | } |
632 | | |
633 | 0 | void PrefetchBuffer::_collect_profile_before_close() { |
634 | 0 | if (_sync_profile != nullptr) { |
635 | 0 | _sync_profile(*this); |
636 | 0 | } |
637 | 0 | } |
638 | | |
639 | | // buffered reader |
640 | | PrefetchBufferedReader::PrefetchBufferedReader(RuntimeProfile* profile, io::FileReaderSPtr reader, |
641 | | PrefetchRange file_range, |
642 | | std::shared_ptr<const IOContext> io_ctx, |
643 | | int64_t buffer_size) |
644 | 5 | : _reader(std::move(reader)), _file_range(file_range), _io_ctx_holder(std::move(io_ctx)) { |
645 | 5 | if (_io_ctx_holder == nullptr) { |
646 | 5 | _io_ctx_holder = std::make_shared<IOContext>(); |
647 | 5 | } |
648 | 5 | _io_ctx = _io_ctx_holder.get(); |
649 | 5 | if (buffer_size == -1L) { |
650 | 5 | buffer_size = config::remote_storage_read_buffer_mb * 1024 * 1024; |
651 | 5 | } |
652 | 5 | _size = _reader->size(); |
653 | 5 | _whole_pre_buffer_size = buffer_size; |
654 | 5 | _file_range.end_offset = std::min(_file_range.end_offset, _size); |
655 | 5 | int buffer_num = buffer_size > s_max_pre_buffer_size |
656 | 5 | ? cast_set<int>(buffer_size) / cast_set<int>(s_max_pre_buffer_size) |
657 | 5 | : 1; |
658 | 5 | std::function<void(PrefetchBuffer&)> sync_buffer = nullptr; |
659 | 5 | if (profile != nullptr) { |
660 | 0 | const char* prefetch_buffered_reader = "PrefetchBufferedReader"; |
661 | 0 | auto* total_time = |
662 | 0 | ADD_CHILD_TIMER(profile, prefetch_buffered_reader, |
663 | 0 | file_scan_profile::parent_or_root(profile, file_scan_profile::IO)); |
664 | 0 | auto copy_time = ADD_CHILD_TIMER(profile, "CopyTime", prefetch_buffered_reader); |
665 | 0 | auto read_time = ADD_CHILD_TIMER(profile, "ReadTime", prefetch_buffered_reader); |
666 | 0 | auto prefetch_request_io = |
667 | 0 | ADD_CHILD_COUNTER(profile, "PreRequestIO", TUnit::UNIT, prefetch_buffered_reader); |
668 | 0 | auto prefetch_request_bytes = ADD_CHILD_COUNTER(profile, "PreRequestBytes", TUnit::BYTES, |
669 | 0 | prefetch_buffered_reader); |
670 | 0 | auto request_io = |
671 | 0 | ADD_CHILD_COUNTER(profile, "RequestIO", TUnit::UNIT, prefetch_buffered_reader); |
672 | 0 | auto request_bytes = |
673 | 0 | ADD_CHILD_COUNTER(profile, "RequestBytes", TUnit::BYTES, prefetch_buffered_reader); |
674 | 0 | sync_buffer = [=](PrefetchBuffer& buf) { |
675 | 0 | COUNTER_UPDATE(total_time, buf._statis.copy_time + buf._statis.read_time); |
676 | 0 | COUNTER_UPDATE(copy_time, buf._statis.copy_time); |
677 | 0 | COUNTER_UPDATE(read_time, buf._statis.read_time); |
678 | 0 | COUNTER_UPDATE(prefetch_request_io, buf._statis.prefetch_request_io); |
679 | 0 | COUNTER_UPDATE(prefetch_request_bytes, buf._statis.prefetch_request_bytes); |
680 | 0 | COUNTER_UPDATE(request_io, buf._statis.request_io); |
681 | 0 | COUNTER_UPDATE(request_bytes, buf._statis.request_bytes); |
682 | 0 | }; |
683 | 0 | } |
684 | | // set the _cur_offset of this reader as same as the inner reader's, |
685 | | // to make sure the buffer reader will start to read at right position. |
686 | 25 | for (int i = 0; i < buffer_num; i++) { |
687 | 20 | _pre_buffers.emplace_back(std::make_shared<PrefetchBuffer>( |
688 | 20 | _file_range, s_max_pre_buffer_size, _whole_pre_buffer_size, _reader, _io_ctx_holder, |
689 | 20 | sync_buffer)); |
690 | 20 | } |
691 | 5 | } |
692 | | |
693 | 5 | PrefetchBufferedReader::~PrefetchBufferedReader() { |
694 | | /// Better not to call virtual functions in a destructor. |
695 | 5 | static_cast<void>(_close_internal()); |
696 | 5 | } |
697 | | |
698 | | Status PrefetchBufferedReader::read_at_impl(size_t offset, Slice result, size_t* bytes_read, |
699 | 17 | const IOContext* io_ctx) { |
700 | 17 | if (!_initialized) { |
701 | 5 | reset_all_buffer(offset); |
702 | 5 | _initialized = true; |
703 | 5 | } |
704 | 17 | if (UNLIKELY(result.get_size() == 0 || offset >= size())) { |
705 | 5 | *bytes_read = 0; |
706 | 5 | return Status::OK(); |
707 | 5 | } |
708 | 12 | size_t nbytes = result.get_size(); |
709 | 12 | int actual_bytes_read = 0; |
710 | 23 | while (actual_bytes_read < nbytes && offset < size()) { |
711 | 12 | size_t read_num = 0; |
712 | 12 | auto buffer_pos = get_buffer_pos(offset); |
713 | 12 | RETURN_IF_ERROR( |
714 | 12 | _pre_buffers[buffer_pos]->read_buffer(offset, result.get_data() + actual_bytes_read, |
715 | 12 | nbytes - actual_bytes_read, &read_num)); |
716 | 11 | actual_bytes_read += read_num; |
717 | 11 | offset += read_num; |
718 | 11 | } |
719 | 11 | *bytes_read = actual_bytes_read; |
720 | 11 | return Status::OK(); |
721 | 12 | } |
722 | | |
723 | 0 | Status PrefetchBufferedReader::close() { |
724 | 0 | return _close_internal(); |
725 | 0 | } |
726 | | |
727 | 5 | Status PrefetchBufferedReader::_close_internal() { |
728 | 5 | if (!_closed) { |
729 | 5 | _closed = true; |
730 | 5 | std::for_each(_pre_buffers.begin(), _pre_buffers.end(), |
731 | 20 | [](std::shared_ptr<PrefetchBuffer>& buffer) { buffer->close(); }); |
732 | 5 | return _reader->close(); |
733 | 5 | } |
734 | | |
735 | 0 | return Status::OK(); |
736 | 5 | } |
737 | | |
738 | 0 | void PrefetchBufferedReader::_collect_profile_before_close() { |
739 | 0 | std::for_each(_pre_buffers.begin(), _pre_buffers.end(), |
740 | 0 | [](std::shared_ptr<PrefetchBuffer>& buffer) { |
741 | 0 | buffer->collect_profile_before_close(); |
742 | 0 | }); |
743 | 0 | if (_reader != nullptr) { |
744 | 0 | _reader->collect_profile_before_close(); |
745 | 0 | } |
746 | 0 | } |
747 | | |
748 | | // InMemoryFileReader |
749 | 2 | InMemoryFileReader::InMemoryFileReader(io::FileReaderSPtr reader) : _reader(std::move(reader)) { |
750 | 2 | _size = _reader->size(); |
751 | 2 | } |
752 | | |
753 | 2 | InMemoryFileReader::~InMemoryFileReader() { |
754 | 2 | static_cast<void>(_close_internal()); |
755 | 2 | } |
756 | | |
757 | 4 | Status InMemoryFileReader::close() { |
758 | 4 | return _close_internal(); |
759 | 4 | } |
760 | | |
761 | 6 | Status InMemoryFileReader::_close_internal() { |
762 | 6 | if (!_closed) { |
763 | 2 | _closed = true; |
764 | 2 | return _reader->close(); |
765 | 2 | } |
766 | 4 | return Status::OK(); |
767 | 6 | } |
768 | | |
769 | | Status InMemoryFileReader::read_at_impl(size_t offset, Slice result, size_t* bytes_read, |
770 | 3 | const IOContext* io_ctx) { |
771 | 3 | if (_data == nullptr) { |
772 | 2 | _data = std::make_unique_for_overwrite<char[]>(_size); |
773 | | |
774 | 2 | size_t file_size = 0; |
775 | 2 | RETURN_IF_ERROR(_reader->read_at(0, Slice(_data.get(), _size), &file_size, io_ctx)); |
776 | 2 | DCHECK_EQ(file_size, _size); |
777 | 2 | } |
778 | 3 | if (UNLIKELY(offset > _size)) { |
779 | 0 | return Status::IOError("Out of bounds access"); |
780 | 0 | } |
781 | 3 | *bytes_read = std::min(result.size, _size - offset); |
782 | 3 | memcpy(result.data, _data.get() + offset, *bytes_read); |
783 | 3 | return Status::OK(); |
784 | 3 | } |
785 | | |
786 | 0 | void InMemoryFileReader::_collect_profile_before_close() { |
787 | 0 | if (_reader != nullptr) { |
788 | 0 | _reader->collect_profile_before_close(); |
789 | 0 | } |
790 | 0 | } |
791 | | |
792 | | // BufferedFileStreamReader |
793 | | BufferedFileStreamReader::BufferedFileStreamReader(io::FileReaderSPtr file, uint64_t offset, |
794 | | uint64_t length, size_t max_buf_size) |
795 | 849 | : _file(file), |
796 | 849 | _file_start_offset(offset), |
797 | 849 | _file_end_offset(offset + length), |
798 | 849 | _max_buf_size(max_buf_size) {} |
799 | | |
800 | | Status BufferedFileStreamReader::read_bytes(const uint8_t** buf, uint64_t offset, |
801 | 2.68k | const size_t bytes_to_read, const IOContext* io_ctx) { |
802 | 2.68k | if (offset < _file_start_offset || offset >= _file_end_offset || |
803 | 2.68k | offset + bytes_to_read > _file_end_offset) { |
804 | 0 | return Status::IOError( |
805 | 0 | "Out-of-bounds Access: offset={}, bytes_to_read={}, file_start={}, " |
806 | 0 | "file_end={}", |
807 | 0 | offset, bytes_to_read, _file_start_offset, _file_end_offset); |
808 | 0 | } |
809 | 2.68k | int64_t end_offset = offset + bytes_to_read; |
810 | 2.68k | if (_buf_start_offset <= offset && _buf_end_offset >= end_offset) { |
811 | 1.80k | *buf = _buf.get() + offset - _buf_start_offset; |
812 | 1.80k | return Status::OK(); |
813 | 1.80k | } |
814 | 879 | size_t buf_size = std::max(_max_buf_size, bytes_to_read); |
815 | 879 | if (_buf_size < buf_size) { |
816 | 764 | auto new_buf = make_unique_buffer<uint8_t>(buf_size); |
817 | 764 | if (offset >= _buf_start_offset && offset < _buf_end_offset) { |
818 | 28 | memcpy(new_buf.get(), _buf.get() + offset - _buf_start_offset, |
819 | 28 | _buf_end_offset - offset); |
820 | 28 | } |
821 | 764 | _buf = std::move(new_buf); |
822 | 764 | _buf_size = buf_size; |
823 | 764 | } else if (offset > _buf_start_offset && offset < _buf_end_offset) { |
824 | 74 | memmove(_buf.get(), _buf.get() + offset - _buf_start_offset, _buf_end_offset - offset); |
825 | 74 | } |
826 | 879 | if (offset < _buf_start_offset || offset >= _buf_end_offset) { |
827 | 777 | _buf_end_offset = offset; |
828 | 777 | } |
829 | 879 | _buf_start_offset = offset; |
830 | 879 | int64_t buf_remaining = _buf_end_offset - _buf_start_offset; |
831 | 879 | int64_t to_read = std::min(_buf_size - buf_remaining, _file_end_offset - _buf_end_offset); |
832 | 879 | int64_t has_read = 0; |
833 | 1.75k | while (has_read < to_read) { |
834 | 879 | size_t loop_read = 0; |
835 | 879 | Slice result(_buf.get() + buf_remaining + has_read, to_read - has_read); |
836 | 879 | RETURN_IF_ERROR(_file->read_at(_buf_end_offset + has_read, result, &loop_read, io_ctx)); |
837 | 879 | if (loop_read == 0) { |
838 | 0 | break; |
839 | 0 | } |
840 | 879 | has_read += loop_read; |
841 | 879 | } |
842 | 879 | if (has_read != to_read) { |
843 | 0 | return Status::Corruption("Try to read {} bytes, but received {} bytes", to_read, has_read); |
844 | 0 | } |
845 | 879 | _buf_end_offset += to_read; |
846 | 879 | *buf = _buf.get(); |
847 | 879 | return Status::OK(); |
848 | 879 | } |
849 | | |
850 | | Status BufferedFileStreamReader::read_bytes(Slice& slice, uint64_t offset, |
851 | 1.30k | const IOContext* io_ctx) { |
852 | 1.30k | return read_bytes((const uint8_t**)&slice.data, offset, slice.size, io_ctx); |
853 | 1.30k | } |
854 | | |
855 | | Result<io::FileReaderSPtr> DelegateReader::create_file_reader( |
856 | | RuntimeProfile* profile, const FileSystemProperties& system_properties, |
857 | | const FileDescription& file_description, const io::FileReaderOptions& reader_options, |
858 | 135 | AccessMode access_mode, const IOContext* io_ctx, const PrefetchRange file_range) { |
859 | 135 | std::shared_ptr<const IOContext> io_ctx_holder; |
860 | 135 | if (io_ctx != nullptr) { |
861 | | // Old API: best-effort safety by copying the IOContext onto the heap. |
862 | 93 | io_ctx_holder = std::make_shared<IOContext>(*io_ctx); |
863 | 93 | } |
864 | 135 | return create_file_reader(profile, system_properties, file_description, reader_options, |
865 | 135 | access_mode, std::move(io_ctx_holder), file_range); |
866 | 135 | } |
867 | | |
868 | | Result<io::FileReaderSPtr> DelegateReader::create_file_reader( |
869 | | RuntimeProfile* profile, const FileSystemProperties& system_properties, |
870 | | const FileDescription& file_description, const io::FileReaderOptions& reader_options, |
871 | | AccessMode access_mode, std::shared_ptr<const IOContext> io_ctx, |
872 | 684 | const PrefetchRange file_range) { |
873 | 684 | if (io_ctx == nullptr) { |
874 | 436 | io_ctx = std::make_shared<IOContext>(); |
875 | 436 | } |
876 | 684 | return FileFactory::create_file_reader(system_properties, file_description, reader_options, |
877 | 684 | profile) |
878 | 684 | .transform([&](auto&& reader) -> io::FileReaderSPtr { |
879 | 679 | if (reader->size() < config::in_memory_file_size && |
880 | 679 | typeid_cast<io::S3FileReader*>(reader.get())) { |
881 | 0 | return std::make_shared<InMemoryFileReader>(std::move(reader)); |
882 | 0 | } |
883 | | |
884 | 679 | if (access_mode == AccessMode::SEQUENTIAL) { |
885 | 0 | bool is_thread_safe = false; |
886 | 0 | if (typeid_cast<io::S3FileReader*>(reader.get())) { |
887 | 0 | is_thread_safe = true; |
888 | 0 | } else if (auto* cached_reader = |
889 | 0 | typeid_cast<io::CachedRemoteFileReader*>(reader.get()); |
890 | 0 | cached_reader && |
891 | 0 | typeid_cast<io::S3FileReader*>(cached_reader->get_remote_reader())) { |
892 | 0 | is_thread_safe = true; |
893 | 0 | } |
894 | 0 | if (is_thread_safe) { |
895 | | // PrefetchBufferedReader needs thread-safe reader to prefetch data concurrently. |
896 | 0 | return std::make_shared<io::PrefetchBufferedReader>( |
897 | 0 | profile, std::move(reader), file_range, io_ctx); |
898 | 0 | } |
899 | 0 | } |
900 | | |
901 | 679 | return reader; |
902 | 679 | }); |
903 | 684 | } |
904 | | |
905 | | Status LinearProbeRangeFinder::get_range_for(int64_t desired_offset, |
906 | 6 | io::PrefetchRange& result_range) { |
907 | 9 | while (index < _ranges.size()) { |
908 | 9 | io::PrefetchRange& range = _ranges[index]; |
909 | 9 | if (range.end_offset > desired_offset) { |
910 | 6 | if (range.start_offset > desired_offset) [[unlikely]] { |
911 | 0 | return Status::InvalidArgument("Invalid desiredOffset"); |
912 | 0 | } |
913 | 6 | result_range = range; |
914 | 6 | return Status::OK(); |
915 | 6 | } |
916 | 3 | ++index; |
917 | 3 | } |
918 | 0 | return Status::InvalidArgument("Invalid desiredOffset"); |
919 | 6 | } |
920 | | |
921 | | RangeCacheFileReader::RangeCacheFileReader(RuntimeProfile* profile, io::FileReaderSPtr inner_reader, |
922 | | std::shared_ptr<RangeFinder> range_finder) |
923 | 62 | : _profile(profile), |
924 | 62 | _inner_reader(std::move(inner_reader)), |
925 | 62 | _range_finder(std::move(range_finder)) { |
926 | 62 | _size = _inner_reader->size(); |
927 | 62 | uint64_t max_cache_size = |
928 | 62 | std::max((uint64_t)4096, (uint64_t)_range_finder->get_max_range_size()); |
929 | 62 | _cache = OwnedSlice(max_cache_size); |
930 | | |
931 | 62 | if (_profile != nullptr) { |
932 | 46 | const char* random_profile = "RangeCacheFileReader"; |
933 | 46 | _total_time = ADD_CHILD_TIMER_WITH_LEVEL( |
934 | 46 | _profile, random_profile, |
935 | 46 | file_scan_profile::parent_or_root(_profile, file_scan_profile::IO), 1); |
936 | 46 | _request_io = |
937 | 46 | ADD_CHILD_COUNTER_WITH_LEVEL(_profile, "RequestIO", TUnit::UNIT, random_profile, 1); |
938 | 46 | _request_bytes = ADD_CHILD_COUNTER_WITH_LEVEL(_profile, "RequestBytes", TUnit::BYTES, |
939 | 46 | random_profile, 1); |
940 | 46 | _request_time = ADD_CHILD_TIMER_WITH_LEVEL(_profile, "RequestTime", random_profile, 1); |
941 | 46 | _read_to_cache_time = |
942 | 46 | ADD_CHILD_TIMER_WITH_LEVEL(_profile, "ReadToCacheTime", random_profile, 1); |
943 | 46 | _cache_refresh_count = ADD_CHILD_COUNTER_WITH_LEVEL(_profile, "CacheRefreshCount", |
944 | 46 | TUnit::UNIT, random_profile, 1); |
945 | 46 | _read_to_cache_bytes = ADD_CHILD_COUNTER_WITH_LEVEL(_profile, "ReadToCacheBytes", |
946 | 46 | TUnit::BYTES, random_profile, 1); |
947 | 46 | } |
948 | 62 | } |
949 | | |
950 | | Status RangeCacheFileReader::read_at_impl(size_t offset, Slice result, size_t* bytes_read, |
951 | 6 | const IOContext* io_ctx) { |
952 | 6 | auto request_size = result.size; |
953 | | |
954 | 6 | _cache_statistics.request_io++; |
955 | 6 | _cache_statistics.request_bytes += request_size; |
956 | 6 | SCOPED_RAW_TIMER(&_cache_statistics.request_time); |
957 | | |
958 | 6 | PrefetchRange range; |
959 | 6 | if (_range_finder->get_range_for(offset, range)) [[likely]] { |
960 | 6 | if (_current_start_offset != range.start_offset) { // need read new range to cache. |
961 | 6 | auto range_size = range.end_offset - range.start_offset; |
962 | | |
963 | 6 | _cache_statistics.cache_refresh_count++; |
964 | 6 | _cache_statistics.read_to_cache_bytes += range_size; |
965 | 6 | SCOPED_RAW_TIMER(&_cache_statistics.read_to_cache_time); |
966 | | |
967 | 6 | Slice cache_slice = {_cache.data(), range_size}; |
968 | 6 | RETURN_IF_ERROR( |
969 | 6 | _inner_reader->read_at(range.start_offset, cache_slice, bytes_read, io_ctx)); |
970 | | |
971 | 6 | if (*bytes_read != range_size) [[unlikely]] { |
972 | 0 | return Status::InternalError( |
973 | 0 | "RangeCacheFileReader use inner reader read bytes {} not eq expect size {}", |
974 | 0 | *bytes_read, range_size); |
975 | 0 | } |
976 | | |
977 | 6 | _current_start_offset = range.start_offset; |
978 | 6 | } |
979 | | |
980 | 6 | int64_t buffer_offset = offset - _current_start_offset; |
981 | 6 | memcpy(result.data, _cache.data() + buffer_offset, request_size); |
982 | 6 | *bytes_read = request_size; |
983 | | |
984 | 6 | return Status::OK(); |
985 | 6 | } else { |
986 | 0 | return Status::InternalError("RangeCacheFileReader read not in Ranges. Offset = {}", |
987 | 0 | offset); |
988 | | // RETURN_IF_ERROR(_inner_reader->read_at(offset, result , bytes_read, io_ctx)); |
989 | | // return Status::OK(); |
990 | | // think return error is ok,otherwise it will cover up the error. |
991 | 0 | } |
992 | 6 | } |
993 | | |
994 | 59 | void RangeCacheFileReader::_collect_profile_before_close() { |
995 | 59 | if (_profile != nullptr) { |
996 | 46 | COUNTER_UPDATE(_total_time, _cache_statistics.request_time); |
997 | 46 | COUNTER_UPDATE(_request_io, _cache_statistics.request_io); |
998 | 46 | COUNTER_UPDATE(_request_bytes, _cache_statistics.request_bytes); |
999 | 46 | COUNTER_UPDATE(_request_time, _cache_statistics.request_time); |
1000 | 46 | COUNTER_UPDATE(_read_to_cache_time, _cache_statistics.read_to_cache_time); |
1001 | 46 | COUNTER_UPDATE(_cache_refresh_count, _cache_statistics.cache_refresh_count); |
1002 | 46 | COUNTER_UPDATE(_read_to_cache_bytes, _cache_statistics.read_to_cache_bytes); |
1003 | 46 | if (_inner_reader != nullptr) { |
1004 | 46 | _inner_reader->collect_profile_before_close(); |
1005 | 46 | } |
1006 | 46 | } |
1007 | 59 | } |
1008 | | |
1009 | | } // namespace io |
1010 | | |
1011 | | } // namespace doris |