be/src/io/cache/file_cache_common.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 | | // This file is copied from |
18 | | // https://github.com/ClickHouse/ClickHouse/blob/master/src/Interpreters/Cache/FileCache_fwd.h |
19 | | // and modified by Doris |
20 | | |
21 | | #pragma once |
22 | | #include <cstdint> |
23 | | #include <vector> |
24 | | |
25 | | #include "common/config.h" |
26 | | #include "core/uint128.h" |
27 | | #include "io/io_common.h" |
28 | | |
29 | | namespace doris::io { |
30 | | |
31 | | inline static constexpr size_t REMOTE_FS_OBJECTS_CACHE_DEFAULT_ELEMENTS = 100 * 1024; |
32 | | inline static constexpr size_t FILE_CACHE_MAX_FILE_BLOCK_SIZE = 1 * 1024 * 1024; |
33 | | inline static constexpr size_t DEFAULT_NORMAL_PERCENT = 40; |
34 | | inline static constexpr size_t DEFAULT_DISPOSABLE_PERCENT = 5; |
35 | | inline static constexpr size_t DEFAULT_INDEX_PERCENT = 5; |
36 | | inline static constexpr size_t DEFAULT_TTL_PERCENT = 50; |
37 | | |
38 | | using uint128_t = UInt128; |
39 | | |
40 | | enum FileCacheType { |
41 | | INDEX = 2, |
42 | | NORMAL = 1, |
43 | | DISPOSABLE = 0, |
44 | | TTL = 3, |
45 | | }; |
46 | | |
47 | 3.90M | inline size_t file_cache_type_index(FileCacheType type) { |
48 | 3.90M | return static_cast<size_t>(type); |
49 | 3.90M | } |
50 | | |
51 | | std::string cache_type_to_surfix(FileCacheType type); |
52 | | FileCacheType surfix_to_cache_type(const std::string& str); |
53 | | |
54 | | FileCacheType string_to_cache_type(const std::string& str); |
55 | | std::string cache_type_to_string(FileCacheType type); |
56 | | |
57 | | struct UInt128Wrapper { |
58 | | uint128_t value_; |
59 | | [[nodiscard]] std::string to_string() const; |
60 | | |
61 | | UInt128Wrapper() = default; |
62 | 25.5k | explicit UInt128Wrapper(const uint128_t& value) : value_(value) {} |
63 | | |
64 | 2.62M | bool operator==(const UInt128Wrapper& other) const { return value_ == other.value_; } |
65 | | |
66 | 18.2k | uint64_t high() const { return static_cast<uint64_t>(value_ >> 64); } |
67 | 18.1k | uint64_t low() const { return static_cast<uint64_t>(value_); } |
68 | | |
69 | 0 | friend std::ostream& operator<<(std::ostream& os, const UInt128Wrapper& wrapper) { |
70 | 0 | os << "UInt128Wrapper(" << wrapper.high() << ", " << wrapper.low() << ")"; |
71 | 0 | return os; |
72 | 0 | } |
73 | | }; |
74 | | |
75 | | struct ReadStatistics { |
76 | | bool hit_cache = true; |
77 | | bool from_peer_cache = false; |
78 | | bool skip_cache = false; |
79 | | int64_t bytes_read = 0; |
80 | | int64_t bytes_read_from_local = 0; |
81 | | int64_t bytes_read_from_remote = 0; |
82 | | int64_t bytes_read_from_peer = 0; |
83 | | int64_t remote_physical_read_bytes = 0; |
84 | | int64_t bytes_write_into_file_cache = 0; |
85 | | int64_t remote_read_timer = 0; |
86 | | int64_t peer_read_timer = 0; |
87 | | int64_t remote_wait_timer = 0; // wait for other downloader |
88 | | int64_t local_read_timer = 0; |
89 | | int64_t local_write_timer = 0; |
90 | | int64_t read_cache_file_directly_timer = 0; |
91 | | int64_t cache_get_or_set_timer = 0; |
92 | | int64_t lock_wait_timer = 0; |
93 | | int64_t get_timer = 0; |
94 | | int64_t set_timer = 0; |
95 | | }; |
96 | | |
97 | | class BlockFileCache; |
98 | | struct FileBlocksHolder; |
99 | | using FileBlocksHolderPtr = std::unique_ptr<FileBlocksHolder>; |
100 | | |
101 | | struct FileCacheAllocatorBuilder { |
102 | | bool _is_cold_data; |
103 | | uint64_t _expiration_time; |
104 | | UInt128Wrapper _cache_hash; |
105 | | BlockFileCache* _cache; // Only one ref, the lifetime is owned by FileCache |
106 | | FileBlocksHolderPtr allocate_cache_holder(size_t offset, size_t size, int64_t tablet_id) const; |
107 | | }; |
108 | | |
109 | | struct KeyHash { |
110 | 2.16M | std::size_t operator()(const UInt128Wrapper& w) const { |
111 | 2.16M | return util_hash::HashLen16(w.value_.low(), w.value_.high()); |
112 | 2.16M | } |
113 | | }; |
114 | | |
115 | | using AccessKeyAndOffset = std::pair<UInt128Wrapper, size_t>; |
116 | | struct KeyAndOffsetHash { |
117 | 182 | std::size_t operator()(const AccessKeyAndOffset& key) const { |
118 | 182 | return KeyHash()(key.first) ^ std::hash<uint64_t>()(key.second); |
119 | 182 | } |
120 | | }; |
121 | | |
122 | | struct KeyMeta { |
123 | | uint64_t expiration_time; // absolute time |
124 | | FileCacheType type; |
125 | | int64_t tablet_id {0}; |
126 | | }; |
127 | | |
128 | | struct FileCacheKey { |
129 | | UInt128Wrapper hash; |
130 | | size_t offset; |
131 | | KeyMeta meta; |
132 | | }; |
133 | | |
134 | | struct FileCacheSettings { |
135 | | size_t capacity {0}; |
136 | | size_t disposable_queue_size {0}; |
137 | | size_t disposable_queue_elements {0}; |
138 | | size_t index_queue_size {0}; |
139 | | size_t index_queue_elements {0}; |
140 | | size_t query_queue_size {0}; |
141 | | size_t query_queue_elements {0}; |
142 | | size_t ttl_queue_size {0}; |
143 | | size_t ttl_queue_elements {0}; |
144 | | size_t max_file_block_size {0}; |
145 | | size_t max_query_cache_size {0}; |
146 | | std::string storage; |
147 | | |
148 | | // to string |
149 | | std::string to_string() const; |
150 | | }; |
151 | | |
152 | | FileCacheSettings get_file_cache_settings(size_t capacity, size_t max_query_cache_size, |
153 | | size_t normal_percent = DEFAULT_NORMAL_PERCENT, |
154 | | size_t disposable_percent = DEFAULT_DISPOSABLE_PERCENT, |
155 | | size_t index_percent = DEFAULT_INDEX_PERCENT, |
156 | | size_t ttl_percent = DEFAULT_TTL_PERCENT, |
157 | | const std::string& storage = "disk"); |
158 | | |
159 | | struct CacheContext { |
160 | 717k | CacheContext(const IOContext* io_context) { |
161 | 717k | if (io_context->expiration_time != 0) { |
162 | 2 | cache_type = FileCacheType::TTL; |
163 | 2 | expiration_time = io_context->expiration_time; |
164 | 717k | } else if (io_context->is_index_data) { |
165 | 7 | cache_type = FileCacheType::INDEX; |
166 | 717k | } else if (io_context->is_disposable) { |
167 | 1 | cache_type = FileCacheType::DISPOSABLE; |
168 | 717k | } else { |
169 | 717k | cache_type = FileCacheType::NORMAL; |
170 | 717k | } |
171 | 717k | query_id = io_context->query_id ? *io_context->query_id : TUniqueId(); |
172 | 717k | is_warmup = io_context->is_warmup; |
173 | 717k | remote_scan_cache_write_limiter = io_context->remote_scan_cache_write_limiter; |
174 | 717k | admit_cache_write_by_remote_scan_limiter = |
175 | 717k | remote_scan_cache_write_limiter != nullptr && |
176 | 717k | io_context->reader_type == ReaderType::READER_QUERY && |
177 | 717k | (!io_context->is_index_data || io_context->is_inverted_index || |
178 | 9 | config::enable_file_cache_query_limit_segment_meta) && |
179 | 717k | !io_context->is_warmup; |
180 | 717k | } |
181 | 1.60k | CacheContext() = default; |
182 | 4 | bool operator==(const CacheContext& rhs) const { |
183 | 4 | return query_id == rhs.query_id && cache_type == rhs.cache_type && |
184 | 4 | expiration_time == rhs.expiration_time && is_cold_data == rhs.is_cold_data; |
185 | 4 | } |
186 | | TUniqueId query_id {}; |
187 | | FileCacheType cache_type {FileCacheType::NORMAL}; |
188 | | int64_t expiration_time {0}; |
189 | | bool is_cold_data {false}; |
190 | | ReadStatistics* stats {nullptr}; |
191 | | bool is_warmup {false}; |
192 | | int64_t tablet_id {0}; |
193 | | RemoteScanCacheWriteLimiter* remote_scan_cache_write_limiter = nullptr; |
194 | | bool admit_cache_write_by_remote_scan_limiter {false}; |
195 | | }; |
196 | | |
197 | | template <class Lock> |
198 | | concept IsXLock = std::same_as<Lock, std::lock_guard<std::mutex>> || |
199 | | std::same_as<Lock, std::unique_lock<std::mutex>>; |
200 | | |
201 | | class LRUQueue { |
202 | | public: |
203 | 2.20k | LRUQueue() = default; |
204 | | LRUQueue(size_t max_size, size_t max_element_size, int64_t hot_data_interval) |
205 | 1.07k | : max_size(max_size), |
206 | 1.07k | max_element_size(max_element_size), |
207 | 1.07k | hot_data_interval(hot_data_interval) {} |
208 | | |
209 | | struct HashFileKeyAndOffset { |
210 | 1.08M | std::size_t operator()(const std::pair<UInt128Wrapper, size_t>& pair) const { |
211 | 1.08M | return KeyHash()(pair.first) + pair.second; |
212 | 1.08M | } |
213 | | }; |
214 | | |
215 | | struct FileKeyAndOffset { |
216 | | UInt128Wrapper hash; |
217 | | size_t offset; |
218 | | size_t size; |
219 | | |
220 | | FileKeyAndOffset(const UInt128Wrapper& hash, size_t offset, size_t size) |
221 | 237k | : hash(hash), offset(offset), size(size) {} |
222 | | }; |
223 | | |
224 | | using Iterator = typename std::list<FileKeyAndOffset>::iterator; |
225 | | |
226 | 3.28k | size_t get_max_size() const { return max_size; } |
227 | 704 | size_t get_max_element_size() const { return max_element_size; } |
228 | | |
229 | | template <class T> |
230 | | requires IsXLock<T> |
231 | 2.71k | size_t get_capacity(T& /* cache_lock */) const { |
232 | 2.71k | return cache_size; |
233 | 2.71k | } |
234 | | |
235 | 705 | size_t get_capacity_unsafe() const { return cache_size; } |
236 | | |
237 | 738 | size_t get_elements_num_unsafe() const { return queue.size(); } |
238 | | |
239 | 236k | size_t get_elements_num(std::lock_guard<std::mutex>& /* cache_lock */) const { |
240 | 236k | return queue.size(); |
241 | 236k | } |
242 | | |
243 | | Iterator add(const UInt128Wrapper& hash, size_t offset, size_t size, |
244 | | std::lock_guard<std::mutex>& cache_lock); |
245 | | template <class T> |
246 | | requires IsXLock<T> |
247 | 216k | void remove(Iterator queue_it, T& /* cache_lock */) { |
248 | 216k | cache_size -= queue_it->size; |
249 | 216k | map.erase(std::make_pair(queue_it->hash, queue_it->offset)); |
250 | 216k | queue.erase(queue_it); |
251 | 216k | } |
252 | | |
253 | | void move_to_end(Iterator queue_it, std::lock_guard<std::mutex>& cache_lock); |
254 | | |
255 | | void resize(Iterator queue_it, size_t new_size, std::lock_guard<std::mutex>& cache_lock); |
256 | | |
257 | | std::string to_string(std::lock_guard<std::mutex>& cache_lock) const; |
258 | | |
259 | | bool contains(const UInt128Wrapper& hash, size_t offset, |
260 | | std::lock_guard<std::mutex>& cache_lock) const; |
261 | | |
262 | 32.6k | Iterator begin() { return queue.begin(); } |
263 | | |
264 | 32.6k | Iterator end() { return queue.end(); } |
265 | | |
266 | | void remove_all(std::lock_guard<std::mutex>& cache_lock); |
267 | | |
268 | | bool pop_front(std::lock_guard<std::mutex>& cache_lock); |
269 | | |
270 | | Iterator get(const UInt128Wrapper& hash, size_t offset, |
271 | | std::lock_guard<std::mutex>& /* cache_lock */) const; |
272 | | |
273 | 743 | int64_t get_hot_data_interval() const { return hot_data_interval; } |
274 | | |
275 | 0 | void clear(std::lock_guard<std::mutex>& cache_lock) { |
276 | 0 | queue.clear(); |
277 | 0 | map.clear(); |
278 | 0 | cache_size = 0; |
279 | 0 | } |
280 | | |
281 | | size_t levenshtein_distance_from(LRUQueue& base, std::lock_guard<std::mutex>& cache_lock); |
282 | | |
283 | | size_t max_size; |
284 | | size_t max_element_size; |
285 | | std::list<FileKeyAndOffset> queue; |
286 | | std::unordered_map<std::pair<UInt128Wrapper, size_t>, Iterator, HashFileKeyAndOffset> map; |
287 | | size_t cache_size = 0; |
288 | | int64_t hot_data_interval {0}; |
289 | | }; |
290 | | struct FileCacheInfo { |
291 | | UInt128Wrapper hash {0}; |
292 | | uint64_t expiration_time {0}; |
293 | | uint64_t size {0}; |
294 | | size_t offset {0}; |
295 | | bool is_tmp {false}; |
296 | | FileCacheType cache_type {NORMAL}; |
297 | | |
298 | | std::string to_string() const; |
299 | | }; |
300 | | |
301 | | class InconsistencyType { |
302 | | uint32_t type; |
303 | | |
304 | | public: |
305 | | enum : uint32_t { |
306 | | // No anomaly |
307 | | NONE = 0, |
308 | | // Missing a block cache metadata in _files |
309 | | NOT_LOADED = 1 << 0, |
310 | | // A block cache is missing in storage |
311 | | MISSING_IN_STORAGE = 1 << 1, |
312 | | // Size of a block cache recorded in _files is inconsistent with the storage |
313 | | SIZE_INCONSISTENT = 1 << 2, |
314 | | // Cache type of a block cache recorded in _files is inconsistent with the storage |
315 | | CACHE_TYPE_INCONSISTENT = 1 << 3, |
316 | | // Expiration time of a block cache recorded in _files is inconsistent with the storage |
317 | | EXPIRATION_TIME_INCONSISTENT = 1 << 4, |
318 | | // File in storage has a _tmp suffix, but the state of block cache in _files is not set to downloading |
319 | | TMP_FILE_EXPECT_DOWNLOADING_STATE = 1 << 5 |
320 | | }; |
321 | 0 | InconsistencyType(uint32_t t = 0) : type(t) {} |
322 | 0 | operator uint32_t&() { return type; } |
323 | | |
324 | | std::string to_string() const; |
325 | | }; |
326 | | |
327 | | struct InconsistencyContext { |
328 | | // The infos in _files of BlockFileCache. |
329 | | std::vector<FileCacheInfo> infos_in_manager; |
330 | | std::vector<FileCacheInfo> infos_in_storage; |
331 | | std::vector<InconsistencyType> types; |
332 | | }; |
333 | | |
334 | | std::optional<int64_t> get_tablet_id(std::string file_path); |
335 | | |
336 | | } // namespace doris::io |