MySQL 9.2.0
Source Code Documentation
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
context.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_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 TlsServerContext *client_ssl_ctx, DestinationTlsContext *dest_tls_context,
59 std::shared_ptr<routing_guidelines::Routing_guidelines_engine>
61 : routing_config_(routing_config),
62 name_(std::move(name)),
63 client_ssl_ctx_{client_ssl_ctx},
64 destination_tls_context_{dest_tls_context},
69 }
70
73 return blocked_endpoints_;
74 }
75
79
82 uint64_t get_max_connect_errors() const {
84 }
85
87
88 const std::string &get_name() const { return name_; }
89
90 /**
91 * identifier part of the name.
92 *
93 * name has the form 'routing:{id}'
94 */
95 std::string get_id() const {
96 auto pos = name_.find(':');
97
98 if (pos == name_.npos) return {};
99
100 return name_.substr(pos + 1);
101 }
102
103 unsigned int get_net_buffer_length() const {
105 }
106
107 std::chrono::milliseconds get_destination_connect_timeout() const {
108 return std::chrono::milliseconds{routing_config_.connect_timeout * 1000};
109 }
110
111 std::chrono::milliseconds get_client_connect_timeout() const {
112 return std::chrono::milliseconds{routing_config_.client_connect_timeout *
113 1000};
114 }
115
116 std::chrono::milliseconds connect_retry_timeout() const {
118 }
119
122 }
123
126 }
127
128 SslMode source_ssl_mode() const noexcept {
130 }
131 SslMode dest_ssl_mode() const noexcept {
133 }
134
135 /**
136 * get the SSL context for the client side of the route.
137 */
139
140 /**
141 * get the SSL context for the server side of the route.
142 *
143 * @param dest_id unique id of the destination
144 * @param hostname name of the destination host
145 *
146 * @returns a TlsClientContext for the destination.
147 * @retval nullptr if creating tls-context failed.
148 */
149 TlsClientContext *dest_ssl_ctx(const std::string &dest_id,
150 const std::string &hostname) {
151 if (destination_tls_context_ == nullptr) return nullptr;
152
153 return destination_tls_context_->get(dest_id, hostname);
154 }
155
158 }
159
162 }
163
165
166 void connection_sharing(const std::optional<bool> &is_enabled) {
167 connection_sharing_ = is_enabled.has_value()
168 ? *is_enabled
170 }
171
172 std::chrono::milliseconds connection_sharing_delay() const {
174 }
175
178 }
179
181
184 }
185
186 std::string dest_ssl_key() const { return routing_config_.dest_ssl_key; }
187 std::string dest_ssl_cert() const { return routing_config_.dest_ssl_cert; }
188
191 }
192
193 std::shared_ptr<routing_guidelines::Routing_guidelines_engine>
195 return routing_guidelines_;
196 }
197
199 std::lock_guard<std::mutex> l{router_info_mtx_};
200 router_info_ = std::move(router_info);
203 }
204
206 std::lock_guard<std::mutex> l{router_info_mtx_};
207 return router_info_;
208 }
209
210 private:
212
213 /** @brief Descriptive name of the connection routing */
214 const std::string name_;
215
216 mutable std::mutex mutex_conn_errors_;
217
218 /** @brief memory in kilobytes allocated for thread's stack */
220
222
224
225 /**
226 * Routing guidelines engine used for the routing.
227 */
228 std::shared_ptr<routing_guidelines::Routing_guidelines_engine>
230
231 mutable std::mutex router_info_mtx_;
233
234 // Connection sharing could be configured in routing plugin config, but it
235 // could be overwritten by routing guidelines
236 std::atomic<bool> connection_sharing_;
237
238 /**
239 * Callbacks for communicating with quarantined destination candidates
240 * instance.
241 */
243
245
246 public:
247 /** @brief Number of active routes */
248 std::atomic<uint16_t> info_active_routes_{0};
249 /** @brief Number of handled routes, not used at the moment */
250 std::atomic<uint64_t> info_handled_routes_{0};
251};
252#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
TlsClientContext * get(const std::string &dest_id, const std::string &hostname)
get a TlsClientContent for a destination.
Definition: destination_ssl_context.cc:80
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:88
std::string dest_ssl_key() const
Definition: context.h:186
const SharedQuarantineHandler & shared_quarantine() const
Definition: context.h:160
std::string get_id() const
identifier part of the name.
Definition: context.h:95
bool router_require_enforce() const
Definition: context.h:189
void connection_sharing(const std::optional< bool > &is_enabled)
Definition: context.h:166
std::chrono::milliseconds get_client_connect_timeout() const
Definition: context.h:111
std::atomic< uint64_t > info_handled_routes_
Number of handled routes, not used at the moment.
Definition: context.h:250
std::mutex router_info_mtx_
Definition: context.h:231
const mysql_harness::Path & get_bind_named_socket() const
Definition: context.h:124
const BlockedEndpoints & blocked_endpoints() const
Definition: context.h:72
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:149
routing_guidelines::Router_info get_router_info() const
Definition: context.h:205
size_t thread_stack_size_
memory in kilobytes allocated for thread's stack
Definition: context.h:219
uint64_t get_handled_routes()
Definition: context.h:81
void increase_info_handled_routes()
Definition: context.cc:36
SharedQuarantineHandler shared_quarantine_handler_
Callbacks for communicating with quarantined destination candidates instance.
Definition: context.h:242
SharedQuarantineHandler & shared_quarantine()
Definition: context.h:156
std::atomic< bool > connection_sharing_
Definition: context.h:236
bool connection_sharing() const
Definition: context.h:164
routing_guidelines::Router_info router_info_
Definition: context.h:232
unsigned int get_net_buffer_length() const
Definition: context.h:103
routing::AccessMode access_mode() const
Definition: context.h:176
uint64_t get_max_connect_errors() const
Definition: context.h:82
std::atomic< uint16_t > info_active_routes_
Number of active routes.
Definition: context.h:248
BaseProtocol::Type get_protocol() const
Definition: context.h:86
std::shared_ptr< routing_guidelines::Routing_guidelines_engine > get_routing_guidelines() const
Definition: context.h:194
BlockedEndpoints blocked_endpoints_
Definition: context.h:244
const mysql_harness::TcpDestination & get_bind_address() const
Definition: context.h:120
std::chrono::seconds wait_for_my_writes_timeout() const
Definition: context.h:182
SslMode source_ssl_mode() const noexcept
Definition: context.h:128
std::chrono::milliseconds get_destination_connect_timeout() const
Definition: context.h:107
BlockedEndpoints & blocked_endpoints()
Definition: context.h:71
TlsServerContext * source_ssl_ctx() const
get the SSL context for the client side of the route.
Definition: context.h:138
DestinationTlsContext * destination_tls_context_
Definition: context.h:223
std::chrono::milliseconds connect_retry_timeout() const
Definition: context.h:116
std::mutex mutex_conn_errors_
Definition: context.h:216
const RoutingConfig routing_config_
Definition: context.h:211
std::string dest_ssl_cert() const
Definition: context.h:187
void increase_info_active_routes()
Definition: context.cc:28
void set_router_info(routing_guidelines::Router_info router_info)
Definition: context.h:198
MySQLRoutingContext(const RoutingConfig &routing_config, std::string name, TlsServerContext *client_ssl_ctx, DestinationTlsContext *dest_tls_context, std::shared_ptr< routing_guidelines::Routing_guidelines_engine > routing_guidelines)
Definition: context.h:56
TlsServerContext * client_ssl_ctx_
Definition: context.h:221
SslMode dest_ssl_mode() const noexcept
Definition: context.h:131
std::shared_ptr< routing_guidelines::Routing_guidelines_engine > routing_guidelines_
Routing guidelines engine used for the routing.
Definition: context.h:229
std::chrono::milliseconds connection_sharing_delay() const
Definition: context.h:172
uint16_t get_active_routes()
Definition: context.h:80
bool wait_for_my_writes() const
Definition: context.h:180
void decrease_info_active_routes()
Definition: context.cc:32
const std::string name_
Descriptive name of the connection routing.
Definition: context.h:214
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:106
mysql_harness::Path named_socket
unix domain socket path to bind to
Definition: routing_config.h:46
bool wait_for_my_writes
Definition: routing_config.h:104
bool router_require_enforce
Definition: routing_config.h:116
SslMode dest_ssl_mode
SslMode of the server side connection.
Definition: routing_config.h:70
mysql_harness::TcpDestination bind_address
IP address to bind to.
Definition: routing_config.h:45
unsigned int client_connect_timeout
client connect timeout in seconds
Definition: routing_config.h:52
SslMode source_ssl_mode
SslMode of the client side connection.
Definition: routing_config.h:56
int connect_timeout
connect-timeout in seconds
Definition: routing_config.h:47
Protocol::Type protocol
protocol (classic, x)
Definition: routing_config.h:42
std::string dest_ssl_cert
Cert file.
Definition: routing_config.h:71
routing::AccessMode access_mode
read_write,read_only,auto
Definition: routing_config.h:101
unsigned int net_buffer_length
Size of buffer to receive packets.
Definition: routing_config.h:53
bool connection_sharing
if connection sharing is allowed.
Definition: routing_config.h:84
std::chrono::milliseconds connect_retry_timeout
timeout of retrying after a transient connect-failure.
Definition: routing_config.h:99
std::string dest_ssl_key
Key file.
Definition: routing_config.h:72
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:86
Definition: shared_quarantine_handler.h:35
Client TLS Context.
Definition: tls_client_context.h:43
TLS Context for the server side.
Definition: tls_server_context.h:51
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:1394
bool is_enabled()
Check if doublewrite is enabled.
Definition: buf0dblwr.h:390
static const size_t kDefaultStackSizeInKiloBytes
Definition: mysql_router_thread.h:44
Definition: routing_guidelines_datatypes.h:30
AccessMode
Definition: routing.h:259
Definition: gcs_xcom_synode.h:64
SslMode
Definition: ssl_mode.h:29
case opt name
Definition: sslopt-case.h:29
Information about this Router instance.
Definition: routing_guidelines.h:57
std::string bind_address
address on which router is listening
Definition: routing_guidelines.h:69
std::string route_name
name of the plugin which handles the connection
Definition: routing_guidelines.h:72
double seconds()
Definition: task.cc:314