MySQL 9.1.0
Source Code Documentation
connection.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2018, 2024, Oracle and/or its affiliates.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License, version 2.0,
6 as published by the Free Software Foundation.
7
8 This program is designed to work with certain software (including
9 but not limited to OpenSSL) that is licensed under separate terms,
10 as designated in a particular file or component or in included license
11 documentation. The authors of MySQL hereby grant you an additional
12 permission to link the program and your derivative works with the
13 separately licensed software that they have either included with
14 the program or referenced in the documentation.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24*/
25
26#ifndef ROUTING_CONNECTION_INCLUDED
27#define ROUTING_CONNECTION_INCLUDED
28
29#include <chrono>
30#include <cstdint> // size_t
31#include <functional>
32#include <memory>
33#include <optional>
34
36#include "context.h"
37#include "destination.h" // RouteDestination
38#include "destination_error.h"
43
45 public:
48 std::function<void(MySQLRoutingConnectionBase *)> remove_callback)
49 : context_(context), remove_callback_(std::move(remove_callback)) {}
50
51 virtual ~MySQLRoutingConnectionBase() = default;
52
54 const MySQLRoutingContext &context() const { return context_; }
55
56 virtual std::string get_destination_id() const = 0;
57
58 virtual std::string read_only_destination_id() const {
59 return get_destination_id();
60 }
61
62 virtual std::string read_write_destination_id() const {
63 return get_destination_id();
64 }
65
66 virtual std::optional<net::ip::tcp::endpoint> destination_endpoint()
67 const = 0;
68
69 virtual std::optional<net::ip::tcp::endpoint> read_only_destination_endpoint()
70 const {
71 return destination_endpoint();
72 }
73
74 virtual std::optional<net::ip::tcp::endpoint>
76 return destination_endpoint();
77 }
78
80
81 /**
82 * @brief Returns address of server to which connection is established.
83 *
84 * @return address of server
85 */
86 virtual std::string get_server_address() const = 0;
87
88 virtual void disconnect() = 0;
89
90 /**
91 * @brief Returns address of client which connected to router
92 *
93 * @return address of client
94 */
95 virtual std::string get_client_address() const = 0;
96
97 std::size_t get_bytes_up() const {
98 return stats_([](const Stats &stats) { return stats.bytes_up; });
99 }
100
101 std::size_t get_bytes_down() const {
102 return stats_([](const Stats &stats) { return stats.bytes_down; });
103 }
104
105 using clock_type = std::chrono::system_clock;
106 using time_point_type = clock_type::time_point;
107
109 return stats_([](const Stats &stats) { return stats.started; });
110 }
111
113 return stats_([](const Stats &stats) { return stats.connected_to_server; });
114 }
115
117 return stats_([](const Stats &stats) { return stats.last_sent_to_server; });
118 }
119
121 return stats_(
122 [](const Stats &stats) { return stats.last_received_from_server; });
123 }
124
125 struct Stats {
126 std::size_t bytes_up{0};
127 std::size_t bytes_down{0};
128
129 time_point_type started{clock_type::now()};
133 };
134
135 Stats get_stats() const {
136 return stats_([](const Stats &stats) { return stats; });
137 }
138
139 void transfered_to_server(size_t bytes) {
140 const auto now = clock_type::now();
141 stats_([bytes, now](Stats &stats) {
142 stats.last_sent_to_server = now;
143 stats.bytes_down += bytes;
144 });
145 }
146
147 void transfered_to_client(size_t bytes) {
148 const auto now = clock_type::now();
149 stats_([bytes, now](Stats &stats) {
150 stats.last_received_from_server = now;
151 stats.bytes_up += bytes;
152 });
153 }
154
156
157 void accepted();
158
159 virtual void connected();
160
161 template <class F>
162 auto disconnect_request(F &&f) {
163 return disconnect_(std::forward<F>(f));
164 }
165
166 bool disconnect_requested() const {
167 return disconnect_([](auto requested) { return requested; });
168 }
169
170 protected:
171 /** @brief wrapper for common data used by all routing threads */
173 /** @brief callback that is called when thread of execution completes */
175
177
179
181
182 private:
184 std::string client_id_;
185 std::string server_id_;
186};
187
189 public:
191
192 ConnectorBase(net::io_context &io_ctx, RouteDestination *route_destination,
193 Destinations &destinations)
194 : io_ctx_{io_ctx},
195 route_destination_{route_destination},
196 destinations_{destinations},
198
199 enum class Function {
202 };
203
206
208
210
211 bool connect_timed_out() const { return connect_timed_out_; }
212
213 void destination_id(std::string id) { destination_id_ = id; }
214 std::string destination_id() const { return destination_id_; }
215
217 std::function<void(std::string, uint16_t, std::error_code)> func) {
218 on_connect_failure_ = std::move(func);
219 }
220
221 void on_connect_success(std::function<void(std::string, uint16_t)> func) {
222 on_connect_success_ = std::move(func);
223 }
224
225 void on_is_destination_good(std::function<bool(std::string, uint16_t)> func) {
226 on_is_destination_good_ = std::move(func);
227 }
228
229 bool is_destination_good(const std::string &hostname, uint16_t port) const {
231
232 return true;
233 }
234
235 protected:
246
248
252
258
260
262
264
266 std::string destination_id_;
267
268 std::function<void(std::string, uint16_t, std::error_code)>
270 std::function<void(std::string, uint16_t)> on_connect_success_;
271 std::function<bool(std::string, uint16_t)> on_is_destination_good_;
272};
273
274template <class ConnectionType>
275class Connector : public ConnectorBase {
276 public:
278
280 switch (func_) {
282 auto init_res = init_destination();
283 if (!init_res) return stdx::unexpected(init_res.error());
284
285 } break;
287 auto connect_res = connect_finish();
288 if (!connect_res) return stdx::unexpected(connect_res.error());
289
290 } break;
291 }
292
293 if (destination_id().empty()) {
294 // stops at 'connect_init()
295 {
296 auto connect_res = try_connect();
297 if (!connect_res) return stdx::unexpected(connect_res.error());
298 }
299 }
301
302 return ret_type{std::in_place,
303 std::make_unique<TcpConnection>(std::move(socket()),
304 std::move(endpoint()))};
305 }
306};
307
308template <class ConnectionType>
310 public:
311 using pool_lookup_cb = std::function<std::optional<ConnectionType>(
313
315 Destinations &destinations, pool_lookup_cb pool_lookup)
316 : ConnectorBase{io_ctx, route_destination, destinations},
317 pool_lookup_{std::move(pool_lookup)} {}
318
320 switch (func_) {
322 auto init_res = init_destination();
323 if (!init_res) return stdx::unexpected(init_res.error());
324
325 } break;
327 auto connect_res = connect_finish();
328 if (!connect_res) return stdx::unexpected(connect_res.error());
329
330 } break;
331 }
332
333 if (destination_id().empty()) {
334 // stops at 'connect_init()
335 if (auto pool_res = probe_pool()) {
336 // return the pooled connection.
337 return std::move(pool_res.value());
338 }
339
340 {
341 auto connect_res = try_connect();
342 if (!connect_res) return stdx::unexpected(connect_res.error());
343 }
344 }
345
346 return {std::in_place, std::make_unique<TcpConnection>(
347 std::move(socket()), std::move(endpoint()))};
348 }
349
350 private:
351 std::optional<ConnectionType> probe_pool() {
353 }
354
356};
357
358#endif /* ROUTING_CONNECTION_INCLUDED */
Definition: connection.h:188
server_protocol_type::endpoint & endpoint()
Definition: connection.h:205
void connect_timed_out(bool v)
Definition: connection.h:209
std::function< void(std::string, uint16_t)> on_connect_success_
Definition: connection.h:270
std::function< void(std::string, uint16_t, std::error_code)> on_connect_failure_
Definition: connection.h:269
net::steady_timer & timer()
Definition: connection.h:207
stdx::expected< void, std::error_code > connect_init()
Definition: connection.cc:90
stdx::expected< void, std::error_code > init_destination()
Definition: connection.cc:38
Destinations & destinations_
Definition: connection.h:254
std::error_code last_ec_
Definition: connection.h:259
net::steady_timer connect_timer_
Definition: connection.h:263
stdx::expected< void, std::error_code > resolve()
Definition: connection.cc:54
server_protocol_type::endpoint server_endpoint_
Definition: connection.h:251
Function
Definition: connection.h:199
stdx::expected< void, std::error_code > init_endpoint()
Definition: connection.cc:84
std::string destination_id() const
Definition: connection.h:214
stdx::expected< void, std::error_code > connected()
Definition: connection.cc:218
stdx::expected< void, std::error_code > connect_failed(std::error_code ec)
ConnectorBase(net::io_context &io_ctx, RouteDestination *route_destination, Destinations &destinations)
Definition: connection.h:192
void destination_id(std::string id)
Definition: connection.h:213
bool connect_timed_out_
Definition: connection.h:265
void on_connect_success(std::function< void(std::string, uint16_t)> func)
Definition: connection.h:221
Destinations::iterator destinations_it_
Definition: connection.h:255
net::ip::tcp::resolver::results_type endpoints_
Definition: connection.h:256
bool connect_timed_out() const
Definition: connection.h:211
std::function< bool(std::string, uint16_t)> on_is_destination_good_
Definition: connection.h:271
Function func_
Definition: connection.h:261
server_protocol_type::socket & socket()
Definition: connection.h:204
net::ip::tcp::resolver resolver_
Definition: connection.h:249
bool is_destination_good(const std::string &hostname, uint16_t port) const
Definition: connection.h:229
void on_is_destination_good(std::function< bool(std::string, uint16_t)> func)
Definition: connection.h:225
stdx::expected< void, std::error_code > next_destination()
Definition: connection.cc:250
net::io_context & io_ctx_
Definition: connection.h:247
stdx::expected< void, std::error_code > try_connect()
Definition: connection.cc:103
std::string destination_id_
Definition: connection.h:266
net::ip::tcp::resolver::results_type::iterator endpoints_it_
Definition: connection.h:257
stdx::expected< void, std::error_code > connect_finish()
Definition: connection.cc:185
server_protocol_type::socket server_sock_
Definition: connection.h:250
stdx::expected< void, std::error_code > next_endpoint()
Definition: connection.cc:230
void on_connect_failure(std::function< void(std::string, uint16_t, std::error_code)> func)
Definition: connection.h:216
RouteDestination * route_destination_
Definition: connection.h:253
Definition: connection.h:275
stdx::expected< ConnectionType, std::error_code > connect()
Definition: connection.h:279
A forward iterable container of destinations.
Definition: destination.h:107
typename container_type::iterator iterator
Definition: destination.h:111
Monitor pattern.
Definition: monitor.h:39
Definition: connection.h:44
virtual std::string get_destination_id() const =0
void accepted()
Definition: connection.cc:277
MySQLRoutingConnectionBase(MySQLRoutingContext &context, std::function< void(MySQLRoutingConnectionBase *)> remove_callback)
Definition: connection.h:46
clock_type::time_point time_point_type
Definition: connection.h:106
virtual void connected()
Definition: connection.cc:285
const MySQLRoutingContext & context() const
Definition: connection.h:54
time_point_type get_started() const
Definition: connection.h:108
Stats get_stats() const
Definition: connection.h:135
std::size_t get_bytes_down() const
Definition: connection.h:101
std::chrono::system_clock clock_type
Definition: connection.h:105
void transfered_to_server(size_t bytes)
Definition: connection.h:139
virtual std::string read_only_destination_id() const
Definition: connection.h:58
virtual std::optional< net::ip::tcp::endpoint > read_write_destination_endpoint() const
Definition: connection.h:75
std::size_t get_bytes_up() const
Definition: connection.h:97
Monitor< bool > disconnect_
Definition: connection.h:178
net::impl::socket::native_handle_type client_fd_
Definition: connection.h:183
virtual std::optional< net::ip::tcp::endpoint > read_only_destination_endpoint() const
Definition: connection.h:69
void disassociate()
Definition: connection.h:155
void log_connection_summary()
Definition: connection.cc:297
Monitor< Stats > stats_
Definition: connection.h:176
virtual void disconnect()=0
time_point_type get_connected_to_server() const
Definition: connection.h:112
virtual std::string get_server_address() const =0
Returns address of server to which connection is established.
std::function< void(MySQLRoutingConnectionBase *)> remove_callback_
callback that is called when thread of execution completes
Definition: connection.h:174
virtual std::string get_client_address() const =0
Returns address of client which connected to router.
virtual ~MySQLRoutingConnectionBase()=default
MySQLRoutingContext & context()
Definition: connection.h:53
void transfered_to_client(size_t bytes)
Definition: connection.h:147
std::string client_id_
Definition: connection.h:184
time_point_type get_last_received_from_server() const
Definition: connection.h:120
virtual std::string read_write_destination_id() const
Definition: connection.h:62
time_point_type get_last_sent_to_server() const
Definition: connection.h:116
auto disconnect_request(F &&f)
Definition: connection.h:162
virtual net::impl::socket::native_handle_type get_client_fd() const =0
virtual std::optional< net::ip::tcp::endpoint > destination_endpoint() const =0
std::string server_id_
Definition: connection.h:185
bool disconnect_requested() const
Definition: connection.h:166
MySQLRoutingContext & context_
wrapper for common data used by all routing threads
Definition: connection.h:172
MySQLRoutingContext holds data used by MySQLRouting (1 per plugin instances) and MySQLRoutingConnecti...
Definition: context.h:59
Definition: connection.h:309
stdx::expected< ConnectionType, std::error_code > connect()
Definition: connection.h:319
pool_lookup_cb pool_lookup_
Definition: connection.h:355
std::optional< ConnectionType > probe_pool()
Definition: connection.h:351
PooledConnector(net::io_context &io_ctx, RouteDestination *route_destination, Destinations &destinations, pool_lookup_cb pool_lookup)
Definition: connection.h:314
std::function< std::optional< ConnectionType >(const server_protocol_type::endpoint &ep)> pool_lookup_cb
Definition: connection.h:312
Manage destinations for a Connection Routing.
Definition: destination.h:189
Definition: socket.h:1090
Definition: timer.h:57
Definition: io_context.h:61
Definition: internet.h:678
Definition: internet.h:542
const_iterator iterator
Definition: internet.h:550
Definition: internet.h:608
TCP protocol.
Definition: internet.h:1155
Definition: expected.h:286
struct stats stats
Definition: mysqlslap.cc:238
bool empty(const Histogram &histogram)
Return true if 'histogram' was built on an empty table.
Definition: histogram.h:693
std::error_code make_error_code(DynamicLoaderErrc ec)
make error_code from a DynamicLoaderErrc.
Definition: dynamic_loader.cc:79
const char * begin(const char *const c)
Definition: base64.h:44
int native_handle_type
Definition: socket_constants.h:51
Definition: gcs_xcom_synode.h:64
unexpected(E) -> unexpected< E >
required uint64 port
Definition: replication_asynchronous_connection_failover.proto:33
Definition: connection.h:125
time_point_type started
Definition: connection.h:129
time_point_type last_sent_to_server
Definition: connection.h:131
time_point_type connected_to_server
Definition: connection.h:130
std::size_t bytes_down
Definition: connection.h:127
time_point_type last_received_from_server
Definition: connection.h:132
std::size_t bytes_up
Definition: connection.h:126
Definition: mysqlslap.cc:240
unsigned long id[MAX_DEAD]
Definition: xcom_base.cc:510