Coverage Report

Created: 2026-06-18 06:08

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