MySQL 8.1.0
Source Code Documentation
metadata_cache_datatypes.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2016, 2023, 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 also distributed 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 included with MySQL.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23*/
24
25#ifndef MYSQLROUTER_METADATA_CACHE_DATATYPES_INCLUDED
26#define MYSQLROUTER_METADATA_CACHE_DATATYPES_INCLUDED
27
29
30#include <algorithm>
31#include <optional>
32#include <string>
33#include <system_error>
34#include <vector>
35
36#include "mysqlrouter/datatypes.h" // UserCredentials
37#include "tcp_address.h"
38
39namespace metadata_cache {
40
41enum class metadata_errc {
42 ok,
49};
50} // namespace metadata_cache
51
52namespace std {
53template <>
54struct is_error_code_enum<metadata_cache::metadata_errc>
55 : public std::true_type {};
56} // namespace std
57
58namespace metadata_cache {
59inline const std::error_category &metadata_cache_category() noexcept {
60 class metadata_category_impl : public std::error_category {
61 public:
62 const char *name() const noexcept override { return "metadata cache"; }
63 std::string message(int ev) const override {
64 switch (static_cast<metadata_errc>(ev)) {
66 return "ok";
68 return "no metadata server accessible";
70 return "did not successfully read metadata from any metadata server";
72 return "metadata refresh terminated";
74 return "cluster not found in the metadata";
76 return "unexpected cluster type";
78 return "highier view_id seen";
79 default:
80 return "unknown";
81 }
82 }
83 };
84
85 static metadata_category_impl instance;
86 return instance;
87}
88
89inline std::error_code make_error_code(metadata_errc e) noexcept {
90 return std::error_code(static_cast<int>(e), metadata_cache_category());
91}
92
95
96/** @class ManagedInstance
97 *
98 * Class ManagedInstance represents a server managed by the topology.
99 */
101 public:
103 const std::string &p_mysql_server_uuid,
104 const ServerMode p_mode, const ServerRole p_role,
105 const std::string &p_host, const uint16_t p_port,
106 const uint16_t p_xport);
107
111 const TCPAddress &addr);
112 operator TCPAddress() const;
113 bool operator==(const ManagedInstance &other) const;
114
115 /** @brief Instance type */
117 /** @brief The uuid of the MySQL server */
118 std::string mysql_server_uuid;
119 /** @brief The mode of the server */
121 /** @brief The role of the server */
123 /** @brief The host name on which the server is running */
124 std::string host;
125 /** The port number in which the server is running */
126 uint16_t port{0};
127 /** The X protocol port number in which the server is running */
128 uint16_t xport{0};
129 /** Node atributes as a json string from metadata */
130 std::string attributes;
131 /** Should the node be hidden from the application to use it */
132 bool hidden;
133 /** Should the Router disconnect existing client sessions to the node when it
134 * is hidden */
136 /** Should the node be ignored for new and existing connections (for example
137 * due to the read_only_targets option) */
138 bool ignore{false};
139};
140
141using cluster_nodes_list_t = std::vector<ManagedInstance>;
142
144
145using metadata_servers_list_t = std::vector<metadata_server_t>;
146
147/** @class ManagedCluster
148 * Represents a cluster (a GR group or AR members)
149 */
151 public:
152 /** @brief UUID in the metadata */
153 std::string id;
154 /** @brief Name of the cluster */
155 std::string name;
156 /** @brief List of the members that belong to the cluster */
158 /** @brief Whether the cluster is in single_primary_mode (from PFS in case of
159 * GR) */
161
162 /** @brief Metadata for the cluster is not consistent (only applicable for
163 * the GR cluster when the data in the GR metadata is not consistent with the
164 * cluster metadata)*/
165 bool md_discrepancy{false};
166
167 /** @brief Is this a PRIMARY Cluster in case of ClusterSet */
168 bool is_primary{true};
169 /** @brief Is the Cluster marked as invalid in the metadata */
170 bool is_invalidated{false};
171
172 bool empty() const noexcept { return members.empty(); }
173
174 void clear() noexcept { members.clear(); }
175};
176
177/** @class ClusterTopology
178 * Represents a cluster (a GR group or AR members) and its metadata servers
179 */
181 using clusters_list_t = std::vector<ManagedCluster>;
182
184 // index of the target cluster in the clusters_data vector
185 std::optional<size_t> target_cluster_pos{};
187
188 /** @brief Id of the view this metadata represents (used for AR and
189 * ClusterSets)*/
190 uint64_t view_id{0};
191
192 /** @brief name of the ClusterSet or empty in case of standalone Cluster */
193 std::string name{};
194
195 // address of the writable metadata server that can be used for updating the
196 // metadata (router version, last_check_in), nullptr_t if not found
197 std::optional<metadata_cache::metadata_server_t> writable_server{};
198
201
202 for (const auto &cluster : clusters_data) {
203 result.insert(result.end(), cluster.members.begin(),
204 cluster.members.end());
205 }
206
207 return result;
208 }
209
211 for (auto &cluster : clusters_data) {
212 cluster.members.clear();
213 }
214 }
215};
216
217/**
218 * @brief Metadata MySQL session configuration
219 */
221 // User credentials used for the connecting to the metadata server.
223
224 // The time in seconds after which trying to connect to metadata server should
225 // time out.
227
228 // The time in seconds after which read from metadata server should time out.
230
231 // Numbers of retries used before giving up the attempt to connect to the
232 // metadata server (not used atm).
234};
235
238 std::string rw_classic_port;
239 std::string ro_classic_port;
240 std::string rw_x_port;
241 std::string ro_x_port;
242};
243
244} // namespace metadata_cache
245
246#endif
Represents a cluster (a GR group or AR members)
Definition: metadata_cache_datatypes.h:150
std::string id
UUID in the metadata.
Definition: metadata_cache_datatypes.h:153
cluster_nodes_list_t members
List of the members that belong to the cluster.
Definition: metadata_cache_datatypes.h:157
bool single_primary_mode
Whether the cluster is in single_primary_mode (from PFS in case of GR)
Definition: metadata_cache_datatypes.h:160
std::string name
Name of the cluster.
Definition: metadata_cache_datatypes.h:155
void clear() noexcept
Definition: metadata_cache_datatypes.h:174
bool empty() const noexcept
Definition: metadata_cache_datatypes.h:172
Class ManagedInstance represents a server managed by the topology.
Definition: metadata_cache_datatypes.h:100
std::string host
The host name on which the server is running.
Definition: metadata_cache_datatypes.h:124
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:135
std::string mysql_server_uuid
The uuid of the MySQL server.
Definition: metadata_cache_datatypes.h:118
mysqlrouter::InstanceType type
Instance type.
Definition: metadata_cache_datatypes.h:116
bool hidden
Should the node be hidden from the application to use it.
Definition: metadata_cache_datatypes.h:132
std::string attributes
Node atributes as a json string from metadata.
Definition: metadata_cache_datatypes.h:130
Defines an IP address with port number
Definition: tcp_address.h:39
static std::optional< size_t > target_cluster_pos(const metadata_cache::ClusterTopology &topology, const std::string &target_cluster_id)
Definition: cluster_metadata_gr.cc:1360
#define METADATA_CACHE_EXPORT
Definition: metadata_cache_export.h:15
static bool ignore
Definition: mysqlimport.cc:69
Definition: metadata_cache.h:52
ServerMode
Definition: metadata_cache_datatypes.h:93
std::vector< metadata_server_t > metadata_servers_list_t
Definition: metadata_cache_datatypes.h:145
std::vector< ManagedInstance > cluster_nodes_list_t
Definition: metadata_cache_datatypes.h:141
const std::error_category & metadata_cache_category() noexcept
Definition: metadata_cache_datatypes.h:59
metadata_errc
Definition: metadata_cache_datatypes.h:41
std::error_code make_error_code(metadata_errc e) noexcept
Definition: metadata_cache_datatypes.h:89
bool operator==(const metadata_cache::ManagedCluster &cluster_a, const metadata_cache::ManagedCluster &cluster_b)
Definition: metadata_cache.cc:285
ServerRole
Definition: metadata_cache_datatypes.h:94
InstanceType
Definition: cluster_metadata.h:195
Definition: varlen_sort.h:183
mode
Definition: file_handle.h:59
struct result result
Definition: result.h:33
required uint64 port
Definition: replication_asynchronous_connection_failover.proto:32
case opt name
Definition: sslopt-case.h:32
Represents a cluster (a GR group or AR members) and its metadata servers.
Definition: metadata_cache_datatypes.h:180
metadata_servers_list_t metadata_servers
Definition: metadata_cache_datatypes.h:186
std::vector< ManagedCluster > clusters_list_t
Definition: metadata_cache_datatypes.h:181
void clear_all_members()
Definition: metadata_cache_datatypes.h:210
clusters_list_t clusters_data
Definition: metadata_cache_datatypes.h:183
cluster_nodes_list_t get_all_members() const
Definition: metadata_cache_datatypes.h:199
Metadata MySQL session configuration.
Definition: metadata_cache_datatypes.h:220
int connection_attempts
Definition: metadata_cache_datatypes.h:233
int connect_timeout
Definition: metadata_cache_datatypes.h:226
mysqlrouter::UserCredentials user_credentials
Definition: metadata_cache_datatypes.h:222
int read_timeout
Definition: metadata_cache_datatypes.h:229
Definition: metadata_cache_datatypes.h:236
std::string rw_classic_port
Definition: metadata_cache_datatypes.h:238
std::string ro_classic_port
Definition: metadata_cache_datatypes.h:239
std::string metadata_user_name
Definition: metadata_cache_datatypes.h:237
std::string rw_x_port
Definition: metadata_cache_datatypes.h:240
std::string ro_x_port
Definition: metadata_cache_datatypes.h:241
Definition: datatypes.h:49
Definition: result.h:29