MySQL 8.4.7
Source Code Documentation
cluster_metadata.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2018, 2025, 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_CLUSTER_METADATA_INCLUDED
27#define MYSQLROUTER_CLUSTER_METADATA_INCLUDED
28
30
31#include <chrono>
32#include <optional>
33#include <stdexcept>
34#include <string>
35
37
38namespace mysqlrouter {
39
40class MySQLSession;
41
43 unsigned int major;
44 unsigned int minor;
45 unsigned int patch;
46
47 bool operator<(const MetadataSchemaVersion &o) const {
48 if (major == o.major) {
49 if (minor == o.minor) {
50 return patch < o.patch;
51 } else {
52 return minor < o.minor;
53 }
54 } else {
55 return major < o.major;
56 }
57 }
58
59 bool operator<=(const MetadataSchemaVersion &o) const {
60 return operator<(o) || operator==(o);
61 }
62
63 bool operator>(const MetadataSchemaVersion &o) const {
64 return operator!=(o) && !operator<(o);
65 }
66
67 bool operator>=(const MetadataSchemaVersion &o) const {
68 return operator>(o) || operator==(o);
69 }
70
71 bool operator==(const MetadataSchemaVersion &o) const {
72 return major == o.major && minor == o.minor && patch == o.patch;
73 }
74
75 bool operator!=(const MetadataSchemaVersion &o) const {
76 return !operator==(o);
77 }
78};
79
80std::string ROUTER_CLUSTER_EXPORT
81to_string(const MetadataSchemaVersion &version);
82
83// Semantic version numbers that this Router version supports for bootstrap mode
85
86// Semantic version number that this Router version supports for routing mode
88 {2, 0, 0}};
89
90// Version that introduced views and support for ReplicaSet cluster type
92
93// Version that introduced support for ClusterSets
95
96// Version that will be is set while the metadata is being updated
98
99MetadataSchemaVersion ROUTER_CLUSTER_EXPORT
101
104 const mysqlrouter::MetadataSchemaVersion &available);
105
108
109// throws std::logic_error, MySQLSession::Error
111
112// throws MySQLSession::Error, std::logic_error, std::out_of_range
114
116
117std::string ROUTER_CLUSTER_EXPORT
119
120template <size_t N>
122 const mysqlrouter::MetadataSchemaVersion (&required)[N],
123 const mysqlrouter::MetadataSchemaVersion &available) {
124 for (size_t i = 0; i < N; ++i) {
126 return true;
127 }
128
129 return false;
130}
131
132template <size_t N>
134 std::string result;
135 for (size_t i = 0; i < N; ++i) {
136 result += to_string(version[i]);
137 if (i != N - 1) {
138 result += ", ";
139 }
140 }
141
142 return result;
143}
144
145enum class ClusterType {
146 GR_V2, /* based on Group Replication (metadata 2.x) */
147 GR_CS, /* based on Group Replication, part of ClusterSet (metadata 2.1+) */
148 RS_V2 /* ReplicaSet (metadata 2.x) */
149};
150
152 const MetadataSchemaVersion &schema_version, MySQLSession *mysql);
153
155get_cluster_type(const MetadataSchemaVersion &schema_version,
156 MySQLSession *mysql, unsigned int router_id);
157
158std::string ROUTER_CLUSTER_EXPORT to_string(const ClusterType cluster_type);
159
160class MetadataUpgradeInProgressException : public std::exception {};
161
164
166
168 public:
171
173 const std::string &value = "")
174 : target_type_(type), target_value_(value) {
176 }
177
178 std::string to_string() const { return target_value_; }
179 const char *c_str() const { return target_value_.c_str(); }
180
184 }
185
186 void target_type(const TargetType value) { target_type_ = value; }
187 void target_value(const std::string &value) { target_value_ = value; }
191 }
192
193 private:
195 std::string target_value_;
198};
199
200constexpr const std::string_view kNodeTagHidden{"_hidden"};
201constexpr const std::string_view kNodeTagDisconnectWhenHidden{
202 "_disconnect_existing_sessions_when_hidden"};
203
204constexpr const bool kNodeTagHiddenDefault{false};
205constexpr const bool kNodeTagDisconnectWhenHiddenDefault{true};
206
208
209std::optional<InstanceType> ROUTER_CLUSTER_EXPORT
210str_to_instance_type(const std::string &);
211
213std::string ROUTER_CLUSTER_EXPORT
215
216constexpr const std::chrono::milliseconds kDefaultMetadataTTLCluster{500};
217constexpr const std::chrono::milliseconds
219 std::chrono::milliseconds(60 * 1000);
220constexpr const std::chrono::milliseconds kDefaultMetadataTTLClusterSet{
221 5000}; // default TTL for ClusterSet is 5 seconds regardless if GR
222 // Notifications are used or not
225
226} // namespace mysqlrouter
227#endif
Definition: cluster_metadata.h:160
Definition: mysql_session.h:153
Definition: cluster_metadata.h:167
void invalidated_cluster_routing_policy(const InvalidatedClusterRoutingPolicy value)
Definition: cluster_metadata.h:188
InvalidatedClusterRoutingPolicy invalidated_cluster_routing_policy() const
Definition: cluster_metadata.h:182
void target_value(const std::string &value)
Definition: cluster_metadata.h:187
TargetCluster(const TargetType type=TargetType::ByPrimaryRole, const std::string &value="")
Definition: cluster_metadata.h:172
TargetType
Definition: cluster_metadata.h:169
InvalidatedClusterRoutingPolicy
Definition: cluster_metadata.h:170
void target_type(const TargetType value)
Definition: cluster_metadata.h:186
std::string target_value_
Definition: cluster_metadata.h:195
TargetType target_type_
Definition: cluster_metadata.h:194
InvalidatedClusterRoutingPolicy invalidated_cluster_routing_policy_
Definition: cluster_metadata.h:196
std::string to_string() const
Definition: cluster_metadata.h:178
const char * c_str() const
Definition: cluster_metadata.h:179
TargetType target_type() const
Definition: cluster_metadata.h:181
Definition: expected.h:286
std::atomic< Type > N
Definition: ut0counter.h:225
Definition: instrumented_condition_variable.h:32
Definition: base64.h:43
constexpr MetadataSchemaVersion kClusterSetsMetadataVersion
Definition: cluster_metadata.h:94
constexpr MetadataSchemaVersion kNewMetadataVersion
Definition: cluster_metadata.h:91
bool ROUTER_CLUSTER_EXPORT is_part_of_cluster_set(MySQLSession *mysql)
Definition: cluster_metadata.cc:1082
ClusterType
Definition: cluster_metadata.h:145
InstanceType
Definition: cluster_metadata.h:207
std::string ROUTER_CLUSTER_EXPORT get_unsupported_server_version_msg(MySQLSession *mysql)
Definition: cluster_metadata.cc:1339
constexpr MetadataSchemaVersion kUpgradeInProgressMetadataVersion
Definition: cluster_metadata.h:97
constexpr MetadataSchemaVersion kRequiredRoutingMetadataSchemaVersion[]
Definition: cluster_metadata.h:87
constexpr const std::string_view kNodeTagDisconnectWhenHidden
Definition: cluster_metadata.h:201
ClusterType ROUTER_CLUSTER_EXPORT get_cluster_type(const MetadataSchemaVersion &schema_version, MySQLSession *mysql)
Definition: cluster_metadata.cc:1125
bool ROUTER_CLUSTER_EXPORT is_server_version_supported(MySQLSession *mysql)
Definition: cluster_metadata.cc:1335
constexpr const std::string_view kNodeTagHidden
Definition: cluster_metadata.h:200
MetadataSchemaVersion ROUTER_CLUSTER_EXPORT get_metadata_schema_version(MySQLSession *mysql)
Definition: cluster_metadata.cc:423
std::string ROUTER_CLUSTER_EXPORT get_metadata_schema_uncompatible_msg(const mysqlrouter::MetadataSchemaVersion &version)
Definition: cluster_metadata.cc:409
stdx::expected< void, std::string > ROUTER_CLUSTER_EXPORT setup_metadata_session(MySQLSession &session)
Definition: cluster_metadata.cc:1265
bool ROUTER_CLUSTER_EXPORT check_group_has_quorum(MySQLSession *mysql)
Definition: cluster_metadata.cc:490
constexpr MetadataSchemaVersion kRequiredBootstrapSchemaVersion[]
Definition: cluster_metadata.h:84
constexpr const std::chrono::milliseconds kDefaultMetadataTTLClusterGRNotificationsON
Definition: cluster_metadata.h:218
constexpr const std::chrono::milliseconds kDefaultMetadataTTLClusterSet
Definition: cluster_metadata.h:220
const bool kDefaultUseGRNotificationsClusterSet
Definition: cluster_metadata.h:224
bool ROUTER_CLUSTER_EXPORT check_group_replication_online(MySQLSession *mysql)
Definition: cluster_metadata.cc:474
bool ROUTER_CLUSTER_EXPORT metadata_schema_version_is_compatible(const mysqlrouter::MetadataSchemaVersion &required, const mysqlrouter::MetadataSchemaVersion &available)
Definition: cluster_metadata.cc:395
constexpr const std::chrono::milliseconds kDefaultMetadataTTLCluster
Definition: cluster_metadata.h:216
constexpr const bool kNodeTagDisconnectWhenHiddenDefault
Definition: cluster_metadata.h:205
constexpr const bool kNodeTagHiddenDefault
Definition: cluster_metadata.h:204
std::optional< InstanceType > ROUTER_CLUSTER_EXPORT str_to_instance_type(const std::string &)
Definition: cluster_metadata.cc:1293
std::string ROUTER_CLUSTER_EXPORT to_string(const MetadataSchemaVersion &version)
Definition: cluster_metadata.cc:418
const bool kDefaultUseGRNotificationsCluster
Definition: cluster_metadata.h:223
struct result result
Definition: result.h:34
required uint64 version
Definition: replication_group_member_actions.proto:41
required string type
Definition: replication_group_member_actions.proto:34
#define ROUTER_CLUSTER_EXPORT
Definition: router_cluster_export.h:15
Definition: cluster_metadata.h:42
bool operator<(const MetadataSchemaVersion &o) const
Definition: cluster_metadata.h:47
bool operator>(const MetadataSchemaVersion &o) const
Definition: cluster_metadata.h:63
bool operator==(const MetadataSchemaVersion &o) const
Definition: cluster_metadata.h:71
bool operator>=(const MetadataSchemaVersion &o) const
Definition: cluster_metadata.h:67
unsigned int minor
Definition: cluster_metadata.h:44
unsigned int major
Definition: cluster_metadata.h:43
unsigned int patch
Definition: cluster_metadata.h:45
bool operator!=(const MetadataSchemaVersion &o) const
Definition: cluster_metadata.h:75
bool operator<=(const MetadataSchemaVersion &o) const
Definition: cluster_metadata.h:59
Definition: result.h:30