Coverage Report

Created: 2025-09-12 10:59

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/root/doris/be/src/util/threadpool.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
// This file is copied from
18
// https://github.com/apache/impala/blob/branch-2.9.0/be/src/util/threadpool.cc
19
// and modified by Doris
20
21
#include "util/threadpool.h"
22
23
#include <algorithm>
24
#include <cstdint>
25
#include <limits>
26
#include <ostream>
27
#include <thread>
28
#include <utility>
29
30
#include "absl/strings/substitute.h"
31
#include "common/exception.h"
32
#include "common/logging.h"
33
#include "util/debug_points.h"
34
#include "util/doris_metrics.h"
35
#include "util/metrics.h"
36
#include "util/stopwatch.hpp"
37
#include "util/thread.h"
38
39
namespace doris {
40
// The name of these varialbs will be useds as metric name in prometheus.
41
DEFINE_GAUGE_METRIC_PROTOTYPE_2ARG(thread_pool_active_threads, MetricUnit::NOUNIT);
42
DEFINE_GAUGE_METRIC_PROTOTYPE_2ARG(thread_pool_queue_size, MetricUnit::NOUNIT);
43
DEFINE_GAUGE_METRIC_PROTOTYPE_2ARG(thread_pool_max_queue_size, MetricUnit::NOUNIT);
44
DEFINE_GAUGE_METRIC_PROTOTYPE_2ARG(thread_pool_max_threads, MetricUnit::NOUNIT);
45
DEFINE_COUNTER_METRIC_PROTOTYPE_2ARG(thread_pool_submit_failed, MetricUnit::NOUNIT);
46
DEFINE_COUNTER_METRIC_PROTOTYPE_2ARG(thread_pool_task_execution_time_ns_total,
47
                                     MetricUnit::NANOSECONDS);
48
DEFINE_COUNTER_METRIC_PROTOTYPE_2ARG(thread_pool_task_execution_count_total, MetricUnit::NOUNIT);
49
DEFINE_COUNTER_METRIC_PROTOTYPE_2ARG(thread_pool_task_wait_worker_time_ns_total,
50
                                     MetricUnit::NANOSECONDS);
51
DEFINE_COUNTER_METRIC_PROTOTYPE_2ARG(thread_pool_task_wait_worker_count_total, MetricUnit::NOUNIT);
52
using namespace ErrorCode;
53
54
using std::string;
55
56
class FunctionRunnable : public Runnable {
57
public:
58
11.1M
    explicit FunctionRunnable(std::function<void()> func) : _func(std::move(func)) {}
59
60
11.1M
    void run() override { _func(); }
61
62
private:
63
    std::function<void()> _func;
64
};
65
66
ThreadPoolBuilder::ThreadPoolBuilder(string name, string workload_group)
67
721
        : _name(std::move(name)),
68
721
          _workload_group(std::move(workload_group)),
69
721
          _min_threads(0),
70
721
          _max_threads(std::thread::hardware_concurrency()),
71
721
          _max_queue_size(std::numeric_limits<int>::max()),
72
721
          _idle_timeout(std::chrono::milliseconds(500)) {}
73
74
674
ThreadPoolBuilder& ThreadPoolBuilder::set_min_threads(int min_threads) {
75
674
    CHECK_GE(min_threads, 0);
76
674
    _min_threads = min_threads;
77
674
    return *this;
78
674
}
79
80
693
ThreadPoolBuilder& ThreadPoolBuilder::set_max_threads(int max_threads) {
81
693
    CHECK_GT(max_threads, 0);
82
693
    _max_threads = max_threads;
83
693
    return *this;
84
693
}
85
86
172
ThreadPoolBuilder& ThreadPoolBuilder::set_max_queue_size(int max_queue_size) {
87
172
    _max_queue_size = max_queue_size;
88
172
    return *this;
89
172
}
90
91
ThreadPoolBuilder& ThreadPoolBuilder::set_cgroup_cpu_ctl(
92
128
        std::weak_ptr<CgroupCpuCtl> cgroup_cpu_ctl) {
93
128
    _cgroup_cpu_ctl = cgroup_cpu_ctl;
94
128
    return *this;
95
128
}
96
97
ThreadPoolToken::ThreadPoolToken(ThreadPool* pool, ThreadPool::ExecutionMode mode,
98
                                 int max_concurrency)
99
1.42M
        : _mode(mode),
100
1.42M
          _pool(pool),
101
1.42M
          _state(State::IDLE),
102
1.42M
          _active_threads(0),
103
1.42M
          _max_concurrency(max_concurrency),
104
1.42M
          _num_submitted_tasks(0),
105
1.42M
          _num_unsubmitted_tasks(0) {
106
1.42M
    if (max_concurrency == 1 && mode != ThreadPool::ExecutionMode::SERIAL) {
107
65.0k
        _mode = ThreadPool::ExecutionMode::SERIAL;
108
65.0k
    }
109
1.42M
}
110
111
1.42M
ThreadPoolToken::~ThreadPoolToken() {
112
1.42M
    shutdown();
113
1.42M
    _pool->release_token(this);
114
1.42M
}
115
116
2.24M
Status ThreadPoolToken::submit(std::shared_ptr<Runnable> r) {
117
2.24M
    return _pool->do_submit(std::move(r), this);
118
2.24M
}
119
120
2.24M
Status ThreadPoolToken::submit_func(std::function<void()> f) {
121
2.24M
    return submit(std::make_shared<FunctionRunnable>(std::move(f)));
122
2.24M
}
123
124
2.18M
void ThreadPoolToken::shutdown() {
125
2.18M
    std::unique_lock<std::mutex> l(_pool->_lock);
126
2.18M
    _pool->check_not_pool_thread_unlocked();
127
128
    // Clear the queue under the lock, but defer the releasing of the tasks
129
    // outside the lock, in case there are concurrent threads wanting to access
130
    // the ThreadPool. The task's destructors may acquire locks, etc, so this
131
    // also prevents lock inversions.
132
2.18M
    std::deque<ThreadPool::Task> to_release = std::move(_entries);
133
2.18M
    _pool->_total_queued_tasks -= to_release.size();
134
135
2.18M
    switch (state()) {
136
1.42M
    case State::IDLE:
137
        // There were no tasks outstanding; we can quiesce the token immediately.
138
1.42M
        transition(State::QUIESCED);
139
1.42M
        break;
140
851
    case State::RUNNING:
141
        // There were outstanding tasks. If any are still running, switch to
142
        // QUIESCING and wait for them to finish (the worker thread executing
143
        // the token's last task will switch the token to QUIESCED). Otherwise,
144
        // we can quiesce the token immediately.
145
146
        // Note: this is an O(n) operation, but it's expected to be infrequent.
147
        // Plus doing it this way (rather than switching to QUIESCING and waiting
148
        // for a worker thread to process the queue entry) helps retain state
149
        // transition symmetry with ThreadPool::shutdown.
150
3.59k
        for (auto it = _pool->_queue.begin(); it != _pool->_queue.end();) {
151
2.74k
            if (*it == this) {
152
267
                it = _pool->_queue.erase(it);
153
2.48k
            } else {
154
2.48k
                it++;
155
2.48k
            }
156
2.74k
        }
157
158
851
        if (_active_threads == 0) {
159
134
            transition(State::QUIESCED);
160
134
            break;
161
134
        }
162
717
        transition(State::QUIESCING);
163
717
        [[fallthrough]];
164
728
    case State::QUIESCING:
165
        // The token is already quiescing. Just wait for a worker thread to
166
        // switch it to QUIESCED.
167
1.45k
        _not_running_cond.wait(l, [this]() { return state() == State::QUIESCED; });
168
728
        break;
169
758k
    default:
170
758k
        break;
171
2.18M
    }
172
2.18M
}
173
174
870k
void ThreadPoolToken::wait() {
175
870k
    std::unique_lock<std::mutex> l(_pool->_lock);
176
870k
    _pool->check_not_pool_thread_unlocked();
177
957k
    _not_running_cond.wait(l, [this]() { return !is_active(); });
178
870k
}
179
180
4.80M
void ThreadPoolToken::transition(State new_state) {
181
4.80M
#ifndef NDEBUG
182
4.80M
    CHECK_NE(_state, new_state);
183
184
4.80M
    switch (_state) {
185
3.11M
    case State::IDLE:
186
3.11M
        CHECK(new_state == State::RUNNING || new_state == State::QUIESCED);
187
3.11M
        if (new_state == State::RUNNING) {
188
1.68M
            CHECK(!_entries.empty());
189
1.68M
        } else {
190
1.42M
            CHECK(_entries.empty());
191
1.42M
            CHECK_EQ(_active_threads, 0);
192
1.42M
        }
193
3.11M
        break;
194
1.68M
    case State::RUNNING:
195
1.68M
        CHECK(new_state == State::IDLE || new_state == State::QUIESCING ||
196
1.68M
              new_state == State::QUIESCED);
197
1.68M
        CHECK(_entries.empty());
198
1.68M
        if (new_state == State::QUIESCING) {
199
752
            CHECK_GT(_active_threads, 0);
200
752
        }
201
1.68M
        break;
202
752
    case State::QUIESCING:
203
752
        CHECK(new_state == State::QUIESCED);
204
752
        CHECK_EQ(_active_threads, 0);
205
752
        break;
206
0
    case State::QUIESCED:
207
0
        CHECK(false); // QUIESCED is a terminal state
208
0
        break;
209
0
    default:
210
0
        throw Exception(Status::FatalError("Unknown token state: {}", _state));
211
4.80M
    }
212
4.80M
#endif
213
214
    // Take actions based on the state we're entering.
215
4.80M
    switch (new_state) {
216
1.68M
    case State::IDLE:
217
3.11M
    case State::QUIESCED:
218
3.11M
        _not_running_cond.notify_all();
219
3.11M
        break;
220
1.68M
    default:
221
1.68M
        break;
222
4.80M
    }
223
224
4.80M
    _state = new_state;
225
4.80M
}
226
227
0
const char* ThreadPoolToken::state_to_string(State s) {
228
0
    switch (s) {
229
0
    case State::IDLE:
230
0
        return "IDLE";
231
0
        break;
232
0
    case State::RUNNING:
233
0
        return "RUNNING";
234
0
        break;
235
0
    case State::QUIESCING:
236
0
        return "QUIESCING";
237
0
        break;
238
0
    case State::QUIESCED:
239
0
        return "QUIESCED";
240
0
        break;
241
0
    }
242
0
    return "<cannot reach here>";
243
0
}
244
245
11.2M
bool ThreadPoolToken::need_dispatch() {
246
11.2M
    return _state == ThreadPoolToken::State::IDLE ||
247
11.2M
           (_mode == ThreadPool::ExecutionMode::CONCURRENT &&
248
9.56M
            _num_submitted_tasks < _max_concurrency);
249
11.2M
}
250
251
ThreadPool::ThreadPool(const ThreadPoolBuilder& builder)
252
681
        : _name(builder._name),
253
681
          _workload_group(builder._workload_group),
254
681
          _min_threads(builder._min_threads),
255
681
          _max_threads(builder._max_threads),
256
681
          _max_queue_size(builder._max_queue_size),
257
681
          _idle_timeout(builder._idle_timeout),
258
681
          _pool_status(Status::Uninitialized("The pool was not initialized.")),
259
681
          _num_threads(0),
260
681
          _num_threads_pending_start(0),
261
681
          _active_threads(0),
262
681
          _total_queued_tasks(0),
263
681
          _cgroup_cpu_ctl(builder._cgroup_cpu_ctl),
264
681
          _tokenless(new_token(ExecutionMode::CONCURRENT)),
265
681
          _id(UniqueId::gen_uid()) {}
266
267
412
ThreadPool::~ThreadPool() {
268
    // There should only be one live token: the one used in tokenless submission.
269
412
    CHECK_EQ(1, _tokens.size()) << absl::Substitute(
270
0
            "Threadpool $0 destroyed with $1 allocated tokens", _name, _tokens.size());
271
412
    shutdown();
272
412
}
273
274
686
Status ThreadPool::try_create_thread(int thread_num, std::lock_guard<std::mutex>&) {
275
10.0k
    for (int i = 0; i < thread_num; i++) {
276
9.37k
        Status status = create_thread();
277
9.37k
        if (status.ok()) {
278
9.37k
            _num_threads_pending_start++;
279
9.37k
        } else {
280
0
            LOG(WARNING) << "Thread pool " << _name << " failed to create thread: " << status;
281
0
            return status;
282
0
        }
283
9.37k
    }
284
686
    return Status::OK();
285
686
}
286
287
681
Status ThreadPool::init() {
288
681
    if (!_pool_status.is<UNINITIALIZED>()) {
289
0
        return Status::NotSupported("The thread pool {} is already initialized", _name);
290
0
    }
291
681
    _pool_status = Status::OK();
292
293
681
    {
294
681
        std::lock_guard<std::mutex> l(_lock);
295
        // create thread failed should not cause threadpool init failed,
296
        // because thread can be created later such as when submit a task.
297
681
        static_cast<void>(try_create_thread(_min_threads, l));
298
681
    }
299
300
    // _id of thread pool is used to make sure when we create thread pool with same name, we can
301
    // get different _metric_entity
302
    // If not, we will have problem when we deregister entity and register hook.
303
681
    _metric_entity = DorisMetrics::instance()->metric_registry()->register_entity(
304
681
            fmt::format("thread_pool_{}", _name), {{"thread_pool_name", _name},
305
681
                                                   {"workload_group", _workload_group},
306
681
                                                   {"id", _id.to_string()}});
307
308
681
    INT_GAUGE_METRIC_REGISTER(_metric_entity, thread_pool_active_threads);
309
681
    INT_GAUGE_METRIC_REGISTER(_metric_entity, thread_pool_max_threads);
310
681
    INT_GAUGE_METRIC_REGISTER(_metric_entity, thread_pool_queue_size);
311
681
    INT_GAUGE_METRIC_REGISTER(_metric_entity, thread_pool_max_queue_size);
312
681
    INT_COUNTER_METRIC_REGISTER(_metric_entity, thread_pool_task_execution_time_ns_total);
313
681
    INT_COUNTER_METRIC_REGISTER(_metric_entity, thread_pool_task_execution_count_total);
314
681
    INT_COUNTER_METRIC_REGISTER(_metric_entity, thread_pool_task_wait_worker_time_ns_total);
315
681
    INT_COUNTER_METRIC_REGISTER(_metric_entity, thread_pool_task_wait_worker_count_total);
316
681
    INT_COUNTER_METRIC_REGISTER(_metric_entity, thread_pool_submit_failed);
317
318
57.5k
    _metric_entity->register_hook("update", [this]() {
319
57.5k
        {
320
57.5k
            std::lock_guard<std::mutex> l(_lock);
321
57.5k
            if (!_pool_status.ok()) {
322
0
                return;
323
0
            }
324
57.5k
        }
325
326
57.5k
        thread_pool_active_threads->set_value(num_active_threads());
327
57.5k
        thread_pool_queue_size->set_value(get_queue_size());
328
57.5k
        thread_pool_max_queue_size->set_value(get_max_queue_size());
329
57.5k
        thread_pool_max_threads->set_value(max_threads());
330
57.5k
    });
331
681
    return Status::OK();
332
681
}
333
334
721
void ThreadPool::shutdown() {
335
    // Why access to doris_metrics is safe here?
336
    // Since DorisMetrics is a singleton, it will be destroyed only after doris_main is exited.
337
    // The shutdown/destroy of ThreadPool is guaranteed to take place before doris_main exits by
338
    // ExecEnv::destroy().
339
721
    DorisMetrics::instance()->metric_registry()->deregister_entity(_metric_entity);
340
721
    std::unique_lock<std::mutex> l(_lock);
341
721
    check_not_pool_thread_unlocked();
342
343
    // Note: this is the same error seen at submission if the pool is at
344
    // capacity, so clients can't tell them apart. This isn't really a practical
345
    // concern though because shutting down a pool typically requires clients to
346
    // be quiesced first, so there's no danger of a client getting confused.
347
    // Not print stack trace here
348
721
    _pool_status = Status::Error<SERVICE_UNAVAILABLE, false>(
349
721
            "The thread pool {} has been shut down.", _name);
350
351
    // Clear the various queues under the lock, but defer the releasing
352
    // of the tasks outside the lock, in case there are concurrent threads
353
    // wanting to access the ThreadPool. The task's destructors may acquire
354
    // locks, etc, so this also prevents lock inversions.
355
721
    _queue.clear();
356
357
721
    std::deque<std::deque<Task>> to_release;
358
797
    for (auto* t : _tokens) {
359
797
        if (!t->_entries.empty()) {
360
16
            to_release.emplace_back(std::move(t->_entries));
361
16
        }
362
797
        switch (t->state()) {
363
424
        case ThreadPoolToken::State::IDLE:
364
            // The token is idle; we can quiesce it immediately.
365
424
            t->transition(ThreadPoolToken::State::QUIESCED);
366
424
            break;
367
46
        case ThreadPoolToken::State::RUNNING:
368
            // The token has tasks associated with it. If they're merely queued
369
            // (i.e. there are no active threads), the tasks will have been removed
370
            // above and we can quiesce immediately. Otherwise, we need to wait for
371
            // the threads to finish.
372
46
            t->transition(t->_active_threads > 0 ? ThreadPoolToken::State::QUIESCING
373
46
                                                 : ThreadPoolToken::State::QUIESCED);
374
46
            break;
375
327
        default:
376
327
            break;
377
797
        }
378
797
    }
379
380
    // The queues are empty. Wake any sleeping worker threads and wait for all
381
    // of them to exit. Some worker threads will exit immediately upon waking,
382
    // while others will exit after they finish executing an outstanding task.
383
721
    _total_queued_tasks = 0;
384
3.86k
    while (!_idle_threads.empty()) {
385
3.13k
        _idle_threads.front().not_empty.notify_one();
386
3.13k
        _idle_threads.pop_front();
387
3.13k
    }
388
389
1.10k
    _no_threads_cond.wait(l, [this]() { return _num_threads + _num_threads_pending_start == 0; });
390
391
    // All the threads have exited. Check the state of each token.
392
797
    for (auto* t : _tokens) {
393
797
        DCHECK(t->state() == ThreadPoolToken::State::IDLE ||
394
797
               t->state() == ThreadPoolToken::State::QUIESCED);
395
797
    }
396
721
}
397
398
1.42M
std::unique_ptr<ThreadPoolToken> ThreadPool::new_token(ExecutionMode mode, int max_concurrency) {
399
1.42M
    std::lock_guard<std::mutex> l(_lock);
400
1.42M
    std::unique_ptr<ThreadPoolToken> t(new ThreadPoolToken(this, mode, max_concurrency));
401
1.42M
    if (!_tokens.insert(t.get()).second) {
402
0
        throw Exception(Status::InternalError("duplicate token"));
403
0
    }
404
1.42M
    return t;
405
1.42M
}
406
407
1.42M
void ThreadPool::release_token(ThreadPoolToken* t) {
408
1.42M
    std::lock_guard<std::mutex> l(_lock);
409
18.4E
    CHECK(!t->is_active()) << absl::Substitute("Token with state $0 may not be released",
410
18.4E
                                               ThreadPoolToken::state_to_string(t->state()));
411
1.42M
    CHECK_EQ(1, _tokens.erase(t));
412
1.42M
}
413
414
8.99M
Status ThreadPool::submit(std::shared_ptr<Runnable> r) {
415
8.99M
    return do_submit(std::move(r), _tokenless.get());
416
8.99M
}
417
418
8.85M
Status ThreadPool::submit_func(std::function<void()> f) {
419
8.85M
    return submit(std::make_shared<FunctionRunnable>(std::move(f)));
420
8.85M
}
421
422
11.2M
Status ThreadPool::do_submit(std::shared_ptr<Runnable> r, ThreadPoolToken* token) {
423
11.2M
    DCHECK(token);
424
425
11.2M
    std::unique_lock<std::mutex> l(_lock);
426
11.2M
    if (!_pool_status.ok()) [[unlikely]] {
427
1
        return _pool_status;
428
1
    }
429
430
11.2M
    if (!token->may_submit_new_tasks()) [[unlikely]] {
431
2.44k
        return Status::Error<SERVICE_UNAVAILABLE>("Thread pool({}) token was shut down", _name);
432
2.44k
    }
433
434
    // Size limit check.
435
11.2M
    int64_t capacity_remaining = static_cast<int64_t>(_max_threads) - _active_threads +
436
11.2M
                                 static_cast<int64_t>(_max_queue_size) - _total_queued_tasks;
437
11.2M
    if (capacity_remaining < 1) {
438
4
        thread_pool_submit_failed->increment(1);
439
4
        return Status::Error<SERVICE_UNAVAILABLE>(
440
4
                "Thread pool {} is at capacity ({}/{} tasks running, {}/{} tasks queued)", _name,
441
4
                _num_threads + _num_threads_pending_start, _max_threads, _total_queued_tasks,
442
4
                _max_queue_size);
443
4
    }
444
445
    // Should we create another thread?
446
447
    // We assume that each current inactive thread will grab one item from the
448
    // queue.  If it seems like we'll need another thread, we create one.
449
    //
450
    // Rather than creating the thread here, while holding the lock, we defer
451
    // it to down below. This is because thread creation can be rather slow
452
    // (hundreds of milliseconds in some cases) and we'd like to allow the
453
    // existing threads to continue to process tasks while we do so.
454
    //
455
    // In theory, a currently active thread could finish immediately after this
456
    // calculation but before our new worker starts running. This would mean we
457
    // created a thread we didn't really need. However, this race is unavoidable
458
    // and harmless.
459
    //
460
    // Of course, we never create more than _max_threads threads no matter what.
461
11.2M
    int threads_from_this_submit =
462
11.2M
            token->is_active() && token->mode() == ExecutionMode::SERIAL ? 0 : 1;
463
11.2M
    int inactive_threads = _num_threads + _num_threads_pending_start - _active_threads;
464
11.2M
    int additional_threads =
465
11.2M
            static_cast<int>(_queue.size()) + threads_from_this_submit - inactive_threads;
466
11.2M
    bool need_a_thread = false;
467
11.2M
    if (additional_threads > 0 && _num_threads + _num_threads_pending_start < _max_threads) {
468
48.0k
        need_a_thread = true;
469
48.0k
        _num_threads_pending_start++;
470
48.0k
    }
471
472
11.2M
    Task task;
473
11.2M
    task.runnable = std::move(r);
474
11.2M
    task.submit_time_wather.start();
475
476
    // Add the task to the token's queue.
477
11.2M
    ThreadPoolToken::State state = token->state();
478
11.2M
    DCHECK(state == ThreadPoolToken::State::IDLE || state == ThreadPoolToken::State::RUNNING);
479
11.2M
    token->_entries.emplace_back(std::move(task));
480
    // When we need to execute the task in the token, we submit the token object to the queue.
481
    // There are currently two places where tokens will be submitted to the queue:
482
    // 1. When submitting a new task, if the token is still in the IDLE state,
483
    //    or the concurrency of the token has not reached the online level, it will be added to the queue.
484
    // 2. When the dispatch thread finishes executing a task:
485
    //    1. If it is a SERIAL token, and there are unsubmitted tasks, submit them to the queue.
486
    //    2. If it is a CONCURRENT token, and there are still unsubmitted tasks, and the upper limit of concurrency is not reached,
487
    //       then submitted to the queue.
488
11.2M
    if (token->need_dispatch()) {
489
10.2M
        _queue.emplace_back(token);
490
10.2M
        ++token->_num_submitted_tasks;
491
10.2M
        if (state == ThreadPoolToken::State::IDLE) {
492
1.68M
            token->transition(ThreadPoolToken::State::RUNNING);
493
1.68M
        }
494
10.2M
    } else {
495
973k
        ++token->_num_unsubmitted_tasks;
496
973k
    }
497
11.2M
    _total_queued_tasks++;
498
499
    // Wake up an idle thread for this task. Choosing the thread at the front of
500
    // the list ensures LIFO semantics as idling threads are also added to the front.
501
    //
502
    // If there are no idle threads, the new task remains on the queue and is
503
    // processed by an active thread (or a thread we're about to create) at some
504
    // point in the future.
505
11.2M
    if (!_idle_threads.empty()) {
506
7.48M
        _idle_threads.front().not_empty.notify_one();
507
7.48M
        _idle_threads.pop_front();
508
7.48M
    }
509
11.2M
    l.unlock();
510
511
11.2M
    if (need_a_thread) {
512
48.0k
        Status status = create_thread();
513
48.0k
        if (!status.ok()) {
514
0
            l.lock();
515
0
            _num_threads_pending_start--;
516
0
            if (_num_threads + _num_threads_pending_start == 0) {
517
                // If we have no threads, we can't do any work.
518
0
                return status;
519
0
            }
520
            // If we failed to create a thread, but there are still some other
521
            // worker threads, log a warning message and continue.
522
0
            LOG(WARNING) << "Thread pool " << _name
523
0
                         << " failed to create thread: " << status.to_string();
524
0
        }
525
48.0k
    }
526
527
11.2M
    return Status::OK();
528
11.2M
}
529
530
138
void ThreadPool::wait() {
531
138
    std::unique_lock<std::mutex> l(_lock);
532
138
    check_not_pool_thread_unlocked();
533
237
    _idle_cond.wait(l, [this]() { return _total_queued_tasks == 0 && _active_threads == 0; });
534
138
}
535
536
57.4k
void ThreadPool::dispatch_thread() {
537
57.4k
    std::unique_lock<std::mutex> l(_lock);
538
57.4k
    if (!_threads.insert(Thread::current_thread()).second) {
539
0
        throw Exception(Status::InternalError("duplicate token"));
540
0
    }
541
57.4k
    DCHECK_GT(_num_threads_pending_start, 0);
542
57.4k
    _num_threads++;
543
57.4k
    _num_threads_pending_start--;
544
545
57.4k
    if (std::shared_ptr<CgroupCpuCtl> cg_cpu_ctl_sptr = _cgroup_cpu_ctl.lock()) {
546
0
        static_cast<void>(cg_cpu_ctl_sptr->add_thread_to_cgroup());
547
0
    }
548
549
    // Owned by this worker thread and added/removed from _idle_threads as needed.
550
57.4k
    IdleThread me;
551
552
39.8M
    while (true) {
553
        // Note: Status::Aborted() is used to indicate normal shutdown.
554
39.8M
        if (!_pool_status.ok()) {
555
3.71k
            VLOG_CRITICAL << "DispatchThread exiting: " << _pool_status.to_string();
556
3.71k
            break;
557
3.71k
        }
558
559
39.8M
        if (_num_threads + _num_threads_pending_start > _max_threads) {
560
50
            break;
561
50
        }
562
563
39.8M
        if (_queue.empty()) {
564
            // There's no work to do, let's go idle.
565
            //
566
            // Note: if FIFO behavior is desired, it's as simple as changing this to push_back().
567
28.5M
            _idle_threads.push_front(me);
568
28.5M
            Defer defer = [&] {
569
                // For some wake ups (i.e. shutdown or do_submit) this thread is
570
                // guaranteed to be unlinked after being awakened. In others (i.e.
571
                // spurious wake-up or Wait timeout), it'll still be linked.
572
28.5M
                if (me.is_linked()) {
573
21.1M
                    _idle_threads.erase(_idle_threads.iterator_to(me));
574
21.1M
                }
575
28.5M
            };
576
28.5M
            if (me.not_empty.wait_for(l, _idle_timeout) == std::cv_status::timeout) {
577
                // After much investigation, it appears that pthread condition variables have
578
                // a weird behavior in which they can return ETIMEDOUT from timed_wait even if
579
                // another thread did in fact signal. Apparently after a timeout there is some
580
                // brief period during which another thread may actually grab the internal mutex
581
                // protecting the state, signal, and release again before we get the mutex. So,
582
                // we'll recheck the empty queue case regardless.
583
21.1M
                if (_queue.empty() && _num_threads + _num_threads_pending_start > _min_threads) {
584
47.9k
                    VLOG_NOTICE << "Releasing worker thread from pool " << _name << " after "
585
0
                                << std::chrono::duration_cast<std::chrono::milliseconds>(
586
0
                                           _idle_timeout)
587
0
                                           .count()
588
0
                                << "ms of idle time.";
589
47.9k
                    break;
590
47.9k
                }
591
21.1M
            }
592
28.5M
            continue;
593
28.5M
        }
594
595
11.2M
        MonotonicStopWatch task_execution_time_watch;
596
11.2M
        task_execution_time_watch.start();
597
        // Get the next token and task to execute.
598
11.2M
        ThreadPoolToken* token = _queue.front();
599
11.2M
        _queue.pop_front();
600
11.2M
        DCHECK_EQ(ThreadPoolToken::State::RUNNING, token->state());
601
11.2M
        DCHECK(!token->_entries.empty());
602
11.2M
        Task task = std::move(token->_entries.front());
603
11.2M
        thread_pool_task_wait_worker_time_ns_total->increment(
604
11.2M
                task.submit_time_wather.elapsed_time());
605
11.2M
        thread_pool_task_wait_worker_count_total->increment(1);
606
11.2M
        token->_entries.pop_front();
607
11.2M
        token->_active_threads++;
608
11.2M
        --_total_queued_tasks;
609
11.2M
        ++_active_threads;
610
611
11.2M
        l.unlock();
612
613
        // Execute the task
614
11.2M
        task.runnable->run();
615
        // Destruct the task while we do not hold the lock.
616
        //
617
        // The task's destructor may be expensive if it has a lot of bound
618
        // objects, and we don't want to block submission of the threadpool.
619
        // In the worst case, the destructor might even try to do something
620
        // with this threadpool, and produce a deadlock.
621
11.2M
        task.runnable.reset();
622
11.2M
        l.lock();
623
11.2M
        thread_pool_task_execution_time_ns_total->increment(
624
11.2M
                task_execution_time_watch.elapsed_time());
625
11.2M
        thread_pool_task_execution_count_total->increment(1);
626
        // Possible states:
627
        // 1. The token was shut down while we ran its task. Transition to QUIESCED.
628
        // 2. The token has no more queued tasks. Transition back to IDLE.
629
        // 3. The token has more tasks. Requeue it and transition back to RUNNABLE.
630
11.2M
        ThreadPoolToken::State state = token->state();
631
11.2M
        DCHECK(state == ThreadPoolToken::State::RUNNING ||
632
11.2M
               state == ThreadPoolToken::State::QUIESCING);
633
11.2M
        --token->_active_threads;
634
11.2M
        --token->_num_submitted_tasks;
635
636
        // handle shutdown && idle
637
11.2M
        if (token->_active_threads == 0) {
638
2.68M
            if (state == ThreadPoolToken::State::QUIESCING) {
639
752
                DCHECK(token->_entries.empty());
640
752
                token->transition(ThreadPoolToken::State::QUIESCED);
641
2.68M
            } else if (token->_entries.empty()) {
642
1.68M
                token->transition(ThreadPoolToken::State::IDLE);
643
1.68M
            }
644
2.68M
        }
645
646
        // We decrease _num_submitted_tasks holding lock, so the following DCHECK works.
647
11.2M
        DCHECK(token->_num_submitted_tasks < token->_max_concurrency);
648
649
        // If token->state is running and there are unsubmitted tasks in the token, we put
650
        // the token back.
651
11.2M
        if (token->_num_unsubmitted_tasks > 0 && state == ThreadPoolToken::State::RUNNING) {
652
            // SERIAL: if _entries is not empty, then num_unsubmitted_tasks must be greater than 0.
653
            // CONCURRENT: we have to check _num_unsubmitted_tasks because there may be at least 2
654
            // threads are running for the token.
655
983k
            _queue.emplace_back(token);
656
983k
            ++token->_num_submitted_tasks;
657
983k
            --token->_num_unsubmitted_tasks;
658
983k
        }
659
660
11.2M
        if (--_active_threads == 0) {
661
1.66M
            _idle_cond.notify_all();
662
1.66M
        }
663
11.2M
    }
664
665
    // It's important that we hold the lock between exiting the loop and dropping
666
    // _num_threads. Otherwise it's possible someone else could come along here
667
    // and add a new task just as the last running thread is about to exit.
668
57.4k
    CHECK(l.owns_lock());
669
670
57.4k
    CHECK_EQ(_threads.erase(Thread::current_thread()), 1);
671
57.4k
    _num_threads--;
672
57.4k
    if (_num_threads + _num_threads_pending_start == 0) {
673
2.02k
        _no_threads_cond.notify_all();
674
675
        // Sanity check: if we're the last thread exiting, the queue ought to be
676
        // empty. Otherwise it will never get processed.
677
2.02k
        CHECK(_queue.empty());
678
2.02k
        DCHECK_EQ(0, _total_queued_tasks);
679
2.02k
    }
680
57.4k
}
681
682
57.4k
Status ThreadPool::create_thread() {
683
57.4k
    return Thread::create("thread pool", absl::Substitute("$0 [worker]", _name),
684
57.4k
                          &ThreadPool::dispatch_thread, this, nullptr);
685
57.4k
}
686
687
3.05M
void ThreadPool::check_not_pool_thread_unlocked() {
688
3.05M
    Thread* current = Thread::current_thread();
689
3.05M
    if (_threads.contains(current)) {
690
0
        throw Exception(
691
0
                Status::FatalError("Thread belonging to thread pool {} with "
692
0
                                   "name {} called pool function that would result in deadlock",
693
0
                                   _name, current->name()));
694
0
    }
695
3.05M
}
696
697
6
Status ThreadPool::set_min_threads(int min_threads) {
698
6
    std::lock_guard<std::mutex> l(_lock);
699
6
    if (min_threads > _max_threads) {
700
        // min threads can not be set greater than max threads
701
1
        return Status::InternalError("set thread pool {} min_threads failed", _name);
702
1
    }
703
5
    _min_threads = min_threads;
704
5
    if (min_threads > _num_threads + _num_threads_pending_start) {
705
0
        int addition_threads = min_threads - _num_threads - _num_threads_pending_start;
706
0
        RETURN_IF_ERROR(try_create_thread(addition_threads, l));
707
0
    }
708
5
    return Status::OK();
709
5
}
710
711
10
Status ThreadPool::set_max_threads(int max_threads) {
712
10
    std::lock_guard<std::mutex> l(_lock);
713
10
    DBUG_EXECUTE_IF("ThreadPool.set_max_threads.force_set", {
714
10
        _max_threads = max_threads;
715
10
        return Status::OK();
716
10
    })
717
10
    if (_min_threads > max_threads) {
718
        // max threads can not be set less than min threads
719
1
        return Status::InternalError("set thread pool {} max_threads failed", _name);
720
1
    }
721
722
9
    _max_threads = max_threads;
723
9
    if (_max_threads > _num_threads + _num_threads_pending_start) {
724
5
        int addition_threads = _max_threads - _num_threads - _num_threads_pending_start;
725
5
        addition_threads = std::min(addition_threads, _total_queued_tasks);
726
5
        RETURN_IF_ERROR(try_create_thread(addition_threads, l));
727
5
    }
728
9
    return Status::OK();
729
9
}
730
731
0
std::ostream& operator<<(std::ostream& o, ThreadPoolToken::State s) {
732
0
    return o << ThreadPoolToken::state_to_string(s);
733
0
}
734
735
} // namespace doris