be/src/io/cache/cached_remote_file_reader.h
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 | | #pragma once |
19 | | |
20 | | #include <cstddef> |
21 | | #include <cstdint> |
22 | | #include <map> |
23 | | #include <shared_mutex> |
24 | | #include <utility> |
25 | | #include <vector> |
26 | | |
27 | | #include "common/status.h" |
28 | | #include "io/cache/block_file_cache.h" |
29 | | #include "io/cache/file_block.h" |
30 | | #include "io/cache/file_cache_common.h" |
31 | | #include "io/fs/file_reader.h" |
32 | | #include "io/fs/file_reader_writer_fwd.h" |
33 | | #include "io/fs/path.h" |
34 | | #include "util/slice.h" |
35 | | |
36 | | namespace doris::io { |
37 | | struct IOContext; |
38 | | struct FileCacheStatistics; |
39 | | |
40 | | class CachedRemoteFileReader final : public FileReader, |
41 | | public std::enable_shared_from_this<CachedRemoteFileReader> { |
42 | | public: |
43 | | CachedRemoteFileReader(FileReaderSPtr remote_file_reader, const FileReaderOptions& opts); |
44 | | |
45 | | ~CachedRemoteFileReader() override; |
46 | | |
47 | | Status close() override; |
48 | | |
49 | 3.23k | const Path& path() const override { return _remote_file_reader->path(); } |
50 | | |
51 | 20.6k | size_t size() const override { return _remote_file_reader->size(); } |
52 | | |
53 | 9.76k | bool closed() const override { return _remote_file_reader->closed(); } |
54 | | |
55 | 1.00k | FileReader* get_remote_reader() { return _remote_file_reader.get(); } |
56 | | |
57 | | static std::pair<size_t, size_t> s_align_size(size_t offset, size_t size, size_t length); |
58 | | |
59 | 0 | int64_t mtime() const override { return _remote_file_reader->mtime(); } |
60 | | |
61 | | // Asynchronously prefetch a range of file cache blocks. |
62 | | // This method triggers read file cache in dryrun mode to warm up the cache |
63 | | // without actually reading the data into user buffers. |
64 | | // |
65 | | // Parameters: |
66 | | // offset: Starting offset in the file |
67 | | // size: Number of bytes to prefetch |
68 | | // io_ctx: IO context (can be nullptr, will create a dryrun context internally) |
69 | | // |
70 | | // Note: This is a best-effort operation. Errors are logged but not returned. |
71 | | void prefetch_range(size_t offset, size_t size, const IOContext* io_ctx = nullptr); |
72 | | |
73 | | protected: |
74 | | Status read_at_impl(size_t offset, Slice result, size_t* bytes_read, |
75 | | const IOContext* io_ctx) override; |
76 | | |
77 | | private: |
78 | | void _insert_file_reader(FileBlockSPtr file_block); |
79 | | |
80 | | // Execute remote read (S3 or peer). |
81 | | Status _execute_remote_read(const std::vector<FileBlockSPtr>& empty_blocks, size_t empty_start, |
82 | | size_t& size, std::unique_ptr<char[]>& buffer, |
83 | | ReadStatistics& stats, const IOContext* io_ctx); |
84 | | |
85 | | void _update_stats(const ReadStatistics& stats, FileCacheStatistics* state, |
86 | | bool is_inverted_index) const; |
87 | | |
88 | | bool _is_doris_table; |
89 | | FileReaderSPtr _remote_file_reader; |
90 | | UInt128Wrapper _cache_hash; |
91 | | BlockFileCache* _cache; |
92 | | std::shared_mutex _mtx; |
93 | | std::map<size_t, FileBlockSPtr> _cache_file_readers; |
94 | | }; |
95 | | |
96 | | } // namespace doris::io |