MySQL 8.4.0
Source Code Documentation
cluster_metadata.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2018, 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 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_LIB_EXPORT to_string(const MetadataSchemaVersion &version);
81
82// Semantic version numbers that this Router version supports for bootstrap mode
84
85// Semantic version number that this Router version supports for routing mode
87 {2, 0, 0}};
88
89// Version that introduced views and support for ReplicaSet cluster type
91
92// Version that introduced support for ClusterSets
94
95// Version that will be is set while the metadata is being updated
97
98MetadataSchemaVersion ROUTER_LIB_EXPORT
100
103 const mysqlrouter::MetadataSchemaVersion &available);
104
107
108// throws std::logic_error, MySQLSession::Error
110
111// throws MySQLSession::Error, std::logic_error, std::out_of_range
113
114template <size_t N>
116 const mysqlrouter::MetadataSchemaVersion (&required)[N],
117 const mysqlrouter::MetadataSchemaVersion &available) {
118 for (size_t i = 0; i < N; ++i) {
120 return true;
121 }
122
123 return false;
124}
125
126template <size_t N>
128 std::string result;
129 for (size_t i = 0; i < N; ++i) {
130 result += to_string(version[i]);
131 if (i != N - 1) {
132 result += ", ";
133 }
134 }
135
136 return result;
137}
138
139enum class ClusterType {
140 GR_V2, /* based on Group Replication (metadata 2.x) */
141 GR_CS, /* based on Group Replication, part of ClusterSet (metadata 2.1+) */
142 RS_V2 /* ReplicaSet (metadata 2.x) */
143};
144
146get_cluster_type(const MetadataSchemaVersion &schema_version,
147 MySQLSession *mysql, unsigned int router_id = 0);
148
149std::string ROUTER_LIB_EXPORT to_string(const ClusterType cluster_type);
150
151class MetadataUpgradeInProgressException : public std::exception {};
152
155
157
159 public:
162
164 const std::string &value = "")
165 : target_type_(type), target_value_(value) {
167 }
168
169 std::string to_string() const { return target_value_; }
170 const char *c_str() const { return target_value_.c_str(); }
171
175 }
176
177 void target_type(const TargetType value) { target_type_ = value; }
178 void target_value(const std::string &value) { target_value_ = value; }
182 }
183
184 private:
186 std::string target_value_;
189};
190
191constexpr const std::string_view kNodeTagHidden{"_hidden"};
192constexpr const std::string_view kNodeTagDisconnectWhenHidden{
193 "_disconnect_existing_sessions_when_hidden"};
194
195constexpr const bool kNodeTagHiddenDefault{false};
196constexpr const bool kNodeTagDisconnectWhenHiddenDefault{true};
197
199
200std::optional<InstanceType> ROUTER_LIB_EXPORT
201str_to_instance_type(const std::string &);
202
203std::string ROUTER_LIB_EXPORT to_string(const InstanceType);
204std::string ROUTER_LIB_EXPORT
206
207constexpr const std::chrono::milliseconds kDefaultMetadataTTLCluster{500};
208constexpr const std::chrono::milliseconds
210 std::chrono::milliseconds(60 * 1000);
211constexpr const std::chrono::milliseconds kDefaultMetadataTTLClusterSet{
212 5000}; // default TTL for ClusterSet is 5 seconds regardless if GR
213 // Notifications are used or not
216
217} // namespace mysqlrouter
218#endif
Definition: cluster_metadata.h:151
Definition: mysql_session.h:153
Definition: cluster_metadata.h:158
void invalidated_cluster_routing_policy(const InvalidatedClusterRoutingPolicy value)
Definition: cluster_metadata.h:179
InvalidatedClusterRoutingPolicy invalidated_cluster_routing_policy() const
Definition: cluster_metadata.h:173
void target_value(const std::string &value)
Definition: cluster_metadata.h:178
TargetCluster(const TargetType type=TargetType::ByPrimaryRole, const std::string &value="")
Definition: cluster_metadata.h:163
TargetType
Definition: cluster_metadata.h:160
InvalidatedClusterRoutingPolicy
Definition: cluster_metadata.h:161
void target_type(const TargetType value)
Definition: cluster_metadata.h:177
std::string target_value_
Definition: cluster_metadata.h:186
TargetType target_type_
Definition: cluster_metadata.h:185
InvalidatedClusterRoutingPolicy invalidated_cluster_routing_policy_
Definition: cluster_metadata.h:187
std::string to_string() const
Definition: cluster_metadata.h:169
const char * c_str() const
Definition: cluster_metadata.h:170
TargetType target_type() const
Definition: cluster_metadata.h:172
Definition: expected.h:284
std::atomic< Type > N
Definition: ut0counter.h:225
Definition: instrumented_condition_variable.h:32
Definition: dim.h:358
std::string ROUTER_LIB_EXPORT get_metadata_schema_uncompatible_msg(const mysqlrouter::MetadataSchemaVersion &version)
Definition: cluster_metadata.cc:411
constexpr MetadataSchemaVersion kClusterSetsMetadataVersion
Definition: cluster_metadata.h:93
constexpr MetadataSchemaVersion kNewMetadataVersion
Definition: cluster_metadata.h:90
bool ROUTER_LIB_EXPORT is_part_of_cluster_set(MySQLSession *mysql)
Definition: cluster_metadata.cc:1084
ClusterType
Definition: cluster_metadata.h:139
InstanceType
Definition: cluster_metadata.h:198
constexpr MetadataSchemaVersion kUpgradeInProgressMetadataVersion
Definition: cluster_metadata.h:96
constexpr MetadataSchemaVersion kRequiredRoutingMetadataSchemaVersion[]
Definition: cluster_metadata.h:86
constexpr const std::string_view kNodeTagDisconnectWhenHidden
Definition: cluster_metadata.h:192
constexpr const std::string_view kNodeTagHidden
Definition: cluster_metadata.h:191
MetadataSchemaVersion ROUTER_LIB_EXPORT get_metadata_schema_version(MySQLSession *mysql)
Definition: cluster_metadata.cc:425
stdx::expected< void, std::string > ROUTER_LIB_EXPORT setup_metadata_session(MySQLSession &session)
Definition: cluster_metadata.cc:1257
bool ROUTER_LIB_EXPORT check_group_has_quorum(MySQLSession *mysql)
Definition: cluster_metadata.cc:492
constexpr MetadataSchemaVersion kRequiredBootstrapSchemaVersion[]
Definition: cluster_metadata.h:83
constexpr const std::chrono::milliseconds kDefaultMetadataTTLClusterGRNotificationsON
Definition: cluster_metadata.h:209
ClusterType ROUTER_LIB_EXPORT get_cluster_type(const MetadataSchemaVersion &schema_version, MySQLSession *mysql, unsigned int router_id=0)
Definition: cluster_metadata.cc:1125
constexpr const std::chrono::milliseconds kDefaultMetadataTTLClusterSet
Definition: cluster_metadata.h:211
const bool kDefaultUseGRNotificationsClusterSet
Definition: cluster_metadata.h:215
bool ROUTER_LIB_EXPORT check_group_replication_online(MySQLSession *mysql)
Definition: cluster_metadata.cc:476
bool ROUTER_LIB_EXPORT metadata_schema_version_is_compatible(const mysqlrouter::MetadataSchemaVersion &required, const mysqlrouter::MetadataSchemaVersion &available)
Definition: cluster_metadata.cc:397
constexpr const std::chrono::milliseconds kDefaultMetadataTTLCluster
Definition: cluster_metadata.h:207
constexpr const bool kNodeTagDisconnectWhenHiddenDefault
Definition: cluster_metadata.h:196
constexpr const bool kNodeTagHiddenDefault
Definition: cluster_metadata.h:195
std::optional< InstanceType > ROUTER_LIB_EXPORT str_to_instance_type(const std::string &)
Definition: cluster_metadata.cc:1285
std::string ROUTER_LIB_EXPORT to_string(const MetadataSchemaVersion &version)
Definition: cluster_metadata.cc:420
const bool kDefaultUseGRNotificationsCluster
Definition: cluster_metadata.h:214
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_LIB_EXPORT
Definition: router_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