-
Notifications
You must be signed in to change notification settings - Fork 411
/
Copy pathResourceControlQueue.cpp
273 lines (243 loc) · 8.58 KB
/
ResourceControlQueue.cpp
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
// Copyright 2023 PingCAP, Inc.
//
// Licensed 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 <Flash/Executor/PipelineExecutorContext.h>
#include <Flash/Executor/toRU.h>
#include <Flash/Pipeline/Schedule/TaskQueues/IOPriorityQueue.h>
#include <Flash/Pipeline/Schedule/TaskQueues/MultiLevelFeedbackQueue.h>
#include <Flash/Pipeline/Schedule/TaskQueues/ResourceControlQueue.h>
#include <Flash/Pipeline/Schedule/Tasks/TaskHelper.h>
namespace DB
{
template <typename NestedTaskQueueType>
void ResourceControlQueue<NestedTaskQueueType>::submit(TaskPtr && task)
{
{
std::lock_guard lock(mu);
submitWithoutLock(std::move(task));
}
cv.notify_one();
}
template <typename NestedTaskQueueType>
void ResourceControlQueue<NestedTaskQueueType>::submit(std::vector<TaskPtr> & tasks)
{
if (tasks.empty())
return;
{
std::lock_guard lock(mu);
for (auto & task : tasks)
{
submitWithoutLock(std::move(task));
cv.notify_one();
}
}
}
template <typename NestedTaskQueueType>
void ResourceControlQueue<NestedTaskQueueType>::submitWithoutLock(TaskPtr && task)
{
if unlikely (is_finished)
{
FINALIZE_TASK(task);
return;
}
const auto & query_id = task->getQueryId();
if unlikely (cancel_query_id_cache.contains(query_id))
{
cancel_task_queue.push_back(std::move(task));
return;
}
const String & name = task->getResourceGroupName();
auto iter = resource_group_task_queues.find(name);
if (iter == resource_group_task_queues.end())
{
auto task_queue = std::make_shared<NestedTaskQueueType>();
auto priority = LocalAdmissionController::global_instance->getPriority(name);
if unlikely (!priority.has_value())
{
error_task_queue.push_back(std::move(task));
return;
}
resource_group_infos.push({name, priority.value(), task_queue});
bool inserted = false;
std::tie(iter, inserted) = resource_group_task_queues.insert({name, task_queue});
assert(inserted);
}
assert(task);
iter->second->submit(std::move(task));
}
template <typename NestedTaskQueueType>
bool ResourceControlQueue<NestedTaskQueueType>::take(TaskPtr & task)
{
assert(!task);
bool is_error_task = false;
SCOPE_EXIT({
if unlikely (is_error_task)
{
assert(task);
task->onErrorOccurred(fmt::format(error_template, task->getResourceGroupName()));
}
});
std::unique_lock lock(mu);
while (true)
{
if unlikely (is_finished)
return false;
if (popTask(cancel_task_queue, task))
return true;
if unlikely (popTask(error_task_queue, task))
{
is_error_task = true;
return true;
}
if unlikely (updateResourceGroupInfosWithoutLock())
continue;
if (!resource_group_infos.empty())
{
const ResourceGroupInfo & group_info = resource_group_infos.top();
const bool ru_exhausted = LocalAdmissionController::isRUExhausted(group_info.priority);
LOG_TRACE(
logger,
"trying to schedule task of resource group {}, priority: {}, ru exhausted: {}, is_finished: {}, "
"task_queue.empty(): {}",
group_info.name,
group_info.priority,
ru_exhausted,
is_finished,
group_info.task_queue->empty());
// When highest priority of resource group is less than zero, means RU of all resource groups are exhausted.
// Should not take any task from nested task queue for this situation.
if (!ru_exhausted)
{
mustTakeTask(group_info.task_queue, task);
return true;
}
}
assert(!task);
// Wakeup when:
// 1. finish() is called.
// 2. refill_token_callback is called by LAC.
cv.wait(lock);
}
}
template <typename NestedTaskQueueType>
void ResourceControlQueue<NestedTaskQueueType>::updateStatistics(const TaskPtr & task, ExecTaskStatus, UInt64 inc_value)
{
assert(task);
auto ru = cpuTimeToRU(inc_value);
const String & name = task->getResourceGroupName();
LOG_TRACE(logger, "resource group {} will consume {} RU(or {} cpu time in ns)", name, ru, inc_value);
LocalAdmissionController::global_instance->consumeResource(name, ru, inc_value);
}
template <typename NestedTaskQueueType>
bool ResourceControlQueue<NestedTaskQueueType>::updateResourceGroupInfosWithoutLock()
{
assert(error_task_queue.empty());
std::priority_queue<ResourceGroupInfo> new_resource_group_infos;
while (!resource_group_infos.empty())
{
const ResourceGroupInfo & group_info = resource_group_infos.top();
if (!group_info.task_queue->empty())
{
auto new_priority = LocalAdmissionController::global_instance->getPriority(group_info.name);
if unlikely (!new_priority.has_value())
{
// resource group has been deleted, take all tasks and erase this group info.
TaskPtr task;
while (!group_info.task_queue->empty())
{
RUNTIME_CHECK(group_info.task_queue->take(task));
error_task_queue.push_back(std::move(task));
}
mustEraseResourceGroupInfoWithoutLock(group_info.name);
}
else
{
// resource group ok, reorder group info by priority.
new_resource_group_infos.push({group_info.name, new_priority.value(), group_info.task_queue});
resource_group_infos.pop();
}
}
else
{
mustEraseResourceGroupInfoWithoutLock(group_info.name);
}
}
resource_group_infos = new_resource_group_infos;
return !error_task_queue.empty();
}
template <typename NestedTaskQueueType>
bool ResourceControlQueue<NestedTaskQueueType>::empty() const
{
std::lock_guard lock(mu);
if (!cancel_task_queue.empty())
return false;
if unlikely (!error_task_queue.empty())
return false;
if (resource_group_task_queues.empty())
return true;
for (const auto & task_queue_iter : resource_group_task_queues)
{
if (!task_queue_iter.second->empty())
return false;
}
return true;
}
template <typename NestedTaskQueueType>
void ResourceControlQueue<NestedTaskQueueType>::finish()
{
{
std::lock_guard lock(mu);
is_finished = true;
for (auto & ele : resource_group_task_queues)
ele.second->finish();
}
cv.notify_all();
}
template <typename NestedTaskQueueType>
void ResourceControlQueue<NestedTaskQueueType>::cancel(const String & query_id, const String & resource_group_name)
{
if unlikely (query_id.empty())
return;
std::lock_guard lock(mu);
if (cancel_query_id_cache.add(query_id))
{
auto iter = resource_group_task_queues.find(resource_group_name);
if (iter != resource_group_task_queues.end())
{
iter->second->collectCancelledTasks(cancel_task_queue, query_id);
}
}
cv.notify_all();
}
template <typename NestedTaskQueueType>
void ResourceControlQueue<NestedTaskQueueType>::mustEraseResourceGroupInfoWithoutLock(const String & name)
{
size_t erase_num = resource_group_task_queues.erase(name);
RUNTIME_CHECK_MSG(
erase_num == 1,
"cannot erase corresponding TaskQueue for task of resource group {}, erase_num: {}",
name,
erase_num);
resource_group_infos.pop();
}
template <typename NestedTaskQueueType>
void ResourceControlQueue<NestedTaskQueueType>::mustTakeTask(const NestedTaskQueuePtr & task_queue, TaskPtr & task)
{
assert(!task_queue->empty());
RUNTIME_CHECK(task_queue->take(task));
assert(task);
}
template class ResourceControlQueue<CPUMultiLevelFeedbackQueue>;
// For now, io_task_thread_pool is not managed by ResourceControl mechanism.
template class ResourceControlQueue<IOPriorityQueue>;
} // namespace DB