Coverage Report

Created: 2026-07-20 15:57

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
246
            : _shutdown(false),
41
246
              _max_elements(max_elements),
42
246
              _total_get_wait_time(0),
43
246
              _total_put_wait_time(0),
44
246
              _get_waiting(0),
45
246
              _put_waiting(0) {}
_ZN5doris13BlockingQueueISt10shared_ptrIN5arrow11RecordBatchEEEC2Ej
Line
Count
Source
40
9
            : _shutdown(false),
41
9
              _max_elements(max_elements),
42
9
              _total_get_wait_time(0),
43
9
              _total_put_wait_time(0),
44
9
              _get_waiting(0),
45
9
              _put_waiting(0) {}
_ZN5doris13BlockingQueueINS_14WorkThreadPoolILb0EE4TaskEEC2Ej
Line
Count
Source
40
52
            : _shutdown(false),
41
52
              _max_elements(max_elements),
42
52
              _total_get_wait_time(0),
43
52
              _total_put_wait_time(0),
44
52
              _get_waiting(0),
45
52
              _put_waiting(0) {}
_ZN5doris13BlockingQueueIPN7RdKafka7MessageEEC2Ej
Line
Count
Source
40
185
            : _shutdown(false),
41
185
              _max_elements(max_elements),
42
185
              _total_get_wait_time(0),
43
185
              _total_put_wait_time(0),
44
185
              _get_waiting(0),
45
185
              _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
958k
    bool blocking_get(T* out) { return controlled_blocking_get(out, MAX_CV_WAIT_TIMEOUT_MS); }
_ZN5doris13BlockingQueueISt10shared_ptrIN5arrow11RecordBatchEEE12blocking_getEPS4_
Line
Count
Source
50
7
    bool blocking_get(T* out) { return controlled_blocking_get(out, MAX_CV_WAIT_TIMEOUT_MS); }
_ZN5doris13BlockingQueueINS_14WorkThreadPoolILb0EE4TaskEE12blocking_getEPS3_
Line
Count
Source
50
958k
    bool blocking_get(T* out) { return controlled_blocking_get(out, MAX_CV_WAIT_TIMEOUT_MS); }
_ZN5doris13BlockingQueueIPN7RdKafka7MessageEE12blocking_getEPS3_
Line
Count
Source
50
185
    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
958k
    bool controlled_blocking_get(T* out, int64_t cv_wait_timeout_ms) {
56
958k
        MonotonicStopWatch timer;
57
958k
        timer.start();
58
958k
        std::unique_lock<std::mutex> unique_lock(_lock);
59
1.92M
        while (!(_shutdown || !_list.empty())) {
60
963k
            ++_get_waiting;
61
963k
            if (_get_cv.wait_for(unique_lock, std::chrono::milliseconds(cv_wait_timeout_ms)) ==
62
963k
                std::cv_status::timeout) {
63
2.20k
                _get_waiting--;
64
2.20k
            }
65
963k
        }
66
958k
        _total_get_wait_time += timer.elapsed_time();
67
68
958k
        if (!_list.empty()) {
69
956k
            *out = _list.front();
70
956k
            _list.pop_front();
71
956k
            if (_put_waiting > 0) {
72
0
                --_put_waiting;
73
0
                unique_lock.unlock();
74
0
                _put_cv.notify_one();
75
0
            }
76
956k
            return true;
77
956k
        } else {
78
1.55k
            assert(_shutdown);
79
2.14k
            return false;
80
1.55k
        }
81
958k
    }
_ZN5doris13BlockingQueueISt10shared_ptrIN5arrow11RecordBatchEEE23controlled_blocking_getEPS4_l
Line
Count
Source
55
7
    bool controlled_blocking_get(T* out, int64_t cv_wait_timeout_ms) {
56
7
        MonotonicStopWatch timer;
57
7
        timer.start();
58
7
        std::unique_lock<std::mutex> unique_lock(_lock);
59
10
        while (!(_shutdown || !_list.empty())) {
60
3
            ++_get_waiting;
61
3
            if (_get_cv.wait_for(unique_lock, std::chrono::milliseconds(cv_wait_timeout_ms)) ==
62
3
                std::cv_status::timeout) {
63
0
                _get_waiting--;
64
0
            }
65
3
        }
66
7
        _total_get_wait_time += timer.elapsed_time();
67
68
7
        if (!_list.empty()) {
69
7
            *out = _list.front();
70
7
            _list.pop_front();
71
7
            if (_put_waiting > 0) {
72
0
                --_put_waiting;
73
0
                unique_lock.unlock();
74
0
                _put_cv.notify_one();
75
0
            }
76
7
            return true;
77
7
        } else {
78
0
            assert(_shutdown);
79
0
            return false;
80
0
        }
81
7
    }
_ZN5doris13BlockingQueueINS_14WorkThreadPoolILb0EE4TaskEE23controlled_blocking_getEPS3_l
Line
Count
Source
55
956k
    bool controlled_blocking_get(T* out, int64_t cv_wait_timeout_ms) {
56
956k
        MonotonicStopWatch timer;
57
956k
        timer.start();
58
956k
        std::unique_lock<std::mutex> unique_lock(_lock);
59
1.91M
        while (!(_shutdown || !_list.empty())) {
60
963k
            ++_get_waiting;
61
963k
            if (_get_cv.wait_for(unique_lock, std::chrono::milliseconds(cv_wait_timeout_ms)) ==
62
963k
                std::cv_status::timeout) {
63
2.19k
                _get_waiting--;
64
2.19k
            }
65
963k
        }
66
956k
        _total_get_wait_time += timer.elapsed_time();
67
68
956k
        if (!_list.empty()) {
69
955k
            *out = _list.front();
70
955k
            _list.pop_front();
71
955k
            if (_put_waiting > 0) {
72
0
                --_put_waiting;
73
0
                unique_lock.unlock();
74
0
                _put_cv.notify_one();
75
0
            }
76
955k
            return true;
77
955k
        } else {
78
1.18k
            assert(_shutdown);
79
1.77k
            return false;
80
1.18k
        }
81
956k
    }
_ZN5doris13BlockingQueueIPN7RdKafka7MessageEE23controlled_blocking_getEPS3_l
Line
Count
Source
55
1.51k
    bool controlled_blocking_get(T* out, int64_t cv_wait_timeout_ms) {
56
1.51k
        MonotonicStopWatch timer;
57
1.51k
        timer.start();
58
1.51k
        std::unique_lock<std::mutex> unique_lock(_lock);
59
2.08k
        while (!(_shutdown || !_list.empty())) {
60
565
            ++_get_waiting;
61
565
            if (_get_cv.wait_for(unique_lock, std::chrono::milliseconds(cv_wait_timeout_ms)) ==
62
565
                std::cv_status::timeout) {
63
5
                _get_waiting--;
64
5
            }
65
565
        }
66
1.51k
        _total_get_wait_time += timer.elapsed_time();
67
68
1.51k
        if (!_list.empty()) {
69
1.14k
            *out = _list.front();
70
1.14k
            _list.pop_front();
71
1.14k
            if (_put_waiting > 0) {
72
0
                --_put_waiting;
73
0
                unique_lock.unlock();
74
0
                _put_cv.notify_one();
75
0
            }
76
1.14k
            return true;
77
1.14k
        } else {
78
370
            assert(_shutdown);
79
370
            return false;
80
370
        }
81
1.51k
    }
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
12
    bool blocking_put(const T& val) { return controlled_blocking_put(val, MAX_CV_WAIT_TIMEOUT_MS); }
_ZN5doris13BlockingQueueISt10shared_ptrIN5arrow11RecordBatchEEE12blocking_putERKS4_
Line
Count
Source
85
12
    bool blocking_put(const T& val) { return controlled_blocking_put(val, MAX_CV_WAIT_TIMEOUT_MS); }
Unexecuted instantiation: _ZN5doris13BlockingQueueINS_14WorkThreadPoolILb0EE4TaskEE12blocking_putERKS3_
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
1.15k
    bool controlled_blocking_put(const T& val, int64_t cv_wait_timeout_ms) {
91
1.15k
        MonotonicStopWatch timer;
92
1.15k
        timer.start();
93
1.15k
        std::unique_lock<std::mutex> unique_lock(_lock);
94
1.15k
        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
1.15k
        _total_put_wait_time += timer.elapsed_time();
102
103
1.15k
        if (_shutdown) {
104
0
            return false;
105
0
        }
106
107
1.15k
        _list.push_back(val);
108
1.15k
        if (_get_waiting > 0) {
109
382
            --_get_waiting;
110
382
            unique_lock.unlock();
111
382
            _get_cv.notify_one();
112
382
        }
113
1.15k
        return true;
114
1.15k
    }
_ZN5doris13BlockingQueueISt10shared_ptrIN5arrow11RecordBatchEEE23controlled_blocking_putERKS4_l
Line
Count
Source
90
12
    bool controlled_blocking_put(const T& val, int64_t cv_wait_timeout_ms) {
91
12
        MonotonicStopWatch timer;
92
12
        timer.start();
93
12
        std::unique_lock<std::mutex> unique_lock(_lock);
94
12
        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
12
        _total_put_wait_time += timer.elapsed_time();
102
103
12
        if (_shutdown) {
104
0
            return false;
105
0
        }
106
107
12
        _list.push_back(val);
108
12
        if (_get_waiting > 0) {
109
3
            --_get_waiting;
110
3
            unique_lock.unlock();
111
3
            _get_cv.notify_one();
112
3
        }
113
12
        return true;
114
12
    }
Unexecuted instantiation: _ZN5doris13BlockingQueueINS_14WorkThreadPoolILb0EE4TaskEE23controlled_blocking_putERKS3_l
_ZN5doris13BlockingQueueIPN7RdKafka7MessageEE23controlled_blocking_putERKS3_l
Line
Count
Source
90
1.14k
    bool controlled_blocking_put(const T& val, int64_t cv_wait_timeout_ms) {
91
1.14k
        MonotonicStopWatch timer;
92
1.14k
        timer.start();
93
1.14k
        std::unique_lock<std::mutex> unique_lock(_lock);
94
1.14k
        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
1.14k
        _total_put_wait_time += timer.elapsed_time();
102
103
1.14k
        if (_shutdown) {
104
0
            return false;
105
0
        }
106
107
1.14k
        _list.push_back(val);
108
1.14k
        if (_get_waiting > 0) {
109
379
            --_get_waiting;
110
379
            unique_lock.unlock();
111
379
            _get_cv.notify_one();
112
379
        }
113
1.14k
        return true;
114
1.14k
    }
Unexecuted instantiation: _ZN5doris13BlockingQueueISt10shared_ptrIN3Aws7Kinesis5Model6RecordEEE23controlled_blocking_putERKS6_l
115
116
    // Return false if queue full or has been shutdown.
117
954k
    bool try_put(const T& val) {
118
954k
        if (_shutdown || _list.size() >= _max_elements) {
119
0
            return false;
120
0
        }
121
122
954k
        MonotonicStopWatch timer;
123
954k
        timer.start();
124
954k
        std::unique_lock<std::mutex> unique_lock(_lock);
125
954k
        _total_put_wait_time += timer.elapsed_time();
126
127
955k
        if (_shutdown || _list.size() >= _max_elements) {
128
0
            return false;
129
0
        }
130
131
954k
        _list.push_back(val);
132
955k
        if (_get_waiting > 0) {
133
955k
            --_get_waiting;
134
955k
            unique_lock.unlock();
135
955k
            _get_cv.notify_one();
136
955k
        }
137
954k
        return true;
138
954k
    }
139
140
    // Shut down the queue. Wakes up all threads waiting on BlockingGet or BlockingPut.
141
596
    void shutdown() {
142
596
        {
143
596
            std::lock_guard<std::mutex> guard(_lock);
144
596
            _shutdown = true;
145
596
        }
146
147
596
        _get_cv.notify_all();
148
596
        _put_cv.notify_all();
149
596
    }
_ZN5doris13BlockingQueueISt10shared_ptrIN5arrow11RecordBatchEEE8shutdownEv
Line
Count
Source
141
5
    void shutdown() {
142
5
        {
143
5
            std::lock_guard<std::mutex> guard(_lock);
144
5
            _shutdown = true;
145
5
        }
146
147
5
        _get_cv.notify_all();
148
5
        _put_cv.notify_all();
149
5
    }
_ZN5doris13BlockingQueueINS_14WorkThreadPoolILb0EE4TaskEE8shutdownEv
Line
Count
Source
141
36
    void shutdown() {
142
36
        {
143
36
            std::lock_guard<std::mutex> guard(_lock);
144
36
            _shutdown = true;
145
36
        }
146
147
36
        _get_cv.notify_all();
148
36
        _put_cv.notify_all();
149
36
    }
_ZN5doris13BlockingQueueIPN7RdKafka7MessageEE8shutdownEv
Line
Count
Source
141
555
    void shutdown() {
142
555
        {
143
555
            std::lock_guard<std::mutex> guard(_lock);
144
555
            _shutdown = true;
145
555
        }
146
147
555
        _get_cv.notify_all();
148
555
        _put_cv.notify_all();
149
555
    }
Unexecuted instantiation: _ZN5doris13BlockingQueueISt10shared_ptrIN3Aws7Kinesis5Model6RecordEEE8shutdownEv
150
151
967k
    uint32_t get_size() const {
152
967k
        std::lock_guard<std::mutex> l(_lock);
153
967k
        return static_cast<uint32_t>(_list.size());
154
967k
    }
_ZNK5doris13BlockingQueueISt10shared_ptrIN5arrow11RecordBatchEEE8get_sizeEv
Line
Count
Source
151
8
    uint32_t get_size() const {
152
8
        std::lock_guard<std::mutex> l(_lock);
153
8
        return static_cast<uint32_t>(_list.size());
154
8
    }
_ZNK5doris13BlockingQueueINS_14WorkThreadPoolILb0EE4TaskEE8get_sizeEv
Line
Count
Source
151
967k
    uint32_t get_size() const {
152
967k
        std::lock_guard<std::mutex> l(_lock);
153
967k
        return static_cast<uint32_t>(_list.size());
154
967k
    }
_ZNK5doris13BlockingQueueIPN7RdKafka7MessageEE8get_sizeEv
Line
Count
Source
151
185
    uint32_t get_size() const {
152
185
        std::lock_guard<std::mutex> l(_lock);
153
185
        return static_cast<uint32_t>(_list.size());
154
185
    }
Unexecuted instantiation: _ZNK5doris13BlockingQueueISt10shared_ptrIN3Aws7Kinesis5Model6RecordEEE8get_sizeEv
155
156
6.87k
    uint32_t get_capacity() const { return _max_elements; }
157
158
    // Returns the total amount of time threads have blocked in BlockingGet.
159
6.87k
    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
6.87k
    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