MySQL 8.0.40
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"
37#include "mysqlrouter/uri.h"
38#include "tcp_address.h"
39
41 : public RouteDestination,
45 public:
47
48 /** @brief Constructor */
50 net::io_context &io_ctx_, const std::string &metadata_cache,
51 const routing::RoutingStrategy routing_strategy,
52 const mysqlrouter::URIQuery &query, const Protocol::Type protocol,
56
57 /** @brief Copy constructor */
59
60 /** @brief Move constructor */
62
63 /** @brief Copy assignment */
65
66 /** @brief Move assignment */
68
69 ~DestMetadataCacheGroup() override;
70
71 void add(const std::string &, uint16_t) override {}
72 void add(const mysql_harness::TCPAddress) override {}
73
74 AddrVector get_destinations() const override;
75
76 /** @brief Returns whether there are destination servers
77 *
78 * The empty() method always returns false for Metadata Cache.
79 *
80 * Checking whether the Metadata Cache is empty for given destination
81 * might be to expensive. We leave this to the get_server() method.
82 *
83 * @return Always returns False for Metadata Cache destination.
84 */
85 bool empty() const noexcept override { return false; }
86
87 /** @brief Start the destination
88 *
89 * It also overwrites parent class' RouteDestination::start(), which launches
90 * Quarantine. For Metadata Cache routing, we don't need it.
91 *
92 * @param env pointer to the PluginFuncEnv object
93 */
94 void start(const mysql_harness::PluginFuncEnv *env) override;
95
96 Destinations destinations() override;
97
99
100 // get cache-api
102
103 std::optional<Destinations> refresh_destinations(
104 const Destinations &dests) override;
105
107
108 /**
109 * advance the current position in the destination by n.
110 */
111 void advance(size_t n);
112
113 void handle_sockets_acceptors() override {
115 }
116
118
119 private:
120 /** @brief The Metadata Cache to use
121 *
122 * cache_name_ is the the section key in the configuration of Metadata Cache.
123 *
124 * For example, given following Metadata Cache configuration, cache_name_ will
125 * be set to "ham":
126 *
127 * [metadata_cache.ham]
128 * host = metadata.example.com
129 *
130 */
131 const std::string cache_name_;
132
133 /** @brief Query part of the URI given as destination in the configuration
134 *
135 * For example, given following Metadata Cache configuration:
136 *
137 * [routing:metadata_read_only]
138 * ..
139 * destination =
140 * metadata_cache:///cluster_name/replicaset_name?allow_primary_reads=yes
141 *
142 * The 'allow_primary_reads' is part of uri_query_.
143 */
145
146 /** @brief Initializes
147 *
148 * This method initialized the object. It goes of the URI query information
149 * and sets members accordingly.
150 */
151 void init();
152
153 /** @brief Gets available destinations from Metadata Cache
154 *
155 * This method gets the destinations using Metadata Cache information. It uses
156 * the `metadata_cache::get_cluster_nodes()` function to get a list of current
157 * managed servers. Bool in the returned pair indicates if (in case of the
158 * round-robin-with-fallback routing strategy) the returned nodes are the
159 * primaries after the fallback (true), regular primaries (false) or
160 * secondaries (false).
161 *
162 */
163 std::pair<AllowedNodes, bool> get_available(
165 bool for_new_connections = true) const;
166
168 const metadata_cache::cluster_nodes_list_t &managed_servers) const;
169
170 Destinations balance(const AllowedNodes &all_replicaset_nodes,
171 bool primary_fallback);
172
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_{};
207};
208
209#endif // ROUTING_DEST_METADATA_CACHE_INCLUDED
Type
supported protocols
Definition: base_protocol.h:32
Definition: dest_metadata_cache.h:44
const std::string cache_name_
The Metadata Cache to use.
Definition: dest_metadata_cache.h:131
const mysqlrouter::URIQuery uri_query_
Query part of the URI given as destination in the configuration.
Definition: dest_metadata_cache.h:144
void advance(size_t n)
advance the current position in the destination by n.
Definition: dest_metadata_cache.cc:529
void subscribe_for_metadata_cache_changes()
Definition: dest_metadata_cache.cc:406
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::AccessMode access_mode=routing::AccessMode::kUndefined, metadata_cache::MetadataCacheAPIBase *cache_api=metadata_cache::MetadataCacheAPI::instance())
Constructor.
Definition: dest_metadata_cache.cc:210
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:71
Destinations primary_destinations()
Definition: dest_metadata_cache.cc:643
routing::RoutingStrategy get_strategy() override
Return our routing strategy.
Definition: dest_metadata_cache.h:117
void add(const mysql_harness::TCPAddress) override
Adds a destination.
Definition: dest_metadata_cache.h:72
std::optional< Destinations > refresh_destinations(const Destinations &dests) override
refresh destinations.
Definition: dest_metadata_cache.cc:468
Destinations destinations() override
get destinations to connect() to.
Definition: dest_metadata_cache.cc:631
~DestMetadataCacheGroup() override
Definition: dest_metadata_cache.cc:419
bool disconnect_on_promoted_to_primary_
Definition: dest_metadata_cache.h:183
void subscribe_for_acceptor_handler()
Definition: dest_metadata_cache.cc:411
routing::AccessMode access_mode_
Definition: dest_metadata_cache.h:175
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:729
void init()
Initializes.
Definition: dest_metadata_cache.cc:322
void on_instances_change(const metadata_cache::ClusterTopology &cluster_topology, const bool md_servers_reachable)
Definition: dest_metadata_cache.cc:668
ServerRole server_role() const
Definition: dest_metadata_cache.h:98
size_t start_pos_
Definition: dest_metadata_cache.h:206
std::pair< AllowedNodes, 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:231
bool subscribed_for_metadata_cache_changes_
Definition: dest_metadata_cache.h:181
metadata_cache::MetadataCacheAPIBase * cache_api()
Definition: dest_metadata_cache.h:101
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:706
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:700
ServerRole server_role_
Definition: dest_metadata_cache.h:177
AddrVector get_destinations() const override
Definition: dest_metadata_cache.cc:653
Destinations balance(const AllowedNodes &all_replicaset_nodes, bool primary_fallback)
Definition: dest_metadata_cache.cc:535
DestMetadataCacheGroup(DestMetadataCacheGroup &&)=delete
Move constructor.
DestMetadataCacheGroup(const DestMetadataCacheGroup &other)=delete
Copy constructor.
AllowedNodes get_available_primaries(const metadata_cache::cluster_nodes_list_t &managed_servers) const
Definition: dest_metadata_cache.cc:304
void handle_sockets_acceptors() override
Trigger listening socket acceptors state handler based on the destination type.
Definition: dest_metadata_cache.h:113
void subscribe_for_md_refresh_handler()
Definition: dest_metadata_cache.cc:415
void start(const mysql_harness::PluginFuncEnv *env) override
Start the destination.
Definition: dest_metadata_cache.cc:740
bool disconnect_on_metadata_unavailable_
Definition: dest_metadata_cache.h:184
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
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:85
DestMetadataCacheGroup & operator=(DestMetadataCacheGroup &&)=delete
Move assignment.
routing::RoutingStrategy routing_strategy_
Definition: dest_metadata_cache.h:173
A forward iterable container of destinations.
Definition: destination.h:97
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:120
Abstract class that provides interface for listener on cluster status changes.
Definition: metadata_cache.h:91
Definition: metadata_cache.h:231
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:148
PluginFuncEnv object.
Definition: loader.h:673
Defines an IP address with port number
Definition: tcp_address.h:40
Definition: io_context.h:61
std::vector< AvailableDestination > AllowedNodes
Definition: destination_status_types.h:59
static char * query
Definition: myisam_ftdump.cc:45
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
AccessMode
Modes supported by Routing plugin.
Definition: routing.h:104
RoutingStrategy
Routing strategies supported by Routing plugin.
Definition: routing.h:111
Represents a cluster (a GR group or AR members) and its metadata servers.
Definition: metadata_cache_datatypes.h:181
int n
Definition: xcom_base.cc:509