Coverage Report

Created: 2025-12-31 18:49

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