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 | | ANN_INDEX_IVF_LIST_CACHE = 24, |
61 | | }; |
62 | | |
63 | 5.78k | static std::string type_string(CacheType type) { |
64 | 5.78k | 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 | 720 | case CacheType::INVERTEDINDEX_SEARCHER_CACHE: |
76 | 720 | return "InvertedIndexSearcherCache"; |
77 | 330 | case CacheType::INVERTEDINDEX_QUERY_CACHE: |
78 | 330 | 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.71k | case CacheType::TABLET_VERSION_CACHE: |
86 | 1.71k | return "MowTabletVersionCache"; |
87 | 0 | case CacheType::LOAD_STATE_CHANNEL_CACHE: |
88 | 0 | return "LoadStateChannelCache "; |
89 | 130 | case CacheType::COMMON_OBJ_LRU_CACHE: |
90 | 130 | 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.71k | case CacheType::CREATE_TABLET_RR_IDX_CACHE: |
96 | 1.71k | return "CreateTabletRRIdxCache"; |
97 | 780 | case CacheType::CLOUD_TABLET_CACHE: |
98 | 780 | 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 | 50 | case CacheType::CONDITION_CACHE: |
110 | 50 | return "ConditionCache"; |
111 | 0 | case CacheType::ANN_INDEX_IVF_LIST_CACHE: |
112 | 0 | return "AnnIndexIVFListCache"; |
113 | 0 | default: |
114 | 0 | throw Exception(Status::FatalError("not match type of cache policy :{}", |
115 | 0 | static_cast<int>(type))); |
116 | 5.78k | } |
117 | 0 | throw Exception(Status::FatalError("__builtin_unreachable")); |
118 | 5.78k | } |
119 | | |
120 | | inline static std::unordered_map<std::string, CacheType> StringToType = { |
121 | | {"DataPageCache", CacheType::DATA_PAGE_CACHE}, |
122 | | {"IndexPageCache", CacheType::INDEXPAGE_CACHE}, |
123 | | {"PKIndexPageCache", CacheType::PK_INDEX_PAGE_CACHE}, |
124 | | {"SchemaCache", CacheType::SCHEMA_CACHE}, |
125 | | {"SegmentCache", CacheType::SEGMENT_CACHE}, |
126 | | {"InvertedIndexSearcherCache", CacheType::INVERTEDINDEX_SEARCHER_CACHE}, |
127 | | {"InvertedIndexQueryCache", CacheType::INVERTEDINDEX_QUERY_CACHE}, |
128 | | {"PointQueryLookupConnectionCache", CacheType::LOOKUP_CONNECTION_CACHE}, |
129 | | {"PointQueryRowCache", CacheType::POINT_QUERY_ROW_CACHE}, |
130 | | {"MowDeleteBitmapAggCache", CacheType::DELETE_BITMAP_AGG_CACHE}, |
131 | | {"MowTabletVersionCache", CacheType::TABLET_VERSION_CACHE}, |
132 | | {"LoadStateChannelCache ", CacheType::LOAD_STATE_CHANNEL_CACHE}, |
133 | | {"CommonObjLRUCache", CacheType::COMMON_OBJ_LRU_CACHE}, |
134 | | {"ForUTCacheSize", CacheType::FOR_UT_CACHE_SIZE}, |
135 | | {"TabletSchemaCache", CacheType::TABLET_SCHEMA_CACHE}, |
136 | | {"CreateTabletRRIdxCache", CacheType::CREATE_TABLET_RR_IDX_CACHE}, |
137 | | {"CloudTabletCache", CacheType::CLOUD_TABLET_CACHE}, |
138 | | {"CloudTxnDeleteBitmapCache", CacheType::CLOUD_TXN_DELETE_BITMAP_CACHE}, |
139 | | {"ForUTCacheNumber", CacheType::FOR_UT_CACHE_NUMBER}, |
140 | | {"QueryCache", CacheType::QUERY_CACHE}, |
141 | | {"TabletColumnObjectPool", CacheType::TABLET_COLUMN_OBJECT_POOL}, |
142 | | {"ConditionCache", CacheType::CONDITION_CACHE}, |
143 | | {"AnnIndexIVFListCache", CacheType::ANN_INDEX_IVF_LIST_CACHE}}; |
144 | | |
145 | 0 | static CacheType string_to_type(std::string type) { |
146 | 0 | if (StringToType.contains(type)) { |
147 | 0 | return StringToType[type]; |
148 | 0 | } else { |
149 | 0 | return CacheType::NONE; |
150 | 0 | } |
151 | 0 | } |
152 | | |
153 | | inline static std::vector<CacheType> MetadataCache { |
154 | | CacheType::SEGMENT_CACHE, CacheType::SCHEMA_CACHE, CacheType::TABLET_SCHEMA_CACHE}; |
155 | | |
156 | | CachePolicy(CacheType type, size_t capacity, uint32_t stale_sweep_time_s, bool enable_prune); |
157 | | virtual ~CachePolicy(); |
158 | | |
159 | | virtual void prune_stale() = 0; |
160 | | virtual void prune_all(bool force) = 0; |
161 | | virtual int64_t adjust_capacity_weighted(double adjust_weighted) = 0; |
162 | | virtual size_t get_capacity() = 0; |
163 | | |
164 | 3.50k | CacheType type() { return _type; } |
165 | 0 | size_t initial_capacity() const { return _initial_capacity; } |
166 | | virtual int64_t reset_initial_capacity(double adjust_weighted) = 0; |
167 | 0 | bool enable_prune() const { return _enable_prune; } |
168 | 0 | RuntimeProfile* profile() { return _profile.get(); } |
169 | | |
170 | | protected: |
171 | 1.15k | void init_profile() { |
172 | 1.15k | _profile = |
173 | 1.15k | std::make_unique<RuntimeProfile>(fmt::format("Cache type={}", type_string(_type))); |
174 | 1.15k | _prune_stale_number_counter = ADD_COUNTER(_profile, "PruneStaleNumber", TUnit::UNIT); |
175 | 1.15k | _prune_all_number_counter = ADD_COUNTER(_profile, "PruneAllNumber", TUnit::UNIT); |
176 | 1.15k | _adjust_capacity_weighted_number_counter = |
177 | 1.15k | ADD_COUNTER(_profile, "SetCapacityNumber", TUnit::UNIT); |
178 | 1.15k | _freed_memory_counter = ADD_COUNTER(_profile, "FreedMemory", TUnit::BYTES); |
179 | 1.15k | _freed_entrys_counter = ADD_COUNTER(_profile, "FreedEntrys", TUnit::UNIT); |
180 | 1.15k | _cost_timer = ADD_TIMER(_profile, "CostTime"); |
181 | 1.15k | } |
182 | | |
183 | | CacheType _type; |
184 | | size_t _initial_capacity {0}; |
185 | | |
186 | | std::unique_ptr<RuntimeProfile> _profile; |
187 | | RuntimeProfile::Counter* _prune_stale_number_counter = nullptr; |
188 | | RuntimeProfile::Counter* _prune_all_number_counter = nullptr; |
189 | | RuntimeProfile::Counter* _adjust_capacity_weighted_number_counter = nullptr; |
190 | | // Reset before each gc |
191 | | RuntimeProfile::Counter* _freed_memory_counter = nullptr; |
192 | | RuntimeProfile::Counter* _freed_entrys_counter = nullptr; |
193 | | RuntimeProfile::Counter* _cost_timer = nullptr; |
194 | | |
195 | | uint32_t _stale_sweep_time_s; |
196 | | bool _enable_prune = true; |
197 | | }; |
198 | | |
199 | | #include "common/compile_check_end.h" |
200 | | } // namespace doris |