MySQL 9.3.0
Source Code Documentation
dest_metadata_cache.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2015, 2025, 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_DEST_METADATA_CACHE_INCLUDED
27#define ROUTING_DEST_METADATA_CACHE_INCLUDED
28
29#include "destination.h"
31#include "mysql_routing.h"
35#include "mysqlrouter/uri.h"
37
39 : public DestinationManager,
43 public:
45
46 /** @brief Constructor */
48 MySQLRoutingContext &routing_ctx,
49 const std::string &metadata_cache,
51 const ServerRole role,
54
55 /** @brief Copy constructor */
57
58 /** @brief Move constructor */
60
61 /** @brief Copy assignment */
63 delete;
64
65 /** @brief Move assignment */
67
69
70 /** @brief Start the destination
71 *
72 * It also overwrites parent class' DestinationManager::start(), which
73 * launches Quarantine. For Metadata Cache routing, we don't need it.
74 *
75 * @param env pointer to the PluginFuncEnv object
76 */
77 void start(const mysql_harness::PluginFuncEnv *env) override;
78
80 return server_role_ == ServerRole::Primary
83 }
84
86 const routing_guidelines::Session_info &session_info) override;
87
88 // get cache-api
90
92 const routing_guidelines::Session_info &session_info) override;
93
94 void handle_sockets_acceptors() override {
96 }
97
98 std::unique_ptr<Destination> get_next_destination(
99 const routing_guidelines::Session_info &session_info) override;
100
101 std::vector<mysql_harness::Destination> get_destination_candidates()
102 const override;
103
104 void connect_status(std::error_code ec) override;
105
106 bool has_read_write() const override { return has_read_write_; }
107 bool has_read_only() const override { return has_read_only_; }
108
109 /**
110 * Update routing guidelines engine with a new routing guideline.
111 *
112 * If the new routing guideline is empty then auto-generated guideline is
113 * used. If the guidelines engine could not be updated then old guideline is
114 * preserved and used.
115 *
116 * @return List of route names of the routes that have been updated.
117 */
119 update_routing_guidelines(const std::string &routing_guidelines_document);
120
121 /**
122 * Clear internal state (indexes, last connection status etc). Used when
123 * guidelines are updated.
124 */
126
127 bool is_dynamic() override { return true; }
128 std::string get_dynamic_plugin_name() override;
129
130 private:
131 /**
132 * Resolve hostnames used in routing guidelines document.
133 *
134 * If hostname could be resolved to multiple addresses (with requested IP
135 * version) then only one of the addresses is used, with unspecified order.
136 *
137 * @param hostnames list of hostnames to be resolved, each host contain
138 * information about hostname and IP version which should be used.
139 *
140 * @return hostname address pairs
141 */
142 std::unordered_map<std::string, net::ip::address>
144 const std::vector<routing_guidelines::Resolve_host> &hostnames);
145
146 /** Fill each destination group with destination candidates, according to the
147 * routing guideline that is being used.
148 *
149 * For example given the group setting [[d1,d2], [d3]] and
150 * d1 containing 127.0.0.1
151 * d2 containing 127.0.1.1 127.0.1.2
152 * d3 containing 127.0.2.1
153 * It will create two destination groups
154 * 1) [127.0.0.1, 127.0.1.1, 127.0.1.2]
155 * 2) [127.0.2.1]
156 * Where group 2) is a backup destination group.
157 */
159
160 /**
161 * Change destination group that is currently being used.
162 *
163 * This happens if there was a connection error and current group could not
164 * provide a destination candidate.
165 *
166 * @retval true successful group change
167 * @retval false unsuccessful group change
168 */
169 bool change_group();
170
171 /**
172 * If the routing guideline enables the connection sharing then it validates
173 * if the sharing prerequisites are met and it could be used. If not then
174 * connection sharing is disabled.
175 *
176 * @param route_name name of the route that enables connection sharing
177 * @param dest destination candidate that is going to be used for connection.
178 */
179 void validate_current_sharing_settings(std::string_view route_name,
180 Destination *dest) const;
181
182 std::unique_ptr<Destination> get_next_destination_impl();
183
184 /** Set information if the last connection was successful. */
185 void set_last_connect_successful(const bool state);
186
187 /**
188 * Get addresses of nodes allowed by the auto-generated routing guideline.
189 *
190 * Should not be called when user-provided guideline is used as in such case
191 * it might be impossible to determine the list upfront (matching criteria
192 * might depend on source IP info for example).
193 */
194 std::vector<routing_guidelines::Server_info>
196
197 /** Get addresses of all nodes in the topology*/
198 std::vector<routing_guidelines::Server_info> get_all_nodes() const;
199
200 /** @brief The Metadata Cache to use
201 *
202 * cache_name_ is the the section key in the configuration of Metadata Cache.
203 *
204 * For example, given following Metadata Cache configuration, cache_name_ will
205 * be set to "ham":
206 *
207 * [metadata_cache.ham]
208 * host = metadata.example.com
209 *
210 */
211 const std::string cache_name_;
212
213 /** @brief Query part of the URI given as destination in the configuration
214 *
215 * For example, given following Metadata Cache configuration:
216 *
217 * [routing:metadata_read_only]
218 * ..
219 * destination =
220 * metadata_cache:///cluster_name/replicaset_name?role=PRIMARY_AND_SECONDARY
221 *
222 * The 'role' is part of uri_query_.
223 */
225
226 /** @brief Initializes
227 *
228 * This method initialized the object. It goes of the URI query information
229 * and sets members accordingly.
230 */
231 void init();
232
233 /** Get destination candidates details from the given topology
234 *
235 * @param cluster_topology topology of the cluster
236 * @param drop_all_hidden shoud the hidden nodes be included in the result
237 *
238 * @return list of destination candidates
239 */
240 virtual std::vector<routing_guidelines::Server_info> get_nodes_from_topology(
241 const metadata_cache::ClusterTopology &cluster_topology,
242 const bool drop_all_hidden) const;
243
244 /**
245 * Get information about nodes available for new connections.
246 */
247 std::vector<routing_guidelines::Server_info> get_new_connection_nodes() const;
248
249 /**
250 * Get information about nodes available for existing connections.
251 */
252 std::vector<routing_guidelines::Server_info> get_old_connection_nodes() const;
253
254 /**
255 * Get a destination candidate that was already selected by the Destination
256 * Manager, this will not balance destinations or change the Destination
257 * Manager internal state.
258 */
259 std::unique_ptr<Destination> get_last_used_destination() const override {
260 return std::make_unique<Destination>(destination_);
261 }
262
264
266
268
271
272 void on_instances_change(const bool md_servers_reachable);
276
277 void notify_instances_changed(const bool md_servers_reachable,
278 const uint64_t /*view_id*/) noexcept override;
279
280 bool update_socket_acceptor_state() noexcept override;
281
282 void on_md_refresh(const bool instances_changed) override;
283
284 /**
285 * Status of a last connection.
286 */
287 enum class ConnectionStatus {
288 InProgress,
289 Failed,
290 NotSet,
291 };
292
293 /** Routing guideline engine. */
294 std::shared_ptr<routing_guidelines::Routing_guidelines_engine>
296
297 /** @brief Protocol for the destination */
299
300 /** Routing strategy that is used within the currently used destination group.
301 */
303
304 /** UUID of a last destination returned by get_next_destination(), used in
305 * wait for primary failover mechanism. */
306 std::string last_server_uuid_;
307
308 /** Guidelines route which is designated by the guidelines engine to handle
309 * connection. */
312
313 /** Destination candidates that are going to be used to create destination
314 * groups. */
315 std::vector<std::vector<Destination>> destination_candidates_;
316
317 /** Index of the currently used destination group. */
319
320 /** Index of the current position within a destination group. */
322
323 // Position of last used destination for each destination groups, used to
324 // fairly balance the load in backup destination groups,
325 std::map<uint16_t, uint16_t> stored_destination_indexes_;
326
327 /** Information about previous connection status. */
328 std::atomic<ConnectionStatus> last_connection_status_{
330
331 /** How many available destinations are in the currently used destination
332 * group. */
334
335 /** Destination manager contains read-write destination candidates. */
336 bool has_read_write_{false};
337
338 /** Destination manager contains read-only destination candidates. */
339 bool has_read_only_{false};
340
341 /** Destination thats used for the connection. */
343};
344
346 const mysqlrouter::URIQuery &uri);
347
348#endif // ROUTING_DEST_METADATA_CACHE_INCLUDED
Type
supported protocols
Definition: base_protocol.h:32
Definition: dest_metadata_cache.h:42
metadata_cache::MetadataCacheAPIBase * cache_api_
Definition: dest_metadata_cache.h:265
std::shared_ptr< routing_guidelines::Routing_guidelines_engine > routing_guidelines_
Routing guideline engine.
Definition: dest_metadata_cache.h:295
uint16_t current_group_position_
Index of the current position within a destination group.
Definition: dest_metadata_cache.h:321
void clear_internal_state()
Clear internal state (indexes, last connection status etc).
Definition: dest_metadata_cache.cc:1044
DestMetadataCacheManager & operator=(DestMetadataCacheManager &&)=delete
Move assignment.
std::atomic< ConnectionStatus > last_connection_status_
Information about previous connection status.
Definition: dest_metadata_cache.h:328
void set_last_connect_successful(const bool state)
Set information if the last connection was successful.
Definition: dest_metadata_cache.cc:1063
Protocol::Type protocol_
Protocol for the destination.
Definition: dest_metadata_cache.h:298
void subscribe_for_metadata_cache_changes()
Definition: dest_metadata_cache.cc:458
stdx::expected< void, std::error_code > init_destinations(const routing_guidelines::Session_info &session_info) override
Set up destination manager, prepare the destinations.
Definition: dest_metadata_cache.cc:913
std::vector< mysql_harness::Destination > get_destination_candidates() const override
Get addresses of all nodes that are a possible destination candidates.
Definition: dest_metadata_cache.cc:429
const std::string cache_name_
The Metadata Cache to use.
Definition: dest_metadata_cache.h:211
routing_guidelines::Routing_guidelines_engine::RouteChanges update_routing_guidelines(const std::string &routing_guidelines_document)
Update routing guidelines engine with a new routing guideline.
Definition: dest_metadata_cache.cc:1006
void on_instances_change(const bool md_servers_reachable)
Definition: dest_metadata_cache.cc:518
bool has_read_only() const override
Check if there are read-only destinations that could be used.
Definition: dest_metadata_cache.h:107
std::vector< routing_guidelines::Server_info > get_old_connection_nodes() const
Get information about nodes available for existing connections.
Definition: dest_metadata_cache.cc:306
void handle_sockets_acceptors() override
Trigger listening socket acceptors state handler based on the destination type.
Definition: dest_metadata_cache.h:94
bool subscribed_for_metadata_cache_changes_
Definition: dest_metadata_cache.h:267
DestMetadataCacheManager(DestMetadataCacheManager &&)=delete
Move constructor.
mysqlrouter::ServerMode purpose() const override
Definition: dest_metadata_cache.h:79
bool is_dynamic() override
Specifies if the destination addresses can be added/removed from the set.
Definition: dest_metadata_cache.h:127
const mysqlrouter::URIQuery uri_query_
Query part of the URI given as destination in the configuration.
Definition: dest_metadata_cache.h:224
void on_md_refresh(const bool instances_changed) override
Callback that is going to be used on each metadata refresh.
Definition: dest_metadata_cache.cc:587
uint16_t current_destination_group_index_
Index of the currently used destination group.
Definition: dest_metadata_cache.h:318
bool has_read_write_
Destination manager contains read-write destination candidates.
Definition: dest_metadata_cache.h:336
bool refresh_destinations(const routing_guidelines::Session_info &session_info) override
refresh destinations.
Definition: dest_metadata_cache.cc:486
std::vector< routing_guidelines::Server_info > get_new_connection_nodes() const
Get information about nodes available for new connections.
Definition: dest_metadata_cache.cc:416
bool change_group()
Change destination group that is currently being used.
Definition: dest_metadata_cache.cc:780
ServerRole server_role_
Definition: dest_metadata_cache.h:263
std::unique_ptr< Destination > get_next_destination_impl()
Definition: dest_metadata_cache.cc:836
bool update_socket_acceptor_state() noexcept override
Callback function that is called when the state of the sockets acceptors is handled during the metada...
Definition: dest_metadata_cache.cc:566
std::map< uint16_t, uint16_t > stored_destination_indexes_
Definition: dest_metadata_cache.h:325
~DestMetadataCacheManager() override
Definition: dest_metadata_cache.cc:475
routing_guidelines::Routing_guidelines_engine::Route_classification route_info_
Guidelines route which is designated by the guidelines engine to handle connection.
Definition: dest_metadata_cache.h:311
void subscribe_for_acceptor_handler()
Definition: dest_metadata_cache.cc:463
DestMetadataCacheManager(const DestMetadataCacheManager &other)=delete
Copy constructor.
void connect_status(std::error_code ec) override
Definition: dest_metadata_cache.cc:1056
std::string get_dynamic_plugin_name() override
Definition: dest_metadata_cache.cc:471
void start(const mysql_harness::PluginFuncEnv *env) override
Start the destination.
Definition: dest_metadata_cache.cc:612
Destination destination_
Destination thats used for the connection.
Definition: dest_metadata_cache.h:342
std::unique_ptr< Destination > get_last_used_destination() const override
Get a destination candidate that was already selected by the Destination Manager, this will not balan...
Definition: dest_metadata_cache.h:259
void notify_instances_changed(const bool md_servers_reachable, const uint64_t) noexcept override
Callback function that is called when state of cluster is changed.
Definition: dest_metadata_cache.cc:561
bool disconnect_on_metadata_unavailable_
Definition: dest_metadata_cache.h:270
bool has_read_only_
Destination manager contains read-only destination candidates.
Definition: dest_metadata_cache.h:339
metadata_cache::MetadataCacheAPIBase * cache_api()
Definition: dest_metadata_cache.h:89
bool disconnect_on_promoted_to_primary_
Definition: dest_metadata_cache.h:269
void subscribe_for_md_refresh_handler()
Definition: dest_metadata_cache.cc:467
bool has_read_write() const override
Check if there are read-write destinations that could be used.
Definition: dest_metadata_cache.h:106
std::string last_server_uuid_
UUID of a last destination returned by get_next_destination(), used in wait for primary failover mech...
Definition: dest_metadata_cache.h:306
void prepare_destination_groups()
Fill each destination group with destination candidates, according to the routing guideline that is b...
Definition: dest_metadata_cache.cc:661
DestMetadataCacheManager & operator=(const DestMetadataCacheManager &)=delete
Copy assignment.
void init()
Initializes.
Definition: dest_metadata_cache.cc:439
ServerRole
Definition: dest_metadata_cache.h:44
@ Primary
Definition: dest_metadata_cache.h:44
@ PrimaryAndSecondary
Definition: dest_metadata_cache.h:44
@ Secondary
Definition: dest_metadata_cache.h:44
std::unique_ptr< Destination > get_next_destination(const routing_guidelines::Session_info &session_info) override
Get destination that should be used for connection attempt.
Definition: dest_metadata_cache.cc:759
std::vector< routing_guidelines::Server_info > get_nodes_allowed_by_routing_guidelines() const
Get addresses of nodes allowed by the auto-generated routing guideline.
Definition: dest_metadata_cache.cc:345
void validate_current_sharing_settings(std::string_view route_name, Destination *dest) const
If the routing guideline enables the connection sharing then it validates if the sharing prerequisite...
Definition: dest_metadata_cache.cc:723
routing::RoutingStrategy strategy_
Routing strategy that is used within the currently used destination group.
Definition: dest_metadata_cache.h:302
std::vector< std::vector< Destination > > destination_candidates_
Destination candidates that are going to be used to create destination groups.
Definition: dest_metadata_cache.h:315
std::vector< routing_guidelines::Server_info > get_all_nodes() const
Get addresses of all nodes in the topology.
Definition: dest_metadata_cache.cc:409
ConnectionStatus
Status of a last connection.
Definition: dest_metadata_cache.h:287
virtual std::vector< routing_guidelines::Server_info > get_nodes_from_topology(const metadata_cache::ClusterTopology &cluster_topology, const bool drop_all_hidden) const
Get destination candidates details from the given topology.
Definition: dest_metadata_cache.cc:246
uint16_t available_dests_in_group_
How many available destinations are in the currently used destination group.
Definition: dest_metadata_cache.h:333
DestMetadataCacheManager(net::io_context &io_ctx, MySQLRoutingContext &routing_ctx, const std::string &metadata_cache, const mysqlrouter::URIQuery &query, const ServerRole role, metadata_cache::MetadataCacheAPIBase *cache_api=metadata_cache::MetadataCacheAPI::instance())
Constructor.
Definition: dest_metadata_cache.cc:226
std::unordered_map< std::string, net::ip::address > resolve_routing_guidelines_hostnames(const std::vector< routing_guidelines::Resolve_host > &hostnames)
Resolve hostnames used in routing guidelines document.
Definition: dest_metadata_cache.cc:626
Manage destinations for a Connection Routing.
Definition: destination.h:84
Destination to forward client connections to.
Definition: destination.h:43
MySQLRoutingContext holds data used by MySQLRouting (1 per plugin instances) and MySQLRoutingConnecti...
Definition: context.h:54
Abstract class that provides interface for listener on whether the listening sockets acceptors state ...
Definition: metadata_cache.h:109
Abstract class that provides interface for listener on cluster status changes.
Definition: metadata_cache.h:82
Definition: metadata_cache.h:216
virtual void handle_sockets_acceptors_on_md_refresh()=0
Toggle socket acceptors state update on next metadata refresh.
static MetadataCacheAPIBase * instance()
Definition: cache_api.cc:49
Abstract class that provides interface for listener on metadata refresh.
Definition: metadata_cache.h:134
PluginFuncEnv object.
Definition: loader.h:675
Definition: io_context.h:61
Definition: expected.h:286
ROUTING_EXPORT DestMetadataCacheManager::ServerRole get_server_role_from_uri(const mysqlrouter::URIQuery &uri)
Definition: dest_metadata_cache.cc:80
static char * query
Definition: myisam_ftdump.cc:47
Defining the class MySQLRouting.
Definition: metadata_cache.h:49
ServerMode
Definition: datatypes.h:50
std::map< std::string, std::string > URIQuery
Definition: uri.h:43
RoutingStrategy
Routing strategies supported by Routing plugin.
Definition: routing.h:271
#define ROUTING_EXPORT
Definition: routing_export.h:15
Represents a cluster (a GR group or AR members) and its metadata servers.
Definition: metadata_cache_datatypes.h:208
Type for names of Routes changed during routing guidelines document update.
Definition: routing_guidelines.h:324
Result of route classification.
Definition: routing_guidelines.h:332
Information about incoming session.
Definition: routing_guidelines.h:103