MySQL 8.0.41
Source Code Documentation
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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 void server_address(const std::string &dest) {
58 stats_([&dest](Stats &stats) { stats.server_address = dest; });
59 }
60
61 virtual void disconnect() = 0;
62
63 void client_address(const std::string &dest) {
64 stats_([&dest](Stats &stats) { stats.client_address = dest; });
65 }
66
67 std::size_t get_bytes_up() const {
68 return stats_([](const Stats &stats) { return stats.bytes_up; });
69 }
70
71 std::size_t get_bytes_down() const {
72 return stats_([](const Stats &stats) { return stats.bytes_down; });
73 }
74
75 using clock_type = std::chrono::system_clock;
76 using time_point_type = clock_type::time_point;
77
79 return stats_([](const Stats &stats) { return stats.started; });
80 }
81
83 return stats_([](const Stats &stats) { return stats.connected_to_server; });
84 }
85
87 return stats_([](const Stats &stats) { return stats.last_sent_to_server; });
88 }
89
91 return stats_(
92 [](const Stats &stats) { return stats.last_received_from_server; });
93 }
94
95 struct Stats {
96 std::string client_address;
97 std::string server_address;
98
99 std::size_t bytes_up{0};
100 std::size_t bytes_down{0};
101
102 time_point_type started{clock_type::now()};
106 };
107
108 Stats get_stats() const {
109 return stats_([](const Stats &stats) { return stats; });
110 }
111
112 void transfered_to_server(size_t bytes) {
113 const auto now = clock_type::now();
114 stats_([bytes, now](Stats &stats) {
115 stats.last_sent_to_server = now;
116 stats.bytes_down += bytes;
117 });
118 }
119
120 void transfered_to_client(size_t bytes) {
121 const auto now = clock_type::now();
122 stats_([bytes, now](Stats &stats) {
123 stats.last_received_from_server = now;
124 stats.bytes_up += bytes;
125 });
126 }
127
129
130 void accepted();
131
132 virtual void connected();
133
134 template <class F>
135 auto disconnect_request(F &&f) {
136 return disconnect_(std::forward<F>(f));
137 }
138
139 bool disconnect_requested() const {
140 return disconnect_([](auto requested) { return requested; });
141 }
142
143 protected:
144 /** @brief wrapper for common data used by all routing threads */
146 /** @brief callback that is called when thread of execution completes */
148
150
152};
153
155 public:
157
158 ConnectorBase(net::io_context &io_ctx, RouteDestination *route_destination,
159 Destinations &destinations)
160 : io_ctx_{io_ctx},
161 route_destination_{route_destination},
162 destinations_{destinations},
164
165 enum class Function {
168 };
169
172
174
176
177 bool connect_timed_out() const { return connect_timed_out_; }
178
179 void destination_id(std::string id) { destination_id_ = id; }
180 std::string destination_id() const { return destination_id_; }
181
183 std::function<void(std::string, uint16_t, std::error_code)> func) {
184 on_connect_failure_ = std::move(func);
185 }
186
187 void on_connect_success(std::function<void(std::string, uint16_t)> func) {
188 on_connect_success_ = std::move(func);
189 }
190
191 void on_is_destination_good(std::function<bool(std::string, uint16_t)> func) {
192 on_is_destination_good_ = std::move(func);
193 }
194
195 bool is_destination_good(const std::string &hostname, uint16_t port) const {
197
198 return true;
199 }
200
201 protected:
212
214
218
224
226
228
230
232 std::string destination_id_;
233
234 std::function<void(std::string, uint16_t, std::error_code)>
236 std::function<void(std::string, uint16_t)> on_connect_success_;
237 std::function<bool(std::string, uint16_t)> on_is_destination_good_;
238};
239
240template <class ConnectionType>
241class Connector : public ConnectorBase {
242 public:
244
246 switch (func_) {
248 auto init_res = init_destination();
249 if (!init_res) return init_res.get_unexpected();
250
251 } break;
253 auto connect_res = connect_finish();
254 if (!connect_res) return connect_res.get_unexpected();
255
256 } break;
257 }
258
259 if (destination_id().empty()) {
260 // stops at 'connect_init()
261 {
262 auto connect_res = try_connect();
263 if (!connect_res) return connect_res.get_unexpected();
264 }
265 }
266
267 return {std::in_place, std::make_unique<TcpConnection>(
268 std::move(socket()), std::move(endpoint()))};
269 }
270};
271
272template <class ConnectionType>
274 public:
275 using pool_lookup_cb = std::function<std::optional<ConnectionType>(
277
279 Destinations &destinations, pool_lookup_cb pool_lookup)
280 : ConnectorBase{io_ctx, route_destination, destinations},
281 pool_lookup_{std::move(pool_lookup)} {}
282
284 switch (func_) {
286 auto init_res = init_destination();
287 if (!init_res) return init_res.get_unexpected();
288
289 } break;
291 auto connect_res = connect_finish();
292 if (!connect_res) return connect_res.get_unexpected();
293
294 } break;
295 }
296
297 if (destination_id().empty()) {
298 // stops at 'connect_init()
299 if (auto pool_res = probe_pool()) {
300 // return the pooled connection.
301 return std::move(pool_res.value());
302 }
303
304 {
305 auto connect_res = try_connect();
306 if (!connect_res) return connect_res.get_unexpected();
307 }
308 }
309
310 return {std::in_place, std::make_unique<TcpConnection>(
311 std::move(socket()), std::move(endpoint()))};
312 }
313
314 private:
315 std::optional<ConnectionType> probe_pool() {
317 }
318
320};
321
322#endif /* ROUTING_CONNECTION_INCLUDED */
Definition: connection.h:154
server_protocol_type::endpoint & endpoint()
Definition: connection.h:171
void connect_timed_out(bool v)
Definition: connection.h:175
std::function< void(std::string, uint16_t)> on_connect_success_
Definition: connection.h:236
std::function< void(std::string, uint16_t, std::error_code)> on_connect_failure_
Definition: connection.h:235
net::steady_timer & timer()
Definition: connection.h:173
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:220
std::error_code last_ec_
Definition: connection.h:225
net::steady_timer connect_timer_
Definition: connection.h:229
stdx::expected< void, std::error_code > resolve()
Definition: connection.cc:55
server_protocol_type::endpoint server_endpoint_
Definition: connection.h:217
Function
Definition: connection.h:165
stdx::expected< void, std::error_code > init_endpoint()
Definition: connection.cc:85
std::string destination_id() const
Definition: connection.h:180
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:158
void destination_id(std::string id)
Definition: connection.h:179
bool connect_timed_out_
Definition: connection.h:231
void on_connect_success(std::function< void(std::string, uint16_t)> func)
Definition: connection.h:187
Destinations::iterator destinations_it_
Definition: connection.h:221
net::ip::tcp::resolver::results_type endpoints_
Definition: connection.h:222
bool connect_timed_out() const
Definition: connection.h:177
std::function< bool(std::string, uint16_t)> on_is_destination_good_
Definition: connection.h:237
Function func_
Definition: connection.h:227
server_protocol_type::socket & socket()
Definition: connection.h:170
net::ip::tcp::resolver resolver_
Definition: connection.h:215
bool is_destination_good(const std::string &hostname, uint16_t port) const
Definition: connection.h:195
void on_is_destination_good(std::function< bool(std::string, uint16_t)> func)
Definition: connection.h:191
stdx::expected< void, std::error_code > next_destination()
Definition: connection.cc:251
net::io_context & io_ctx_
Definition: connection.h:213
stdx::expected< void, std::error_code > try_connect()
Definition: connection.cc:104
std::string destination_id_
Definition: connection.h:232
net::ip::tcp::resolver::results_type::iterator endpoints_it_
Definition: connection.h:223
stdx::expected< void, std::error_code > connect_finish()
Definition: connection.cc:186
server_protocol_type::socket server_sock_
Definition: connection.h:216
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:182
RouteDestination * route_destination_
Definition: connection.h:219
Definition: connection.h:241
stdx::expected< ConnectionType, std::error_code > connect()
Definition: connection.h:245
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:76
virtual void connected()
Definition: connection.cc:283
const MySQLRoutingContext & context() const
Definition: connection.h:53
time_point_type get_started() const
Definition: connection.h:78
Stats get_stats() const
Definition: connection.h:108
std::size_t get_bytes_down() const
Definition: connection.h:71
std::chrono::system_clock clock_type
Definition: connection.h:75
void transfered_to_server(size_t bytes)
Definition: connection.h:112
std::size_t get_bytes_up() const
Definition: connection.h:67
Monitor< bool > disconnect_
Definition: connection.h:151
void disassociate()
Definition: connection.h:128
Monitor< Stats > stats_
Definition: connection.h:149
virtual void disconnect()=0
time_point_type get_connected_to_server() const
Definition: connection.h:82
std::function< void(MySQLRoutingConnectionBase *)> remove_callback_
callback that is called when thread of execution completes
Definition: connection.h:147
virtual ~MySQLRoutingConnectionBase()=default
MySQLRoutingContext & context()
Definition: connection.h:52
void client_address(const std::string &dest)
Definition: connection.h:63
void transfered_to_client(size_t bytes)
Definition: connection.h:120
time_point_type get_last_received_from_server() const
Definition: connection.h:90
time_point_type get_last_sent_to_server() const
Definition: connection.h:86
auto disconnect_request(F &&f)
Definition: connection.h:135
void server_address(const std::string &dest)
Definition: connection.h:57
bool disconnect_requested() const
Definition: connection.h:139
MySQLRoutingContext & context_
wrapper for common data used by all routing threads
Definition: connection.h:145
MySQLRoutingContext holds data used by MySQLRouting (1 per plugin instances) and MySQLRoutingConnecti...
Definition: context.h:59
Definition: connection.h:273
stdx::expected< ConnectionType, std::error_code > connect()
Definition: connection.h:283
pool_lookup_cb pool_lookup_
Definition: connection.h:319
std::optional< ConnectionType > probe_pool()
Definition: connection.h:315
PooledConnector(net::io_context &io_ctx, RouteDestination *route_destination, Destinations &destinations, pool_lookup_cb pool_lookup)
Definition: connection.h:278
std::function< std::optional< ConnectionType >(const server_protocol_type::endpoint &ep)> pool_lookup_cb
Definition: connection.h:276
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:95
time_point_type started
Definition: connection.h:102
time_point_type last_sent_to_server
Definition: connection.h:104
time_point_type connected_to_server
Definition: connection.h:103
std::size_t bytes_down
Definition: connection.h:100
std::string client_address
Definition: connection.h:96
std::string server_address
Definition: connection.h:97
time_point_type last_received_from_server
Definition: connection.h:105
std::size_t bytes_up
Definition: connection.h:99
Definition: mysqlslap.cc:238
unsigned long id[MAX_DEAD]
Definition: xcom_base.cc:510