Coverage Report

Created: 2026-05-14 18:32

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/util/proto_util.h
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
#pragma once
19
20
#include <brpc/http_method.h>
21
#include <gen_cpp/internal_service.pb.h>
22
23
#include "common/config.h"
24
#include "common/status.h"
25
#include "runtime/exec_env.h"
26
#include "runtime/runtime_state.h"
27
#include "util/brpc_client_cache.h"
28
#include "util/network_util.h"
29
30
namespace doris {
31
32
// When the tuple/block data is greater than 2G, embed the tuple/block data
33
// and the request serialization string in the attachment, and use "http" brpc.
34
// "http"brpc requires that only one of request and attachment be non-null.
35
//
36
// 2G: In the default "baidu_std" brpcd, upper limit of the request and attachment length is 2G.
37
constexpr size_t MIN_HTTP_BRPC_SIZE = (1ULL << 31);
38
39
template <typename Params, typename Closure>
40
Status request_embed_attachmentv2(Params* brpc_request, const std::string& data,
41
                                  std::unique_ptr<Closure>& closure);
42
43
// Embed column_values and brpc request serialization string in controller attachment.
44
template <typename Params, typename Closure>
45
Status request_embed_attachment_contain_blockv2(Params* brpc_request,
46
0
                                                std::unique_ptr<Closure>& closure) {
47
0
    auto* block = brpc_request->mutable_block();
48
0
    std::string column_values = std::move(*block->mutable_column_values());
49
0
    block->mutable_column_values()->clear();
50
0
    return request_embed_attachmentv2(brpc_request, column_values, closure);
51
0
}
Unexecuted instantiation: _ZN5doris40request_embed_attachment_contain_blockv2INS_28PTabletWriterAddBlockRequestENS_18AutoReleaseClosureIS1_NS_18WriteBlockCallbackINS_27PTabletWriterAddBlockResultEEEEEEENS_6StatusEPT_RSt10unique_ptrIT0_St14default_deleteISB_EE
Unexecuted instantiation: _ZN5doris40request_embed_attachment_contain_blockv2INS_19PTransmitDataParamsENS_18AutoReleaseClosureIS1_NS_20ExchangeSendCallbackINS_19PTransmitDataResultEEEEEEENS_6StatusEPT_RSt10unique_ptrIT0_St14default_deleteISB_EE
52
53
1.49M
inline bool enable_http_send_block(const PTransmitDataParams& request) {
54
1.49M
    if (!config::transfer_large_data_by_brpc) {
55
0
        return false;
56
0
    }
57
1.49M
    if (!request.has_block() || !request.block().has_column_values()) {
58
1.47M
        return false;
59
1.47M
    }
60
13.2k
    if (request.ByteSizeLong() < MIN_HTTP_BRPC_SIZE) {
61
0
        return false;
62
0
    }
63
13.2k
    return true;
64
13.2k
}
65
66
template <typename Closure>
67
1.48M
void transmit_blockv2(PBackendService_Stub* stub, std::unique_ptr<Closure> closure) {
68
1.48M
    closure->cntl_->http_request().Clear();
69
1.48M
    stub->transmit_block(closure->cntl_.get(), closure->request_.get(), closure->response_.get(),
70
1.48M
                         closure.get());
71
1.48M
    closure.release();
72
1.48M
}
73
74
template <typename Closure>
75
Status transmit_block_httpv2_impl(ExecEnv* exec_env, std::unique_ptr<Closure> closure,
76
0
                                  TNetworkAddress brpc_dest_addr) {
77
0
    std::string host = brpc_dest_addr.hostname;
78
0
    auto* dns_cache = ExecEnv::GetInstance()->dns_cache();
79
0
    if (dns_cache == nullptr) {
80
0
        LOG(WARNING) << "DNS cache is not initialized, skipping hostname resolve";
81
0
    } else if (!is_valid_ip(brpc_dest_addr.hostname)) {
82
0
        Status status = dns_cache->get(brpc_dest_addr.hostname, &host);
83
0
        if (!status.ok()) {
84
0
            LOG(WARNING) << "failed to get ip from host " << brpc_dest_addr.hostname << ": "
85
0
                         << status.to_string();
86
0
            return Status::InternalError("failed to get ip from host {}", brpc_dest_addr.hostname);
87
0
        }
88
0
    }
89
    //format an ipv6 address
90
0
    std::string brpc_url = get_brpc_http_url(brpc_dest_addr.hostname, brpc_dest_addr.port);
91
92
0
    std::shared_ptr<PBackendService_Stub> brpc_http_stub =
93
0
            exec_env->brpc_internal_client_cache()->get_new_client_no_cache(brpc_url, "http");
94
0
    if (brpc_http_stub == nullptr) {
95
0
        return Status::InternalError("failed to open brpc http client to {}", brpc_url);
96
0
    }
97
0
    closure->cntl_->http_request().uri() =
98
0
            brpc_url + "/PInternalServiceImpl/transmit_block_by_http";
99
0
    closure->cntl_->http_request().set_method(brpc::HTTP_METHOD_POST);
100
0
    closure->cntl_->http_request().set_content_type("application/json");
101
0
    brpc_http_stub->transmit_block_by_http(closure->cntl_.get(), nullptr, closure->response_.get(),
102
0
                                           closure.get());
103
0
    closure.release();
104
105
0
    return Status::OK();
106
0
}
107
108
template <typename Closure>
109
Status transmit_block_httpv2(ExecEnv* exec_env, std::unique_ptr<Closure> closure,
110
0
                             TNetworkAddress brpc_dest_addr) {
111
0
    RETURN_IF_ERROR(request_embed_attachment_contain_blockv2(closure->request_.get(), closure));
112
0
    return transmit_block_httpv2_impl(exec_env, std::move(closure), brpc_dest_addr);
113
0
}
114
115
template <typename Closure>
116
Status transmit_block_httpv2_with_attachment_data(ExecEnv* exec_env,
117
                                                  std::unique_ptr<Closure> closure,
118
                                                  TNetworkAddress brpc_dest_addr,
119
0
                                                  const std::string& data) {
120
0
    RETURN_IF_ERROR(request_embed_attachmentv2(closure->request_.get(), data, closure));
121
0
    return transmit_block_httpv2_impl(exec_env, std::move(closure), brpc_dest_addr);
122
0
}
123
124
template <typename Params, typename Closure>
125
Status request_embed_attachmentv2(Params* brpc_request, const std::string& data,
126
0
                                  std::unique_ptr<Closure>& closure) {
127
0
    butil::IOBuf attachment;
128
129
    // step1: serialize brpc_request to string, and append to attachment.
130
0
    std::string req_str;
131
0
    if (!brpc_request->SerializeToString(&req_str)) {
132
0
        return Status::InternalError("failed to serialize the request");
133
0
    }
134
0
    int64_t req_str_size = req_str.size();
135
0
    attachment.append(&req_str_size, sizeof(req_str_size));
136
0
    attachment.append(req_str);
137
138
    // step2: append data to attachment and put it in the closure.
139
0
    int64_t data_size = data.size();
140
0
    attachment.append(&data_size, sizeof(data_size));
141
0
    try {
142
0
        attachment.append(data);
143
0
    } catch (...) {
144
0
        LOG(WARNING) << "Try to alloc " << data_size
145
0
                     << " bytes for append data to attachment failed. ";
146
0
        return Status::MemoryAllocFailed("request embed attachment failed to memcpy {} bytes",
147
0
                                         data_size);
148
0
    }
149
    // step3: attachment add to closure.
150
0
    closure->cntl_->request_attachment().swap(attachment);
151
0
    return Status::OK();
152
0
}
Unexecuted instantiation: _ZN5doris26request_embed_attachmentv2INS_28PTabletWriterAddBlockRequestENS_18AutoReleaseClosureIS1_NS_18WriteBlockCallbackINS_27PTabletWriterAddBlockResultEEEEEEENS_6StatusEPT_RKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERSt10unique_ptrIT0_St14default_deleteISJ_EE
Unexecuted instantiation: _ZN5doris26request_embed_attachmentv2INS_19PTransmitDataParamsENS_18AutoReleaseClosureIS1_NS_20ExchangeSendCallbackINS_19PTransmitDataResultEEEEEEENS_6StatusEPT_RKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERSt10unique_ptrIT0_St14default_deleteISJ_EE
153
154
// Extract the brpc request and block from the controller attachment,
155
// and put the block into the request.
156
template <typename Params>
157
2
Status attachment_extract_request_contain_block(Params* brpc_request, brpc::Controller* cntl) {
158
2
    auto block = brpc_request->mutable_block();
159
2
    return attachment_extract_request(brpc_request, cntl, block->mutable_column_values());
160
2
}
Unexecuted instantiation: _ZN5doris40attachment_extract_request_contain_blockINS_28PTabletWriterAddBlockRequestEEENS_6StatusEPT_PN4brpc10ControllerE
_ZN5doris40attachment_extract_request_contain_blockINS_19PTransmitDataParamsEEENS_6StatusEPT_PN4brpc10ControllerE
Line
Count
Source
157
2
Status attachment_extract_request_contain_block(Params* brpc_request, brpc::Controller* cntl) {
158
2
    auto block = brpc_request->mutable_block();
159
2
    return attachment_extract_request(brpc_request, cntl, block->mutable_column_values());
160
2
}
161
162
template <typename Params>
163
2
Status attachment_extract_request(Params* brpc_request, brpc::Controller* cntl, std::string* data) {
164
2
    const butil::IOBuf& io_buf = cntl->request_attachment();
165
166
    // step1: deserialize request string to brpc_request from attachment.
167
2
    int64_t req_str_size;
168
2
    io_buf.copy_to(&req_str_size, sizeof(req_str_size), 0);
169
2
    std::string req_str;
170
2
    io_buf.copy_to(&req_str, req_str_size, sizeof(req_str_size));
171
2
    brpc_request->ParseFromString(req_str);
172
173
    // step2: extract data from attachment.
174
2
    int64_t data_size;
175
2
    io_buf.copy_to(&data_size, sizeof(data_size), sizeof(req_str_size) + req_str_size);
176
2
    try {
177
2
        io_buf.copy_to(data, data_size, sizeof(data_size) + sizeof(req_str_size) + req_str_size);
178
2
    } catch (...) {
179
0
        LOG(WARNING) << "Try to alloc " << data_size
180
0
                     << " bytes for extract data from attachment failed. ";
181
0
        return Status::MemoryAllocFailed("attachment extract request failed to memcpy {} bytes",
182
0
                                         data_size);
183
0
    }
184
2
    return Status::OK();
185
2
}
Unexecuted instantiation: _ZN5doris26attachment_extract_requestINS_28PTabletWriterAddBlockRequestEEENS_6StatusEPT_PN4brpc10ControllerEPNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE
_ZN5doris26attachment_extract_requestINS_19PTransmitDataParamsEEENS_6StatusEPT_PN4brpc10ControllerEPNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE
Line
Count
Source
163
2
Status attachment_extract_request(Params* brpc_request, brpc::Controller* cntl, std::string* data) {
164
2
    const butil::IOBuf& io_buf = cntl->request_attachment();
165
166
    // step1: deserialize request string to brpc_request from attachment.
167
2
    int64_t req_str_size;
168
2
    io_buf.copy_to(&req_str_size, sizeof(req_str_size), 0);
169
2
    std::string req_str;
170
2
    io_buf.copy_to(&req_str, req_str_size, sizeof(req_str_size));
171
2
    brpc_request->ParseFromString(req_str);
172
173
    // step2: extract data from attachment.
174
2
    int64_t data_size;
175
2
    io_buf.copy_to(&data_size, sizeof(data_size), sizeof(req_str_size) + req_str_size);
176
2
    try {
177
2
        io_buf.copy_to(data, data_size, sizeof(data_size) + sizeof(req_str_size) + req_str_size);
178
2
    } catch (...) {
179
0
        LOG(WARNING) << "Try to alloc " << data_size
180
0
                     << " bytes for extract data from attachment failed. ";
181
0
        return Status::MemoryAllocFailed("attachment extract request failed to memcpy {} bytes",
182
0
                                         data_size);
183
0
    }
184
2
    return Status::OK();
185
2
}
186
187
} // namespace doris