Coverage Report

Created: 2026-03-12 16:03

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/runtime/memory/cache_policy.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/olap_file.pb.h>
21
22
#include <vector>
23
24
#include "runtime/runtime_profile.h"
25
26
namespace doris {
27
#include "common/compile_check_begin.h"
28
29
static constexpr int32_t CACHE_MIN_PRUNE_SIZE = 67108864; // 64M
30
static constexpr int32_t CACHE_MIN_PRUNE_NUMBER = 1024;
31
32
// Base of all caches. register to CacheManager when cache is constructed.
33
class CachePolicy {
34
public:
35
    enum class CacheType {
36
        DATA_PAGE_CACHE = 0,
37
        INDEXPAGE_CACHE = 1,
38
        PK_INDEX_PAGE_CACHE = 2,
39
        SCHEMA_CACHE = 3,
40
        SEGMENT_CACHE = 4,
41
        INVERTEDINDEX_SEARCHER_CACHE = 5,
42
        INVERTEDINDEX_QUERY_CACHE = 6,
43
        LOOKUP_CONNECTION_CACHE = 7,
44
        POINT_QUERY_ROW_CACHE = 8,
45
        DELETE_BITMAP_AGG_CACHE = 9,
46
        TABLET_VERSION_CACHE = 10,
47
        LOAD_STATE_CHANNEL_CACHE = 11,
48
        COMMON_OBJ_LRU_CACHE = 12,
49
        FOR_UT_CACHE_SIZE = 13,
50
        TABLET_SCHEMA_CACHE = 14,
51
        CREATE_TABLET_RR_IDX_CACHE = 15,
52
        CLOUD_TABLET_CACHE = 16,
53
        CLOUD_TXN_DELETE_BITMAP_CACHE = 17,
54
        NONE = 18, // not be used
55
        FOR_UT_CACHE_NUMBER = 19,
56
        QUERY_CACHE = 20,
57
        TABLET_COLUMN_OBJECT_POOL = 21,
58
        SCHEMA_CLOUD_DICTIONARY_CACHE = 22,
59
        CONDITION_CACHE = 23,
60
    };
61
62
8.09k
    static std::string type_string(CacheType type) {
63
8.09k
        switch (type) {
64
179
        case CacheType::DATA_PAGE_CACHE:
65
179
            return "DataPageCache";
66
180
        case CacheType::INDEXPAGE_CACHE:
67
180
            return "IndexPageCache";
68
180
        case CacheType::PK_INDEX_PAGE_CACHE:
69
180
            return "PKIndexPageCache";
70
134
        case CacheType::SCHEMA_CACHE:
71
134
            return "SchemaCache";
72
43
        case CacheType::SEGMENT_CACHE:
73
43
            return "SegmentCache";
74
854
        case CacheType::INVERTEDINDEX_SEARCHER_CACHE:
75
854
            return "InvertedIndexSearcherCache";
76
464
        case CacheType::INVERTEDINDEX_QUERY_CACHE:
77
464
            return "InvertedIndexQueryCache";
78
194
        case CacheType::LOOKUP_CONNECTION_CACHE:
79
194
            return "PointQueryLookupConnectionCache";
80
149
        case CacheType::POINT_QUERY_ROW_CACHE:
81
149
            return "PointQueryRowCache";
82
139
        case CacheType::DELETE_BITMAP_AGG_CACHE:
83
139
            return "MowDeleteBitmapAggCache";
84
1.78k
        case CacheType::TABLET_VERSION_CACHE:
85
1.78k
            return "MowTabletVersionCache";
86
38
        case CacheType::LOAD_STATE_CHANNEL_CACHE:
87
38
            return "LoadStateChannelCache ";
88
345
        case CacheType::COMMON_OBJ_LRU_CACHE:
89
345
            return "CommonObjLRUCache";
90
40
        case CacheType::FOR_UT_CACHE_SIZE:
91
40
            return "ForUTCacheSize";
92
233
        case CacheType::TABLET_SCHEMA_CACHE:
93
233
            return "TabletSchemaCache";
94
1.80k
        case CacheType::CREATE_TABLET_RR_IDX_CACHE:
95
1.80k
            return "CreateTabletRRIdxCache";
96
675
        case CacheType::CLOUD_TABLET_CACHE:
97
675
            return "CloudTabletCache";
98
77
        case CacheType::CLOUD_TXN_DELETE_BITMAP_CACHE:
99
77
            return "CloudTxnDeleteBitmapCache";
100
27
        case CacheType::FOR_UT_CACHE_NUMBER:
101
27
            return "ForUTCacheNumber";
102
159
        case CacheType::QUERY_CACHE:
103
159
            return "QueryCache";
104
254
        case CacheType::TABLET_COLUMN_OBJECT_POOL:
105
254
            return "TabletColumnObjectPool";
106
0
        case CacheType::SCHEMA_CLOUD_DICTIONARY_CACHE:
107
0
            return "SchemaCloudDictionaryCache";
108
150
        case CacheType::CONDITION_CACHE:
109
150
            return "ConditionCache";
110
0
        default:
111
0
            throw Exception(Status::FatalError("not match type of cache policy :{}",
112
0
                                               static_cast<int>(type)));
113
8.09k
        }
114
0
        throw Exception(Status::FatalError("__builtin_unreachable"));
115
8.09k
    }
116
117
    inline static std::unordered_map<std::string, CacheType> StringToType = {
118
            {"DataPageCache", CacheType::DATA_PAGE_CACHE},
119
            {"IndexPageCache", CacheType::INDEXPAGE_CACHE},
120
            {"PKIndexPageCache", CacheType::PK_INDEX_PAGE_CACHE},
121
            {"SchemaCache", CacheType::SCHEMA_CACHE},
122
            {"SegmentCache", CacheType::SEGMENT_CACHE},
123
            {"InvertedIndexSearcherCache", CacheType::INVERTEDINDEX_SEARCHER_CACHE},
124
            {"InvertedIndexQueryCache", CacheType::INVERTEDINDEX_QUERY_CACHE},
125
            {"PointQueryLookupConnectionCache", CacheType::LOOKUP_CONNECTION_CACHE},
126
            {"PointQueryRowCache", CacheType::POINT_QUERY_ROW_CACHE},
127
            {"MowDeleteBitmapAggCache", CacheType::DELETE_BITMAP_AGG_CACHE},
128
            {"MowTabletVersionCache", CacheType::TABLET_VERSION_CACHE},
129
            {"LoadStateChannelCache ", CacheType::LOAD_STATE_CHANNEL_CACHE},
130
            {"CommonObjLRUCache", CacheType::COMMON_OBJ_LRU_CACHE},
131
            {"ForUTCacheSize", CacheType::FOR_UT_CACHE_SIZE},
132
            {"TabletSchemaCache", CacheType::TABLET_SCHEMA_CACHE},
133
            {"CreateTabletRRIdxCache", CacheType::CREATE_TABLET_RR_IDX_CACHE},
134
            {"CloudTabletCache", CacheType::CLOUD_TABLET_CACHE},
135
            {"CloudTxnDeleteBitmapCache", CacheType::CLOUD_TXN_DELETE_BITMAP_CACHE},
136
            {"ForUTCacheNumber", CacheType::FOR_UT_CACHE_NUMBER},
137
            {"QueryCache", CacheType::QUERY_CACHE},
138
            {"TabletColumnObjectPool", CacheType::TABLET_COLUMN_OBJECT_POOL},
139
    };
140
141
0
    static CacheType string_to_type(std::string type) {
142
0
        if (StringToType.contains(type)) {
143
0
            return StringToType[type];
144
0
        } else {
145
0
            return CacheType::NONE;
146
0
        }
147
0
    }
148
149
    inline static std::vector<CacheType> MetadataCache {
150
            CacheType::SEGMENT_CACHE, CacheType::SCHEMA_CACHE, CacheType::TABLET_SCHEMA_CACHE};
151
152
    CachePolicy(CacheType type, size_t capacity, uint32_t stale_sweep_time_s, bool enable_prune);
153
    virtual ~CachePolicy();
154
155
    virtual void prune_stale() = 0;
156
    virtual void prune_all(bool force) = 0;
157
    virtual int64_t adjust_capacity_weighted(double adjust_weighted) = 0;
158
    virtual size_t get_capacity() = 0;
159
160
3.82k
    CacheType type() { return _type; }
161
0
    size_t initial_capacity() const { return _initial_capacity; }
162
    virtual int64_t reset_initial_capacity(double adjust_weighted) = 0;
163
1.72k
    bool enable_prune() const { return _enable_prune; }
164
2.88k
    RuntimeProfile* profile() { return _profile.get(); }
165
166
protected:
167
1.25k
    void init_profile() {
168
1.25k
        _profile =
169
1.25k
                std::make_unique<RuntimeProfile>(fmt::format("Cache type={}", type_string(_type)));
170
1.25k
        _prune_stale_number_counter = ADD_COUNTER(_profile, "PruneStaleNumber", TUnit::UNIT);
171
1.25k
        _prune_all_number_counter = ADD_COUNTER(_profile, "PruneAllNumber", TUnit::UNIT);
172
1.25k
        _adjust_capacity_weighted_number_counter =
173
1.25k
                ADD_COUNTER(_profile, "SetCapacityNumber", TUnit::UNIT);
174
1.25k
        _freed_memory_counter = ADD_COUNTER(_profile, "FreedMemory", TUnit::BYTES);
175
1.25k
        _freed_entrys_counter = ADD_COUNTER(_profile, "FreedEntrys", TUnit::UNIT);
176
1.25k
        _cost_timer = ADD_TIMER(_profile, "CostTime");
177
1.25k
    }
178
179
    CacheType _type;
180
    size_t _initial_capacity {0};
181
182
    std::unique_ptr<RuntimeProfile> _profile;
183
    RuntimeProfile::Counter* _prune_stale_number_counter = nullptr;
184
    RuntimeProfile::Counter* _prune_all_number_counter = nullptr;
185
    RuntimeProfile::Counter* _adjust_capacity_weighted_number_counter = nullptr;
186
    // Reset before each gc
187
    RuntimeProfile::Counter* _freed_memory_counter = nullptr;
188
    RuntimeProfile::Counter* _freed_entrys_counter = nullptr;
189
    RuntimeProfile::Counter* _cost_timer = nullptr;
190
191
    uint32_t _stale_sweep_time_s;
192
    bool _enable_prune = true;
193
};
194
195
#include "common/compile_check_end.h"
196
} // namespace doris