Coverage Report

Created: 2026-04-10 04:05

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