MySQL 9.1.0
Source Code Documentation
destination.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2015, 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_DESTINATION_INCLUDED
27#define ROUTING_DESTINATION_INCLUDED
28
29#include <atomic>
30#include <cstdint>
31#include <list>
32#include <mutex>
33#include <optional>
34#include <string>
35#include <system_error>
36#include <vector>
37
38#include "my_compiler.h" // MY_ATTRIBUTE
43#include "mysqlrouter/routing.h"
44#include "protocol/protocol.h"
45#include "tcp_address.h"
46
47namespace mysql_harness {
48class PluginFuncEnv;
49}
50
51// first argument is the new set of the allowed nodes
52// second argument is a set of nodes that can be used for new connections
53// third argument is an indication whether we should disconnect existing
54// connections (based on disconnect_on_metadata_unavailable setting)
55// fourth argument is the description of the condition that triggered the change
56// (like 'metadata change' etc.) can be used for logging purposes by the caller
58 std::function<void(const AllowedNodes &, const AllowedNodes &, const bool,
59 const std::string &)>;
60// NOTE: this has to be container like std::list that does not invalidate
61// iterators when it is modified as we return the iterator to the inserted
62// callback to the caller to allow unregistering
63using AllowedNodesChangeCallbacksList = std::list<AllowedNodesChangedCallback>;
65 AllowedNodesChangeCallbacksList::iterator;
66// Starting a socket acceptor returns a value indicating if the start succeeded.
68 std::function<stdx::expected<void, std::string>()>;
69using StopSocketAcceptorCallback = std::function<void()>;
70// First callback argument informs if the instances returned from the metadata
71// has changed. Second argument is a list of new instances available after
72// md refresh.
74 std::function<void(const bool, const AllowedNodes &)>;
75// Callback argument is a destination we want to check, value returned is
76// true if the destination is quarantined, false otherwise.
78 std::function<bool(const mysql_harness::TCPAddress &)>;
79
80/** @class DestinationNodesStateNotifier
81 *
82 * Allows the obervers to register for notifications on the change in the state
83 * of the destination nodes.
84 */
86 public:
87 /** @brief Registers the callback for notification on the change in the
88 * state if the destination nodes.
89 *
90 * @param clb callback that should be called
91 * @return identifier of the inserted callback, can be used to unregister
92 * the callback
93 */
97
98 /** @brief Unregisters the callback registered with
99 * register_allowed_nodes_change_callback().
100 *
101 * @param it iterator returned by the call to
102 * register_allowed_nodes_change_callback()
103 */
106
107 /**
108 * Registers the callback for notification that the routing socket acceptor
109 * should accept new connections.
110 *
111 * @param clb callback that should be called
112 */
114 const StartSocketAcceptorCallback &clb);
115
116 /**
117 * Unregisters the callback registered with
118 * register_start_router_socket_acceptor().
119 */
121
122 /**
123 * Registers the callback for notification that the routing socket acceptor
124 * should stop accepting new connections.
125 *
126 * @param clb callback that should be called
127 */
129 const StopSocketAcceptorCallback &clb);
130
131 /**
132 * Unregisters the callback registered with
133 * register_stop_router_socket_acceptor().
134 */
136
137 /**
138 * Registers a callback that is going to be used on metadata refresh
139 *
140 * @param callback Callback that will be called on each metadata refresh.
141 */
143
144 /**
145 * Unregisters the callback registered with
146 * register_md_refresh_callback().
147 */
149
150 /**
151 * Registers a callback that could be used for checking if the provided
152 * destination candidate is currently quarantined.
153 *
154 * @param clb Callback to query unreachable destinations.
155 */
158
159 /**
160 * Unregisters the callback registered with
161 * register_query_quarantined_destinations().
162 */
164
165 protected:
172 mutable std::mutex md_refresh_callback_mtx_;
175};
176
177/** @class RouteDestination
178 * @brief Manage destinations for a Connection Routing
179 *
180 * This class manages destinations which are used in Connection Routing.
181 * A destination is usually a MySQL Server and is stored using the IP
182 * or hostname together with the TCP port (defaulting to 3306 for classic
183 * protocol or to 33060 for x protocol).
184 *
185 * RouteDestination is meant to be a base class and used to inherite and
186 * create class which change the behavior. For example, the `get_next()`
187 * method is usually changed to get the next server in the list.
188 */
190 public:
191 using AddrVector = std::vector<mysql_harness::TCPAddress>;
192
193 /** @brief Default constructor
194 *
195 * @param io_ctx context for IO operations
196 * @param protocol Protocol for the destination, defaults to value returned
197 * by Protocol::get_default()
198 */
201 : io_ctx_(io_ctx), protocol_(protocol) {}
202
203 /** @brief Destructor */
204 virtual ~RouteDestination() = default;
205
206 RouteDestination(const RouteDestination &other) = delete;
210
211 /** @brief Return our routing strategy
212 */
214
215 /** @brief Adds a destination
216 *
217 * Adds a destination using the given address and port number.
218 *
219 * @param dest destination address
220 */
221 virtual void add(const mysql_harness::TCPAddress dest);
222
223 /** @overload */
224 virtual void add(const std::string &address, uint16_t port);
225
226 /** @brief Removes a destination
227 *
228 * Removes a destination using the given address and port number.
229 *
230 * @param address IP or name
231 * @param port Port number
232 */
233 virtual void remove(const std::string &address, uint16_t port);
234
235 /** @brief Gets destination based on address and port
236 *
237 * Gets destination base on given address and port and returns a pair
238 * with the information.
239 *
240 * Raises std::out_of_range when the combination of address and port
241 * is not in the list of destinations.
242 *
243 * This function can be used to check whether given destination is in
244 * the list.
245 *
246 * @param address IP or name
247 * @param port Port number
248 * @return an instance of mysql_harness::TCPAddress
249 */
250 virtual mysql_harness::TCPAddress get(const std::string &address,
251 uint16_t port);
252
253 /** @brief Removes all destinations
254 *
255 * Removes all destinations from the list.
256 */
257 virtual void clear();
258
259 /** @brief Gets the number of destinations
260 *
261 * Gets the number of destinations currently in the list.
262 *
263 * @return Number of destinations as size_t
264 */
265 size_t size() noexcept;
266
267 /** @brief Returns whether there are destinations
268 *
269 * @return whether the destination is empty
270 */
271 virtual bool empty() const noexcept { return destinations_.empty(); }
272
273 /** @brief Start the destination threads (if any)
274 *
275 * @param env pointer to the PluginFuncEnv object
276 */
277 virtual void start(const mysql_harness::PluginFuncEnv *env);
278
279 AddrVector::iterator begin() { return destinations_.begin(); }
280
281 AddrVector::const_iterator begin() const { return destinations_.begin(); }
282
283 AddrVector::iterator end() { return destinations_.end(); }
284
285 AddrVector::const_iterator end() const { return destinations_.end(); }
286
287 virtual AddrVector get_destinations() const;
288
289 /**
290 * get destinations to connect() to.
291 *
292 * destinations are in order of preference.
293 */
295
298 }
299
300 /**
301 * refresh destinations.
302 *
303 * should be called after connecting to all destinations failed.
304 *
305 * @param dests previous destinations.
306 *
307 * @returns new destinations, if there are any.
308 */
309 virtual std::optional<Destinations> refresh_destinations(
310 const Destinations &dests);
311
312 /**
313 * Trigger listening socket acceptors state handler based on the destination
314 * type.
315 */
316 virtual void handle_sockets_acceptors() {}
317
318 protected:
319 /** @brief List of destinations */
321
322 /** @brief Mutex for updating destinations and iterator */
323 std::mutex mutex_update_;
324
326
327 /** @brief Protocol for the destination */
329};
330
331#endif // ROUTING_DESTINATION_INCLUDED
Type
supported protocols
Definition: base_protocol.h:32
Allows the obervers to register for notifications on the change in the state of the destination nodes...
Definition: destination.h:85
StartSocketAcceptorCallback start_router_socket_acceptor_callback_
Definition: destination.h:168
MetadataRefreshCallback md_refresh_callback_
Definition: destination.h:167
StopSocketAcceptorCallback stop_router_socket_acceptor_callback_
Definition: destination.h:169
void unregister_query_quarantined_destinations()
Unregisters the callback registered with register_query_quarantined_destinations().
Definition: destination.cc:96
std::mutex socket_acceptor_handle_callbacks_mtx
Definition: destination.h:173
void register_query_quarantined_destinations(const QueryQuarantinedDestinationsCallback &clb)
Registers a callback that could be used for checking if the provided destination candidate is current...
Definition: destination.cc:88
QueryQuarantinedDestinationsCallback query_quarantined_destinations_callback_
Definition: destination.h:170
AllowedNodesChangeCallbacksList allowed_nodes_change_callbacks_
Definition: destination.h:166
std::mutex md_refresh_callback_mtx_
Definition: destination.h:172
std::mutex allowed_nodes_change_callbacks_mtx_
Definition: destination.h:171
void unregister_md_refresh_callback()
Unregisters the callback registered with register_md_refresh_callback().
Definition: destination.cc:83
std::mutex query_quarantined_destinations_callback_mtx_
Definition: destination.h:174
void register_md_refresh_callback(const MetadataRefreshCallback &callback)
Registers a callback that is going to be used on metadata refresh.
Definition: destination.cc:77
void register_start_router_socket_acceptor(const StartSocketAcceptorCallback &clb)
Registers the callback for notification that the routing socket acceptor should accept new connection...
Definition: destination.cc:55
void unregister_allowed_nodes_change_callback(const AllowedNodesChangeCallbacksListIterator &it)
Unregisters the callback registered with register_allowed_nodes_change_callback().
Definition: destination.cc:49
void unregister_stop_router_socket_acceptor()
Unregisters the callback registered with register_stop_router_socket_acceptor().
Definition: destination.cc:72
void unregister_start_router_socket_acceptor()
Unregisters the callback registered with register_start_router_socket_acceptor().
Definition: destination.cc:61
void register_stop_router_socket_acceptor(const StopSocketAcceptorCallback &clb)
Registers the callback for notification that the routing socket acceptor should stop accepting new co...
Definition: destination.cc:66
AllowedNodesChangeCallbacksListIterator register_allowed_nodes_change_callback(const AllowedNodesChangedCallback &clb)
Registers the callback for notification on the change in the state if the destination nodes.
Definition: destination.cc:42
A forward iterable container of destinations.
Definition: destination.h:107
static Type get_default()
Definition: protocol.h:37
Manage destinations for a Connection Routing.
Definition: destination.h:189
virtual ~RouteDestination()=default
Destructor.
RouteDestination(const RouteDestination &other)=delete
AddrVector destinations_
List of destinations.
Definition: destination.h:320
virtual AddrVector get_destinations() const
Definition: destination.cc:151
virtual Destinations destinations()=0
get destinations to connect() to.
virtual void remove(const std::string &address, uint16_t port)
Removes a destination.
Definition: destination.cc:119
AddrVector::const_iterator begin() const
Definition: destination.h:281
virtual void handle_sockets_acceptors()
Trigger listening socket acceptors state handler based on the destination type.
Definition: destination.h:316
RouteDestination & operator=(const RouteDestination &other)=delete
net::io_context & io_ctx_
Definition: destination.h:325
RouteDestination(net::io_context &io_ctx, Protocol::Type protocol=Protocol::get_default())
Default constructor.
Definition: destination.h:199
virtual std::optional< Destinations > refresh_destinations(const Destinations &dests)
refresh destinations.
Definition: destination.cc:158
virtual routing::RoutingStrategy get_strategy()=0
Return our routing strategy.
AddrVector::const_iterator end() const
Definition: destination.h:285
std::vector< mysql_harness::TCPAddress > AddrVector
Definition: destination.h:191
Protocol::Type protocol_
Protocol for the destination.
Definition: destination.h:328
RouteDestination(RouteDestination &&other)=delete
std::mutex mutex_update_
Mutex for updating destinations and iterator.
Definition: destination.h:323
virtual bool empty() const noexcept
Returns whether there are destinations.
Definition: destination.h:271
RouteDestination & operator=(RouteDestination &&other)=delete
AddrVector::iterator end()
Definition: destination.h:283
virtual void add(const mysql_harness::TCPAddress dest)
Adds a destination.
Definition: destination.cc:104
virtual void clear()
Removes all destinations.
Definition: destination.cc:143
size_t size() noexcept
Gets the number of destinations.
Definition: destination.cc:141
AddrVector::iterator begin()
Definition: destination.h:279
virtual mysql_harness::TCPAddress get(const std::string &address, uint16_t port)
Gets destination based on address and port.
Definition: destination.cc:131
virtual void start(const mysql_harness::PluginFuncEnv *env)
Start the destination threads (if any)
Definition: destination.cc:156
virtual mysqlrouter::ServerMode purpose() const
Definition: destination.h:296
PluginFuncEnv object.
Definition: loader.h:673
Defines an IP address with port number
Definition: tcp_address.h:40
Definition: io_context.h:61
std::vector< AvailableDestination > AllowedNodes
Definition: destination_status_types.h:62
Header for compiler-dependent features.
Definition: common.h:42
ServerMode
Definition: datatypes.h:50
RoutingStrategy
Routing strategies supported by Routing plugin.
Definition: routing.h:265
required uint64 port
Definition: replication_asynchronous_connection_failover.proto:33
std::function< void()> StopSocketAcceptorCallback
Definition: destination.h:69
std::function< stdx::expected< void, std::string >()> StartSocketAcceptorCallback
Definition: destination.h:68
std::function< void(const AllowedNodes &, const AllowedNodes &, const bool, const std::string &)> AllowedNodesChangedCallback
Definition: destination.h:59
AllowedNodesChangeCallbacksList::iterator AllowedNodesChangeCallbacksListIterator
Definition: destination.h:65
std::list< AllowedNodesChangedCallback > AllowedNodesChangeCallbacksList
Definition: destination.h:63
std::function< void(const bool, const AllowedNodes &)> MetadataRefreshCallback
Definition: destination.h:74
std::function< bool(const mysql_harness::TCPAddress &)> QueryQuarantinedDestinationsCallback
Definition: destination.h:78