Coverage Report

Created: 2026-05-29 13:56

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/exec/pipeline/task_scheduler.cpp
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
#include "exec/pipeline/task_scheduler.h"
19
20
#include <fmt/format.h>
21
#include <gen_cpp/types.pb.h>
22
#include <glog/logging.h>
23
#include <sched.h>
24
25
// IWYU pragma: no_include <bits/chrono.h>
26
#include <algorithm>
27
#include <chrono> // IWYU pragma: keep
28
#include <cstddef>
29
#include <functional>
30
#include <memory>
31
#include <mutex>
32
#include <ostream>
33
#include <string>
34
#include <utility>
35
36
#include "common/logging.h"
37
#include "common/status.h"
38
#include "core/value/vdatetime_value.h"
39
#include "exec/pipeline/pipeline_fragment_context.h"
40
#include "exec/pipeline/pipeline_task.h"
41
#include "runtime/query_context.h"
42
#include "runtime/thread_context.h"
43
#include "util/thread.h"
44
#include "util/threadpool.h"
45
#include "util/time.h"
46
#include "util/uid_util.h"
47
48
namespace doris {
49
81
TaskScheduler::~TaskScheduler() {
50
81
    stop();
51
81
    LOG(INFO) << "Task scheduler " << _name << " shutdown";
52
81
}
53
54
68
Status TaskScheduler::start() {
55
68
    RETURN_IF_ERROR(ThreadPoolBuilder(_name)
56
68
                            .set_min_threads(_num_threads)
57
68
                            .set_max_threads(_num_threads)
58
68
                            .set_max_queue_size(0)
59
68
                            .set_cgroup_cpu_ctl(_cgroup_cpu_ctl)
60
68
                            .build(&_fix_thread_pool));
61
68
    LOG_INFO("TaskScheduler set cores").tag("size", _num_threads);
62
3.09k
    for (int32_t i = 0; i < _num_threads; ++i) {
63
3.02k
        RETURN_IF_ERROR(_fix_thread_pool->submit_func([this, i] { _do_work(i); }));
64
3.02k
    }
65
68
    return Status::OK();
66
68
}
67
68
3.12M
Status TaskScheduler::submit(PipelineTaskSPtr task) {
69
3.12M
    return _task_queue.push_back(task);
70
3.12M
}
71
72
// after close_task, task maybe destructed.
73
819k
void close_task(PipelineTask* task, Status exec_status, PipelineFragmentContext* ctx) {
74
    // Has to attach memory tracker here, because the close task will also release some memory.
75
    // Should count the memory to the query or the query's memory will not decrease when part of
76
    // task finished.
77
819k
    SCOPED_ATTACH_TASK(task->runtime_state());
78
819k
    if (!exec_status.ok()) {
79
1.43k
        ctx->cancel(exec_status);
80
1.43k
        LOG(WARNING) << fmt::format("Pipeline task failed. query_id: {} reason: {}",
81
1.43k
                                    print_id(ctx->get_query_id()), exec_status.to_string());
82
1.43k
    }
83
819k
    Status status = task->close(exec_status);
84
819k
    if (!status.ok()) {
85
233
        ctx->cancel(status);
86
233
    }
87
819k
    status = task->finalize();
88
819k
    if (!status.ok()) {
89
0
        ctx->cancel(status);
90
0
    }
91
819k
}
92
93
3.01k
void TaskScheduler::_do_work(int index) {
94
5.01M
    while (!_need_to_stop) {
95
5.01M
        auto task = _task_queue.take(index);
96
5.01M
        if (!task) {
97
1.00M
            continue;
98
1.00M
        }
99
100
        // The task is already running, maybe block in now dependency wake up by other thread
101
        // but the block thread still hold the task, so put it back to the queue, until the hold
102
        // thread set task->set_running(false)
103
        // set_running return the old value
104
4.01M
        if (task->set_running(true)) {
105
110k
            static_cast<void>(_task_queue.push_back(task, index));
106
110k
            continue;
107
110k
        }
108
109
3.90M
        if (task->is_finalized()) {
110
0
            task->set_running(false);
111
0
            continue;
112
0
        }
113
114
3.90M
        auto fragment_context = task->fragment_context().lock();
115
3.90M
        if (!fragment_context) {
116
            // Fragment already finished
117
0
            task->set_running(false);
118
0
            continue;
119
0
        }
120
121
3.90M
        task->set_thread_id(index);
122
123
3.90M
        bool done = false;
124
3.90M
        auto status = Status::OK();
125
3.90M
        int64_t exec_ns = 0;
126
3.90M
        SCOPED_RAW_TIMER(&exec_ns);
127
3.90M
        Defer task_running_defer {[&]() {
128
            // If fragment is finished, fragment context will be de-constructed with all tasks in it.
129
3.11M
            if (done || !status.ok()) {
130
819k
                auto id = task->pipeline_id();
131
819k
                close_task(task.get(), status, fragment_context.get());
132
819k
                task->set_running(false);
133
819k
                fragment_context->decrement_running_task(id);
134
2.29M
            } else {
135
2.29M
                task->set_running(false);
136
2.29M
            }
137
3.11M
            _task_queue.update_statistics(task.get(), exec_ns);
138
3.11M
        }};
139
3.90M
        bool canceled = fragment_context->is_canceled();
140
141
        // Close task if canceled
142
3.90M
        if (canceled) {
143
1.25k
            status = fragment_context->get_query_ctx()->exec_status();
144
1.25k
            DCHECK(!status.ok());
145
1.25k
            continue;
146
1.25k
        }
147
148
        // Main logics of execution
149
3.90M
        ASSIGN_STATUS_IF_CATCH_EXCEPTION(status = task->execute(&done), status);
150
3.11M
        fragment_context->trigger_report_if_necessary();
151
3.11M
    }
152
3.01k
}
153
154
111
void TaskScheduler::stop() {
155
111
    if (!_shutdown) {
156
81
        _task_queue.close();
157
81
        if (_fix_thread_pool) {
158
30
            _need_to_stop = true;
159
30
            _fix_thread_pool->shutdown();
160
30
            _fix_thread_pool->wait();
161
30
        }
162
        // Should set at the ending of the stop to ensure that the
163
        // pool is stopped. For example, if there are 2 threads call stop
164
        // then if one thread set shutdown = false, then another thread will
165
        // not check it and will free task scheduler.
166
81
        _shutdown = true;
167
81
    }
168
111
}
169
170
3.12M
Status HybridTaskScheduler::submit(PipelineTaskSPtr task) {
171
3.12M
    if (task->is_blockable()) {
172
9.76k
        return _blocking_scheduler.submit(task);
173
3.11M
    } else {
174
3.11M
        return _simple_scheduler.submit(task);
175
3.11M
    }
176
3.12M
}
177
178
34
Status HybridTaskScheduler::start() {
179
34
    RETURN_IF_ERROR(_blocking_scheduler.start());
180
34
    RETURN_IF_ERROR(_simple_scheduler.start());
181
34
    return Status::OK();
182
34
}
183
184
15
void HybridTaskScheduler::stop() {
185
15
    _blocking_scheduler.stop();
186
15
    _simple_scheduler.stop();
187
15
}
188
189
} // namespace doris