MySQL 9.5.0
Source Code Documentation
metadata_cache_datatypes.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2016, 2025, 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 MYSQLROUTER_METADATA_CACHE_DATATYPES_INCLUDED
27#define MYSQLROUTER_METADATA_CACHE_DATATYPES_INCLUDED
28
30
31#include <map>
32#include <optional>
33#include <string>
34#include <system_error>
35#include <vector>
36
38#include "mysqlrouter/cluster_metadata.h" // InstanceType
39#include "mysqlrouter/datatypes.h" // UserCredentials
40
41namespace metadata_cache {
42
43enum class metadata_errc {
44 ok,
53};
54} // namespace metadata_cache
55
56namespace std {
57template <>
58struct is_error_code_enum<metadata_cache::metadata_errc>
59 : public std::true_type {};
60} // namespace std
61
62namespace metadata_cache {
63inline const std::error_category &metadata_cache_category() noexcept {
64 class metadata_category_impl : public std::error_category {
65 public:
66 const char *name() const noexcept override { return "metadata cache"; }
67 std::string message(int ev) const override {
68 switch (static_cast<metadata_errc>(ev)) {
70 return "ok";
72 return "no metadata server accessible";
74 return "did not successfully read metadata from any metadata server";
76 return "metadata refresh terminated";
78 return "cluster not found in the metadata";
80 return "unexpected cluster type";
82 return "higher view_id seen";
84 return "metadata schema version not supported";
86 return "failed updating Replication Group status";
87 default:
88 return "unknown";
89 }
90 }
91 };
92
93 static metadata_category_impl instance;
94 return instance;
95}
96
97inline std::error_code make_error_code(metadata_errc e) noexcept {
98 return std::error_code(static_cast<int>(e), metadata_cache_category());
99}
100
103
104/** @class ManagedInstance
105 *
106 * Class ManagedInstance represents a server managed by the topology.
107 */
109 public:
111 const std::string &p_mysql_server_uuid,
112 const ServerMode p_mode, const ServerRole p_role,
113 const std::string &p_host, const uint16_t p_port,
114 const uint16_t p_xport, std::string label);
115
120
124 operator mysql_harness::TcpDestination() const;
125 bool operator==(const ManagedInstance &other) const;
126
127 /** @brief Instance type */
129 /** @brief The uuid of the MySQL server */
130 std::string mysql_server_uuid;
131 /** @brief The mode of the server */
132 ServerMode mode{ServerMode::Unavailable};
133 /** @brief The role of the server */
135 /** @brief The host name on which the server is running */
136 std::string host;
137 /** The port number in which the server is running */
138 uint16_t port{0};
139 /** The X protocol port number in which the server is running */
140 uint16_t xport{0};
141 /** Node atributes as a json string from metadata */
142 std::string attributes;
143 /** Should the node be hidden from the application to use it */
145 /** Should the Router disconnect existing client sessions to the node when it
146 * is hidden */
147 bool disconnect_existing_sessions_when_hidden{
149 /** Should the node be ignored for new and existing connections (for example
150 * due to the read_only_targets option) */
151 bool ignore{false};
152
153 /** Server tags as defined in the metadata, parsed as a key-value pairs */
154 std::map<std::string, std::string, std::less<>> tags{};
155
156 uint32_t version{0};
157
158 std::string label;
159
160 std::string to_string() const {
161 std::string result = "uuid: " + mysql_server_uuid + "\n";
162 result += "address: " + host + ":" + std::to_string(port) + "\n";
163
164 return result;
165 }
166
168 return {host, port};
169 }
170
172};
173
174using cluster_nodes_list_t = std::vector<ManagedInstance>;
175
177
178using metadata_servers_list_t = std::vector<metadata_server_t>;
179
180/** @class ManagedCluster
181 * Represents a cluster (a GR group or AR members)
182 */
184 public:
185 /** @brief UUID in the metadata */
186 std::string id;
187 /** @brief Name of the cluster */
188 std::string name;
189 /** @brief List of the members that belong to the cluster */
191 /** @brief Whether the cluster is in single_primary_mode (from PFS in case of
192 * GR) */
194
195 /** @brief Metadata for the cluster is not consistent (only applicable for
196 * the GR cluster when the data in the GR metadata is not consistent with the
197 * cluster metadata)*/
198 bool md_discrepancy{false};
199
200 bool has_quorum{true};
201
202 /** @brief Is this a PRIMARY Cluster in case of ClusterSet */
203 bool is_primary{true};
204 /** @brief Is the Cluster marked as invalid in the metadata */
205 bool is_invalidated{false};
206
207 bool empty() const noexcept { return members.empty(); }
208
209 void clear() noexcept { members.clear(); }
210};
211
212using clusters_list_t = std::vector<ManagedCluster>;
213
214/** @class ClusterTopology
215 * Represents a cluster (a GR group or AR members) and its metadata servers
216 */
218 using clusters_list_t = std::vector<ManagedCluster>;
219
221 // index of the target cluster in the clusters_data vector
222 std::optional<size_t> target_cluster_pos{};
224
225 /** @brief Id of the view this metadata represents (used for AR and
226 * ClusterSets)*/
227 uint64_t view_id{0};
228
229 /** @brief name of the ClusterSet or empty in case of standalone Cluster */
230 std::string name{};
231
232 // address of the writable metadata server that can be used for updating the
233 // metadata (router version, last_check_in), nullptr_t if not found
234 std::optional<metadata_cache::metadata_server_t> writable_server{};
235
238
239 for (const auto &cluster : clusters_data) {
240 result.insert(result.end(), cluster.members.begin(),
241 cluster.members.end());
242 }
243
244 return result;
245 }
246
248 for (auto &cluster : clusters_data) {
249 cluster.members.clear();
250 }
251 }
252};
253
254/**
255 * @brief Metadata MySQL session configuration
256 */
258 // User credentials used for the connecting to the metadata server.
260
261 // The time in seconds after which trying to connect to metadata server should
262 // time out.
264
265 // The time in seconds after which read from metadata server should time out.
267
268 // Numbers of retries used before giving up the attempt to connect to the
269 // metadata server (not used atm).
271};
272
275 std::string rw_classic_port;
276 std::string ro_classic_port;
278 std::string rw_x_port;
279 std::string ro_x_port;
280};
281
282} // namespace metadata_cache
283
284#endif
Represents a cluster (a GR group or AR members)
Definition: metadata_cache_datatypes.h:183
std::string id
UUID in the metadata.
Definition: metadata_cache_datatypes.h:186
cluster_nodes_list_t members
List of the members that belong to the cluster.
Definition: metadata_cache_datatypes.h:190
bool single_primary_mode
Whether the cluster is in single_primary_mode (from PFS in case of GR)
Definition: metadata_cache_datatypes.h:193
std::string name
Name of the cluster.
Definition: metadata_cache_datatypes.h:188
void clear() noexcept
Definition: metadata_cache_datatypes.h:209
bool empty() const noexcept
Definition: metadata_cache_datatypes.h:207
Class ManagedInstance represents a server managed by the topology.
Definition: metadata_cache_datatypes.h:108
std::string host
The host name on which the server is running.
Definition: metadata_cache_datatypes.h:136
mysql_harness::TcpDestination x_destination() const
Definition: metadata_cache_datatypes.h:171
std::string to_string() const
Definition: metadata_cache_datatypes.h:160
std::string label
Definition: metadata_cache_datatypes.h:158
ManagedInstance(ManagedInstance &&)=default
std::string mysql_server_uuid
The uuid of the MySQL server.
Definition: metadata_cache_datatypes.h:130
ManagedInstance & operator=(ManagedInstance &&)=default
mysqlrouter::InstanceType type
Instance type.
Definition: metadata_cache_datatypes.h:128
mysql_harness::TcpDestination classic_destination() const
Definition: metadata_cache_datatypes.h:167
ManagedInstance & operator=(const ManagedInstance &)=default
ManagedInstance(const ManagedInstance &)=default
std::string attributes
Node atributes as a json string from metadata.
Definition: metadata_cache_datatypes.h:142
Definition: destination.h:40
static std::optional< size_t > target_cluster_pos(const metadata_cache::ClusterTopology &topology, const std::string &target_cluster_id)
Definition: cluster_metadata_gr.cc:1372
static std::string to_string(const LEX_STRING &str)
Definition: lex_string.h:50
#define METADATA_CACHE_EXPORT
Definition: metadata_cache_export.h:15
const char * host
Definition: mysqladmin.cc:66
Definition: metadata_cache.h:49
std::vector< metadata_server_t > metadata_servers_list_t
Definition: metadata_cache_datatypes.h:178
std::vector< ManagedInstance > cluster_nodes_list_t
Definition: metadata_cache_datatypes.h:174
const std::error_category & metadata_cache_category() noexcept
Definition: metadata_cache_datatypes.h:63
metadata_errc
Definition: metadata_cache_datatypes.h:43
std::error_code make_error_code(metadata_errc e) noexcept
Definition: metadata_cache_datatypes.h:97
bool operator==(const metadata_cache::ManagedCluster &cluster_a, const metadata_cache::ManagedCluster &cluster_b)
Definition: metadata_cache.cc:295
ServerRole
Definition: metadata_cache_datatypes.h:102
std::vector< ManagedCluster > clusters_list_t
Definition: metadata_cache_datatypes.h:212
InstanceType
Definition: cluster_metadata.h:216
ServerMode
Definition: datatypes.h:50
constexpr const bool kNodeTagDisconnectWhenHiddenDefault
Definition: cluster_metadata.h:214
constexpr const bool kNodeTagHiddenDefault
Definition: cluster_metadata.h:213
Definition: gcs_xcom_synode.h:64
mode
Definition: file_handle.h:61
struct result result
Definition: result.h:34
required uint64 port
Definition: replication_asynchronous_connection_failover.proto:33
required uint64 version
Definition: replication_group_member_actions.proto:41
case opt name
Definition: sslopt-case.h:29
Represents a cluster (a GR group or AR members) and its metadata servers.
Definition: metadata_cache_datatypes.h:217
metadata_servers_list_t metadata_servers
Definition: metadata_cache_datatypes.h:223
std::vector< ManagedCluster > clusters_list_t
Definition: metadata_cache_datatypes.h:218
void clear_all_members()
Definition: metadata_cache_datatypes.h:247
clusters_list_t clusters_data
Definition: metadata_cache_datatypes.h:220
cluster_nodes_list_t get_all_members() const
Definition: metadata_cache_datatypes.h:236
Metadata MySQL session configuration.
Definition: metadata_cache_datatypes.h:257
int connection_attempts
Definition: metadata_cache_datatypes.h:270
int connect_timeout
Definition: metadata_cache_datatypes.h:263
mysqlrouter::UserCredentials user_credentials
Definition: metadata_cache_datatypes.h:259
int read_timeout
Definition: metadata_cache_datatypes.h:266
Definition: metadata_cache_datatypes.h:273
std::string rw_classic_port
Definition: metadata_cache_datatypes.h:275
std::string ro_classic_port
Definition: metadata_cache_datatypes.h:276
std::string metadata_user_name
Definition: metadata_cache_datatypes.h:274
std::string rw_x_port
Definition: metadata_cache_datatypes.h:278
std::string rw_split_classic_port
Definition: metadata_cache_datatypes.h:277
std::string ro_x_port
Definition: metadata_cache_datatypes.h:279
Definition: datatypes.h:45
Definition: result.h:30