Coverage Report

Created: 2026-05-17 01:25

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