Coverage Report

Created: 2026-06-02 20:13

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
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
    using LruDumpEntry = std::tuple<UInt128Wrapper, size_t, size_t>;
44
45
    CacheLRUDumper(BlockFileCache* mgr, LRUQueueRecorder* recorder)
46
182
            : _mgr(mgr), _recorder(recorder) {
47
182
        auto now = std::chrono::system_clock::now();
48
182
        auto in_time_t = std::chrono::system_clock::to_time_t(now);
49
182
        std::stringstream ss;
50
182
        ss << std::put_time(std::localtime(&in_time_t), "%Y%m%d%H%M%S");
51
182
        _start_time = ss.str();
52
182
    };
53
54
    void dump_queue(const std::string& queue_name, bool force);
55
    void restore_queue(LRUQueue& queue, const std::string& queue_name,
56
                       std::lock_guard<std::mutex>& cache_lock);
57
    void remove_lru_dump_files();
58
1.31k
    void set_first_dump_done() { _is_first_dump = false; }
59
60
private:
61
    Status do_dump_queue(LRUQueue& queue, const std::string& queue_name);
62
    Status do_dump_queue(const std::vector<LruDumpEntry>& elements, const std::string& queue_name);
63
    std::vector<LruDumpEntry> collect_lru_queue_entries_locked(
64
            LRUQueue& queue, std::lock_guard<std::mutex>& lru_log_lock);
65
    Status check_ofstream_status(std::ofstream& out, std::string& filename);
66
    Status check_ifstream_status(std::ifstream& in, std::string& filename);
67
    Status dump_one_lru_entry(std::ofstream& out, std::string& filename, const UInt128Wrapper& hash,
68
                              size_t offset, size_t size);
69
    Status finalize_dump(std::ofstream& out, size_t entry_num, std::string& tmp_filename,
70
                         std::string& final_filename, size_t& file_size);
71
    Status parse_dump_footer(std::ifstream& in, std::string& filename, size_t& entry_num);
72
    Status parse_one_lru_entry(std::ifstream& in, std::string& filename, UInt128Wrapper& hash,
73
                               size_t& offset, size_t& size);
74
    Status flush_current_group(std::ofstream& out, std::string& filename);
75
76
    struct Footer {
77
        size_t meta_offset;
78
        uint32_t checksum;
79
        uint8_t version;
80
        char magic[3];
81
82
        std::string serialize_as_string() const;
83
        bool deserialize_from_string(const std::string& data);
84
    } __attribute__((packed));
85
86
private:
87
    // For dumping
88
    doris::io::cache::LRUDumpEntryGroupPb _current_dump_group;
89
    doris::io::cache::LRUDumpMetaPb _dump_meta;
90
    size_t _current_dump_group_count = 0;
91
92
    // For parsing
93
    doris::io::cache::LRUDumpEntryGroupPb _current_parse_group;
94
    doris::io::cache::LRUDumpMetaPb _parse_meta;
95
96
    BlockFileCache* _mgr;
97
    LRUQueueRecorder* _recorder;
98
99
    std::string _start_time;
100
    bool _is_first_dump = true;
101
};
102
} // namespace doris::io