Coverage Report

Created: 2026-07-22 03: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
40
            : _shutdown(false),
45
40
              _max_elements(max_elements),
46
40
              _total_get_wait_time(0),
47
40
              _total_put_wait_time(0),
48
40
              _get_waiting(0),
49
40
              _put_waiting(0) {}
_ZN5doris13BlockingQueueINS_14WorkThreadPoolILb0EE4TaskEEC2Ej
Line
Count
Source
44
28
            : _shutdown(false),
45
28
              _max_elements(max_elements),
46
28
              _total_get_wait_time(0),
47
28
              _total_put_wait_time(0),
48
28
              _get_waiting(0),
49
28
              _put_waiting(0) {}
_ZN5doris13BlockingQueueIiEC2Ej
Line
Count
Source
44
4
            : _shutdown(false),
45
4
              _max_elements(max_elements),
46
4
              _total_get_wait_time(0),
47
4
              _total_put_wait_time(0),
48
4
              _get_waiting(0),
49
4
              _put_waiting(0) {}
_ZN5doris13BlockingQueueIlEC2Ej
Line
Count
Source
44
1
            : _shutdown(false),
45
1
              _max_elements(max_elements),
46
1
              _total_get_wait_time(0),
47
1
              _total_put_wait_time(0),
48
1
              _get_waiting(0),
49
1
              _put_waiting(0) {}
_ZN5doris13BlockingQueueISt10shared_ptrIN5arrow11RecordBatchEEEC2Ej
Line
Count
Source
44
6
            : _shutdown(false),
45
6
              _max_elements(max_elements),
46
6
              _total_get_wait_time(0),
47
6
              _total_put_wait_time(0),
48
6
              _get_waiting(0),
49
6
              _put_waiting(0) {}
_ZN5doris13BlockingQueueIPN7RdKafka7MessageEEC2Ej
Line
Count
Source
44
1
            : _shutdown(false),
45
1
              _max_elements(max_elements),
46
1
              _total_get_wait_time(0),
47
1
              _total_put_wait_time(0),
48
1
              _get_waiting(0),
49
1
              _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
60.1k
    bool blocking_get(T* out) { return controlled_blocking_get(out, MAX_CV_WAIT_TIMEOUT_MS); }
_ZN5doris13BlockingQueueINS_14WorkThreadPoolILb0EE4TaskEE12blocking_getEPS3_
Line
Count
Source
54
141
    bool blocking_get(T* out) { return controlled_blocking_get(out, MAX_CV_WAIT_TIMEOUT_MS); }
_ZN5doris13BlockingQueueIiE12blocking_getEPi
Line
Count
Source
54
59.9k
    bool blocking_get(T* out) { return controlled_blocking_get(out, MAX_CV_WAIT_TIMEOUT_MS); }
_ZN5doris13BlockingQueueIlE12blocking_getEPl
Line
Count
Source
54
2
    bool blocking_get(T* out) { return controlled_blocking_get(out, MAX_CV_WAIT_TIMEOUT_MS); }
_ZN5doris13BlockingQueueISt10shared_ptrIN5arrow11RecordBatchEEE12blocking_getEPS4_
Line
Count
Source
54
2
    bool blocking_get(T* out) { return controlled_blocking_get(out, MAX_CV_WAIT_TIMEOUT_MS); }
_ZN5doris13BlockingQueueIPN7RdKafka7MessageEE12blocking_getEPS3_
Line
Count
Source
54
1
    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
60.1k
    bool controlled_blocking_get(T* out, int64_t cv_wait_timeout_ms) {
60
60.1k
        MonotonicStopWatch timer;
61
60.1k
        timer.start();
62
60.1k
        std::unique_lock<std::mutex> unique_lock(_lock);
63
60.1k
#ifdef BE_TEST
64
60.1k
        TEST_SYNC_POINT("BlockingQueue::controlled_blocking_get::after_lock");
65
60.1k
#endif
66
61.1k
        while (!(_shutdown || !_list.empty())) {
67
1.01k
            ++_get_waiting;
68
1.01k
#ifdef BE_TEST
69
1.01k
            TEST_SYNC_POINT("BlockingQueue::controlled_blocking_get::before_wait");
70
1.01k
#endif
71
1.01k
            _get_cv.wait_for(unique_lock, std::chrono::milliseconds(cv_wait_timeout_ms));
72
1.01k
            DCHECK_GT(_get_waiting, 0);
73
1.01k
            --_get_waiting;
74
1.01k
        }
75
60.1k
        _total_get_wait_time += timer.elapsed_time();
76
77
60.1k
        if (!_list.empty()) {
78
50.0k
            *out = _list.front();
79
50.0k
            _list.pop_front();
80
50.0k
            const bool has_put_waiter = _put_waiting > 0;
81
50.0k
            unique_lock.unlock();
82
50.0k
            if (has_put_waiter) {
83
13.9k
                _put_cv.notify_one();
84
13.9k
            }
85
50.0k
            return true;
86
50.0k
        } else {
87
10.1k
            assert(_shutdown);
88
10.1k
            return false;
89
10.1k
        }
90
60.1k
    }
_ZN5doris13BlockingQueueINS_14WorkThreadPoolILb0EE4TaskEE23controlled_blocking_getEPS3_l
Line
Count
Source
59
141
    bool controlled_blocking_get(T* out, int64_t cv_wait_timeout_ms) {
60
141
        MonotonicStopWatch timer;
61
141
        timer.start();
62
141
        std::unique_lock<std::mutex> unique_lock(_lock);
63
141
#ifdef BE_TEST
64
141
        TEST_SYNC_POINT("BlockingQueue::controlled_blocking_get::after_lock");
65
141
#endif
66
282
        while (!(_shutdown || !_list.empty())) {
67
141
            ++_get_waiting;
68
141
#ifdef BE_TEST
69
141
            TEST_SYNC_POINT("BlockingQueue::controlled_blocking_get::before_wait");
70
141
#endif
71
141
            _get_cv.wait_for(unique_lock, std::chrono::milliseconds(cv_wait_timeout_ms));
72
141
            DCHECK_GT(_get_waiting, 0);
73
141
            --_get_waiting;
74
141
        }
75
141
        _total_get_wait_time += timer.elapsed_time();
76
77
141
        if (!_list.empty()) {
78
29
            *out = _list.front();
79
29
            _list.pop_front();
80
29
            const bool has_put_waiter = _put_waiting > 0;
81
29
            unique_lock.unlock();
82
29
            if (has_put_waiter) {
83
0
                _put_cv.notify_one();
84
0
            }
85
29
            return true;
86
112
        } else {
87
112
            assert(_shutdown);
88
112
            return false;
89
112
        }
90
141
    }
_ZN5doris13BlockingQueueIiE23controlled_blocking_getEPil
Line
Count
Source
59
59.9k
    bool controlled_blocking_get(T* out, int64_t cv_wait_timeout_ms) {
60
59.9k
        MonotonicStopWatch timer;
61
59.9k
        timer.start();
62
59.9k
        std::unique_lock<std::mutex> unique_lock(_lock);
63
59.9k
#ifdef BE_TEST
64
59.9k
        TEST_SYNC_POINT("BlockingQueue::controlled_blocking_get::after_lock");
65
59.9k
#endif
66
60.8k
        while (!(_shutdown || !_list.empty())) {
67
868
            ++_get_waiting;
68
868
#ifdef BE_TEST
69
868
            TEST_SYNC_POINT("BlockingQueue::controlled_blocking_get::before_wait");
70
868
#endif
71
868
            _get_cv.wait_for(unique_lock, std::chrono::milliseconds(cv_wait_timeout_ms));
72
868
            DCHECK_GT(_get_waiting, 0);
73
868
            --_get_waiting;
74
868
        }
75
59.9k
        _total_get_wait_time += timer.elapsed_time();
76
77
59.9k
        if (!_list.empty()) {
78
50.0k
            *out = _list.front();
79
50.0k
            _list.pop_front();
80
50.0k
            const bool has_put_waiter = _put_waiting > 0;
81
50.0k
            unique_lock.unlock();
82
50.0k
            if (has_put_waiter) {
83
13.9k
                _put_cv.notify_one();
84
13.9k
            }
85
50.0k
            return true;
86
50.0k
        } else {
87
9.98k
            assert(_shutdown);
88
10.0k
            return false;
89
9.98k
        }
90
59.9k
    }
_ZN5doris13BlockingQueueIlE23controlled_blocking_getEPll
Line
Count
Source
59
2
    bool controlled_blocking_get(T* out, int64_t cv_wait_timeout_ms) {
60
2
        MonotonicStopWatch timer;
61
2
        timer.start();
62
2
        std::unique_lock<std::mutex> unique_lock(_lock);
63
2
#ifdef BE_TEST
64
2
        TEST_SYNC_POINT("BlockingQueue::controlled_blocking_get::after_lock");
65
2
#endif
66
2
        while (!(_shutdown || !_list.empty())) {
67
0
            ++_get_waiting;
68
0
#ifdef BE_TEST
69
0
            TEST_SYNC_POINT("BlockingQueue::controlled_blocking_get::before_wait");
70
0
#endif
71
0
            _get_cv.wait_for(unique_lock, std::chrono::milliseconds(cv_wait_timeout_ms));
72
0
            DCHECK_GT(_get_waiting, 0);
73
0
            --_get_waiting;
74
0
        }
75
2
        _total_get_wait_time += timer.elapsed_time();
76
77
2
        if (!_list.empty()) {
78
1
            *out = _list.front();
79
1
            _list.pop_front();
80
1
            const bool has_put_waiter = _put_waiting > 0;
81
1
            unique_lock.unlock();
82
1
            if (has_put_waiter) {
83
0
                _put_cv.notify_one();
84
0
            }
85
1
            return true;
86
1
        } else {
87
1
            assert(_shutdown);
88
1
            return false;
89
1
        }
90
2
    }
_ZN5doris13BlockingQueueISt10shared_ptrIN5arrow11RecordBatchEEE23controlled_blocking_getEPS4_l
Line
Count
Source
59
2
    bool controlled_blocking_get(T* out, int64_t cv_wait_timeout_ms) {
60
2
        MonotonicStopWatch timer;
61
2
        timer.start();
62
2
        std::unique_lock<std::mutex> unique_lock(_lock);
63
2
#ifdef BE_TEST
64
2
        TEST_SYNC_POINT("BlockingQueue::controlled_blocking_get::after_lock");
65
2
#endif
66
2
        while (!(_shutdown || !_list.empty())) {
67
0
            ++_get_waiting;
68
0
#ifdef BE_TEST
69
0
            TEST_SYNC_POINT("BlockingQueue::controlled_blocking_get::before_wait");
70
0
#endif
71
0
            _get_cv.wait_for(unique_lock, std::chrono::milliseconds(cv_wait_timeout_ms));
72
0
            DCHECK_GT(_get_waiting, 0);
73
0
            --_get_waiting;
74
0
        }
75
2
        _total_get_wait_time += timer.elapsed_time();
76
77
2
        if (!_list.empty()) {
78
2
            *out = _list.front();
79
2
            _list.pop_front();
80
2
            const bool has_put_waiter = _put_waiting > 0;
81
2
            unique_lock.unlock();
82
2
            if (has_put_waiter) {
83
0
                _put_cv.notify_one();
84
0
            }
85
2
            return true;
86
2
        } else {
87
0
            assert(_shutdown);
88
0
            return false;
89
0
        }
90
2
    }
_ZN5doris13BlockingQueueIPN7RdKafka7MessageEE23controlled_blocking_getEPS3_l
Line
Count
Source
59
2
    bool controlled_blocking_get(T* out, int64_t cv_wait_timeout_ms) {
60
2
        MonotonicStopWatch timer;
61
2
        timer.start();
62
2
        std::unique_lock<std::mutex> unique_lock(_lock);
63
2
#ifdef BE_TEST
64
2
        TEST_SYNC_POINT("BlockingQueue::controlled_blocking_get::after_lock");
65
2
#endif
66
8
        while (!(_shutdown || !_list.empty())) {
67
6
            ++_get_waiting;
68
6
#ifdef BE_TEST
69
6
            TEST_SYNC_POINT("BlockingQueue::controlled_blocking_get::before_wait");
70
6
#endif
71
6
            _get_cv.wait_for(unique_lock, std::chrono::milliseconds(cv_wait_timeout_ms));
72
6
            DCHECK_GT(_get_waiting, 0);
73
6
            --_get_waiting;
74
6
        }
75
2
        _total_get_wait_time += timer.elapsed_time();
76
77
2
        if (!_list.empty()) {
78
0
            *out = _list.front();
79
0
            _list.pop_front();
80
0
            const bool has_put_waiter = _put_waiting > 0;
81
0
            unique_lock.unlock();
82
0
            if (has_put_waiter) {
83
0
                _put_cv.notify_one();
84
0
            }
85
0
            return true;
86
2
        } else {
87
2
            assert(_shutdown);
88
2
            return false;
89
2
        }
90
2
    }
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
49.9k
    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
94
4
    bool blocking_put(const T& val) { return controlled_blocking_put(val, MAX_CV_WAIT_TIMEOUT_MS); }
_ZN5doris13BlockingQueueIiE12blocking_putERKi
Line
Count
Source
94
49.9k
    bool blocking_put(const T& val) { return controlled_blocking_put(val, MAX_CV_WAIT_TIMEOUT_MS); }
_ZN5doris13BlockingQueueIlE12blocking_putERKl
Line
Count
Source
94
2
    bool blocking_put(const T& val) { return controlled_blocking_put(val, MAX_CV_WAIT_TIMEOUT_MS); }
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
49.9k
    bool controlled_blocking_put(const T& val, int64_t cv_wait_timeout_ms) {
100
49.9k
        MonotonicStopWatch timer;
101
49.9k
        timer.start();
102
49.9k
        std::unique_lock<std::mutex> unique_lock(_lock);
103
54.0k
        while (!(_shutdown || _list.size() < _max_elements)) {
104
4.00k
            ++_put_waiting;
105
4.00k
#ifdef BE_TEST
106
4.00k
            TEST_SYNC_POINT("BlockingQueue::controlled_blocking_put::before_wait");
107
4.00k
#endif
108
4.00k
            _put_cv.wait_for(unique_lock, std::chrono::milliseconds(cv_wait_timeout_ms));
109
4.00k
            DCHECK_GT(_put_waiting, 0);
110
4.00k
            --_put_waiting;
111
4.00k
        }
112
49.9k
        _total_put_wait_time += timer.elapsed_time();
113
114
49.9k
        if (_shutdown) {
115
1
            return false;
116
1
        }
117
118
49.9k
        _list.push_back(val);
119
49.9k
        const bool has_get_waiter = _get_waiting > 0;
120
49.9k
        unique_lock.unlock();
121
49.9k
        if (has_get_waiter) {
122
3.05k
            _get_cv.notify_one();
123
3.05k
        }
124
49.9k
        return true;
125
49.9k
    }
Unexecuted instantiation: _ZN5doris13BlockingQueueINS_14WorkThreadPoolILb0EE4TaskEE23controlled_blocking_putERKS3_l
_ZN5doris13BlockingQueueISt10shared_ptrIN5arrow11RecordBatchEEE23controlled_blocking_putERKS4_l
Line
Count
Source
99
4
    bool controlled_blocking_put(const T& val, int64_t cv_wait_timeout_ms) {
100
4
        MonotonicStopWatch timer;
101
4
        timer.start();
102
4
        std::unique_lock<std::mutex> unique_lock(_lock);
103
4
        while (!(_shutdown || _list.size() < _max_elements)) {
104
0
            ++_put_waiting;
105
0
#ifdef BE_TEST
106
0
            TEST_SYNC_POINT("BlockingQueue::controlled_blocking_put::before_wait");
107
0
#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
4
        _total_put_wait_time += timer.elapsed_time();
113
114
4
        if (_shutdown) {
115
0
            return false;
116
0
        }
117
118
4
        _list.push_back(val);
119
4
        const bool has_get_waiter = _get_waiting > 0;
120
4
        unique_lock.unlock();
121
4
        if (has_get_waiter) {
122
0
            _get_cv.notify_one();
123
0
        }
124
4
        return true;
125
4
    }
_ZN5doris13BlockingQueueIiE23controlled_blocking_putERKil
Line
Count
Source
99
49.9k
    bool controlled_blocking_put(const T& val, int64_t cv_wait_timeout_ms) {
100
49.9k
        MonotonicStopWatch timer;
101
49.9k
        timer.start();
102
49.9k
        std::unique_lock<std::mutex> unique_lock(_lock);
103
54.0k
        while (!(_shutdown || _list.size() < _max_elements)) {
104
4.00k
            ++_put_waiting;
105
4.00k
#ifdef BE_TEST
106
4.00k
            TEST_SYNC_POINT("BlockingQueue::controlled_blocking_put::before_wait");
107
4.00k
#endif
108
4.00k
            _put_cv.wait_for(unique_lock, std::chrono::milliseconds(cv_wait_timeout_ms));
109
4.00k
            DCHECK_GT(_put_waiting, 0);
110
4.00k
            --_put_waiting;
111
4.00k
        }
112
49.9k
        _total_put_wait_time += timer.elapsed_time();
113
114
49.9k
        if (_shutdown) {
115
0
            return false;
116
0
        }
117
118
49.9k
        _list.push_back(val);
119
49.9k
        const bool has_get_waiter = _get_waiting > 0;
120
49.9k
        unique_lock.unlock();
121
49.9k
        if (has_get_waiter) {
122
3.05k
            _get_cv.notify_one();
123
3.05k
        }
124
49.9k
        return true;
125
49.9k
    }
_ZN5doris13BlockingQueueIlE23controlled_blocking_putERKll
Line
Count
Source
99
2
    bool controlled_blocking_put(const T& val, int64_t cv_wait_timeout_ms) {
100
2
        MonotonicStopWatch timer;
101
2
        timer.start();
102
2
        std::unique_lock<std::mutex> unique_lock(_lock);
103
2
        while (!(_shutdown || _list.size() < _max_elements)) {
104
0
            ++_put_waiting;
105
0
#ifdef BE_TEST
106
0
            TEST_SYNC_POINT("BlockingQueue::controlled_blocking_put::before_wait");
107
0
#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
2
        _total_put_wait_time += timer.elapsed_time();
113
114
2
        if (_shutdown) {
115
1
            return false;
116
1
        }
117
118
1
        _list.push_back(val);
119
1
        const bool has_get_waiter = _get_waiting > 0;
120
1
        unique_lock.unlock();
121
1
        if (has_get_waiter) {
122
0
            _get_cv.notify_one();
123
0
        }
124
1
        return true;
125
2
    }
Unexecuted instantiation: _ZN5doris13BlockingQueueIPN7RdKafka7MessageEE23controlled_blocking_putERKS3_l
Unexecuted instantiation: _ZN5doris13BlockingQueueISt10shared_ptrIN3Aws7Kinesis5Model6RecordEEE23controlled_blocking_putERKS6_l
126
127
    // Return false if queue full or has been shutdown.
128
32
    bool try_put(const T& val) {
129
32
        MonotonicStopWatch timer;
130
32
        timer.start();
131
32
        std::unique_lock<std::mutex> unique_lock(_lock);
132
32
#ifdef BE_TEST
133
32
        TEST_SYNC_POINT("BlockingQueue::try_put::after_lock");
134
32
#endif
135
32
        _total_put_wait_time += timer.elapsed_time();
136
137
32
        if (_shutdown || _list.size() >= _max_elements) {
138
0
            return false;
139
0
        }
140
141
32
        _list.push_back(val);
142
32
        const bool has_get_waiter = _get_waiting > 0;
143
32
        unique_lock.unlock();
144
32
        if (has_get_waiter) {
145
31
            _get_cv.notify_one();
146
31
        }
147
32
        return true;
148
32
    }
_ZN5doris13BlockingQueueINS_14WorkThreadPoolILb0EE4TaskEE7try_putERKS3_
Line
Count
Source
128
29
    bool try_put(const T& val) {
129
29
        MonotonicStopWatch timer;
130
29
        timer.start();
131
29
        std::unique_lock<std::mutex> unique_lock(_lock);
132
29
#ifdef BE_TEST
133
29
        TEST_SYNC_POINT("BlockingQueue::try_put::after_lock");
134
29
#endif
135
29
        _total_put_wait_time += timer.elapsed_time();
136
137
29
        if (_shutdown || _list.size() >= _max_elements) {
138
0
            return false;
139
0
        }
140
141
29
        _list.push_back(val);
142
29
        const bool has_get_waiter = _get_waiting > 0;
143
29
        unique_lock.unlock();
144
29
        if (has_get_waiter) {
145
29
            _get_cv.notify_one();
146
29
        }
147
29
        return true;
148
29
    }
_ZN5doris13BlockingQueueIiE7try_putERKi
Line
Count
Source
128
3
    bool try_put(const T& val) {
129
3
        MonotonicStopWatch timer;
130
3
        timer.start();
131
3
        std::unique_lock<std::mutex> unique_lock(_lock);
132
3
#ifdef BE_TEST
133
3
        TEST_SYNC_POINT("BlockingQueue::try_put::after_lock");
134
3
#endif
135
3
        _total_put_wait_time += timer.elapsed_time();
136
137
3
        if (_shutdown || _list.size() >= _max_elements) {
138
0
            return false;
139
0
        }
140
141
3
        _list.push_back(val);
142
3
        const bool has_get_waiter = _get_waiting > 0;
143
3
        unique_lock.unlock();
144
3
        if (has_get_waiter) {
145
2
            _get_cv.notify_one();
146
2
        }
147
3
        return true;
148
3
    }
149
150
    // Shut down the queue. Wakes up all threads waiting on BlockingGet or BlockingPut.
151
37
    void shutdown() {
152
37
        {
153
37
            std::lock_guard<std::mutex> guard(_lock);
154
37
            _shutdown = true;
155
37
        }
156
157
37
        _get_cv.notify_all();
158
37
        _put_cv.notify_all();
159
37
    }
_ZN5doris13BlockingQueueINS_14WorkThreadPoolILb0EE4TaskEE8shutdownEv
Line
Count
Source
151
28
    void shutdown() {
152
28
        {
153
28
            std::lock_guard<std::mutex> guard(_lock);
154
28
            _shutdown = true;
155
28
        }
156
157
28
        _get_cv.notify_all();
158
28
        _put_cv.notify_all();
159
28
    }
_ZN5doris13BlockingQueueIlE8shutdownEv
Line
Count
Source
151
1
    void shutdown() {
152
1
        {
153
1
            std::lock_guard<std::mutex> guard(_lock);
154
1
            _shutdown = true;
155
1
        }
156
157
1
        _get_cv.notify_all();
158
1
        _put_cv.notify_all();
159
1
    }
_ZN5doris13BlockingQueueIiE8shutdownEv
Line
Count
Source
151
3
    void shutdown() {
152
3
        {
153
3
            std::lock_guard<std::mutex> guard(_lock);
154
3
            _shutdown = true;
155
3
        }
156
157
3
        _get_cv.notify_all();
158
3
        _put_cv.notify_all();
159
3
    }
_ZN5doris13BlockingQueueISt10shared_ptrIN5arrow11RecordBatchEEE8shutdownEv
Line
Count
Source
151
2
    void shutdown() {
152
2
        {
153
2
            std::lock_guard<std::mutex> guard(_lock);
154
2
            _shutdown = true;
155
2
        }
156
157
2
        _get_cv.notify_all();
158
2
        _put_cv.notify_all();
159
2
    }
_ZN5doris13BlockingQueueIPN7RdKafka7MessageEE8shutdownEv
Line
Count
Source
151
3
    void shutdown() {
152
3
        {
153
3
            std::lock_guard<std::mutex> guard(_lock);
154
3
            _shutdown = true;
155
3
        }
156
157
3
        _get_cv.notify_all();
158
3
        _put_cv.notify_all();
159
3
    }
Unexecuted instantiation: _ZN5doris13BlockingQueueISt10shared_ptrIN3Aws7Kinesis5Model6RecordEEE8shutdownEv
160
161
364
    uint32_t get_size() const {
162
364
        std::lock_guard<std::mutex> l(_lock);
163
364
        return static_cast<uint32_t>(_list.size());
164
364
    }
_ZNK5doris13BlockingQueueINS_14WorkThreadPoolILb0EE4TaskEE8get_sizeEv
Line
Count
Source
161
363
    uint32_t get_size() const {
162
363
        std::lock_guard<std::mutex> l(_lock);
163
363
        return static_cast<uint32_t>(_list.size());
164
363
    }
Unexecuted instantiation: _ZNK5doris13BlockingQueueISt10shared_ptrIN5arrow11RecordBatchEEE8get_sizeEv
_ZNK5doris13BlockingQueueIPN7RdKafka7MessageEE8get_sizeEv
Line
Count
Source
161
1
    uint32_t get_size() const {
162
1
        std::lock_guard<std::mutex> l(_lock);
163
1
        return static_cast<uint32_t>(_list.size());
164
1
    }
Unexecuted instantiation: _ZNK5doris13BlockingQueueISt10shared_ptrIN3Aws7Kinesis5Model6RecordEEE8get_sizeEv
165
166
224
    uint32_t get_capacity() const { return _max_elements; }
167
168
#ifdef BE_TEST
169
8
    size_t get_waiting_count_for_test() const {
170
8
        std::lock_guard<std::mutex> guard(_lock);
171
8
        return _get_waiting;
172
8
    }
173
174
8
    size_t put_waiting_count_for_test() const {
175
8
        std::lock_guard<std::mutex> guard(_lock);
176
8
        return _put_waiting;
177
8
    }
178
#endif
179
180
    // Returns the total amount of time threads have blocked in BlockingGet.
181
223
    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
223
    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