MySQL 9.1.0
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_split_endpoint,
110 const std::string &rw_x_endpoint, const std::string &ro_x_endpoint,
111 const std::string &username) = 0;
112
113 virtual std::vector<std::string> get_routing_mode_queries() = 0;
114
115 /** @brief Verify that host is a valid metadata server
116 *
117 *
118 * @throws MySQLSession::Error
119 * @throws std::runtime_error
120 * @throws std::out_of_range
121 * @throws std::logic_error
122 *
123 * checks that the server
124 *
125 * - has the metadata in the correct version
126 * - contains metadata for the group it's in (in case of GR cluster)
127 * (metadata server group must be same as managed group currently)
128 */
129 virtual void require_metadata_is_ok();
130
131 /** @brief Verify that host is a valid cluster member (either Group
132 * Replication or ReplicaSet cluster)
133 *
134 * @throws MySQLSession::Error
135 * @throws std::runtime_error
136 * @throws std::out_of_range
137 * @throws std::logic_error
138 */
139 virtual void require_cluster_is_ok() = 0;
140
141 virtual std::string get_cluster_type_specific_id() = 0;
142
144
146
147 virtual std::vector<std::string> get_grant_statements(
148 const std::string &new_accounts) const = 0;
149
150 virtual std::vector<std::tuple<std::string, unsigned long>>
152
154
155 virtual uint64_t get_view_id(
156 const std::string & /*cluster_type_specific_id*/) {
157 return 0;
158 }
159
160 protected:
161 // throws MySQLSession::Error, std::out_of_range, std::logic_error
162 virtual uint64_t query_cluster_count() = 0;
163
167};
168
170 public:
175 : ClusterMetadata(schema_version, mysql, sockops) {}
176
177 ~ClusterMetadataGR() override = default;
178
179 // For GR cluster Group Replication ID
180 std::string get_cluster_type_specific_id() override;
181
182 void require_cluster_is_ok() override;
183
184 std::vector<std::tuple<std::string, unsigned long>> fetch_cluster_hosts()
185 override;
186};
187
189 public:
194 : ClusterMetadataGR(schema_version, mysql, sockops) {}
195
196 ~ClusterMetadataGRV2() override = default;
197
200 }
201
203
204 std::vector<std::string> get_routing_mode_queries() override;
205
207
209 uint32_t router_id, const std::string &hostname_override = "") override;
210
212 const uint32_t router_id, const std::string &cluster_id,
213 const std::string &target_cluster, const std::string &rw_endpoint,
214 const std::string &ro_endpoint, const std::string &rw_split_endpoint,
215 const std::string &rw_x_endpoint, const std::string &ro_x_endpoint,
216 const std::string &username) override;
217
218 uint32_t register_router(const std::string &router_name, const bool overwrite,
219 const std::string &hostname_override = "") override;
220
221 std::vector<std::string> get_grant_statements(
222 const std::string &new_accounts) const override;
223
224 protected:
225 uint64_t query_cluster_count() override;
226};
227
229 public:
231 const MetadataSchemaVersion &schema_version, MySQLSession *mysql,
232 const OptionsMap & /*options*/,
235
236 ~ClusterMetadataGRInClusterSet() override = default;
237
240 }
241
242 // nothing specific to check for ClusterSet
243 void require_metadata_is_ok() override {}
244
246
247 std::vector<std::tuple<std::string, unsigned long>> fetch_cluster_hosts()
248 override;
249
250 enum class TargetClusterType {
251 // target should be the cluster on which we bootstrap
253 // target should be the Priamry Cluster
255 // target should be the Cluster with the given name
257 };
258
259 std::string get_cluster_type_specific_id() override;
260 uint64_t get_view_id(const std::string &cluster_type_specific_id) override;
261
263 const uint32_t router_id, const std::string &cluster_id,
264 const std::string &target_cluster, const std::string &rw_endpoint,
265 const std::string &ro_endpoint, const std::string &rw_split_endpoint,
266 const std::string &rw_x_endpoint, const std::string &ro_x_endpoint,
267 const std::string &username) override;
268
269 protected:
272};
273
275 public:
280 : ClusterMetadata(schema_version, mysql, sockops) {}
281
282 ~ClusterMetadataAR() override = default;
283
286 }
287
288 void require_cluster_is_ok() override {
289 // Nothing specific to check for ReplicaSet cluster
290 }
291
293
296 }
297
298 std::string get_cluster_type_specific_id() override;
299
300 uint64_t get_view_id(
301 const std::string & /*cluster_type_specific_id*/) override;
302
303 std::vector<std::string> get_routing_mode_queries() override;
304
306 uint32_t router_id, const std::string &hostname_override = "") override;
307
309 const uint32_t router_id, const std::string &cluster_id,
310 const std::string &target_cluster, const std::string &rw_endpoint,
311 const std::string &ro_endpoint, const std::string &rw_split_endpoint,
312 const std::string &rw_x_endpoint, const std::string &ro_x_endpoint,
313 const std::string &username) override;
314
315 uint32_t register_router(const std::string &router_name, const bool overwrite,
316 const std::string &hostname_override = "") override;
317
318 std::vector<std::string> get_grant_statements(
319 const std::string &new_accounts) const override;
320
321 std::vector<std::tuple<std::string, unsigned long>> fetch_cluster_hosts()
322 override;
323
324 protected:
325 uint64_t query_cluster_count() override;
326};
327
328std::unique_ptr<ClusterMetadata> ROUTER_CLUSTER_EXPORT
329create_metadata(const MetadataSchemaVersion &schema_version,
330 MySQLSession *mysql, const OptionsMap &options = {},
333
334} // namespace mysqlrouter
335
336#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:274
void require_cluster_is_ok() override
Verify that host is a valid cluster member (either Group Replication or ReplicaSet cluster)
Definition: cluster_metadata.h:288
InstanceType fetch_current_instance_type() override
Definition: cluster_metadata.h:294
std::vector< std::tuple< std::string, unsigned long > > fetch_cluster_hosts() override
Definition: cluster_metadata.cc:1030
std::vector< std::string > get_grant_statements(const std::string &new_accounts) const override
Definition: cluster_metadata.cc:1245
std::vector< std::string > get_routing_mode_queries() override
Definition: cluster_metadata.cc:1019
mysqlrouter::ClusterType get_type() override
Definition: cluster_metadata.h:284
uint64_t get_view_id(const std::string &) override
Definition: cluster_metadata.cc:1193
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:388
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:122
std::string get_cluster_type_specific_id() override
Definition: cluster_metadata.cc:1001
ClusterMetadataAR(const MetadataSchemaVersion &schema_version, MySQLSession *mysql, mysql_harness::SocketOperationsBase *sockops=mysql_harness::SocketOperations::instance())
Definition: cluster_metadata.h:276
ClusterInfo fetch_metadata_servers() override
Definition: cluster_metadata.cc:997
~ClusterMetadataAR() override=default
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_split_endpoint, const std::string &rw_x_endpoint, const std::string &ro_x_endpoint, const std::string &username) override
Definition: cluster_metadata.cc:315
uint64_t query_cluster_count() override
Definition: cluster_metadata.cc:976
Definition: cluster_metadata.h:228
void require_metadata_is_ok() override
Verify that host is a valid metadata server.
Definition: cluster_metadata.h:243
std::string target_cluster_name_
Definition: cluster_metadata.h:271
std::vector< std::tuple< std::string, unsigned long > > fetch_cluster_hosts() override
Definition: cluster_metadata.cc:867
TargetClusterType target_cluster_type_
Definition: cluster_metadata.h:270
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_split_endpoint, const std::string &rw_x_endpoint, const std::string &ro_x_endpoint, const std::string &username) override
Definition: cluster_metadata.cc:327
mysqlrouter::ClusterType get_type() override
Definition: cluster_metadata.h:238
ClusterInfo fetch_metadata_servers() override
Definition: cluster_metadata.cc:765
std::string get_cluster_type_specific_id() override
Definition: cluster_metadata.cc:879
uint64_t get_view_id(const std::string &cluster_type_specific_id) override
Definition: cluster_metadata.cc:902
TargetClusterType
Definition: cluster_metadata.h:250
ClusterMetadataGRInClusterSet(const MetadataSchemaVersion &schema_version, MySQLSession *mysql, const OptionsMap &, mysql_harness::SocketOperationsBase *sockops=mysql_harness::SocketOperations::instance())
Definition: cluster_metadata.cc:743
Definition: cluster_metadata.h:188
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:116
std::vector< std::string > get_grant_statements(const std::string &new_accounts) const override
Definition: cluster_metadata.cc:1240
std::vector< std::string > get_routing_mode_queries() override
Definition: cluster_metadata.cc:972
uint64_t query_cluster_count() override
Definition: cluster_metadata.cc:649
ClusterInfo fetch_metadata_servers() override
Definition: cluster_metadata.cc:721
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_split_endpoint, const std::string &rw_x_endpoint, const std::string &ro_x_endpoint, const std::string &username) override
Definition: cluster_metadata.cc:303
~ClusterMetadataGRV2() override=default
ClusterMetadataGRV2(const MetadataSchemaVersion &schema_version, MySQLSession *mysql, mysql_harness::SocketOperationsBase *sockops=mysql_harness::SocketOperations::instance())
Definition: cluster_metadata.h:190
mysqlrouter::ClusterType get_type() override
Definition: cluster_metadata.h:198
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:381
InstanceType fetch_current_instance_type() override
Definition: cluster_metadata.cc:725
Definition: cluster_metadata.h:169
ClusterMetadataGR(const MetadataSchemaVersion &schema_version, MySQLSession *mysql, mysql_harness::SocketOperationsBase *sockops=mysql_harness::SocketOperations::instance())
Definition: cluster_metadata.h:171
void require_cluster_is_ok() override
Verify that host is a valid cluster member (either Group Replication or ReplicaSet cluster)
Definition: cluster_metadata.cc:538
std::vector< std::tuple< std::string, unsigned long > > fetch_cluster_hosts() override
Definition: cluster_metadata.cc:572
~ClusterMetadataGR() override=default
std::string get_cluster_type_specific_id() override
Definition: cluster_metadata.cc:610
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:153
virtual mysqlrouter::ClusterType get_type()=0
virtual uint64_t get_view_id(const std::string &)
Definition: cluster_metadata.h:155
mysqlrouter::MetadataSchemaVersion schema_version_
Definition: cluster_metadata.h:166
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
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_split_endpoint, const std::string &rw_x_endpoint, const std::string &ro_x_endpoint, const std::string &username)=0
mysql_harness::SocketOperationsBase * socket_operations_
Definition: cluster_metadata.h:165
MySQLSession * mysql_
Definition: cluster_metadata.h:164
virtual InstanceType fetch_current_instance_type()=0
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:518
virtual uint32_t register_router(const std::string &router_name, const bool overwrite, const std::string &hostname_override="")=0
Registers Router in metadata database.
Definition: mysql_session.h:154
Definition: instrumented_condition_variable.h:32
Definition: base64.h:43
ClusterType
Definition: cluster_metadata.h:140
InstanceType
Definition: cluster_metadata.h:199
std::unique_ptr< ClusterMetadata > create_metadata(const MetadataSchemaVersion &schema_version, MySQLSession *mysql, const OptionsMap &options, mysql_harness::SocketOperationsBase *sockops)
Definition: cluster_metadata.cc:1163
std::map< std::string, std::string > OptionsMap
Definition: cluster_metadata.h:57
Definition: options.cc:57
#define ROUTER_CLUSTER_EXPORT
Definition: router_cluster_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:42