be/src/io/cache/cache_lru_dumper.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 <gen_cpp/file_cache.pb.h> |
21 | | |
22 | | #include <chrono> |
23 | | #include <cstdint> |
24 | | #include <cstring> |
25 | | #include <ctime> |
26 | | #include <filesystem> |
27 | | #include <fstream> |
28 | | #include <iostream> |
29 | | #include <mutex> |
30 | | #include <sstream> |
31 | | #include <string> |
32 | | #include <tuple> |
33 | | #include <vector> |
34 | | |
35 | | #include "io/cache/file_cache_common.h" |
36 | | |
37 | | namespace doris::io { |
38 | | class LRUQueue; |
39 | | class LRUQueueRecorder; |
40 | | |
41 | | class CacheLRUDumper { |
42 | | public: |
43 | | CacheLRUDumper(BlockFileCache* mgr, LRUQueueRecorder* recorder) |
44 | 167 | : _mgr(mgr), _recorder(recorder) { |
45 | 167 | auto now = std::chrono::system_clock::now(); |
46 | 167 | auto in_time_t = std::chrono::system_clock::to_time_t(now); |
47 | 167 | std::stringstream ss; |
48 | 167 | ss << std::put_time(std::localtime(&in_time_t), "%Y%m%d%H%M%S"); |
49 | 167 | _start_time = ss.str(); |
50 | 167 | }; |
51 | | |
52 | | void dump_queue(const std::string& queue_name, bool force); |
53 | | void restore_queue(LRUQueue& queue, const std::string& queue_name, |
54 | | std::lock_guard<std::mutex>& cache_lock); |
55 | | void remove_lru_dump_files(); |
56 | 1.64k | void set_first_dump_done() { _is_first_dump = false; } |
57 | | |
58 | | private: |
59 | | void do_dump_queue(LRUQueue& queue, const std::string& queue_name); |
60 | | Status check_ofstream_status(std::ofstream& out, std::string& filename); |
61 | | Status check_ifstream_status(std::ifstream& in, std::string& filename); |
62 | | Status dump_one_lru_entry(std::ofstream& out, std::string& filename, const UInt128Wrapper& hash, |
63 | | size_t offset, size_t size); |
64 | | Status finalize_dump(std::ofstream& out, size_t entry_num, std::string& tmp_filename, |
65 | | std::string& final_filename, size_t& file_size); |
66 | | Status parse_dump_footer(std::ifstream& in, std::string& filename, size_t& entry_num); |
67 | | Status parse_one_lru_entry(std::ifstream& in, std::string& filename, UInt128Wrapper& hash, |
68 | | size_t& offset, size_t& size); |
69 | | Status flush_current_group(std::ofstream& out, std::string& filename); |
70 | | |
71 | | struct Footer { |
72 | | size_t meta_offset; |
73 | | uint32_t checksum; |
74 | | uint8_t version; |
75 | | char magic[3]; |
76 | | |
77 | | std::string serialize_as_string() const; |
78 | | bool deserialize_from_string(const std::string& data); |
79 | | } __attribute__((packed)); |
80 | | |
81 | | private: |
82 | | // For dumping |
83 | | doris::io::cache::LRUDumpEntryGroupPb _current_dump_group; |
84 | | doris::io::cache::LRUDumpMetaPb _dump_meta; |
85 | | size_t _current_dump_group_count = 0; |
86 | | |
87 | | // For parsing |
88 | | doris::io::cache::LRUDumpEntryGroupPb _current_parse_group; |
89 | | doris::io::cache::LRUDumpMetaPb _parse_meta; |
90 | | |
91 | | BlockFileCache* _mgr; |
92 | | LRUQueueRecorder* _recorder; |
93 | | |
94 | | std::string _start_time; |
95 | | bool _is_first_dump = true; |
96 | | }; |
97 | | } // namespace doris::io |