Coverage Report

Created: 2026-06-30 15:29

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