MySQL 8.0.30
Source Code Documentation
cluster_metadata.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2016, 2022, 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 ROUTER_CLUSTER_METADATA_INCLUDED
26#define ROUTER_CLUSTER_METADATA_INCLUDED
27
28#include <stdexcept>
29
30#include "config_generator.h"
33#include "socket_operations.h"
34
35namespace mysqlrouter {
36
38 std::vector<std::string> metadata_servers;
39 std::string cluster_id;
40 // GR name for GR cluster
42 // name of the cluster (or clusterset in case of the clusterset)
43 std::string name;
44 // whether this cluster is a primary cluster in case it is a member of a
45 // ClusterSet
46 bool is_primary{false};
47
48 std::string get_cluster_type_specific_id() const {
51 }
52};
53
54using OptionsMap = std::map<std::string, std::string>;
55
57 public:
62 : mysql_(mysql),
63 socket_operations_(sockops),
64 schema_version_(schema_version) {}
65
66 virtual ~ClusterMetadata() = default;
67
69
70 /** @brief Checks if Router with given id is already registered in metadata
71 * database, and belongs to our machine
72 *
73 * @param router_id Router id
74 * @param hostname_override If non-empty, this hostname will be used instead
75 * of getting queried from OS
76 *
77 * @throws LocalHostnameResolutionError(std::runtime_error) on hostname query
78 * failure
79 * @throws std::runtime_error if router_id doesn't exist, or is associated
80 * with a different host
81 * @throws MySQLSession::Error(std::runtime_error) on database error
82 */
84 const uint32_t router_id, const std::string &hostname_override = "") = 0;
85
86 /** @brief Registers Router in metadata database
87 *
88 * @param router_name Router name
89 * @param overwrite if Router name is already registered, allow this
90 * registration to be "hijacked" instead of throwing
91 * @param hostname_override If non-empty, this hostname will be used instead
92 * of getting queried from OS
93 *
94 * @returns newly-assigned router_id
95 *
96 * @throws LocalHostnameResolutionError(std::runtime_error) on hostname query
97 * failure, std::runtime_error on other failure
98 */
99 virtual uint32_t register_router(
100 const std::string &router_name, const bool overwrite,
101 const std::string &hostname_override = "") = 0;
102
103 virtual void update_router_info(
104 const uint32_t router_id, const std::string &cluster_id,
105 const std::string &target_cluster, const std::string &rw_endpoint,
106 const std::string &ro_endpoint, const std::string &rw_x_endpoint,
107 const std::string &ro_x_endpoint, const std::string &username) = 0;
108
109 virtual std::vector<std::string> get_routing_mode_queries(
110 const std::string &cluster_name) = 0;
111
112 /** @brief Verify that host is a valid metadata server
113 *
114 *
115 * @throws MySQLSession::Error
116 * @throws std::runtime_error
117 * @throws std::out_of_range
118 * @throws std::logic_error
119 *
120 * checks that the server
121 *
122 * - has the metadata in the correct version
123 * - contains metadata for the group it's in (in case of GR cluster)
124 * (metadata server group must be same as managed group currently)
125 */
126 virtual void require_metadata_is_ok();
127
128 /** @brief Verify that host is a valid cluster member (either Group
129 * Replication or ReplicaSet cluster)
130 *
131 * @throws MySQLSession::Error
132 * @throws std::runtime_error
133 * @throws std::out_of_range
134 * @throws std::logic_error
135 */
136 virtual void require_cluster_is_ok() = 0;
137
138 virtual std::string get_cluster_type_specific_id() = 0;
139
141
142 virtual std::vector<std::string> get_grant_statements(
143 const std::string &new_accounts) const = 0;
144
145 virtual std::vector<std::tuple<std::string, unsigned long>>
147
149
150 virtual uint64_t get_view_id(
151 const std::string & /*cluster_type_specific_id*/) {
152 return 0;
153 }
154
155 protected:
156 // throws MySQLSession::Error, std::out_of_range, std::logic_error
157 virtual uint64_t query_cluster_count() = 0;
158
162};
163
165 public:
170 : ClusterMetadata(schema_version, mysql, sockops) {}
171
172 ~ClusterMetadataGR() override = default;
173
174 // For GR cluster Group Replication ID
175 std::string get_cluster_type_specific_id() override;
176
177 void require_cluster_is_ok() override;
178
179 std::vector<std::tuple<std::string, unsigned long>> fetch_cluster_hosts()
180 override;
181};
182
184 public:
189 : ClusterMetadataGR(schema_version, mysql, sockops) {}
190
191 ~ClusterMetadataGRV1() override = default;
192
195 }
196
198
199 std::vector<std::string> get_routing_mode_queries(
200 const std::string &cluster_name) override;
201
203 const uint32_t router_id,
204 const std::string &hostname_override = "") override;
205
207 const uint32_t router_id, const std::string &cluster_id,
208 const std::string &target_cluster, const std::string &rw_endpoint,
209 const std::string &ro_endpoint, const std::string &rw_x_endpoint,
210 const std::string &ro_x_endpoint, const std::string &username) override;
211
212 uint32_t register_router(const std::string &router_name, const bool overwrite,
213 const std::string &hostname_override = "") override;
214
215 std::vector<std::string> get_grant_statements(
216 const std::string &new_accounts) const override;
217
218 protected:
219 uint64_t query_cluster_count() override;
220};
221
223 public:
228 : ClusterMetadataGR(schema_version, mysql, sockops) {}
229
230 ~ClusterMetadataGRV2() override = default;
231
234 }
235
237
238 std::vector<std::string> get_routing_mode_queries(
239 const std::string &cluster_name) 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(
331 const std::string &cluster_name) override;
332
334 uint32_t router_id, const std::string &hostname_override = "") override;
335
337 const uint32_t router_id, const std::string &cluster_id,
338 const std::string &target_cluster, const std::string &rw_endpoint,
339 const std::string &ro_endpoint, const std::string &rw_x_endpoint,
340 const std::string &ro_x_endpoint, const std::string &username) override;
341
342 uint32_t register_router(const std::string &router_name, const bool overwrite,
343 const std::string &hostname_override = "") override;
344
345 std::vector<std::string> get_grant_statements(
346 const std::string &new_accounts) const override;
347
348 std::vector<std::tuple<std::string, unsigned long>> fetch_cluster_hosts()
349 override;
350
351 protected:
352 uint64_t query_cluster_count() override;
353};
354
355std::unique_ptr<ClusterMetadata> create_metadata(
356 const MetadataSchemaVersion &schema_version, MySQLSession *mysql,
357 const OptionsMap &options = {},
360
361} // namespace mysqlrouter
362
363#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:44
static SocketOperations * instance()
Definition: socket_operations.cc:41
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:269
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:1047
std::vector< std::string > get_grant_statements(const std::string &new_accounts) const override
Definition: cluster_metadata.cc:1283
mysqlrouter::ClusterType get_type() override
Definition: cluster_metadata.h:315
std::vector< std::string > get_routing_mode_queries(const std::string &cluster_name) override
Definition: cluster_metadata.cc:1035
uint64_t get_view_id(const std::string &) override
Definition: cluster_metadata.cc:1217
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:424
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:153
std::string get_cluster_type_specific_id() override
Definition: cluster_metadata.cc:1017
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:1013
~ClusterMetadataAR() override=default
uint64_t query_cluster_count() override
Definition: cluster_metadata.cc:992
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:881
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:784
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:279
std::string get_cluster_type_specific_id() override
Definition: cluster_metadata.cc:893
uint64_t get_view_id(const std::string &cluster_type_specific_id) override
Definition: cluster_metadata.cc:915
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:762
Definition: cluster_metadata.h:183
std::vector< std::string > get_grant_statements(const std::string &new_accounts) const override
Definition: cluster_metadata.cc:1273
ClusterInfo fetch_metadata_servers() override
Definition: cluster_metadata.cc:754
uint64_t query_cluster_count() override
Definition: cluster_metadata.cc:687
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:141
~ClusterMetadataGRV1() override=default
std::vector< std::string > get_routing_mode_queries(const std::string &cluster_name) override
Definition: cluster_metadata.cc:980
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:410
mysqlrouter::ClusterType get_type() override
Definition: cluster_metadata.h:193
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:250
ClusterMetadataGRV1(const MetadataSchemaVersion &schema_version, MySQLSession *mysql, mysql_harness::SocketOperationsBase *sockops=mysql_harness::SocketOperations::instance())
Definition: cluster_metadata.h:185
Definition: cluster_metadata.h:222
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:259
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:147
std::vector< std::string > get_grant_statements(const std::string &new_accounts) const override
Definition: cluster_metadata.cc:1278
uint64_t query_cluster_count() override
Definition: cluster_metadata.cc:691
ClusterInfo fetch_metadata_servers() override
Definition: cluster_metadata.cc:758
~ClusterMetadataGRV2() override=default
std::vector< std::string > get_routing_mode_queries(const std::string &cluster_name) override
Definition: cluster_metadata.cc:986
ClusterMetadataGRV2(const MetadataSchemaVersion &schema_version, MySQLSession *mysql, mysql_harness::SocketOperationsBase *sockops=mysql_harness::SocketOperations::instance())
Definition: cluster_metadata.h:224
mysqlrouter::ClusterType get_type() override
Definition: cluster_metadata.h:232
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:417
Definition: cluster_metadata.h:164
ClusterMetadataGR(const MetadataSchemaVersion &schema_version, MySQLSession *mysql, mysql_harness::SocketOperationsBase *sockops=mysql_harness::SocketOperations::instance())
Definition: cluster_metadata.h:166
void require_cluster_is_ok() override
Verify that host is a valid cluster member (either Group Replication or ReplicaSet cluster)
Definition: cluster_metadata.cc:567
std::vector< std::tuple< std::string, unsigned long > > fetch_cluster_hosts() override
Definition: cluster_metadata.cc:601
~ClusterMetadataGR() override=default
std::string get_cluster_type_specific_id() override
Definition: cluster_metadata.cc:639
Definition: cluster_metadata.h:56
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
virtual std::vector< std::string > get_routing_mode_queries(const std::string &cluster_name)=0
MySQLSession & get_session()
Definition: cluster_metadata.h:148
virtual mysqlrouter::ClusterType get_type()=0
virtual uint64_t get_view_id(const std::string &)
Definition: cluster_metadata.h:150
mysqlrouter::MetadataSchemaVersion schema_version_
Definition: cluster_metadata.h:161
virtual uint64_t query_cluster_count()=0
ClusterMetadata(const MetadataSchemaVersion &schema_version, MySQLSession *mysql, mysql_harness::SocketOperationsBase *sockops=mysql_harness::SocketOperations::instance())
Definition: cluster_metadata.h:58
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:160
MySQLSession * mysql_
Definition: cluster_metadata.h:159
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:547
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:154
static MYSQL mysql
Definition: mysql.cc:153
Definition: dim.h:357
ClusterType
Definition: cluster_metadata.h:128
std::unique_ptr< ClusterMetadata > create_metadata(const MetadataSchemaVersion &schema_version, MySQLSession *mysql, const OptionsMap &options, mysql_harness::SocketOperationsBase *sockops)
Definition: cluster_metadata.cc:1184
std::map< std::string, std::string > OptionsMap
Definition: cluster_metadata.h:54
Definition: options.cc:48
Definition: cluster_metadata.h:37
bool is_primary
Definition: cluster_metadata.h:46
std::string get_cluster_type_specific_id() const
Definition: cluster_metadata.h:48
std::string cluster_type_specific_id
Definition: cluster_metadata.h:41
std::string cluster_id
Definition: cluster_metadata.h:39
std::vector< std::string > metadata_servers
Definition: cluster_metadata.h:38
std::string name
Definition: cluster_metadata.h:43
Definition: cluster_metadata.h:39