Coverage Report

Created: 2026-07-23 18:52

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
#ifdef BE_TEST
34
#include "cpp/sync_point.h"
35
#endif
36
37
namespace doris {
38
// Fixed capacity FIFO queue, where both BlockingGet and BlockingPut operations block
39
// if the queue is empty or full, respectively.
40
template <typename T>
41
class BlockingQueue {
42
public:
43
    BlockingQueue(uint32_t max_elements)
44
138
            : _shutdown(false),
45
138
              _max_elements(max_elements),
46
138
              _total_get_wait_time(0),
47
138
              _total_put_wait_time(0),
48
138
              _get_waiting(0),
49
138
              _put_waiting(0) {}
_ZN5doris13BlockingQueueISt10shared_ptrIN5arrow11RecordBatchEEEC2Ej
Line
Count
Source
44
9
            : _shutdown(false),
45
9
              _max_elements(max_elements),
46
9
              _total_get_wait_time(0),
47
9
              _total_put_wait_time(0),
48
9
              _get_waiting(0),
49
9
              _put_waiting(0) {}
_ZN5doris13BlockingQueueINS_14WorkThreadPoolILb0EE4TaskEEC2Ej
Line
Count
Source
44
56
            : _shutdown(false),
45
56
              _max_elements(max_elements),
46
56
              _total_get_wait_time(0),
47
56
              _total_put_wait_time(0),
48
56
              _get_waiting(0),
49
56
              _put_waiting(0) {}
_ZN5doris13BlockingQueueIPN7RdKafka7MessageEEC2Ej
Line
Count
Source
44
73
            : _shutdown(false),
45
73
              _max_elements(max_elements),
46
73
              _total_get_wait_time(0),
47
73
              _total_put_wait_time(0),
48
73
              _get_waiting(0),
49
73
              _put_waiting(0) {}
Unexecuted instantiation: _ZN5doris13BlockingQueueISt10shared_ptrIN3Aws7Kinesis5Model6RecordEEEC2Ej
50
51
    // Get an element from the queue, waiting indefinitely for one to become available.
52
    // Returns false if we were shut down prior to getting the element, and there
53
    // are no more elements available.
54
887k
    bool blocking_get(T* out) { return controlled_blocking_get(out, MAX_CV_WAIT_TIMEOUT_MS); }
_ZN5doris13BlockingQueueISt10shared_ptrIN5arrow11RecordBatchEEE12blocking_getEPS4_
Line
Count
Source
54
7
    bool blocking_get(T* out) { return controlled_blocking_get(out, MAX_CV_WAIT_TIMEOUT_MS); }
_ZN5doris13BlockingQueueINS_14WorkThreadPoolILb0EE4TaskEE12blocking_getEPS3_
Line
Count
Source
54
887k
    bool blocking_get(T* out) { return controlled_blocking_get(out, MAX_CV_WAIT_TIMEOUT_MS); }
_ZN5doris13BlockingQueueIPN7RdKafka7MessageEE12blocking_getEPS3_
Line
Count
Source
54
73
    bool blocking_get(T* out) { return controlled_blocking_get(out, MAX_CV_WAIT_TIMEOUT_MS); }
Unexecuted instantiation: _ZN5doris13BlockingQueueISt10shared_ptrIN3Aws7Kinesis5Model6RecordEEE12blocking_getEPS6_
55
56
    // Blocking_get and blocking_put may cause deadlock,
57
    // but we still don't find root cause,
58
    // introduce condition variable wait timeout to avoid blocking queue deadlock temporarily.
59
885k
    bool controlled_blocking_get(T* out, int64_t cv_wait_timeout_ms) {
60
885k
        MonotonicStopWatch timer;
61
885k
        timer.start();
62
885k
        std::unique_lock<std::mutex> unique_lock(_lock);
63
#ifdef BE_TEST
64
        TEST_SYNC_POINT("BlockingQueue::controlled_blocking_get::after_lock");
65
#endif
66
1.77M
        while (!(_shutdown || !_list.empty())) {
67
891k
            ++_get_waiting;
68
#ifdef BE_TEST
69
            TEST_SYNC_POINT("BlockingQueue::controlled_blocking_get::before_wait");
70
#endif
71
891k
            _get_cv.wait_for(unique_lock, std::chrono::milliseconds(cv_wait_timeout_ms));
72
891k
            DCHECK_GT(_get_waiting, 0);
73
891k
            --_get_waiting;
74
891k
        }
75
885k
        _total_get_wait_time += timer.elapsed_time();
76
77
885k
        if (!_list.empty()) {
78
884k
            *out = _list.front();
79
884k
            _list.pop_front();
80
884k
            const bool has_put_waiter = _put_waiting > 0;
81
884k
            unique_lock.unlock();
82
884k
            if (has_put_waiter) {
83
0
                _put_cv.notify_one();
84
0
            }
85
884k
            return true;
86
884k
        } else {
87
909
            assert(_shutdown);
88
2.75k
            return false;
89
909
        }
90
885k
    }
_ZN5doris13BlockingQueueISt10shared_ptrIN5arrow11RecordBatchEEE23controlled_blocking_getEPS4_l
Line
Count
Source
59
7
    bool controlled_blocking_get(T* out, int64_t cv_wait_timeout_ms) {
60
7
        MonotonicStopWatch timer;
61
7
        timer.start();
62
7
        std::unique_lock<std::mutex> unique_lock(_lock);
63
#ifdef BE_TEST
64
        TEST_SYNC_POINT("BlockingQueue::controlled_blocking_get::after_lock");
65
#endif
66
10
        while (!(_shutdown || !_list.empty())) {
67
3
            ++_get_waiting;
68
#ifdef BE_TEST
69
            TEST_SYNC_POINT("BlockingQueue::controlled_blocking_get::before_wait");
70
#endif
71
3
            _get_cv.wait_for(unique_lock, std::chrono::milliseconds(cv_wait_timeout_ms));
72
3
            DCHECK_GT(_get_waiting, 0);
73
3
            --_get_waiting;
74
3
        }
75
7
        _total_get_wait_time += timer.elapsed_time();
76
77
7
        if (!_list.empty()) {
78
7
            *out = _list.front();
79
7
            _list.pop_front();
80
7
            const bool has_put_waiter = _put_waiting > 0;
81
7
            unique_lock.unlock();
82
7
            if (has_put_waiter) {
83
0
                _put_cv.notify_one();
84
0
            }
85
7
            return true;
86
7
        } else {
87
0
            assert(_shutdown);
88
0
            return false;
89
0
        }
90
7
    }
_ZN5doris13BlockingQueueINS_14WorkThreadPoolILb0EE4TaskEE23controlled_blocking_getEPS3_l
Line
Count
Source
59
884k
    bool controlled_blocking_get(T* out, int64_t cv_wait_timeout_ms) {
60
884k
        MonotonicStopWatch timer;
61
884k
        timer.start();
62
884k
        std::unique_lock<std::mutex> unique_lock(_lock);
63
#ifdef BE_TEST
64
        TEST_SYNC_POINT("BlockingQueue::controlled_blocking_get::after_lock");
65
#endif
66
1.77M
        while (!(_shutdown || !_list.empty())) {
67
890k
            ++_get_waiting;
68
#ifdef BE_TEST
69
            TEST_SYNC_POINT("BlockingQueue::controlled_blocking_get::before_wait");
70
#endif
71
890k
            _get_cv.wait_for(unique_lock, std::chrono::milliseconds(cv_wait_timeout_ms));
72
890k
            DCHECK_GT(_get_waiting, 0);
73
890k
            --_get_waiting;
74
890k
        }
75
884k
        _total_get_wait_time += timer.elapsed_time();
76
77
884k
        if (!_list.empty()) {
78
883k
            *out = _list.front();
79
883k
            _list.pop_front();
80
883k
            const bool has_put_waiter = _put_waiting > 0;
81
883k
            unique_lock.unlock();
82
883k
            if (has_put_waiter) {
83
0
                _put_cv.notify_one();
84
0
            }
85
883k
            return true;
86
883k
        } else {
87
763
            assert(_shutdown);
88
2.60k
            return false;
89
763
        }
90
884k
    }
_ZN5doris13BlockingQueueIPN7RdKafka7MessageEE23controlled_blocking_getEPS3_l
Line
Count
Source
59
888
    bool controlled_blocking_get(T* out, int64_t cv_wait_timeout_ms) {
60
888
        MonotonicStopWatch timer;
61
888
        timer.start();
62
888
        std::unique_lock<std::mutex> unique_lock(_lock);
63
#ifdef BE_TEST
64
        TEST_SYNC_POINT("BlockingQueue::controlled_blocking_get::after_lock");
65
#endif
66
1.10k
        while (!(_shutdown || !_list.empty())) {
67
216
            ++_get_waiting;
68
#ifdef BE_TEST
69
            TEST_SYNC_POINT("BlockingQueue::controlled_blocking_get::before_wait");
70
#endif
71
216
            _get_cv.wait_for(unique_lock, std::chrono::milliseconds(cv_wait_timeout_ms));
72
216
            DCHECK_GT(_get_waiting, 0);
73
216
            --_get_waiting;
74
216
        }
75
888
        _total_get_wait_time += timer.elapsed_time();
76
77
888
        if (!_list.empty()) {
78
742
            *out = _list.front();
79
742
            _list.pop_front();
80
742
            const bool has_put_waiter = _put_waiting > 0;
81
742
            unique_lock.unlock();
82
742
            if (has_put_waiter) {
83
0
                _put_cv.notify_one();
84
0
            }
85
742
            return true;
86
742
        } else {
87
146
            assert(_shutdown);
88
146
            return false;
89
146
        }
90
888
    }
Unexecuted instantiation: _ZN5doris13BlockingQueueISt10shared_ptrIN3Aws7Kinesis5Model6RecordEEE23controlled_blocking_getEPS6_l
91
92
    // Puts an element into the queue, waiting indefinitely until there is space.
93
    // If the queue is shut down, returns false.
94
12
    bool blocking_put(const T& val) { return controlled_blocking_put(val, MAX_CV_WAIT_TIMEOUT_MS); }
_ZN5doris13BlockingQueueISt10shared_ptrIN5arrow11RecordBatchEEE12blocking_putERKS4_
Line
Count
Source
94
12
    bool blocking_put(const T& val) { return controlled_blocking_put(val, MAX_CV_WAIT_TIMEOUT_MS); }
Unexecuted instantiation: _ZN5doris13BlockingQueueINS_14WorkThreadPoolILb0EE4TaskEE12blocking_putERKS3_
95
96
    // Blocking_get and blocking_put may cause deadlock,
97
    // but we still don't find root cause,
98
    // introduce condition variable wait timeout to avoid blocking queue deadlock temporarily.
99
754
    bool controlled_blocking_put(const T& val, int64_t cv_wait_timeout_ms) {
100
754
        MonotonicStopWatch timer;
101
754
        timer.start();
102
754
        std::unique_lock<std::mutex> unique_lock(_lock);
103
754
        while (!(_shutdown || _list.size() < _max_elements)) {
104
0
            ++_put_waiting;
105
#ifdef BE_TEST
106
            TEST_SYNC_POINT("BlockingQueue::controlled_blocking_put::before_wait");
107
#endif
108
0
            _put_cv.wait_for(unique_lock, std::chrono::milliseconds(cv_wait_timeout_ms));
109
0
            DCHECK_GT(_put_waiting, 0);
110
0
            --_put_waiting;
111
0
        }
112
754
        _total_put_wait_time += timer.elapsed_time();
113
114
754
        if (_shutdown) {
115
0
            return false;
116
0
        }
117
118
754
        _list.push_back(val);
119
754
        const bool has_get_waiter = _get_waiting > 0;
120
754
        unique_lock.unlock();
121
754
        if (has_get_waiter) {
122
208
            _get_cv.notify_one();
123
208
        }
124
754
        return true;
125
754
    }
_ZN5doris13BlockingQueueISt10shared_ptrIN5arrow11RecordBatchEEE23controlled_blocking_putERKS4_l
Line
Count
Source
99
12
    bool controlled_blocking_put(const T& val, int64_t cv_wait_timeout_ms) {
100
12
        MonotonicStopWatch timer;
101
12
        timer.start();
102
12
        std::unique_lock<std::mutex> unique_lock(_lock);
103
12
        while (!(_shutdown || _list.size() < _max_elements)) {
104
0
            ++_put_waiting;
105
#ifdef BE_TEST
106
            TEST_SYNC_POINT("BlockingQueue::controlled_blocking_put::before_wait");
107
#endif
108
0
            _put_cv.wait_for(unique_lock, std::chrono::milliseconds(cv_wait_timeout_ms));
109
0
            DCHECK_GT(_put_waiting, 0);
110
0
            --_put_waiting;
111
0
        }
112
12
        _total_put_wait_time += timer.elapsed_time();
113
114
12
        if (_shutdown) {
115
0
            return false;
116
0
        }
117
118
12
        _list.push_back(val);
119
12
        const bool has_get_waiter = _get_waiting > 0;
120
12
        unique_lock.unlock();
121
12
        if (has_get_waiter) {
122
3
            _get_cv.notify_one();
123
3
        }
124
12
        return true;
125
12
    }
Unexecuted instantiation: _ZN5doris13BlockingQueueINS_14WorkThreadPoolILb0EE4TaskEE23controlled_blocking_putERKS3_l
_ZN5doris13BlockingQueueIPN7RdKafka7MessageEE23controlled_blocking_putERKS3_l
Line
Count
Source
99
742
    bool controlled_blocking_put(const T& val, int64_t cv_wait_timeout_ms) {
100
742
        MonotonicStopWatch timer;
101
742
        timer.start();
102
742
        std::unique_lock<std::mutex> unique_lock(_lock);
103
742
        while (!(_shutdown || _list.size() < _max_elements)) {
104
0
            ++_put_waiting;
105
#ifdef BE_TEST
106
            TEST_SYNC_POINT("BlockingQueue::controlled_blocking_put::before_wait");
107
#endif
108
0
            _put_cv.wait_for(unique_lock, std::chrono::milliseconds(cv_wait_timeout_ms));
109
0
            DCHECK_GT(_put_waiting, 0);
110
0
            --_put_waiting;
111
0
        }
112
742
        _total_put_wait_time += timer.elapsed_time();
113
114
742
        if (_shutdown) {
115
0
            return false;
116
0
        }
117
118
742
        _list.push_back(val);
119
742
        const bool has_get_waiter = _get_waiting > 0;
120
742
        unique_lock.unlock();
121
742
        if (has_get_waiter) {
122
205
            _get_cv.notify_one();
123
205
        }
124
742
        return true;
125
742
    }
Unexecuted instantiation: _ZN5doris13BlockingQueueISt10shared_ptrIN3Aws7Kinesis5Model6RecordEEE23controlled_blocking_putERKS6_l
126
127
    // Return false if queue full or has been shutdown.
128
882k
    bool try_put(const T& val) {
129
882k
        MonotonicStopWatch timer;
130
882k
        timer.start();
131
882k
        std::unique_lock<std::mutex> unique_lock(_lock);
132
#ifdef BE_TEST
133
        TEST_SYNC_POINT("BlockingQueue::try_put::after_lock");
134
#endif
135
882k
        _total_put_wait_time += timer.elapsed_time();
136
137
883k
        if (_shutdown || _list.size() >= _max_elements) {
138
0
            return false;
139
0
        }
140
141
882k
        _list.push_back(val);
142
882k
        const bool has_get_waiter = _get_waiting > 0;
143
882k
        unique_lock.unlock();
144
883k
        if (has_get_waiter) {
145
883k
            _get_cv.notify_one();
146
883k
        }
147
882k
        return true;
148
882k
    }
149
150
    // Shut down the queue. Wakes up all threads waiting on BlockingGet or BlockingPut.
151
264
    void shutdown() {
152
264
        {
153
264
            std::lock_guard<std::mutex> guard(_lock);
154
264
            _shutdown = true;
155
264
        }
156
157
264
        _get_cv.notify_all();
158
264
        _put_cv.notify_all();
159
264
    }
_ZN5doris13BlockingQueueISt10shared_ptrIN5arrow11RecordBatchEEE8shutdownEv
Line
Count
Source
151
5
    void shutdown() {
152
5
        {
153
5
            std::lock_guard<std::mutex> guard(_lock);
154
5
            _shutdown = true;
155
5
        }
156
157
5
        _get_cv.notify_all();
158
5
        _put_cv.notify_all();
159
5
    }
_ZN5doris13BlockingQueueINS_14WorkThreadPoolILb0EE4TaskEE8shutdownEv
Line
Count
Source
151
40
    void shutdown() {
152
40
        {
153
40
            std::lock_guard<std::mutex> guard(_lock);
154
40
            _shutdown = true;
155
40
        }
156
157
40
        _get_cv.notify_all();
158
40
        _put_cv.notify_all();
159
40
    }
_ZN5doris13BlockingQueueIPN7RdKafka7MessageEE8shutdownEv
Line
Count
Source
151
219
    void shutdown() {
152
219
        {
153
219
            std::lock_guard<std::mutex> guard(_lock);
154
219
            _shutdown = true;
155
219
        }
156
157
219
        _get_cv.notify_all();
158
219
        _put_cv.notify_all();
159
219
    }
Unexecuted instantiation: _ZN5doris13BlockingQueueISt10shared_ptrIN3Aws7Kinesis5Model6RecordEEE8shutdownEv
160
161
896k
    uint32_t get_size() const {
162
896k
        std::lock_guard<std::mutex> l(_lock);
163
896k
        return static_cast<uint32_t>(_list.size());
164
896k
    }
_ZNK5doris13BlockingQueueISt10shared_ptrIN5arrow11RecordBatchEEE8get_sizeEv
Line
Count
Source
161
8
    uint32_t get_size() const {
162
8
        std::lock_guard<std::mutex> l(_lock);
163
8
        return static_cast<uint32_t>(_list.size());
164
8
    }
_ZNK5doris13BlockingQueueINS_14WorkThreadPoolILb0EE4TaskEE8get_sizeEv
Line
Count
Source
161
896k
    uint32_t get_size() const {
162
896k
        std::lock_guard<std::mutex> l(_lock);
163
896k
        return static_cast<uint32_t>(_list.size());
164
896k
    }
_ZNK5doris13BlockingQueueIPN7RdKafka7MessageEE8get_sizeEv
Line
Count
Source
161
73
    uint32_t get_size() const {
162
73
        std::lock_guard<std::mutex> l(_lock);
163
73
        return static_cast<uint32_t>(_list.size());
164
73
    }
Unexecuted instantiation: _ZNK5doris13BlockingQueueISt10shared_ptrIN3Aws7Kinesis5Model6RecordEEE8get_sizeEv
165
166
8.54k
    uint32_t get_capacity() const { return _max_elements; }
167
168
#ifdef BE_TEST
169
    size_t get_waiting_count_for_test() const {
170
        std::lock_guard<std::mutex> guard(_lock);
171
        return _get_waiting;
172
    }
173
174
    size_t put_waiting_count_for_test() const {
175
        std::lock_guard<std::mutex> guard(_lock);
176
        return _put_waiting;
177
    }
178
#endif
179
180
    // Returns the total amount of time threads have blocked in BlockingGet.
181
8.54k
    uint64_t total_get_wait_time() const { return _total_get_wait_time; }
182
183
    // Returns the total amount of time threads have blocked in BlockingPut.
184
8.54k
    uint64_t total_put_wait_time() const { return _total_put_wait_time; }
185
186
private:
187
    static constexpr int64_t MAX_CV_WAIT_TIMEOUT_MS = 60 * 60 * 1000; // 1 hour
188
    bool _shutdown;
189
    const int _max_elements;
190
    std::condition_variable _get_cv; // 'get' callers wait on this
191
    std::condition_variable _put_cv; // 'put' callers wait on this
192
    // _lock guards access to _shutdown, _get_waiting, _put_waiting, _list, total_get_wait_time, and total_put_wait_time
193
    mutable std::mutex _lock;
194
    std::list<T> _list;
195
    std::atomic<uint64_t> _total_get_wait_time;
196
    std::atomic<uint64_t> _total_put_wait_time;
197
    // Number of threads currently inside the corresponding wait_for() call. The waiter that
198
    // increments a counter is also responsible for decrementing it after every wakeup reason.
199
    // Notifiers only inspect these counters and never consume a waiter's registration.
200
    size_t _get_waiting;
201
    size_t _put_waiting;
202
};
203
} // namespace doris