forked from apache/doris
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresult_cache.cpp
More file actions
282 lines (259 loc) · 8.75 KB
/
Copy pathresult_cache.cpp
File metadata and controls
282 lines (259 loc) · 8.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
#include "runtime/cache/result_cache.h"
#include <gen_cpp/internal_service.pb.h>
#include <glog/logging.h>
#include <iostream>
#include <list>
#include <utility>
#include "common/metrics/doris_metrics.h"
#include "runtime/cache/cache_utils.h"
#include "storage/olap_define.h"
namespace doris {
/**
* Remove the tail node of link
*/
ResultNode* ResultNodeList::pop() {
remove(_head);
return _head;
}
void ResultNodeList::remove(ResultNode* node) {
if (!node) return;
if (node == _head) _head = node->get_next();
if (node == _tail) _tail = node->get_prev();
node->unlink();
_node_count--;
}
void ResultNodeList::push_back(ResultNode* node) {
if (!node) return;
if (!_head) _head = node;
node->append(_tail);
_tail = node;
_node_count++;
}
void ResultNodeList::move_tail(ResultNode* node) {
if (!node || node == _tail) return;
if (!_head)
_head = node;
else if (node == _head)
_head = node->get_next();
node->unlink();
node->append(_tail);
_tail = node;
}
void ResultNodeList::delete_node(ResultNode** node) {
(*node)->clear();
SAFE_DELETE(*node);
}
void ResultNodeList::clear() {
LOG(INFO) << "clear result node list.";
while (_head) {
ResultNode* tmp_node = _head->get_next();
_head->clear();
SAFE_DELETE(_head);
_head = tmp_node;
}
_node_count = 0;
}
/**
* Find the node and update partition data
* New node, the node updated in the first partition will move to the tail of the list
*/
void ResultCache::update(const PUpdateCacheRequest* request, PCacheResponse* response) {
ResultNode* node;
PCacheStatus status;
bool update_first = false;
UniqueId sql_key = request->sql_key();
LOG(INFO) << "update cache, sql key:" << sql_key;
CacheWriteLock write_lock(_cache_mtx);
auto it = _node_map.find(sql_key);
if (it != _node_map.end()) {
node = it->second;
_cache_size -= node->get_data_size();
_partition_count -= node->get_partition_count();
status = node->update_partition(request, update_first);
} else {
node = _node_list.new_node(sql_key);
status = node->update_partition(request, update_first);
_node_list.push_back(node);
_node_map[sql_key] = node;
_node_count += 1;
}
if (update_first) {
_node_list.move_tail(node);
}
_cache_size += node->get_data_size();
_partition_count += node->get_partition_count();
response->set_status(status);
prune();
update_monitor();
}
/**
* Fetch cache through sql key, partition key, version and time
*/
void ResultCache::fetch(const PFetchCacheRequest* request, PFetchCacheResult* result) {
bool hit_first = false;
ResultNodeMap::iterator node_it;
const UniqueId sql_key = request->sql_key();
LOG(INFO) << "fetch cache, sql key:" << sql_key;
{
CacheReadLock read_lock(_cache_mtx);
node_it = _node_map.find(sql_key);
if (node_it == _node_map.end()) {
result->set_status(PCacheStatus::NO_SQL_KEY);
LOG(INFO) << "no such sql key:" << sql_key;
return;
}
ResultNode* node = node_it->second;
PartitionRowBatchList part_rowbatch_list;
PCacheStatus status = node->fetch_partition(request, part_rowbatch_list, hit_first);
for (auto part_it = part_rowbatch_list.begin(); part_it != part_rowbatch_list.end();
part_it++) {
PCacheValue* srcValue = (*part_it)->get_value();
if (srcValue != nullptr) {
PCacheValue* value = result->add_values();
value->CopyFrom(*srcValue);
LOG(INFO) << "fetch cache partition key:" << srcValue->param().partition_key();
} else {
LOG(WARNING) << "prowbatch of cache is null";
status = PCacheStatus::EMPTY_DATA;
break;
}
}
if (status == PCacheStatus::CACHE_OK && part_rowbatch_list.empty()) {
status = PCacheStatus::EMPTY_DATA;
}
result->set_status(status);
}
if (hit_first) {
CacheWriteLock write_lock(_cache_mtx);
_node_list.move_tail(node_it->second);
}
}
bool ResultCache::contains(const UniqueId& sql_key) {
CacheReadLock read_lock(_cache_mtx);
return _node_map.find(sql_key) != _node_map.end();
}
/**
* enum PClearType {
* CLEAR_ALL = 0,
* PRUNE_CACHE = 1,
* CLEAR_BEFORE_TIME = 2,
* CLEAR_SQL_KEY = 3
* };
*/
void ResultCache::clear(const PClearCacheRequest* request, PCacheResponse* response) {
LOG(INFO) << "clear cache type" << request->clear_type()
<< ", node size:" << _node_list.get_node_count() << ", map size:" << _node_map.size();
CacheWriteLock write_lock(_cache_mtx);
//0 clear, 1 prune, 2 before_time,3 sql_key
switch (request->clear_type()) {
case PClearType::CLEAR_ALL:
_node_list.clear();
_node_map.clear();
_cache_size = 0;
_node_count = 0;
_partition_count = 0;
break;
case PClearType::PRUNE_CACHE:
prune();
break;
default:
break;
}
update_monitor();
response->set_status(PCacheStatus::CACHE_OK);
}
//private method
ResultNode* find_min_time_node(ResultNode* result_node) {
if (result_node->get_prev()) {
if (result_node->get_prev()->first_partition_last_time() <=
result_node->first_partition_last_time()) {
return result_node->get_prev();
}
}
if (result_node->get_next()) {
if (result_node->get_next()->first_partition_last_time() <
result_node->first_partition_last_time()) {
return result_node->get_next();
}
}
return result_node;
}
/*
* Two-dimensional array, prune the min last_read_time PartitionRowBatch.
* The following example is the last read time array.
* 1 and 2 is the read time, nodes with pruning read time < 3
* Before:
* 1,2 //_head ResultNode*
* 1,2,3,4,5
* 2,4,3,6,8
* 5,7,9,11,13 //_tail ResultNode*
* After:
* 4,5 //_head
* 4,3,6,8
* 5,7,9,11,13 //_tail
*/
void ResultCache::prune() {
if (_cache_size <= (_max_size + _elasticity_size)) {
return;
}
LOG(INFO) << "begin prune cache, cache_size : " << _cache_size << ", max_size : " << _max_size
<< ", elasticity_size : " << _elasticity_size;
ResultNode* result_node = _node_list.get_head();
while (_cache_size > _max_size) {
if (result_node == nullptr) {
break;
}
result_node = find_min_time_node(result_node);
_cache_size -= result_node->prune_first();
if (result_node->get_data_size() == 0) {
ResultNode* next_node;
if (result_node->get_next()) {
next_node = result_node->get_next();
} else if (result_node->get_prev()) {
next_node = result_node->get_prev();
} else {
next_node = _node_list.get_head();
}
remove(result_node);
result_node = next_node;
}
}
LOG(INFO) << "finish prune, cache_size : " << _cache_size;
_node_count = _node_map.size();
_cache_size = 0;
_partition_count = 0;
for (auto node_it = _node_map.begin(); node_it != _node_map.end(); node_it++) {
_partition_count += node_it->second->get_partition_count();
_cache_size += node_it->second->get_data_size();
}
}
void ResultCache::remove(ResultNode* result_node) {
auto node_it = _node_map.find(result_node->get_sql_key());
if (node_it != _node_map.end()) {
_node_map.erase(node_it);
_node_list.remove(result_node);
_node_list.delete_node(&result_node);
}
}
void ResultCache::update_monitor() {
DorisMetrics::instance()->query_cache_memory_total_byte->set_value(_cache_size);
DorisMetrics::instance()->query_cache_sql_total_count->set_value(_node_count);
DorisMetrics::instance()->query_cache_partition_total_count->set_value(_partition_count);
}
} // namespace doris