Coverage Report

Created: 2026-03-25 07:39

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.60k
        : _queue_blocks_lock(child_count),
33
4.60k
          _queue_blocks(child_count),
34
4.60k
          _free_blocks_lock(child_count),
35
4.60k
          _free_blocks(child_count),
36
4.60k
          _child_count(child_count),
37
4.60k
          _is_finished(child_count),
38
4.60k
          _is_canceled(child_count),
39
4.60k
          _cur_bytes_in_queue(child_count),
40
4.60k
          _cur_blocks_nums_in_queue(child_count),
41
4.60k
          _flag_queue_idx(0) {
42
14.3k
    for (int i = 0; i < child_count; ++i) {
43
9.70k
        _queue_blocks_lock[i].reset(new std::mutex());
44
9.70k
        _free_blocks_lock[i].reset(new std::mutex());
45
9.70k
        _is_finished[i] = false;
46
9.70k
        _is_canceled[i] = false;
47
9.70k
        _cur_bytes_in_queue[i] = 0;
48
9.70k
        _cur_blocks_nums_in_queue[i] = 0;
49
9.70k
    }
50
4.60k
    _un_finished_counter = child_count;
51
4.60k
    _sink_dependencies.resize(child_count, nullptr);
52
4.60k
}
53
54
9.63k
std::unique_ptr<Block> DataQueue::get_free_block(int child_idx) {
55
9.63k
    {
56
9.63k
        INJECT_MOCK_SLEEP(std::lock_guard<std::mutex> l(*_free_blocks_lock[child_idx]));
57
9.63k
        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
9.63k
    }
63
64
9.62k
    return Block::create_unique();
65
9.63k
}
66
67
9.63k
void DataQueue::push_free_block(std::unique_ptr<Block> block, int child_idx) {
68
9.63k
    DCHECK(block->rows() == 0);
69
70
9.63k
    if (!_is_low_memory_mode) {
71
9.63k
        INJECT_MOCK_SLEEP(std::lock_guard<std::mutex> l(*_free_blocks_lock[child_idx]));
72
9.63k
        _free_blocks[child_idx].emplace_back(std::move(block));
73
9.63k
    }
74
9.63k
}
75
76
4.49k
void DataQueue::clear_free_blocks() {
77
14.1k
    for (size_t child_idx = 0; child_idx < _free_blocks.size(); ++child_idx) {
78
9.61k
        std::lock_guard<std::mutex> l(*_free_blocks_lock[child_idx]);
79
9.61k
        std::deque<std::unique_ptr<Block>> tmp_queue;
80
9.61k
        _free_blocks[child_idx].swap(tmp_queue);
81
9.61k
    }
82
4.49k
}
83
84
4.49k
void DataQueue::terminate() {
85
14.1k
    for (int i = 0; i < _queue_blocks.size(); i++) {
86
9.62k
        set_finish(i);
87
9.62k
        INJECT_MOCK_SLEEP(std::lock_guard<std::mutex> l(*_queue_blocks_lock[i]));
88
9.62k
        if (_cur_blocks_nums_in_queue[i] > 0) {
89
3
            _queue_blocks[i].clear();
90
3
            _cur_bytes_in_queue[i] = 0;
91
3
            _cur_blocks_nums_in_queue[i] = 0;
92
3
            _sink_dependencies[i]->set_always_ready();
93
3
        }
94
9.62k
    }
95
4.49k
    clear_free_blocks();
96
4.49k
}
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
55.7k
bool DataQueue::remaining_has_data() {
102
55.7k
    int count = _child_count;
103
172k
    while (--count >= 0) {
104
126k
        _flag_queue_idx++;
105
126k
        if (_flag_queue_idx == _child_count) {
106
50.9k
            _flag_queue_idx = 0;
107
50.9k
        }
108
126k
        if (_cur_blocks_nums_in_queue[_flag_queue_idx] > 0) {
109
9.78k
            return true;
110
9.78k
        }
111
126k
    }
112
46.0k
    return false;
113
55.7k
}
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
46.8k
Status DataQueue::get_block_from_queue(std::unique_ptr<Block>* output_block, int* child_idx) {
118
46.8k
    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
46.8k
    {
124
46.8k
        INJECT_MOCK_SLEEP(std::lock_guard<std::mutex> l(*_queue_blocks_lock[_flag_queue_idx]));
125
46.8k
        if (_cur_blocks_nums_in_queue[_flag_queue_idx] > 0) {
126
9.80k
            *output_block = std::move(_queue_blocks[_flag_queue_idx].front());
127
9.80k
            _queue_blocks[_flag_queue_idx].pop_front();
128
9.80k
            if (child_idx) {
129
9.80k
                *child_idx = _flag_queue_idx;
130
9.80k
            }
131
9.80k
            _cur_bytes_in_queue[_flag_queue_idx] -= (*output_block)->allocated_bytes();
132
9.80k
            _cur_blocks_nums_in_queue[_flag_queue_idx] -= 1;
133
9.80k
            if (_cur_blocks_nums_in_queue[_flag_queue_idx] == 0) {
134
9.72k
                _sink_dependencies[_flag_queue_idx]->set_ready();
135
9.72k
            }
136
9.80k
            auto old_value = _cur_blocks_total_nums.fetch_sub(1);
137
9.80k
            if (old_value == 1 && _source_dependency) {
138
9.23k
                set_source_block();
139
9.23k
            }
140
9.80k
        }
141
46.8k
    }
142
46.8k
    return Status::OK();
143
46.8k
}
144
145
9.79k
Status DataQueue::push_block(std::unique_ptr<Block> block, int child_idx) {
146
9.79k
    if (!block) {
147
0
        return Status::OK();
148
0
    }
149
9.79k
    {
150
9.79k
        INJECT_MOCK_SLEEP(std::lock_guard<std::mutex> l(*_queue_blocks_lock[child_idx]));
151
9.79k
        if (_is_finished[child_idx]) {
152
0
            return Status::EndOfFile("Already finish");
153
0
        }
154
9.79k
        _cur_bytes_in_queue[child_idx] += block->allocated_bytes();
155
9.79k
        _queue_blocks[child_idx].emplace_back(std::move(block));
156
9.79k
        _cur_blocks_nums_in_queue[child_idx] += 1;
157
158
9.79k
        if (_cur_blocks_nums_in_queue[child_idx] > _max_blocks_in_sub_queue) {
159
84
            _sink_dependencies[child_idx]->block();
160
84
        }
161
9.79k
        _cur_blocks_total_nums++;
162
163
9.79k
        set_source_ready();
164
9.79k
    }
165
0
    return Status::OK();
166
9.79k
}
167
168
19.3k
void DataQueue::set_finish(int child_idx) {
169
19.3k
    INJECT_MOCK_SLEEP(std::lock_guard<std::mutex> l(*_queue_blocks_lock[child_idx]));
170
19.3k
    if (_is_finished[child_idx]) {
171
9.62k
        return;
172
9.62k
    }
173
9.72k
    _is_finished[child_idx] = true;
174
9.72k
    if (_un_finished_counter.fetch_sub(1) == 1) {
175
4.60k
        _is_all_finished = true;
176
4.60k
    }
177
9.72k
    set_source_ready();
178
9.72k
}
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
55.5k
bool DataQueue::is_all_finish() {
196
55.5k
    return _is_all_finished;
197
55.5k
}
198
199
19.5k
void DataQueue::set_source_ready() {
200
19.5k
    if (_source_dependency) {
201
19.5k
        std::unique_lock lc(_source_lock);
202
19.5k
        _source_dependency->set_ready();
203
19.5k
    }
204
19.5k
}
205
206
9.23k
void DataQueue::set_source_block() {
207
9.23k
    if (_cur_blocks_total_nums == 0 && !is_all_finish()) {
208
4.74k
        std::unique_lock lc(_source_lock);
209
        // Performing the judgment twice, attempting to avoid blocking the source as much as possible.
210
4.74k
        if (_cur_blocks_total_nums == 0 && !is_all_finish()) {
211
4.74k
            _source_dependency->block();
212
4.74k
        }
213
4.74k
    }
214
9.23k
}
215
216
} // namespace doris