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