MySQL 8.3.0
Source Code Documentation
dest_metadata_cache.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2015, 2023, 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 also distributed 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 included with MySQL.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23*/
24
25#ifndef ROUTING_DEST_METADATA_CACHE_INCLUDED
26#define ROUTING_DEST_METADATA_CACHE_INCLUDED
27
28#include <system_error>
29#include <thread>
30
31#include "destination.h"
33#include "mysql_routing.h"
37#include "mysqlrouter/uri.h"
38#include "tcp_address.h"
39
41 : public RouteDestination,
45 public:
47
48 /** @brief Constructor */
50 const std::string &metadata_cache,
51 const routing::RoutingStrategy routing_strategy,
53 const Protocol::Type protocol,
54 const routing::Mode mode = routing::Mode::kUndefined,
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
101 // get cache-api
103
104 std::optional<Destinations> refresh_destinations(
105 const Destinations &dests) override;
106
108
109 /**
110 * advance the current position in the destination by n.
111 */
112 void advance(size_t n);
113
114 void handle_sockets_acceptors() override {
116 }
117
119
120 private:
121 /** @brief The Metadata Cache to use
122 *
123 * cache_name_ is the the section key in the configuration of Metadata Cache.
124 *
125 * For example, given following Metadata Cache configuration, cache_name_ will
126 * be set to "ham":
127 *
128 * [metadata_cache.ham]
129 * host = metadata.example.com
130 *
131 */
132 const std::string cache_name_;
133
134 /** @brief Query part of the URI given as destination in the configuration
135 *
136 * For example, given following Metadata Cache configuration:
137 *
138 * [routing:metadata_read_only]
139 * ..
140 * destination =
141 * metadata_cache:///cluster_name/replicaset_name?allow_primary_reads=yes
142 *
143 * The 'allow_primary_reads' is part of uri_query_.
144 */
146
147 /** @brief Initializes
148 *
149 * This method initialized the object. It goes of the URI query information
150 * and sets members accordingly.
151 */
152 void init();
153
154 /** @brief Gets available destinations from Metadata Cache
155 *
156 * This method gets the destinations using Metadata Cache information. It uses
157 * the `metadata_cache::get_cluster_nodes()` function to get a list of current
158 * managed servers. Bool in the returned pair indicates if (in case of the
159 * round-robin-with-fallback routing strategy) the returned nodes are the
160 * primaries after the fallback (true), regular primaries (false) or
161 * secondaries (false).
162 *
163 */
164 std::pair<metadata_cache::cluster_nodes_list_t, bool> get_available(
166 bool for_new_connections = true) const;
167
169 const metadata_cache::cluster_nodes_list_t &managed_servers) const;
170
172 const metadata_cache::cluster_nodes_list_t &all_replicaset_nodes,
173 bool primary_fallback);
174
176
178
180
182
184
187
189 const metadata_cache::ClusterTopology &cluster_topology,
190 const bool md_servers_reachable);
194
196 const metadata_cache::ClusterTopology &cluster_topology,
197 const bool md_servers_reachable,
198 const uint64_t /*view_id*/) noexcept override;
199
201 const metadata_cache::cluster_nodes_list_t &instances) noexcept override;
202
203 void on_md_refresh(
204 const bool instances_changed,
205 const metadata_cache::ClusterTopology &cluster_topology) override;
206
207 // MUST take the RouteDestination Mutex
208 size_t start_pos_{};
211};
212
214 const mysqlrouter::URIQuery &uri);
215
216#endif // ROUTING_DEST_METADATA_CACHE_INCLUDED
Type
supported protocols
Definition: base_protocol.h:31
Definition: dest_metadata_cache.h:44
const std::string cache_name_
The Metadata Cache to use.
Definition: dest_metadata_cache.h:132
const mysqlrouter::URIQuery uri_query_
Query part of the URI given as destination in the configuration.
Definition: dest_metadata_cache.h:145
void advance(size_t n)
advance the current position in the destination by n.
Definition: dest_metadata_cache.cc:532
void subscribe_for_metadata_cache_changes()
Definition: dest_metadata_cache.cc:403
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:763
routing::RoutingStrategy get_strategy() override
Return our routing strategy.
Definition: dest_metadata_cache.h:118
routing::Mode mode_
Definition: dest_metadata_cache.h:177
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:471
Destinations destinations() override
get destinations to connect() to.
Definition: dest_metadata_cache.cc:753
~DestMetadataCacheGroup() override
Definition: dest_metadata_cache.cc:416
bool disconnect_on_promoted_to_primary_
Definition: dest_metadata_cache.h:185
void subscribe_for_acceptor_handler()
Definition: dest_metadata_cache.cc:408
metadata_cache::cluster_nodes_list_t get_available_primaries(const metadata_cache::cluster_nodes_list_t &managed_servers) const
Definition: dest_metadata_cache.cc:304
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:865
void init()
Initializes.
Definition: dest_metadata_cache.cc:319
Destinations balance(const metadata_cache::cluster_nodes_list_t &all_replicaset_nodes, bool primary_fallback)
Definition: dest_metadata_cache.cc:538
void on_instances_change(const metadata_cache::ClusterTopology &cluster_topology, const bool md_servers_reachable)
Definition: dest_metadata_cache.cc:790
ServerRole server_role() const
Definition: dest_metadata_cache.h:99
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:232
size_t start_pos_
Definition: dest_metadata_cache.h:208
DestMetadataCacheGroup(net::io_context &io_ctx_, const std::string &metadata_cache, const routing::RoutingStrategy routing_strategy, const mysqlrouter::URIQuery &query, const Protocol::Type protocol, const routing::Mode mode=routing::Mode::kUndefined, metadata_cache::MetadataCacheAPIBase *cache_api=metadata_cache::MetadataCacheAPI::instance())
Constructor.
Definition: dest_metadata_cache.cc:211
bool subscribed_for_metadata_cache_changes_
Definition: dest_metadata_cache.h:183
metadata_cache::MetadataCacheAPIBase * cache_api()
Definition: dest_metadata_cache.h:102
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:842
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:836
ServerRole server_role_
Definition: dest_metadata_cache.h:179
AddrVector get_destinations() const override
Definition: dest_metadata_cache.cc:773
size_t rw_start_pos_
Definition: dest_metadata_cache.h:210
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:114
void subscribe_for_md_refresh_handler()
Definition: dest_metadata_cache.cc:412
void start(const mysql_harness::PluginFuncEnv *env) override
Start the destination.
Definition: dest_metadata_cache.cc:893
bool disconnect_on_metadata_unavailable_
Definition: dest_metadata_cache.h:186
DestMetadataCacheGroup & operator=(const DestMetadataCacheGroup &)=delete
Copy assignment.
ServerRole
Definition: dest_metadata_cache.h:46
@ PrimaryAndSecondary
Definition: dest_metadata_cache.h:46
@ Secondary
Definition: dest_metadata_cache.h:46
@ Primary
Definition: dest_metadata_cache.h:46
size_t ro_start_pos_
Definition: dest_metadata_cache.h:209
metadata_cache::MetadataCacheAPIBase * cache_api_
Definition: dest_metadata_cache.h:181
bool empty() const noexcept override
Returns whether there are destination servers.
Definition: dest_metadata_cache.h:86
DestMetadataCacheGroup & operator=(DestMetadataCacheGroup &&)=delete
Move assignment.
routing::RoutingStrategy routing_strategy_
Definition: dest_metadata_cache.h:175
A forward iterable container of destinations.
Definition: destination.h:106
Manage destinations for a Connection Routing.
Definition: destination.h:187
net::io_context & io_ctx_
Definition: destination.h:319
std::vector< mysql_harness::TCPAddress > AddrVector
Definition: destination.h:189
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:48
Abstract class that provides interface for listener on metadata refresh.
Definition: metadata_cache.h:143
PluginFuncEnv object.
Definition: loader.h:672
Defines an IP address with port number
Definition: tcp_address.h:39
Definition: io_context.h:60
ROUTING_EXPORT DestMetadataCacheGroup::ServerRole get_server_role_from_uri(const mysqlrouter::URIQuery &uri)
Definition: dest_metadata_cache.cc:71
static char * query
Definition: myisam_ftdump.cc:46
Defining the class MySQLRouting.
Definition: metadata_cache.h:52
std::vector< ManagedInstance > cluster_nodes_list_t
Definition: metadata_cache_datatypes.h:141
std::map< std::string, std::string > URIQuery
Definition: uri.h:46
RoutingStrategy
Routing strategies supported by Routing plugin.
Definition: routing.h:152
RoutingMode
Modes supported by Routing plugin.
Definition: routing.h:133
mode
Definition: file_handle.h:59
#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:182
int n
Definition: xcom_base.cc:508