be/src/exec/exchange/vdata_stream_recvr.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/exchange/vdata_stream_recvr.h" |
19 | | |
20 | | #include <fmt/format.h> |
21 | | #include <gen_cpp/Metrics_types.h> |
22 | | #include <gen_cpp/Types_types.h> |
23 | | #include <gen_cpp/data.pb.h> |
24 | | |
25 | | #include <algorithm> |
26 | | #include <functional> |
27 | | #include <string> |
28 | | |
29 | | #include "common/logging.h" |
30 | | #include "core/block/block.h" |
31 | | #include "core/block/materialize_block.h" |
32 | | #include "exec/exchange/vdata_stream_mgr.h" |
33 | | #include "exec/operator/exchange_sink_operator.h" |
34 | | #include "exec/operator/exchange_source_operator.h" |
35 | | #include "exec/sort/sort_cursor.h" |
36 | | #include "exec/sort/vsorted_run_merger.h" |
37 | | #include "runtime/memory/mem_tracker.h" |
38 | | #include "runtime/runtime_state.h" |
39 | | #include "runtime/thread_context.h" |
40 | | #include "util/defer_op.h" |
41 | | #include "util/uid_util.h" |
42 | | |
43 | | namespace doris { |
44 | | |
45 | | VDataStreamRecvr::SenderQueue::SenderQueue(VDataStreamRecvr* parent_recvr, int num_senders, |
46 | | std::shared_ptr<Dependency> local_channel_dependency) |
47 | 664k | : _recvr(parent_recvr), |
48 | 664k | _is_cancelled(false), |
49 | 664k | _num_remaining_senders(num_senders), |
50 | 664k | _local_channel_dependency(local_channel_dependency) { |
51 | 664k | _cancel_status = Status::OK(); |
52 | 664k | _queue_mem_tracker = std::make_unique<MemTracker>("local data queue mem tracker"); |
53 | 664k | } |
54 | | |
55 | 673k | VDataStreamRecvr::SenderQueue::~SenderQueue() { |
56 | 673k | for (auto& block_item : _block_queue) { |
57 | 0 | block_item.call_done(_recvr); |
58 | 0 | } |
59 | 673k | _block_queue.clear(); |
60 | 673k | } |
61 | | |
62 | 878k | Status VDataStreamRecvr::SenderQueue::get_batch(Block* block, bool* eos) { |
63 | 878k | #ifndef NDEBUG |
64 | 879k | if (!_is_cancelled && _block_queue.empty() && _num_remaining_senders > 0) { |
65 | 0 | throw doris::Exception(ErrorCode::INTERNAL_ERROR, |
66 | 0 | "_is_cancelled: {}, _block_queue_empty: {}, " |
67 | 0 | "_num_remaining_senders: {}", |
68 | 0 | _is_cancelled, _block_queue.empty(), _num_remaining_senders); |
69 | 0 | } |
70 | 878k | #endif |
71 | 878k | BlockItem block_item; |
72 | 878k | { |
73 | 878k | INJECT_MOCK_SLEEP(std::lock_guard<std::mutex> l(_lock)); |
74 | | //check and get block_item from data_queue |
75 | 878k | if (_is_cancelled) { |
76 | 1 | RETURN_IF_ERROR(_cancel_status); |
77 | 1 | return Status::Cancelled("Cancelled"); |
78 | 1 | } |
79 | | |
80 | 878k | if (_block_queue.empty()) { |
81 | 655k | if (_num_remaining_senders != 0) { |
82 | 0 | return Status::InternalError( |
83 | 0 | "Data queue is empty but there are still remaining senders. " |
84 | 0 | "_num_remaining_senders: {}", |
85 | 0 | _num_remaining_senders); |
86 | 0 | } |
87 | 655k | *eos = true; |
88 | 655k | return Status::OK(); |
89 | 655k | } |
90 | | |
91 | 878k | DCHECK(!_block_queue.empty()); |
92 | 223k | block_item = std::move(_block_queue.front()); |
93 | 223k | _block_queue.pop_front(); |
94 | 223k | } |
95 | 0 | BlockUPtr next_block; |
96 | 223k | RETURN_IF_ERROR(block_item.get_block(next_block)); |
97 | 223k | size_t block_byte_size = block_item.block_byte_size(); |
98 | 223k | COUNTER_UPDATE(_recvr->_deserialize_row_batch_timer, block_item.deserialize_time()); |
99 | 223k | COUNTER_UPDATE(_recvr->_decompress_timer, block_item.decompress_time()); |
100 | 223k | COUNTER_UPDATE(_recvr->_decompress_bytes, block_item.decompress_bytes()); |
101 | 223k | _recvr->_memory_used_counter->update(-(int64_t)block_byte_size); |
102 | 223k | INJECT_MOCK_SLEEP(std::lock_guard<std::mutex> l(_lock)); |
103 | 223k | sub_blocks_memory_usage(block_byte_size); |
104 | 223k | if (_block_queue.empty() && _source_dependency) { |
105 | 183k | if (!_is_cancelled && _num_remaining_senders > 0) { |
106 | 71.9k | _source_dependency->block(); |
107 | 71.9k | } |
108 | 183k | } |
109 | | |
110 | 223k | block_item.call_done(_recvr); |
111 | | |
112 | 223k | DCHECK(block->empty()); |
113 | 223k | block->swap(*next_block); |
114 | 223k | *eos = false; |
115 | 223k | return Status::OK(); |
116 | 223k | } |
117 | | |
118 | 1.56M | void VDataStreamRecvr::SenderQueue::set_source_ready(std::lock_guard<std::mutex>&) { |
119 | | // Here, it is necessary to check if _source_dependency is not nullptr. |
120 | | // This is because the queue might be closed before setting the source dependency. |
121 | 1.56M | if (!_source_dependency) { |
122 | 11 | return; |
123 | 11 | } |
124 | 1.56M | const bool should_wait = !_is_cancelled && _block_queue.empty() && _num_remaining_senders > 0; |
125 | 1.56M | if (!should_wait) { |
126 | 1.56M | _source_dependency->set_ready(); |
127 | 1.56M | } |
128 | 1.56M | } |
129 | | |
130 | 79 | std::string VDataStreamRecvr::SenderQueue::debug_string() { |
131 | 79 | fmt::memory_buffer debug_string_buffer; |
132 | 79 | fmt::format_to(debug_string_buffer, |
133 | 79 | "_num_remaining_senders = {}, block_queue size = {}, _is_cancelled: {}, " |
134 | 79 | "_cancel_status: {}, _sender_eos_set: (", |
135 | 79 | _num_remaining_senders, _block_queue.size(), _is_cancelled, |
136 | 79 | _cancel_status.to_string()); |
137 | 79 | std::lock_guard<std::mutex> l(_lock); |
138 | 79 | for (auto& i : _sender_eos_set) { |
139 | 0 | fmt::format_to(debug_string_buffer, "{}, ", i); |
140 | 0 | } |
141 | 79 | fmt::format_to(debug_string_buffer, ")"); |
142 | 79 | return fmt::to_string(debug_string_buffer); |
143 | 79 | } |
144 | | |
145 | | Status VDataStreamRecvr::SenderQueue::add_block(std::unique_ptr<PBlock> pblock, int be_number, |
146 | | int64_t packet_seq, |
147 | | ::google::protobuf::Closure** done, |
148 | | const int64_t wait_for_worker, |
149 | 499 | const uint64_t time_to_find_recvr) { |
150 | 499 | { |
151 | 499 | INJECT_MOCK_SLEEP(std::lock_guard<std::mutex> l(_lock)); |
152 | 499 | if (_is_cancelled) { |
153 | 0 | return Status::OK(); |
154 | 0 | } |
155 | 499 | auto iter = _packet_seq_map.find(be_number); |
156 | 499 | if (iter != _packet_seq_map.end()) { |
157 | 495 | if (iter->second >= packet_seq) { |
158 | 0 | return Status::InternalError( |
159 | 0 | "packet already exist [cur_packet_id= {} receive_packet_id={}]", |
160 | 0 | iter->second, packet_seq); |
161 | 0 | } |
162 | 495 | iter->second = packet_seq; |
163 | 495 | } else { |
164 | 4 | _packet_seq_map.emplace(be_number, packet_seq); |
165 | 4 | } |
166 | | |
167 | 499 | DCHECK(_num_remaining_senders >= 0); |
168 | 499 | if (_num_remaining_senders == 0) { |
169 | 0 | DCHECK(_sender_eos_set.contains(be_number)); |
170 | 0 | return Status::OK(); |
171 | 0 | } |
172 | 499 | } |
173 | | |
174 | 499 | INJECT_MOCK_SLEEP(std::lock_guard<std::mutex> l(_lock)); |
175 | 499 | if (_is_cancelled) { |
176 | 0 | return Status::OK(); |
177 | 0 | } |
178 | | |
179 | 499 | const auto block_byte_size = pblock->ByteSizeLong(); |
180 | 499 | COUNTER_UPDATE(_recvr->_blocks_produced_counter, 1); |
181 | 499 | if (_recvr->_max_wait_worker_time->value() < wait_for_worker) { |
182 | 0 | _recvr->_max_wait_worker_time->set(wait_for_worker); |
183 | 0 | } |
184 | | |
185 | 499 | if (_recvr->_max_find_recvr_time->value() < time_to_find_recvr) { |
186 | 0 | _recvr->_max_find_recvr_time->set((int64_t)time_to_find_recvr); |
187 | 0 | } |
188 | | |
189 | 499 | _block_queue.emplace_back(std::move(pblock), block_byte_size); |
190 | 499 | COUNTER_UPDATE(_recvr->_remote_bytes_received_counter, block_byte_size); |
191 | 499 | set_source_ready(l); |
192 | | |
193 | | // if done is nullptr, this function can't delay this response |
194 | 502 | if (done != nullptr && _recvr->exceeds_limit(block_byte_size)) { |
195 | 1 | _block_queue.back().set_done(*done); |
196 | 1 | *done = nullptr; |
197 | 1 | } |
198 | 499 | _recvr->_memory_used_counter->update(block_byte_size); |
199 | 499 | add_blocks_memory_usage(block_byte_size); |
200 | 499 | return Status::OK(); |
201 | 499 | } |
202 | | |
203 | | Status VDataStreamRecvr::SenderQueue::add_blocks(const PTransmitDataParams* request, |
204 | | ::google::protobuf::Closure** done, |
205 | | const int64_t wait_for_worker, |
206 | 76.2k | const uint64_t time_to_find_recvr) { |
207 | 76.2k | { |
208 | 76.2k | INJECT_MOCK_SLEEP(std::lock_guard<std::mutex> l(_lock)); |
209 | 76.2k | if (_is_cancelled) { |
210 | 1 | return Status::OK(); |
211 | 1 | } |
212 | 76.2k | const int be_number = request->be_number(); |
213 | | // In the request, the packet_seq for blocks is [request->packet_seq() - blocks_size(), request->packet_seq()) |
214 | | // Note this is a left-closed, right-open interval; the packet_seq of the last block is request->packet_seq() - 1 |
215 | | // We store the packet_seq of the last block in _packet_seq_map so we can compare it with the packet_seq of the next received packet |
216 | 76.2k | const int64_t packet_seq = request->packet_seq() - 1; |
217 | 76.2k | auto iter = _packet_seq_map.find(be_number); |
218 | 76.2k | if (iter != _packet_seq_map.end()) { |
219 | 13.3k | if (iter->second > (packet_seq - request->blocks_size())) { |
220 | 0 | return Status::InternalError( |
221 | 0 | "packet already exist [cur_packet_id= {} receive_packet_id={}]", |
222 | 0 | iter->second, packet_seq); |
223 | 0 | } |
224 | 13.3k | iter->second = packet_seq; |
225 | 62.8k | } else { |
226 | 62.8k | _packet_seq_map.emplace(be_number, packet_seq); |
227 | 62.8k | } |
228 | | |
229 | 76.2k | DCHECK(_num_remaining_senders >= 0); |
230 | 76.2k | if (_num_remaining_senders == 0) { |
231 | 0 | DCHECK(_sender_eos_set.end() != _sender_eos_set.find(be_number)); |
232 | 0 | return Status::OK(); |
233 | 0 | } |
234 | 76.2k | } |
235 | | |
236 | 76.2k | INJECT_MOCK_SLEEP(std::lock_guard<std::mutex> l(_lock)); |
237 | 76.2k | if (_is_cancelled) { |
238 | 0 | return Status::OK(); |
239 | 0 | } |
240 | | |
241 | 76.2k | int64_t total_block_byte_size = 0; |
242 | 152k | for (int i = 0; i < request->blocks_size(); i++) { |
243 | 76.2k | std::unique_ptr<PBlock> pblock = std::make_unique<PBlock>(); |
244 | 76.2k | pblock->CopyFrom(request->blocks(i)); |
245 | | |
246 | 76.2k | const auto block_byte_size = pblock->ByteSizeLong(); |
247 | 76.2k | COUNTER_UPDATE(_recvr->_blocks_produced_counter, 1); |
248 | 76.2k | if (_recvr->_max_wait_worker_time->value() < wait_for_worker) { |
249 | 1 | _recvr->_max_wait_worker_time->set(wait_for_worker); |
250 | 1 | } |
251 | | |
252 | 76.2k | if (_recvr->_max_find_recvr_time->value() < time_to_find_recvr) { |
253 | 54.1k | _recvr->_max_find_recvr_time->set((int64_t)time_to_find_recvr); |
254 | 54.1k | } |
255 | | |
256 | 76.2k | _block_queue.emplace_back(std::move(pblock), block_byte_size); |
257 | 76.2k | COUNTER_UPDATE(_recvr->_remote_bytes_received_counter, block_byte_size); |
258 | 76.2k | total_block_byte_size += block_byte_size; |
259 | 76.2k | } |
260 | | |
261 | 76.2k | set_source_ready(l); |
262 | | |
263 | | // if done is nullptr, this function can't delay this response |
264 | 76.2k | if (done != nullptr && _recvr->exceeds_limit(total_block_byte_size)) { |
265 | 3 | _block_queue.back().set_done(*done); |
266 | 3 | *done = nullptr; |
267 | 3 | } |
268 | 76.2k | _recvr->_memory_used_counter->update(total_block_byte_size); |
269 | 76.2k | add_blocks_memory_usage(total_block_byte_size); |
270 | 76.2k | return Status::OK(); |
271 | 76.2k | } |
272 | | |
273 | 148k | void VDataStreamRecvr::SenderQueue::add_block(Block* block, bool use_move) { |
274 | 148k | if (block->rows() == 0) { |
275 | 0 | return; |
276 | 0 | } |
277 | 148k | { |
278 | 148k | INJECT_MOCK_SLEEP(std::unique_lock<std::mutex> l(_lock)); |
279 | 148k | if (_is_cancelled) { |
280 | 297 | return; |
281 | 297 | } |
282 | 148k | DCHECK(_num_remaining_senders >= 0); |
283 | 148k | if (_num_remaining_senders == 0) { |
284 | 1 | return; |
285 | 1 | } |
286 | 148k | } |
287 | 148k | BlockUPtr nblock = Block::create_unique(block->get_columns_with_type_and_name()); |
288 | | |
289 | | // local exchange should copy the block contented if use move == false |
290 | 148k | if (use_move) { |
291 | 141k | block->clear(); |
292 | 141k | } else { |
293 | 6.76k | auto rows = block->rows(); |
294 | 19.6k | for (int i = 0; i < nblock->columns(); ++i) { |
295 | 12.8k | nblock->get_by_position(i).column = |
296 | 12.8k | nblock->get_by_position(i).column->clone_resized(rows); |
297 | 12.8k | } |
298 | 6.76k | } |
299 | 148k | materialize_block_inplace(*nblock); |
300 | | |
301 | 148k | auto block_mem_size = nblock->allocated_bytes(); |
302 | 148k | { |
303 | 148k | INJECT_MOCK_SLEEP(std::lock_guard<std::mutex> l(_lock)); |
304 | 148k | if (_is_cancelled) { |
305 | 2 | return; |
306 | 2 | } |
307 | 148k | _block_queue.emplace_back(std::move(nblock), block_mem_size); |
308 | 148k | set_source_ready(l); |
309 | 148k | COUNTER_UPDATE(_recvr->_local_bytes_received_counter, block_mem_size); |
310 | 148k | _recvr->_memory_used_counter->update(block_mem_size); |
311 | 148k | add_blocks_memory_usage(block_mem_size); |
312 | 148k | } |
313 | 148k | } |
314 | | |
315 | 4.38M | void VDataStreamRecvr::SenderQueue::decrement_senders(int be_number) { |
316 | 4.38M | INJECT_MOCK_SLEEP(std::lock_guard<std::mutex> l(_lock)); |
317 | 4.38M | if (_sender_eos_set.end() != _sender_eos_set.find(be_number)) { |
318 | 0 | return; |
319 | 0 | } |
320 | 4.38M | _sender_eos_set.insert(be_number); |
321 | 4.38M | DCHECK_GT(_num_remaining_senders, 0); |
322 | 4.38M | _num_remaining_senders--; |
323 | 18.4E | VLOG_FILE << "decremented senders: fragment_instance_id=" |
324 | 18.4E | << print_id(_recvr->fragment_instance_id()) << " node_id=" << _recvr->dest_node_id() |
325 | 18.4E | << " #senders=" << _num_remaining_senders; |
326 | 4.38M | if (_num_remaining_senders == 0) { |
327 | 662k | set_source_ready(l); |
328 | 662k | } |
329 | 4.38M | } |
330 | | |
331 | 673k | void VDataStreamRecvr::SenderQueue::cancel(Status cancel_status) { |
332 | 673k | { |
333 | 673k | INJECT_MOCK_SLEEP(std::lock_guard<std::mutex> l(_lock)); |
334 | 674k | if (_is_cancelled) { |
335 | 674k | return; |
336 | 674k | } |
337 | 18.4E | _is_cancelled = true; |
338 | 18.4E | _cancel_status = cancel_status; |
339 | 18.4E | set_source_ready(l); |
340 | 18.4E | VLOG_QUERY << "cancelled stream: _fragment_instance_id=" |
341 | 18.4E | << print_id(_recvr->fragment_instance_id()) |
342 | 18.4E | << " node_id=" << _recvr->dest_node_id(); |
343 | 18.4E | } |
344 | 0 | { |
345 | 18.4E | INJECT_MOCK_SLEEP(std::lock_guard<std::mutex> l(_lock)); |
346 | 18.4E | for (auto& block_item : _block_queue) { |
347 | 0 | block_item.call_done(_recvr); |
348 | 0 | } |
349 | 18.4E | _block_queue.clear(); |
350 | 18.4E | } |
351 | 18.4E | } |
352 | | |
353 | 673k | void VDataStreamRecvr::SenderQueue::close() { |
354 | | // If _is_cancelled is not set to true, there may be concurrent send |
355 | | // which add batch to _block_queue. The batch added after _block_queue |
356 | | // is clear will be memory leak |
357 | 673k | INJECT_MOCK_SLEEP(std::lock_guard<std::mutex> l(_lock)); |
358 | 673k | _is_cancelled = true; |
359 | 673k | set_source_ready(l); |
360 | | |
361 | 673k | for (auto& block_item : _block_queue) { |
362 | 1.38k | block_item.call_done(_recvr); |
363 | 1.38k | } |
364 | | // Delete any batches queued in _block_queue |
365 | 673k | _block_queue.clear(); |
366 | 673k | } |
367 | | |
368 | | VDataStreamRecvr::VDataStreamRecvr(VDataStreamMgr* stream_mgr, |
369 | | RuntimeProfile::HighWaterMarkCounter* memory_used_counter, |
370 | | RuntimeState* state, const TUniqueId& fragment_instance_id, |
371 | | PlanNodeId dest_node_id, int num_senders, bool is_merging, |
372 | | RuntimeProfile* profile, size_t data_queue_capacity) |
373 | 443k | : HasTaskExecutionCtx(state), |
374 | 443k | _mgr(stream_mgr), |
375 | 443k | _memory_used_counter(memory_used_counter), |
376 | 443k | _resource_ctx(state->get_query_ctx()->resource_ctx()), |
377 | 443k | _query_context(state->get_query_ctx()->shared_from_this()), |
378 | 443k | _fragment_instance_id(fragment_instance_id), |
379 | 443k | _dest_node_id(dest_node_id), |
380 | 443k | _is_merging(is_merging), |
381 | 443k | _is_closed(false), |
382 | 443k | _sender_queue_mem_limit(data_queue_capacity), |
383 | 443k | _profile(profile) { |
384 | | // DataStreamRecvr may be destructed after the instance execution thread ends. |
385 | 443k | _mem_tracker = |
386 | 443k | std::make_unique<MemTracker>("VDataStreamRecvr:" + print_id(_fragment_instance_id)); |
387 | 443k | SCOPED_CONSUME_MEM_TRACKER(_mem_tracker.get()); |
388 | | |
389 | | // Create one queue per sender if is_merging is true. |
390 | 443k | int num_queues = is_merging ? num_senders : 1; |
391 | 443k | _sender_to_local_channel_dependency.resize(num_queues); |
392 | 1.11M | for (size_t i = 0; i < num_queues; i++) { |
393 | 668k | _sender_to_local_channel_dependency[i] = Dependency::create_shared( |
394 | 668k | _dest_node_id, _dest_node_id, fmt::format("LocalExchangeChannelDependency_{}", i), |
395 | 668k | true); |
396 | 668k | } |
397 | 443k | _sender_queues.reserve(num_queues); |
398 | 443k | int num_sender_per_queue = is_merging ? 1 : num_senders; |
399 | 1.10M | for (int i = 0; i < num_queues; ++i) { |
400 | 665k | SenderQueue* queue = nullptr; |
401 | 665k | queue = _sender_queue_pool.add(new SenderQueue(this, num_sender_per_queue, |
402 | 665k | _sender_to_local_channel_dependency[i])); |
403 | 665k | _sender_queues.push_back(queue); |
404 | 665k | } |
405 | | |
406 | | // Initialize the counters |
407 | 443k | _remote_bytes_received_counter = ADD_COUNTER(_profile, "RemoteBytesReceived", TUnit::BYTES); |
408 | 443k | _local_bytes_received_counter = ADD_COUNTER(_profile, "LocalBytesReceived", TUnit::BYTES); |
409 | | |
410 | 443k | _deserialize_row_batch_timer = ADD_TIMER(_profile, "DeserializeRowBatchTimer"); |
411 | 443k | _data_arrival_timer = ADD_TIMER(_profile, "DataArrivalWaitTime"); |
412 | 443k | _buffer_full_total_timer = ADD_TIMER(_profile, "SendersBlockedTotalTimer(*)"); |
413 | 443k | _first_batch_wait_total_timer = ADD_TIMER(_profile, "FirstBatchArrivalWaitTime"); |
414 | 443k | _decompress_timer = ADD_TIMER(_profile, "DecompressTime"); |
415 | 443k | _decompress_bytes = ADD_COUNTER(_profile, "DecompressBytes", TUnit::BYTES); |
416 | 443k | _blocks_produced_counter = ADD_COUNTER(_profile, "BlocksProduced", TUnit::UNIT); |
417 | 443k | _max_wait_worker_time = ADD_COUNTER(_profile, "MaxWaitForWorkerTime", TUnit::UNIT); |
418 | 443k | _max_wait_to_process_time = ADD_COUNTER(_profile, "MaxWaitToProcessTime", TUnit::UNIT); |
419 | 443k | _max_find_recvr_time = ADD_COUNTER(_profile, "MaxFindRecvrTime(NS)", TUnit::UNIT); |
420 | 443k | } |
421 | | |
422 | 447k | VDataStreamRecvr::~VDataStreamRecvr() { |
423 | 18.4E | DCHECK(_mgr == nullptr) << "Must call close()"; |
424 | 447k | } |
425 | | |
426 | | Status VDataStreamRecvr::create_merger(const VExprContextSPtrs& ordering_expr, |
427 | | const std::vector<bool>& is_asc_order, |
428 | | const std::vector<bool>& nulls_first, size_t batch_size, |
429 | 42.3k | int64_t limit, size_t offset) { |
430 | 42.3k | DCHECK(_is_merging); |
431 | 42.3k | SCOPED_CONSUME_MEM_TRACKER(_mem_tracker.get()); |
432 | 42.3k | std::vector<BlockSupplier> child_block_suppliers; |
433 | | // Create the merger that will a single stream of sorted rows. |
434 | 42.3k | _merger.reset(new VSortedRunMerger(ordering_expr, is_asc_order, nulls_first, batch_size, limit, |
435 | 42.3k | offset, _profile)); |
436 | | |
437 | 309k | for (int i = 0; i < _sender_queues.size(); ++i) { |
438 | 267k | child_block_suppliers.emplace_back(std::bind(std::mem_fn(&SenderQueue::get_batch), |
439 | 267k | _sender_queues[i], std::placeholders::_1, |
440 | 267k | std::placeholders::_2)); |
441 | 267k | } |
442 | 42.3k | RETURN_IF_ERROR(_merger->prepare(child_block_suppliers)); |
443 | 42.3k | return Status::OK(); |
444 | 42.3k | } |
445 | | |
446 | | Status VDataStreamRecvr::add_block(std::unique_ptr<PBlock> pblock, int sender_id, int be_number, |
447 | | int64_t packet_seq, ::google::protobuf::Closure** done, |
448 | | const int64_t wait_for_worker, |
449 | 0 | const uint64_t time_to_find_recvr) { |
450 | 0 | SCOPED_ATTACH_TASK(_resource_ctx); |
451 | 0 | if (_query_context->low_memory_mode()) { |
452 | 0 | set_low_memory_mode(); |
453 | 0 | } |
454 | |
|
455 | 0 | int use_sender_id = _is_merging ? sender_id : 0; |
456 | 0 | return _sender_queues[use_sender_id]->add_block(std::move(pblock), be_number, packet_seq, done, |
457 | 0 | wait_for_worker, time_to_find_recvr); |
458 | 0 | } |
459 | | |
460 | | Status VDataStreamRecvr::add_blocks(const PTransmitDataParams* request, |
461 | | ::google::protobuf::Closure** done, |
462 | | const int64_t wait_for_worker, |
463 | 76.1k | const uint64_t time_to_find_recvr) { |
464 | 76.1k | SCOPED_ATTACH_TASK(_resource_ctx); |
465 | 76.1k | if (_query_context->low_memory_mode()) { |
466 | 0 | set_low_memory_mode(); |
467 | 0 | } |
468 | 76.1k | int use_sender_id = _is_merging ? request->sender_id() : 0; |
469 | 76.1k | return _sender_queues[use_sender_id]->add_blocks(request, done, wait_for_worker, |
470 | 76.1k | time_to_find_recvr); |
471 | 76.1k | } |
472 | | |
473 | 148k | void VDataStreamRecvr::add_block(Block* block, int sender_id, bool use_move) { |
474 | 148k | if (_query_context->low_memory_mode()) { |
475 | 0 | set_low_memory_mode(); |
476 | 0 | } |
477 | 148k | int use_sender_id = _is_merging ? sender_id : 0; |
478 | 148k | _sender_queues[use_sender_id]->add_block(block, use_move); |
479 | 148k | } |
480 | | |
481 | 61 | std::string VDataStreamRecvr::debug_string() { |
482 | 61 | fmt::memory_buffer debug_string_buffer; |
483 | 61 | fmt::format_to(debug_string_buffer, |
484 | 61 | "fragment_instance_id: {}, _dest_node_id: {}, _is_merging: {}, _is_closed: {}", |
485 | 61 | print_id(_fragment_instance_id), _dest_node_id, _is_merging, _is_closed); |
486 | 140 | for (size_t i = 0; i < _sender_queues.size(); i++) { |
487 | 79 | fmt::format_to(debug_string_buffer, "No. {} queue: {}", i, |
488 | 79 | _sender_queues[i]->debug_string()); |
489 | 79 | } |
490 | 61 | return fmt::to_string(debug_string_buffer); |
491 | 61 | } |
492 | | |
493 | 1.76M | std::shared_ptr<Dependency> VDataStreamRecvr::get_local_channel_dependency(int sender_id) { |
494 | 1.76M | DCHECK(_sender_to_local_channel_dependency[_is_merging ? sender_id : 0] != nullptr); |
495 | 1.76M | return _sender_to_local_channel_dependency[_is_merging ? sender_id : 0]; |
496 | 1.76M | } |
497 | | |
498 | 653k | Status VDataStreamRecvr::get_next(Block* block, bool* eos) { |
499 | 653k | if (!_is_merging) { |
500 | 550k | block->clear(); |
501 | 550k | return _sender_queues[0]->get_batch(block, eos); |
502 | 550k | } else { |
503 | 102k | return _merger->get_next(block, eos); |
504 | 102k | } |
505 | 653k | } |
506 | | |
507 | 4.39M | void VDataStreamRecvr::remove_sender(int sender_id, int be_number, Status exec_status) { |
508 | 4.39M | if (!exec_status.ok()) { |
509 | 0 | cancel_stream(exec_status); |
510 | 0 | return; |
511 | 0 | } |
512 | 4.39M | int use_sender_id = _is_merging ? sender_id : 0; |
513 | 4.39M | _sender_queues[use_sender_id]->decrement_senders(be_number); |
514 | 4.39M | } |
515 | | |
516 | 447k | void VDataStreamRecvr::cancel_stream(Status exec_status) { |
517 | 18.4E | VLOG_QUERY << "cancel_stream: fragment_instance_id=" << print_id(_fragment_instance_id) |
518 | 18.4E | << exec_status; |
519 | | |
520 | 1.12M | for (int i = 0; i < _sender_queues.size(); ++i) { |
521 | 674k | _sender_queues[i]->cancel(exec_status); |
522 | 674k | } |
523 | 447k | } |
524 | | |
525 | 225k | void VDataStreamRecvr::SenderQueue::add_blocks_memory_usage(int64_t size) { |
526 | 225k | DCHECK(size >= 0); |
527 | 225k | _recvr->_mem_tracker->consume(size); |
528 | 225k | _queue_mem_tracker->consume(size); |
529 | 225k | if (_local_channel_dependency && exceeds_limit()) { |
530 | 917 | _local_channel_dependency->block(); |
531 | 917 | } |
532 | 225k | } |
533 | | |
534 | 223k | void VDataStreamRecvr::SenderQueue::sub_blocks_memory_usage(int64_t size) { |
535 | 223k | DCHECK(size >= 0); |
536 | 223k | _recvr->_mem_tracker->release(size); |
537 | 223k | _queue_mem_tracker->release(size); |
538 | 223k | if (_local_channel_dependency && (!exceeds_limit())) { |
539 | 223k | _local_channel_dependency->set_ready(); |
540 | 223k | } |
541 | 223k | } |
542 | | |
543 | 449k | bool VDataStreamRecvr::SenderQueue::exceeds_limit() { |
544 | 449k | const size_t queue_byte_size = _queue_mem_tracker->consumption(); |
545 | 449k | return _recvr->queue_exceeds_limit(queue_byte_size); |
546 | 449k | } |
547 | | |
548 | 76.7k | bool VDataStreamRecvr::exceeds_limit(size_t block_byte_size) { |
549 | 76.7k | return _mem_tracker->consumption() + block_byte_size > config::exchg_node_buffer_size_bytes; |
550 | 76.7k | } |
551 | | |
552 | 449k | bool VDataStreamRecvr::queue_exceeds_limit(size_t queue_byte_size) const { |
553 | 449k | return queue_byte_size >= _sender_queue_mem_limit; |
554 | 449k | } |
555 | | |
556 | 892k | void VDataStreamRecvr::close() { |
557 | 892k | if (_is_closed) { |
558 | 445k | return; |
559 | 445k | } |
560 | 446k | _is_closed = true; |
561 | 673k | for (auto& it : _sender_to_local_channel_dependency) { |
562 | 673k | it->set_always_ready(); |
563 | 673k | } |
564 | 1.12M | for (int i = 0; i < _sender_queues.size(); ++i) { |
565 | 673k | _sender_queues[i]->close(); |
566 | 673k | } |
567 | | // Remove this receiver from the DataStreamMgr that created it. |
568 | | // TODO: log error msg |
569 | 447k | if (_mgr) { |
570 | 447k | static_cast<void>(_mgr->deregister_recvr(fragment_instance_id(), dest_node_id())); |
571 | 447k | } |
572 | 446k | _mgr = nullptr; |
573 | | |
574 | 446k | _merger.reset(); |
575 | 446k | } |
576 | | |
577 | 437k | void VDataStreamRecvr::set_sink_dep_always_ready() const { |
578 | 663k | for (auto dep : _sender_to_local_channel_dependency) { |
579 | 663k | dep->set_always_ready(); |
580 | 663k | } |
581 | 437k | } |
582 | | |
583 | | } // namespace doris |