MySQL 8.0.39
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"
42
44 public:
47 std::function<void(MySQLRoutingConnectionBase *)> remove_callback)
48 : context_(context), remove_callback_(std::move(remove_callback)) {}
49
50 virtual ~MySQLRoutingConnectionBase() = default;
51
53 const MySQLRoutingContext &context() const { return context_; }
54
55 virtual std::string get_destination_id() const = 0;
56
57 /**
58 * @brief Returns address of server to which connection is established.
59 *
60 * @return address of server
61 */
62 virtual std::string get_server_address() const = 0;
63
64 virtual void disconnect() = 0;
65
66 /**
67 * @brief Returns address of client which connected to router
68 *
69 * @return address of client
70 */
71 virtual std::string get_client_address() const = 0;
72
73 std::size_t get_bytes_up() const {
74 return stats_([](const Stats &stats) { return stats.bytes_up; });
75 }
76
77 std::size_t get_bytes_down() const {
78 return stats_([](const Stats &stats) { return stats.bytes_down; });
79 }
80
81 using clock_type = std::chrono::system_clock;
82 using time_point_type = clock_type::time_point;
83
85 return stats_([](const Stats &stats) { return stats.started; });
86 }
87
89 return stats_([](const Stats &stats) { return stats.connected_to_server; });
90 }
91
93 return stats_([](const Stats &stats) { return stats.last_sent_to_server; });
94 }
95
97 return stats_(
98 [](const Stats &stats) { return stats.last_received_from_server; });
99 }
100
101 struct Stats {
102 std::size_t bytes_up{0};
103 std::size_t bytes_down{0};
104
105 time_point_type started{clock_type::now()};
109 };
110
111 Stats get_stats() const {
112 return stats_([](const Stats &stats) { return stats; });
113 }
114
115 void transfered_to_server(size_t bytes) {
116 const auto now = clock_type::now();
117 stats_([bytes, now](Stats &stats) {
118 stats.last_sent_to_server = now;
119 stats.bytes_down += bytes;
120 });
121 }
122
123 void transfered_to_client(size_t bytes) {
124 const auto now = clock_type::now();
125 stats_([bytes, now](Stats &stats) {
126 stats.last_received_from_server = now;
127 stats.bytes_up += bytes;
128 });
129 }
130
132
133 void accepted();
134
135 virtual void connected();
136
137 template <class F>
138 auto disconnect_request(F &&f) {
139 return disconnect_(std::forward<F>(f));
140 }
141
142 bool disconnect_requested() const {
143 return disconnect_([](auto requested) { return requested; });
144 }
145
146 protected:
147 /** @brief wrapper for common data used by all routing threads */
149 /** @brief callback that is called when thread of execution completes */
151
153
155};
156
158 public:
160
161 ConnectorBase(net::io_context &io_ctx, RouteDestination *route_destination,
162 Destinations &destinations)
163 : io_ctx_{io_ctx},
164 route_destination_{route_destination},
165 destinations_{destinations},
167
168 enum class Function {
171 };
172
175
177
179
180 bool connect_timed_out() const { return connect_timed_out_; }
181
182 void destination_id(std::string id) { destination_id_ = id; }
183 std::string destination_id() const { return destination_id_; }
184
186 std::function<void(std::string, uint16_t, std::error_code)> func) {
187 on_connect_failure_ = std::move(func);
188 }
189
190 void on_connect_success(std::function<void(std::string, uint16_t)> func) {
191 on_connect_success_ = std::move(func);
192 }
193
194 void on_is_destination_good(std::function<bool(std::string, uint16_t)> func) {
195 on_is_destination_good_ = std::move(func);
196 }
197
198 bool is_destination_good(const std::string &hostname, uint16_t port) const {
200
201 return true;
202 }
203
204 protected:
215
217
221
227
229
231
233
235 std::string destination_id_;
236
237 std::function<void(std::string, uint16_t, std::error_code)>
239 std::function<void(std::string, uint16_t)> on_connect_success_;
240 std::function<bool(std::string, uint16_t)> on_is_destination_good_;
241};
242
243template <class ConnectionType>
244class Connector : public ConnectorBase {
245 public:
247
249 switch (func_) {
251 auto init_res = init_destination();
252 if (!init_res) return init_res.get_unexpected();
253
254 } break;
256 auto connect_res = connect_finish();
257 if (!connect_res) return connect_res.get_unexpected();
258
259 } break;
260 }
261
262 if (destination_id().empty()) {
263 // stops at 'connect_init()
264 {
265 auto connect_res = try_connect();
266 if (!connect_res) return connect_res.get_unexpected();
267 }
268 }
269
270 return {std::in_place, std::make_unique<TcpConnection>(
271 std::move(socket()), std::move(endpoint()))};
272 }
273};
274
275template <class ConnectionType>
277 public:
278 using pool_lookup_cb = std::function<std::optional<ConnectionType>(
280
282 Destinations &destinations, pool_lookup_cb pool_lookup)
283 : ConnectorBase{io_ctx, route_destination, destinations},
284 pool_lookup_{std::move(pool_lookup)} {}
285
287 switch (func_) {
289 auto init_res = init_destination();
290 if (!init_res) return init_res.get_unexpected();
291
292 } break;
294 auto connect_res = connect_finish();
295 if (!connect_res) return connect_res.get_unexpected();
296
297 } break;
298 }
299
300 if (destination_id().empty()) {
301 // stops at 'connect_init()
302 if (auto pool_res = probe_pool()) {
303 // return the pooled connection.
304 return std::move(pool_res.value());
305 }
306
307 {
308 auto connect_res = try_connect();
309 if (!connect_res) return connect_res.get_unexpected();
310 }
311 }
312
313 return {std::in_place, std::make_unique<TcpConnection>(
314 std::move(socket()), std::move(endpoint()))};
315 }
316
317 private:
318 std::optional<ConnectionType> probe_pool() {
320 }
321
323};
324
325#endif /* ROUTING_CONNECTION_INCLUDED */
Definition: connection.h:157
server_protocol_type::endpoint & endpoint()
Definition: connection.h:174
void connect_timed_out(bool v)
Definition: connection.h:178
std::function< void(std::string, uint16_t)> on_connect_success_
Definition: connection.h:239
std::function< void(std::string, uint16_t, std::error_code)> on_connect_failure_
Definition: connection.h:238
net::steady_timer & timer()
Definition: connection.h:176
stdx::expected< void, std::error_code > connect_init()
Definition: connection.cc:91
stdx::expected< void, std::error_code > init_destination()
Definition: connection.cc:38
Destinations & destinations_
Definition: connection.h:223
std::error_code last_ec_
Definition: connection.h:228
net::steady_timer connect_timer_
Definition: connection.h:232
stdx::expected< void, std::error_code > resolve()
Definition: connection.cc:55
server_protocol_type::endpoint server_endpoint_
Definition: connection.h:220
Function
Definition: connection.h:168
stdx::expected< void, std::error_code > init_endpoint()
Definition: connection.cc:85
std::string destination_id() const
Definition: connection.h:183
stdx::expected< void, std::error_code > connected()
Definition: connection.cc:219
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:161
void destination_id(std::string id)
Definition: connection.h:182
bool connect_timed_out_
Definition: connection.h:234
void on_connect_success(std::function< void(std::string, uint16_t)> func)
Definition: connection.h:190
Destinations::iterator destinations_it_
Definition: connection.h:224
net::ip::tcp::resolver::results_type endpoints_
Definition: connection.h:225
bool connect_timed_out() const
Definition: connection.h:180
std::function< bool(std::string, uint16_t)> on_is_destination_good_
Definition: connection.h:240
Function func_
Definition: connection.h:230
server_protocol_type::socket & socket()
Definition: connection.h:173
net::ip::tcp::resolver resolver_
Definition: connection.h:218
bool is_destination_good(const std::string &hostname, uint16_t port) const
Definition: connection.h:198
void on_is_destination_good(std::function< bool(std::string, uint16_t)> func)
Definition: connection.h:194
stdx::expected< void, std::error_code > next_destination()
Definition: connection.cc:251
net::io_context & io_ctx_
Definition: connection.h:216
stdx::expected< void, std::error_code > try_connect()
Definition: connection.cc:104
std::string destination_id_
Definition: connection.h:235
net::ip::tcp::resolver::results_type::iterator endpoints_it_
Definition: connection.h:226
stdx::expected< void, std::error_code > connect_finish()
Definition: connection.cc:186
server_protocol_type::socket server_sock_
Definition: connection.h:219
stdx::expected< void, std::error_code > next_endpoint()
Definition: connection.cc:231
void on_connect_failure(std::function< void(std::string, uint16_t, std::error_code)> func)
Definition: connection.h:185
RouteDestination * route_destination_
Definition: connection.h:222
Definition: connection.h:244
stdx::expected< ConnectionType, std::error_code > connect()
Definition: connection.h:248
A forward iterable container of destinations.
Definition: destination.h:97
typename container_type::iterator iterator
Definition: destination.h:101
Monitor pattern.
Definition: monitor.h:39
Definition: connection.h:43
virtual std::string get_destination_id() const =0
void accepted()
Definition: connection.cc:278
MySQLRoutingConnectionBase(MySQLRoutingContext &context, std::function< void(MySQLRoutingConnectionBase *)> remove_callback)
Definition: connection.h:45
clock_type::time_point time_point_type
Definition: connection.h:82
virtual void connected()
Definition: connection.cc:283
const MySQLRoutingContext & context() const
Definition: connection.h:53
time_point_type get_started() const
Definition: connection.h:84
Stats get_stats() const
Definition: connection.h:111
std::size_t get_bytes_down() const
Definition: connection.h:77
std::chrono::system_clock clock_type
Definition: connection.h:81
void transfered_to_server(size_t bytes)
Definition: connection.h:115
std::size_t get_bytes_up() const
Definition: connection.h:73
Monitor< bool > disconnect_
Definition: connection.h:154
void disassociate()
Definition: connection.h:131
Monitor< Stats > stats_
Definition: connection.h:152
virtual void disconnect()=0
time_point_type get_connected_to_server() const
Definition: connection.h:88
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:150
virtual std::string get_client_address() const =0
Returns address of client which connected to router.
virtual ~MySQLRoutingConnectionBase()=default
MySQLRoutingContext & context()
Definition: connection.h:52
void transfered_to_client(size_t bytes)
Definition: connection.h:123
time_point_type get_last_received_from_server() const
Definition: connection.h:96
time_point_type get_last_sent_to_server() const
Definition: connection.h:92
auto disconnect_request(F &&f)
Definition: connection.h:138
bool disconnect_requested() const
Definition: connection.h:142
MySQLRoutingContext & context_
wrapper for common data used by all routing threads
Definition: connection.h:148
MySQLRoutingContext holds data used by MySQLRouting (1 per plugin instances) and MySQLRoutingConnecti...
Definition: context.h:59
Definition: connection.h:276
stdx::expected< ConnectionType, std::error_code > connect()
Definition: connection.h:286
pool_lookup_cb pool_lookup_
Definition: connection.h:322
std::optional< ConnectionType > probe_pool()
Definition: connection.h:318
PooledConnector(net::io_context &io_ctx, RouteDestination *route_destination, Destinations &destinations, pool_lookup_cb pool_lookup)
Definition: connection.h:281
std::function< std::optional< ConnectionType >(const server_protocol_type::endpoint &ep)> pool_lookup_cb
Definition: connection.h:279
Manage destinations for a Connection Routing.
Definition: destination.h:188
Definition: socket.h:1144
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:944
struct stats stats
Definition: mysqlslap.cc:236
bool empty(const Histogram &histogram)
Return true if 'histogram' was built on an empty table.
Definition: histogram.h:672
std::error_code make_error_code(DynamicLoaderErrc ec)
make error_code from a DynamicLoaderErrc.
Definition: dynamic_loader.cc:79
Definition: gcs_xcom_synode.h:64
required uint64 port
Definition: replication_asynchronous_connection_failover.proto:33
Definition: connection.h:101
time_point_type started
Definition: connection.h:105
time_point_type last_sent_to_server
Definition: connection.h:107
time_point_type connected_to_server
Definition: connection.h:106
std::size_t bytes_down
Definition: connection.h:103
time_point_type last_received_from_server
Definition: connection.h:108
std::size_t bytes_up
Definition: connection.h:102
Definition: mysqlslap.cc:238
unsigned long id[MAX_DEAD]
Definition: xcom_base.cc:510