MySQL 8.4.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
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?role=PRIMARY_AND_SECONDARY
142 *
143 * The 'role' 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
185
187 const metadata_cache::ClusterTopology &cluster_topology,
188 const bool md_servers_reachable);
192
194 const metadata_cache::ClusterTopology &cluster_topology,
195 const bool md_servers_reachable,
196 const uint64_t /*view_id*/) noexcept override;
197
199 const metadata_cache::cluster_nodes_list_t &instances) noexcept override;
200
201 void on_md_refresh(
202 const bool instances_changed,
203 const metadata_cache::ClusterTopology &cluster_topology) override;
204
205 // MUST take the RouteDestination Mutex
206 size_t start_pos_{};
209};
210
212 const mysqlrouter::URIQuery &uri);
213
214#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: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: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:118
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:183
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: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:197
size_t start_pos_
Definition: dest_metadata_cache.h:206
bool subscribed_for_metadata_cache_changes_
Definition: dest_metadata_cache.h:181
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: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: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:177
AddrVector get_destinations() const override
Definition: dest_metadata_cache.cc:696
size_t rw_start_pos_
Definition: dest_metadata_cache.h:208
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: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:184
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:207
metadata_cache::MetadataCacheAPIBase * cache_api_
Definition: dest_metadata_cache.h:179
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:107
Manage destinations for a Connection Routing.
Definition: destination.h:188
net::io_context & io_ctx_
Definition: destination.h:320
std::vector< mysql_harness::TCPAddress > AddrVector
Definition: destination.h:190
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
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