Coverage Report

Created: 2026-07-07 22:17

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/util/blocking_queue.hpp
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/blocking-queue.hpp
19
// and modified by Doris
20
21
#pragma once
22
23
#include <unistd.h>
24
25
#include <atomic>
26
#include <condition_variable>
27
#include <list>
28
#include <mutex>
29
30
#include "common/logging.h"
31
#include "util/stopwatch.hpp"
32
33
namespace doris {
34
// Fixed capacity FIFO queue, where both BlockingGet and BlockingPut operations block
35
// if the queue is empty or full, respectively.
36
template <typename T>
37
class BlockingQueue {
38
public:
39
    BlockingQueue(uint32_t max_elements)
40
76
            : _shutdown(false),
41
76
              _max_elements(max_elements),
42
76
              _total_get_wait_time(0),
43
76
              _total_put_wait_time(0),
44
76
              _get_waiting(0),
45
76
              _put_waiting(0) {}
_ZN5doris13BlockingQueueINS_14WorkThreadPoolILb0EE4TaskEEC2Ej
Line
Count
Source
40
56
            : _shutdown(false),
41
56
              _max_elements(max_elements),
42
56
              _total_get_wait_time(0),
43
56
              _total_put_wait_time(0),
44
56
              _get_waiting(0),
45
56
              _put_waiting(0) {}
_ZN5doris13BlockingQueueIiEC2Ej
Line
Count
Source
40
4
            : _shutdown(false),
41
4
              _max_elements(max_elements),
42
4
              _total_get_wait_time(0),
43
4
              _total_put_wait_time(0),
44
4
              _get_waiting(0),
45
4
              _put_waiting(0) {}
_ZN5doris13BlockingQueueIlEC2Ej
Line
Count
Source
40
2
            : _shutdown(false),
41
2
              _max_elements(max_elements),
42
2
              _total_get_wait_time(0),
43
2
              _total_put_wait_time(0),
44
2
              _get_waiting(0),
45
2
              _put_waiting(0) {}
_ZN5doris13BlockingQueueISt10shared_ptrIN5arrow11RecordBatchEEEC2Ej
Line
Count
Source
40
12
            : _shutdown(false),
41
12
              _max_elements(max_elements),
42
12
              _total_get_wait_time(0),
43
12
              _total_put_wait_time(0),
44
12
              _get_waiting(0),
45
12
              _put_waiting(0) {}
_ZN5doris13BlockingQueueIPN7RdKafka7MessageEEC2Ej
Line
Count
Source
40
2
            : _shutdown(false),
41
2
              _max_elements(max_elements),
42
2
              _total_get_wait_time(0),
43
2
              _total_put_wait_time(0),
44
2
              _get_waiting(0),
45
2
              _put_waiting(0) {}
Unexecuted instantiation: _ZN5doris13BlockingQueueISt10shared_ptrIN3Aws7Kinesis5Model6RecordEEEC2Ej
46
47
    // Get an element from the queue, waiting indefinitely for one to become available.
48
    // Returns false if we were shut down prior to getting the element, and there
49
    // are no more elements available.
50
119k
    bool blocking_get(T* out) { return controlled_blocking_get(out, MAX_CV_WAIT_TIMEOUT_MS); }
_ZN5doris13BlockingQueueINS_14WorkThreadPoolILb0EE4TaskEE12blocking_getEPS3_
Line
Count
Source
50
284
    bool blocking_get(T* out) { return controlled_blocking_get(out, MAX_CV_WAIT_TIMEOUT_MS); }
_ZN5doris13BlockingQueueIiE12blocking_getEPi
Line
Count
Source
50
119k
    bool blocking_get(T* out) { return controlled_blocking_get(out, MAX_CV_WAIT_TIMEOUT_MS); }
_ZN5doris13BlockingQueueIlE12blocking_getEPl
Line
Count
Source
50
4
    bool blocking_get(T* out) { return controlled_blocking_get(out, MAX_CV_WAIT_TIMEOUT_MS); }
_ZN5doris13BlockingQueueISt10shared_ptrIN5arrow11RecordBatchEEE12blocking_getEPS4_
Line
Count
Source
50
4
    bool blocking_get(T* out) { return controlled_blocking_get(out, MAX_CV_WAIT_TIMEOUT_MS); }
_ZN5doris13BlockingQueueIPN7RdKafka7MessageEE12blocking_getEPS3_
Line
Count
Source
50
2
    bool blocking_get(T* out) { return controlled_blocking_get(out, MAX_CV_WAIT_TIMEOUT_MS); }
Unexecuted instantiation: _ZN5doris13BlockingQueueISt10shared_ptrIN3Aws7Kinesis5Model6RecordEEE12blocking_getEPS6_
51
52
    // Blocking_get and blocking_put may cause deadlock,
53
    // but we still don't find root cause,
54
    // introduce condition variable wait timeout to avoid blocking queue deadlock temporarily.
55
119k
    bool controlled_blocking_get(T* out, int64_t cv_wait_timeout_ms) {
56
119k
        MonotonicStopWatch timer;
57
119k
        timer.start();
58
119k
        std::unique_lock<std::mutex> unique_lock(_lock);
59
119k
        while (!(_shutdown || !_list.empty())) {
60
296
            ++_get_waiting;
61
296
            if (_get_cv.wait_for(unique_lock, std::chrono::milliseconds(cv_wait_timeout_ms)) ==
62
296
                std::cv_status::timeout) {
63
10
                _get_waiting--;
64
10
            }
65
296
        }
66
119k
        _total_get_wait_time += timer.elapsed_time();
67
68
119k
        if (!_list.empty()) {
69
100k
            *out = _list.front();
70
100k
            _list.pop_front();
71
100k
            if (_put_waiting > 0) {
72
0
                --_put_waiting;
73
0
                unique_lock.unlock();
74
0
                _put_cv.notify_one();
75
0
            }
76
100k
            return true;
77
100k
        } else {
78
19.3k
            assert(_shutdown);
79
20.2k
            return false;
80
19.3k
        }
81
119k
    }
_ZN5doris13BlockingQueueINS_14WorkThreadPoolILb0EE4TaskEE23controlled_blocking_getEPS3_l
Line
Count
Source
55
284
    bool controlled_blocking_get(T* out, int64_t cv_wait_timeout_ms) {
56
284
        MonotonicStopWatch timer;
57
284
        timer.start();
58
284
        std::unique_lock<std::mutex> unique_lock(_lock);
59
568
        while (!(_shutdown || !_list.empty())) {
60
284
            ++_get_waiting;
61
284
            if (_get_cv.wait_for(unique_lock, std::chrono::milliseconds(cv_wait_timeout_ms)) ==
62
284
                std::cv_status::timeout) {
63
0
                _get_waiting--;
64
0
            }
65
284
        }
66
284
        _total_get_wait_time += timer.elapsed_time();
67
68
284
        if (!_list.empty()) {
69
60
            *out = _list.front();
70
60
            _list.pop_front();
71
60
            if (_put_waiting > 0) {
72
0
                --_put_waiting;
73
0
                unique_lock.unlock();
74
0
                _put_cv.notify_one();
75
0
            }
76
60
            return true;
77
224
        } else {
78
224
            assert(_shutdown);
79
224
            return false;
80
224
        }
81
284
    }
_ZN5doris13BlockingQueueIiE23controlled_blocking_getEPil
Line
Count
Source
55
119k
    bool controlled_blocking_get(T* out, int64_t cv_wait_timeout_ms) {
56
119k
        MonotonicStopWatch timer;
57
119k
        timer.start();
58
119k
        std::unique_lock<std::mutex> unique_lock(_lock);
59
119k
        while (!(_shutdown || !_list.empty())) {
60
0
            ++_get_waiting;
61
0
            if (_get_cv.wait_for(unique_lock, std::chrono::milliseconds(cv_wait_timeout_ms)) ==
62
0
                std::cv_status::timeout) {
63
0
                _get_waiting--;
64
0
            }
65
0
        }
66
119k
        _total_get_wait_time += timer.elapsed_time();
67
68
119k
        if (!_list.empty()) {
69
100k
            *out = _list.front();
70
100k
            _list.pop_front();
71
100k
            if (_put_waiting > 0) {
72
0
                --_put_waiting;
73
0
                unique_lock.unlock();
74
0
                _put_cv.notify_one();
75
0
            }
76
100k
            return true;
77
100k
        } else {
78
19.1k
            assert(_shutdown);
79
20.0k
            return false;
80
19.1k
        }
81
119k
    }
_ZN5doris13BlockingQueueIlE23controlled_blocking_getEPll
Line
Count
Source
55
4
    bool controlled_blocking_get(T* out, int64_t cv_wait_timeout_ms) {
56
4
        MonotonicStopWatch timer;
57
4
        timer.start();
58
4
        std::unique_lock<std::mutex> unique_lock(_lock);
59
4
        while (!(_shutdown || !_list.empty())) {
60
0
            ++_get_waiting;
61
0
            if (_get_cv.wait_for(unique_lock, std::chrono::milliseconds(cv_wait_timeout_ms)) ==
62
0
                std::cv_status::timeout) {
63
0
                _get_waiting--;
64
0
            }
65
0
        }
66
4
        _total_get_wait_time += timer.elapsed_time();
67
68
4
        if (!_list.empty()) {
69
2
            *out = _list.front();
70
2
            _list.pop_front();
71
2
            if (_put_waiting > 0) {
72
0
                --_put_waiting;
73
0
                unique_lock.unlock();
74
0
                _put_cv.notify_one();
75
0
            }
76
2
            return true;
77
2
        } else {
78
2
            assert(_shutdown);
79
2
            return false;
80
2
        }
81
4
    }
_ZN5doris13BlockingQueueISt10shared_ptrIN5arrow11RecordBatchEEE23controlled_blocking_getEPS4_l
Line
Count
Source
55
4
    bool controlled_blocking_get(T* out, int64_t cv_wait_timeout_ms) {
56
4
        MonotonicStopWatch timer;
57
4
        timer.start();
58
4
        std::unique_lock<std::mutex> unique_lock(_lock);
59
4
        while (!(_shutdown || !_list.empty())) {
60
0
            ++_get_waiting;
61
0
            if (_get_cv.wait_for(unique_lock, std::chrono::milliseconds(cv_wait_timeout_ms)) ==
62
0
                std::cv_status::timeout) {
63
0
                _get_waiting--;
64
0
            }
65
0
        }
66
4
        _total_get_wait_time += timer.elapsed_time();
67
68
4
        if (!_list.empty()) {
69
4
            *out = _list.front();
70
4
            _list.pop_front();
71
4
            if (_put_waiting > 0) {
72
0
                --_put_waiting;
73
0
                unique_lock.unlock();
74
0
                _put_cv.notify_one();
75
0
            }
76
4
            return true;
77
4
        } else {
78
0
            assert(_shutdown);
79
0
            return false;
80
0
        }
81
4
    }
_ZN5doris13BlockingQueueIPN7RdKafka7MessageEE23controlled_blocking_getEPS3_l
Line
Count
Source
55
4
    bool controlled_blocking_get(T* out, int64_t cv_wait_timeout_ms) {
56
4
        MonotonicStopWatch timer;
57
4
        timer.start();
58
4
        std::unique_lock<std::mutex> unique_lock(_lock);
59
16
        while (!(_shutdown || !_list.empty())) {
60
12
            ++_get_waiting;
61
12
            if (_get_cv.wait_for(unique_lock, std::chrono::milliseconds(cv_wait_timeout_ms)) ==
62
12
                std::cv_status::timeout) {
63
10
                _get_waiting--;
64
10
            }
65
12
        }
66
4
        _total_get_wait_time += timer.elapsed_time();
67
68
4
        if (!_list.empty()) {
69
0
            *out = _list.front();
70
0
            _list.pop_front();
71
0
            if (_put_waiting > 0) {
72
0
                --_put_waiting;
73
0
                unique_lock.unlock();
74
0
                _put_cv.notify_one();
75
0
            }
76
0
            return true;
77
4
        } else {
78
4
            assert(_shutdown);
79
4
            return false;
80
4
        }
81
4
    }
Unexecuted instantiation: _ZN5doris13BlockingQueueISt10shared_ptrIN3Aws7Kinesis5Model6RecordEEE23controlled_blocking_getEPS6_l
82
83
    // Puts an element into the queue, waiting indefinitely until there is space.
84
    // If the queue is shut down, returns false.
85
99.8k
    bool blocking_put(const T& val) { return controlled_blocking_put(val, MAX_CV_WAIT_TIMEOUT_MS); }
Unexecuted instantiation: _ZN5doris13BlockingQueueINS_14WorkThreadPoolILb0EE4TaskEE12blocking_putERKS3_
_ZN5doris13BlockingQueueISt10shared_ptrIN5arrow11RecordBatchEEE12blocking_putERKS4_
Line
Count
Source
85
8
    bool blocking_put(const T& val) { return controlled_blocking_put(val, MAX_CV_WAIT_TIMEOUT_MS); }
_ZN5doris13BlockingQueueIiE12blocking_putERKi
Line
Count
Source
85
99.8k
    bool blocking_put(const T& val) { return controlled_blocking_put(val, MAX_CV_WAIT_TIMEOUT_MS); }
_ZN5doris13BlockingQueueIlE12blocking_putERKl
Line
Count
Source
85
4
    bool blocking_put(const T& val) { return controlled_blocking_put(val, MAX_CV_WAIT_TIMEOUT_MS); }
86
87
    // Blocking_get and blocking_put may cause deadlock,
88
    // but we still don't find root cause,
89
    // introduce condition variable wait timeout to avoid blocking queue deadlock temporarily.
90
99.6k
    bool controlled_blocking_put(const T& val, int64_t cv_wait_timeout_ms) {
91
99.6k
        MonotonicStopWatch timer;
92
99.6k
        timer.start();
93
99.6k
        std::unique_lock<std::mutex> unique_lock(_lock);
94
100k
        while (!(_shutdown || _list.size() < _max_elements)) {
95
0
            ++_put_waiting;
96
0
            if (_put_cv.wait_for(unique_lock, std::chrono::milliseconds(cv_wait_timeout_ms)) ==
97
0
                std::cv_status::timeout) {
98
0
                _put_waiting--;
99
0
            }
100
0
        }
101
99.6k
        _total_put_wait_time += timer.elapsed_time();
102
103
99.6k
        if (_shutdown) {
104
2
            return false;
105
2
        }
106
107
99.6k
        _list.push_back(val);
108
99.6k
        if (_get_waiting > 0) {
109
0
            --_get_waiting;
110
0
            unique_lock.unlock();
111
0
            _get_cv.notify_one();
112
0
        }
113
99.6k
        return true;
114
99.6k
    }
Unexecuted instantiation: _ZN5doris13BlockingQueueINS_14WorkThreadPoolILb0EE4TaskEE23controlled_blocking_putERKS3_l
_ZN5doris13BlockingQueueISt10shared_ptrIN5arrow11RecordBatchEEE23controlled_blocking_putERKS4_l
Line
Count
Source
90
8
    bool controlled_blocking_put(const T& val, int64_t cv_wait_timeout_ms) {
91
8
        MonotonicStopWatch timer;
92
8
        timer.start();
93
8
        std::unique_lock<std::mutex> unique_lock(_lock);
94
8
        while (!(_shutdown || _list.size() < _max_elements)) {
95
0
            ++_put_waiting;
96
0
            if (_put_cv.wait_for(unique_lock, std::chrono::milliseconds(cv_wait_timeout_ms)) ==
97
0
                std::cv_status::timeout) {
98
0
                _put_waiting--;
99
0
            }
100
0
        }
101
8
        _total_put_wait_time += timer.elapsed_time();
102
103
8
        if (_shutdown) {
104
0
            return false;
105
0
        }
106
107
8
        _list.push_back(val);
108
8
        if (_get_waiting > 0) {
109
0
            --_get_waiting;
110
0
            unique_lock.unlock();
111
0
            _get_cv.notify_one();
112
0
        }
113
8
        return true;
114
8
    }
_ZN5doris13BlockingQueueIiE23controlled_blocking_putERKil
Line
Count
Source
90
99.6k
    bool controlled_blocking_put(const T& val, int64_t cv_wait_timeout_ms) {
91
99.6k
        MonotonicStopWatch timer;
92
99.6k
        timer.start();
93
99.6k
        std::unique_lock<std::mutex> unique_lock(_lock);
94
100k
        while (!(_shutdown || _list.size() < _max_elements)) {
95
0
            ++_put_waiting;
96
0
            if (_put_cv.wait_for(unique_lock, std::chrono::milliseconds(cv_wait_timeout_ms)) ==
97
0
                std::cv_status::timeout) {
98
0
                _put_waiting--;
99
0
            }
100
0
        }
101
99.6k
        _total_put_wait_time += timer.elapsed_time();
102
103
99.6k
        if (_shutdown) {
104
0
            return false;
105
0
        }
106
107
99.6k
        _list.push_back(val);
108
99.6k
        if (_get_waiting > 0) {
109
0
            --_get_waiting;
110
0
            unique_lock.unlock();
111
0
            _get_cv.notify_one();
112
0
        }
113
99.6k
        return true;
114
99.6k
    }
_ZN5doris13BlockingQueueIlE23controlled_blocking_putERKll
Line
Count
Source
90
4
    bool controlled_blocking_put(const T& val, int64_t cv_wait_timeout_ms) {
91
4
        MonotonicStopWatch timer;
92
4
        timer.start();
93
4
        std::unique_lock<std::mutex> unique_lock(_lock);
94
4
        while (!(_shutdown || _list.size() < _max_elements)) {
95
0
            ++_put_waiting;
96
0
            if (_put_cv.wait_for(unique_lock, std::chrono::milliseconds(cv_wait_timeout_ms)) ==
97
0
                std::cv_status::timeout) {
98
0
                _put_waiting--;
99
0
            }
100
0
        }
101
4
        _total_put_wait_time += timer.elapsed_time();
102
103
4
        if (_shutdown) {
104
2
            return false;
105
2
        }
106
107
2
        _list.push_back(val);
108
2
        if (_get_waiting > 0) {
109
0
            --_get_waiting;
110
0
            unique_lock.unlock();
111
0
            _get_cv.notify_one();
112
0
        }
113
2
        return true;
114
4
    }
Unexecuted instantiation: _ZN5doris13BlockingQueueIPN7RdKafka7MessageEE23controlled_blocking_putERKS3_l
Unexecuted instantiation: _ZN5doris13BlockingQueueISt10shared_ptrIN3Aws7Kinesis5Model6RecordEEE23controlled_blocking_putERKS6_l
115
116
    // Return false if queue full or has been shutdown.
117
60
    bool try_put(const T& val) {
118
60
        if (_shutdown || _list.size() >= _max_elements) {
119
0
            return false;
120
0
        }
121
122
60
        MonotonicStopWatch timer;
123
60
        timer.start();
124
60
        std::unique_lock<std::mutex> unique_lock(_lock);
125
60
        _total_put_wait_time += timer.elapsed_time();
126
127
60
        if (_shutdown || _list.size() >= _max_elements) {
128
0
            return false;
129
0
        }
130
131
60
        _list.push_back(val);
132
60
        if (_get_waiting > 0) {
133
60
            --_get_waiting;
134
60
            unique_lock.unlock();
135
60
            _get_cv.notify_one();
136
60
        }
137
60
        return true;
138
60
    }
139
140
    // Shut down the queue. Wakes up all threads waiting on BlockingGet or BlockingPut.
141
70
    void shutdown() {
142
70
        {
143
70
            std::lock_guard<std::mutex> guard(_lock);
144
70
            _shutdown = true;
145
70
        }
146
147
70
        _get_cv.notify_all();
148
70
        _put_cv.notify_all();
149
70
    }
_ZN5doris13BlockingQueueINS_14WorkThreadPoolILb0EE4TaskEE8shutdownEv
Line
Count
Source
141
56
    void shutdown() {
142
56
        {
143
56
            std::lock_guard<std::mutex> guard(_lock);
144
56
            _shutdown = true;
145
56
        }
146
147
56
        _get_cv.notify_all();
148
56
        _put_cv.notify_all();
149
56
    }
_ZN5doris13BlockingQueueIlE8shutdownEv
Line
Count
Source
141
2
    void shutdown() {
142
2
        {
143
2
            std::lock_guard<std::mutex> guard(_lock);
144
2
            _shutdown = true;
145
2
        }
146
147
2
        _get_cv.notify_all();
148
2
        _put_cv.notify_all();
149
2
    }
_ZN5doris13BlockingQueueIiE8shutdownEv
Line
Count
Source
141
2
    void shutdown() {
142
2
        {
143
2
            std::lock_guard<std::mutex> guard(_lock);
144
2
            _shutdown = true;
145
2
        }
146
147
2
        _get_cv.notify_all();
148
2
        _put_cv.notify_all();
149
2
    }
_ZN5doris13BlockingQueueISt10shared_ptrIN5arrow11RecordBatchEEE8shutdownEv
Line
Count
Source
141
4
    void shutdown() {
142
4
        {
143
4
            std::lock_guard<std::mutex> guard(_lock);
144
4
            _shutdown = true;
145
4
        }
146
147
4
        _get_cv.notify_all();
148
4
        _put_cv.notify_all();
149
4
    }
_ZN5doris13BlockingQueueIPN7RdKafka7MessageEE8shutdownEv
Line
Count
Source
141
6
    void shutdown() {
142
6
        {
143
6
            std::lock_guard<std::mutex> guard(_lock);
144
6
            _shutdown = true;
145
6
        }
146
147
6
        _get_cv.notify_all();
148
6
        _put_cv.notify_all();
149
6
    }
Unexecuted instantiation: _ZN5doris13BlockingQueueISt10shared_ptrIN3Aws7Kinesis5Model6RecordEEE8shutdownEv
150
151
733
    uint32_t get_size() const {
152
733
        std::lock_guard<std::mutex> l(_lock);
153
733
        return static_cast<uint32_t>(_list.size());
154
733
    }
_ZNK5doris13BlockingQueueINS_14WorkThreadPoolILb0EE4TaskEE8get_sizeEv
Line
Count
Source
151
731
    uint32_t get_size() const {
152
731
        std::lock_guard<std::mutex> l(_lock);
153
731
        return static_cast<uint32_t>(_list.size());
154
731
    }
Unexecuted instantiation: _ZNK5doris13BlockingQueueISt10shared_ptrIN5arrow11RecordBatchEEE8get_sizeEv
_ZNK5doris13BlockingQueueIPN7RdKafka7MessageEE8get_sizeEv
Line
Count
Source
151
2
    uint32_t get_size() const {
152
2
        std::lock_guard<std::mutex> l(_lock);
153
2
        return static_cast<uint32_t>(_list.size());
154
2
    }
Unexecuted instantiation: _ZNK5doris13BlockingQueueISt10shared_ptrIN3Aws7Kinesis5Model6RecordEEE8get_sizeEv
155
156
448
    uint32_t get_capacity() const { return _max_elements; }
157
158
    // Returns the total amount of time threads have blocked in BlockingGet.
159
447
    uint64_t total_get_wait_time() const { return _total_get_wait_time; }
160
161
    // Returns the total amount of time threads have blocked in BlockingPut.
162
447
    uint64_t total_put_wait_time() const { return _total_put_wait_time; }
163
164
private:
165
    static constexpr int64_t MAX_CV_WAIT_TIMEOUT_MS = 60 * 60 * 1000; // 1 hour
166
    bool _shutdown;
167
    const int _max_elements;
168
    std::condition_variable _get_cv; // 'get' callers wait on this
169
    std::condition_variable _put_cv; // 'put' callers wait on this
170
    // _lock guards access to _list, total_get_wait_time, and total_put_wait_time
171
    mutable std::mutex _lock;
172
    std::list<T> _list;
173
    std::atomic<uint64_t> _total_get_wait_time;
174
    std::atomic<uint64_t> _total_put_wait_time;
175
    size_t _get_waiting;
176
    size_t _put_waiting;
177
};
178
} // namespace doris