Coverage Report

Created: 2026-04-03 05:40

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