-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Expand file tree
/
Copy pathscanner_scheduler.h
More file actions
427 lines (362 loc) · 17.2 KB
/
scanner_scheduler.h
File metadata and controls
427 lines (362 loc) · 17.2 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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
// 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.
#pragma once
#include <atomic>
#include <memory>
#include "common/be_mock_util.h"
#include "common/status.h"
#include "exec/scan/scanner_context.h"
#include "exec/scan/task_executor/listenable_future.h"
#include "exec/scan/task_executor/ticker.h"
#include "exec/scan/task_executor/time_sharing/time_sharing_task_executor.h"
#include "util/threadpool.h"
namespace doris {
class ExecEnv;
class Scanner;
class Block;
template <typename T>
class BlockingQueue;
} // namespace doris
namespace doris {
class ScannerDelegate;
class ScanTask;
class ScannerContext;
class ScannerScheduler;
struct SimplifiedScanTask {
SimplifiedScanTask() = default;
SimplifiedScanTask(std::function<bool()> scan_func,
std::shared_ptr<ScannerContext> scanner_context,
std::shared_ptr<ScanTask> scan_task) {
this->scan_func = scan_func;
this->scanner_context = scanner_context;
this->scan_task = scan_task;
}
std::function<bool()> scan_func;
std::shared_ptr<ScannerContext> scanner_context = nullptr;
std::shared_ptr<ScanTask> scan_task = nullptr;
};
class ScannerSplitRunner : public SplitRunner {
public:
ScannerSplitRunner(std::string name, std::function<bool()> scan_func)
: _name(std::move(name)), _scan_func(scan_func), _started(false) {}
Status init() override { return Status::OK(); }
Result<SharedListenableFuture<Void>> process_for(std::chrono::nanoseconds) override;
void close(const Status& status) override {}
std::string get_info() const override { return ""; }
bool is_finished() override;
Status finished_status() override;
bool is_started() const;
bool is_auto_reschedule() const override;
private:
std::string _name;
std::function<bool()> _scan_func;
std::atomic<bool> _started;
SharedListenableFuture<Void> _completion_future;
};
// Abstract interface for scan scheduler
// Responsible for the scheduling and execution of all Scanners of a BE node.
// Execution thread pool
// When a ScannerContext is launched, it will submit the running scanners to this scheduler.
// The scheduling thread will submit the running scanner and its ScannerContext
// to the execution thread pool to do the actual scan task.
// Each Scanner will act as a producer, read the next block and put it into
// the corresponding block queue.
// The corresponding ScanNode will act as a consumer to consume blocks from the block queue.
// After the block is consumed, the unfinished scanner will resubmit to this scheduler.
class ScannerScheduler {
public:
virtual ~ScannerScheduler() {}
Status submit(std::shared_ptr<ScannerContext> ctx, std::shared_ptr<ScanTask> scan_task);
static int default_local_scan_thread_num();
static int default_remote_scan_thread_num();
static int get_remote_scan_thread_queue_size();
static int default_min_active_scan_threads();
static int default_min_active_file_scan_threads();
virtual Status start(int max_thread_num, int min_thread_num, int queue_size,
int min_active_scan_threads) = 0;
virtual void stop() = 0;
virtual Status submit_scan_task(SimplifiedScanTask scan_task) = 0;
virtual Status submit_scan_task(SimplifiedScanTask scan_task,
const std::string& task_id_string) = 0;
virtual void reset_thread_num(int new_max_thread_num, int new_min_thread_num,
int min_active_scan_threads) = 0;
int get_min_active_scan_threads() const { return _min_active_scan_threads; }
virtual int get_queue_size() = 0;
virtual int get_active_threads() = 0;
virtual std::vector<int> thread_debug_info() = 0;
virtual Status schedule_scan_task(std::shared_ptr<ScannerContext> scanner_ctx,
std::shared_ptr<ScanTask> current_scan_task,
std::unique_lock<std::mutex>& transfer_lock) = 0;
protected:
int _min_active_scan_threads;
private:
static void _scanner_scan(std::shared_ptr<ScannerContext> ctx,
std::shared_ptr<ScanTask> scan_task);
static void _make_sure_virtual_col_is_materialized(const std::shared_ptr<Scanner>& scanner,
Block* block);
};
class ThreadPoolSimplifiedScanScheduler MOCK_REMOVE(final) : public ScannerScheduler {
public:
ThreadPoolSimplifiedScanScheduler(std::string sched_name,
std::shared_ptr<CgroupCpuCtl> cgroup_cpu_ctl,
std::string workload_group = "system")
: _is_stop(false),
_cgroup_cpu_ctl(cgroup_cpu_ctl),
_sched_name(sched_name),
_workload_group(workload_group) {}
~ThreadPoolSimplifiedScanScheduler() override {
#ifndef BE_TEST
stop();
#endif
LOG(INFO) << "Scanner scheduler " << _sched_name << " shutdown";
}
void stop() override {
_is_stop.store(true);
_scan_thread_pool->shutdown();
_scan_thread_pool->wait();
}
Status start(int max_thread_num, int min_thread_num, int queue_size,
int min_active_scan_threads) override {
_min_active_scan_threads = min_active_scan_threads;
RETURN_IF_ERROR(ThreadPoolBuilder(_sched_name, _workload_group)
.set_min_threads(min_thread_num)
.set_max_threads(max_thread_num)
.set_max_queue_size(queue_size)
.set_cgroup_cpu_ctl(_cgroup_cpu_ctl)
.build(&_scan_thread_pool));
return Status::OK();
}
Status submit_scan_task(SimplifiedScanTask scan_task) override {
if (!_is_stop) {
return _scan_thread_pool->submit_func([scan_task] { scan_task.scan_func(); });
} else {
return Status::InternalError<false>("scanner pool {} is shutdown.", _sched_name);
}
}
Status submit_scan_task(SimplifiedScanTask scan_task,
const std::string& task_id_string) override {
return submit_scan_task(scan_task);
}
void reset_thread_num(int new_max_thread_num, int new_min_thread_num,
int min_active_scan_threads) override {
_min_active_scan_threads = min_active_scan_threads;
int cur_max_thread_num = _scan_thread_pool->max_threads();
int cur_min_thread_num = _scan_thread_pool->min_threads();
if (cur_max_thread_num == new_max_thread_num && cur_min_thread_num == new_min_thread_num) {
return;
}
if (new_max_thread_num >= cur_max_thread_num) {
Status st_max = _scan_thread_pool->set_max_threads(new_max_thread_num);
if (!st_max.ok()) {
LOG(WARNING) << "Failed to set max threads for scan thread pool: "
<< st_max.to_string();
}
Status st_min = _scan_thread_pool->set_min_threads(new_min_thread_num);
if (!st_min.ok()) {
LOG(WARNING) << "Failed to set min threads for scan thread pool: "
<< st_min.to_string();
}
} else {
Status st_min = _scan_thread_pool->set_min_threads(new_min_thread_num);
if (!st_min.ok()) {
LOG(WARNING) << "Failed to set min threads for scan thread pool: "
<< st_min.to_string();
}
Status st_max = _scan_thread_pool->set_max_threads(new_max_thread_num);
if (!st_max.ok()) {
LOG(WARNING) << "Failed to set max threads for scan thread pool: "
<< st_max.to_string();
}
}
}
int get_queue_size() override { return _scan_thread_pool->get_queue_size(); }
int get_active_threads() override { return _scan_thread_pool->num_active_threads(); }
std::vector<int> thread_debug_info() override { return _scan_thread_pool->debug_info(); }
Status schedule_scan_task(std::shared_ptr<ScannerContext> scanner_ctx,
std::shared_ptr<ScanTask> current_scan_task,
std::unique_lock<std::mutex>& transfer_lock) override;
private:
std::unique_ptr<ThreadPool> _scan_thread_pool;
std::atomic<bool> _is_stop;
std::weak_ptr<CgroupCpuCtl> _cgroup_cpu_ctl;
std::string _sched_name;
std::string _workload_group;
std::shared_mutex _lock;
};
class TaskExecutorSimplifiedScanScheduler final : public ScannerScheduler {
public:
TaskExecutorSimplifiedScanScheduler(std::string sched_name,
std::shared_ptr<CgroupCpuCtl> cgroup_cpu_ctl,
std::string workload_group = "system")
: _is_stop(false),
_cgroup_cpu_ctl(cgroup_cpu_ctl),
_sched_name(sched_name),
_workload_group(workload_group) {}
~TaskExecutorSimplifiedScanScheduler() override {
#ifndef BE_TEST
stop();
#endif
LOG(INFO) << "Scanner scheduler " << _sched_name << " shutdown";
}
void stop() override {
_is_stop.store(true);
_task_executor->stop();
_task_executor->wait();
}
Status start(int max_thread_num, int min_thread_num, int queue_size,
int min_active_scan_threads) override {
_min_active_scan_threads = min_active_scan_threads;
TimeSharingTaskExecutor::ThreadConfig thread_config;
thread_config.thread_name = _sched_name;
thread_config.workload_group = _workload_group;
thread_config.max_thread_num = max_thread_num;
thread_config.min_thread_num = min_thread_num;
thread_config.max_queue_size = queue_size;
thread_config.cgroup_cpu_ctl = _cgroup_cpu_ctl;
_task_executor = TimeSharingTaskExecutor::create_shared(
thread_config, max_thread_num * 2, config::task_executor_min_concurrency_per_task,
config::task_executor_max_concurrency_per_task > 0
? config::task_executor_max_concurrency_per_task
: std::numeric_limits<int>::max(),
std::make_shared<SystemTicker>(), nullptr, false);
RETURN_IF_ERROR(_task_executor->init());
RETURN_IF_ERROR(_task_executor->start());
return Status::OK();
}
Status submit_scan_task(SimplifiedScanTask scan_task) override {
if (!_is_stop) {
std::shared_ptr<SplitRunner> split_runner;
if (scan_task.scan_task->is_first_schedule) {
split_runner = std::make_shared<ScannerSplitRunner>("scanner_split_runner",
scan_task.scan_func);
RETURN_IF_ERROR(split_runner->init());
auto result = _task_executor->enqueue_splits(
scan_task.scanner_context->task_handle(), false, {split_runner});
if (!result.has_value()) {
LOG(WARNING) << "enqueue_splits failed: " << result.error();
return result.error();
}
scan_task.scan_task->is_first_schedule = false;
} else {
split_runner = scan_task.scan_task->split_runner.lock();
if (split_runner == nullptr) {
return Status::OK();
}
RETURN_IF_ERROR(_task_executor->re_enqueue_split(
scan_task.scanner_context->task_handle(), false, split_runner));
}
scan_task.scan_task->split_runner = split_runner;
return Status::OK();
} else {
return Status::InternalError<false>("scanner pool {} is shutdown.", _sched_name);
}
}
// A task has only one split. When the split is created, the task is created according to the task_id,
// and the task is automatically removed when the split ends.
// Now it is only for PInternalService::multiget_data_v2 used by TopN materialization.
Status submit_scan_task(SimplifiedScanTask scan_task,
const std::string& task_id_string) override {
if (!_is_stop) {
TaskId task_id(task_id_string);
std::shared_ptr<TaskHandle> task_handle = DORIS_TRY(_task_executor->create_task(
task_id, []() { return 0.0; },
config::task_executor_initial_max_concurrency_per_task > 0
? config::task_executor_initial_max_concurrency_per_task
: std::max(48, CpuInfo::num_cores() * 2),
std::chrono::milliseconds(100), std::nullopt));
auto wrapped_scan_func = [this, task_handle, scan_func = scan_task.scan_func]() {
bool result = scan_func();
if (result) {
static_cast<void>(_task_executor->remove_task(task_handle));
}
return result;
};
auto split_runner =
std::make_shared<ScannerSplitRunner>("scanner_split_runner", wrapped_scan_func);
RETURN_IF_ERROR(split_runner->init());
auto result = _task_executor->enqueue_splits(task_handle, false, {split_runner});
if (!result.has_value()) {
LOG(WARNING) << "enqueue_splits failed: " << result.error();
return result.error();
}
return Status::OK();
} else {
return Status::InternalError<false>("scanner pool {} is shutdown.", _sched_name);
}
}
void reset_thread_num(int new_max_thread_num, int new_min_thread_num,
int min_active_scan_threads) override {
_min_active_scan_threads = min_active_scan_threads;
auto task_executor =
std::dynamic_pointer_cast<doris::TimeSharingTaskExecutor>(_task_executor);
int cur_max_thread_num = task_executor->max_threads();
int cur_min_thread_num = task_executor->min_threads();
if (cur_max_thread_num == new_max_thread_num && cur_min_thread_num == new_min_thread_num) {
return;
}
if (new_max_thread_num >= cur_max_thread_num) {
Status st_max = task_executor->set_max_threads(new_max_thread_num);
if (!st_max.ok()) {
LOG(WARNING) << "Failed to set max threads for scan thread pool: "
<< st_max.to_string();
}
Status st_min = task_executor->set_min_threads(new_min_thread_num);
if (!st_min.ok()) {
LOG(WARNING) << "Failed to set min threads for scan thread pool: "
<< st_min.to_string();
}
} else {
Status st_min = task_executor->set_min_threads(new_min_thread_num);
if (!st_min.ok()) {
LOG(WARNING) << "Failed to set min threads for scan thread pool: "
<< st_min.to_string();
}
Status st_max = task_executor->set_max_threads(new_max_thread_num);
if (!st_max.ok()) {
LOG(WARNING) << "Failed to set max threads for scan thread pool: "
<< st_max.to_string();
}
}
}
int get_queue_size() override {
auto task_executor =
std::dynamic_pointer_cast<doris::TimeSharingTaskExecutor>(_task_executor);
return task_executor->get_queue_size();
}
int get_active_threads() override {
auto task_executor =
std::dynamic_pointer_cast<doris::TimeSharingTaskExecutor>(_task_executor);
return task_executor->num_active_threads();
}
std::vector<int> thread_debug_info() override {
auto task_executor =
std::dynamic_pointer_cast<doris::TimeSharingTaskExecutor>(_task_executor);
return task_executor->debug_info();
}
std::shared_ptr<TaskExecutor> task_executor() const { return _task_executor; }
Status schedule_scan_task(std::shared_ptr<ScannerContext> scanner_ctx,
std::shared_ptr<ScanTask> current_scan_task,
std::unique_lock<std::mutex>& transfer_lock) override;
private:
std::atomic<bool> _is_stop;
std::weak_ptr<CgroupCpuCtl> _cgroup_cpu_ctl;
std::string _sched_name;
std::string _workload_group;
std::shared_mutex _lock;
std::shared_ptr<TaskExecutor> _task_executor = nullptr;
};
} // namespace doris