Coverage Report

Created: 2026-10-05 02:06

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
166
            : _shutdown(false),
45
166
              _max_elements(max_elements),
46
166
              _total_get_wait_time(0),
47
166
              _total_put_wait_time(0),
48
166
              _get_waiting(0),
49
166
              _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
92
            : _shutdown(false),
45
92
              _max_elements(max_elements),
46
92
              _total_get_wait_time(0),
47
92
              _total_put_wait_time(0),
48
92
              _get_waiting(0),
49
92
              _put_waiting(0) {}
_ZN5doris13BlockingQueueIPN7RdKafka7MessageEEC2Ej
Line
Count
Source
44
65
            : _shutdown(false),
45
65
              _max_elements(max_elements),
46
65
              _total_get_wait_time(0),
47
65
              _total_put_wait_time(0),
48
65
              _get_waiting(0),
49
65
              _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
697k
    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
697k
    bool blocking_get(T* out) { return controlled_blocking_get(out, MAX_CV_WAIT_TIMEOUT_MS); }
_ZN5doris13BlockingQueueIPN7RdKafka7MessageEE12blocking_getEPS3_
Line
Count
Source
54
65
    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
692k
    bool controlled_blocking_get(T* out, int64_t cv_wait_timeout_ms) {
60
692k
        MonotonicStopWatch timer;
61
692k
        timer.start();
62
692k
        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.39M
        while (!(_shutdown || !_list.empty())) {
67
699k
            ++_get_waiting;
68
#ifdef BE_TEST
69
            TEST_SYNC_POINT("BlockingQueue::controlled_blocking_get::before_wait");
70
#endif
71
699k
            _get_cv.wait_for(unique_lock, std::chrono::milliseconds(cv_wait_timeout_ms));
72
699k
            DCHECK_GT(_get_waiting, 0);
73
699k
            --_get_waiting;
74
699k
        }
75
692k
        _total_get_wait_time += timer.elapsed_time();
76
77
693k
        if (!_list.empty()) {
78
693k
            *out = _list.front();
79
693k
            _list.pop_front();
80
693k
            const bool has_put_waiter = _put_waiting > 0;
81
693k
            unique_lock.unlock();
82
693k
            if (has_put_waiter) {
83
0
                _put_cv.notify_one();
84
0
            }
85
693k
            return true;
86
18.4E
        } else {
87
18.4E
            assert(_shutdown);
88
2.85k
            return false;
89
18.4E
        }
90
692k
    }
_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
9
        while (!(_shutdown || !_list.empty())) {
67
2
            ++_get_waiting;
68
#ifdef BE_TEST
69
            TEST_SYNC_POINT("BlockingQueue::controlled_blocking_get::before_wait");
70
#endif
71
2
            _get_cv.wait_for(unique_lock, std::chrono::milliseconds(cv_wait_timeout_ms));
72
2
            DCHECK_GT(_get_waiting, 0);
73
2
            --_get_waiting;
74
2
        }
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
692k
    bool controlled_blocking_get(T* out, int64_t cv_wait_timeout_ms) {
60
692k
        MonotonicStopWatch timer;
61
692k
        timer.start();
62
692k
        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.39M
        while (!(_shutdown || !_list.empty())) {
67
699k
            ++_get_waiting;
68
#ifdef BE_TEST
69
            TEST_SYNC_POINT("BlockingQueue::controlled_blocking_get::before_wait");
70
#endif
71
699k
            _get_cv.wait_for(unique_lock, std::chrono::milliseconds(cv_wait_timeout_ms));
72
699k
            DCHECK_GT(_get_waiting, 0);
73
699k
            --_get_waiting;
74
699k
        }
75
692k
        _total_get_wait_time += timer.elapsed_time();
76
77
693k
        if (!_list.empty()) {
78
693k
            *out = _list.front();
79
693k
            _list.pop_front();
80
693k
            const bool has_put_waiter = _put_waiting > 0;
81
693k
            unique_lock.unlock();
82
693k
            if (has_put_waiter) {
83
0
                _put_cv.notify_one();
84
0
            }
85
693k
            return true;
86
18.4E
        } else {
87
18.4E
            assert(_shutdown);
88
2.72k
            return false;
89
18.4E
        }
90
692k
    }
_ZN5doris13BlockingQueueIPN7RdKafka7MessageEE23controlled_blocking_getEPS3_l
Line
Count
Source
59
722
    bool controlled_blocking_get(T* out, int64_t cv_wait_timeout_ms) {
60
722
        MonotonicStopWatch timer;
61
722
        timer.start();
62
722
        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
913
        while (!(_shutdown || !_list.empty())) {
67
191
            ++_get_waiting;
68
#ifdef BE_TEST
69
            TEST_SYNC_POINT("BlockingQueue::controlled_blocking_get::before_wait");
70
#endif
71
191
            _get_cv.wait_for(unique_lock, std::chrono::milliseconds(cv_wait_timeout_ms));
72
191
            DCHECK_GT(_get_waiting, 0);
73
191
            --_get_waiting;
74
191
        }
75
722
        _total_get_wait_time += timer.elapsed_time();
76
77
722
        if (!_list.empty()) {
78
592
            *out = _list.front();
79
592
            _list.pop_front();
80
592
            const bool has_put_waiter = _put_waiting > 0;
81
592
            unique_lock.unlock();
82
592
            if (has_put_waiter) {
83
0
                _put_cv.notify_one();
84
0
            }
85
592
            return true;
86
592
        } else {
87
130
            assert(_shutdown);
88
130
            return false;
89
130
        }
90
722
    }
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
604
    bool controlled_blocking_put(const T& val, int64_t cv_wait_timeout_ms) {
100
604
        MonotonicStopWatch timer;
101
604
        timer.start();
102
604
        std::unique_lock<std::mutex> unique_lock(_lock);
103
604
        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
604
        _total_put_wait_time += timer.elapsed_time();
113
114
604
        if (_shutdown) {
115
0
            return false;
116
0
        }
117
118
604
        _list.push_back(val);
119
604
        const bool has_get_waiter = _get_waiting > 0;
120
604
        unique_lock.unlock();
121
604
        if (has_get_waiter) {
122
167
            _get_cv.notify_one();
123
167
        }
124
604
        return true;
125
604
    }
_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
2
            _get_cv.notify_one();
123
2
        }
124
12
        return true;
125
12
    }
Unexecuted instantiation: _ZN5doris13BlockingQueueINS_14WorkThreadPoolILb0EE4TaskEE23controlled_blocking_putERKS3_l
_ZN5doris13BlockingQueueIPN7RdKafka7MessageEE23controlled_blocking_putERKS3_l
Line
Count
Source
99
592
    bool controlled_blocking_put(const T& val, int64_t cv_wait_timeout_ms) {
100
592
        MonotonicStopWatch timer;
101
592
        timer.start();
102
592
        std::unique_lock<std::mutex> unique_lock(_lock);
103
592
        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
592
        _total_put_wait_time += timer.elapsed_time();
113
114
592
        if (_shutdown) {
115
0
            return false;
116
0
        }
117
118
592
        _list.push_back(val);
119
592
        const bool has_get_waiter = _get_waiting > 0;
120
592
        unique_lock.unlock();
121
592
        if (has_get_waiter) {
122
165
            _get_cv.notify_one();
123
165
        }
124
592
        return true;
125
592
    }
Unexecuted instantiation: _ZN5doris13BlockingQueueISt10shared_ptrIN3Aws7Kinesis5Model6RecordEEE23controlled_blocking_putERKS6_l
126
127
    // Return false if queue full or has been shutdown.
128
691k
    bool try_put(const T& val) {
129
691k
        MonotonicStopWatch timer;
130
691k
        timer.start();
131
691k
        std::unique_lock<std::mutex> unique_lock(_lock);
132
#ifdef BE_TEST
133
        TEST_SYNC_POINT("BlockingQueue::try_put::after_lock");
134
#endif
135
691k
        _total_put_wait_time += timer.elapsed_time();
136
137
693k
        if (_shutdown || _list.size() >= _max_elements) {
138
5
            return false;
139
5
        }
140
141
691k
        _list.push_back(val);
142
691k
        const bool has_get_waiter = _get_waiting > 0;
143
691k
        unique_lock.unlock();
144
693k
        if (has_get_waiter) {
145
693k
            _get_cv.notify_one();
146
693k
        }
147
691k
        return true;
148
691k
    }
149
150
    // Shut down the queue. Wakes up all threads waiting on BlockingGet or BlockingPut.
151
287
    void shutdown() {
152
287
        {
153
287
            std::lock_guard<std::mutex> guard(_lock);
154
287
            _shutdown = true;
155
287
        }
156
157
287
        _get_cv.notify_all();
158
287
        _put_cv.notify_all();
159
287
    }
_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
87
    void shutdown() {
152
87
        {
153
87
            std::lock_guard<std::mutex> guard(_lock);
154
87
            _shutdown = true;
155
87
        }
156
157
87
        _get_cv.notify_all();
158
87
        _put_cv.notify_all();
159
87
    }
_ZN5doris13BlockingQueueIPN7RdKafka7MessageEE8shutdownEv
Line
Count
Source
151
195
    void shutdown() {
152
195
        {
153
195
            std::lock_guard<std::mutex> guard(_lock);
154
195
            _shutdown = true;
155
195
        }
156
157
195
        _get_cv.notify_all();
158
195
        _put_cv.notify_all();
159
195
    }
Unexecuted instantiation: _ZN5doris13BlockingQueueISt10shared_ptrIN3Aws7Kinesis5Model6RecordEEE8shutdownEv
160
161
703k
    uint32_t get_size() const {
162
703k
        std::lock_guard<std::mutex> l(_lock);
163
703k
        return static_cast<uint32_t>(_list.size());
164
703k
    }
_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
703k
    uint32_t get_size() const {
162
703k
        std::lock_guard<std::mutex> l(_lock);
163
703k
        return static_cast<uint32_t>(_list.size());
164
703k
    }
_ZNK5doris13BlockingQueueIPN7RdKafka7MessageEE8get_sizeEv
Line
Count
Source
161
65
    uint32_t get_size() const {
162
65
        std::lock_guard<std::mutex> l(_lock);
163
65
        return static_cast<uint32_t>(_list.size());
164
65
    }
Unexecuted instantiation: _ZNK5doris13BlockingQueueISt10shared_ptrIN3Aws7Kinesis5Model6RecordEEE8get_sizeEv
165
166
8.96k
    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.95k
    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.95k
    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