MySQL 9.2.0
Source Code Documentation
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
dest_metadata_cache.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_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 private:
128 /**
129 * Resolve hostnames used in routing guidelines document.
130 *
131 * If hostname could be resolved to multiple addresses (with requested IP
132 * version) then only one of the addresses is used, with unspecified order.
133 *
134 * @param hostnames list of hostnames to be resolved, each host contain
135 * information about hostname and IP version which should be used.
136 *
137 * @return hostname address pairs
138 */
139 std::unordered_map<std::string, net::ip::address>
141 const std::vector<routing_guidelines::Resolve_host> &hostnames);
142
143 /** Fill each destination group with destination candidates, according to the
144 * routing guideline that is being used.
145 *
146 * For example given the group setting [[d1,d2], [d3]] and
147 * d1 containing 127.0.0.1
148 * d2 containing 127.0.1.1 127.0.1.2
149 * d3 containing 127.0.2.1
150 * It will create two destination groups
151 * 1) [127.0.0.1, 127.0.1.1, 127.0.1.2]
152 * 2) [127.0.2.1]
153 * Where group 2) is a backup destination group.
154 */
156
157 /**
158 * Change destination group that is currently being used.
159 *
160 * This happens if there was a connection error and current group could not
161 * provide a destination candidate.
162 *
163 * @retval true successful group change
164 * @retval false unsuccessful group change
165 */
166 bool change_group();
167
168 /**
169 * If the routing guideline enables the connection sharing then it validates
170 * if the sharing prerequisites are met and it could be used. If not then
171 * connection sharing is disabled.
172 *
173 * @param route_name name of the route that enables connection sharing
174 * @param dest destination candidate that is going to be used for connection.
175 */
176 void validate_current_sharing_settings(std::string_view route_name,
177 Destination *dest) const;
178
179 std::unique_ptr<Destination> get_next_destination_impl();
180
181 /** Set information if the last connection was successful. */
182 void set_last_connect_successful(const bool state);
183
184 /**
185 * Get addresses of nodes allowed by the auto-generated routing guideline.
186 *
187 * Should not be called when user-provided guideline is used as in such case
188 * it might be impossible to determine the list upfront (matching criteria
189 * might depend on source IP info for example).
190 */
191 std::vector<routing_guidelines::Server_info>
193
194 /** Get addresses of all nodes in the topology*/
195 std::vector<routing_guidelines::Server_info> get_all_nodes() const;
196
197 /** @brief The Metadata Cache to use
198 *
199 * cache_name_ is the the section key in the configuration of Metadata Cache.
200 *
201 * For example, given following Metadata Cache configuration, cache_name_ will
202 * be set to "ham":
203 *
204 * [metadata_cache.ham]
205 * host = metadata.example.com
206 *
207 */
208 const std::string cache_name_;
209
210 /** @brief Query part of the URI given as destination in the configuration
211 *
212 * For example, given following Metadata Cache configuration:
213 *
214 * [routing:metadata_read_only]
215 * ..
216 * destination =
217 * metadata_cache:///cluster_name/replicaset_name?role=PRIMARY_AND_SECONDARY
218 *
219 * The 'role' is part of uri_query_.
220 */
222
223 /** @brief Initializes
224 *
225 * This method initialized the object. It goes of the URI query information
226 * and sets members accordingly.
227 */
228 void init();
229
230 /** Get destination candidates details from the given topology
231 *
232 * @param cluster_topology topology of the cluster
233 * @param drop_all_hidden shoud the hidden nodes be included in the result
234 *
235 * @return list of destination candidates
236 */
237 virtual std::vector<routing_guidelines::Server_info> get_nodes_from_topology(
238 const metadata_cache::ClusterTopology &cluster_topology,
239 const bool drop_all_hidden) const;
240
241 /**
242 * Get information about nodes available for new connections.
243 */
244 std::vector<routing_guidelines::Server_info> get_new_connection_nodes() const;
245
246 /**
247 * Get information about nodes available for existing connections.
248 */
249 std::vector<routing_guidelines::Server_info> get_old_connection_nodes() const;
250
251 /**
252 * Get a destination candidate that was already selected by the Destination
253 * Manager, this will not balance destinations or change the Destination
254 * Manager internal state.
255 */
256 std::unique_ptr<Destination> get_last_used_destination() const override {
257 return std::make_unique<Destination>(destination_);
258 }
259
261
263
265
268
269 void on_instances_change(const bool md_servers_reachable);
273
274 void notify_instances_changed(const bool md_servers_reachable,
275 const uint64_t /*view_id*/) noexcept override;
276
277 bool update_socket_acceptor_state() noexcept override;
278
279 void on_md_refresh(const bool instances_changed) override;
280
281 /**
282 * Status of a last connection.
283 */
284 enum class ConnectionStatus {
285 InProgress,
286 Failed,
287 NotSet,
288 };
289
290 /** Routing guideline engine. */
291 std::shared_ptr<routing_guidelines::Routing_guidelines_engine>
293
294 /** @brief Protocol for the destination */
296
297 /** Routing strategy that is used within the currently used destination group.
298 */
300
301 /** UUID of a last destination returned by get_next_destination(), used in
302 * wait for primary failover mechanism. */
303 std::string last_server_uuid_;
304
305 /** Guidelines route which is designated by the guidelines engine to handle
306 * connection. */
309
310 /** Destination candidates that are going to be used to create destination
311 * groups. */
312 std::vector<std::vector<Destination>> destination_candidates_;
313
314 /** Index of the currently used destination group. */
316
317 /** Index of the current position within a destination group. */
319
320 // Position of last used destination for each destination groups, used to
321 // fairly balance the load in backup destination groups,
322 std::map<uint16_t, uint16_t> stored_destination_indexes_;
323
324 /** Information about previous connection status. */
325 std::atomic<ConnectionStatus> last_connection_status_{
327
328 /** How many available destinations are in the currently used destination
329 * group. */
331
332 /** Destination manager contains read-write destination candidates. */
333 bool has_read_write_{false};
334
335 /** Destination manager contains read-only destination candidates. */
336 bool has_read_only_{false};
337
338 /** Destination thats used for the connection. */
340};
341
343 const mysqlrouter::URIQuery &uri);
344
345#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:262
std::shared_ptr< routing_guidelines::Routing_guidelines_engine > routing_guidelines_
Routing guideline engine.
Definition: dest_metadata_cache.h:292
uint16_t current_group_position_
Index of the current position within a destination group.
Definition: dest_metadata_cache.h:318
void clear_internal_state()
Clear internal state (indexes, last connection status etc).
Definition: dest_metadata_cache.cc:1040
DestMetadataCacheManager & operator=(DestMetadataCacheManager &&)=delete
Move assignment.
std::atomic< ConnectionStatus > last_connection_status_
Information about previous connection status.
Definition: dest_metadata_cache.h:325
void set_last_connect_successful(const bool state)
Set information if the last connection was successful.
Definition: dest_metadata_cache.cc:1059
Protocol::Type protocol_
Protocol for the destination.
Definition: dest_metadata_cache.h:295
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:909
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:208
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:1002
void on_instances_change(const bool md_servers_reachable)
Definition: dest_metadata_cache.cc:514
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:264
DestMetadataCacheManager(DestMetadataCacheManager &&)=delete
Move constructor.
mysqlrouter::ServerMode purpose() const override
Definition: dest_metadata_cache.h:79
const mysqlrouter::URIQuery uri_query_
Query part of the URI given as destination in the configuration.
Definition: dest_metadata_cache.h:221
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:583
uint16_t current_destination_group_index_
Index of the currently used destination group.
Definition: dest_metadata_cache.h:315
bool has_read_write_
Destination manager contains read-write destination candidates.
Definition: dest_metadata_cache.h:333
bool refresh_destinations(const routing_guidelines::Session_info &session_info) override
refresh destinations.
Definition: dest_metadata_cache.cc:482
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:776
ServerRole server_role_
Definition: dest_metadata_cache.h:260
std::unique_ptr< Destination > get_next_destination_impl()
Definition: dest_metadata_cache.cc:832
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:562
std::map< uint16_t, uint16_t > stored_destination_indexes_
Definition: dest_metadata_cache.h:322
~DestMetadataCacheManager() override
Definition: dest_metadata_cache.cc:471
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:308
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:1052
void start(const mysql_harness::PluginFuncEnv *env) override
Start the destination.
Definition: dest_metadata_cache.cc:608
Destination destination_
Destination thats used for the connection.
Definition: dest_metadata_cache.h:339
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:256
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:557
bool disconnect_on_metadata_unavailable_
Definition: dest_metadata_cache.h:267
bool has_read_only_
Destination manager contains read-only destination candidates.
Definition: dest_metadata_cache.h:336
metadata_cache::MetadataCacheAPIBase * cache_api()
Definition: dest_metadata_cache.h:89
bool disconnect_on_promoted_to_primary_
Definition: dest_metadata_cache.h:266
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:303
void prepare_destination_groups()
Fill each destination group with destination candidates, according to the routing guideline that is b...
Definition: dest_metadata_cache.cc:657
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:755
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:719
routing::RoutingStrategy strategy_
Routing strategy that is used within the currently used destination group.
Definition: dest_metadata_cache.h:299
std::vector< std::vector< Destination > > destination_candidates_
Destination candidates that are going to be used to create destination groups.
Definition: dest_metadata_cache.h:312
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:284
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:330
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:622
Manage destinations for a Connection Routing.
Definition: destination.h:163
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:673
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:265
#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:207
Type for names of Routes changed during routing guidelines document update.
Definition: routing_guidelines.h:322
Result of route classification.
Definition: routing_guidelines.h:330
Information about incoming session.
Definition: routing_guidelines.h:101