Coverage Report

Created: 2026-08-11 01:04

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/root/doris/be/src/util/thrift_client.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 <thrift/protocol/TBinaryProtocol.h>
21
#include <thrift/transport/TBufferTransports.h>
22
#include <thrift/transport/TSSLSocket.h>
23
#include <thrift/transport/TSocket.h>
24
25
#include <memory>
26
#include <sstream>
27
#include <string>
28
29
#include "common/logging.h"
30
#include "common/status.h"
31
#include "util/thrift_server.h"
32
33
namespace apache {
34
namespace thrift {
35
namespace transport {
36
class TTransport;
37
} // namespace transport
38
} // namespace thrift
39
} // namespace apache
40
41
namespace doris {
42
43
#define THRIFT_MOVE_VALUES(thrift, member, value) \
44
0
    thrift.__isset.member = true;                 \
45
0
    thrift.member = std::move(value);
46
47
// Super class for templatized thrift clients.
48
class ThriftClientImpl {
49
public:
50
0
    virtual ~ThriftClientImpl() { close(); }
51
0
    const std::string& ipaddress() { return _ipaddress; }
52
0
    int port() const { return _port; }
53
54
    // Open the connection to the remote server. May be called
55
    // repeatedly, is idempotent unless there is a failure to connect.
56
    Status open();
57
58
    // Retry the Open num_retries time waiting wait_ms milliseconds between retries.
59
    Status open_with_retry(int num_retries, int wait_ms);
60
61
    // close the connection with the remote server. May be called
62
    // repeatedly.
63
    void close();
64
65
    // Set the connect timeout
66
0
    void set_conn_timeout(int ms) { _socket->setConnTimeout(ms); }
67
68
    // Set the receive timeout
69
0
    void set_recv_timeout(int ms) { _socket->setRecvTimeout(ms); }
70
71
    // Set the send timeout
72
0
    void set_send_timeout(int ms) { _socket->setSendTimeout(ms); }
73
74
protected:
75
    ThriftClientImpl(const std::string& ipaddress, int port);
76
77
    std::string _ipaddress;
78
    int _port;
79
80
    // All shared pointers, because Thrift requires them to be
81
    std::shared_ptr<apache::thrift::transport::TSocket> _socket;
82
    std::shared_ptr<apache::thrift::transport::TTransport> _transport;
83
    std::shared_ptr<apache::thrift::protocol::TBinaryProtocol> _protocol;
84
};
85
86
// Utility client to a Thrift server. The parameter type is the
87
// Thrift interface type that the server implements.
88
template <class InterfaceType>
89
class ThriftClient : public ThriftClientImpl {
90
public:
91
    ThriftClient(const std::string& ipaddress, int port);
92
93
    ThriftClient(const std::string& ipaddress, int port, ThriftServer::ServerType server_type);
94
95
    // Returns the object used to actually make RPCs against the remote server
96
0
    InterfaceType* iface() { return _iface.get(); }
Unexecuted instantiation: _ZN5doris12ThriftClientINS_21FrontendServiceClientEE5ifaceEv
Unexecuted instantiation: _ZN5doris12ThriftClientINS_24TPaloBrokerServiceClientEE5ifaceEv
Unexecuted instantiation: _ZN5doris12ThriftClientINS_20BackendServiceClientEE5ifaceEv
97
98
private:
99
    std::shared_ptr<InterfaceType> _iface;
100
};
101
102
template <class InterfaceType>
103
ThriftClient<InterfaceType>::ThriftClient(const std::string& ipaddress, int port)
104
        : ThriftClientImpl(ipaddress, port) {
105
    _transport.reset(new apache::thrift::transport::TBufferedTransport(_socket));
106
    _transport->getConfiguration()->setMaxMessageSize(config::thrift_max_message_size);
107
    _protocol.reset(new apache::thrift::protocol::TBinaryProtocol(_transport));
108
    _iface.reset(new InterfaceType(_protocol));
109
}
110
111
template <class InterfaceType>
112
ThriftClient<InterfaceType>::ThriftClient(const std::string& ipaddress, int port,
113
                                          ThriftServer::ServerType server_type)
114
0
        : ThriftClientImpl(ipaddress, port) {
115
0
    switch (server_type) {
116
0
    case ThriftServer::NON_BLOCKING:
117
0
        _transport.reset(new apache::thrift::transport::TFramedTransport(_socket));
118
0
        _transport->getConfiguration()->setMaxMessageSize(config::thrift_max_message_size);
119
0
        break;
120
0
    case ThriftServer::THREADED:
121
0
    case ThriftServer::THREAD_POOL:
122
0
        _transport.reset(new apache::thrift::transport::TBufferedTransport(_socket));
123
0
        _transport->getConfiguration()->setMaxMessageSize(config::thrift_max_message_size);
124
0
        break;
125
0
    default:
126
0
        std::stringstream error_msg;
127
0
        error_msg << "Unsupported server type: " << server_type;
128
0
        LOG(ERROR) << error_msg.str();
129
0
        DCHECK(false);
130
0
        break;
131
0
    }
132
133
0
    _protocol.reset(new apache::thrift::protocol::TBinaryProtocol(_transport));
134
0
    _iface.reset(new InterfaceType(_protocol));
135
0
}
Unexecuted instantiation: _ZN5doris12ThriftClientINS_21FrontendServiceClientEEC2ERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEiNS_12ThriftServer10ServerTypeE
Unexecuted instantiation: _ZN5doris12ThriftClientINS_24TPaloBrokerServiceClientEEC2ERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEiNS_12ThriftServer10ServerTypeE
Unexecuted instantiation: _ZN5doris12ThriftClientINS_20BackendServiceClientEEC2ERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEiNS_12ThriftServer10ServerTypeE
136
137
} // namespace doris