MySQL 8.0.39
Source Code Documentation
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 ROUTER_CLUSTER_METADATA_INCLUDED
27#define ROUTER_CLUSTER_METADATA_INCLUDED
28
30
31#include <stdexcept>
32
33#include "config_generator.h"
36#include "socket_operations.h"
37
38namespace mysqlrouter {
39
41 std::vector<std::string> metadata_servers;
42 std::string cluster_id;
43 // GR name for GR cluster
45 // name of the cluster (or clusterset in case of the clusterset)
46 std::string name;
47 // whether this cluster is a primary cluster in case it is a member of a
48 // ClusterSet
49 bool is_primary{false};
50
51 std::string get_cluster_type_specific_id() const {
54 }
55};
56
57using OptionsMap = std::map<std::string, std::string>;
58
60 public:
65 : mysql_(mysql),
66 socket_operations_(sockops),
67 schema_version_(schema_version) {}
68
69 virtual ~ClusterMetadata() = default;
70
72
73 /** @brief Checks if Router with given id is already registered in metadata
74 * database, and belongs to our machine
75 *
76 * @param router_id Router id
77 * @param hostname_override If non-empty, this hostname will be used instead
78 * of getting queried from OS
79 *
80 * @throws LocalHostnameResolutionError(std::runtime_error) on hostname query
81 * failure
82 * @throws std::runtime_error if router_id doesn't exist, or is associated
83 * with a different host
84 * @throws MySQLSession::Error(std::runtime_error) on database error
85 */
87 const uint32_t router_id, const std::string &hostname_override = "") = 0;
88
89 /** @brief Registers Router in metadata database
90 *
91 * @param router_name Router name
92 * @param overwrite if Router name is already registered, allow this
93 * registration to be "hijacked" instead of throwing
94 * @param hostname_override If non-empty, this hostname will be used instead
95 * of getting queried from OS
96 *
97 * @returns newly-assigned router_id
98 *
99 * @throws LocalHostnameResolutionError(std::runtime_error) on hostname query
100 * failure, std::runtime_error on other failure
101 */
102 virtual uint32_t register_router(
103 const std::string &router_name, const bool overwrite,
104 const std::string &hostname_override = "") = 0;
105
106 virtual void update_router_info(
107 const uint32_t router_id, const std::string &cluster_id,
108 const std::string &target_cluster, const std::string &rw_endpoint,
109 const std::string &ro_endpoint, const std::string &rw_x_endpoint,
110 const std::string &ro_x_endpoint, const std::string &username) = 0;
111
112 virtual std::vector<std::string> get_routing_mode_queries() = 0;
113
114 /** @brief Verify that host is a valid metadata server
115 *
116 *
117 * @throws MySQLSession::Error
118 * @throws std::runtime_error
119 * @throws std::out_of_range
120 * @throws std::logic_error
121 *
122 * checks that the server
123 *
124 * - has the metadata in the correct version
125 * - contains metadata for the group it's in (in case of GR cluster)
126 * (metadata server group must be same as managed group currently)
127 */
128 virtual void require_metadata_is_ok();
129
130 /** @brief Verify that host is a valid cluster member (either Group
131 * Replication or ReplicaSet cluster)
132 *
133 * @throws MySQLSession::Error
134 * @throws std::runtime_error
135 * @throws std::out_of_range
136 * @throws std::logic_error
137 */
138 virtual void require_cluster_is_ok() = 0;
139
140 virtual std::string get_cluster_type_specific_id() = 0;
141
143
144 virtual std::vector<std::string> get_grant_statements(
145 const std::string &new_accounts) const = 0;
146
147 virtual std::vector<std::tuple<std::string, unsigned long>>
149
151
152 virtual uint64_t get_view_id(
153 const std::string & /*cluster_type_specific_id*/) {
154 return 0;
155 }
156
157 protected:
158 // throws MySQLSession::Error, std::out_of_range, std::logic_error
159 virtual uint64_t query_cluster_count() = 0;
160
164};
165
167 public:
172 : ClusterMetadata(schema_version, mysql, sockops) {}
173
174 ~ClusterMetadataGR() override = default;
175
176 // For GR cluster Group Replication ID
177 std::string get_cluster_type_specific_id() override;
178
179 void require_cluster_is_ok() override;
180
181 std::vector<std::tuple<std::string, unsigned long>> fetch_cluster_hosts()
182 override;
183};
184
186 public:
191 : ClusterMetadataGR(schema_version, mysql, sockops) {}
192
193 ~ClusterMetadataGRV1() override = default;
194
197 }
198
200
201 std::vector<std::string> get_routing_mode_queries() override;
202
204 const uint32_t router_id,
205 const std::string &hostname_override = "") override;
206
208 const uint32_t router_id, const std::string &cluster_id,
209 const std::string &target_cluster, const std::string &rw_endpoint,
210 const std::string &ro_endpoint, const std::string &rw_x_endpoint,
211 const std::string &ro_x_endpoint, const std::string &username) override;
212
213 uint32_t register_router(const std::string &router_name, const bool overwrite,
214 const std::string &hostname_override = "") override;
215
216 std::vector<std::string> get_grant_statements(
217 const std::string &new_accounts) const override;
218
219 protected:
220 uint64_t query_cluster_count() override;
221};
222
224 public:
229 : ClusterMetadataGR(schema_version, mysql, sockops) {}
230
231 ~ClusterMetadataGRV2() override = default;
232
235 }
236
238
239 std::vector<std::string> get_routing_mode_queries() override;
240
242 uint32_t router_id, const std::string &hostname_override = "") override;
243
245 const uint32_t router_id, const std::string &cluster_id,
246 const std::string &target_cluster, const std::string &rw_endpoint,
247 const std::string &ro_endpoint, const std::string &rw_x_endpoint,
248 const std::string &ro_x_endpoint, const std::string &username) override;
249
250 uint32_t register_router(const std::string &router_name, const bool overwrite,
251 const std::string &hostname_override = "") override;
252
253 std::vector<std::string> get_grant_statements(
254 const std::string &new_accounts) const override;
255
256 protected:
257 uint64_t query_cluster_count() override;
258};
259
261 public:
263 const MetadataSchemaVersion &schema_version, MySQLSession *mysql,
264 const OptionsMap & /*options*/,
267
268 ~ClusterMetadataGRInClusterSet() override = default;
269
272 }
273
274 // nothing specific to check for ClusterSet
275 void require_metadata_is_ok() override {}
276
278
279 std::vector<std::tuple<std::string, unsigned long>> fetch_cluster_hosts()
280 override;
281
282 enum class TargetClusterType {
283 // target should be the cluster on which we bootstrap
285 // target should be the Priamry Cluster
287 // target should be the Cluster with the given name
289 };
290
291 std::string get_cluster_type_specific_id() override;
292 uint64_t get_view_id(const std::string &cluster_type_specific_id) override;
293
295 const uint32_t router_id, const std::string &cluster_id,
296 const std::string &target_cluster, const std::string &rw_endpoint,
297 const std::string &ro_endpoint, const std::string &rw_x_endpoint,
298 const std::string &ro_x_endpoint, const std::string &username) override;
299
300 protected:
303};
304
306 public:
311 : ClusterMetadata(schema_version, mysql, sockops) {}
312
313 ~ClusterMetadataAR() override = default;
314
317 }
318
319 void require_cluster_is_ok() override {
320 // Nothing specific to check for ReplicaSet cluster
321 }
322
324
325 std::string get_cluster_type_specific_id() override;
326
327 uint64_t get_view_id(
328 const std::string & /*cluster_type_specific_id*/) override;
329
330 std::vector<std::string> get_routing_mode_queries() override;
331
333 uint32_t router_id, const std::string &hostname_override = "") override;
334
336 const uint32_t router_id, const std::string &cluster_id,
337 const std::string &target_cluster, const std::string &rw_endpoint,
338 const std::string &ro_endpoint, const std::string &rw_x_endpoint,
339 const std::string &ro_x_endpoint, const std::string &username) override;
340
341 uint32_t register_router(const std::string &router_name, const bool overwrite,
342 const std::string &hostname_override = "") override;
343
344 std::vector<std::string> get_grant_statements(
345 const std::string &new_accounts) const override;
346
347 std::vector<std::tuple<std::string, unsigned long>> fetch_cluster_hosts()
348 override;
349
350 protected:
351 uint64_t query_cluster_count() override;
352};
353
354std::unique_ptr<ClusterMetadata> ROUTER_LIB_EXPORT
355create_metadata(const MetadataSchemaVersion &schema_version,
356 MySQLSession *mysql, const OptionsMap &options = {},
359
360} // namespace mysqlrouter
361
362#endif // ROUTER_CLUSTER_METADATA_INCLUDED
Base class to allow multiple SocketOperations implementations (at least one "real" and one mock for t...
Definition: socket_operations.h:45
static SocketOperations * instance()
Definition: socket_operations.cc:42
Definition: cluster_metadata.h:305
void update_router_info(const uint32_t router_id, const std::string &cluster_id, const std::string &target_cluster, const std::string &rw_endpoint, const std::string &ro_endpoint, const std::string &rw_x_endpoint, const std::string &ro_x_endpoint, const std::string &username) override
Definition: cluster_metadata.cc:270
void require_cluster_is_ok() override
Verify that host is a valid cluster member (either Group Replication or ReplicaSet cluster)
Definition: cluster_metadata.h:319
std::vector< std::tuple< std::string, unsigned long > > fetch_cluster_hosts() override
Definition: cluster_metadata.cc:1071
std::vector< std::string > get_grant_statements(const std::string &new_accounts) const override
Definition: cluster_metadata.cc:1307
std::vector< std::string > get_routing_mode_queries() override
Definition: cluster_metadata.cc:1059
mysqlrouter::ClusterType get_type() override
Definition: cluster_metadata.h:315
uint64_t get_view_id(const std::string &) override
Definition: cluster_metadata.cc:1241
uint32_t register_router(const std::string &router_name, const bool overwrite, const std::string &hostname_override="") override
Registers Router in metadata database.
Definition: cluster_metadata.cc:425
void verify_router_id_is_ours(uint32_t router_id, const std::string &hostname_override="") override
Checks if Router with given id is already registered in metadata database, and belongs to our machine...
Definition: cluster_metadata.cc:154
std::string get_cluster_type_specific_id() override
Definition: cluster_metadata.cc:1041
ClusterMetadataAR(const MetadataSchemaVersion &schema_version, MySQLSession *mysql, mysql_harness::SocketOperationsBase *sockops=mysql_harness::SocketOperations::instance())
Definition: cluster_metadata.h:307
ClusterInfo fetch_metadata_servers() override
Definition: cluster_metadata.cc:1037
~ClusterMetadataAR() override=default
uint64_t query_cluster_count() override
Definition: cluster_metadata.cc:1016
Definition: cluster_metadata.h:260
void require_metadata_is_ok() override
Verify that host is a valid metadata server.
Definition: cluster_metadata.h:275
std::string target_cluster_name_
Definition: cluster_metadata.h:302
std::vector< std::tuple< std::string, unsigned long > > fetch_cluster_hosts() override
Definition: cluster_metadata.cc:908
TargetClusterType target_cluster_type_
Definition: cluster_metadata.h:301
mysqlrouter::ClusterType get_type() override
Definition: cluster_metadata.h:270
ClusterInfo fetch_metadata_servers() override
Definition: cluster_metadata.cc:811
void update_router_info(const uint32_t router_id, const std::string &cluster_id, const std::string &target_cluster, const std::string &rw_endpoint, const std::string &ro_endpoint, const std::string &rw_x_endpoint, const std::string &ro_x_endpoint, const std::string &username) override
Definition: cluster_metadata.cc:280
std::string get_cluster_type_specific_id() override
Definition: cluster_metadata.cc:920
uint64_t get_view_id(const std::string &cluster_type_specific_id) override
Definition: cluster_metadata.cc:942
TargetClusterType
Definition: cluster_metadata.h:282
ClusterMetadataGRInClusterSet(const MetadataSchemaVersion &schema_version, MySQLSession *mysql, const OptionsMap &, mysql_harness::SocketOperationsBase *sockops=mysql_harness::SocketOperations::instance())
Definition: cluster_metadata.cc:789
Definition: cluster_metadata.h:185
std::vector< std::string > get_routing_mode_queries() override
Definition: cluster_metadata.cc:1008
std::vector< std::string > get_grant_statements(const std::string &new_accounts) const override
Definition: cluster_metadata.cc:1297
ClusterInfo fetch_metadata_servers() override
Definition: cluster_metadata.cc:781
uint64_t query_cluster_count() override
Definition: cluster_metadata.cc:701
void verify_router_id_is_ours(const uint32_t router_id, const std::string &hostname_override="") override
Checks if Router with given id is already registered in metadata database, and belongs to our machine...
Definition: cluster_metadata.cc:142
~ClusterMetadataGRV1() override=default
uint32_t register_router(const std::string &router_name, const bool overwrite, const std::string &hostname_override="") override
Registers Router in metadata database.
Definition: cluster_metadata.cc:411
mysqlrouter::ClusterType get_type() override
Definition: cluster_metadata.h:195
void update_router_info(const uint32_t router_id, const std::string &cluster_id, const std::string &target_cluster, const std::string &rw_endpoint, const std::string &ro_endpoint, const std::string &rw_x_endpoint, const std::string &ro_x_endpoint, const std::string &username) override
Definition: cluster_metadata.cc:251
ClusterMetadataGRV1(const MetadataSchemaVersion &schema_version, MySQLSession *mysql, mysql_harness::SocketOperationsBase *sockops=mysql_harness::SocketOperations::instance())
Definition: cluster_metadata.h:187
Definition: cluster_metadata.h:223
void update_router_info(const uint32_t router_id, const std::string &cluster_id, const std::string &target_cluster, const std::string &rw_endpoint, const std::string &ro_endpoint, const std::string &rw_x_endpoint, const std::string &ro_x_endpoint, const std::string &username) override
Definition: cluster_metadata.cc:260
void verify_router_id_is_ours(uint32_t router_id, const std::string &hostname_override="") override
Checks if Router with given id is already registered in metadata database, and belongs to our machine...
Definition: cluster_metadata.cc:148
std::vector< std::string > get_grant_statements(const std::string &new_accounts) const override
Definition: cluster_metadata.cc:1302
std::vector< std::string > get_routing_mode_queries() override
Definition: cluster_metadata.cc:1012
uint64_t query_cluster_count() override
Definition: cluster_metadata.cc:705
ClusterInfo fetch_metadata_servers() override
Definition: cluster_metadata.cc:785
~ClusterMetadataGRV2() override=default
ClusterMetadataGRV2(const MetadataSchemaVersion &schema_version, MySQLSession *mysql, mysql_harness::SocketOperationsBase *sockops=mysql_harness::SocketOperations::instance())
Definition: cluster_metadata.h:225
mysqlrouter::ClusterType get_type() override
Definition: cluster_metadata.h:233
uint32_t register_router(const std::string &router_name, const bool overwrite, const std::string &hostname_override="") override
Registers Router in metadata database.
Definition: cluster_metadata.cc:418
Definition: cluster_metadata.h:166
ClusterMetadataGR(const MetadataSchemaVersion &schema_version, MySQLSession *mysql, mysql_harness::SocketOperationsBase *sockops=mysql_harness::SocketOperations::instance())
Definition: cluster_metadata.h:168
void require_cluster_is_ok() override
Verify that host is a valid cluster member (either Group Replication or ReplicaSet cluster)
Definition: cluster_metadata.cc:581
std::vector< std::tuple< std::string, unsigned long > > fetch_cluster_hosts() override
Definition: cluster_metadata.cc:615
~ClusterMetadataGR() override=default
std::string get_cluster_type_specific_id() override
Definition: cluster_metadata.cc:653
Definition: cluster_metadata.h:59
virtual void require_cluster_is_ok()=0
Verify that host is a valid cluster member (either Group Replication or ReplicaSet cluster)
virtual std::string get_cluster_type_specific_id()=0
virtual std::vector< std::string > get_grant_statements(const std::string &new_accounts) const =0
MySQLSession & get_session()
Definition: cluster_metadata.h:150
virtual mysqlrouter::ClusterType get_type()=0
virtual uint64_t get_view_id(const std::string &)
Definition: cluster_metadata.h:152
mysqlrouter::MetadataSchemaVersion schema_version_
Definition: cluster_metadata.h:163
virtual uint64_t query_cluster_count()=0
virtual std::vector< std::string > get_routing_mode_queries()=0
ClusterMetadata(const MetadataSchemaVersion &schema_version, MySQLSession *mysql, mysql_harness::SocketOperationsBase *sockops=mysql_harness::SocketOperations::instance())
Definition: cluster_metadata.h:61
virtual ~ClusterMetadata()=default
virtual void verify_router_id_is_ours(const uint32_t router_id, const std::string &hostname_override="")=0
Checks if Router with given id is already registered in metadata database, and belongs to our machine...
virtual ClusterInfo fetch_metadata_servers()=0
mysql_harness::SocketOperationsBase * socket_operations_
Definition: cluster_metadata.h:162
MySQLSession * mysql_
Definition: cluster_metadata.h:161
virtual std::vector< std::tuple< std::string, unsigned long > > fetch_cluster_hosts()=0
virtual void require_metadata_is_ok()
Verify that host is a valid metadata server.
Definition: cluster_metadata.cc:561
virtual uint32_t register_router(const std::string &router_name, const bool overwrite, const std::string &hostname_override="")=0
Registers Router in metadata database.
virtual void update_router_info(const uint32_t router_id, const std::string &cluster_id, const std::string &target_cluster, const std::string &rw_endpoint, const std::string &ro_endpoint, const std::string &rw_x_endpoint, const std::string &ro_x_endpoint, const std::string &username)=0
Definition: mysql_session.h:153
Definition: instrumented_condition_variable.h:32
Definition: dim.h:358
std::unique_ptr< ClusterMetadata > create_metadata(const MetadataSchemaVersion &schema_version, MySQLSession *mysql, const OptionsMap &options, mysql_harness::SocketOperationsBase *sockops)
Definition: cluster_metadata.cc:1208
ClusterType
Definition: cluster_metadata.h:141
std::map< std::string, std::string > OptionsMap
Definition: cluster_metadata.h:57
Definition: options.cc:49
#define ROUTER_LIB_EXPORT
Definition: router_export.h:15
Definition: cluster_metadata.h:40
bool is_primary
Definition: cluster_metadata.h:49
std::string get_cluster_type_specific_id() const
Definition: cluster_metadata.h:51
std::string cluster_type_specific_id
Definition: cluster_metadata.h:44
std::string cluster_id
Definition: cluster_metadata.h:42
std::vector< std::string > metadata_servers
Definition: cluster_metadata.h:41
std::string name
Definition: cluster_metadata.h:46
Definition: cluster_metadata.h:40