Coverage Report

Created: 2026-04-10 04:10

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
71
TaskScheduler::~TaskScheduler() {
53
71
    stop();
54
71
    LOG(INFO) << "Task scheduler " << _name << " shutdown";
55
71
}
56
57
74
Status TaskScheduler::start() {
58
74
    RETURN_IF_ERROR(ThreadPoolBuilder(_name)
59
74
                            .set_min_threads(_num_threads)
60
74
                            .set_max_threads(_num_threads)
61
74
                            .set_max_queue_size(0)
62
74
                            .set_cgroup_cpu_ctl(_cgroup_cpu_ctl)
63
74
                            .build(&_fix_thread_pool));
64
74
    LOG_INFO("TaskScheduler set cores").tag("size", _num_threads);
65
3.38k
    for (int32_t i = 0; i < _num_threads; ++i) {
66
3.31k
        RETURN_IF_ERROR(_fix_thread_pool->submit_func([this, i] { _do_work(i); }));
67
3.31k
    }
68
74
    return Status::OK();
69
74
}
70
71
5.64M
Status TaskScheduler::submit(PipelineTaskSPtr task) {
72
5.64M
    return _task_queue.push_back(task);
73
5.64M
}
74
75
// after close_task, task maybe destructed.
76
1.57M
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
1.57M
    SCOPED_ATTACH_TASK(task->runtime_state());
81
1.57M
    if (!exec_status.ok()) {
82
3.68k
        ctx->cancel(exec_status);
83
3.68k
        LOG(WARNING) << fmt::format("Pipeline task failed. query_id: {} reason: {}",
84
3.68k
                                    print_id(ctx->get_query_id()), exec_status.to_string());
85
3.68k
    }
86
1.57M
    Status status = task->close(exec_status);
87
1.57M
    if (!status.ok()) {
88
1.34k
        ctx->cancel(status);
89
1.34k
    }
90
1.57M
    status = task->finalize();
91
1.57M
    if (!status.ok()) {
92
0
        ctx->cancel(status);
93
0
    }
94
1.57M
}
95
96
3.29k
void TaskScheduler::_do_work(int index) {
97
8.70M
    while (!_need_to_stop) {
98
8.70M
        auto task = _task_queue.take(index);
99
8.70M
        if (!task) {
100
1.90M
            continue;
101
1.90M
        }
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
6.80M
        if (task->set_running(true)) {
108
473k
            static_cast<void>(_task_queue.push_back(task, index));
109
473k
            continue;
110
473k
        }
111
112
6.33M
        if (task->is_finalized()) {
113
1
            task->set_running(false);
114
1
            continue;
115
1
        }
116
117
6.33M
        auto fragment_context = task->fragment_context().lock();
118
6.33M
        if (!fragment_context) {
119
            // Fragment already finished
120
0
            task->set_running(false);
121
0
            continue;
122
0
        }
123
124
6.33M
        task->set_thread_id(index);
125
126
6.33M
        bool done = false;
127
6.33M
        auto status = Status::OK();
128
6.33M
        int64_t exec_ns = 0;
129
6.33M
        SCOPED_RAW_TIMER(&exec_ns);
130
6.33M
        Defer task_running_defer {[&]() {
131
            // If fragment is finished, fragment context will be de-constructed with all tasks in it.
132
5.63M
            if (done || !status.ok()) {
133
1.58M
                auto id = task->pipeline_id();
134
1.58M
                close_task(task.get(), status, fragment_context.get());
135
1.58M
                task->set_running(false);
136
1.58M
                fragment_context->decrement_running_task(id);
137
4.05M
            } else {
138
4.05M
                task->set_running(false);
139
4.05M
            }
140
5.63M
            _task_queue.update_statistics(task.get(), exec_ns);
141
5.63M
        }};
142
6.33M
        bool canceled = fragment_context->is_canceled();
143
144
        // Close task if canceled
145
6.33M
        if (canceled) {
146
2.40k
            status = fragment_context->get_query_ctx()->exec_status();
147
2.40k
            DCHECK(!status.ok());
148
2.40k
            continue;
149
2.40k
        }
150
151
        // Main logics of execution
152
6.32M
        ASSIGN_STATUS_IF_CATCH_EXCEPTION(
153
                //TODO: use a better enclose to abstracting these
154
6.32M
                if (ExecEnv::GetInstance()->pipeline_tracer_context()->enabled()) {
155
6.32M
                    TUniqueId query_id = fragment_context->get_query_id();
156
6.32M
                    std::string task_name = task->task_name();
157
158
6.32M
                    std::thread::id tid = std::this_thread::get_id();
159
6.32M
                    uint64_t thread_id = *reinterpret_cast<uint64_t*>(&tid);
160
6.32M
                    uint64_t start_time = MonotonicMicros();
161
162
6.32M
                    status = task->execute(&done);
163
164
6.32M
                    uint64_t end_time = MonotonicMicros();
165
6.32M
                    ExecEnv::GetInstance()->pipeline_tracer_context()->record(
166
6.32M
                            {query_id, task_name, static_cast<uint32_t>(index), thread_id,
167
6.32M
                             start_time, end_time});
168
6.32M
                } else { status = task->execute(&done); },
169
6.32M
                status);
170
5.63M
        fragment_context->trigger_report_if_necessary();
171
5.63M
    }
172
3.29k
}
173
174
101
void TaskScheduler::stop() {
175
101
    if (!_shutdown) {
176
71
        _task_queue.close();
177
71
        if (_fix_thread_pool) {
178
30
            _need_to_stop = true;
179
30
            _fix_thread_pool->shutdown();
180
30
            _fix_thread_pool->wait();
181
30
        }
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
71
        _shutdown = true;
187
71
    }
188
101
}
189
190
5.64M
Status HybridTaskScheduler::submit(PipelineTaskSPtr task) {
191
5.64M
    if (task->is_blockable()) {
192
6.94k
        return _blocking_scheduler.submit(task);
193
5.63M
    } else {
194
5.63M
        return _simple_scheduler.submit(task);
195
5.63M
    }
196
5.64M
}
197
198
37
Status HybridTaskScheduler::start() {
199
37
    RETURN_IF_ERROR(_blocking_scheduler.start());
200
37
    RETURN_IF_ERROR(_simple_scheduler.start());
201
37
    return Status::OK();
202
37
}
203
204
15
void HybridTaskScheduler::stop() {
205
15
    _blocking_scheduler.stop();
206
15
    _simple_scheduler.stop();
207
15
}
208
209
} // namespace doris