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