Coverage Report

Created: 2026-06-26 03:55

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
682k
        : _recvr(parent_recvr),
48
682k
          _is_cancelled(false),
49
682k
          _num_remaining_senders(num_senders),
50
682k
          _local_channel_dependency(local_channel_dependency) {
51
682k
    _cancel_status = Status::OK();
52
682k
    _queue_mem_tracker = std::make_unique<MemTracker>("local data queue mem tracker");
53
682k
}
54
55
690k
VDataStreamRecvr::SenderQueue::~SenderQueue() {
56
690k
    run_block_queue_done_callbacks(_block_queue);
57
690k
    _block_queue.clear();
58
690k
}
59
60
951k
Status VDataStreamRecvr::SenderQueue::get_batch(Block* block, bool* eos) {
61
951k
    BlockItem block_item;
62
951k
    {
63
951k
        INJECT_MOCK_SLEEP(std::lock_guard<std::mutex> l(_lock));
64
951k
#ifndef NDEBUG
65
952k
        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
951k
#endif
72
        //check and get block_item from data_queue
73
951k
        if (_is_cancelled) {
74
3
            RETURN_IF_ERROR(_cancel_status);
75
2
            return Status::Cancelled("Cancelled");
76
3
        }
77
78
951k
        if (_block_queue.empty()) {
79
672k
            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
672k
            *eos = true;
86
672k
            return Status::OK();
87
672k
        }
88
89
951k
        DCHECK(!_block_queue.empty());
90
279k
        block_item = std::move(_block_queue.front());
91
279k
        _block_queue.pop_front();
92
279k
    }
93
0
    BlockUPtr next_block;
94
279k
    RETURN_IF_ERROR(block_item.get_block(next_block));
95
279k
    size_t block_byte_size = block_item.block_byte_size();
96
279k
    COUNTER_UPDATE(_recvr->_deserialize_row_batch_timer, block_item.deserialize_time());
97
279k
    COUNTER_UPDATE(_recvr->_decompress_timer, block_item.decompress_time());
98
279k
    COUNTER_UPDATE(_recvr->_decompress_bytes, block_item.decompress_bytes());
99
279k
    _recvr->_memory_used_counter->update(-(int64_t)block_byte_size);
100
279k
    INJECT_MOCK_SLEEP(std::lock_guard<std::mutex> l(_lock));
101
279k
    sub_blocks_memory_usage(block_byte_size);
102
279k
    if (_block_queue.empty() && _source_dependency) {
103
224k
        if (!_is_cancelled && _num_remaining_senders > 0) {
104
108k
            _source_dependency->block();
105
108k
        }
106
224k
    }
107
108
279k
    block_item.call_done(_recvr);
109
110
279k
    DCHECK(block->empty());
111
279k
    block->swap(*next_block);
112
279k
    *eos = false;
113
279k
    return Status::OK();
114
279k
}
115
116
1.65M
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
1.65M
    if (!_source_dependency) {
120
54
        return;
121
54
    }
122
1.65M
    const bool should_wait = !_is_cancelled && _block_queue.empty() && _num_remaining_senders > 0;
123
1.65M
    if (!should_wait) {
124
1.65M
        _source_dependency->set_ready();
125
1.65M
    }
126
1.65M
}
127
128
void VDataStreamRecvr::SenderQueue::run_block_queue_done_callbacks(
129
1.38M
        std::list<BlockItem>& block_queue) {
130
1.38M
    for (auto& block_item : block_queue) {
131
1.35k
        block_item.call_done(_recvr);
132
1.35k
    }
133
1.38M
}
134
135
15
std::string VDataStreamRecvr::SenderQueue::debug_string() {
136
15
    std::lock_guard<std::mutex> l(_lock);
137
15
    fmt::memory_buffer debug_string_buffer;
138
15
    fmt::format_to(debug_string_buffer,
139
15
                   "_num_remaining_senders = {}, block_queue size = {}, _is_cancelled: {}, "
140
15
                   "_cancel_status: {}, _sender_eos_set: (",
141
15
                   _num_remaining_senders, _block_queue.size(), _is_cancelled,
142
15
                   _cancel_status.to_string());
143
15
    for (auto& i : _sender_eos_set) {
144
0
        fmt::format_to(debug_string_buffer, "{}, ", i);
145
0
    }
146
15
    fmt::format_to(debug_string_buffer, ")");
147
15
    return fmt::to_string(debug_string_buffer);
148
15
}
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.10k
                                                const uint64_t time_to_find_recvr) {
155
2.10k
    {
156
2.10k
        INJECT_MOCK_SLEEP(std::lock_guard<std::mutex> l(_lock));
157
2.10k
        if (_is_cancelled) {
158
1.55k
            return Status::OK();
159
1.55k
        }
160
546
        auto iter = _packet_seq_map.find(be_number);
161
546
        if (iter != _packet_seq_map.end()) {
162
532
            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
532
            iter->second = packet_seq;
168
532
        } else {
169
14
            _packet_seq_map.emplace(be_number, packet_seq);
170
14
        }
171
172
546
        DCHECK(_num_remaining_senders >= 0);
173
546
        if (_num_remaining_senders == 0) {
174
0
            DCHECK(_sender_eos_set.contains(be_number));
175
0
            return Status::OK();
176
0
        }
177
546
    }
178
179
546
    INJECT_MOCK_SLEEP(std::lock_guard<std::mutex> l(_lock));
180
546
    if (_is_cancelled) {
181
5
        return Status::OK();
182
5
    }
183
184
541
    const auto block_byte_size = pblock->ByteSizeLong();
185
541
    COUNTER_UPDATE(_recvr->_blocks_produced_counter, 1);
186
541
    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
541
    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
541
    _block_queue.emplace_back(std::move(pblock), block_byte_size);
195
541
    COUNTER_UPDATE(_recvr->_remote_bytes_received_counter, block_byte_size);
196
541
    set_source_ready(l);
197
198
    // if done is nullptr, this function can't delay this response
199
542
    if (done != nullptr && _recvr->exceeds_limit(block_byte_size)) {
200
41
        _block_queue.back().set_done(*done);
201
41
        *done = nullptr;
202
41
    }
203
541
    _recvr->_memory_used_counter->update(block_byte_size);
204
541
    add_blocks_memory_usage(block_byte_size);
205
541
    return Status::OK();
206
546
}
207
208
Status VDataStreamRecvr::SenderQueue::add_blocks(const PTransmitDataParams* request,
209
                                                 ::google::protobuf::Closure** done,
210
                                                 const int64_t wait_for_worker,
211
80.0k
                                                 const uint64_t time_to_find_recvr) {
212
80.0k
    {
213
80.0k
        INJECT_MOCK_SLEEP(std::lock_guard<std::mutex> l(_lock));
214
80.0k
        if (_is_cancelled) {
215
1
            return Status::OK();
216
1
        }
217
80.0k
        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
80.0k
        const int64_t packet_seq = request->packet_seq() - 1;
222
80.0k
        auto iter = _packet_seq_map.find(be_number);
223
80.0k
        if (iter != _packet_seq_map.end()) {
224
15.8k
            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
15.8k
            iter->second = packet_seq;
230
64.2k
        } else {
231
64.2k
            _packet_seq_map.emplace(be_number, packet_seq);
232
64.2k
        }
233
234
80.0k
        DCHECK(_num_remaining_senders >= 0);
235
80.0k
        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
80.0k
    }
240
241
80.0k
    INJECT_MOCK_SLEEP(std::lock_guard<std::mutex> l(_lock));
242
80.0k
    if (_is_cancelled) {
243
0
        return Status::OK();
244
0
    }
245
246
80.0k
    int64_t total_block_byte_size = 0;
247
160k
    for (int i = 0; i < request->blocks_size(); i++) {
248
80.1k
        std::unique_ptr<PBlock> pblock = std::make_unique<PBlock>();
249
80.1k
        pblock->CopyFrom(request->blocks(i));
250
251
80.1k
        const auto block_byte_size = pblock->ByteSizeLong();
252
80.1k
        COUNTER_UPDATE(_recvr->_blocks_produced_counter, 1);
253
80.1k
        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
80.1k
        if (_recvr->_max_find_recvr_time->value() < time_to_find_recvr) {
258
54.7k
            _recvr->_max_find_recvr_time->set((int64_t)time_to_find_recvr);
259
54.7k
        }
260
261
80.1k
        _block_queue.emplace_back(std::move(pblock), block_byte_size);
262
80.1k
        COUNTER_UPDATE(_recvr->_remote_bytes_received_counter, block_byte_size);
263
80.1k
        total_block_byte_size += block_byte_size;
264
80.1k
    }
265
266
80.0k
    set_source_ready(l);
267
268
    // if done is nullptr, this function can't delay this response
269
80.0k
    if (done != nullptr && _recvr->exceeds_limit(total_block_byte_size)) {
270
3
        _block_queue.back().set_done(*done);
271
3
        *done = nullptr;
272
3
    }
273
80.0k
    _recvr->_memory_used_counter->update(total_block_byte_size);
274
80.0k
    add_blocks_memory_usage(total_block_byte_size);
275
80.0k
    return Status::OK();
276
80.0k
}
277
278
200k
void VDataStreamRecvr::SenderQueue::add_block(Block* block, bool use_move) {
279
200k
    if (block->rows() == 0) {
280
0
        return;
281
0
    }
282
200k
    {
283
200k
        INJECT_MOCK_SLEEP(std::unique_lock<std::mutex> l(_lock));
284
200k
        if (_is_cancelled) {
285
297
            return;
286
297
        }
287
200k
        DCHECK(_num_remaining_senders >= 0);
288
200k
        if (_num_remaining_senders == 0) {
289
1
            return;
290
1
        }
291
200k
    }
292
200k
    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
200k
    if (use_move) {
296
191k
        block->clear();
297
191k
    } else {
298
8.50k
        auto rows = block->rows();
299
22.8k
        for (int i = 0; i < nblock->columns(); ++i) {
300
14.3k
            nblock->get_by_position(i).column =
301
14.3k
                    nblock->get_by_position(i).column->clone_resized(rows);
302
14.3k
        }
303
8.50k
    }
304
200k
    materialize_block_inplace(*nblock);
305
306
200k
    auto block_mem_size = nblock->allocated_bytes();
307
200k
    {
308
200k
        INJECT_MOCK_SLEEP(std::lock_guard<std::mutex> l(_lock));
309
200k
        if (_is_cancelled) {
310
0
            return;
311
0
        }
312
200k
        _block_queue.emplace_back(std::move(nblock), block_mem_size);
313
200k
        set_source_ready(l);
314
200k
        COUNTER_UPDATE(_recvr->_local_bytes_received_counter, block_mem_size);
315
200k
        _recvr->_memory_used_counter->update(block_mem_size);
316
200k
        add_blocks_memory_usage(block_mem_size);
317
200k
    }
318
200k
}
319
320
4.61M
void VDataStreamRecvr::SenderQueue::decrement_senders(int be_number) {
321
4.61M
    INJECT_MOCK_SLEEP(std::lock_guard<std::mutex> l(_lock));
322
4.61M
    if (_sender_eos_set.end() != _sender_eos_set.find(be_number)) {
323
0
        return;
324
0
    }
325
4.61M
    _sender_eos_set.insert(be_number);
326
4.61M
    DCHECK_GT(_num_remaining_senders, 0);
327
4.61M
    _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
4.61M
    if (_num_remaining_senders == 0) {
332
681k
        set_source_ready(l);
333
681k
    }
334
4.61M
}
335
336
690k
void VDataStreamRecvr::SenderQueue::cancel(Status cancel_status) {
337
690k
    std::list<BlockItem> block_queue;
338
690k
    {
339
690k
        INJECT_MOCK_SLEEP(std::lock_guard<std::mutex> l(_lock));
340
690k
        if (_is_cancelled) {
341
690k
            return;
342
690k
        }
343
18.4E
        _is_cancelled = true;
344
18.4E
        _cancel_status = cancel_status;
345
18.4E
        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
18.4E
        block_queue.splice(block_queue.end(), _block_queue);
350
18.4E
    }
351
0
    run_block_queue_done_callbacks(block_queue);
352
18.4E
}
353
354
689k
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
689k
    std::list<BlockItem> block_queue;
359
689k
    {
360
689k
        INJECT_MOCK_SLEEP(std::lock_guard<std::mutex> l(_lock));
361
689k
        _is_cancelled = true;
362
689k
        set_source_ready(l);
363
689k
        block_queue.splice(block_queue.end(), _block_queue);
364
689k
    }
365
    // Release delayed RPC callbacks after the queue state is fully closed.
366
689k
    run_block_queue_done_callbacks(block_queue);
367
689k
}
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
465k
        : HasTaskExecutionCtx(state),
375
465k
          _mgr(stream_mgr),
376
465k
          _memory_used_counter(memory_used_counter),
377
465k
          _resource_ctx(state->get_query_ctx()->resource_ctx()),
378
465k
          _query_context(state->get_query_ctx()->shared_from_this()),
379
465k
          _fragment_instance_id(fragment_instance_id),
380
465k
          _dest_node_id(dest_node_id),
381
465k
          _is_merging(is_merging),
382
465k
          _is_closed(false),
383
465k
          _sender_queue_mem_limit(data_queue_capacity),
384
465k
          _profile(profile) {
385
    // DataStreamRecvr may be destructed after the instance execution thread ends.
386
465k
    _mem_tracker =
387
465k
            std::make_unique<MemTracker>("VDataStreamRecvr:" + print_id(_fragment_instance_id));
388
465k
    SCOPED_CONSUME_MEM_TRACKER(_mem_tracker.get());
389
390
    // Create one queue per sender if is_merging is true.
391
465k
    int num_queues = is_merging ? num_senders : 1;
392
465k
    _sender_to_local_channel_dependency.resize(num_queues);
393
1.15M
    for (size_t i = 0; i < num_queues; i++) {
394
685k
        _sender_to_local_channel_dependency[i] = Dependency::create_shared(
395
685k
                _dest_node_id, _dest_node_id, fmt::format("LocalExchangeChannelDependency_{}", i),
396
685k
                true);
397
685k
    }
398
465k
    _sender_queues.reserve(num_queues);
399
465k
    int num_sender_per_queue = is_merging ? 1 : num_senders;
400
1.15M
    for (int i = 0; i < num_queues; ++i) {
401
684k
        SenderQueue* queue = nullptr;
402
684k
        queue = _sender_queue_pool.add(new SenderQueue(this, num_sender_per_queue,
403
684k
                                                       _sender_to_local_channel_dependency[i]));
404
684k
        _sender_queues.push_back(queue);
405
684k
    }
406
407
    // Initialize the counters
408
465k
    _remote_bytes_received_counter = ADD_COUNTER(_profile, "RemoteBytesReceived", TUnit::BYTES);
409
465k
    _local_bytes_received_counter = ADD_COUNTER(_profile, "LocalBytesReceived", TUnit::BYTES);
410
411
465k
    _deserialize_row_batch_timer = ADD_TIMER(_profile, "DeserializeRowBatchTimer");
412
465k
    _data_arrival_timer = ADD_TIMER(_profile, "DataArrivalWaitTime");
413
465k
    _buffer_full_total_timer = ADD_TIMER(_profile, "SendersBlockedTotalTimer(*)");
414
465k
    _first_batch_wait_total_timer = ADD_TIMER(_profile, "FirstBatchArrivalWaitTime");
415
465k
    _decompress_timer = ADD_TIMER(_profile, "DecompressTime");
416
465k
    _decompress_bytes = ADD_COUNTER(_profile, "DecompressBytes", TUnit::BYTES);
417
465k
    _blocks_produced_counter = ADD_COUNTER(_profile, "BlocksProduced", TUnit::UNIT);
418
465k
    _max_wait_worker_time = ADD_COUNTER(_profile, "MaxWaitForWorkerTime", TUnit::UNIT);
419
465k
    _max_wait_to_process_time = ADD_COUNTER(_profile, "MaxWaitToProcessTime", TUnit::UNIT);
420
465k
    _max_find_recvr_time = ADD_COUNTER(_profile, "MaxFindRecvrCpuTime(NS)", TUnit::UNIT);
421
465k
}
422
423
468k
VDataStreamRecvr::~VDataStreamRecvr() {
424
18.4E
    DCHECK(_mgr == nullptr) << "Must call close()";
425
468k
}
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
43.2k
                                       int64_t limit, size_t offset) {
431
43.2k
    DCHECK(_is_merging);
432
43.2k
    SCOPED_CONSUME_MEM_TRACKER(_mem_tracker.get());
433
43.2k
    std::vector<BlockSupplier> child_block_suppliers;
434
    // Create the merger that will a single stream of sorted rows.
435
43.2k
    _merger.reset(new VSortedRunMerger(ordering_expr, is_asc_order, nulls_first, batch_size, limit,
436
43.2k
                                       offset, _profile));
437
438
307k
    for (int i = 0; i < _sender_queues.size(); ++i) {
439
263k
        child_block_suppliers.emplace_back(std::bind(std::mem_fn(&SenderQueue::get_batch),
440
263k
                                                     _sender_queues[i], std::placeholders::_1,
441
263k
                                                     std::placeholders::_2));
442
263k
    }
443
43.2k
    RETURN_IF_ERROR(_merger->prepare(child_block_suppliers));
444
43.2k
    return Status::OK();
445
43.2k
}
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
80.0k
                                    const uint64_t time_to_find_recvr) {
465
80.0k
    SCOPED_ATTACH_TASK(_resource_ctx);
466
80.0k
    if (_query_context->low_memory_mode()) {
467
0
        set_low_memory_mode();
468
0
    }
469
80.0k
    int use_sender_id = _is_merging ? request->sender_id() : 0;
470
80.0k
    return _sender_queues[use_sender_id]->add_blocks(request, done, wait_for_worker,
471
80.0k
                                                     time_to_find_recvr);
472
80.0k
}
473
474
199k
void VDataStreamRecvr::add_block(Block* block, int sender_id, bool use_move) {
475
199k
    if (_query_context->low_memory_mode()) {
476
0
        set_low_memory_mode();
477
0
    }
478
199k
    int use_sender_id = _is_merging ? sender_id : 0;
479
199k
    _sender_queues[use_sender_id]->add_block(block, use_move);
480
199k
}
481
482
15
std::string VDataStreamRecvr::debug_string() {
483
15
    fmt::memory_buffer debug_string_buffer;
484
15
    fmt::format_to(debug_string_buffer,
485
15
                   "fragment_instance_id: {}, _dest_node_id: {}, _is_merging: {}, _is_closed: {}",
486
15
                   print_id(_fragment_instance_id), _dest_node_id, _is_merging, _is_closed);
487
30
    for (size_t i = 0; i < _sender_queues.size(); i++) {
488
15
        fmt::format_to(debug_string_buffer, "No. {} queue: {}", i,
489
15
                       _sender_queues[i]->debug_string());
490
15
    }
491
15
    return fmt::to_string(debug_string_buffer);
492
15
}
493
494
3.07M
std::shared_ptr<Dependency> VDataStreamRecvr::get_local_channel_dependency(int sender_id) {
495
3.07M
    DCHECK(_sender_to_local_channel_dependency[_is_merging ? sender_id : 0] != nullptr);
496
3.07M
    return _sender_to_local_channel_dependency[_is_merging ? sender_id : 0];
497
3.07M
}
498
499
730k
Status VDataStreamRecvr::get_next(Block* block, bool* eos) {
500
730k
    if (!_is_merging) {
501
621k
        block->clear();
502
621k
        return _sender_queues[0]->get_batch(block, eos);
503
621k
    } else {
504
109k
        return _merger->get_next(block, eos);
505
109k
    }
506
730k
}
507
508
4.61M
void VDataStreamRecvr::remove_sender(int sender_id, int be_number, Status exec_status) {
509
4.61M
    if (!exec_status.ok()) {
510
0
        cancel_stream(exec_status);
511
0
        return;
512
0
    }
513
4.61M
    int use_sender_id = _is_merging ? sender_id : 0;
514
4.61M
    _sender_queues[use_sender_id]->decrement_senders(be_number);
515
4.61M
}
516
517
468k
void VDataStreamRecvr::cancel_stream(Status exec_status) {
518
18.4E
    VLOG_QUERY << "cancel_stream: fragment_instance_id=" << print_id(_fragment_instance_id)
519
18.4E
               << exec_status;
520
521
1.15M
    for (int i = 0; i < _sender_queues.size(); ++i) {
522
690k
        _sender_queues[i]->cancel(exec_status);
523
690k
    }
524
468k
}
525
526
280k
void VDataStreamRecvr::SenderQueue::add_blocks_memory_usage(int64_t size) {
527
280k
    DCHECK(size >= 0);
528
280k
    _recvr->_mem_tracker->consume(size);
529
280k
    _queue_mem_tracker->consume(size);
530
280k
    if (_local_channel_dependency && exceeds_limit()) {
531
5.04k
        _local_channel_dependency->block();
532
5.04k
    }
533
280k
}
534
535
279k
void VDataStreamRecvr::SenderQueue::sub_blocks_memory_usage(int64_t size) {
536
279k
    DCHECK(size >= 0);
537
279k
    _recvr->_mem_tracker->release(size);
538
279k
    _queue_mem_tracker->release(size);
539
279k
    if (_local_channel_dependency && (!exceeds_limit())) {
540
279k
        _local_channel_dependency->set_ready();
541
279k
    }
542
279k
}
543
544
560k
bool VDataStreamRecvr::SenderQueue::exceeds_limit() {
545
560k
    const size_t queue_byte_size = _queue_mem_tracker->consumption();
546
560k
    return _recvr->queue_exceeds_limit(queue_byte_size);
547
560k
}
548
549
80.5k
bool VDataStreamRecvr::exceeds_limit(size_t block_byte_size) {
550
80.5k
    return _mem_tracker->consumption() + block_byte_size > config::exchg_node_buffer_size_bytes;
551
80.5k
}
552
553
560k
bool VDataStreamRecvr::queue_exceeds_limit(size_t queue_byte_size) const {
554
560k
    return queue_byte_size >= _sender_queue_mem_limit;
555
560k
}
556
557
934k
void VDataStreamRecvr::close() {
558
934k
    if (_is_closed) {
559
466k
        return;
560
466k
    }
561
467k
    _is_closed = true;
562
689k
    for (auto& it : _sender_to_local_channel_dependency) {
563
689k
        it->set_always_ready();
564
689k
    }
565
1.15M
    for (int i = 0; i < _sender_queues.size(); ++i) {
566
690k
        _sender_queues[i]->close();
567
690k
    }
568
    // Remove this receiver from the DataStreamMgr that created it.
569
    // TODO: log error msg
570
468k
    if (_mgr) {
571
468k
        static_cast<void>(_mgr->deregister_recvr(fragment_instance_id(), dest_node_id()));
572
468k
    }
573
467k
    _mgr = nullptr;
574
575
467k
    _merger.reset();
576
467k
}
577
578
459k
void VDataStreamRecvr::set_sink_dep_always_ready() const {
579
680k
    for (auto dep : _sender_to_local_channel_dependency) {
580
680k
        dep->set_always_ready();
581
680k
    }
582
459k
}
583
584
} // namespace doris