MySQL 9.2.0
Source Code Documentation
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
cluster_metadata.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2016, 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 METADATA_CACHE_CLUSTER_METADATA_INCLUDED
27#define METADATA_CACHE_CLUSTER_METADATA_INCLUDED
28
30
37#include "router_options.h"
38
39#include <chrono>
40#include <memory>
41#include <optional>
42#include <string>
43#include <vector>
44
46
47namespace mysqlrouter {
48class MySQLSession;
49} // namespace mysqlrouter
50
51namespace xcl {
52class XSession;
53} // namespace xcl
54
56 std::function<bool(mysqlrouter::MySQLSession &connection,
58
59/** @class ClusterMetadata
60 *
61 * The `ClusterMetadata` class encapsulates a connection to the Metadata server.
62 * It uses the mysqlrouter::MySQLSession to setup, manage and retrieve results.
63 *
64 */
66 public:
67 /** @brief Constructor
68 *
69 * @param session_config Metadata MySQL session configuration
70 * @param ssl_options SSL related options to use for MySQL connections)
71 */
74 const mysqlrouter::SSLOptions &ssl_options);
75
76 // disable copy as it isn't needed right now. Feel free to enable
77 // must be explicitly defined though.
78 explicit ClusterMetadata(const ClusterMetadata &) = delete;
80
81 /** @brief Destructor
82 *
83 * Disconnect and release the connection to the metadata node.
84 */
85 ~ClusterMetadata() override;
86
87 /** @brief Connects with the Metadata server and sets up the session
88 * parameters
89 *
90 *
91 * @param metadata_server the server instance for which the connection
92 * should be attempted.
93 *
94 * @return a boolean to indicate if the connection and session parameters
95 * setup was successful.
96 */
98 &metadata_server) noexcept override;
99
100 /** @brief Disconnects from the Metadata server
101 *
102 */
103 void disconnect() noexcept override { metadata_connection_.reset(); }
104
105 /** @brief Gets the object representing the session to the metadata server
106 */
107 std::shared_ptr<mysqlrouter::MySQLSession> get_connection() override {
108 return metadata_connection_;
109 }
110
112 const metadata_cache::metadata_server_t &rw_server,
113 const unsigned router_id,
114 const metadata_cache::RouterAttributes &router_attributes) override;
115
117 const metadata_cache::metadata_server_t &rw_server,
118 const unsigned router_id) override;
119
120 void report_guideline_name(const std::string &guideline_name,
121 const metadata_cache::metadata_server_t &rw_server,
122 const unsigned router_id) override;
123
124 auth_credentials_t fetch_auth_credentials(
125 const metadata_cache::metadata_server_t &md_server,
126 const mysqlrouter::TargetCluster &target_cluster) override;
127
128 std::optional<routing_guidelines::Router_info> fetch_router_info(
129 const uint16_t router_id) override;
130
132 fetch_routing_guidelines_document(const uint16_t router_id) override;
133
134 std::optional<metadata_cache::metadata_server_t> find_rw_server(
135 const std::vector<metadata_cache::ManagedInstance> &instances);
136
137 std::optional<metadata_cache::metadata_server_t> find_rw_server(
138 const std::vector<metadata_cache::ManagedCluster> &clusters);
139
140 std::optional<std::chrono::seconds>
142 return router_options_.get_stats_updates_frequency();
143 }
144
145 protected:
146 /** Connects a MYSQL connection to the given instance
147 */
148 bool do_connect(mysqlrouter::MySQLSession &connection,
150
151 // throws metadata_cache::metadata_error and
152 // MetadataUpgradeInProgressException
153 mysqlrouter::MetadataSchemaVersion get_and_check_metadata_schema_version(
155
158 const mysqlrouter::MetadataSchemaVersion &schema_version,
159 const uint16_t router_id) {
161 return R"(SELECT guideline FROM
162mysql_innodb_cluster_metadata.routing_guidelines WHERE guideline_id = (
163 SELECT COALESCE(RO.router_options->>'$.guideline',
164 CS.router_options->>'$.guideline',
165 CL.router_options->>'$.guideline')
166 FROM
167 mysql_innodb_cluster_metadata.v2_router_options AS RO
168 LEFT JOIN
169 mysql_innodb_cluster_metadata.clustersets AS CS ON RO.clusterset_id = CS.clusterset_id
170 LEFT JOIN
171 mysql_innodb_cluster_metadata.clusters AS CL ON RO.cluster_id = CL.cluster_id
172 WHERE RO.router_id = )" +
173 std::to_string(router_id) + ")";
174 } else {
177 }
178 }
179
180 // Metadata node generic information
183
185
186#if 0 // not used so far
187 // The number of times we should try connecting to the metadata server if a
188 // connection attempt fails.
189 int connection_attempts_;
190#endif
191
192 // connection to metadata server (it may also be shared with GR status queries
193 // for optimisation purposes)
194 std::shared_ptr<mysqlrouter::MySQLSession> metadata_connection_;
195
197};
198
199std::string as_string(const char *input_str);
200
203 const size_t classic_port_column,
204 const size_t x_port_column);
205
207 const std::string &attributes);
208
209#endif // METADATA_CACHE_CLUSTER_METADATA_INCLUDED
The ClusterMetadata class encapsulates a connection to the Metadata server.
Definition: cluster_metadata.h:65
~ClusterMetadata() override
Destructor.
ClusterMetadata(const ClusterMetadata &)=delete
std::optional< std::chrono::seconds > get_periodic_stats_update_frequency() noexcept override
Definition: cluster_metadata.h:141
ClusterMetadata & operator=(const ClusterMetadata &)=delete
mysql_ssl_mode ssl_mode_
Definition: cluster_metadata.h:181
void disconnect() noexcept override
Disconnects from the Metadata server.
Definition: cluster_metadata.h:103
RouterOptions router_options_
Definition: cluster_metadata.h:196
mysqlrouter::SSLOptions ssl_options_
Definition: cluster_metadata.h:182
std::shared_ptr< mysqlrouter::MySQLSession > metadata_connection_
Definition: cluster_metadata.h:194
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:157
metadata_cache::MetadataCacheMySQLSessionConfig session_config_
Definition: cluster_metadata.h:184
std::shared_ptr< mysqlrouter::MySQLSession > get_connection() override
Gets the object representing the session to the metadata server.
Definition: cluster_metadata.h:107
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:104
Definition: destination.h:40
Definition: mysql_session.h:154
std::vector< const char * > Row
Definition: mysql_session.h:158
Definition: cluster_metadata.h:135
Definition: expected.h:286
void set_instance_attributes(metadata_cache::ManagedInstance &instance, const std::string &attributes)
Definition: cluster_metadata.cc:627
std::function< bool(mysqlrouter::MySQLSession &connection, const metadata_cache::ManagedInstance &mi)> ConnectCallback
Definition: cluster_metadata.h:57
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:195
#define METADATA_CACHE_EXPORT
Definition: metadata_cache_export.h:15
mysql_ssl_mode
Definition: mysql.h:272
static void do_connect(struct st_command *command)
Definition: mysqltest.cc:6704
std::error_code make_error_code(DynamicLoaderErrc ec)
make error_code from a DynamicLoaderErrc.
Definition: dynamic_loader.cc:79
Definition: base64.h:43
constexpr MetadataSchemaVersion kRoutingGuidelinesMetadataVersion
Definition: cluster_metadata.h:62
std::string ROUTER_CLUSTER_EXPORT to_string(const TargetCluster::InvalidatedClusterRoutingPolicy)
Definition: cluster_metadata.cc:1402
unexpected(E) -> unexpected< E >
Definition: cluster_metadata.h:51
Definition: group_replication_metadata.h:38
Metadata MySQL session configuration.
Definition: metadata_cache_datatypes.h:247
Definition: metadata_cache_datatypes.h:263
Definition: cluster_metadata.h:43
SSL connection related options.
Definition: datatypes.h:34