MySQL 26.10.0
Source Code Documentation
context.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2018, 2026, 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_CONTEXT_INCLUDED
27#define ROUTING_CONTEXT_INCLUDED
28
29#include <atomic>
30#include <chrono>
31#include <memory>
32#include <mutex>
33#include <string>
34
35#include "blocked_endpoints.h"
37#include "mysql/harness/filesystem.h" // Path
39#include "mysql_router_thread.h"
43#include "mysqlrouter/routing.h"
45#include "routing_config.h"
47
48/**
49 * @brief MySQLRoutingContext holds data used by MySQLRouting (1 per plugin
50 * instances) and MySQLRoutingConnection instances (many instances). It is
51 * created and owned by MySQLRouting while MySQLRoutingConnection objects hold
52 * reference to it.
53 */
55 public:
57 const RoutingConfig &routing_config, std::string name,
58 std::shared_ptr<TlsServerContext> client_ssl_ctx,
59 DestinationTlsContext *dest_tls_context,
60 std::shared_ptr<routing_guidelines::Routing_guidelines_engine>
62 : routing_config_(routing_config),
63 name_(std::move(name)),
64 client_ssl_ctx_{std::move(client_ssl_ctx)},
65 destination_tls_context_{dest_tls_context},
70 }
71
74 return blocked_endpoints_;
75 }
76
80
83 uint64_t get_max_connect_errors() const {
85 }
86
88
89 const std::string &get_name() const { return name_; }
90
91 /**
92 * identifier part of the name.
93 *
94 * name has the form 'routing:{id}'
95 */
96 std::string get_id() const {
97 auto pos = name_.find(':');
98
99 if (pos == name_.npos) return {};
100
101 return name_.substr(pos + 1);
102 }
103
104 unsigned int get_net_buffer_length() const {
106 }
107
110 }
111
114 1000};
115 }
116
119 }
120
123 }
124
127 }
128
131 }
134 }
135
136 /**
137 * get the SSL context for the client side of the route.
138 */
139 std::shared_ptr<TlsServerContext> source_ssl_ctx() const {
140 std::lock_guard<std::mutex> lock{source_ssl_contexts_mtx_};
141 return client_ssl_ctx_;
142 }
143
144 void replace_source_ssl_ctx(std::shared_ptr<TlsServerContext> tls_context) {
145 std::lock_guard<std::mutex> lock{source_ssl_contexts_mtx_};
146 client_ssl_ctx_ = std::move(tls_context);
147 }
148
151 }
152
153 /**
154 * get the SSL context for the server side of the route.
155 *
156 * @param dest_id unique id of the destination
157 * @param hostname name of the destination host
158 *
159 * @returns a TlsClientContext for the destination.
160 * @retval nullptr if creating tls-context failed.
161 */
162 std::shared_ptr<TlsClientContext> dest_ssl_ctx(const std::string &dest_id,
163 const std::string &hostname) {
164 if (destination_tls_context_ == nullptr) return {};
165
166 return destination_tls_context_->get(dest_id, hostname);
167 }
168
171 }
172
175 }
176
178
179 void connection_sharing(const std::optional<bool> &is_enabled) {
180 connection_sharing_ = is_enabled.has_value()
181 ? *is_enabled
183 }
184
187 }
188
191 }
192
194
197 }
198
199 std::string dest_ssl_key() const { return routing_config_.dest_ssl_key; }
200 std::string dest_ssl_cert() const { return routing_config_.dest_ssl_cert; }
201
204 }
205
206 std::shared_ptr<routing_guidelines::Routing_guidelines_engine>
208 return routing_guidelines_;
209 }
210
212 std::lock_guard<std::mutex> l{router_info_mtx_};
213 router_info_ = std::move(router_info);
216 }
217
219 std::lock_guard<std::mutex> l{router_info_mtx_};
220 return router_info_;
221 }
222
223 private:
225
226 /** @brief Descriptive name of the connection routing */
227 const std::string name_;
228
229 mutable std::mutex mutex_conn_errors_;
230
231 /** @brief memory in kilobytes allocated for thread's stack */
233
234 mutable std::mutex source_ssl_contexts_mtx_;
235 std::shared_ptr<TlsServerContext> client_ssl_ctx_;
236
238
239 /**
240 * Routing guidelines engine used for the routing.
241 */
242 std::shared_ptr<routing_guidelines::Routing_guidelines_engine>
244
245 mutable std::mutex router_info_mtx_;
247
248 // Connection sharing could be configured in routing plugin config, but it
249 // could be overwritten by routing guidelines
250 std::atomic<bool> connection_sharing_;
251
252 /**
253 * Callbacks for communicating with quarantined destination candidates
254 * instance.
255 */
257
259
260 public:
261 /** @brief Number of active routes */
262 std::atomic<uint16_t> info_active_routes_{0};
263 /** @brief Number of handled routes, not used at the moment */
264 std::atomic<uint64_t> info_handled_routes_{0};
265};
266#endif /* ROUTING_CONTEXT_INCLUDED */
Type
supported protocols
Definition: base_protocol.h:32
Definition: blocked_endpoints.h:34
uint64_t max_connect_errors() const
Definition: blocked_endpoints.h:39
TlsClientContext per destination.
Definition: destination_ssl_context.h:42
std::shared_ptr< TlsClientContext > get(const std::string &dest_id, const std::string &hostname)
get a TlsClientContent for a destination.
Definition: destination_ssl_context.cc:93
MySQLRoutingContext holds data used by MySQLRouting (1 per plugin instances) and MySQLRoutingConnecti...
Definition: context.h:54
const std::string & get_name() const
Definition: context.h:89
std::string dest_ssl_key() const
Definition: context.h:199
const SharedQuarantineHandler & shared_quarantine() const
Definition: context.h:173
std::string get_id() const
identifier part of the name.
Definition: context.h:96
bool router_require_enforce() const
Definition: context.h:202
std::mutex source_ssl_contexts_mtx_
Definition: context.h:234
void connection_sharing(const std::optional< bool > &is_enabled)
Definition: context.h:179
std::chrono::milliseconds get_client_connect_timeout() const
Definition: context.h:112
std::atomic< uint64_t > info_handled_routes_
Number of handled routes, not used at the moment.
Definition: context.h:264
std::mutex router_info_mtx_
Definition: context.h:245
const mysql_harness::Path & get_bind_named_socket() const
Definition: context.h:125
const BlockedEndpoints & blocked_endpoints() const
Definition: context.h:73
routing_guidelines::Router_info get_router_info() const
Definition: context.h:218
size_t thread_stack_size_
memory in kilobytes allocated for thread's stack
Definition: context.h:232
uint64_t get_handled_routes()
Definition: context.h:82
void increase_info_handled_routes()
Definition: context.cc:36
SharedQuarantineHandler shared_quarantine_handler_
Callbacks for communicating with quarantined destination candidates instance.
Definition: context.h:256
void replace_source_ssl_ctx(std::shared_ptr< TlsServerContext > tls_context)
Definition: context.h:144
SharedQuarantineHandler & shared_quarantine()
Definition: context.h:169
std::atomic< bool > connection_sharing_
Definition: context.h:250
bool connection_sharing() const
Definition: context.h:177
routing_guidelines::Router_info router_info_
Definition: context.h:246
std::shared_ptr< TlsServerContext > client_ssl_ctx_
Definition: context.h:235
unsigned int get_net_buffer_length() const
Definition: context.h:104
routing::AccessMode access_mode() const
Definition: context.h:189
uint64_t get_max_connect_errors() const
Definition: context.h:83
std::atomic< uint16_t > info_active_routes_
Number of active routes.
Definition: context.h:262
MySQLRoutingContext(const RoutingConfig &routing_config, std::string name, std::shared_ptr< TlsServerContext > client_ssl_ctx, DestinationTlsContext *dest_tls_context, std::shared_ptr< routing_guidelines::Routing_guidelines_engine > routing_guidelines)
Definition: context.h:56
BaseProtocol::Type get_protocol() const
Definition: context.h:87
std::shared_ptr< routing_guidelines::Routing_guidelines_engine > get_routing_guidelines() const
Definition: context.h:207
BlockedEndpoints blocked_endpoints_
Definition: context.h:258
std::shared_ptr< TlsServerContext > source_ssl_ctx() const
get the SSL context for the client side of the route.
Definition: context.h:139
const mysql_harness::TcpDestination & get_bind_address() const
Definition: context.h:121
std::chrono::seconds wait_for_my_writes_timeout() const
Definition: context.h:195
SslMode source_ssl_mode() const noexcept
Definition: context.h:129
std::chrono::milliseconds get_destination_connect_timeout() const
Definition: context.h:108
BlockedEndpoints & blocked_endpoints()
Definition: context.h:72
DestinationTlsContext * destination_tls_context_
Definition: context.h:237
std::chrono::milliseconds connect_retry_timeout() const
Definition: context.h:117
std::mutex mutex_conn_errors_
Definition: context.h:229
const RoutingConfig routing_config_
Definition: context.h:224
std::string dest_ssl_cert() const
Definition: context.h:200
void increase_info_active_routes()
Definition: context.cc:28
void set_router_info(routing_guidelines::Router_info router_info)
Definition: context.h:211
std::shared_ptr< TlsClientContext > dest_ssl_ctx(const std::string &dest_id, const std::string &hostname)
get the SSL context for the server side of the route.
Definition: context.h:162
SslMode dest_ssl_mode() const noexcept
Definition: context.h:132
std::shared_ptr< routing_guidelines::Routing_guidelines_engine > routing_guidelines_
Routing guidelines engine used for the routing.
Definition: context.h:243
std::chrono::milliseconds connection_sharing_delay() const
Definition: context.h:185
uint16_t get_active_routes()
Definition: context.h:81
DestinationTlsContext * destination_ssl_config() const
Definition: context.h:149
bool wait_for_my_writes() const
Definition: context.h:193
void decrease_info_active_routes()
Definition: context.cc:32
const std::string name_
Descriptive name of the connection routing.
Definition: context.h:227
route specific configuration.
Definition: routing_config.h:40
std::chrono::seconds wait_for_my_writes_timeout
how long to wait for writes to be applied before reads.
Definition: routing_config.h:107
mysql_harness::Path named_socket
unix domain socket path to bind to
Definition: routing_config.h:47
bool wait_for_my_writes
Definition: routing_config.h:105
bool router_require_enforce
Definition: routing_config.h:117
SslMode dest_ssl_mode
SslMode of the server side connection.
Definition: routing_config.h:71
mysql_harness::TcpDestination bind_address
IP address to bind to.
Definition: routing_config.h:46
unsigned int client_connect_timeout
client connect timeout in seconds
Definition: routing_config.h:53
SslMode source_ssl_mode
SslMode of the client side connection.
Definition: routing_config.h:57
int connect_timeout
connect-timeout in seconds
Definition: routing_config.h:48
Protocol::Type protocol
protocol (classic, x)
Definition: routing_config.h:42
std::string dest_ssl_cert
Cert file.
Definition: routing_config.h:72
routing::AccessMode access_mode
read_write,read_only,auto
Definition: routing_config.h:102
unsigned int net_buffer_length
Size of buffer to receive packets.
Definition: routing_config.h:54
bool connection_sharing
if connection sharing is allowed.
Definition: routing_config.h:85
std::chrono::milliseconds connect_retry_timeout
timeout of retrying after a transient connect-failure.
Definition: routing_config.h:100
std::string dest_ssl_key
Key file.
Definition: routing_config.h:73
std::chrono::milliseconds connection_sharing_delay
delay before an idling connection is moved to the pool and connection sharing is allowed.
Definition: routing_config.h:87
Definition: shared_quarantine_handler.h:35
Class representing a path in a file system.
Definition: filesystem.h:63
Definition: destination.h:40
const std::string & hostname() const
Definition: destination.h:57
ulong max_connect_errors
Definition: mysqld.cc:1413
bool is_enabled()
Check if doublewrite is enabled.
Definition: buf0dblwr.h:391
Provides atomic access in shared-exclusive modes.
Definition: shared_spin_lock.h:79
std::chrono::seconds seconds
Definition: authorize_manager.cc:71
std::chrono::milliseconds milliseconds
Definition: authorize_manager.cc:70
noexcept
The return type for any call_and_catch(f, args...) call where f(args...) returns Type.
Definition: call_and_catch.h:76
static const size_t kDefaultStackSizeInKiloBytes
Definition: mysql_router_thread.h:44
Definition: routing_guidelines_datatypes.h:31
AccessMode
Definition: routing.h:265
Define std::hash<Gtid>.
Definition: gtid.h:355
SslMode
Definition: ssl_mode.h:29
case opt name
Definition: sslopt-case.h:29
Information about this Router instance.
Definition: routing_guidelines.h:59
std::string bind_address
address on which router is listening
Definition: routing_guidelines.h:71
std::string route_name
name of the plugin which handles the connection
Definition: routing_guidelines.h:74