Coverage Report

Created: 2026-05-18 11:08

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/exec/exchange/vdata_stream_sender.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_sender.h"
19
20
#include <fmt/format.h>
21
#include <fmt/ranges.h> // IWYU pragma: keep
22
#include <gen_cpp/DataSinks_types.h>
23
#include <gen_cpp/Metrics_types.h>
24
#include <gen_cpp/Types_types.h>
25
#include <gen_cpp/data.pb.h>
26
#include <gen_cpp/internal_service.pb.h>
27
#include <glog/logging.h>
28
#include <stddef.h>
29
30
#include <algorithm>
31
#include <cstdint>
32
#include <functional>
33
#include <map>
34
#include <memory>
35
#include <random>
36
37
#include "common/object_pool.h"
38
#include "common/status.h"
39
#include "core/column/column_const.h"
40
#include "exec/common/sip_hash.h"
41
#include "exec/exchange/vdata_stream_mgr.h"
42
#include "exec/exchange/vdata_stream_recvr.h"
43
#include "exec/operator/exchange_sink_operator.h"
44
#include "exec/operator/result_file_sink_operator.h"
45
#include "exec/sink/vrow_distribution.h"
46
#include "exec/sink/writer/vtablet_writer_v2.h"
47
#include "exprs/vexpr.h"
48
#include "runtime/descriptors.h"
49
#include "runtime/runtime_state.h"
50
#include "runtime/thread_context.h"
51
#include "storage/tablet_info.h"
52
#include "util/proto_util.h"
53
54
namespace doris {
55
56
246k
Status Channel::init(RuntimeState* state) {
57
    // only enable_local_exchange() is true and the destination address is localhost, then the channel is local
58
246k
    _is_local &= state->enable_local_exchange();
59
60
246k
    if (_is_local) {
61
245k
        return Status::OK();
62
245k
    }
63
64
632
    RETURN_IF_ERROR(_init_brpc_stub(state));
65
66
632
    return Status::OK();
67
632
}
68
69
610
Status Channel::_init_brpc_stub(RuntimeState* state) {
70
610
    if (_brpc_dest_addr.hostname.empty()) {
71
0
        LOG(WARNING) << "there is no brpc destination address's hostname"
72
0
                        ", maybe version is not compatible.";
73
0
        return Status::InternalError("no brpc destination");
74
0
    }
75
76
610
    auto network_address = _brpc_dest_addr;
77
610
    if (_brpc_dest_addr.hostname == BackendOptions::get_localhost()) {
78
610
        _brpc_stub = state->exec_env()->brpc_internal_client_cache()->get_client(
79
610
                "127.0.0.1", _brpc_dest_addr.port);
80
610
        network_address.hostname = "127.0.0.1";
81
610
    } else {
82
0
        _brpc_stub = state->exec_env()->brpc_internal_client_cache()->get_client(_brpc_dest_addr);
83
0
    }
84
85
610
    if (!_brpc_stub) {
86
0
        std::string msg = fmt::format("Get rpc stub failed, dest_addr={}:{}",
87
0
                                      _brpc_dest_addr.hostname, _brpc_dest_addr.port);
88
0
        LOG(WARNING) << msg;
89
0
        return Status::InternalError(msg);
90
0
    }
91
92
610
    if (config::enable_brpc_connection_check) {
93
610
        state->get_query_ctx()->add_using_brpc_stub(_brpc_dest_addr, _brpc_stub);
94
610
    }
95
610
    return Status::OK();
96
610
}
97
98
245k
Status Channel::open(RuntimeState* state) {
99
245k
    if (_is_local) {
100
245k
        RETURN_IF_ERROR(_find_local_recvr(state));
101
245k
    }
102
245k
    _be_number = state->be_number();
103
245k
    _brpc_timeout_ms = get_execution_rpc_timeout_ms(state->execution_timeout());
104
245k
    _serializer.set_is_local(_is_local);
105
106
    // In bucket shuffle join will set fragment_instance_id (-1, -1)
107
    // to build a camouflaged empty channel. the ip and port is '0.0.0.0:0"
108
    // so the empty channel not need call function close_internal()
109
245k
    _need_close = (_fragment_instance_id.hi != -1 && _fragment_instance_id.lo != -1);
110
111
245k
    return Status::OK();
112
245k
}
113
114
244k
Status Channel::_find_local_recvr(RuntimeState* state) {
115
244k
    auto st = _parent->state()->exec_env()->vstream_mgr()->find_recvr(_fragment_instance_id,
116
244k
                                                                      _dest_node_id, &_local_recvr);
117
244k
    if (!st.ok()) {
118
        // If could not find local receiver, then it means the channel is EOF.
119
        // Maybe downstream task is finished already.
120
        //if (_receiver_status.ok()) {
121
        //    _receiver_status = Status::EndOfFile("local data stream receiver is deconstructed");
122
        //}
123
0
        LOG(INFO) << "Query: " << print_id(state->query_id())
124
0
                  << " recvr is not found, maybe downstream task is finished. error st is: "
125
0
                  << st.to_string();
126
0
    }
127
244k
    return Status::OK();
128
244k
}
129
Status Channel::add_rows(Block* block, const uint32_t* data, const uint32_t offset,
130
99.6k
                         const uint32_t size, bool eos) {
131
99.6k
    if (_fragment_instance_id.lo == -1) {
132
0
        return Status::OK();
133
0
    }
134
135
99.6k
    bool serialized = false;
136
99.6k
    if (_pblock == nullptr) {
137
76.1k
        _pblock = std::make_unique<PBlock>();
138
76.1k
    }
139
99.6k
    RETURN_IF_ERROR(_serializer.next_serialized_block(block, _pblock.get(), 1, &serialized, eos,
140
99.6k
                                                      data, offset, size));
141
99.6k
    if (serialized) {
142
77.0k
        RETURN_IF_ERROR(_send_current_block(eos));
143
77.0k
    }
144
145
99.4k
    return Status::OK();
146
99.6k
}
147
148
245k
std::shared_ptr<Dependency> Channel::get_local_channel_dependency() {
149
245k
    if (!_local_recvr) {
150
5
        return nullptr;
151
5
    }
152
245k
    return _local_recvr->get_local_channel_dependency(_parent->sender_id());
153
245k
}
154
155
623k
int64_t Channel::mem_usage() const {
156
623k
    return _serializer.mem_usage();
157
623k
}
158
159
1.17k
Status Channel::send_remote_block(std::unique_ptr<PBlock>&& block, bool eos) {
160
1.17k
    COUNTER_UPDATE(_parent->blocks_sent_counter(), 1);
161
162
1.17k
    if (eos) {
163
796
        if (_eos_send) {
164
610
            return Status::OK();
165
610
        } else {
166
186
            _eos_send = true;
167
186
        }
168
796
    }
169
562
    if (eos || block->column_metas_size()) {
170
562
        RETURN_IF_ERROR(_buffer->add_block(this, {std::move(block), eos}));
171
562
    }
172
562
    return Status::OK();
173
562
}
174
175
422
Status Channel::send_broadcast_block(std::shared_ptr<BroadcastPBlockHolder>& block, bool eos) {
176
422
    COUNTER_UPDATE(_parent->blocks_sent_counter(), 1);
177
424
    if (eos) {
178
424
        if (_eos_send) {
179
0
            return Status::OK();
180
0
        }
181
424
        _eos_send = true;
182
424
    }
183
424
    if (eos || block->get_block()->column_metas_size()) {
184
424
        RETURN_IF_ERROR(_buffer->add_block(this, {block, eos}));
185
424
    }
186
422
    return Status::OK();
187
422
}
188
189
321k
Status Channel::_send_current_block(bool eos) {
190
321k
    if (is_local()) {
191
321k
        return _send_local_block(eos);
192
321k
    }
193
    // here _pblock maybe nullptr , but we must send the eos to the receiver
194
623
    return send_remote_block(std::move(_pblock), eos);
195
321k
}
196
197
320k
Status Channel::_send_local_block(bool eos) {
198
320k
    Block block;
199
320k
    if (_serializer.get_block() != nullptr) {
200
152k
        block = _serializer.get_block()->to_block();
201
152k
        _serializer.get_block()->set_mutable_columns(block.clone_empty_columns());
202
152k
    }
203
204
321k
    if (!block.empty() || eos) { // if eos is true, we MUST to send an empty block
205
321k
        RETURN_IF_ERROR(send_local_block(&block, eos, true));
206
321k
    }
207
320k
    return Status::OK();
208
320k
}
209
210
484k
Status Channel::send_local_block(Block* block, bool eos, bool can_be_moved) {
211
484k
    SCOPED_TIMER(_parent->local_send_timer());
212
213
484k
    if (eos) {
214
453k
        if (_eos_send) {
215
208k
            return Status::OK();
216
244k
        } else {
217
244k
            _eos_send = true;
218
244k
        }
219
453k
    }
220
221
275k
    if (is_receiver_eof()) {
222
0
        return _receiver_status;
223
0
    }
224
275k
    auto receiver_status = _recvr_status();
225
    // _local_recvr depdend on ExchangeLocalState* _parent to do some memory counter settings
226
    // but it only owns a raw pointer, so that the ExchangeLocalState object may be deconstructed.
227
    // Lock the fragment context to ensure the runtime state and other objects are not deconstructed
228
275k
    TaskExecutionContextSPtr ctx_lock = nullptr;
229
276k
    if (receiver_status.ok() && _local_recvr != nullptr) {
230
276k
        ctx_lock = _local_recvr->task_exec_ctx();
231
        // Do not return internal error, because when query finished, the downstream node
232
        // may finish before upstream node. And the object maybe deconstructed. If return error
233
        // then the upstream node may report error status to FE, the query is failed.
234
276k
        if (ctx_lock == nullptr) {
235
0
            receiver_status = Status::EndOfFile("local data stream receiver is deconstructed");
236
0
        }
237
276k
    }
238
277k
    if (receiver_status.ok()) {
239
277k
        COUNTER_UPDATE(_parent->local_bytes_send_counter(), block->bytes());
240
277k
        COUNTER_UPDATE(_parent->local_sent_rows(), block->rows());
241
277k
        COUNTER_UPDATE(_parent->blocks_sent_counter(), 1);
242
243
277k
        const auto sender_id = _parent->sender_id();
244
277k
        if (!block->empty()) [[likely]] {
245
64.3k
            _local_recvr->add_block(block, sender_id, can_be_moved);
246
64.3k
        }
247
248
277k
        if (eos) [[unlikely]] {
249
244k
            _local_recvr->remove_sender(sender_id, _be_number, Status::OK());
250
244k
            _parent->on_channel_finished(_fragment_instance_id.lo);
251
244k
        }
252
277k
        return Status::OK();
253
18.4E
    } else {
254
18.4E
        _receiver_status = std::move(receiver_status);
255
18.4E
        _parent->on_channel_finished(_fragment_instance_id.lo);
256
18.4E
        return _receiver_status;
257
18.4E
    }
258
275k
}
259
260
0
std::string Channel::debug_string() const {
261
0
    fmt::memory_buffer debug_string_buffer;
262
0
    fmt::format_to(debug_string_buffer,
263
0
                   "fragment_instance_id: {}, _dest_node_id: {}, _is_local: {}, _receiver_status: "
264
0
                   "{}, _closed: {}, _need_close: {}, _be_number: {}, _eos_send: {}",
265
0
                   print_id(_fragment_instance_id), _dest_node_id, _is_local,
266
0
                   _receiver_status.to_string(), _closed, _need_close, _be_number, _eos_send);
267
0
    if (_is_local) {
268
0
        fmt::format_to(debug_string_buffer, "_local_recvr: {}", _local_recvr->debug_string());
269
0
    }
270
0
    return fmt::to_string(debug_string_buffer);
271
0
}
272
273
245k
Status Channel::close(RuntimeState* state) {
274
245k
    if (_closed) {
275
178
        return Status::OK();
276
178
    }
277
245k
    _closed = true;
278
245k
    if (!_need_close) {
279
0
        return Status::OK();
280
0
    }
281
282
245k
    if (is_receiver_eof()) {
283
176
        _serializer.reset_block();
284
176
        return Status::OK();
285
245k
    } else {
286
245k
        return _send_current_block(true);
287
245k
    }
288
245k
}
289
290
BlockSerializer::BlockSerializer(ExchangeSinkLocalState* parent, bool is_local)
291
389k
        : _parent(parent), _is_local(is_local), _batch_size(parent->state()->batch_size()) {}
292
293
Status BlockSerializer::next_serialized_block(Block* block, PBlock* dest, size_t num_receivers,
294
                                              bool* serialized, bool eos, const uint32_t* data,
295
100k
                                              const uint32_t offset, const uint32_t size) {
296
100k
    if (_mutable_block == nullptr) {
297
76.4k
        _mutable_block = MutableBlock::create_unique(block->clone_empty());
298
76.4k
    }
299
300
100k
    {
301
100k
        SCOPED_TIMER(_parent->merge_block_timer());
302
100k
        if (data) {
303
99.6k
            if (size > 0) {
304
23.5k
                RETURN_IF_ERROR(
305
23.5k
                        _mutable_block->add_rows(block, data + offset, data + offset + size));
306
23.5k
            }
307
99.6k
        } else if (!block->empty()) {
308
366
            RETURN_IF_ERROR(_mutable_block->merge(*block));
309
366
        }
310
100k
    }
311
312
100k
    if (_mutable_block->rows() >= _batch_size || eos ||
313
100k
        (_mutable_block->rows() > 0 && _mutable_block->allocated_bytes() > _buffer_mem_limit)) {
314
77.4k
        if (!_is_local) {
315
0
            RETURN_IF_ERROR(_serialize_block(dest, num_receivers));
316
0
        }
317
77.4k
        *serialized = true;
318
77.4k
    } else {
319
22.6k
        *serialized = false;
320
22.6k
    }
321
100k
    return Status::OK();
322
100k
}
323
324
0
Status BlockSerializer::_serialize_block(PBlock* dest, size_t num_receivers) {
325
0
    if (_mutable_block && _mutable_block->rows() > 0) {
326
0
        auto block = _mutable_block->to_block();
327
0
        RETURN_IF_ERROR(serialize_block(&block, dest, num_receivers));
328
0
        if (_parent->state()->low_memory_mode()) {
329
0
            reset_block();
330
0
        } else {
331
0
            block.clear_column_data();
332
0
            _mutable_block->set_mutable_columns(block.mutate_columns());
333
0
        }
334
0
    }
335
336
0
    return Status::OK();
337
0
}
338
339
868
Status BlockSerializer::serialize_block(const Block* src, PBlock* dest, size_t num_receivers) {
340
868
    SCOPED_TIMER(_parent->_serialize_batch_timer);
341
868
    dest->Clear();
342
868
    size_t uncompressed_bytes = 0, compressed_bytes = 0;
343
868
    int64_t compress_time = 0;
344
868
    RETURN_IF_ERROR(src->serialize(_parent->_state->be_exec_version(), dest, &uncompressed_bytes,
345
868
                                   &compressed_bytes, &compress_time, _parent->compression_type(),
346
868
                                   _parent->transfer_large_data_by_brpc()));
347
868
    COUNTER_UPDATE(_parent->_bytes_sent_counter, compressed_bytes * num_receivers);
348
868
    COUNTER_UPDATE(_parent->_uncompressed_bytes_counter, uncompressed_bytes * num_receivers);
349
868
    COUNTER_UPDATE(_parent->_compress_timer, compress_time);
350
868
#ifndef BE_TEST
351
868
    _parent->state()->get_query_ctx()->resource_ctx()->io_context()->update_shuffle_send_bytes(
352
868
            compressed_bytes * num_receivers);
353
868
    _parent->state()->get_query_ctx()->resource_ctx()->io_context()->update_shuffle_send_rows(
354
868
            src->rows() * num_receivers);
355
868
#endif
356
357
868
    return Status::OK();
358
868
}
359
360
} // namespace doris