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