be/src/io/fs/file_handle_cache.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 | | // This file is copied from |
19 | | // https://github.com/apache/impala/blob/master/be/src/runtime/io/handle-cache.h |
20 | | // and modified by Doris |
21 | | |
22 | | #pragma once |
23 | | |
24 | | #include <array> |
25 | | #include <cstdint> |
26 | | #include <list> |
27 | | #include <map> |
28 | | #include <memory> |
29 | | #include <string> |
30 | | #include <utility> |
31 | | |
32 | | #include "common/status.h" |
33 | | #include "io/fs/file_system.h" |
34 | | #include "io/fs/hdfs.h" |
35 | | #include "util/aligned_new.h" |
36 | | #include "util/lru_multi_cache.inline.h" |
37 | | #include "util/thread.h" |
38 | | |
39 | | namespace doris::io { |
40 | | |
41 | | /// This abstract class is a small wrapper around the hdfsFile handle and the file system |
42 | | /// instance which is needed to close the file handle. The handle incorporates |
43 | | /// the last modified time of the file when it was opened. This is used to distinguish |
44 | | /// between file handles for files that can be updated or overwritten. |
45 | | /// This is used only through its subclasses, CachedHdfsFileHandle and |
46 | | /// ExclusiveHdfsFileHandle. |
47 | | class HdfsFileHandle { |
48 | | public: |
49 | | /// Destructor will close the file handle |
50 | | ~HdfsFileHandle(); |
51 | | |
52 | | /// Init opens the file handle |
53 | | Status init(int64_t file_size); |
54 | | |
55 | 0 | hdfsFS fs() const { return _fs; } |
56 | 0 | hdfsFile file() const { return _hdfs_file; } |
57 | 0 | int64_t mtime() const { return _mtime; } |
58 | 0 | int64_t file_size() const { return _file_size; } |
59 | | |
60 | | protected: |
61 | | HdfsFileHandle(const hdfsFS& fs, const std::string& fname, int64_t mtime) |
62 | 0 | : _fs(fs), _fname(fname), _mtime(mtime) {} |
63 | | |
64 | | private: |
65 | | hdfsFS _fs; |
66 | | const std::string _fname; |
67 | | hdfsFile _hdfs_file = nullptr; |
68 | | int64_t _mtime; |
69 | | int64_t _file_size; |
70 | | }; |
71 | | |
72 | | /// CachedHdfsFileHandles are owned by the file handle cache and are used for no |
73 | | /// other purpose. |
74 | | class CachedHdfsFileHandle : public HdfsFileHandle { |
75 | | public: |
76 | | CachedHdfsFileHandle(const hdfsFS& fs, const std::string& fname, int64_t mtime); |
77 | | ~CachedHdfsFileHandle(); |
78 | | }; |
79 | | |
80 | | /// ExclusiveHdfsFileHandles are used for all purposes where a CachedHdfsFileHandle |
81 | | /// is not appropriate. |
82 | | class ExclusiveHdfsFileHandle : public HdfsFileHandle { |
83 | | public: |
84 | | ExclusiveHdfsFileHandle(const hdfsFS& fs, const std::string& fname, int64_t mtime) |
85 | 0 | : HdfsFileHandle(fs, fname, mtime) {} |
86 | | }; |
87 | | |
88 | | /// The FileHandleCache is a data structure that owns HdfsFileHandles to share between |
89 | | /// threads. The HdfsFileHandles are hash partitioned across NUM_PARTITIONS partitions. |
90 | | /// Each partition operates independently with its own locks, reducing contention |
91 | | /// between concurrent threads. The `capacity` is split between the partitions and is |
92 | | /// enforced independently. |
93 | | /// |
94 | | /// Threads check out a file handle for exclusive access, released automatically by RAII |
95 | | /// accessor. If the file handle is not already present in the cache or all file handles |
96 | | /// for this file are checked out, the file handle is emplaced in the cache. The cache can |
97 | | /// contain multiple file handles for the same file. If a file handle is checked out, it |
98 | | /// cannot be evicted from the cache. In this case, a cache can exceed the specified |
99 | | /// capacity. |
100 | | /// |
101 | | /// Remote file systems could keep a connection as part of the file handle without support |
102 | | /// for unbuffering. The file handle cache is not suitable for those systems, as the cache |
103 | | /// size can exceed the limit on the number of concurrent connections. HDFS does not |
104 | | /// maintain a connection in the file handle, S3A client supports unbuffering since |
105 | | /// IMPALA-8428, so those do not have this restriction. |
106 | | /// |
107 | | /// If there is a file handle in the cache and the underlying file is deleted, |
108 | | /// the file handle might keep the file from being deleted at the OS level. This can |
109 | | /// take up disk space and impact correctness. To avoid this, the cache will evict any |
110 | | /// file handle that has been unused for longer than threshold specified by |
111 | | /// `unused_handle_timeout_secs`. Eviction is disabled when the threshold is 0. |
112 | | /// |
113 | | /// TODO: The cache should also evict file handles more aggressively if the file handle's |
114 | | /// mtime is older than the file's current mtime. |
115 | | class FileHandleCache { |
116 | | private: |
117 | | using CacheKey = std::pair<hdfsFS, std::pair<std::string, int64_t>>; |
118 | | |
119 | | /// Each partition operates independently, and thus has its own thread-safe cache. |
120 | | /// To avoid contention on the lock_ due to false sharing the partitions are |
121 | | /// aligned to cache line boundaries. |
122 | | struct FileHandleCachePartition : public CacheLineAligned { |
123 | | // The same HDFS path can be opened through different hdfsFS instances with |
124 | | // different authentication contexts, so the filesystem handle is part of the key. |
125 | | typedef LruMultiCache<CacheKey, CachedHdfsFileHandle> CacheType; |
126 | | CacheType cache; |
127 | | }; |
128 | | |
129 | | public: |
130 | | /// RAII accessor built over LruMultiCache::Accessor to handle metrics and unbuffering. |
131 | | /// Composition is used instead of inheritance to support the usage as in/out parameter |
132 | | class Accessor { |
133 | | public: |
134 | | Accessor(); |
135 | | Accessor(FileHandleCachePartition::CacheType::Accessor&& cache_accessor); |
136 | 0 | Accessor(Accessor&&) = default; |
137 | | Accessor& operator=(Accessor&&) = default; |
138 | | |
139 | | DISALLOW_COPY_AND_ASSIGN(Accessor); |
140 | | |
141 | | /// Handles metrics and unbuffering |
142 | | ~Accessor(); |
143 | | |
144 | | /// Set function can be used if the Accessor is used as in/out parameter. |
145 | | void set(FileHandleCachePartition::CacheType::Accessor&& cache_accessor); |
146 | | |
147 | | /// Interface mimics LruMultiCache::Accessor's interface, handles metrics |
148 | | CachedHdfsFileHandle* get(); |
149 | | void release(); |
150 | | void destroy(); |
151 | | |
152 | | private: |
153 | | FileHandleCachePartition::CacheType::Accessor _cache_accessor; |
154 | | }; |
155 | | |
156 | | /// Instantiates the cache with `capacity` split evenly across NUM_PARTITIONS |
157 | | /// partitions. If the capacity does not split evenly, then the capacity is rounded |
158 | | /// up. The cache will age out any file handle that is unused for |
159 | | /// `unused_handle_timeout_secs` seconds. Age out is disabled if this is set to zero. |
160 | | FileHandleCache(size_t capacity, size_t num_partitions, uint64_t unused_handle_timeout_secs); |
161 | | |
162 | | /// Destructor is only called for backend tests |
163 | | ~FileHandleCache(); |
164 | | |
165 | | /// Starts up a thread that monitors the age of file handles and evicts any that |
166 | | /// exceed the limit. |
167 | | Status init() WARN_UNUSED_RESULT; |
168 | | |
169 | | /// Get a file handle accessor from the cache for the specified filename (fname) and |
170 | | /// last modification time (mtime). This will hash the filename to determine |
171 | | /// which partition to use for this file handle. |
172 | | /// |
173 | | /// If 'require_new_handle' is false and the partition contains an available handle, |
174 | | /// an accessor is returned and cache_hit is set to true. Otherwise, the partition will |
175 | | /// emplace file handle, an accessor to it will be returned with cache_hit set to false. |
176 | | /// On failure, empty accessor will be returned. In either case, the partition may evict |
177 | | /// a file handle to make room for the new file handle. |
178 | | /// |
179 | | /// This obtains exclusive control over the returned file handle. |
180 | | Status get_file_handle(const hdfsFS& fs, const std::string& fname, int64_t mtime, |
181 | | int64_t file_size, bool require_new_handle, Accessor* accessor, |
182 | | bool* cache_hit) WARN_UNUSED_RESULT; |
183 | | |
184 | | #ifdef BE_TEST |
185 | | static bool same_cache_key_for_test(const hdfsFS& lhs_fs, const std::string& lhs_fname, |
186 | | int64_t lhs_mtime, const hdfsFS& rhs_fs, |
187 | | const std::string& rhs_fname, int64_t rhs_mtime); |
188 | | #endif |
189 | | |
190 | | private: |
191 | 8 | static CacheKey make_cache_key(const hdfsFS& fs, const std::string& fname, int64_t mtime) { |
192 | 8 | return {fs, {fname, mtime}}; |
193 | 8 | } |
194 | | |
195 | | /// Periodic check to evict unused file handles. Only executed by _eviction_thread. |
196 | | void _evict_handles_loop(); |
197 | | |
198 | | std::vector<FileHandleCachePartition> _cache_partitions; |
199 | | |
200 | | /// Maximum time before an unused file handle is aged out of the cache. |
201 | | /// Aging out is disabled if this is set to 0. |
202 | | uint64_t _unused_handle_timeout_secs; |
203 | | |
204 | | /// Thread to check for unused file handles to evict. This thread will exit when |
205 | | /// the _shut_down_promise is set. |
206 | | std::shared_ptr<Thread> _eviction_thread; |
207 | | std::atomic<bool> _is_shut_down = {false}; |
208 | | }; |
209 | | |
210 | | } // namespace doris::io |