Coverage Report

Created: 2026-04-14 13:01

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/exec/operator/data_queue.cpp
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
18
#include "exec/operator/data_queue.h"
19
20
#include <glog/logging.h>
21
22
#include <algorithm>
23
#include <mutex>
24
#include <utility>
25
26
#include "core/block/block.h"
27
#include "exec/pipeline/dependency.h"
28
29
namespace doris {
30
DataQueue::DataQueue(int child_count)
31
4.19k
        : _queue_blocks_lock(child_count),
32
4.19k
          _queue_blocks(child_count),
33
4.19k
          _free_blocks_lock(child_count),
34
4.19k
          _free_blocks(child_count),
35
4.19k
          _child_count(child_count),
36
4.19k
          _is_finished(child_count),
37
4.19k
          _is_canceled(child_count),
38
4.19k
          _cur_bytes_in_queue(child_count),
39
4.19k
          _cur_blocks_nums_in_queue(child_count),
40
4.19k
          _flag_queue_idx(0) {
41
12.7k
    for (int i = 0; i < child_count; ++i) {
42
8.59k
        _queue_blocks_lock[i].reset(new std::mutex());
43
8.59k
        _free_blocks_lock[i].reset(new std::mutex());
44
8.59k
        _is_finished[i] = false;
45
8.59k
        _is_canceled[i] = false;
46
8.59k
        _cur_bytes_in_queue[i] = 0;
47
8.59k
        _cur_blocks_nums_in_queue[i] = 0;
48
8.59k
    }
49
4.19k
    _un_finished_counter = child_count;
50
4.19k
    _sink_dependencies.resize(child_count, nullptr);
51
4.19k
}
52
53
8.66k
std::unique_ptr<Block> DataQueue::get_free_block(int child_idx) {
54
8.66k
    {
55
8.66k
        INJECT_MOCK_SLEEP(std::lock_guard<std::mutex> l(*_free_blocks_lock[child_idx]));
56
8.66k
        if (!_free_blocks[child_idx].empty()) {
57
3
            auto block = std::move(_free_blocks[child_idx].front());
58
3
            _free_blocks[child_idx].pop_front();
59
3
            return block;
60
3
        }
61
8.66k
    }
62
63
8.66k
    return Block::create_unique();
64
8.66k
}
65
66
8.65k
void DataQueue::push_free_block(std::unique_ptr<Block> block, int child_idx) {
67
8.65k
    DCHECK(block->rows() == 0);
68
69
8.65k
    if (!_is_low_memory_mode) {
70
8.65k
        INJECT_MOCK_SLEEP(std::lock_guard<std::mutex> l(*_free_blocks_lock[child_idx]));
71
8.65k
        _free_blocks[child_idx].emplace_back(std::move(block));
72
8.65k
    }
73
8.65k
}
74
75
4.17k
void DataQueue::clear_free_blocks() {
76
12.8k
    for (size_t child_idx = 0; child_idx < _free_blocks.size(); ++child_idx) {
77
8.63k
        std::lock_guard<std::mutex> l(*_free_blocks_lock[child_idx]);
78
8.63k
        std::deque<std::unique_ptr<Block>> tmp_queue;
79
8.63k
        _free_blocks[child_idx].swap(tmp_queue);
80
8.63k
    }
81
4.17k
}
82
83
4.17k
void DataQueue::terminate() {
84
12.8k
    for (int i = 0; i < _queue_blocks.size(); i++) {
85
8.64k
        set_finish(i);
86
8.64k
        INJECT_MOCK_SLEEP(std::lock_guard<std::mutex> l(*_queue_blocks_lock[i]));
87
8.64k
        if (_cur_blocks_nums_in_queue[i] > 0) {
88
1
            _queue_blocks[i].clear();
89
1
            _cur_bytes_in_queue[i] = 0;
90
1
            _cur_blocks_nums_in_queue[i] = 0;
91
1
            _sink_dependencies[i]->set_always_ready();
92
1
        }
93
8.64k
    }
94
4.17k
    clear_free_blocks();
95
4.17k
}
96
97
//check which queue have data, and save the idx in _flag_queue_idx,
98
//so next loop, will check the record idx + 1 first
99
//maybe it's useful with many queue, others maybe always 0
100
53.5k
bool DataQueue::remaining_has_data() {
101
53.5k
    int count = _child_count;
102
147k
    while (--count >= 0) {
103
103k
        _flag_queue_idx++;
104
103k
        if (_flag_queue_idx == _child_count) {
105
49.1k
            _flag_queue_idx = 0;
106
49.1k
        }
107
103k
        if (_cur_blocks_nums_in_queue[_flag_queue_idx] > 0) {
108
8.81k
            return true;
109
8.81k
        }
110
103k
    }
111
44.7k
    return false;
112
53.5k
}
113
114
//the _flag_queue_idx indicate which queue has data, and in check can_read
115
//will be set idx in remaining_has_data function
116
45.1k
Status DataQueue::get_block_from_queue(std::unique_ptr<Block>* output_block, int* child_idx) {
117
45.1k
    if (_is_canceled[_flag_queue_idx]) {
118
0
        return Status::InternalError("Current queue of idx {} have beed canceled: ",
119
0
                                     _flag_queue_idx);
120
0
    }
121
122
45.1k
    {
123
45.1k
        INJECT_MOCK_SLEEP(std::lock_guard<std::mutex> l(*_queue_blocks_lock[_flag_queue_idx]));
124
45.1k
        if (_cur_blocks_nums_in_queue[_flag_queue_idx] > 0) {
125
8.82k
            *output_block = std::move(_queue_blocks[_flag_queue_idx].front());
126
8.82k
            _queue_blocks[_flag_queue_idx].pop_front();
127
8.82k
            if (child_idx) {
128
8.82k
                *child_idx = _flag_queue_idx;
129
8.82k
            }
130
8.82k
            _cur_bytes_in_queue[_flag_queue_idx] -= (*output_block)->allocated_bytes();
131
8.82k
            _cur_blocks_nums_in_queue[_flag_queue_idx] -= 1;
132
8.82k
            if (_cur_blocks_nums_in_queue[_flag_queue_idx] == 0) {
133
8.75k
                _sink_dependencies[_flag_queue_idx]->set_ready();
134
8.75k
            }
135
8.82k
            auto old_value = _cur_blocks_total_nums.fetch_sub(1);
136
8.82k
            if (old_value == 1 && _source_dependency) {
137
8.34k
                set_source_block();
138
8.34k
            }
139
8.82k
        }
140
45.1k
    }
141
45.1k
    return Status::OK();
142
45.1k
}
143
144
8.80k
Status DataQueue::push_block(std::unique_ptr<Block> block, int child_idx) {
145
8.80k
    if (!block) {
146
0
        return Status::OK();
147
0
    }
148
8.80k
    {
149
8.80k
        INJECT_MOCK_SLEEP(std::lock_guard<std::mutex> l(*_queue_blocks_lock[child_idx]));
150
8.80k
        if (_is_finished[child_idx]) {
151
0
            return Status::EndOfFile("Already finish");
152
0
        }
153
8.80k
        _cur_bytes_in_queue[child_idx] += block->allocated_bytes();
154
8.80k
        _queue_blocks[child_idx].emplace_back(std::move(block));
155
8.80k
        _cur_blocks_nums_in_queue[child_idx] += 1;
156
157
8.80k
        if (_cur_blocks_nums_in_queue[child_idx] > _max_blocks_in_sub_queue) {
158
77
            _sink_dependencies[child_idx]->block();
159
77
        }
160
8.80k
        _cur_blocks_total_nums++;
161
162
8.80k
        set_source_ready();
163
8.80k
    }
164
0
    return Status::OK();
165
8.80k
}
166
167
17.3k
void DataQueue::set_finish(int child_idx) {
168
17.3k
    INJECT_MOCK_SLEEP(std::lock_guard<std::mutex> l(*_queue_blocks_lock[child_idx]));
169
17.3k
    if (_is_finished[child_idx]) {
170
8.65k
        return;
171
8.65k
    }
172
8.66k
    _is_finished[child_idx] = true;
173
8.66k
    if (_un_finished_counter.fetch_sub(1) == 1) {
174
4.20k
        _is_all_finished = true;
175
4.20k
    }
176
8.66k
    set_source_ready();
177
8.66k
}
178
179
0
void DataQueue::set_canceled(int child_idx) {
180
0
    INJECT_MOCK_SLEEP(std::lock_guard<std::mutex> l(*_queue_blocks_lock[child_idx]));
181
0
    DCHECK(!_is_finished[child_idx]);
182
0
    _is_canceled[child_idx] = true;
183
0
    _is_finished[child_idx] = true;
184
0
    if (_un_finished_counter.fetch_sub(1) == 1) {
185
0
        _is_all_finished = true;
186
0
    }
187
0
    set_source_ready();
188
0
}
189
190
3
bool DataQueue::is_finish(int child_idx) {
191
3
    return _is_finished[child_idx];
192
3
}
193
194
53.0k
bool DataQueue::is_all_finish() {
195
53.0k
    return _is_all_finished;
196
53.0k
}
197
198
17.5k
void DataQueue::set_source_ready() {
199
17.5k
    if (_source_dependency) {
200
17.5k
        std::unique_lock lc(_source_lock);
201
17.5k
        _source_dependency->set_ready();
202
17.5k
    }
203
17.5k
}
204
205
8.34k
void DataQueue::set_source_block() {
206
8.34k
    if (_cur_blocks_total_nums == 0 && !is_all_finish()) {
207
4.17k
        std::unique_lock lc(_source_lock);
208
        // Performing the judgment twice, attempting to avoid blocking the source as much as possible.
209
4.18k
        if (_cur_blocks_total_nums == 0 && !is_all_finish()) {
210
4.17k
            _source_dependency->block();
211
4.17k
        }
212
4.17k
    }
213
8.34k
}
214
215
} // namespace doris