MySQL 9.0.0
Source Code Documentation
metadata_cache_datatypes.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 MYSQLROUTER_METADATA_CACHE_DATATYPES_INCLUDED
27#define MYSQLROUTER_METADATA_CACHE_DATATYPES_INCLUDED
28
30
31#include <algorithm>
32#include <optional>
33#include <string>
34#include <system_error>
35#include <vector>
36
37#include "mysqlrouter/datatypes.h" // UserCredentials
38#include "tcp_address.h"
39
40namespace metadata_cache {
41
42enum class metadata_errc {
43 ok,
50};
51} // namespace metadata_cache
52
53namespace std {
54template <>
55struct is_error_code_enum<metadata_cache::metadata_errc>
56 : public std::true_type {};
57} // namespace std
58
59namespace metadata_cache {
60inline const std::error_category &metadata_cache_category() noexcept {
61 class metadata_category_impl : public std::error_category {
62 public:
63 const char *name() const noexcept override { return "metadata cache"; }
64 std::string message(int ev) const override {
65 switch (static_cast<metadata_errc>(ev)) {
67 return "ok";
69 return "no metadata server accessible";
71 return "did not successfully read metadata from any metadata server";
73 return "metadata refresh terminated";
75 return "cluster not found in the metadata";
77 return "unexpected cluster type";
79 return "highier view_id seen";
80 default:
81 return "unknown";
82 }
83 }
84 };
85
86 static metadata_category_impl instance;
87 return instance;
88}
89
90inline std::error_code make_error_code(metadata_errc e) noexcept {
91 return std::error_code(static_cast<int>(e), metadata_cache_category());
92}
93
96
97/** @class ManagedInstance
98 *
99 * Class ManagedInstance represents a server managed by the topology.
100 */
102 public:
104 const std::string &p_mysql_server_uuid,
105 const ServerMode p_mode, const ServerRole p_role,
106 const std::string &p_host, const uint16_t p_port,
107 const uint16_t p_xport);
108
112 const TCPAddress &addr);
113 operator TCPAddress() const;
114 bool operator==(const ManagedInstance &other) const;
115
116 /** @brief Instance type */
118 /** @brief The uuid of the MySQL server */
119 std::string mysql_server_uuid;
120 /** @brief The mode of the server */
121 ServerMode mode{ServerMode::Unavailable};
122 /** @brief The role of the server */
124 /** @brief The host name on which the server is running */
125 std::string host;
126 /** The port number in which the server is running */
127 uint16_t port{0};
128 /** The X protocol port number in which the server is running */
129 uint16_t xport{0};
130 /** Node atributes as a json string from metadata */
131 std::string attributes;
132 /** Should the node be hidden from the application to use it */
133 bool hidden;
134 /** Should the Router disconnect existing client sessions to the node when it
135 * is hidden */
137 /** Should the node be ignored for new and existing connections (for example
138 * due to the read_only_targets option) */
139 bool ignore{false};
140};
141
142using cluster_nodes_list_t = std::vector<ManagedInstance>;
143
145
146using metadata_servers_list_t = std::vector<metadata_server_t>;
147
148/** @class ManagedCluster
149 * Represents a cluster (a GR group or AR members)
150 */
152 public:
153 /** @brief UUID in the metadata */
154 std::string id;
155 /** @brief Name of the cluster */
156 std::string name;
157 /** @brief List of the members that belong to the cluster */
159 /** @brief Whether the cluster is in single_primary_mode (from PFS in case of
160 * GR) */
162
163 /** @brief Metadata for the cluster is not consistent (only applicable for
164 * the GR cluster when the data in the GR metadata is not consistent with the
165 * cluster metadata)*/
166 bool md_discrepancy{false};
167
168 bool has_quorum{true};
169
170 /** @brief Is this a PRIMARY Cluster in case of ClusterSet */
171 bool is_primary{true};
172 /** @brief Is the Cluster marked as invalid in the metadata */
173 bool is_invalidated{false};
174
175 bool empty() const noexcept { return members.empty(); }
176
177 void clear() noexcept { members.clear(); }
178};
179
180/** @class ClusterTopology
181 * Represents a cluster (a GR group or AR members) and its metadata servers
182 */
184 using clusters_list_t = std::vector<ManagedCluster>;
185
187 // index of the target cluster in the clusters_data vector
188 std::optional<size_t> target_cluster_pos{};
190
191 /** @brief Id of the view this metadata represents (used for AR and
192 * ClusterSets)*/
193 uint64_t view_id{0};
194
195 /** @brief name of the ClusterSet or empty in case of standalone Cluster */
196 std::string name{};
197
198 // address of the writable metadata server that can be used for updating the
199 // metadata (router version, last_check_in), nullptr_t if not found
200 std::optional<metadata_cache::metadata_server_t> writable_server{};
201
204
205 for (const auto &cluster : clusters_data) {
206 result.insert(result.end(), cluster.members.begin(),
207 cluster.members.end());
208 }
209
210 return result;
211 }
212
214 for (auto &cluster : clusters_data) {
215 cluster.members.clear();
216 }
217 }
218};
219
220/**
221 * @brief Metadata MySQL session configuration
222 */
224 // User credentials used for the connecting to the metadata server.
226
227 // The time in seconds after which trying to connect to metadata server should
228 // time out.
230
231 // The time in seconds after which read from metadata server should time out.
233
234 // Numbers of retries used before giving up the attempt to connect to the
235 // metadata server (not used atm).
237};
238
241 std::string rw_classic_port;
242 std::string ro_classic_port;
244 std::string rw_x_port;
245 std::string ro_x_port;
246};
247
248} // namespace metadata_cache
249
250#endif
Represents a cluster (a GR group or AR members)
Definition: metadata_cache_datatypes.h:151
std::string id
UUID in the metadata.
Definition: metadata_cache_datatypes.h:154
cluster_nodes_list_t members
List of the members that belong to the cluster.
Definition: metadata_cache_datatypes.h:158
bool single_primary_mode
Whether the cluster is in single_primary_mode (from PFS in case of GR)
Definition: metadata_cache_datatypes.h:161
std::string name
Name of the cluster.
Definition: metadata_cache_datatypes.h:156
void clear() noexcept
Definition: metadata_cache_datatypes.h:177
bool empty() const noexcept
Definition: metadata_cache_datatypes.h:175
Class ManagedInstance represents a server managed by the topology.
Definition: metadata_cache_datatypes.h:101
std::string host
The host name on which the server is running.
Definition: metadata_cache_datatypes.h:125
bool disconnect_existing_sessions_when_hidden
Should the Router disconnect existing client sessions to the node when it is hidden.
Definition: metadata_cache_datatypes.h:136
std::string mysql_server_uuid
The uuid of the MySQL server.
Definition: metadata_cache_datatypes.h:119
mysqlrouter::InstanceType type
Instance type.
Definition: metadata_cache_datatypes.h:117
bool hidden
Should the node be hidden from the application to use it.
Definition: metadata_cache_datatypes.h:133
std::string attributes
Node atributes as a json string from metadata.
Definition: metadata_cache_datatypes.h:131
Defines an IP address with port number
Definition: tcp_address.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:1261
#define METADATA_CACHE_EXPORT
Definition: metadata_cache_export.h:15
Definition: metadata_cache.h:53
std::vector< metadata_server_t > metadata_servers_list_t
Definition: metadata_cache_datatypes.h:146
std::vector< ManagedInstance > cluster_nodes_list_t
Definition: metadata_cache_datatypes.h:142
const std::error_category & metadata_cache_category() noexcept
Definition: metadata_cache_datatypes.h:60
metadata_errc
Definition: metadata_cache_datatypes.h:42
std::error_code make_error_code(metadata_errc e) noexcept
Definition: metadata_cache_datatypes.h:90
bool operator==(const metadata_cache::ManagedCluster &cluster_a, const metadata_cache::ManagedCluster &cluster_b)
Definition: metadata_cache.cc:286
ServerRole
Definition: metadata_cache_datatypes.h:95
InstanceType
Definition: cluster_metadata.h:199
ServerMode
Definition: datatypes.h:55
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
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:183
metadata_servers_list_t metadata_servers
Definition: metadata_cache_datatypes.h:189
std::vector< ManagedCluster > clusters_list_t
Definition: metadata_cache_datatypes.h:184
void clear_all_members()
Definition: metadata_cache_datatypes.h:213
clusters_list_t clusters_data
Definition: metadata_cache_datatypes.h:186
cluster_nodes_list_t get_all_members() const
Definition: metadata_cache_datatypes.h:202
Metadata MySQL session configuration.
Definition: metadata_cache_datatypes.h:223
int connection_attempts
Definition: metadata_cache_datatypes.h:236
int connect_timeout
Definition: metadata_cache_datatypes.h:229
mysqlrouter::UserCredentials user_credentials
Definition: metadata_cache_datatypes.h:225
int read_timeout
Definition: metadata_cache_datatypes.h:232
Definition: metadata_cache_datatypes.h:239
std::string rw_classic_port
Definition: metadata_cache_datatypes.h:241
std::string ro_classic_port
Definition: metadata_cache_datatypes.h:242
std::string metadata_user_name
Definition: metadata_cache_datatypes.h:240
std::string rw_x_port
Definition: metadata_cache_datatypes.h:244
std::string rw_split_classic_port
Definition: metadata_cache_datatypes.h:243
std::string ro_x_port
Definition: metadata_cache_datatypes.h:245
Definition: datatypes.h:50
Definition: result.h:30