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