MySQL 8.4.13
Source Code Documentation
destination.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2015, 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_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// Refreshing a socket acceptor returns a value indicating if the refresh
67// succeeded.
69 std::function<stdx::expected<void, std::string>()>;
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 * state may need to be refreshed.
110 *
111 * @param clb callback that should be called
112 */
115
116 /**
117 * Unregisters the callback registered with
118 * register_refresh_router_socket_acceptor_state().
119 */
121
122 /**
123 * Registers a callback that is going to be used on metadata refresh
124 *
125 * @param callback Callback that will be called on each metadata refresh.
126 */
128
129 /**
130 * Unregisters the callback registered with
131 * register_md_refresh_callback().
132 */
134
135 /**
136 * Registers a callback that could be used for checking if the provided
137 * destination candidate is currently quarantined.
138 *
139 * @param clb Callback to query unreachable destinations.
140 */
143
144 /**
145 * Unregisters the callback registered with
146 * register_query_quarantined_destinations().
147 */
149
150 protected:
157 mutable std::mutex md_refresh_callback_mtx_;
160};
161
162/** @class RouteDestination
163 * @brief Manage destinations for a Connection Routing
164 *
165 * This class manages destinations which are used in Connection Routing.
166 * A destination is usually a MySQL Server and is stored using the IP
167 * or hostname together with the TCP port (defaulting to 3306 for classic
168 * protocol or to 33060 for x protocol).
169 *
170 * RouteDestination is meant to be a base class and used to inherite and
171 * create class which change the behavior. For example, the `get_next()`
172 * method is usually changed to get the next server in the list.
173 */
175 public:
176 using AddrVector = std::vector<mysql_harness::TCPAddress>;
177
178 /** @brief Default constructor
179 *
180 * @param io_ctx context for IO operations
181 * @param protocol Protocol for the destination, defaults to value returned
182 * by Protocol::get_default()
183 */
186 : io_ctx_(io_ctx), protocol_(protocol) {}
187
188 /** @brief Destructor */
189 virtual ~RouteDestination() = default;
190
191 RouteDestination(const RouteDestination &other) = delete;
195
196 /** @brief Return our routing strategy
197 */
199
200 /** @brief Adds a destination
201 *
202 * Adds a destination using the given address and port number.
203 *
204 * @param dest destination address
205 */
206 virtual void add(const mysql_harness::TCPAddress dest);
207
208 /** @overload */
209 virtual void add(const std::string &address, uint16_t port);
210
211 /** @brief Removes a destination
212 *
213 * Removes a destination using the given address and port number.
214 *
215 * @param address IP or name
216 * @param port Port number
217 */
218 virtual void remove(const std::string &address, uint16_t port);
219
220 /** @brief Gets destination based on address and port
221 *
222 * Gets destination base on given address and port and returns a pair
223 * with the information.
224 *
225 * Raises std::out_of_range when the combination of address and port
226 * is not in the list of destinations.
227 *
228 * This function can be used to check whether given destination is in
229 * the list.
230 *
231 * @param address IP or name
232 * @param port Port number
233 * @return an instance of mysql_harness::TCPAddress
234 */
235 virtual mysql_harness::TCPAddress get(const std::string &address,
236 uint16_t port);
237
238 /** @brief Removes all destinations
239 *
240 * Removes all destinations from the list.
241 */
242 virtual void clear();
243
244 /** @brief Gets the number of destinations
245 *
246 * Gets the number of destinations currently in the list.
247 *
248 * @return Number of destinations as size_t
249 */
250 size_t size() noexcept;
251
252 /** @brief Returns whether there are destinations
253 *
254 * @return whether the destination is empty
255 */
256 virtual bool empty() const noexcept { return destinations_.empty(); }
257
258 /** @brief Start the destination threads (if any)
259 *
260 * @param env pointer to the PluginFuncEnv object
261 */
262 virtual void start(const mysql_harness::PluginFuncEnv *env);
263
264 AddrVector::iterator begin() { return destinations_.begin(); }
265
266 AddrVector::const_iterator begin() const { return destinations_.begin(); }
267
268 AddrVector::iterator end() { return destinations_.end(); }
269
270 AddrVector::const_iterator end() const { return destinations_.end(); }
271
272 virtual AddrVector get_destinations() const;
273
274 /**
275 * get destinations to connect() to.
276 *
277 * destinations are in order of preference.
278 */
280
283 }
284
285 /**
286 * refresh destinations.
287 *
288 * should be called after connecting to all destinations failed.
289 *
290 * @param dests previous destinations.
291 *
292 * @returns new destinations, if there are any.
293 */
294 virtual std::optional<Destinations> refresh_destinations(
295 const Destinations &dests);
296
297 /**
298 * Trigger listening socket acceptors state handler based on the destination
299 * type.
300 */
301 virtual void handle_sockets_acceptors() {}
302
303 protected:
304 /** @brief List of destinations */
306
307 /** @brief Mutex for updating destinations and iterator */
308 std::mutex mutex_update_;
309
311
312 /** @brief Protocol for the destination */
314};
315
316#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
MetadataRefreshCallback md_refresh_callback_
Definition: destination.h:152
RefreshSocketAcceptorStateCallback refresh_router_socket_acceptor_state_callback_
Definition: destination.h:154
void unregister_refresh_router_socket_acceptor_state()
Unregisters the callback registered with register_refresh_router_socket_acceptor_state().
Definition: destination.cc:63
void unregister_query_quarantined_destinations()
Unregisters the callback registered with register_query_quarantined_destinations().
Definition: destination.cc:87
void register_refresh_router_socket_acceptor_state(const RefreshSocketAcceptorStateCallback &clb)
Registers the callback for notification that the routing socket acceptor state may need to be refresh...
Definition: destination.cc:56
std::mutex socket_acceptor_handle_callbacks_mtx
Definition: destination.h:158
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:79
QueryQuarantinedDestinationsCallback query_quarantined_destinations_callback_
Definition: destination.h:155
AllowedNodesChangeCallbacksList allowed_nodes_change_callbacks_
Definition: destination.h:151
std::mutex md_refresh_callback_mtx_
Definition: destination.h:157
std::mutex allowed_nodes_change_callbacks_mtx_
Definition: destination.h:156
void unregister_md_refresh_callback()
Unregisters the callback registered with register_md_refresh_callback().
Definition: destination.cc:74
std::mutex query_quarantined_destinations_callback_mtx_
Definition: destination.h:159
void register_md_refresh_callback(const MetadataRefreshCallback &callback)
Registers a callback that is going to be used on metadata refresh.
Definition: destination.cc:68
void unregister_allowed_nodes_change_callback(const AllowedNodesChangeCallbacksListIterator &it)
Unregisters the callback registered with register_allowed_nodes_change_callback().
Definition: destination.cc:49
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:174
virtual ~RouteDestination()=default
Destructor.
RouteDestination(const RouteDestination &other)=delete
AddrVector destinations_
List of destinations.
Definition: destination.h:305
virtual AddrVector get_destinations() const
Definition: destination.cc:142
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:110
AddrVector::const_iterator begin() const
Definition: destination.h:266
virtual void handle_sockets_acceptors()
Trigger listening socket acceptors state handler based on the destination type.
Definition: destination.h:301
RouteDestination & operator=(const RouteDestination &other)=delete
net::io_context & io_ctx_
Definition: destination.h:310
RouteDestination(net::io_context &io_ctx, Protocol::Type protocol=Protocol::get_default())
Default constructor.
Definition: destination.h:184
virtual std::optional< Destinations > refresh_destinations(const Destinations &dests)
refresh destinations.
Definition: destination.cc:149
virtual routing::RoutingStrategy get_strategy()=0
Return our routing strategy.
AddrVector::const_iterator end() const
Definition: destination.h:270
std::vector< mysql_harness::TCPAddress > AddrVector
Definition: destination.h:176
Protocol::Type protocol_
Protocol for the destination.
Definition: destination.h:313
RouteDestination(RouteDestination &&other)=delete
std::mutex mutex_update_
Mutex for updating destinations and iterator.
Definition: destination.h:308
virtual bool empty() const noexcept
Returns whether there are destinations.
Definition: destination.h:256
RouteDestination & operator=(RouteDestination &&other)=delete
AddrVector::iterator end()
Definition: destination.h:268
virtual void add(const mysql_harness::TCPAddress dest)
Adds a destination.
Definition: destination.cc:95
virtual void clear()
Removes all destinations.
Definition: destination.cc:134
size_t size() noexcept
Gets the number of destinations.
Definition: destination.cc:132
AddrVector::iterator begin()
Definition: destination.h:264
virtual mysql_harness::TCPAddress get(const std::string &address, uint16_t port)
Gets destination based on address and port.
Definition: destination.cc:122
virtual void start(const mysql_harness::PluginFuncEnv *env)
Start the destination threads (if any)
Definition: destination.cc:147
virtual mysqlrouter::ServerMode purpose() const
Definition: destination.h:281
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:60
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(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< stdx::expected< void, std::string >()> RefreshSocketAcceptorStateCallback
Definition: destination.h:69
std::function< bool(const mysql_harness::TCPAddress &)> QueryQuarantinedDestinationsCallback
Definition: destination.h:78