MySQL 9.1.0
Source Code Documentation
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 <system_error>
30#include <thread>
31
32#include "destination.h"
34#include "mysql_routing.h"
38#include "mysqlrouter/uri.h"
39#include "tcp_address.h"
40
42 : public RouteDestination,
46 public:
48
49 /** @brief Constructor */
51 const std::string &metadata_cache,
52 const routing::RoutingStrategy routing_strategy,
54 const Protocol::Type protocol,
57
58 /** @brief Copy constructor */
60
61 /** @brief Move constructor */
63
64 /** @brief Copy assignment */
66
67 /** @brief Move assignment */
69
70 ~DestMetadataCacheGroup() override;
71
72 void add(const std::string &, uint16_t) override {}
73 void add(const mysql_harness::TCPAddress) override {}
74
75 AddrVector get_destinations() const override;
76
77 /** @brief Returns whether there are destination servers
78 *
79 * The empty() method always returns false for Metadata Cache.
80 *
81 * Checking whether the Metadata Cache is empty for given destination
82 * might be to expensive. We leave this to the get_server() method.
83 *
84 * @return Always returns False for Metadata Cache destination.
85 */
86 bool empty() const noexcept override { return false; }
87
88 /** @brief Start the destination
89 *
90 * It also overwrites parent class' RouteDestination::start(), which launches
91 * Quarantine. For Metadata Cache routing, we don't need it.
92 *
93 * @param env pointer to the PluginFuncEnv object
94 */
95 void start(const mysql_harness::PluginFuncEnv *env) override;
96
97 Destinations destinations() override;
98
100 return server_role_ == ServerRole::Primary
103 }
104
106
107 // get cache-api
109
110 std::optional<Destinations> refresh_destinations(
111 const Destinations &dests) override;
112
114
115 /**
116 * advance the current position in the destination by n.
117 */
118 void advance(size_t n);
119
120 void handle_sockets_acceptors() override {
122 }
123
125
126 private:
127 /** @brief The Metadata Cache to use
128 *
129 * cache_name_ is the the section key in the configuration of Metadata Cache.
130 *
131 * For example, given following Metadata Cache configuration, cache_name_ will
132 * be set to "ham":
133 *
134 * [metadata_cache.ham]
135 * host = metadata.example.com
136 *
137 */
138 const std::string cache_name_;
139
140 /** @brief Query part of the URI given as destination in the configuration
141 *
142 * For example, given following Metadata Cache configuration:
143 *
144 * [routing:metadata_read_only]
145 * ..
146 * destination =
147 * metadata_cache:///cluster_name/replicaset_name?role=PRIMARY_AND_SECONDARY
148 *
149 * The 'role' is part of uri_query_.
150 */
152
153 /** @brief Initializes
154 *
155 * This method initialized the object. It goes of the URI query information
156 * and sets members accordingly.
157 */
158 void init();
159
160 /** @brief Gets available destinations from Metadata Cache
161 *
162 * This method gets the destinations using Metadata Cache information. It uses
163 * the `metadata_cache::get_cluster_nodes()` function to get a list of current
164 * managed servers. Bool in the returned pair indicates if (in case of the
165 * round-robin-with-fallback routing strategy) the returned nodes are the
166 * primaries after the fallback (true), regular primaries (false) or
167 * secondaries (false).
168 *
169 */
170 std::pair<metadata_cache::cluster_nodes_list_t, bool> get_available(
172 bool for_new_connections = true) const;
173
175 const metadata_cache::cluster_nodes_list_t &managed_servers) const;
176
178 const metadata_cache::cluster_nodes_list_t &all_replicaset_nodes,
179 bool primary_fallback);
180
182
184
186
188
191
193 const metadata_cache::ClusterTopology &cluster_topology,
194 const bool md_servers_reachable);
198
200 const metadata_cache::ClusterTopology &cluster_topology,
201 const bool md_servers_reachable,
202 const uint64_t /*view_id*/) noexcept override;
203
205 const metadata_cache::cluster_nodes_list_t &instances) noexcept override;
206
207 void on_md_refresh(
208 const bool instances_changed,
209 const metadata_cache::ClusterTopology &cluster_topology) override;
210
211 // MUST take the RouteDestination Mutex
212 size_t start_pos_{};
215};
216
218 const mysqlrouter::URIQuery &uri);
219
220#endif // ROUTING_DEST_METADATA_CACHE_INCLUDED
Type
supported protocols
Definition: base_protocol.h:32
Definition: dest_metadata_cache.h:45
const std::string cache_name_
The Metadata Cache to use.
Definition: dest_metadata_cache.h:138
const mysqlrouter::URIQuery uri_query_
Query part of the URI given as destination in the configuration.
Definition: dest_metadata_cache.h:151
void advance(size_t n)
advance the current position in the destination by n.
Definition: dest_metadata_cache.cc:455
void subscribe_for_metadata_cache_changes()
Definition: dest_metadata_cache.cc:326
void add(const std::string &, uint16_t) override
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: dest_metadata_cache.h:72
Destinations primary_destinations()
Definition: dest_metadata_cache.cc:686
routing::RoutingStrategy get_strategy() override
Return our routing strategy.
Definition: dest_metadata_cache.h:124
void add(const mysql_harness::TCPAddress) override
Adds a destination.
Definition: dest_metadata_cache.h:73
std::optional< Destinations > refresh_destinations(const Destinations &dests) override
refresh destinations.
Definition: dest_metadata_cache.cc:394
Destinations destinations() override
get destinations to connect() to.
Definition: dest_metadata_cache.cc:676
~DestMetadataCacheGroup() override
Definition: dest_metadata_cache.cc:339
bool disconnect_on_promoted_to_primary_
Definition: dest_metadata_cache.h:189
void subscribe_for_acceptor_handler()
Definition: dest_metadata_cache.cc:331
metadata_cache::cluster_nodes_list_t get_available_primaries(const metadata_cache::cluster_nodes_list_t &managed_servers) const
Definition: dest_metadata_cache.cc:269
void on_md_refresh(const bool instances_changed, const metadata_cache::ClusterTopology &cluster_topology) override
Callback that is going to be used on each metadata refresh.
Definition: dest_metadata_cache.cc:788
void init()
Initializes.
Definition: dest_metadata_cache.cc:284
Destinations balance(const metadata_cache::cluster_nodes_list_t &all_replicaset_nodes, bool primary_fallback)
Definition: dest_metadata_cache.cc:461
void on_instances_change(const metadata_cache::ClusterTopology &cluster_topology, const bool md_servers_reachable)
Definition: dest_metadata_cache.cc:713
ServerRole server_role() const
Definition: dest_metadata_cache.h:105
std::pair< metadata_cache::cluster_nodes_list_t, bool > get_available(const metadata_cache::cluster_nodes_list_t &instances, bool for_new_connections=true) const
Gets available destinations from Metadata Cache.
Definition: dest_metadata_cache.cc:197
size_t start_pos_
Definition: dest_metadata_cache.h:212
bool subscribed_for_metadata_cache_changes_
Definition: dest_metadata_cache.h:187
DestMetadataCacheGroup(net::io_context &io_ctx_, const std::string &metadata_cache, const routing::RoutingStrategy routing_strategy, const mysqlrouter::URIQuery &query, const Protocol::Type protocol, metadata_cache::MetadataCacheAPIBase *cache_api=metadata_cache::MetadataCacheAPI::instance())
Constructor.
Definition: dest_metadata_cache.cc:177
metadata_cache::MetadataCacheAPIBase * cache_api()
Definition: dest_metadata_cache.h:108
bool update_socket_acceptor_state(const metadata_cache::cluster_nodes_list_t &instances) noexcept override
Callback function that is called when the state of the sockets acceptors is handled during the metada...
Definition: dest_metadata_cache.cc:765
void notify_instances_changed(const metadata_cache::ClusterTopology &cluster_topology, 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:759
ServerRole server_role_
Definition: dest_metadata_cache.h:183
AddrVector get_destinations() const override
Definition: dest_metadata_cache.cc:696
size_t rw_start_pos_
Definition: dest_metadata_cache.h:214
DestMetadataCacheGroup(DestMetadataCacheGroup &&)=delete
Move constructor.
DestMetadataCacheGroup(const DestMetadataCacheGroup &other)=delete
Copy constructor.
void handle_sockets_acceptors() override
Trigger listening socket acceptors state handler based on the destination type.
Definition: dest_metadata_cache.h:120
void subscribe_for_md_refresh_handler()
Definition: dest_metadata_cache.cc:335
void start(const mysql_harness::PluginFuncEnv *env) override
Start the destination.
Definition: dest_metadata_cache.cc:816
bool disconnect_on_metadata_unavailable_
Definition: dest_metadata_cache.h:190
DestMetadataCacheGroup & operator=(const DestMetadataCacheGroup &)=delete
Copy assignment.
ServerRole
Definition: dest_metadata_cache.h:47
@ PrimaryAndSecondary
Definition: dest_metadata_cache.h:47
@ Secondary
Definition: dest_metadata_cache.h:47
@ Primary
Definition: dest_metadata_cache.h:47
size_t ro_start_pos_
Definition: dest_metadata_cache.h:213
metadata_cache::MetadataCacheAPIBase * cache_api_
Definition: dest_metadata_cache.h:185
bool empty() const noexcept override
Returns whether there are destination servers.
Definition: dest_metadata_cache.h:86
mysqlrouter::ServerMode purpose() const override
Definition: dest_metadata_cache.h:99
DestMetadataCacheGroup & operator=(DestMetadataCacheGroup &&)=delete
Move assignment.
routing::RoutingStrategy routing_strategy_
Definition: dest_metadata_cache.h:181
A forward iterable container of destinations.
Definition: destination.h:107
Manage destinations for a Connection Routing.
Definition: destination.h:189
net::io_context & io_ctx_
Definition: destination.h:325
std::vector< mysql_harness::TCPAddress > AddrVector
Definition: destination.h:191
Abstract class that provides interface for listener on whether the listening sockets acceptors state ...
Definition: metadata_cache.h:115
Abstract class that provides interface for listener on cluster status changes.
Definition: metadata_cache.h:86
Definition: metadata_cache.h:226
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:143
PluginFuncEnv object.
Definition: loader.h:673
Defines an IP address with port number
Definition: tcp_address.h:40
Definition: io_context.h:61
ROUTING_EXPORT DestMetadataCacheGroup::ServerRole get_server_role_from_uri(const mysqlrouter::URIQuery &uri)
Definition: dest_metadata_cache.cc:74
static char * query
Definition: myisam_ftdump.cc:47
Defining the class MySQLRouting.
Definition: metadata_cache.h:53
std::vector< ManagedInstance > cluster_nodes_list_t
Definition: metadata_cache_datatypes.h:142
ServerMode
Definition: datatypes.h:50
std::map< std::string, std::string > URIQuery
Definition: uri.h:47
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:183
int n
Definition: xcom_base.cc:509