Coverage Report

Created: 2026-03-15 08:11

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/exec/sink/load_stream_stub.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/sink/load_stream_stub.h"
19
20
#include <sstream>
21
22
#include "common/cast_set.h"
23
#include "runtime/query_context.h"
24
#include "storage/rowset/rowset_writer.h"
25
#include "util/brpc_client_cache.h"
26
#include "util/debug_points.h"
27
#include "util/network_util.h"
28
#include "util/thrift_util.h"
29
#include "util/uid_util.h"
30
31
namespace doris {
32
#include "common/compile_check_begin.h"
33
34
int LoadStreamReplyHandler::on_received_messages(brpc::StreamId id, butil::IOBuf* const messages[],
35
0
                                                 size_t size) {
36
0
    auto stub = _stub.lock();
37
0
    if (!stub) {
38
0
        LOG(WARNING) << "stub is not exist when on_received_messages, " << *this
39
0
                     << ", stream_id=" << id;
40
0
        return 0;
41
0
    }
42
0
    for (size_t i = 0; i < size; i++) {
43
0
        butil::IOBufAsZeroCopyInputStream wrapper(*messages[i]);
44
0
        PLoadStreamResponse response;
45
0
        response.ParseFromZeroCopyStream(&wrapper);
46
47
0
        if (response.eos()) {
48
0
            stub->_is_eos.store(true);
49
0
        }
50
51
0
        Status st = Status::create<false>(response.status());
52
53
0
        std::stringstream ss;
54
0
        ss << "on_received_messages, " << *this << ", stream_id=" << id;
55
0
        if (response.success_tablet_ids_size() > 0) {
56
0
            ss << ", success tablet ids:";
57
0
            for (auto tablet_id : response.success_tablet_ids()) {
58
0
                ss << " " << tablet_id;
59
0
            }
60
0
            std::lock_guard<bthread::Mutex> lock(stub->_success_tablets_mutex);
61
0
            for (auto tablet_id : response.success_tablet_ids()) {
62
0
                stub->_success_tablets.push_back(tablet_id);
63
0
            }
64
0
        }
65
0
        if (response.failed_tablets_size() > 0) {
66
0
            ss << ", failed tablet ids:";
67
0
            for (auto pb : response.failed_tablets()) {
68
0
                ss << " " << pb.id() << ":" << Status::create(pb.status());
69
0
            }
70
0
            std::lock_guard<bthread::Mutex> lock(stub->_failed_tablets_mutex);
71
0
            for (auto pb : response.failed_tablets()) {
72
0
                stub->_failed_tablets.emplace(pb.id(), Status::create(pb.status()));
73
0
            }
74
0
        }
75
0
        if (response.tablet_schemas_size() > 0) {
76
0
            ss << ", tablet schema num: " << response.tablet_schemas_size();
77
0
            std::lock_guard<bthread::Mutex> lock(stub->_schema_mutex);
78
0
            for (const auto& schema : response.tablet_schemas()) {
79
0
                auto tablet_schema = std::make_unique<TabletSchema>();
80
0
                tablet_schema->init_from_pb(schema.tablet_schema());
81
0
                stub->_tablet_schema_for_index->emplace(schema.index_id(),
82
0
                                                        std::move(tablet_schema));
83
0
                stub->_enable_unique_mow_for_index->emplace(
84
0
                        schema.index_id(), schema.enable_unique_key_merge_on_write());
85
0
            }
86
0
            stub->_schema_cv.notify_all();
87
0
        }
88
0
        ss << ", status: " << st;
89
0
        LOG(INFO) << ss.str();
90
91
0
        if (response.tablet_load_rowset_num_infos_size() > 0) {
92
0
            stub->_refresh_back_pressure_version_wait_time(response.tablet_load_rowset_num_infos());
93
0
        }
94
95
0
        if (response.has_load_stream_profile()) {
96
0
            TRuntimeProfileTree tprofile;
97
0
            const uint8_t* buf =
98
0
                    reinterpret_cast<const uint8_t*>(response.load_stream_profile().data());
99
0
            uint32_t len = cast_set<uint32_t>(response.load_stream_profile().size());
100
0
            auto status = deserialize_thrift_msg(buf, &len, false, &tprofile);
101
0
            if (status.ok()) {
102
                // TODO
103
                //_sink->_state->load_channel_profile()->update(tprofile);
104
0
            } else {
105
0
                LOG(WARNING) << "load stream TRuntimeProfileTree deserialize failed, errmsg="
106
0
                             << status;
107
0
            }
108
0
        }
109
0
    }
110
0
    return 0;
111
0
}
112
113
0
void LoadStreamReplyHandler::on_closed(brpc::StreamId id) {
114
0
    Defer defer {[this]() { delete this; }};
115
0
    LOG(INFO) << "on_closed, " << *this << ", stream_id=" << id;
116
0
    auto stub = _stub.lock();
117
0
    if (!stub) {
118
0
        LOG(WARNING) << "stub is not exist when on_closed, " << *this;
119
0
        return;
120
0
    }
121
0
    stub->_is_closed.store(true);
122
0
}
123
124
0
inline std::ostream& operator<<(std::ostream& ostr, const LoadStreamReplyHandler& handler) {
125
0
    ostr << "LoadStreamReplyHandler load_id=" << UniqueId(handler._load_id)
126
0
         << ", dst_id=" << handler._dst_id;
127
0
    return ostr;
128
0
}
129
130
LoadStreamStub::LoadStreamStub(PUniqueId load_id, int64_t src_id,
131
                               std::shared_ptr<IndexToTabletSchema> schema_map,
132
                               std::shared_ptr<IndexToEnableMoW> mow_map, bool incremental)
133
57
        : _load_id(load_id),
134
57
          _src_id(src_id),
135
57
          _tablet_schema_for_index(schema_map),
136
57
          _enable_unique_mow_for_index(mow_map),
137
57
          _is_incremental(incremental) {};
138
139
57
LoadStreamStub::~LoadStreamStub() {
140
57
    if (_is_open.load() && !_is_closed.load()) {
141
0
        auto ret = brpc::StreamClose(_stream_id);
142
0
        LOG(INFO) << *this << " is deconstructed, close " << (ret == 0 ? "success" : "failed");
143
0
    }
144
57
}
145
146
// open_load_stream
147
Status LoadStreamStub::open(BrpcClientCache<PBackendService_Stub>* client_cache,
148
                            const NodeInfo& node_info, int64_t txn_id,
149
                            const OlapTableSchemaParam& schema,
150
                            const std::vector<PTabletID>& tablets_for_schema, int total_streams,
151
0
                            int64_t idle_timeout_ms, bool enable_profile) {
152
0
    std::unique_lock<bthread::Mutex> lock(_open_mutex);
153
0
    if (_is_init.load()) {
154
0
        return _status;
155
0
    }
156
0
    _is_init.store(true);
157
0
    _dst_id = node_info.id;
158
0
    brpc::StreamOptions opt;
159
0
    opt.max_buf_size = cast_set<int>(config::load_stream_max_buf_size);
160
0
    opt.idle_timeout_ms = idle_timeout_ms;
161
0
    opt.messages_in_batch = config::load_stream_messages_in_batch;
162
0
    opt.handler = new LoadStreamReplyHandler(_load_id, _dst_id, shared_from_this());
163
0
    brpc::Controller cntl;
164
0
    if (int ret = brpc::StreamCreate(&_stream_id, cntl, &opt)) {
165
0
        delete opt.handler;
166
0
        _status = Status::Error<true>(ret, "Failed to create stream");
167
0
        return _status;
168
0
    }
169
0
    cntl.set_timeout_ms(config::open_load_stream_timeout_ms);
170
0
    POpenLoadStreamRequest request;
171
0
    *request.mutable_load_id() = _load_id;
172
0
    request.set_src_id(_src_id);
173
0
    request.set_txn_id(txn_id);
174
0
    request.set_enable_profile(enable_profile);
175
0
    if (_is_incremental) {
176
0
        request.set_total_streams(0);
177
0
    } else if (total_streams > 0) {
178
0
        request.set_total_streams(total_streams);
179
0
    } else {
180
0
        _status = Status::InternalError("total_streams should be greator than 0");
181
0
        return _status;
182
0
    }
183
0
    request.set_idle_timeout_ms(idle_timeout_ms);
184
0
    schema.to_protobuf(request.mutable_schema());
185
0
    for (auto& tablet : tablets_for_schema) {
186
0
        *request.add_tablets() = tablet;
187
0
    }
188
0
    POpenLoadStreamResponse response;
189
    // set connection_group "streaming" to distinguish with non-streaming connections
190
0
    const auto& stub = client_cache->get_client(node_info.host, node_info.brpc_port);
191
0
    if (stub == nullptr) {
192
0
        return Status::InternalError("failed to init brpc client to {}:{}", node_info.host,
193
0
                                     node_info.brpc_port);
194
0
    }
195
0
    stub->open_load_stream(&cntl, &request, &response, nullptr);
196
0
    for (const auto& resp : response.tablet_schemas()) {
197
0
        auto tablet_schema = std::make_unique<TabletSchema>();
198
0
        tablet_schema->init_from_pb(resp.tablet_schema());
199
0
        _tablet_schema_for_index->emplace(resp.index_id(), std::move(tablet_schema));
200
0
        _enable_unique_mow_for_index->emplace(resp.index_id(),
201
0
                                              resp.enable_unique_key_merge_on_write());
202
0
    }
203
0
    if (response.tablet_load_rowset_num_infos_size() > 0) {
204
0
        _refresh_back_pressure_version_wait_time(response.tablet_load_rowset_num_infos());
205
0
    }
206
0
    if (cntl.Failed()) {
207
0
        brpc::StreamClose(_stream_id);
208
0
        _status = Status::InternalError("Failed to connect to backend {}: {}", _dst_id,
209
0
                                        cntl.ErrorText());
210
0
        return _status;
211
0
    }
212
0
    LOG(INFO) << "open load stream to host=" << node_info.host << ", port=" << node_info.brpc_port
213
0
              << ", " << *this;
214
0
    _is_open.store(true);
215
0
    _status = Status::OK();
216
0
    return _status;
217
0
}
218
219
// APPEND_DATA
220
Status LoadStreamStub::append_data(int64_t partition_id, int64_t index_id, int64_t tablet_id,
221
                                   int32_t segment_id, uint64_t offset, std::span<const Slice> data,
222
0
                                   bool segment_eos, FileType file_type) {
223
0
    if (!_is_open.load()) {
224
0
        add_failed_tablet(tablet_id, _status);
225
0
        return _status;
226
0
    }
227
0
    DBUG_EXECUTE_IF("LoadStreamStub.skip_send_segment", { return Status::OK(); });
228
0
    PStreamHeader header;
229
0
    header.set_src_id(_src_id);
230
0
    *header.mutable_load_id() = _load_id;
231
0
    header.set_partition_id(partition_id);
232
0
    header.set_index_id(index_id);
233
0
    header.set_tablet_id(tablet_id);
234
0
    header.set_segment_id(segment_id);
235
0
    header.set_segment_eos(segment_eos);
236
0
    header.set_offset(offset);
237
0
    header.set_opcode(doris::PStreamHeader::APPEND_DATA);
238
0
    header.set_file_type(file_type);
239
0
    return _encode_and_send(header, data);
240
0
}
241
242
// ADD_SEGMENT
243
Status LoadStreamStub::add_segment(int64_t partition_id, int64_t index_id, int64_t tablet_id,
244
0
                                   int32_t segment_id, const SegmentStatistics& segment_stat) {
245
0
    if (!_is_open.load()) {
246
0
        add_failed_tablet(tablet_id, _status);
247
0
        return _status;
248
0
    }
249
0
    DBUG_EXECUTE_IF("LoadStreamStub.skip_send_segment", { return Status::OK(); });
250
0
    PStreamHeader header;
251
0
    header.set_src_id(_src_id);
252
0
    *header.mutable_load_id() = _load_id;
253
0
    header.set_partition_id(partition_id);
254
0
    header.set_index_id(index_id);
255
0
    header.set_tablet_id(tablet_id);
256
0
    header.set_segment_id(segment_id);
257
0
    header.set_opcode(doris::PStreamHeader::ADD_SEGMENT);
258
0
    segment_stat.to_pb(header.mutable_segment_statistics());
259
0
    return _encode_and_send(header);
260
0
}
261
262
// CLOSE_LOAD
263
Status LoadStreamStub::close_load(const std::vector<PTabletID>& tablets_to_commit,
264
0
                                  int num_incremental_streams) {
265
0
    if (!_is_open.load()) {
266
0
        return _status;
267
0
    }
268
0
    PStreamHeader header;
269
0
    *header.mutable_load_id() = _load_id;
270
0
    header.set_src_id(_src_id);
271
0
    header.set_opcode(doris::PStreamHeader::CLOSE_LOAD);
272
0
    for (const auto& tablet : tablets_to_commit) {
273
0
        *header.add_tablets() = tablet;
274
0
    }
275
0
    header.set_num_incremental_streams(num_incremental_streams);
276
0
    _status = _encode_and_send(header);
277
0
    if (!_status.ok()) {
278
0
        LOG(WARNING) << "stream " << _stream_id << " close failed: " << _status;
279
0
        return _status;
280
0
    }
281
0
    _is_closing.store(true);
282
0
    return Status::OK();
283
0
}
284
285
// GET_SCHEMA
286
0
Status LoadStreamStub::get_schema(const std::vector<PTabletID>& tablets) {
287
0
    if (!_is_open.load()) {
288
0
        return _status;
289
0
    }
290
0
    PStreamHeader header;
291
0
    *header.mutable_load_id() = _load_id;
292
0
    header.set_src_id(_src_id);
293
0
    header.set_opcode(doris::PStreamHeader::GET_SCHEMA);
294
0
    std::ostringstream oss;
295
0
    oss << "fetching tablet schema from stream " << _stream_id
296
0
        << ", load id: " << print_id(_load_id) << ", tablet id:";
297
0
    for (const auto& tablet : tablets) {
298
0
        *header.add_tablets() = tablet;
299
0
        oss << " " << tablet.tablet_id();
300
0
    }
301
0
    if (tablets.size() == 0) {
302
0
        oss << " none";
303
0
    }
304
0
    LOG(INFO) << oss.str();
305
0
    return _encode_and_send(header);
306
0
}
307
308
Status LoadStreamStub::wait_for_schema(int64_t partition_id, int64_t index_id, int64_t tablet_id,
309
0
                                       int64_t timeout_ms) {
310
0
    if (!_is_open.load()) {
311
0
        return _status;
312
0
    }
313
0
    if (_tablet_schema_for_index->contains(index_id)) {
314
0
        return Status::OK();
315
0
    }
316
0
    PTabletID tablet;
317
0
    tablet.set_partition_id(partition_id);
318
0
    tablet.set_index_id(index_id);
319
0
    tablet.set_tablet_id(tablet_id);
320
0
    RETURN_IF_ERROR(get_schema({tablet}));
321
322
0
    MonotonicStopWatch watch;
323
0
    watch.start();
324
0
    while (!_tablet_schema_for_index->contains(index_id) &&
325
0
           watch.elapsed_time() / 1000 / 1000 < timeout_ms) {
326
0
        RETURN_IF_ERROR(check_cancel());
327
0
        static_cast<void>(wait_for_new_schema(100));
328
0
    }
329
330
0
    if (!_tablet_schema_for_index->contains(index_id)) {
331
0
        return Status::TimedOut("timeout to get tablet schema for index {}", index_id);
332
0
    }
333
0
    return Status::OK();
334
0
}
335
336
0
Status LoadStreamStub::close_finish_check(RuntimeState* state, bool* is_closed) {
337
0
    DBUG_EXECUTE_IF("LoadStreamStub::close_wait.long_wait", DBUG_BLOCK);
338
0
    DBUG_EXECUTE_IF("LoadStreamStub::close_finish_check.close_failed",
339
0
                    { return Status::InternalError("close failed"); });
340
0
    *is_closed = true;
341
0
    if (!_is_open.load()) {
342
        // we don't need to close wait on non-open streams
343
0
        return Status::OK();
344
0
    }
345
    // If stream is cancelled (e.g., due to connection failure), treat it as closed
346
    // to avoid waiting indefinitely for a stream that will never respond.
347
0
    if (_is_cancelled.load()) {
348
0
        return check_cancel();
349
0
    }
350
0
    if (state->get_query_ctx()->is_cancelled()) {
351
0
        return state->get_query_ctx()->exec_status();
352
0
    }
353
0
    if (!_is_closing.load()) {
354
0
        *is_closed = false;
355
0
        return _status;
356
0
    }
357
0
    if (_is_closed.load()) {
358
0
        RETURN_IF_ERROR(check_cancel());
359
0
        if (!_is_eos.load()) {
360
0
            return Status::InternalError("Stream closed without EOS, {}", to_string());
361
0
        }
362
0
        return Status::OK();
363
0
    }
364
0
    *is_closed = false;
365
0
    return Status::OK();
366
0
}
367
368
0
void LoadStreamStub::cancel(Status reason) {
369
0
    LOG(WARNING) << *this << " is cancelled because of " << reason;
370
0
    if (_is_open.load()) {
371
0
        brpc::StreamClose(_stream_id);
372
0
    }
373
0
    {
374
0
        std::lock_guard<bthread::Mutex> lock(_cancel_mutex);
375
0
        _cancel_st = reason;
376
0
        _is_cancelled.store(true);
377
0
    }
378
0
    _is_closed.store(true);
379
0
}
380
381
0
Status LoadStreamStub::_encode_and_send(PStreamHeader& header, std::span<const Slice> data) {
382
0
    butil::IOBuf buf;
383
0
    size_t header_len = header.ByteSizeLong();
384
0
    buf.append(reinterpret_cast<uint8_t*>(&header_len), sizeof(header_len));
385
0
    buf.append(header.SerializeAsString());
386
0
    size_t data_len = std::transform_reduce(data.begin(), data.end(), 0, std::plus(),
387
0
                                            [](const Slice& s) { return s.get_size(); });
388
0
    buf.append(reinterpret_cast<uint8_t*>(&data_len), sizeof(data_len));
389
0
    for (const auto& slice : data) {
390
0
        buf.append(slice.get_data(), slice.get_size());
391
0
    }
392
0
    bool eos = header.opcode() == doris::PStreamHeader::CLOSE_LOAD;
393
0
    bool get_schema = header.opcode() == doris::PStreamHeader::GET_SCHEMA;
394
0
    add_bytes_written(buf.size());
395
0
    return _send_with_buffer(buf, eos || get_schema);
396
0
}
397
398
0
Status LoadStreamStub::_send_with_buffer(butil::IOBuf& buf, bool sync) {
399
0
    butil::IOBuf output;
400
0
    std::unique_lock<decltype(_buffer_mutex)> buffer_lock(_buffer_mutex);
401
0
    _buffer.append(buf);
402
0
    if (!sync && _buffer.size() < config::brpc_streaming_client_batch_bytes) {
403
0
        return Status::OK();
404
0
    }
405
0
    output.swap(_buffer);
406
    // acquire send lock while holding buffer lock, to ensure the message order
407
0
    std::lock_guard<decltype(_send_mutex)> send_lock(_send_mutex);
408
0
    buffer_lock.unlock();
409
0
    VLOG_DEBUG << "send buf size : " << output.size() << ", sync: " << sync;
410
0
    auto st = _send_with_retry(output);
411
0
    if (!st.ok()) {
412
0
        _handle_failure(output, st);
413
0
    }
414
0
    return st;
415
0
}
416
417
0
void LoadStreamStub::_handle_failure(butil::IOBuf& buf, Status st) {
418
0
    while (buf.size() > 0) {
419
        // step 1: parse header
420
0
        size_t hdr_len = 0;
421
0
        buf.cutn((void*)&hdr_len, sizeof(size_t));
422
0
        butil::IOBuf hdr_buf;
423
0
        PStreamHeader hdr;
424
0
        buf.cutn(&hdr_buf, hdr_len);
425
0
        butil::IOBufAsZeroCopyInputStream wrapper(hdr_buf);
426
0
        hdr.ParseFromZeroCopyStream(&wrapper);
427
428
        // step 2: cut data
429
0
        size_t data_len = 0;
430
0
        buf.cutn((void*)&data_len, sizeof(size_t));
431
0
        butil::IOBuf data_buf;
432
0
        buf.cutn(&data_buf, data_len);
433
434
        // step 3: handle failure
435
0
        switch (hdr.opcode()) {
436
0
        case PStreamHeader::ADD_SEGMENT:
437
0
        case PStreamHeader::APPEND_DATA: {
438
0
            DBUG_EXECUTE_IF("LoadStreamStub._handle_failure.append_data_failed", {
439
0
                add_failed_tablet(hdr.tablet_id(), st);
440
0
                return;
441
0
            });
442
0
            DBUG_EXECUTE_IF("LoadStreamStub._handle_failure.add_segment_failed", {
443
0
                add_failed_tablet(hdr.tablet_id(), st);
444
0
                return;
445
0
            });
446
0
            add_failed_tablet(hdr.tablet_id(), st);
447
0
        } break;
448
0
        case PStreamHeader::CLOSE_LOAD: {
449
0
            DBUG_EXECUTE_IF("LoadStreamStub._handle_failure.close_load_failed", {
450
0
                brpc::StreamClose(_stream_id);
451
0
                return;
452
0
            });
453
0
            brpc::StreamClose(_stream_id);
454
0
        } break;
455
0
        case PStreamHeader::GET_SCHEMA: {
456
0
            DBUG_EXECUTE_IF("LoadStreamStub._handle_failure.get_schema_failed", {
457
                // Just log and let wait_for_schema timeout
458
0
                std::ostringstream oss;
459
0
                for (const auto& tablet : hdr.tablets()) {
460
0
                    oss << " " << tablet.tablet_id();
461
0
                }
462
0
                LOG(WARNING) << "failed to send GET_SCHEMA request, tablet_id:" << oss.str() << ", "
463
0
                             << *this;
464
0
                return;
465
0
            });
466
            // Just log and let wait_for_schema timeout
467
0
            std::ostringstream oss;
468
0
            for (const auto& tablet : hdr.tablets()) {
469
0
                oss << " " << tablet.tablet_id();
470
0
            }
471
0
            LOG(WARNING) << "failed to send GET_SCHEMA request, tablet_id:" << oss.str() << ", "
472
0
                         << *this;
473
0
        } break;
474
0
        default:
475
0
            LOG(WARNING) << "unexpected stream message " << hdr.opcode() << ", " << *this;
476
0
            DCHECK(false);
477
0
        }
478
0
    }
479
0
}
480
481
0
Status LoadStreamStub::_send_with_retry(butil::IOBuf& buf) {
482
0
    for (;;) {
483
0
        RETURN_IF_ERROR(check_cancel());
484
0
        int ret;
485
0
        {
486
0
            DBUG_EXECUTE_IF("LoadStreamStub._send_with_retry.delay_before_send", {
487
0
                int64_t delay_ms = dp->param<int64_t>("delay_ms", 1000);
488
0
                bthread_usleep(delay_ms * 1000);
489
0
            });
490
0
            brpc::StreamWriteOptions options;
491
0
            options.write_in_background = config::enable_brpc_stream_write_background;
492
0
            ret = brpc::StreamWrite(_stream_id, buf, &options);
493
0
        }
494
0
        DBUG_EXECUTE_IF("LoadStreamStub._send_with_retry.stream_write_failed", { ret = EPIPE; });
495
0
        switch (ret) {
496
0
        case 0:
497
0
            return Status::OK();
498
0
        case EAGAIN: {
499
0
            const timespec time = butil::seconds_from_now(config::load_stream_eagain_wait_seconds);
500
0
            int wait_ret = brpc::StreamWait(_stream_id, &time);
501
0
            if (wait_ret != 0) {
502
0
                return Status::InternalError("StreamWait failed, err={}, {}", wait_ret,
503
0
                                             to_string());
504
0
            }
505
0
            break;
506
0
        }
507
0
        default:
508
0
            return Status::InternalError("StreamWrite failed, err={}, {}", ret, to_string());
509
0
        }
510
0
    }
511
0
}
512
513
void LoadStreamStub::_refresh_back_pressure_version_wait_time(
514
        const ::google::protobuf::RepeatedPtrField<::doris::PTabletLoadRowsetInfo>&
515
0
                tablet_load_infos) {
516
0
    int64_t max_rowset_num_gap = 0;
517
    // if any one tablet is under high load pressure, we would make the whole procedure
518
    // sleep to prevent the corresponding BE return -235
519
0
    std::for_each(
520
0
            tablet_load_infos.begin(), tablet_load_infos.end(),
521
0
            [&max_rowset_num_gap](auto& load_info) {
522
0
                int64_t cur_rowset_num = load_info.current_rowset_nums();
523
0
                int64_t high_load_point = load_info.max_config_rowset_nums() *
524
0
                                          (config::load_back_pressure_version_threshold / 100);
525
0
                DCHECK(cur_rowset_num > high_load_point);
526
0
                max_rowset_num_gap = std::max(max_rowset_num_gap, cur_rowset_num - high_load_point);
527
0
            });
528
    // to slow down the high load pressure
529
    // we would use the rowset num gap to calculate one sleep time
530
    // for example:
531
    // if the max tablet version is 2000, there are 3 BE
532
    // A: ====================  1800
533
    // B: ===================   1700
534
    // C: ==================    1600
535
    //    ==================    1600
536
    //                      ^
537
    //                      the high load point
538
    // then then max gap is 1800 - (max tablet version * config::load_back_pressure_version_threshold / 100) = 200,
539
    // we would make the whole send procesure sleep
540
    // 1200ms for compaction to be done toe reduce the high pressure
541
0
    auto max_time = config::max_load_back_pressure_version_wait_time_ms;
542
0
    if (UNLIKELY(max_rowset_num_gap > 0)) {
543
0
        _load_back_pressure_version_wait_time_ms.store(
544
0
                std::min(max_rowset_num_gap + 1000, max_time));
545
0
        LOG(INFO) << "try to back pressure version, wait time(ms): "
546
0
                  << _load_back_pressure_version_wait_time_ms << ", load id: " << print_id(_load_id)
547
0
                  << ", max_rowset_num_gap: " << max_rowset_num_gap;
548
0
    }
549
0
}
550
551
0
std::string LoadStreamStub::to_string() {
552
0
    std::ostringstream ss;
553
0
    ss << *this;
554
0
    return ss.str();
555
0
}
556
557
0
inline std::ostream& operator<<(std::ostream& ostr, const LoadStreamStub& stub) {
558
0
    ostr << "LoadStreamStub load_id=" << print_id(stub._load_id) << ", src_id=" << stub._src_id
559
0
         << ", dst_id=" << stub._dst_id << ", stream_id=" << stub._stream_id;
560
0
    return ostr;
561
0
}
562
563
Status LoadStreamStubs::open(BrpcClientCache<PBackendService_Stub>* client_cache,
564
                             const NodeInfo& node_info, int64_t txn_id,
565
                             const OlapTableSchemaParam& schema,
566
                             const std::vector<PTabletID>& tablets_for_schema, int total_streams,
567
0
                             int64_t idle_timeout_ms, bool enable_profile) {
568
0
    bool get_schema = true;
569
0
    auto status = Status::OK();
570
0
    bool first_stream = true;
571
0
    for (auto& stream : _streams) {
572
0
        Status st;
573
0
        if (get_schema) {
574
0
            st = stream->open(client_cache, node_info, txn_id, schema, tablets_for_schema,
575
0
                              total_streams, idle_timeout_ms, enable_profile);
576
0
        } else {
577
0
            st = stream->open(client_cache, node_info, txn_id, schema, {}, total_streams,
578
0
                              idle_timeout_ms, enable_profile);
579
0
        }
580
        // Simulate one stream open failure within LoadStreamStubs.
581
        // This causes the successfully opened streams to be cancelled,
582
        // reproducing the bug where cancelled streams cause close_wait timeout.
583
0
        DBUG_EXECUTE_IF("LoadStreamStubs.open.fail_one_stream", {
584
0
            if (st.ok() && !first_stream) {
585
0
                st = Status::InternalError("Injected stream open failure");
586
0
            }
587
0
        });
588
0
        if (st.ok()) {
589
0
            get_schema = false;
590
0
        } else {
591
0
            LOG(WARNING) << "open stream failed: " << st << "; stream: " << *stream;
592
0
            status = st;
593
            // no break here to try get schema from the rest streams
594
0
        }
595
0
        first_stream = false;
596
0
    }
597
    // only mark open when all streams open success
598
0
    _open_success.store(status.ok());
599
    // cancel all streams if open failed
600
0
    if (!status.ok()) {
601
0
        cancel(status);
602
0
    }
603
0
    return status;
604
0
}
605
606
Status LoadStreamStubs::close_load(const std::vector<PTabletID>& tablets_to_commit,
607
0
                                   int num_incremental_streams) {
608
0
    if (!_open_success.load()) {
609
0
        return Status::InternalError("streams not open");
610
0
    }
611
0
    bool first = true;
612
0
    auto status = Status::OK();
613
0
    for (auto& stream : _streams) {
614
0
        Status st;
615
0
        if (first) {
616
0
            st = stream->close_load(tablets_to_commit, num_incremental_streams);
617
0
            first = false;
618
0
        } else {
619
0
            st = stream->close_load({}, num_incremental_streams);
620
0
        }
621
0
        if (!st.ok()) {
622
            LOG(WARNING) << "close_load failed: " << st << "; stream: " << *stream;
623
0
        }
624
0
    }
625
0
    return status;
626
0
}
627
628
} // namespace doris