Coverage Report

Created: 2026-07-19 09:42

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 <utility>
24
25
#include "common/thread_safety_annotations.h"
26
#include "core/block/block.h"
27
#include "exec/pipeline/dependency.h"
28
#include "fmt/format.h"
29
30
namespace doris {
31
32
9.08k
void SubQueue::try_pop(std::unique_ptr<Block>* output_block) {
33
9.08k
    LockGuard l(queue_lock);
34
9.08k
    if (!blocks.empty()) {
35
9.08k
        *output_block = std::move(blocks.front());
36
9.08k
        blocks.pop_front();
37
9.08k
        bytes_in_queue -= (*output_block)->allocated_bytes();
38
9.08k
        blocks_in_queue -= 1;
39
9.08k
        if (blocks.empty()) {
40
8.09k
            sink_dependency->set_ready();
41
8.09k
        }
42
9.08k
    }
43
9.08k
}
44
45
9.16k
bool SubQueue::try_push(std::unique_ptr<Block> block, std::atomic_uint32_t& total_counter) {
46
9.16k
    LockGuard l(queue_lock);
47
9.16k
    if (is_finished) {
48
43
        return false;
49
43
    }
50
9.12k
    total_counter++;
51
9.12k
    bytes_in_queue += block->allocated_bytes();
52
9.12k
    blocks.emplace_back(std::move(block));
53
9.12k
    blocks_in_queue += 1;
54
9.12k
    if (static_cast<int64_t>(blocks.size()) > max_blocks_in_queue.load()) {
55
1.00k
        sink_dependency->block();
56
1.00k
    }
57
9.12k
    return true;
58
9.16k
}
59
60
bool SubQueue::mark_finished(std::atomic_uint32_t& unfinished_counter,
61
12.2k
                             std::atomic_bool& all_finished) {
62
12.2k
    LockGuard l(queue_lock);
63
12.2k
    if (is_finished) {
64
6.09k
        return false;
65
6.09k
    }
66
6.20k
    is_finished = true;
67
6.20k
    if (unfinished_counter.fetch_sub(1) == 1) {
68
2.96k
        all_finished = true;
69
2.96k
    }
70
6.20k
    return true;
71
12.2k
}
72
73
6.15k
void SubQueue::clear_blocks() {
74
6.15k
    bool need_set_always_ready = false;
75
6.15k
    {
76
6.15k
        LockGuard l(queue_lock);
77
6.15k
        if (!blocks.empty()) {
78
38
            blocks.clear();
79
38
            bytes_in_queue = 0;
80
38
            blocks_in_queue = 0;
81
38
            need_set_always_ready = true;
82
38
        }
83
6.15k
    }
84
    // Notify outside of queue_lock to keep lock ordering simple.
85
6.15k
    if (need_set_always_ready) {
86
38
        sink_dependency->set_always_ready();
87
38
    }
88
6.15k
}
89
90
2.97k
DataQueue::DataQueue(int child_count) : _sub_queues(child_count), _child_count(child_count) {
91
6.22k
    for (auto& sub : _sub_queues) {
92
6.22k
        sub = std::make_unique<SubQueue>();
93
6.22k
    }
94
2.97k
    _un_finished_counter = child_count;
95
2.97k
}
96
97
3.94k
bool DataQueue::has_more_data() const {
98
3.94k
    return _cur_blocks_total_nums.load() > 0;
99
3.94k
}
100
101
5
std::string DataQueue::debug_string() const {
102
5
    return fmt::format("(is_all_finish = {}, has_data = {})", is_all_finish(), has_more_data());
103
5
}
104
105
void DataQueue::set_source_dependency(std::shared_ptr<Dependency> source_dependency)
106
2.96k
        NO_THREAD_SAFETY_ANALYSIS {
107
2.96k
    _source_dependency = std::move(source_dependency);
108
2.96k
}
109
110
6.21k
void DataQueue::set_sink_dependency(Dependency* sink_dependency, int child_idx) {
111
6.21k
    _sub_queues[child_idx]->sink_dependency = sink_dependency;
112
6.21k
}
113
114
6.17k
void DataQueue::set_max_blocks_in_sub_queue(int64_t max_blocks) {
115
14.2k
    for (auto& sub : _sub_queues) {
116
14.2k
        sub->max_blocks_in_queue = max_blocks;
117
14.2k
    }
118
6.17k
}
119
120
1
void DataQueue::set_low_memory_mode() {
121
1
    _is_low_memory_mode = true;
122
3
    for (auto& sub : _sub_queues) {
123
3
        sub->max_blocks_in_queue = 1;
124
3
    }
125
1
    clear_free_blocks();
126
1
}
127
128
8.99k
std::unique_ptr<Block> DataQueue::get_free_block(int child_idx) {
129
8.99k
    auto& sub = *_sub_queues[child_idx];
130
8.99k
    {
131
8.99k
        LockGuard l(sub.free_lock);
132
8.99k
        if (!sub.free_blocks.empty()) {
133
1.91k
            auto block = std::move(sub.free_blocks.front());
134
1.91k
            sub.free_blocks.pop_front();
135
1.91k
            return block;
136
1.91k
        }
137
8.99k
    }
138
139
7.08k
    return Block::create_unique();
140
8.99k
}
141
142
8.91k
void DataQueue::push_free_block(DataQueueBlock&& queue_block) {
143
8.91k
    if (!queue_block.block) {
144
0
        return;
145
0
    }
146
8.91k
    DCHECK(queue_block.block->rows() == 0);
147
148
8.91k
    if (!_is_low_memory_mode) {
149
8.90k
        auto& sub = *_sub_queues[queue_block.child_idx];
150
8.90k
        LockGuard l(sub.free_lock);
151
8.90k
        sub.free_blocks.emplace_back(std::move(queue_block.block));
152
8.90k
    }
153
8.91k
}
154
155
2.93k
void DataQueue::clear_free_blocks() {
156
6.16k
    for (auto& sub : _sub_queues) {
157
6.16k
        LockGuard l(sub->free_lock);
158
6.16k
        std::deque<std::unique_ptr<Block>> tmp_queue;
159
6.16k
        sub->free_blocks.swap(tmp_queue);
160
6.16k
    }
161
2.93k
}
162
163
2.93k
void DataQueue::terminate() {
164
9.09k
    for (int i = 0; i < _child_count; ++i) {
165
6.16k
        mark_finish(i);
166
6.16k
        _sub_queues[i]->clear_blocks();
167
6.16k
    }
168
2.93k
    _cur_blocks_total_nums = 0;
169
2.93k
    clear_free_blocks();
170
2.93k
    set_source_always_ready();
171
2.93k
}
172
173
9.08k
Result<DataQueueBlock> DataQueue::get_block_from_queue() {
174
9.08k
    DataQueueBlock result;
175
9.08k
    const int start_idx = (_flag_queue_idx + 1) % _child_count;
176
12.9k
    for (int offset = 0; offset < _child_count; ++offset) {
177
12.9k
        const int idx = (start_idx + offset) % _child_count;
178
12.9k
        if (_sub_queues[idx]->blocks_in_queue.load() == 0) {
179
3.87k
            continue;
180
3.87k
        }
181
182
9.08k
        auto& sub = *_sub_queues[idx];
183
9.08k
        sub.try_pop(&result.block);
184
9.08k
        if (!result.block) {
185
0
            continue;
186
0
        }
187
9.08k
        result.child_idx = idx;
188
9.08k
        _flag_queue_idx = idx;
189
9.08k
        auto old_total = _cur_blocks_total_nums.fetch_sub(1);
190
9.08k
        if (old_total == 1) {
191
7.14k
            set_source_block();
192
7.14k
        }
193
9.08k
        break;
194
9.08k
    }
195
196
    // A producer enqueues its final block before marking the child finished. Observe completion
197
    // first, then recheck queued data to avoid reporting EOS while the final block is still queued.
198
9.08k
    result.eos = is_all_finish() && !has_more_data();
199
9.08k
    return result;
200
9.08k
}
201
202
9.18k
Status DataQueue::push_block(std::unique_ptr<Block> block, int child_idx, bool eos) {
203
9.18k
    DCHECK(block || eos);
204
9.18k
    if (!block && !eos) {
205
0
        return Status::OK();
206
0
    }
207
208
9.18k
    if (block) {
209
9.15k
        auto& sub = *_sub_queues[child_idx];
210
        // total_counter is incremented inside try_push under queue_lock, only when the
211
        // block is actually enqueued. This ensures get_block_from_queue() always observes
212
        // _cur_blocks_total_nums >= 1 when it successfully pops a block, with no risk of
213
        // underflow or the need for a rollback on failure.
214
9.15k
        if (!sub.try_push(std::move(block), _cur_blocks_total_nums)) {
215
42
            return Status::EndOfFile("SubQueue already finished");
216
42
        }
217
9.15k
    }
218
219
9.13k
    if (eos) {
220
6.13k
        mark_finish(child_idx);
221
6.13k
    }
222
9.13k
    set_source_ready();
223
9.13k
    return Status::OK();
224
9.18k
}
225
226
12.2k
void DataQueue::mark_finish(int child_idx) {
227
12.2k
    auto& sub = *_sub_queues[child_idx];
228
12.2k
    if (!sub.mark_finished(_un_finished_counter, _is_all_finished)) {
229
6.09k
        return;
230
6.09k
    }
231
12.2k
}
232
233
16.2k
bool DataQueue::is_all_finish() const {
234
16.2k
    return _is_all_finished;
235
16.2k
}
236
237
9.15k
void DataQueue::set_source_ready() {
238
9.15k
    LockGuard lc(_source_lock);
239
9.15k
    if (_source_dependency) {
240
9.14k
        _source_dependency->set_ready();
241
9.14k
    }
242
9.15k
}
243
244
2.93k
void DataQueue::set_source_always_ready() {
245
2.93k
    LockGuard lc(_source_lock);
246
2.93k
    if (_source_dependency) {
247
2.93k
        _source_dependency->set_always_ready();
248
2.93k
    }
249
2.93k
}
250
251
7.14k
void DataQueue::set_source_block() {
252
    // Re-check under _source_lock to avoid blocking the source when a concurrent push
253
    // has already added new blocks (or all children have finished) since we observed
254
    // the counter drop to zero.
255
7.14k
    LockGuard lc(_source_lock);
256
7.14k
    if (_source_dependency && _cur_blocks_total_nums == 0 && !is_all_finish()) {
257
4.24k
        _source_dependency->block();
258
4.24k
    }
259
7.14k
}
260
261
} // namespace doris