Coverage Report

Created: 2026-07-06 12:17

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