Coverage Report

Created: 2026-06-30 10:25

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
48
TaskScheduler::~TaskScheduler() {
51
48
    stop();
52
48
    LOG(INFO) << "Task scheduler " << _name << " shutdown";
53
48
}
54
55
2
Status TaskScheduler::start() {
56
2
    RETURN_IF_ERROR(ThreadPoolBuilder(_name)
57
2
                            .set_min_threads(_num_threads)
58
2
                            .set_max_threads(_num_threads)
59
2
                            .set_max_queue_size(0)
60
2
                            .set_cgroup_cpu_ctl(_cgroup_cpu_ctl)
61
2
                            .build(&_fix_thread_pool));
62
2
    LOG_INFO("TaskScheduler set cores").tag("size", _num_threads);
63
4
    for (int32_t i = 0; i < _num_threads; ++i) {
64
2
        RETURN_IF_ERROR(_fix_thread_pool->submit_func([this, i] { _do_work(i); }));
65
2
    }
66
2
    return Status::OK();
67
2
}
68
69
1
Status TaskScheduler::submit(PipelineTaskSPtr task) {
70
1
    return _task_queue.push_back(task);
71
1
}
72
73
// after close_task, task maybe destructed.
74
1
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
    SCOPED_ATTACH_TASK(task->runtime_state());
79
1
    if (!exec_status.ok()) {
80
1
        ctx->cancel(exec_status);
81
1
        LOG(WARNING) << fmt::format("Pipeline task failed. query_id: {} reason: {}",
82
1
                                    print_id(ctx->get_query_id()), exec_status.to_string());
83
1
    }
84
1
    Status status = task->close(exec_status);
85
1
    if (!status.ok()) {
86
0
        ctx->cancel(status);
87
0
    }
88
1
    status = task->finalize();
89
1
    if (!status.ok()) {
90
0
        ctx->cancel(status);
91
0
    }
92
1
}
93
94
2
void TaskScheduler::_do_work(int index) {
95
5
    while (!_need_to_stop) {
96
3
        auto task = _task_queue.take(index);
97
3
        if (!task) {
98
2
            continue;
99
2
        }
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
1
        if (task->set_running(true)) {
106
0
            static_cast<void>(_task_queue.push_back(task, index));
107
0
            continue;
108
0
        }
109
110
1
        if (task->is_finalized()) {
111
0
            task->set_running(false);
112
0
            continue;
113
0
        }
114
115
1
        auto fragment_context = task->fragment_context().lock();
116
1
        if (!fragment_context) {
117
            // Fragment already finished
118
0
            task->set_running(false);
119
0
            continue;
120
0
        }
121
122
1
        task->set_thread_id(index);
123
124
1
        bool done = false;
125
1
        auto status = Status::OK();
126
1
        int64_t exec_ns = 0;
127
1
        SCOPED_RAW_TIMER(&exec_ns);
128
1
        Defer task_running_defer {[&]() {
129
            // If fragment is finished, fragment context will be de-constructed with all tasks in it.
130
1
            if (done || !status.ok()) {
131
1
                auto id = task->pipeline_id();
132
1
                close_task(task.get(), status, fragment_context.get());
133
1
                task->set_running(false);
134
1
                fragment_context->decrement_running_task(id);
135
1
            } else {
136
0
                task->set_running(false);
137
0
            }
138
1
            _task_queue.update_statistics(task.get(), exec_ns);
139
1
        }};
140
1
        bool canceled = fragment_context->is_canceled();
141
142
        // Close task if canceled
143
1
        if (canceled) {
144
0
            status = fragment_context->get_query_ctx()->exec_status();
145
0
            DCHECK(!status.ok());
146
0
            continue;
147
0
        }
148
149
        // Main logics of execution
150
1
        try {
151
1
            ASSIGN_STATUS_IF_CATCH_EXCEPTION(status = task->execute(&done), status);
152
1
        } catch (const std::exception& e) {
153
1
            status = Status::InternalError("Catch std::exception: {}", e.what());
154
1
        }
155
1
        fragment_context->trigger_report_if_necessary();
156
1
    }
157
2
}
158
159
54
void TaskScheduler::stop() {
160
54
    if (!_shutdown) {
161
48
        _task_queue.close();
162
48
        if (_fix_thread_pool) {
163
2
            _need_to_stop = true;
164
2
            _fix_thread_pool->shutdown();
165
2
            _fix_thread_pool->wait();
166
2
        }
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
48
        _shutdown = true;
172
48
    }
173
54
}
174
175
3
Status HybridTaskScheduler::submit(PipelineTaskSPtr task) {
176
3
    bool blockable = false;
177
3
    {
178
3
        std::unique_lock<std::mutex> blockable_check_lock(task->_blockable_check_lock);
179
3
        if (!task->_accept_submit) {
180
2
            return Status::OK();
181
2
        }
182
1
        blockable = task->is_blockable();
183
1
    }
184
1
    if (blockable) {
185
0
        return _blocking_scheduler.submit(task);
186
1
    } else {
187
1
        return _simple_scheduler.submit(task);
188
1
    }
189
1
}
190
191
1
Status HybridTaskScheduler::start() {
192
1
    RETURN_IF_ERROR(_blocking_scheduler.start());
193
1
    RETURN_IF_ERROR(_simple_scheduler.start());
194
1
    return Status::OK();
195
1
}
196
197
3
void HybridTaskScheduler::stop() {
198
3
    _blocking_scheduler.stop();
199
3
    _simple_scheduler.stop();
200
3
}
201
202
} // namespace doris