forked from apache/doris
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_queue.cpp
More file actions
244 lines (214 loc) · 7.13 KB
/
Copy pathdata_queue.cpp
File metadata and controls
244 lines (214 loc) · 7.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
#include "exec/operator/data_queue.h"
#include <glog/logging.h>
#include <algorithm>
#include <utility>
#include "common/thread_safety_annotations.h"
#include "core/block/block.h"
#include "exec/pipeline/dependency.h"
namespace doris {
void SubQueue::try_pop(std::unique_ptr<Block>* output_block) {
LockGuard l(queue_lock);
if (!blocks.empty()) {
*output_block = std::move(blocks.front());
blocks.pop_front();
bytes_in_queue -= (*output_block)->allocated_bytes();
blocks_in_queue -= 1;
if (blocks.empty()) {
sink_dependency->set_ready();
}
}
}
bool SubQueue::try_push(std::unique_ptr<Block> block, std::atomic_uint32_t& total_counter) {
LockGuard l(queue_lock);
if (is_finished) {
return false;
}
total_counter++;
bytes_in_queue += block->allocated_bytes();
blocks.emplace_back(std::move(block));
blocks_in_queue += 1;
if (static_cast<int64_t>(blocks.size()) > max_blocks_in_queue.load()) {
sink_dependency->block();
}
return true;
}
bool SubQueue::mark_finished(std::atomic_uint32_t& unfinished_counter,
std::atomic_bool& all_finished) {
LockGuard l(queue_lock);
if (is_finished) {
return false;
}
is_finished = true;
if (unfinished_counter.fetch_sub(1) == 1) {
all_finished = true;
}
return true;
}
void SubQueue::clear_blocks() {
bool need_set_always_ready = false;
{
LockGuard l(queue_lock);
if (!blocks.empty()) {
blocks.clear();
bytes_in_queue = 0;
blocks_in_queue = 0;
need_set_always_ready = true;
}
}
// Notify outside of queue_lock to keep lock ordering simple.
if (need_set_always_ready) {
sink_dependency->set_always_ready();
}
}
DataQueue::DataQueue(int child_count) : _sub_queues(child_count), _child_count(child_count) {
for (auto& sub : _sub_queues) {
sub = std::make_unique<SubQueue>();
}
_un_finished_counter = child_count;
}
bool DataQueue::has_more_data() const {
return _cur_blocks_total_nums.load() > 0;
}
void DataQueue::set_source_dependency(std::shared_ptr<Dependency> source_dependency)
NO_THREAD_SAFETY_ANALYSIS {
_source_dependency = std::move(source_dependency);
}
void DataQueue::set_sink_dependency(Dependency* sink_dependency, int child_idx) {
_sub_queues[child_idx]->sink_dependency = sink_dependency;
}
void DataQueue::set_max_blocks_in_sub_queue(int64_t max_blocks) {
for (auto& sub : _sub_queues) {
sub->max_blocks_in_queue = max_blocks;
}
}
void DataQueue::set_low_memory_mode() {
_is_low_memory_mode = true;
for (auto& sub : _sub_queues) {
sub->max_blocks_in_queue = 1;
}
clear_free_blocks();
}
std::unique_ptr<Block> DataQueue::get_free_block(int child_idx) {
auto& sub = *_sub_queues[child_idx];
{
LockGuard l(sub.free_lock);
if (!sub.free_blocks.empty()) {
auto block = std::move(sub.free_blocks.front());
sub.free_blocks.pop_front();
return block;
}
}
return Block::create_unique();
}
void DataQueue::push_free_block(std::unique_ptr<Block> block, int child_idx) {
DCHECK(block->rows() == 0);
if (!_is_low_memory_mode) {
auto& sub = *_sub_queues[child_idx];
LockGuard l(sub.free_lock);
sub.free_blocks.emplace_back(std::move(block));
}
}
void DataQueue::clear_free_blocks() {
for (auto& sub : _sub_queues) {
LockGuard l(sub->free_lock);
std::deque<std::unique_ptr<Block>> tmp_queue;
sub->free_blocks.swap(tmp_queue);
}
}
void DataQueue::terminate() {
for (int i = 0; i < _child_count; ++i) {
set_finish(i);
_sub_queues[i]->clear_blocks();
}
clear_free_blocks();
}
//check which queue have data, and save the idx in _flag_queue_idx,
//so next loop, will check the record idx + 1 first
//maybe it's useful with many queue, others maybe always 0
bool DataQueue::remaining_has_data() {
int count = _child_count;
while (--count >= 0) {
_flag_queue_idx++;
if (_flag_queue_idx == _child_count) {
_flag_queue_idx = 0;
}
if (_sub_queues[_flag_queue_idx]->blocks_in_queue.load() > 0) {
return true;
}
}
return false;
}
//the _flag_queue_idx indicate which queue has data, and in check can_read
//will be set idx in remaining_has_data function
Status DataQueue::get_block_from_queue(std::unique_ptr<Block>* output_block, int* child_idx) {
const int idx = _flag_queue_idx;
auto& sub = *_sub_queues[idx];
sub.try_pop(output_block);
if (*output_block) {
if (child_idx) {
*child_idx = idx;
}
auto old_total = _cur_blocks_total_nums.fetch_sub(1);
if (old_total == 1) {
set_source_block();
}
}
return Status::OK();
}
Status DataQueue::push_block(std::unique_ptr<Block> block, int child_idx) {
if (!block) {
return Status::OK();
}
auto& sub = *_sub_queues[child_idx];
// total_counter is incremented inside try_push under queue_lock, only when the
// block is actually enqueued. This ensures get_block_from_queue() always observes
// _cur_blocks_total_nums >= 1 when it successfully pops a block, with no risk of
// underflow or the need for a rollback on failure.
if (!sub.try_push(std::move(block), _cur_blocks_total_nums)) {
return Status::EndOfFile("SubQueue already finished");
}
set_source_ready();
return Status::OK();
}
void DataQueue::set_finish(int child_idx) {
auto& sub = *_sub_queues[child_idx];
if (!sub.mark_finished(_un_finished_counter, _is_all_finished)) {
return;
}
set_source_ready();
}
bool DataQueue::is_all_finish() {
return _is_all_finished;
}
void DataQueue::set_source_ready() {
LockGuard lc(_source_lock);
if (_source_dependency) {
_source_dependency->set_ready();
}
}
void DataQueue::set_source_block() {
// Re-check under _source_lock to avoid blocking the source when a concurrent push
// has already added new blocks (or all children have finished) since we observed
// the counter drop to zero.
LockGuard lc(_source_lock);
if (_source_dependency && _cur_blocks_total_nums == 0 && !is_all_finish()) {
_source_dependency->block();
}
}
} // namespace doris