Coverage Report

Created: 2026-03-12 17:15

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/runtime/cache/result_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
#pragma once
19
20
#include <cstdio>
21
#include <shared_mutex>
22
#include <unordered_map>
23
24
#include "runtime/cache/result_node.h"
25
#include "util/uid_util.h"
26
27
namespace doris {
28
class PCacheResponse;
29
class PClearCacheRequest;
30
class PFetchCacheRequest;
31
class PFetchCacheResult;
32
class PUpdateCacheRequest;
33
34
typedef std::unordered_map<UniqueId, ResultNode*> ResultNodeMap;
35
36
// a doubly linked list class, point to result node
37
class ResultNodeList {
38
public:
39
19
    ResultNodeList() : _head(nullptr), _tail(nullptr), _node_count(0) {}
40
16
    virtual ~ResultNodeList() {}
41
42
136
    ResultNode* new_node(const UniqueId& sql_key) { return new ResultNode(sql_key); }
43
44
    void delete_node(ResultNode** node);
45
46
    ResultNode* pop();
47
    void move_tail(ResultNode* node);
48
    //Just remove node from link, do not delete node
49
    void remove(ResultNode* node);
50
    void push_back(ResultNode* node);
51
    void clear();
52
53
0
    ResultNode* get_head() const { return _head; }
54
55
0
    ResultNode* get_tail() const { return _tail; }
56
57
14
    size_t get_node_count() const { return _node_count; }
58
59
private:
60
    ResultNode* _head = nullptr;
61
    ResultNode* _tail = nullptr;
62
    size_t _node_count;
63
};
64
65
/**
66
 * Cache results of query, including the entire result set or the result set of divided partitions.
67
 * Two data structures, one is unordered_map and the other is a doubly linked list, corresponding to a result node.
68
 * If the cache is hit, the node will be moved to the end of the linked list.
69
 * If the cache is cleared, nodes that are expired or have not been accessed for a long time will be cleared.
70
 */
71
class ResultCache {
72
public:
73
19
    ResultCache(int32_t max_size, int32_t elasticity_size) {
74
19
        _max_size = static_cast<size_t>(max_size) * 1024 * 1024;
75
19
        _elasticity_size = static_cast<size_t>(elasticity_size) * 1024 * 1024;
76
19
        _cache_size = 0;
77
19
        _node_count = 0;
78
19
        _partition_count = 0;
79
19
    }
80
81
16
    virtual ~ResultCache() {
82
16
        _node_list.clear();
83
16
        _node_map.clear();
84
16
    }
85
86
    void update(const PUpdateCacheRequest* request, PCacheResponse* response);
87
    void fetch(const PFetchCacheRequest* request, PFetchCacheResult* result);
88
    bool contains(const UniqueId& sql_key);
89
    void clear(const PClearCacheRequest* request, PCacheResponse* response);
90
91
    size_t get_cache_size() { return _cache_size; }
92
93
private:
94
    void prune();
95
    void remove(ResultNode* result_node);
96
    void update_monitor();
97
98
    //At the same time, multithreaded reading
99
    //Single thread updating and cleaning(only single be, Fe is not affected)
100
    mutable std::shared_mutex _cache_mtx;
101
    ResultNodeMap _node_map;
102
    //List of result nodes corresponding to SqlKey,last recently used at the tail
103
    ResultNodeList _node_list;
104
    size_t _cache_size;
105
    size_t _max_size;
106
    double _elasticity_size;
107
    size_t _node_count;
108
    size_t _partition_count;
109
110
private:
111
    ResultCache();
112
    ResultCache(const ResultCache&);
113
    const ResultCache& operator=(const ResultCache&);
114
};
115
116
} // namespace doris