Coverage Report

Created: 2026-07-20 19:43

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
177
void SubQueue::try_pop(std::unique_ptr<Block>* output_block) {
33
177
    LockGuard l(queue_lock);
34
177
    if (!blocks.empty()) {
35
176
        *output_block = std::move(blocks.front());
36
176
        blocks.pop_front();
37
176
        bytes_in_queue -= (*output_block)->allocated_bytes();
38
176
        blocks_in_queue -= 1;
39
176
        if (blocks.empty()) {
40
110
            sink_dependency->set_ready();
41
110
        }
42
176
    }
43
177
}
44
45
193
bool SubQueue::try_push(std::unique_ptr<Block> block, std::atomic_uint32_t& total_counter) {
46
193
    LockGuard l(queue_lock);
47
193
    if (is_finished) {
48
1
        return false;
49
1
    }
50
192
    total_counter++;
51
192
    bytes_in_queue += block->allocated_bytes();
52
192
    blocks.emplace_back(std::move(block));
53
192
    blocks_in_queue += 1;
54
192
    if (static_cast<int64_t>(blocks.size()) > max_blocks_in_queue.load()) {
55
81
        sink_dependency->block();
56
81
    }
57
192
    return true;
58
193
}
59
60
bool SubQueue::mark_finished(std::atomic_uint32_t& unfinished_counter,
61
34
                             std::atomic_bool& all_finished) {
62
34
    LockGuard l(queue_lock);
63
34
    if (is_finished) {
64
2
        return false;
65
2
    }
66
32
    is_finished = true;
67
32
    if (unfinished_counter.fetch_sub(1) == 1) {
68
18
        all_finished = true;
69
18
    }
70
32
    return true;
71
34
}
72
73
5
void SubQueue::clear_blocks() {
74
5
    bool need_set_always_ready = false;
75
5
    {
76
5
        LockGuard l(queue_lock);
77
5
        if (!blocks.empty()) {
78
3
            blocks.clear();
79
3
            bytes_in_queue = 0;
80
3
            blocks_in_queue = 0;
81
3
            need_set_always_ready = true;
82
3
        }
83
5
    }
84
    // Notify outside of queue_lock to keep lock ordering simple.
85
5
    if (need_set_always_ready) {
86
3
        sink_dependency->set_always_ready();
87
3
    }
88
5
}
89
90
26
DataQueue::DataQueue(int child_count) : _sub_queues(child_count), _child_count(child_count) {
91
54
    for (auto& sub : _sub_queues) {
92
54
        sub = std::make_unique<SubQueue>();
93
54
    }
94
26
    _un_finished_counter = child_count;
95
26
}
96
97
29
bool DataQueue::has_more_data() const {
98
29
    return _cur_blocks_total_nums.load() > 0;
99
29
}
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
26
        NO_THREAD_SAFETY_ANALYSIS {
107
26
    _source_dependency = std::move(source_dependency);
108
26
}
109
110
53
void DataQueue::set_sink_dependency(Dependency* sink_dependency, int child_idx) {
111
53
    _sub_queues[child_idx]->sink_dependency = sink_dependency;
112
53
}
113
114
15
void DataQueue::set_max_blocks_in_sub_queue(int64_t max_blocks) {
115
23
    for (auto& sub : _sub_queues) {
116
23
        sub->max_blocks_in_queue = max_blocks;
117
23
    }
118
15
}
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
15
std::unique_ptr<Block> DataQueue::get_free_block(int child_idx) {
129
15
    auto& sub = *_sub_queues[child_idx];
130
15
    {
131
15
        LockGuard l(sub.free_lock);
132
15
        if (!sub.free_blocks.empty()) {
133
3
            auto block = std::move(sub.free_blocks.front());
134
3
            sub.free_blocks.pop_front();
135
3
            return block;
136
3
        }
137
15
    }
138
139
12
    return Block::create_unique();
140
15
}
141
142
14
void DataQueue::push_free_block(DataQueueBlock&& queue_block) {
143
14
    if (!queue_block.block) {
144
0
        return;
145
0
    }
146
14
    DCHECK(queue_block.block->rows() == 0);
147
148
14
    if (!_is_low_memory_mode) {
149
13
        auto& sub = *_sub_queues[queue_block.child_idx];
150
13
        LockGuard l(sub.free_lock);
151
13
        sub.free_blocks.emplace_back(std::move(queue_block.block));
152
13
    }
153
14
}
154
155
2
void DataQueue::clear_free_blocks() {
156
6
    for (auto& sub : _sub_queues) {
157
6
        LockGuard l(sub->free_lock);
158
6
        std::deque<std::unique_ptr<Block>> tmp_queue;
159
6
        sub->free_blocks.swap(tmp_queue);
160
6
    }
161
2
}
162
163
1
void DataQueue::terminate() {
164
4
    for (int i = 0; i < _child_count; ++i) {
165
3
        mark_finish(i);
166
3
        _sub_queues[i]->clear_blocks();
167
3
    }
168
1
    _cur_blocks_total_nums = 0;
169
1
    clear_free_blocks();
170
1
    set_source_always_ready();
171
1
}
172
173
390
Result<DataQueueBlock> DataQueue::get_block_from_queue() {
174
390
    DataQueueBlock result;
175
390
    const int start_idx = (_flag_queue_idx + 1) % _child_count;
176
1.07k
    for (int offset = 0; offset < _child_count; ++offset) {
177
851
        const int idx = (start_idx + offset) % _child_count;
178
851
        if (_sub_queues[idx]->blocks_in_queue.load() == 0) {
179
680
            continue;
180
680
        }
181
182
171
        auto& sub = *_sub_queues[idx];
183
171
        sub.try_pop(&result.block);
184
171
        if (!result.block) {
185
0
            continue;
186
0
        }
187
171
        result.child_idx = idx;
188
171
        _flag_queue_idx = idx;
189
171
        auto old_total = _cur_blocks_total_nums.fetch_sub(1);
190
171
        if (old_total == 1) {
191
19
            set_source_block();
192
19
        }
193
171
        break;
194
171
    }
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
390
    result.eos = is_all_finish() && !has_more_data();
199
390
    return result;
200
390
}
201
202
196
Status DataQueue::push_block(std::unique_ptr<Block> block, int child_idx, bool eos) {
203
196
    DCHECK(block || eos);
204
196
    if (!block && !eos) {
205
0
        return Status::OK();
206
0
    }
207
208
196
    if (block) {
209
182
        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
182
        if (!sub.try_push(std::move(block), _cur_blocks_total_nums)) {
215
0
            return Status::EndOfFile("SubQueue already finished");
216
0
        }
217
182
    }
218
219
196
    if (eos) {
220
27
        mark_finish(child_idx);
221
27
    }
222
196
    set_source_ready();
223
196
    return Status::OK();
224
196
}
225
226
30
void DataQueue::mark_finish(int child_idx) {
227
30
    auto& sub = *_sub_queues[child_idx];
228
30
    if (!sub.mark_finished(_un_finished_counter, _is_all_finished)) {
229
1
        return;
230
1
    }
231
30
}
232
233
412
bool DataQueue::is_all_finish() const {
234
412
    return _is_all_finished;
235
412
}
236
237
196
void DataQueue::set_source_ready() {
238
196
    LockGuard lc(_source_lock);
239
196
    if (_source_dependency) {
240
196
        _source_dependency->set_ready();
241
196
    }
242
196
}
243
244
1
void DataQueue::set_source_always_ready() {
245
1
    LockGuard lc(_source_lock);
246
1
    if (_source_dependency) {
247
1
        _source_dependency->set_always_ready();
248
1
    }
249
1
}
250
251
19
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
19
    LockGuard lc(_source_lock);
256
19
    if (_source_dependency && _cur_blocks_total_nums == 0 && !is_all_finish()) {
257
7
        _source_dependency->block();
258
7
    }
259
19
}
260
261
} // namespace doris