MySQL 9.0.0
Source Code Documentation
gtidset.h
Go to the documentation of this file.
1/* Copyright (c) 2021, 2024, Oracle and/or its affiliates.
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License, version 2.0,
5 as published by the Free Software Foundation.
6
7 This program is designed to work with certain software (including
8 but not limited to OpenSSL) that is licensed under separate terms,
9 as designated in a particular file or component or in included license
10 documentation. The authors of MySQL hereby grant you an additional
11 permission to link the program and your derivative works with the
12 separately licensed software that they have either included with
13 the program or referenced in the documentation.
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, version 2.0, 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#ifndef MYSQL_GTID_GTID_SET_H
25#define MYSQL_GTID_GTID_SET_H
26
27#include <cstddef>
28#include <map>
29#include <set>
30#include <sstream>
31
33#include "mysql/gtid/global.h"
34#include "mysql/gtid/gtid.h"
35#include "mysql/gtid/tag.h"
36#include "mysql/gtid/tsid.h"
37
38/// @addtogroup GroupLibsMysqlGtid
39/// @{
40
41namespace mysql::gtid {
42
43/**
44 * @brief This class represents a range of transaction identifiers.
45 *
46 * A transaction identifier is composed of two parts, the UUID and the sequence
47 * number.
48 *
49 * There can be multiple transaction identifiers for a given UUID. When their
50 * sequence number are contiguous they can be represented as an interval. This
51 * is the class that represents on of those intervals.
52 *
53 */
55 public:
56 /// In 'UUID:GNO-GNO', this is the '-'
57 static const inline std::string SEPARATOR_GNO_START_END{"-"};
58
59 private:
62
63 public:
64 virtual ~Gno_interval() = default;
65
66 /**
67 * @brief Copy assignment.
68 *
69 * @note This operator does not perform any check about the other interval
70 * being valid or not. It is up to the caller to verify that if needed.
71 *
72 * @param other The other interval to copy.
73 * @return Gno_interval& a reference to the copied interval.
74 */
75 Gno_interval &operator=(const Gno_interval &other);
76
77 /**
78 * @brief Construct a new Gno_interval object from the other one provided.
79 *
80 * @note This operator does not perform any check about the other interval
81 * being valid or not. It is up to the caller to verify that if needed.
82 *
83 * @param other The object to copy.
84 */
85 Gno_interval(const Gno_interval &other);
86
87 /**
88 * @brief Compares this interval with another one. Returns true if there is a
89 * match.
90 *
91 * @note This operator does not perform any check about the other interval
92 * being valid or not. It is up to the caller to verify that if needed, before
93 * calling this member function.
94 *
95 * @param other The other interval to compare this one with.
96 * @return true If both intervals match.
97 * @return false If intervals do not match.
98 */
99 virtual bool operator==(const Gno_interval &other) const;
100
101 /**
102 * @brief Construct a new Gno_interval object.
103 *
104 * @param start The start of the interval (inclusive).
105 * @param end The end of the interval (inclusive).
106 */
108
109 /**
110 * @brief Establishes a total order between two intervals.
111 *
112 * Total order is determined using the start and end values of
113 * the interval.
114 *
115 * An interval A comes before interval B if its start value is smaller
116 * than the start value of B.
117 *
118 * If B's start value is smaller than A's start value then B comes
119 * before A.
120 *
121 * If A and B have the same start value, the the end of the intervals
122 * are checked and if A has a lower interval end than B, then A is
123 * considered to come before B in that case.
124 *
125 * @note This operator does not perform any check about the other interval
126 * being valid or not. It is up to the caller to verify that if needed, before
127 * calling this member function.
128 *
129 * @param other the other interval.
130 * @return true if this is smaller than the other
131 * @return false if this is equal or greater than the other.
132 */
133 virtual bool operator<(const Gno_interval &other) const;
134
135 /**
136 * @brief This checks whether this interval intersects with the other.
137 *
138 * @note This operator does not perform any check about the other interval
139 * being valid or not. It is up to the caller to verify that if needed, before
140 * calling this member function.
141 *
142 * @param other The other interval to check against this one.
143 * @return true if the intervals intersect.
144 * @return false if it does not intersect.
145 */
146 virtual bool intersects(const Gno_interval &other) const;
147
148 /**
149 * @brief Checks if this interval is contiguous with the other one.
150 *
151 * Two intervals are contiguous if they do not intersect but there
152 * are no gaps between them. No gaps means that the upper limit of
153 * interval A is the value immediately preceding the lower limit
154 * of interval B, under numeric natural ordering.
155 *
156 * @note This operator does not perform any check about the other interval
157 * being valid or not. It is up to the caller to verify that if needed, before
158 * calling this member function.
159 *
160 * @param other the interval to check against.
161 * @return true if the intervals are contiguous.
162 * @return false otherwise.
163 */
164 virtual bool contiguous(const Gno_interval &other) const;
165
166 /**
167 * @brief Checks if the other interval intersects or is contiguous with this
168 * one.
169 *
170 * @param other The interval to check against this one.
171 * @return true if the other interval intersects or is contiguous with this
172 * one.
173 * @return false otherwise.
174 */
175 virtual bool intersects_or_contiguous(const Gno_interval &other) const;
176
177 /**
178 * @brief Adds the other interval to this one.
179 *
180 * For this operation to complete successfully, the intervals must
181 * intersect or be contiguous. Otherwise, the operation will fail.
182 *
183 * @note This operator does not perform any check about the other interval
184 * being valid or not. It is up to the caller to verify that if needed, before
185 * calling this member function.
186 *
187 * @param other The interval to add to this one.
188 * @return true if the adding was not successful.
189 * @return false if the adding was successful.
190 */
191 virtual bool add(const Gno_interval &other);
192
193 /**
194 * @brief Gets the first sequence number in the interval.
195 *
196 * @return the first sequence number of the interval.
197 */
198 virtual gno_t get_start() const;
199
200 /**
201 * @brief Gets the last sequence number in the interval.
202 *
203 * @return the last sequence number in the interval.
204 */
205 virtual gno_t get_end() const;
206
207 /**
208 * @brief Number of entries in this interval.
209 *
210 * @return the size of the interval.
211 */
212 virtual std::size_t count() const;
213
214 /**
215 * @brief Gets a human readable representation of this identifier.
216 *
217 * @return A human readable representation of this identifier.
218 */
219 virtual std::string to_string() const;
220
221 /**
222 * @brief Checks whether this interval is valid or not.
223 *
224 * An interval is invalid if the start value is smaller than 0 or
225 * if the start is larger than the end of the interval.
226 *
227 * @return true if the interval is valid, false otherwise.
228 */
229 virtual bool is_valid() const;
230};
231
232/**
233 * @brief This class represents a set of transaction identifiers.
234 *
235 * A set of transaction identifiers contains zero or more entries. When there
236 * are multiple entries, there can multiple intervals as well. Different
237 * intervals may share the same UUID part or not. This class abstracts that
238 * in-memory representation.
239 *
240 */
241class Gtid_set {
242 public:
243 static const inline std::string empty_gtid_set_str{""};
244 /// In 'UUID:INTERVAL:INTERVAL', this is the second ':'
245 static const inline std::string separator_interval{":"};
246 /// In 'SID:GNO,SID:GNO', this is the ','
247 static const inline std::string separator_uuid_set{","};
250
251 protected:
252 [[NODISCARD]] virtual bool do_add(const Tsid &tsid,
253 const Gno_interval &interval);
254 [[NODISCARD]] virtual bool do_add(const Uuid &uuid, const Tag &tag,
255 const Gno_interval &interval);
256
257 public:
258 /**
259 * @brief Tsid_interval_map is a 2-level map between tsids and an ordered
260 * set of Gno_intervals.
261 * Uuid is mapped into GTID tags, each pair of Uuid and Tag (TSID) is mapped
262 * into ordered set of gno intervals
263 */
264 using Interval_set = std::set<Gno_interval>;
265 using Tag_interval_map = std::map<Tag, Interval_set>;
266 using Tsid_interval_map = std::map<Uuid, Tag_interval_map>;
267
268 Gtid_set() = default;
269 virtual ~Gtid_set();
270
271 Gtid_set(const Gtid_set &other) = delete;
272
273 /**
274 * @brief Copy assignment.
275 *
276 * @note This operator does not check whether the parameters are valid
277 * or not. The caller should perform such check before calling this member
278 * function.
279 *
280 * @param other the Gtid_set to be copied over to this one.
281 * @return Gtid_set& a reference to the copied Gtid_set.
282 */
283 Gtid_set &operator=(const Gtid_set &other);
284
285 /**
286 * @brief Compares this set with another one.
287 *
288 * @param other The other set to compare this one with.
289 * @return true If both sets match.
290 * @return false If sets do not match.
291 */
292 virtual bool operator==(const Gtid_set &other) const;
293
294 /**
295 * @brief Iterates through recorded TSIDs and returns
296 * format of the Gtid_set
297 *
298 * @return Format of this GTID set
299 * @see Gtid_format
300 */
301 virtual Gtid_format get_gtid_set_format() const;
302
303 /**
304 * @brief Adds a new interval indexed by the given uuid.
305 *
306 * @note This member function does not check whether the parameters are valid
307 * or not. The caller should perform such check before calling this member
308 * function.
309 *
310 * @return true if the there was an error adding the interval, false
311 * otherwise.
312 */
313 [[NODISCARD]] virtual bool add(const Tsid &tsid,
314 const Gno_interval &interval);
315
316 /**
317 * @brief Gets a copy of the internal set.
318 *
319 * @return an internal copy of the given set.
320 */
321 virtual const Tsid_interval_map &get_gtid_set() const;
322
323 /**
324 * @brief Add a set of identifiers to this one.
325 *
326 * @note This member function does not check whether the parameters are valid
327 * or not. The caller should perform such check before calling this member
328 * function.
329 *
330 * @param other the set to add to this one.
331 * @return true if there was a failure adding the gtids.
332 * @return false otherwise.
333 */
334 virtual bool add(const Gtid_set &other);
335
336 /**
337 * @brief Get the num TSIDs held in the GTID set
338 *
339 * @return std::size_t Number of TSIDs
340 */
341 virtual std::size_t get_num_tsids() const;
342
343 /**
344 * @brief Adds the given identifier to this set.
345 *
346 * @note This member function does not check whether the parameters are valid
347 * or not. The caller should perform such check before calling this member
348 * function.
349 *
350 * @param gtid the identifier to add.
351 * @return true if it failed while adding the identifier.
352 * @return false otherwise.
353 */
354 virtual bool add(const Gtid &gtid);
355
356 /**
357 * @brief Checks whether this set contains the given identifier.
358 *
359 * @note This member function does not check whether the parameters are valid
360 * or not. The caller should perform such check before calling this member
361 * function.
362 *
363 * @param gtid the gtid to check whehther it exists in this set or not.
364 * @return true if the identifier exists in this set.
365 * @return false if the identifier does not exist in this set.
366 */
367 virtual bool contains(const Gtid &gtid) const;
368
369 /**
370 * @brief A human readable representation of this set.
371 *
372 * @return a human readable representation of this set.
373 */
374 virtual std::string to_string() const;
375
376 /**
377 * @brief Resets this set, making it empty.
378 *
379 */
380 virtual void reset();
381
382 /**
383 * @brief Returns true if this is an empty set.
384 *
385 * @return true
386 * @return false
387 */
388 virtual bool is_empty() const;
389
390 /**
391 * @brief Gets the number of entries in this set.
392 *
393 * @return the cardinality of this set.
394 */
395 virtual std::size_t count() const;
396
397 protected:
398 /**
399 * @brief An ordered map of entries mapping Uuid to a list of intervals.
400 */
402};
403
404} // namespace mysql::gtid
405
406/// @}
407
408#endif // MYSQL_GTID_GTID_SET_H
This class represents a range of transaction identifiers.
Definition: gtidset.h:54
virtual bool intersects_or_contiguous(const Gno_interval &other) const
Checks if the other interval intersects or is contiguous with this one.
Definition: gtidset.cpp:75
virtual bool operator==(const Gno_interval &other) const
Compares this interval with another one.
Definition: gtidset.cpp:46
static const std::string SEPARATOR_GNO_START_END
In 'UUID:GNO-GNO', this is the '-'.
Definition: gtidset.h:57
gno_t m_start
Definition: gtidset.h:60
virtual ~Gno_interval()=default
Gno_interval(const Gno_interval &other)
Construct a new Gno_interval object from the other one provided.
Definition: gtidset.cpp:37
virtual gno_t get_start() const
Gets the first sequence number in the interval.
Definition: gtidset.cpp:79
virtual std::string to_string() const
Gets a human readable representation of this identifier.
Definition: gtidset.cpp:91
gno_t m_next_gno_after_end
Definition: gtidset.h:61
Gno_interval & operator=(const Gno_interval &other)
Copy assignment.
Definition: gtidset.cpp:40
virtual bool intersects(const Gno_interval &other) const
This checks whether this interval intersects with the other.
Definition: gtidset.cpp:59
virtual bool add(const Gno_interval &other)
Adds the other interval to this one.
Definition: gtidset.cpp:82
virtual gno_t get_end() const
Gets the last sequence number in the interval.
Definition: gtidset.cpp:80
virtual std::size_t count() const
Number of entries in this interval.
Definition: gtidset.cpp:30
virtual bool operator<(const Gno_interval &other) const
Establishes a total order between two intervals.
Definition: gtidset.cpp:51
virtual bool contiguous(const Gno_interval &other) const
Checks if this interval is contiguous with the other one.
Definition: gtidset.cpp:69
virtual bool is_valid() const
Checks whether this interval is valid or not.
Definition: gtidset.cpp:100
This class represents a set of transaction identifiers.
Definition: gtidset.h:241
virtual std::size_t count() const
Gets the number of entries in this set.
Definition: gtidset.cpp:277
virtual std::string to_string() const
A human readable representation of this set.
Definition: gtidset.cpp:225
Gtid_set(const Gtid_set &other)=delete
static const std::string separator_uuid_set
In 'SID:GNO,SID:GNO', this is the ','.
Definition: gtidset.h:247
virtual bool operator==(const Gtid_set &other) const
Compares this set with another one.
Definition: gtidset.cpp:113
virtual bool do_add(const Tsid &tsid, const Gno_interval &interval)
Definition: gtidset.cpp:159
std::map< Uuid, Tag_interval_map > Tsid_interval_map
Definition: gtidset.h:266
std::set< Gno_interval > Interval_set
Tsid_interval_map is a 2-level map between tsids and an ordered set of Gno_intervals.
Definition: gtidset.h:264
static const std::string separator_interval
In 'UUID:INTERVAL:INTERVAL', this is the second ':'.
Definition: gtidset.h:245
virtual Gtid_format get_gtid_set_format() const
Iterates through recorded TSIDs and returns format of the Gtid_set.
Definition: gtidset.cpp:293
static const std::string empty_gtid_set_str
Definition: gtidset.h:243
virtual bool add(const Tsid &tsid, const Gno_interval &interval)
Adds a new interval indexed by the given uuid.
Definition: gtidset.cpp:205
virtual void reset()
Resets this set, making it empty.
Definition: gtidset.cpp:291
virtual const Tsid_interval_map & get_gtid_set() const
Gets a copy of the internal set.
Definition: gtidset.cpp:155
virtual bool contains(const Gtid &gtid) const
Checks whether this set contains the given identifier.
Definition: gtidset.cpp:252
virtual bool is_empty() const
Returns true if this is an empty set.
Definition: gtidset.cpp:275
std::map< Tag, Interval_set > Tag_interval_map
Definition: gtidset.h:265
Tsid_interval_map m_gtid_set
An ordered map of entries mapping Uuid to a list of intervals.
Definition: gtidset.h:401
Gtid_set & operator=(const Gtid_set &other)
Copy assignment.
Definition: gtidset.cpp:106
virtual std::size_t get_num_tsids() const
Get the num TSIDs held in the GTID set.
Definition: gtidset.cpp:267
Represents a MySQL Global Transaction Identifier.
Definition: gtid.h:51
Representation of the GTID tag.
Definition: tag.h:51
Represents Transaction Source Identifier which is composed of source UUID and transaction tag.
Definition: tsid.h:47
static void start(mysql_harness::PluginFuncEnv *env)
Definition: http_auth_backend_plugin.cc:180
static int interval
Definition: mysqladmin.cc:71
Definition: global.h:35
Gtid_format
Gtid binary format indicator.
Definition: gtid_format.h:38
std::int64_t gno_t
Definition: global.h:37
#define NODISCARD
The function attribute [[NODISCARD]] is a replacement for [[nodiscard]] to workaround a gcc bug.
Definition: nodiscard.h:47
mysql::gtid::Tsid Tsid
Definition: rpl_gtid_state.cc:56
Uuid is a trivial and of standard layout The structure contains the following components.
Definition: uuid.h:64