Coverage Report

Created: 2026-03-13 03:47

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