MySQL 26.10.0
Source Code Documentation
cluster_metadata.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2016, 2026, 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 METADATA_CACHE_CLUSTER_METADATA_INCLUDED
27#define METADATA_CACHE_CLUSTER_METADATA_INCLUDED
28
31
38#include "router_options.h"
39
40#include <chrono>
41#include <memory>
42#include <mutex>
43#include <optional>
44#include <string>
45#include <vector>
46
48
49namespace mysqlrouter {
50class MySQLSession;
51} // namespace mysqlrouter
52
53namespace xcl {
54class XSession;
55} // namespace xcl
56
58 std::function<bool(mysqlrouter::MySQLSession &connection,
60
61/** @class ClusterMetadata
62 *
63 * The `ClusterMetadata` class encapsulates a connection to the Metadata server.
64 * It uses the mysqlrouter::MySQLSession to setup, manage and retrieve results.
65 *
66 */
68 public:
69 /** @brief Constructor
70 *
71 * @param session_config Metadata MySQL session configuration
72 * @param ssl_options SSL related options to use for MySQL connections)
73 */
76 const mysqlrouter::SSLOptions &ssl_options);
77
78 // disable copy as it isn't needed right now. Feel free to enable
79 // must be explicitly defined though.
80 explicit ClusterMetadata(const ClusterMetadata &) = delete;
82
83 /** @brief Destructor
84 *
85 * Disconnect and release the connection to the metadata node.
86 */
87 ~ClusterMetadata() override;
88
89 /** Reapply the TLS material used for subsequently created sessions. */
90 void reload_tls();
91
92 /** @brief Connects with the Metadata server and sets up the session
93 * parameters
94 *
95 *
96 * @param metadata_server the server instance for which the connection
97 * should be attempted.
98 *
99 * @return a boolean to indicate if the connection and session parameters
100 * setup was successful.
101 */
103 &metadata_server) noexcept override;
104
105 /** @brief Disconnects from the Metadata server
106 *
107 */
108 void disconnect() noexcept override { metadata_connection_.reset(); }
109
110 /**
111 * check if the metadata_connection_ connection is connected to
112 * metadata_server and alive.
113 */
114 [[nodiscard]] bool is_connected_to(
115 const metadata_cache::metadata_server_t &metadata_server) const;
116
117 /**
118 * make a connection to metadata_server.
119 *
120 * if the metadata_connection_ already is connected to metadata_server, return
121 * that instead.
122 */
124 make_connection_shared(
125 const metadata_cache::metadata_server_t &metadata_server);
126
127 /** @brief Gets the object representing the session to the metadata server
128 */
129 std::shared_ptr<mysqlrouter::MySQLSession> get_connection() override {
130 return metadata_connection_;
131 }
132
134 const metadata_cache::metadata_server_t &rw_server,
135 const unsigned router_id,
136 const metadata_cache::RouterAttributes &router_attributes) override;
137
139 const metadata_cache::metadata_server_t &rw_server,
140 const unsigned router_id) override;
141
142 void report_guideline_name(const std::string &guideline_name,
143 const metadata_cache::metadata_server_t &rw_server,
144 const unsigned router_id) override;
145
146 auth_credentials_t fetch_auth_credentials(
147 const metadata_cache::metadata_server_t &md_server,
148 const mysqlrouter::TargetCluster &target_cluster) override;
149
150 std::optional<routing_guidelines::Router_info> fetch_router_info(
151 const uint16_t router_id) override;
152
154 fetch_routing_guidelines_document(const uint16_t router_id) override;
155
156 std::optional<metadata_cache::metadata_server_t> find_rw_server(
157 const std::vector<metadata_cache::ManagedInstance> &instances);
158
159 std::optional<metadata_cache::metadata_server_t> find_rw_server(
160 const std::vector<metadata_cache::ManagedCluster> &clusters);
161
162 std::optional<std::chrono::seconds>
164 return router_options_.get_stats_updates_frequency();
165 }
166
167 protected:
168 /** Connects a MYSQL connection to the given instance
169 */
170 bool do_connect(mysqlrouter::MySQLSession &connection,
172
173 // throws metadata_cache::metadata_error and
174 // MetadataUpgradeInProgressException
175 mysqlrouter::MetadataSchemaVersion get_and_check_metadata_schema_version(
177
180 const mysqlrouter::MetadataSchemaVersion &schema_version,
181 const uint16_t router_id) {
183 return R"(SELECT guideline FROM
184mysql_innodb_cluster_metadata.routing_guidelines WHERE guideline_id = (
185 SELECT COALESCE(RO.router_options->>'$.guideline',
186 CS.router_options->>'$.guideline',
187 CL.router_options->>'$.guideline')
188 FROM
189 mysql_innodb_cluster_metadata.v2_router_options AS RO
190 LEFT JOIN
191 mysql_innodb_cluster_metadata.clustersets AS CS ON RO.clusterset_id = CS.clusterset_id
192 LEFT JOIN
193 mysql_innodb_cluster_metadata.clusters AS CL ON RO.cluster_id = CL.cluster_id
194 WHERE RO.router_id = )" +
195 std::to_string(router_id) + ")";
196 } else {
199 }
200 }
201
202 // Metadata node generic information
204 void validate_tls_options(const mysqlrouter::SSLOptions &ssl_options) const;
205
206 // Never changes after startup. A reload validates these original paths and
207 // publishes the resulting effective options for new metadata connections.
209 mutable std::mutex ssl_options_m_;
210
212
213#if 0 // not used so far
214 // The number of times we should try connecting to the metadata server if a
215 // connection attempt fails.
216 int connection_attempts_;
217#endif
218
219 // connection to metadata server (it may also be shared with GR status queries
220 // for optimisation purposes)
221 std::shared_ptr<mysqlrouter::MySQLSession> metadata_connection_;
222
224};
225
226std::string as_string(const char *input_str);
227
230 const size_t classic_port_column,
231 const size_t x_port_column);
232
234 const std::string &attributes);
235
236#endif // METADATA_CACHE_CLUSTER_METADATA_INCLUDED
The ClusterMetadata class encapsulates a connection to the Metadata server.
Definition: cluster_metadata.h:67
~ClusterMetadata() override
Destructor.
ClusterMetadata(const ClusterMetadata &)=delete
std::optional< std::chrono::seconds > get_periodic_stats_update_frequency() noexcept override
Definition: cluster_metadata.h:163
ClusterMetadata & operator=(const ClusterMetadata &)=delete
mysql_ssl_mode ssl_mode_
Definition: cluster_metadata.h:203
void disconnect() noexcept override
Disconnects from the Metadata server.
Definition: cluster_metadata.h:108
RouterOptions router_options_
Definition: cluster_metadata.h:223
mysqlrouter::SSLOptions ssl_options_
Definition: cluster_metadata.h:208
std::shared_ptr< mysqlrouter::MySQLSession > metadata_connection_
Definition: cluster_metadata.h:221
std::mutex ssl_options_m_
Definition: cluster_metadata.h:209
stdx::expected< std::string, std::error_code > get_select_routing_guidelines_query(const mysqlrouter::MetadataSchemaVersion &schema_version, const uint16_t router_id)
Definition: cluster_metadata.h:179
metadata_cache::MetadataCacheMySQLSessionConfig session_config_
Definition: cluster_metadata.h:211
std::shared_ptr< mysqlrouter::MySQLSession > get_connection() override
Gets the object representing the session to the metadata server.
Definition: cluster_metadata.h:129
The metadata class is used to create a pluggable transport layer from which the metadata is fetched f...
Definition: metadata.h:52
virtual bool update_router_last_check_in(const metadata_cache::metadata_server_t &rw_server, const unsigned router_id)=0
virtual bool connect_and_setup_session(const metadata_cache::metadata_server_t &metadata_server)=0
virtual std::optional< routing_guidelines::Router_info > fetch_router_info(const uint16_t router_id)=0
virtual bool update_router_attributes(const metadata_cache::metadata_server_t &rw_server, const unsigned router_id, const metadata_cache::RouterAttributes &router_attributes)=0
virtual auth_credentials_t fetch_auth_credentials(const metadata_cache::metadata_server_t &md_server, const mysqlrouter::TargetCluster &target_cluster)=0
virtual stdx::expected< std::string, std::error_code > fetch_routing_guidelines_document(const uint16_t router_id)=0
virtual void report_guideline_name(const std::string &guideline_name, const metadata_cache::metadata_server_t &rw_server, const unsigned router_id)=0
Represents the Router options in v2_routers view in the metadata schema.
Definition: router_options.h:58
Class ManagedInstance represents a server managed by the topology.
Definition: metadata_cache_datatypes.h:108
Definition: destination.h:40
Definition: mysql_session.h:157
std::vector< const char * > Row
Definition: mysql_session.h:161
Definition: cluster_metadata.h:179
Definition: expected.h:286
void set_instance_attributes(metadata_cache::ManagedInstance &instance, const std::string &attributes)
Definition: cluster_metadata.cc:676
std::function< bool(mysqlrouter::MySQLSession &connection, const metadata_cache::ManagedInstance &mi)> ConnectCallback
Definition: cluster_metadata.h:59
std::string as_string(const char *input_str)
Return a string representation of the input character string.
Definition: cluster_metadata.cc:68
bool set_instance_ports(metadata_cache::ManagedInstance &instance, const mysqlrouter::MySQLSession::Row &row, const size_t classic_port_column, const size_t x_port_column)
Definition: cluster_metadata.cc:258
#define METADATA_CACHE_EXPORT
Definition: metadata_cache_export.h:15
mysql_ssl_mode
Definition: mysql.h:275
static void do_connect(struct st_command *command)
Definition: mysqltest.cc:6858
noexcept
The return type for any call_and_catch(f, args...) call where f(args...) returns Type.
Definition: call_and_catch.h:76
std::error_code make_error_code(DynamicLoaderErrc ec)
make error_code from a DynamicLoaderErrc.
Definition: dynamic_loader.cc:97
Definition: mrs_component_embedded_mysql_session.h:29
constexpr MetadataSchemaVersion kRoutingGuidelinesMetadataVersion
Definition: cluster_metadata.h:100
std::string ROUTER_CLUSTER_EXPORT to_string(const TargetCluster::InvalidatedClusterRoutingPolicy)
Definition: cluster_metadata.cc:1425
unexpected(E) -> unexpected< E >
Definition: cluster_metadata.h:53
Definition: group_replication_metadata.h:38
Metadata MySQL session configuration.
Definition: metadata_cache_datatypes.h:257
Definition: metadata_cache_datatypes.h:273
Definition: cluster_metadata.h:45
SSL connection related options.
Definition: datatypes.h:34