MySQL 8.4.0
Source Code Documentation
rpl_gtid.h
Go to the documentation of this file.
1/* Copyright (c) 2011, 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 RPL_GTID_H_INCLUDED
25#define RPL_GTID_H_INCLUDED
26
27#include <atomic>
28#include <cinttypes>
29#include <list>
30#include <mutex> // std::adopt_lock_t
31#include <vector>
32
33#include "map_helpers.h"
34#include "my_dbug.h"
35#include "my_thread_local.h"
36#include "mysql/binlog/event/compression/base.h" // mysql::binlog::event::compression::type
37#include "mysql/gtid/global.h"
38#include "mysql/gtid/gtid.h"
39#include "mysql/gtid/tsid.h"
40#include "mysql/gtid/uuid.h"
42#include "mysql/psi/mysql_rwlock.h" // mysql_rwlock_t
43#include "mysql/strings/m_ctype.h" // my_isspace
45#include "prealloced_array.h" // Prealloced_array
46#include "sql/changestreams/index/locked_sidno_set.h" // Locked_sidno_set
48#include "sql/rpl_reporting.h" // MAX_SLAVE_ERRMSG
49#include "template_utils.h"
50
51class Table_ref;
52class THD;
53
54/**
55 Report an error from code that can be linked into either the server
56 or mysqlbinlog. There is no common error reporting mechanism, so we
57 have to duplicate the error message (write it out in the source file
58 for mysqlbinlog, write it in share/messages_to_clients.txt for the
59 server).
60
61 @param MYSQLBINLOG_ERROR arguments to mysqlbinlog's 'error'
62 function, including the function call parentheses
63 @param SERVER_ERROR arguments to my_error, including the function
64 call parentheses.
65*/
66#ifndef MYSQL_SERVER
67#define BINLOG_ERROR(MYSQLBINLOG_ERROR, SERVER_ERROR) error MYSQLBINLOG_ERROR
68#else
69#define BINLOG_ERROR(MYSQLBINLOG_ERROR, SERVER_ERROR) my_error SERVER_ERROR
70#endif
71
78
79/**
80 This macro is used to check that the given character, pointed to by the
81 character pointer, is a space or not.
82*/
83#define SKIP_WHITESPACE() \
84 while (my_isspace(&my_charset_utf8mb3_general_ci, *s)) s++
85
86/*
87 This macro must be used to filter out parts of the code that
88 is not used now but may be useful in future. In other words,
89 we want to keep such code until we make up our minds on whether
90 it should be removed or not.
91*/
92#undef NON_DISABLED_GTID
93
94/*
95 This macro must be used to filter out parts of the code that
96 is not used now but we are not sure if there is a bug around
97 them. In other words, we want to keep such code until we have
98 time to investigate it.
99*/
100#undef NON_ERROR_GTID
101
102#ifdef MYSQL_SERVER
103class String;
104class THD;
105#endif // ifdef MYSQL_SERVER
106
107/// Type of SIDNO (source ID number, first component of GTID)
109
110/// GNO, the second (numeric) component of a GTID, is an alias of
111/// mysql::gtid::gno_t
114
115namespace cs::index {
116
117class Locked_sidno_set;
118
119} // namespace cs::index
120
121/**
122 Generic return type for many functions that can succeed or fail.
123
124 This is used in conjunction with the macros below for functions where
125 the return status either indicates "success" or "failure". It
126 provides the following features:
127
128 - The macros can be used to conveniently propagate errors from
129 called functions back to the caller.
130
131 - If a function is expected to print an error using my_error before
132 it returns an error status, then the macros assert that my_error
133 has been called.
134
135 - Does a DBUG_PRINT before returning failure.
136*/
138 /// The function completed successfully.
140 /// The function completed with error but did not report it.
142 /// The function completed with error and has called my_error.
145
146/**
147 @def __CHECK_RETURN_STATUS
148 Lowest level macro used in the PROPAGATE_* and RETURN_* macros
149 below.
150
151 If NDEBUG is defined, does nothing. Otherwise, if STATUS is
152 RETURN_STATUS_OK, does nothing; otherwise, make a dbug printout and
153 (if ALLOW_UNREPORTED==0) assert that STATUS !=
154 RETURN_STATUS_UNREPORTED.
155
156 @param STATUS The status to return.
157 @param ACTION A text that describes what we are doing: either
158 "Returning" or "Propagating" (used in DBUG_PRINT macros)
159 @param STATUS_NAME The stringified version of the STATUS (used in
160 DBUG_PRINT macros).
161 @param ALLOW_UNREPORTED If false, the macro asserts that STATUS is
162 not RETURN_STATUS_UNREPORTED_ERROR.
163*/
164#ifdef NDEBUG
165#define __CHECK_RETURN_STATUS(STATUS, ACTION, STATUS_NAME, ALLOW_UNREPORTED)
166#else
167extern void check_return_status(enum_return_status status, const char *action,
168 const char *status_name, int allow_unreported);
169#define __CHECK_RETURN_STATUS(STATUS, ACTION, STATUS_NAME, ALLOW_UNREPORTED) \
170 check_return_status(STATUS, ACTION, STATUS_NAME, ALLOW_UNREPORTED);
171#endif
172/**
173 Low-level macro that checks if STATUS is RETURN_STATUS_OK; if it is
174 not, then RETURN_VALUE is returned.
175 @see __DO_RETURN_STATUS
176*/
177#define __PROPAGATE_ERROR(STATUS, RETURN_VALUE, ALLOW_UNREPORTED) \
178 do { \
179 enum_return_status __propagate_error_status = STATUS; \
180 if (__propagate_error_status != RETURN_STATUS_OK) { \
181 __CHECK_RETURN_STATUS(__propagate_error_status, "Propagating", #STATUS, \
182 ALLOW_UNREPORTED); \
183 return RETURN_VALUE; \
184 } \
185 } while (0)
186/// Low-level macro that returns STATUS. @see __DO_RETURN_STATUS
187#define __RETURN_STATUS(STATUS, ALLOW_UNREPORTED) \
188 do { \
189 enum_return_status __return_status_status = STATUS; \
190 __CHECK_RETURN_STATUS(__return_status_status, "Returning", #STATUS, \
191 ALLOW_UNREPORTED); \
192 return __return_status_status; \
193 } while (0)
194/**
195 If STATUS (of type enum_return_status) returns RETURN_STATUS_OK,
196 does nothing; otherwise, does a DBUG_PRINT and returns STATUS.
197*/
198#define PROPAGATE_ERROR(STATUS) \
199 __PROPAGATE_ERROR(STATUS, __propagate_error_status, true)
200/**
201 If STATUS (of type enum_return_status) returns RETURN_STATUS_OK,
202 does nothing; otherwise asserts that STATUS ==
203 RETURN_STATUS_REPORTED_ERROR, does a DBUG_PRINT, and returns STATUS.
204*/
205#define PROPAGATE_REPORTED_ERROR(STATUS) \
206 __PROPAGATE_ERROR(STATUS, __propagate_error_status, false)
207/**
208 If STATUS (of type enum_return_status) returns RETURN_STATUS_OK,
209 does nothing; otherwise asserts that STATUS ==
210 RETURN_STATUS_REPORTED_ERROR, does a DBUG_PRINT, and returns 1.
211*/
212#define PROPAGATE_REPORTED_ERROR_INT(STATUS) __PROPAGATE_ERROR(STATUS, 1, false)
213/**
214 If STATUS returns something else than RETURN_STATUS_OK, does a
215 DBUG_PRINT. Then, returns STATUS.
216*/
217#define RETURN_STATUS(STATUS) __RETURN_STATUS(STATUS, true)
218/**
219 Asserts that STATUS is not RETURN_STATUS_UNREPORTED_ERROR. Then, if
220 STATUS is RETURN_STATUS_REPORTED_ERROR, does a DBUG_PRINT. Then,
221 returns STATUS.
222*/
223#define RETURN_REPORTED_STATUS(STATUS) __RETURN_STATUS(STATUS, false)
224/// Returns RETURN_STATUS_OK.
225#define RETURN_OK return RETURN_STATUS_OK
226/// Does a DBUG_PRINT and returns RETURN_STATUS_REPORTED_ERROR.
227#define RETURN_REPORTED_ERROR RETURN_STATUS(RETURN_STATUS_REPORTED_ERROR)
228/// Does a DBUG_PRINT and returns RETURN_STATUS_UNREPORTED_ERROR.
229#define RETURN_UNREPORTED_ERROR RETURN_STATUS(RETURN_STATUS_UNREPORTED_ERROR)
230
231/**
232 enum to map the result of Uuid::parse to the above Macros
233*/
236 if (status == 0)
237 RETURN_OK;
238 else
240}
241
242/**
243 Possible values for ENFORCE_GTID_CONSISTENCY.
244*/
250/**
251 Strings holding the enumeration values for
252 gtid_consistency_mode_names. Use get_gtid_consistency_mode_string
253 instead of accessing this directly.
254*/
255extern const char *gtid_consistency_mode_names[];
256/**
257 Current value for ENFORCE_GTID_CONSISTENCY.
258 Don't use this directly; use get_gtid_consistency_mode.
259*/
260extern ulong _gtid_consistency_mode;
261/**
262 Return the current value of ENFORCE_GTID_CONSISTENCY.
263
264 Caller must hold global_tsid_lock.rdlock.
265*/
267/// Return the given GTID_CONSISTENCY_MODE as a string.
271}
272/**
273 Return the current value of ENFORCE_GTID_CONSISTENCY as a string.
274
275 Caller must hold global_tsid_lock.rdlock.
276*/
279}
280
281/// One-past-the-max value of GNO
282const rpl_gno GNO_END = INT64_MAX;
283/// If the GNO goes above the number, generate a warning.
285/// The length of MAX_GNO when printed in decimal.
286const int MAX_GNO_TEXT_LENGTH = 19;
287/// The maximal possible length of thread_id when printed in decimal.
289
290/**
291 Parse a GNO from a string.
292
293 @param s Pointer to the string. *s will advance to the end of the
294 parsed GNO, if a correct GNO is found.
295 @retval GNO if a correct GNO (i.e., 0 or positive number) was found.
296 @retval -1 otherwise.
297*/
298rpl_gno parse_gno(const char **s);
299/**
300 Formats a GNO as a string.
301
302 @param s The buffer.
303 @param gno The GNO.
304 @return Length of the generated string.
305*/
306int format_gno(char *s, rpl_gno gno);
307
309
310/**
311 This has the functionality of mysql_rwlock_t, with two differences:
312 1. It has additional operations to check if the read and/or write lock
313 is held at the moment.
314 2. It is wrapped in an object-oriented interface.
315
316 Note that the assertions do not check whether *this* thread has
317 taken the lock (that would be more complicated as it would require a
318 dynamic data structure). Luckily, it is still likely that the
319 assertions find bugs where a thread forgot to take a lock, because
320 most of the time most locks are only used by one thread at a time.
321
322 The assertions are no-ops when DBUG is off.
323*/
325 public:
326 /// Initialize this Checkable_rwlock.
328#if defined(HAVE_PSI_INTERFACE)
329 PSI_rwlock_key psi_key [[maybe_unused]] = 0
330#endif
331 ) {
332#ifndef NDEBUG
333 m_lock_state.store(0);
334 m_dbug_trace = true;
335#else
336 m_is_write_lock = false;
337#endif
338#if defined(HAVE_PSI_INTERFACE)
339 mysql_rwlock_init(psi_key, &m_rwlock);
340#else
342#endif
343 }
344 /// Destroy this Checkable_lock.
346
353 };
354
355 /**
356 RAII class to acquire a lock for the duration of a block.
357 */
358 class Guard {
361
362 public:
363 /**
364 Create a guard, and optionally acquire a lock on it.
365 */
369 switch (lock_type) {
370 case READ_LOCK:
371 rdlock();
372 break;
373 case WRITE_LOCK:
374 wrlock();
375 break;
376 case TRY_READ_LOCK:
377 tryrdlock();
378 break;
379 case TRY_WRITE_LOCK:
380 trywrlock();
381 break;
382 case NO_LOCK:
383 break;
384 }
385 }
386
387 /**
388 Create a guard, assuming the caller already holds a lock on it.
389 */
391 std::adopt_lock_t t [[maybe_unused]])
392 : m_lock(lock), m_lock_type(lock_type) {
394 switch (lock_type) {
395 case READ_LOCK:
396 lock.assert_some_rdlock();
397 break;
398 case WRITE_LOCK:
399 lock.assert_some_wrlock();
400 break;
401 case TRY_READ_LOCK:
402 case TRY_WRITE_LOCK:
403 case NO_LOCK:
404 break;
405 }
406 }
407
408 /// Objects of this class should not be copied or moved.
409 Guard(Guard const &copy) = delete;
410 Guard(Guard const &&copy) = delete;
411
412 /// Unlock on destruct.
416 }
417
418 /// Acquire the read lock.
419 void rdlock() {
421 assert(m_lock_type == NO_LOCK);
422 m_lock.rdlock();
424 }
425
426 /// Acquire the write lock.
427 void wrlock() {
429 assert(m_lock_type == NO_LOCK);
430 m_lock.wrlock();
432 }
433
434 /**
435 Try to acquire the write lock, and fail if it cannot be
436 immediately granted.
437 */
438 int trywrlock() {
440 assert(m_lock_type == NO_LOCK);
441 int ret = m_lock.trywrlock();
442 if (ret == 0) m_lock_type = WRITE_LOCK;
443 return ret;
444 }
445
446 /**
447 Try to acquire a read lock, and fail if it cannot be
448 immediately granted.
449 */
450 int tryrdlock() {
452 assert(m_lock_type == NO_LOCK);
453 int ret = m_lock.tryrdlock();
454 if (ret == 0) m_lock_type = READ_LOCK;
455 return ret;
456 }
457
458 /// Unlock the lock.
459 void unlock() {
461 assert(m_lock_type != NO_LOCK);
462 m_lock.unlock();
463 }
464
465 /// Unlock the lock, if it was acquired by this guard.
468 if (m_lock_type != NO_LOCK) unlock();
469 }
470
471 /// Return the underlying Checkable_rwlock object.
472 Checkable_rwlock &get_lock() const { return m_lock; }
473
474 /// Return true if this object is read locked.
475 bool is_rdlocked() const { return m_lock_type == READ_LOCK; }
476
477 /// Return true if this object is write locked.
478 bool is_wrlocked() const { return m_lock_type == WRITE_LOCK; }
479
480 /// Return true if this object is either read locked or write locked.
481 bool is_locked() const { return m_lock_type != NO_LOCK; }
482 };
483
484 /// Acquire the read lock.
485 inline void rdlock() {
488#ifndef NDEBUG
489 if (m_dbug_trace) DBUG_PRINT("info", ("%p.rdlock()", this));
490 ++m_lock_state;
491#endif
492 }
493 /// Acquire the write lock.
494 inline void wrlock() {
497#ifndef NDEBUG
498 if (m_dbug_trace) DBUG_PRINT("info", ("%p.wrlock()", this));
499 m_lock_state.store(-1);
500#else
501 m_is_write_lock = true;
502#endif
503 }
504 /// Release the lock (whether it is a write or read lock).
505 inline void unlock() {
507#ifndef NDEBUG
508 if (m_dbug_trace) DBUG_PRINT("info", ("%p.unlock()", this));
509 int val = m_lock_state.load();
510 if (val > 0)
511 --m_lock_state;
512 else if (val == -1)
513 m_lock_state.store(0);
514 else
515 assert(0);
516#else
517 m_is_write_lock = false;
518#endif
520 }
521 /**
522 Return true if the write lock is held. Must only be called by
523 threads that hold a lock.
524 */
525 inline bool is_wrlock() {
527#ifndef NDEBUG
528 return get_state() == -1;
529#else
530 return m_is_write_lock;
531#endif
532 }
533
534 /**
535 Return 0 if the write lock is held, otherwise an error will be returned.
536 */
537 inline int trywrlock() {
539
540 if (ret == 0) {
542#ifndef NDEBUG
543 if (m_dbug_trace) DBUG_PRINT("info", ("%p.wrlock()", this));
544 m_lock_state.store(-1);
545#else
546 m_is_write_lock = true;
547#endif
548 }
549
550 return ret;
551 }
552
553 /**
554 Return 0 if the read lock is held, otherwise an error will be returned.
555 */
556 inline int tryrdlock() {
558
559 if (ret == 0) {
561#ifndef NDEBUG
562 if (m_dbug_trace) DBUG_PRINT("info", ("%p.rdlock()", this));
563 ++m_lock_state;
564#endif
565 }
566
567 return ret;
568 }
569
570 /// Assert that some thread holds either the read or the write lock.
571 inline void assert_some_lock() const { assert(get_state() != 0); }
572 /// Assert that some thread holds the read lock.
573 inline void assert_some_rdlock() const { assert(get_state() > 0); }
574 /// Assert that some thread holds the write lock.
575 inline void assert_some_wrlock() const { assert(get_state() == -1); }
576 /// Assert that no thread holds the write lock.
577 inline void assert_no_wrlock() const { assert(get_state() >= 0); }
578 /// Assert that no thread holds the read lock.
579 inline void assert_no_rdlock() const { assert(get_state() <= 0); }
580 /// Assert that no thread holds read or write lock.
581 inline void assert_no_lock() const { assert(get_state() == 0); }
582
583#ifndef NDEBUG
584
585 /// If enabled, print any lock/unlock operations to the DBUG trace.
587
588 private:
589 /**
590 The state of the lock:
591 0 - not locked
592 -1 - write locked
593 >0 - read locked by that many threads
594 */
595 std::atomic<int32> m_lock_state;
596 /// Read lock_state atomically and return the value.
597 inline int32 get_state() const { return m_lock_state.load(); }
598
599#else
600
601 private:
602 bool m_is_write_lock;
603
604#endif
605 /// The rwlock.
607};
608
609/// Protects Gtid_state. See comment above gtid_state for details.
611
612/**
613 Class to access the value of @@global.gtid_mode in an efficient and
614 thread-safe manner.
615*/
617 private:
618 std::atomic<int> m_atomic_mode;
619
620 public:
622
623 /**
624 The sys_var framework needs a variable of type ulong to store the
625 value in. The sys_var framework takes the value from there, but
626 we copy it (in the methods of sys_var_gtid_mode) to the atomic
627 value Gtid_mode::mode, and use only that in all other places.
628 */
629 static ulong sysvar_mode;
630
631 /// Possible values for @@global.gtid_mode.
633 /**
634 New transactions are anonymous. Replicated transactions must be
635 anonymous; replicated GTID-transactions generate an error.
636 */
637 OFF = 0,
639 /**
640 New transactions are anonyomus. Replicated transactions can be
641 either anonymous or GTID-transactions.
642 */
644 /**
645 New transactions are GTID-transactions. Replicated transactions
646 can be either anonymous or GTID-transactions.
647 */
649 /**
650 New transactions are GTID-transactions. Replicated transactions
651 must be GTID-transactions; replicated anonymous transactions
652 generate an error.
653 */
654 ON = 3
655 };
656
657 /**
658 Strings holding the enumeration values for gtid_mode. Use
659 Gtid_mode::get_string instead of accessing this directly.
660 */
661 static const char *names[];
662
663 /**
664 Protects updates to @@global.gtid_mode.
665
666 SET @@global.gtid_mode will try to take the write lock. If the
667 lock is not granted immediately, SET will fail.
668
669 Thus, any other operation can block updates to
670 @@global.gtid_mode by acquiring the read lock.
671 */
673
674 /**
675 Set a new value for @@global.gtid_mode.
676
677 This should only be called from Sys_var_gtid_mode::global_update
678 and gtid_server_init.
679 */
680 void set(value_type value);
681
682 public:
683 /**
684 Return the current gtid_mode as an enumeration value.
685 */
686 value_type get() const;
687
688#ifndef NDEBUG
689 /**
690 Return the current gtid_mode as a string.
691
692 Used only for debugging. Non-debug code typically reads and acts
693 on the enum value before printing it. Then it is better to print
694 the enum value.
695 */
696 const char *get_string() const;
697#endif // ifndef NDEBUG
698
699 /**
700 Return the given string gtid_mode as an enumeration value.
701
702 @param s The string to decode.
703
704 @return A pair, where the first component indicates failure and
705 the second component is the GTID_MODE. Specifically, the first
706 component is false if the string is a valid GTID_MODE, and true if
707 it is not.
708 */
709 static std::pair<bool, value_type> from_string(std::string s);
710
711 /// Return the given gtid_mode as a string.
712 static const char *to_string(value_type value);
713};
714
715std::ostream &operator<<(std::ostream &oss, Gtid_mode::value_type const &mode);
716#ifndef NDEBUG
717/**
718 Typically, code will print Gtid_mode only after reading and acting
719 on the enum value. Then it is better to print the enum value than to
720 read the shared resource again. Hence we enable this only in debug
721 mode, since it makes more sense to just get the string when doing a
722 debug printout.
723*/
724std::ostream &operator<<(std::ostream &oss, Gtid_mode const &mode);
725#endif
726
727/**
728 The one and only instance of Gtid_mode.
729
730 All access to @@global.gtid_mode should use this object.
731*/
733
734/**
735 Represents a bidirectional map between TSID and SIDNO.
736
737 SIDNOs are always numbers greater or equal to 1.
738
739 This data structure OPTIONALLY knows of a read-write lock that
740 protects the number of SIDNOs. The lock is provided by the invoker
741 of the constructor and it is generally the caller's responsibility
742 to acquire the read lock. If the lock is not NULL, access methods
743 assert that the caller already holds the read (or write) lock. If
744 the lock is not NULL and a method of this class grows the number of
745 SIDNOs, then the method temporarily upgrades this lock to a write
746 lock and then degrades it to a read lock again; there will be a
747 short period when the lock is not held at all.
748*/
749class Tsid_map {
750 public:
751 /**
752 Create this Tsid_map.
753
754 @param tsid_lock Read-write lock that protects updates to the
755 number of SIDNOs.
756 */
758
759 /// Destroy this Tsid_map.
760 ~Tsid_map();
761 /**
762 Clears this Tsid_map (for RESET REPLICA)
763
764 @return RETURN_STATUS_OK or RETURN_STAUTS_REPORTED_ERROR
765 */
767
773 using Tsid_to_sidno_it = Tsid_to_sidno_map::const_iterator;
774 using Tsid_ref = std::reference_wrapper<const Tsid>;
775 using Sidno_to_tsid_cont = std::vector<Tsid_ref, Malloc_allocator<Tsid_ref>>;
776
777 /**
778 Add the given TSID to this map if it does not already exist.
779
780 The caller must hold the read lock or write lock on tsid_lock
781 before invoking this function. If the TSID does not exist in this
782 map, it will release the read lock, take a write lock, update the
783 map, release the write lock, and take the read lock again.
784
785 @param tsid The TSID.
786 @retval SIDNO The SIDNO for the TSID (a new SIDNO if the TSID did
787 not exist, an existing if it did exist).
788 @retval negative Error. This function calls my_error.
789 */
790 [[NODISCARD]] rpl_sidno add_tsid(const Tsid &tsid);
791 /**
792 Get the SIDNO for a given TSID
793
794 The caller must hold the read lock on tsid_lock before invoking
795 this function.
796
797 @param tsid The TSID.
798 @retval SIDNO if the given TSID exists in this map.
799 @retval 0 if the given TSID does not exist in this map.
800 */
801 rpl_sidno tsid_to_sidno(const Tsid &tsid) const {
802 if (tsid_lock != nullptr) tsid_lock->assert_some_lock();
803 const auto it = _tsid_to_sidno.find(tsid);
804 if (it == _tsid_to_sidno.end()) return 0;
805 return it->second;
806 }
807 /**
808 Get the TSID for a given SIDNO.
809
810 Raises an assertion if the SIDNO is not valid.
811
812 If need_lock is true, acquires tsid_lock->rdlock; otherwise asserts
813 that it is held already.
814
815 @param sidno The SIDNO.
816 @param need_lock If true, and tsid_lock!=NULL, this function will
817 acquire tsid_lock before looking up the sid, and then release
818 it. If false, and tsid_lock!=NULL, this function will assert the
819 tsid_lock is already held. If tsid_lock==NULL, nothing is done
820 w.r.t. locking.
821 @retval NULL The SIDNO does not exist in this map.
822 @retval ref Reference to the TSID. The data is shared with this
823 Tsid_map, so should not be modified. It is safe to read the data
824 even after this Tsid_map is modified, but not if this Tsid_map is
825 destroyed.
826 */
827 const Tsid &sidno_to_tsid(rpl_sidno sidno, bool need_lock = false) const {
828 if (tsid_lock != nullptr) {
829 if (need_lock)
830 tsid_lock->rdlock();
831 else
833 }
834 assert(sidno >= 1 && sidno <= get_max_sidno());
835 const auto &ret = (_sidno_to_tsid[sidno - 1]);
836 if (tsid_lock != nullptr && need_lock) tsid_lock->unlock();
837 return ret.get();
838 }
839
840 /**
841 Returns TSID to SID map
842
843 The caller must hold the read or write lock on tsid_lock before
844 invoking this function.
845
846 @return constant reference to sid_to_sidno container
847 */
848 const Tsid_to_sidno_map &get_sorted_sidno() const { return _sorted; }
849
850 const rpl_sidno &get_sidno(const Tsid_to_sidno_it &it) const {
851 if (tsid_lock != nullptr) tsid_lock->assert_some_lock();
852 return it->second;
853 }
854
855 const Tsid &get_tsid(const Tsid_to_sidno_it &it) const {
856 if (tsid_lock != nullptr) tsid_lock->assert_some_lock();
857 return it->first;
858 }
859
860 /**
861 Return the biggest sidno in this Tsid_map.
862
863 The caller must hold the read or write lock on tsid_lock before
864 invoking this function.
865 */
867 if (tsid_lock != nullptr) tsid_lock->assert_some_lock();
868 return static_cast<rpl_sidno>(_sidno_to_tsid.size());
869 }
870
871 /// Return the tsid_lock.
873
874 /**
875 Deep copy this Tsid_map to dest.
876
877 The caller must hold:
878 * the read lock on this tsid_lock
879 * the write lock on the dest tsid_lock
880 before invoking this function.
881
882 @param[out] dest The Tsid_map to which the tsids and sidnos will
883 be copied.
884 @return RETURN_STATUS_OK or RETURN_STATUS_REPORTED_ERROR.
885 */
887
888 private:
889 /**
890 Create a Node from the given SIDNO and a TSID and add it to
891 _sidno_to_tsid, _tsid_to_sidno, and _sorted.
892
893 The caller must hold the write lock on tsid_lock before invoking
894 this function.
895
896 @param sidno The SIDNO to add.
897 @param tsid The TSID to add.
898 @return RETURN_STATUS_OK or RETURN_STATUS_REPORTED_ERROR.
899 */
900 [[NODISCARD]] enum_return_status add_node(rpl_sidno sidno, const Tsid &tsid);
901
902 /// Read-write lock that protects updates to the number of SIDNOs.
904
905 /**
906 Array that maps SIDNO to TSID; the element at index N points to a
907 Node with SIDNO N-1.
908 */
911
912 /**
913 Hash that maps TSID to SIDNO.
914 */
916 /**
917 Data structure that maps numbers in the interval [0, get_max_sidno()-1] to
918 SIDNOs, in order of increasing TSID.
919
920 @see Tsid_map::get_sorted_sidno.
921 */
923};
924
926
927/**
928 Represents a growable array where each element contains a mutex and
929 a condition variable.
930
931 Each element can be locked, unlocked, broadcast, or waited for, and
932 it is possible to call "THD::enter_cond" for the condition. The
933 allowed indexes range from 0, inclusive, to get_max_index(),
934 inclusive. Initially there are zero elements (and get_max_index()
935 returns -1); more elements can be allocated by calling
936 ensure_index().
937
938 This data structure has a read-write lock that protects the number
939 of elements. The lock is provided by the invoker of the constructor
940 and it is generally the caller's responsibility to acquire the read
941 lock. Access methods assert that the caller already holds the read
942 (or write) lock. If a method of this class grows the number of
943 elements, then the method temporarily upgrades this lock to a write
944 lock and then degrades it to a read lock again; there will be a
945 short period when the lock is not held at all.
946*/
948 public:
949 /**
950 Create a new Mutex_cond_array.
951
952 @param global_lock Read-write lock that protects updates to the
953 number of elements.
954 */
956 /// Destroy this object.
958 /// Lock the n'th mutex.
959 inline void lock(int n) const {
962 }
963 /// Unlock the n'th mutex.
964 inline void unlock(int n) const {
967 }
968 /// Broadcast the n'th condition.
969 inline void broadcast(int n) const {
971 }
972 /**
973 Assert that this thread owns the n'th mutex.
974 This is a no-op if NDEBUG is on.
975 */
976 inline void assert_owner(int n [[maybe_unused]]) const {
977#ifndef NDEBUG
979#endif
980 }
981 /**
982 Assert that this thread does not own the n'th mutex.
983 This is a no-op if NDEBUG is on.
984 */
985 inline void assert_not_owner(int n [[maybe_unused]]) const {
986#ifndef NDEBUG
988#endif
989 }
990
991 /**
992 Wait for signal on the n'th condition variable.
993
994 The caller must hold the read lock or write lock on tsid_lock, as
995 well as the nth mutex lock, before invoking this function. The
996 tsid_lock will be released, whereas the mutex will be released
997 during the wait and (atomically) re-acquired when the wait ends
998 or the timeout is reached.
999
1000 @param[in] thd THD object for the calling thread.
1001 @param[in] sidno Condition variable to wait for.
1002 @param[in] abstime The absolute point in time when the wait times
1003 out and stops, or NULL to wait indefinitely.
1004
1005 @retval false Success.
1006 @retval true Failure: either timeout or thread was killed. If
1007 thread was killed, the error has been generated.
1008 */
1009 inline bool wait(const THD *thd, int sidno, struct timespec *abstime) const {
1010 DBUG_TRACE;
1011 int error = 0;
1012 Mutex_cond *mutex_cond = get_mutex_cond(sidno);
1014 mysql_mutex_assert_owner(&mutex_cond->mutex);
1015 if (is_thd_killed(thd)) return true;
1016 if (abstime != nullptr)
1017 error =
1018 mysql_cond_timedwait(&mutex_cond->cond, &mutex_cond->mutex, abstime);
1019 else
1020 mysql_cond_wait(&mutex_cond->cond, &mutex_cond->mutex);
1021 mysql_mutex_assert_owner(&mutex_cond->mutex);
1022 return is_timeout(error);
1023 }
1024#ifdef MYSQL_SERVER
1025 /// Execute THD::enter_cond for the n'th condition variable.
1026 void enter_cond(THD *thd, int n, PSI_stage_info *stage,
1027 PSI_stage_info *old_stage) const;
1028#endif // ifdef MYSQL_SERVER
1029 /// Return the greatest addressable index in this Mutex_cond_array.
1030 inline int get_max_index() const {
1032 return static_cast<int>(m_array.size() - 1);
1033 }
1034 /**
1035 Grows the array so that the given index fits.
1036
1037 If the array is grown, the global_lock is temporarily upgraded to
1038 a write lock and then degraded again; there will be a
1039 short period when the lock is not held at all.
1040
1041 @param n The index.
1042 @return RETURN_OK or RETURN_REPORTED_ERROR
1043 */
1044 enum_return_status ensure_index(int n);
1045
1046 private:
1047 /**
1048 Return true if the given THD is killed.
1049
1050 @param[in] thd - The thread object
1051 @retval true - thread is killed
1052 false - thread not killed
1053 */
1054 bool is_thd_killed(const THD *thd) const;
1055 /// A mutex/cond pair.
1056 struct Mutex_cond {
1059 };
1060 /// Return the Nth Mutex_cond object
1061 inline Mutex_cond *get_mutex_cond(int n) const {
1062 global_lock->assert_some_lock();
1063 assert(n <= get_max_index());
1064 Mutex_cond *ret = m_array[n];
1065 assert(ret);
1066 return ret;
1067 }
1068 /// Read-write lock that protects updates to the number of elements.
1071};
1072
1073/**
1074 Holds information about a GTID interval: the sidno, the first gno
1075 and the last gno of this interval.
1076*/
1078 /* SIDNO of this Gtid interval. */
1080 /* The first GNO of this Gtid interval. */
1082 /* The last GNO of this Gtid interval. */
1085 sidno = sid_no;
1086 gno_start = start;
1087 gno_end = end;
1088 }
1089};
1090
1091/**
1092 TODO: Move this structure to mysql/binlog/event/control_events.h
1093 when we start using C++11.
1094 Holds information about a GTID: the sidno and the gno.
1095
1096 This is a POD. It has to be a POD because it is part of
1097 Gtid_specification, which has to be a POD because it is used in
1098 THD::variables.
1099*/
1100struct Gtid {
1103 /// SIDNO of this Gtid.
1105 /// GNO of this Gtid.
1107
1108 /// Set both components to 0.
1109 void clear() {
1110 sidno = 0;
1111 gno = 0;
1112 }
1113 /// Set both components to the given, positive values.
1114 void set(rpl_sidno sidno_arg, rpl_gno gno_arg) {
1115 assert(sidno_arg > 0);
1116 assert(gno_arg > 0);
1117 assert(gno_arg < GNO_END);
1118 sidno = sidno_arg;
1119 gno = gno_arg;
1120 }
1121 /**
1122 Return true if sidno is zero (and assert that gno is zero too in
1123 this case).
1124 */
1125 bool is_empty() const {
1126 // check that gno is not set inconsistently
1127 if (sidno <= 0)
1128 assert(gno == 0);
1129 else
1130 assert(gno > 0);
1131 return sidno == 0;
1132 }
1133 /**
1134 The maximal length of the textual representation of a TSID, not
1135 including the terminating '\0'.
1136 */
1137 static const int MAX_TEXT_LENGTH =
1139 /**
1140 Returns true if parse() would succeed, but doesn't store the
1141 result anywhere
1142 */
1143 static bool is_valid(const char *text);
1144 /**
1145 Convert a Gtid to a string.
1146 @param tsid the TSID to use. This overrides the sidno of this Gtid.
1147 @param[out] buf Buffer to store the Gtid in (normally
1148 MAX_TEXT_LENGTH+1 bytes long).
1149 @return Length of the string, not counting '\0'.
1150 */
1151 int to_string(const Tsid &tsid, char *buf) const;
1152
1153 /**
1154 Convert this Gtid to a string.
1155 @param tsid_map tsid_map to use when converting sidno to a TSID.
1156 @param[out] buf Buffer to store the Gtid in (normally
1157 MAX_TEXT_LENGTH+1 bytes long).
1158 @param need_lock If true, the function will acquire tsid_map->tsid_lock;
1159 otherwise it will assert that the lock is held.
1160 @return Length of the string, not counting '\0'.
1161 */
1162 int to_string(const Tsid_map *tsid_map, char *buf,
1163 bool need_lock = false) const;
1164 /// Returns true if this Gtid has the same sid and gno as 'other'.
1165 bool equals(const Gtid &other) const {
1166 return sidno == other.sidno && gno == other.gno;
1167 }
1168 /**
1169 Parses the given string and stores in this Gtid.
1170
1171 @param tsid_map tsid_map to use when converting TSID to a sidno.
1172 @param text The text to parse
1173 @return status of operation
1174 */
1176 const char *text);
1177
1178 /// @brief Parses TAG from a textual representation of the GTID (text)
1179 /// @param[in] text String with full GTID specification
1180 /// @param[in] pos Current position within a text
1181 /// @return Parsed tag
1182 /// Updated position within text
1183 static std::pair<Tag, std::size_t> parse_tag_str(const char *text,
1184 std::size_t pos);
1185
1186 /// @brief Helper used to report BINLOG error
1187 /// @param[in] text String with full GTID specification
1188 static void report_parsing_error(const char *text);
1189
1190 /// @brief Parses GTID from text. In case GTID is valid, it will return
1191 /// "ok" status code and a valid mysql::gtid::Gtid object. Otherwise,
1192 /// it will return an empty Gtid and error
1193 /// @param[in] text Text containing textual representation of a GTID
1194 static std::pair<mysql::utils::Return_status, mysql::gtid::Gtid>
1195 parse_gtid_from_cstring(const char *text);
1196
1197 /// @brief Definition of GTID separator (colon) which separates UUID and GNO
1198 static constexpr auto gtid_separator = ':';
1199
1200#ifndef NDEBUG
1201 /// Debug only: print this Gtid to stdout.
1202 void print(const Tsid_map *tsid_map) const {
1203 char buf[MAX_TEXT_LENGTH + 1];
1204 to_string(tsid_map, buf);
1205 printf("%s\n", buf);
1206 }
1207#endif
1208 /// Print this Gtid to the trace file if debug is enabled; no-op otherwise.
1209 void dbug_print(const Tsid_map *tsid_map [[maybe_unused]],
1210 const char *text [[maybe_unused]] = "",
1211 bool need_lock [[maybe_unused]] = false) const {
1212#ifndef NDEBUG
1213 char buf[MAX_TEXT_LENGTH + 1];
1214 to_string(tsid_map, buf, need_lock);
1215 DBUG_PRINT("info", ("%s%s%s", text, *text ? ": " : "", buf));
1216#endif
1217 }
1218
1219 protected:
1220 /// @brief Converts internal gno into the string
1221 /// @param[out] buf Buffer to store the GNO in
1222 /// @return Length of the string
1223 int to_string_gno(char *buf) const;
1224
1225 /// @brief Helper function used to skip whitespaces in GTID specification
1226 /// @param[in] text String with full GTID specification
1227 /// @param[in] pos Current position within a text
1228 /// @return Updated position in text
1229 static std::size_t skip_whitespace(const char *text, std::size_t pos);
1230
1231 /// @brief Parses SID from a textual representation of the GTID.
1232 /// @param[in] text String with full GTID specification
1233 /// @param[in] pos Current position within a text
1234 /// @return operation status;
1235 /// Parsed sidno in case status is equal to true,
1236 /// 0 otherwise;
1237 /// Updated position within text up to which characters has
1238 /// been accepted
1239 static std::tuple<mysql::utils::Return_status, rpl_sid, std::size_t>
1240 parse_sid_str(const char *text, std::size_t pos);
1241
1242 /// @brief Parses GNO from a textual representation of the GTID (text)
1243 /// @param[in] text String with full GTID specification
1244 /// @param[in] pos Current position within a text
1245 /// @return operation status;
1246 /// Parsed gno in case status is equal to true or 0;
1247 /// Updated position within text
1248 static std::tuple<mysql::utils::Return_status, rpl_gno, std::size_t>
1249 parse_gno_str(const char *text, std::size_t pos);
1250
1251 /// @brief Parses GTID separator from a textual representation of the GTID
1252 /// (text)
1253 /// @param[in] text String with full GTID specification
1254 /// @param[in] pos Current position within a text
1255 /// @return parsing status, error in case separator could not have been parsed
1256 /// Updated position within text
1257 static std::pair<mysql::utils::Return_status, std::size_t>
1258 parse_gtid_separator(const char *text, std::size_t pos);
1259};
1260
1261/// Structure to store the GTID and timing information.
1263 /// GTID being monitored.
1265 /// OCT of the GTID being monitored.
1267 /// ICT of the GTID being monitored.
1269 /// When the GTID transaction started to be processed.
1271 /// When the GTID transaction finished to be processed.
1273 /// True if the GTID is being applied but will be skipped.
1275 /// True when this information contains useful data.
1277 /// Number of the last transient error of this transaction
1279 /// Message of the last transient error of this transaction
1281 /// Timestamp in microseconds of the last transient error of this transaction
1283 /// Number of times this transaction was retried
1285 /// True when the transaction is retrying
1287 /// The compression type
1289 /// The compressed bytes
1291 /// The uncompressed bytes
1293
1294 /// Constructor
1296 /// Copy constructor
1298
1300
1301 /// Clear all fields of the structure.
1302 void clear();
1303
1304 /**
1305 Copies this transaction monitoring information to the output parameters
1306 passed as input, which are the corresponding fields in a replication
1307 performance schema table.
1308
1309 @param[in] tsid_map The TSID map for the GTID.
1310 @param[out] gtid_arg GTID field in the PS table.
1311 @param[out] gtid_length_arg Length of the GTID as string.
1312 @param[out] original_commit_ts_arg The original commit timestamp.
1313 @param[out] immediate_commit_ts_arg The immediate commit timestamp.
1314 @param[out] start_time_arg The start time field.
1315 */
1316 void copy_to_ps_table(Tsid_map *tsid_map, char *gtid_arg,
1317 uint *gtid_length_arg,
1318 ulonglong *original_commit_ts_arg,
1319 ulonglong *immediate_commit_ts_arg,
1320 ulonglong *start_time_arg) const;
1321
1322 /**
1323 Copies this transaction monitoring information to the output parameters
1324 passed as input, which are the corresponding fields in a replication
1325 performance schema table.
1326
1327 @param[in] tsid_map The TSID map for the GTID.
1328 @param[out] gtid_arg GTID field in the PS table.
1329 @param[out] gtid_length_arg Length of the GTID as string.
1330 @param[out] original_commit_ts_arg The original commit timestamp.
1331 @param[out] immediate_commit_ts_arg The immediate commit timestamp.
1332 @param[out] start_time_arg The start time field.
1333 @param[out] end_time_arg The end time field. This can be null
1334 when the PS table fields are for the
1335 "still processing" information.
1336 */
1337 void copy_to_ps_table(Tsid_map *tsid_map, char *gtid_arg,
1338 uint *gtid_length_arg,
1339 ulonglong *original_commit_ts_arg,
1340 ulonglong *immediate_commit_ts_arg,
1341 ulonglong *start_time_arg,
1342 ulonglong *end_time_arg) const;
1343
1344 /**
1345 Copies this transaction monitoring information to the output parameters
1346 passed as input, which are the corresponding fields in a replication
1347 performance schema table.
1348
1349 @param[in] tsid_map The TSID map for the GTID.
1350 @param[out] gtid_arg GTID field in the PS table.
1351 @param[out] gtid_length_arg Length of the GTID as string.
1352 @param[out] original_commit_ts_arg The original commit timestamp.
1353 @param[out] immediate_commit_ts_arg The immediate commit timestamp.
1354 @param[out] start_time_arg The start time field.
1355 @param[out] last_transient_errno_arg The last transient error
1356 number.
1357 @param[out] last_transient_errmsg_arg The last transient error
1358 message.
1359 @param[out] last_transient_errmsg_length_arg Length of the last transient
1360 error message.
1361 @param[out] last_transient_timestamp_arg The last transient error
1362 timestamp.
1363 @param[out] retries_count_arg The total number of retries for
1364 this transaction.
1365 */
1366 void copy_to_ps_table(
1367 Tsid_map *tsid_map, char *gtid_arg, uint *gtid_length_arg,
1368 ulonglong *original_commit_ts_arg, ulonglong *immediate_commit_ts_arg,
1369 ulonglong *start_time_arg, uint *last_transient_errno_arg,
1370 char *last_transient_errmsg_arg, uint *last_transient_errmsg_length_arg,
1371 ulonglong *last_transient_timestamp_arg, ulong *retries_count_arg) const;
1372
1373 /**
1374 Copies this transaction monitoring information to the output parameters
1375 passed as input, which are the corresponding fields in a replication
1376 performance schema table.
1377
1378 @param[in] tsid_map The TSID map for the GTID.
1379 @param[out] gtid_arg GTID field in the PS table.
1380 @param[out] gtid_length_arg Length of the GTID as string.
1381 @param[out] original_commit_ts_arg The original commit timestamp.
1382 @param[out] immediate_commit_ts_arg The immediate commit timestamp.
1383 @param[out] start_time_arg The start time field.
1384 @param[out] end_time_arg The end time field. This can be
1385 null when the PS table fields
1386 are for the "still processing"
1387 information.
1388 @param[out] last_transient_errno_arg The last transient error
1389 number.
1390 @param[out] last_transient_errmsg_arg The last transient error
1391 message.
1392 @param[out] last_transient_errmsg_length_arg Length of the last transient
1393 error message.
1394 @param[out] last_transient_timestamp_arg The last transient error
1395 timestamp.
1396 @param[out] retries_count_arg The total number of retries for
1397 this transaction.
1398 */
1399 void copy_to_ps_table(
1400 Tsid_map *tsid_map, char *gtid_arg, uint *gtid_length_arg,
1401 ulonglong *original_commit_ts_arg, ulonglong *immediate_commit_ts_arg,
1402 ulonglong *start_time_arg, ulonglong *end_time_arg,
1403 uint *last_transient_errno_arg, char *last_transient_errmsg_arg,
1404 uint *last_transient_errmsg_length_arg,
1405 ulonglong *last_transient_timestamp_arg, ulong *retries_count_arg) const;
1406};
1407
1408/**
1409 Stores information to monitor a transaction during the different replication
1410 stages.
1411*/
1413 public:
1414 /**
1415 Create this GTID monitoring info object.
1416
1417 @param atomic_mutex_arg When specified, this object will rely on the mutex
1418 to arbitrate the read/update access to object data.
1419 This will be used by the receiver thread, relying
1420 on mi->data_lock. When no mutex is specified, the
1421 object will rely on its own atomic mechanism.
1422 */
1423 Gtid_monitoring_info(mysql_mutex_t *atomic_mutex_arg = nullptr);
1424
1425 /// Destroy this GTID monitoring info object.
1427
1428 protected:
1429 /// Holds information about transaction being processed.
1431 /// Holds information about the last processed transaction.
1433
1434 private:
1435 /**
1436 Mutex arbitrating the atomic access to the object.
1437
1438 Some Gtid_monitoring_info will rely on replication thread locks
1439 (i.e.: the Master_info's one rely on mi->data_lock, that is already
1440 acquired every time the Gtid_monitoring_info needs to be updated).
1441
1442 Other Gtid_monitoring_info will rely on an atomic lock implemented
1443 in this class to avoid overlapped reads and writes over the information.
1444 (i.e.: the Relay_log_info's one sometimes is updated without rli locks).
1445
1446 When atomic_mutex is NULL, the object will rely on its own atomic
1447 mechanism.
1448 */
1450
1451 /// The atomic locked flag.
1452 std::atomic<bool> atomic_locked{false};
1453#ifndef NDEBUG
1454 /// Flag to assert the atomic lock behavior.
1455 bool is_locked = false;
1456#endif
1457
1458 public:
1459 /**
1460 Lock this object when no thread mutex is used to arbitrate the access.
1461 */
1462 void atomic_lock();
1463 /**
1464 Unlock this object when no thread mutex is used to arbitrate the access.
1465 */
1466 void atomic_unlock();
1467 /**
1468 Clear all monitoring information.
1469 */
1470 void clear();
1471 /**
1472 Clear only the processing_trx monitoring info.
1473 */
1474 void clear_processing_trx();
1475 /**
1476 Clear only the last_processed_trx monitoring info.
1477 */
1479
1480 /**
1481 Sets the initial monitoring information.
1482 @param gtid_arg The Gtid to be stored.
1483 @param original_ts_arg The original commit timestamp of the GTID.
1484 @param immediate_ts_arg The immediate commit timestamp of the GTID.
1485 @param skipped_arg True if the GTID was already applied.
1486 This only make sense for applier threads.
1487 That's why it is false by default.
1488 */
1489 void start(Gtid gtid_arg, ulonglong original_ts_arg,
1490 ulonglong immediate_ts_arg, bool skipped_arg = false);
1491
1492 void update(mysql::binlog::event::compression::type t, size_t payload_size,
1493 size_t uncompressed_size);
1494
1495 /**
1496 Sets the final information, copy processing info to last_processed
1497 and clears processing info.
1498 */
1499 void finish();
1500 /**
1501 Copies both processing_trx and last_processed_trx info to other
1502 Trx_monitoring_info structures.
1503
1504 @param[out] processing_dest The destination of processing_trx.
1505 @param[out] last_processed_dest The destination of last_processed_trx.
1506 */
1507 void copy_info_to(Trx_monitoring_info *processing_dest,
1508 Trx_monitoring_info *last_processed_dest);
1509 /**
1510 Copies all monitoring info to other Gtid_monitoring_info object.
1511
1512 @param[out] dest The destination Gtid_monitoring_info.
1513 */
1515 /// Returns true if the processing_trx is set, false otherwise.
1516 bool is_processing_trx_set();
1517 /// Returns the GTID of the processing_trx.
1519 /**
1520 Stores the information about the last transient error in the current
1521 transaction, namely: the error number, message and total number of retries.
1522 It also sets the timestamp for this error.
1523
1524 @param transient_errno_arg The number of the transient error in this
1525 transaction.
1526 @param transient_err_message_arg The message of this transient error.
1527 @param trans_retries_arg The number of times this transaction has
1528 been retried.
1529 */
1530 void store_transient_error(uint transient_errno_arg,
1531 const char *transient_err_message_arg,
1532 ulong trans_retries_arg);
1533};
1534
1535/**
1536 Represents a set of GTIDs.
1537
1538 This is structured as an array, indexed by SIDNO, where each element
1539 contains a linked list of intervals.
1540
1541 This data structure OPTIONALLY knows of a Tsid_map that gives a
1542 correspondence between SIDNO and TSID. If the Tsid_map is NULL, then
1543 operations that require a Tsid_map - printing and parsing - raise an
1544 assertion.
1545
1546 This data structure OPTIONALLY knows of a read-write lock that
1547 protects the number of SIDNOs. The lock is provided by the invoker
1548 of the constructor and it is generally the caller's responsibility
1549 to acquire the read lock. If the lock is not NULL, access methods
1550 assert that the caller already holds the read (or write) lock. If
1551 the lock is not NULL and a method of this class grows the number of
1552 SIDNOs, then the method temporarily upgrades this lock to a write
1553 lock and then degrades it to a read lock again; there will be a
1554 short period when the lock is not held at all.
1555*/
1557 public:
1559 /**
1560 Constructs a new, empty Gtid_set.
1561
1562 @param tsid_map The Tsid_map to use, or NULL if this Gtid_set
1563 should not have a Tsid_map.
1564 @param tsid_lock Read-write lock that protects updates to the
1565 number of TSIDs. This may be NULL if such changes do not need to be
1566 protected.
1567 */
1569 /**
1570 Constructs a new Gtid_set that contains the gtids in the given
1571 string, in the same format as add_gtid_text(char *).
1572
1573 @param tsid_map The Tsid_map to use for TSIDs.
1574 @param text The text to parse.
1575 @param status Will be set to RETURN_STATUS_OK on success or
1576 RETURN_STATUS_REPORTED_ERROR on error.
1577 @param tsid_lock Read/write lock to protect changes in the number
1578 of SIDs with. This may be NULL if such changes do not need to be
1579 protected.
1580 If tsid_lock != NULL, then the read lock on tsid_lock must be held
1581 before calling this function. If the array is grown, tsid_lock is
1582 temporarily upgraded to a write lock and then degraded again;
1583 there will be a short period when the lock is not held at all.
1584 */
1585 Gtid_set(Tsid_map *tsid_map, const char *text, enum_return_status *status,
1586 Checkable_rwlock *tsid_lock = nullptr);
1587
1588 private:
1589 /// Worker for the constructor.
1590 void init();
1591
1592 public:
1593 /// Destroy this Gtid_set.
1594 ~Gtid_set();
1595 /**
1596 Removes all gtids from this Gtid_set.
1597
1598 This does not deallocate anything: if gtids are added later,
1599 existing allocated memory will be re-used.
1600 */
1601 void clear();
1602 /**
1603 Removes all gtids from this Gtid_set and clear all the sidnos
1604 used by the Gtid_set and it's TSID map.
1605
1606 This does not deallocate anything: if gtids are added later,
1607 existing allocated memory will be re-used.
1608 */
1610 /**
1611 Adds the given GTID to this Gtid_set.
1612
1613 The SIDNO must exist in the Gtid_set before this function is called.
1614
1615 @param sidno SIDNO of the GTID to add.
1616 @param gno GNO of the GTID to add.
1617 */
1618 void _add_gtid(rpl_sidno sidno, rpl_gno gno) {
1619 DBUG_TRACE;
1620 assert(sidno > 0);
1621 assert(gno > 0);
1622 assert(gno < GNO_END);
1623 Interval_iterator ivit(this, sidno);
1625 add_gno_interval(&ivit, gno, gno + 1, &lock);
1626 return;
1627 }
1628 /**
1629 Removes the given GTID from this Gtid_set.
1630
1631 @param sidno SIDNO of the GTID to remove.
1632 @param gno GNO of the GTID to remove.
1633 */
1634 void _remove_gtid(rpl_sidno sidno, rpl_gno gno) {
1635 DBUG_TRACE;
1636 if (sidno <= get_max_sidno()) {
1637 Interval_iterator ivit(this, sidno);
1639 remove_gno_interval(&ivit, gno, gno + 1, &lock);
1640 }
1641 return;
1642 }
1643 /**
1644 Adds the given GTID to this Gtid_set.
1645
1646 The SIDNO must exist in the Gtid_set before this function is called.
1647
1648 @param gtid Gtid to add.
1649 */
1650 void _add_gtid(const Gtid &gtid) { _add_gtid(gtid.sidno, gtid.gno); }
1651 /**
1652 Removes the given GTID from this Gtid_set.
1653
1654 @param gtid Gtid to remove.
1655 */
1656 void _remove_gtid(const Gtid &gtid) { _remove_gtid(gtid.sidno, gtid.gno); }
1657 /**
1658 Adds all gtids from the given Gtid_set to this Gtid_set.
1659
1660 If tsid_lock != NULL, then the read lock must be held before
1661 calling this function. If a new sidno is added so that the array
1662 of lists of intervals is grown, tsid_lock is temporarily upgraded
1663 to a write lock and then degraded again; there will be a short
1664 period when the lock is not held at all.
1665
1666 @param other The Gtid_set to add.
1667 @return RETURN_STATUS_OK or RETURN_STATUS_REPORTED_ERROR.
1668 */
1670 /**
1671 Removes all gtids in the given Gtid_set from this Gtid_set.
1672
1673 @param other The Gtid_set to remove.
1674 */
1675 void remove_gtid_set(const Gtid_set *other);
1676 /**
1677 Removes all intervals of 'other' for a given SIDNO, from 'this'.
1678
1679 Example:
1680 this = A:1-100, B:1-100
1681 other = A:1-100, B:1-50, C:1-100
1682 this.remove_intervals_for_sidno(other, B) = A:1-100, B:51-100
1683
1684 It is not required that the intervals exist in this Gtid_set.
1685
1686 @param other The set to remove.
1687 @param sidno The sidno to remove.
1688 */
1689 void remove_intervals_for_sidno(Gtid_set *other, rpl_sidno sidno);
1690 /**
1691 Adds the set of GTIDs represented by the given string to this Gtid_set.
1692
1693 The string must have the format of a comma-separated list of zero
1694 or more of the following items:
1695
1696 XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX(:NUMBER+(-NUMBER)?)*
1697 | ANONYMOUS
1698
1699 Each X is a hexadecimal digit (upper- or lowercase).
1700 NUMBER is a decimal, 0xhex, or 0oct number.
1701
1702 The start of an interval must be greater than 0. The end of an
1703 interval may be 0, but any interval that has an endpoint that
1704 is smaller than the start is discarded.
1705
1706 The string can start with an optional '+' appender qualifier
1707 which triggers @c executed_gtids and @c lost_gtids set examination
1708 on the matter of disjointness with the one being added.
1709
1710 If tsid_lock != NULL, then the read lock on tsid_lock must be held
1711 before calling this function. If a new sidno is added so that the
1712 array of lists of intervals is grown, tsid_lock is temporarily
1713 upgraded to a write lock and then degraded again; there will be a
1714 short period when the lock is not held at all.
1715
1716 @param text The string to parse.
1717 @param [in,out] anonymous If this is NULL, ANONYMOUS is not
1718 allowed. If this is not NULL, it will be set to true if the
1719 anonymous GTID was found; false otherwise.
1720 @param[in,out] starts_with_plus If this is not NULL, the string may
1721 optionally begin with a '+' character, and *starts_with_plus will
1722 be set to true if the plus character is present. If this is NULL,
1723 no plus is allowed at the begin of the string.
1724
1725 @return RETURN_STATUS_OK or RETURN_STATUS_REPORTED_ERROR.
1726 */
1727 enum_return_status add_gtid_text(const char *text, bool *anonymous = nullptr,
1728 bool *starts_with_plus = nullptr);
1729
1730 /// @brief Adds specified GTID (TSID+GNO) to this Gtid_set.
1731 /// @param gtid mysql::gtid::Gtid object
1732 /// @return RETURN_STATUS_OK or RETURN_STATUS_REPORTED_ERROR.
1734
1735 /**
1736 Decodes a Gtid_set from the given string.
1737
1738 @param encoded The string to parse.
1739 @param length The number of bytes.
1740 @param actual_length If this is not NULL, it is set to the number
1741 of bytes used by the encoding (which may be less than 'length').
1742 If this is NULL, an error is generated if the encoding is shorter
1743 than the given 'length'.
1744 @return RETURN_STATUS_OK or RETURN_STATUS_REPORTED_ERROR.
1745 */
1746 enum_return_status add_gtid_encoding(const uchar *encoded, size_t length,
1747 size_t *actual_length = nullptr);
1748 /// Return true iff the given GTID exists in this set.
1749 bool contains_gtid(rpl_sidno sidno, rpl_gno gno) const;
1750 /// Return true iff the given GTID exists in this set.
1751 bool contains_gtid(const Gtid &gtid) const {
1752 return contains_gtid(gtid.sidno, gtid.gno);
1753 }
1754 // Get last gno or 0 if this set is empty.
1755 rpl_gno get_last_gno(rpl_sidno sidno) const;
1756 /// Returns the maximal sidno that this Gtid_set currently has space for.
1759 return static_cast<rpl_sidno>(m_intervals.size());
1760 }
1761 /**
1762 Allocates space for all sidnos up to the given sidno in the array of
1763 intervals. The sidno must exist in the Tsid_map associated with this
1764 Gtid_set.
1765
1766 If tsid_lock != NULL, then the read lock on tsid_lock must be held
1767 before calling this function. If the array is grown, tsid_lock is
1768 temporarily upgraded to a write lock and then degraded again;
1769 there will be a short period when the lock is not held at all.
1770
1771 @param sidno The SIDNO.
1772 @return RETURN_STATUS_OK or RETURN_STATUS_REPORTED_ERROR.
1773 */
1775 /// Returns true if this Gtid_set is a subset of the other Gtid_set.
1776 bool is_subset(const Gtid_set *super) const;
1777 /// Returns true if this Gtid_set is a non equal subset of the other Gtid_set.
1778 bool is_subset_not_equals(const Gtid_set *super) const {
1779 return (is_subset(super) && !equals(super));
1780 }
1781
1782 /**
1783 Returns true if this Gtid_set is a subset of the given gtid_set
1784 on the given superset_sidno and subset_sidno.
1785
1786 @param super Gtid_set with which this->gtid_set needs to be
1787 compared
1788 @param superset_sidno The sidno that will be compared, relative to
1789 super->tsid_map.
1790 @param subset_sidno The sidno that will be compared, relative to
1791 this->tsid_map.
1792 @return true If 'this' Gtid_set is subset of given
1793 'super' Gtid_set.
1794 false If 'this' Gtid_set is *not* subset of given
1795 'super' Gtid_set.
1796 */
1797 bool is_subset_for_sidno(const Gtid_set *super, rpl_sidno superset_sidno,
1798 rpl_sidno subset_sidno) const;
1799
1800 /// @brief Returns true if this Gtid_set is a subset of the given gtid_set
1801 /// with respect to the given sid
1802 /// @details This function will traverse all TSIDs with SID equal
1803 /// to "sid" parameter (all registered tags for a given SID)
1804 /// @param super Gtid_set with which this->gtid_set needs to be compared
1805 /// @param sid Sid for which we will do the testing
1806 /// @return True in case super is a superset for this gtid set w.r.t. the
1807 /// given sid; false otherwise
1808 bool is_subset_for_sid(const Gtid_set *super, const rpl_sid &sid) const;
1809
1810 /// Returns true if there is a least one element of this Gtid_set in
1811 /// the other Gtid_set.
1812 bool is_intersection_nonempty(const Gtid_set *other) const;
1813 /**
1814 Add the intersection of this Gtid_set and the other Gtid_set to result.
1815
1816 @param other The Gtid_set to intersect with this Gtid_set
1817 @param result Gtid_set where the result will be stored.
1818 @return RETURN_STATUS_OK or RETURN_STATUS_REPORTED_ERROR.
1819 */
1821 /// Returns true if this Gtid_set is empty.
1822 bool is_empty() const {
1823 Gtid_iterator git(this);
1824 return git.get().sidno == 0;
1825 }
1826
1827 /**
1828 Return true if the size of the set is greater than or equal to the given
1829 number. The size is measure in number of GTIDs, i.e., total length of all
1830 intervals.
1831
1832 @param num Number to compare with
1833 @retval true if the set contains >= num GTIDs.
1834 @retval false if the set contains < num GTIDs.
1835 */
1837
1838 /**
1839 What is the count of all the GTIDs in all intervals for a sidno
1840
1841 @param sidno The sidno that contains the intervals
1842
1843 @return the number of all GTIDs in all intervals
1844 */
1846 Const_interval_iterator ivit(this, sidno);
1847 ulonglong ret = 0;
1848 while (ivit.get() != nullptr) {
1849 ret += ivit.get()->end - ivit.get()->start;
1850 ivit.next();
1851 }
1852 return ret;
1853 }
1854
1855 /**
1856 Returns true if this Gtid_set contains at least one GTID with
1857 the given SIDNO.
1858
1859 @param sidno The SIDNO to test.
1860 @retval true The SIDNO is less than or equal to the max SIDNO, and
1861 there is at least one GTID with this SIDNO.
1862 @retval false The SIDNO is greater than the max SIDNO, or there is
1863 no GTID with this SIDNO.
1864 */
1865 bool contains_sidno(rpl_sidno sidno) const {
1866 assert(sidno >= 1);
1867 if (sidno > get_max_sidno()) return false;
1868 Const_interval_iterator ivit(this, sidno);
1869 return ivit.get() != nullptr;
1870 }
1871 /**
1872 Returns true if the given string is a valid specification of a
1873 Gtid_set, false otherwise.
1874 */
1875 static bool is_valid(const char *text);
1876
1877 /**
1878 Class Gtid_set::String_format defines the separators used by
1879 Gtid_set::to_string.
1880 */
1882 /// The generated string begins with this.
1883 const char *begin;
1884 /// The generated string begins with this.
1885 const char *end;
1886 /// In 'SID:TAG', this is the ':'
1888 /// In 'TSID:GNO', this is the ':'
1890 /// In 'SID:GNO-GNO', this is the '-'
1892 /// In 'SID:GNO:GNO', this is the second ':'
1894 /// In 'SID:GNO,SID:GNO', this is the ','
1896 /// If the set is empty and this is not NULL, then this string is generated.
1897 const char *empty_set_string;
1898 /// The following fields are the lengths of each field above.
1899 const int begin_length;
1900 const int end_length;
1907 };
1908
1909 /// @brief Checks if this Gtid set contains any tagged GTIDs
1910 /// @retval true This gtid set contains tagged GTIDs
1911 /// @retval false This gtid set contains only untagged GTIDs
1912 bool contains_tags() const;
1913 /**
1914 Returns the length of the output from to_string.
1915
1916 @warning This does not include the trailing '\0', so your buffer
1917 needs space for get_string_length() + 1 characters.
1918
1919 @param string_format String_format object that specifies
1920 separators in the resulting text.
1921 @return The length.
1922 */
1923 size_t get_string_length(const String_format *string_format = nullptr) const;
1924 /**
1925 Formats this Gtid_set as a string and saves in a given buffer.
1926
1927 @param[out] buf Pointer to the buffer where the string should be
1928 stored. This should have size at least get_string_length()+1.
1929 @param need_lock If this Gtid_set has a tsid_lock, then the write
1930 lock must be held while generating the string. If this parameter
1931 is true, then this function acquires and releases the lock;
1932 otherwise it asserts that the caller holds the lock.
1933 @param string_format String_format object that specifies
1934 separators in the resulting text.
1935 @return Length of the generated string.
1936 */
1937 size_t to_string(char *buf, bool need_lock = false,
1938 const String_format *string_format = nullptr) const;
1939
1940 /**
1941 Formats a Gtid_set as a string and saves in a newly allocated buffer.
1942 @param[out] buf Pointer to pointer to string. The function will
1943 set it to point to the newly allocated buffer, or NULL on out of memory.
1944 @param need_lock If this Gtid_set has a tsid_lock, then the write
1945 lock must be held while generating the string. If this parameter
1946 is true, then this function acquires and releases the lock;
1947 otherwise it asserts that the caller holds the lock.
1948 @param string_format Specifies how to format the string.
1949 @retval Length of the generated string, or -1 on out of memory.
1950 */
1951 long to_string(char **buf, bool need_lock = false,
1952 const String_format *string_format = nullptr) const;
1953#ifndef NDEBUG
1954 /// Debug only: Print this Gtid_set to stdout.
1955
1956 /// For use with C `printf`
1957 void print(bool need_lock = false,
1958 const Gtid_set::String_format *sf = nullptr) const {
1959 char *str;
1960 to_string(&str, need_lock, sf);
1961 printf("%s\n", str ? str : "out of memory in Gtid_set::print");
1962 my_free(str);
1963 }
1964
1965 /// For use with C++ `std::ostream`
1966 inline friend std::ostream &operator<<(std::ostream &os, const Gtid_set &in) {
1967 char *str;
1968 in.to_string(&str, true, nullptr);
1969 os << std::string(str) << std::flush;
1970 my_free(str);
1971 return os;
1972 }
1973#endif
1974 /**
1975 Print this Gtid_set to the trace file if debug is enabled; no-op
1976 otherwise.
1977 */
1978 void dbug_print(const char *text [[maybe_unused]] = "",
1979 bool need_lock [[maybe_unused]] = false,
1980 const Gtid_set::String_format *sf
1981 [[maybe_unused]] = nullptr) const {
1982#ifndef NDEBUG
1983 char *str;
1984 to_string(&str, need_lock, sf);
1985 DBUG_PRINT("info", ("%s%s'%s'", text, *text ? ": " : "",
1986 str ? str : "out of memory in Gtid_set::dbug_print"));
1987 my_free(str);
1988#endif
1989 }
1990 /**
1991 Gets all gtid intervals from this Gtid_set.
1992
1993 @param[out] gtid_intervals Store all gtid intervals from this Gtid_set.
1994 */
1995 void get_gtid_intervals(std::list<Gtid_interval> *gtid_intervals) const;
1996 /**
1997 The default String_format: the format understood by
1998 add_gtid_text(const char *).
1999 */
2001 /**
2002 String_format useful to generate an SQL string: the string is
2003 wrapped in single quotes and there is a newline between SIDs.
2004 */
2006 /**
2007 String_format for printing the Gtid_set commented: the string is
2008 not quote-wrapped, and every TSID is on a new line with a leading '# '.
2009 */
2011
2012 /// Return the Tsid_map associated with this Gtid_set.
2013 Tsid_map *get_tsid_map() const { return tsid_map; }
2014
2015 /**
2016 Represents one element in the linked list of intervals associated
2017 with a SIDNO.
2018 */
2019 struct Interval {
2020 public:
2021 /// The first GNO of this interval.
2023 /// The first GNO after this interval.
2025 /// Return true iff this interval is equal to the given interval.
2026 bool equals(const Interval &other) const {
2027 return start == other.start && end == other.end;
2028 }
2029 /// Pointer to next interval in list.
2031 };
2032
2033 /**
2034 Provides an array of Intervals that this Gtid_set can use when
2035 gtids are subsequently added. This can be used as an
2036 optimization, to reduce allocation for sets that have a known
2037 number of intervals.
2038
2039 @param n_intervals The number of intervals to add.
2040 @param intervals_param Array of n_intervals intervals.
2041 */
2042 void add_interval_memory(int n_intervals, Interval *intervals_param) {
2044 add_interval_memory_lock_taken(n_intervals, intervals_param);
2046 }
2047
2048 /**
2049 Iterator over intervals for a given SIDNO.
2050
2051 This is an abstract template class, used as a common base class
2052 for Const_interval_iterator and Interval_iterator.
2053
2054 The iterator always points to an interval pointer. The interval
2055 pointer is either the initial pointer into the list, or the next
2056 pointer of one of the intervals in the list.
2057 */
2058 template <typename Gtid_set_p, typename Interval_p>
2060 public:
2061 /**
2062 Construct a new iterator over the GNO intervals for a given Gtid_set.
2063
2064 @param gtid_set The Gtid_set.
2065 @param sidno The SIDNO.
2066 */
2067 Interval_iterator_base(Gtid_set_p gtid_set, rpl_sidno sidno) {
2068 assert(sidno >= 1 && sidno <= gtid_set->get_max_sidno());
2069 init(gtid_set, sidno);
2070 }
2071 /// Construct a new iterator over the free intervals of a Gtid_set.
2072 Interval_iterator_base(Gtid_set_p gtid_set) {
2073 p = const_cast<Interval_p *>(&gtid_set->free_intervals);
2074 }
2075 /// Reset this iterator.
2076 inline void init(Gtid_set_p gtid_set, rpl_sidno sidno) {
2077 p = const_cast<Interval_p *>(&gtid_set->m_intervals[sidno - 1]);
2078 }
2079 /// Advance current_elem one step.
2080 inline void next() {
2081 assert(*p != nullptr);
2082 p = const_cast<Interval_p *>(&(*p)->next);
2083 }
2084 /// Return current_elem.
2085 inline Interval_p get() const { return *p; }
2086
2087 protected:
2088 /**
2089 Holds the address of the 'next' pointer of the previous element,
2090 or the address of the initial pointer into the list, if the
2091 current element is the first element.
2092 */
2093 Interval_p *p;
2094 };
2095
2096 /**
2097 Iterator over intervals of a const Gtid_set.
2098 */
2100 : public Interval_iterator_base<const Gtid_set *, const Interval *> {
2101 public:
2102 /// Create this Const_interval_iterator.
2104 : Interval_iterator_base<const Gtid_set *, const Interval *>(gtid_set,
2105 sidno) {}
2106 /// Create this Const_interval_iterator.
2108 : Interval_iterator_base<const Gtid_set *, const Interval *>(gtid_set) {
2109 }
2110 };
2111
2112 /**
2113 Iterator over intervals of a non-const Gtid_set, with additional
2114 methods to modify the Gtid_set.
2115 */
2117 : public Interval_iterator_base<Gtid_set *, Interval *> {
2118 public:
2119 /// Create this Interval_iterator.
2121 : Interval_iterator_base<Gtid_set *, Interval *>(gtid_set, sidno) {}
2122 /// Destroy this Interval_iterator.
2124 : Interval_iterator_base<Gtid_set *, Interval *>(gtid_set) {}
2125
2126 private:
2127 /**
2128 Set current_elem to the given Interval but do not touch the
2129 next pointer of the given Interval.
2130 */
2131 inline void set(Interval *iv) { *p = iv; }
2132 /// Insert the given element before current_elem.
2133 inline void insert(Interval *iv) {
2134 iv->next = *p;
2135 set(iv);
2136 }
2137 /// Remove current_elem.
2138 inline void remove(Gtid_set *gtid_set) {
2139 assert(get() != nullptr);
2140 Interval *next = (*p)->next;
2141 gtid_set->put_free_interval(*p);
2142 set(next);
2143 }
2144 /**
2145 Only Gtid_set is allowed to use set/insert/remove.
2146
2147 They are not safe to use from other code because: (1) very easy
2148 to make a mistakes (2) they don't clear cached_string_format or
2149 cached_string_length.
2150 */
2151 friend class Gtid_set;
2152 };
2153
2154 /**
2155 Iterator over all gtids in a Gtid_set. This is a const
2156 iterator; it does not allow modification of the Gtid_set.
2157 */
2159 public:
2160 Gtid_iterator(const Gtid_set *gs) : gtid_set(gs), sidno(0), ivit(gs) {
2161 if (gs->tsid_lock != nullptr) gs->tsid_lock->assert_some_wrlock();
2162 next_sidno();
2163 }
2164 /// Advance to next gtid.
2165 inline void next() {
2166 assert(gno > 0 && sidno > 0);
2167 // go to next GTID in current interval
2168 gno++;
2169 // end of interval? then go to next interval for this sidno
2170 if (gno == ivit.get()->end) {
2171 ivit.next();
2172 const Interval *iv = ivit.get();
2173 // last interval for this sidno? then go to next sidno
2174 if (iv == nullptr) {
2175 next_sidno();
2176 // last sidno? then don't try more
2177 if (sidno == 0) return;
2178 iv = ivit.get();
2179 }
2180 gno = iv->start;
2181 }
2182 }
2183 /// Return next gtid, or {0,0} if we reached the end.
2184 inline Gtid get() const {
2185 Gtid ret = {sidno, gno};
2186 return ret;
2187 }
2188
2189 private:
2190 /// Find the next sidno that has one or more intervals.
2191 inline void next_sidno() {
2192 const Interval *iv;
2193 do {
2194 sidno++;
2195 if (sidno > gtid_set->get_max_sidno()) {
2196 sidno = 0;
2197 gno = 0;
2198 return;
2199 }
2201 iv = ivit.get();
2202 } while (iv == nullptr);
2203 gno = iv->start;
2204 }
2205 /// The Gtid_set we iterate over.
2207 /**
2208 The SIDNO of the current element, or 0 if the iterator is past
2209 the last element.
2210 */
2212 /**
2213 The GNO of the current element, or 0 if the iterator is past the
2214 last element.
2215 */
2217 /// Iterator over the intervals for the current SIDNO.
2219 };
2220
2221 public:
2222 /// @brief Encodes this Gtid_set as a binary string.
2223 /// @param buf Buffer to write into
2224 /// @param skip_tagged_gtids When true, tagged GTIDS will be filtered out
2225 void encode(uchar *buf, bool skip_tagged_gtids = false) const;
2226
2227 /// @brief Returns the length of this Gtid_set when encoded using the
2228 /// encode() function. Before calculation, analyzes GTID set format
2229 /// @param skip_tagged_gtids When true, tagged GTIDS will be filtered out
2230 /// @return length of Gtid_set encoding
2231 size_t get_encoded_length(bool skip_tagged_gtids = false) const;
2232
2233 /// Returns the length of this Gtid_set when encoded using the
2234 /// encode() function. Uses already analyzed GTID set format (faster version)
2235 /// @param format Gtid format
2236 /// @param skip_tagged_gtids When true, tagged GTIDS will be filtered out
2237 /// @return Encoded gtid_set length
2238 std::size_t get_encoded_length(const mysql::gtid::Gtid_format &format,
2239 bool skip_tagged_gtids) const;
2240
2241 /// Returns true if this Gtid_set is equal to the other Gtid_set.
2242 /// @param[in] other Gtid set to compare against
2243 /// @return true in case gtid sets contain the same GTIDs
2244 bool equals(const Gtid_set *other) const;
2245
2246 private:
2247 /**
2248 Contains a list of intervals allocated by this Gtid_set. When a
2249 method of this class needs a new interval and there are no more
2250 free intervals, a new Interval_chunk is allocated and the
2251 intervals of it are added to the list of free intervals.
2252 */
2256 };
2257 /// The default number of intervals in an Interval_chunk.
2258 static const int CHUNK_GROW_SIZE = 8;
2259
2260 /**
2261 Return true if the given sidno of this Gtid_set contains the same
2262 intervals as the given sidno of the other Gtid_set.
2263
2264 @param sidno SIDNO to check for this Gtid_set.
2265 @param other Other Gtid_set
2266 @param other_sidno SIDNO to check in other.
2267 @return true if equal, false is not equal.
2268 */
2269 bool sidno_equals(rpl_sidno sidno, const Gtid_set *other,
2270 rpl_sidno other_sidno) const;
2271
2272 /// Return the number of intervals for the given sidno.
2273 int get_n_intervals(rpl_sidno sidno) const {
2274 Const_interval_iterator ivit(this, sidno);
2275 int ret = 0;
2276 while (ivit.get() != nullptr) {
2277 ret++;
2278 ivit.next();
2279 }
2280 return ret;
2281 }
2282 /// Return the number of intervals in this Gtid_set.
2283 int get_n_intervals() const {
2284 if (tsid_lock != nullptr) tsid_lock->assert_some_wrlock();
2285 rpl_sidno max_sidno = get_max_sidno();
2286 int ret = 0;
2287 for (rpl_sidno sidno = 1; sidno < max_sidno; sidno++)
2288 ret += get_n_intervals(sidno);
2289 return ret;
2290 }
2291
2292 /// @brief Goes through recorded tsids. In case any of the TSIDs has a tag,
2293 /// this function will return Gtid_format::tagged. Otherwise, it will
2294 /// return Gtid_format::untagged
2295 /// @param skip_tagged_gtids When true, function will always return
2296 /// Gtid_format::untagged
2297 /// @returns Gtid encoding format
2299 bool skip_tagged_gtids) const;
2300
2301 /**
2302 Allocates a new chunk of Intervals and adds them to the list of
2303 unused intervals.
2304
2305 @param size The number of intervals in this chunk
2306 */
2307 void create_new_chunk(int size);
2308 /**
2309 Returns a fresh new Interval object.
2310
2311 This usually does not require any real allocation, it only pops
2312 the first interval from the list of free intervals. If there are
2313 no free intervals, it calls create_new_chunk.
2314
2315 @param out The resulting Interval* will be stored here.
2316 */
2317 void get_free_interval(Interval **out);
2318 /**
2319 Puts the given interval in the list of free intervals. Does not
2320 unlink it from its place in any other list.
2321 */
2322 void put_free_interval(Interval *iv);
2323 /**
2324 Like add_interval_memory, but does not acquire
2325 free_intervals_mutex.
2326 @see Gtid_set::add_interval_memory
2327 */
2328 void add_interval_memory_lock_taken(int n_ivs, Interval *ivs);
2329
2330 /// Read-write lock that protects updates to the number of TSIDs.
2332 /**
2333 Lock protecting the list of free intervals. This lock is only
2334 used if tsid_lock is not NULL.
2335 */
2337 /**
2338 Class representing a lock on free_intervals_mutex.
2339
2340 This is used by the add_* and remove_* functions. The lock is
2341 declared by the top-level function and a pointer to the lock is
2342 passed down to low-level functions. If the low-level function
2343 decides to access the free intervals list, then it acquires the
2344 lock. The lock is then automatically released by the destructor
2345 when the top-level function returns.
2346
2347 The lock is not taken if Gtid_set->tsid_lock == NULL; such
2348 Gtid_sets are assumed to be thread-local.
2349 */
2351 public:
2352 /// Create a new lock, but do not acquire it.
2354 : gtid_set(_gtid_set), locked(false) {}
2355 /// Lock the lock if it is not already locked.
2357 if (gtid_set->tsid_lock && !locked) {
2359 locked = true;
2360 }
2361 }
2362 /// Lock the lock if it is locked.
2364 if (gtid_set->tsid_lock && locked) {
2366 locked = false;
2367 }
2368 }
2369 /// Destroy this object and unlock the lock if it is locked.
2371
2372 private:
2375 };
2378 }
2379
2380 /**
2381 Adds the interval (start, end) to the given Interval_iterator.
2382
2383 This is the lowest-level function that adds gtids; this is where
2384 Interval objects are added, grown, or merged.
2385
2386 @param ivitp Pointer to iterator. After this function returns,
2387 the current_element of the iterator will be the interval that
2388 contains start and end.
2389 @param start The first GNO in the interval.
2390 @param end The first GNO after the interval.
2391 @param lock If this function has to add or remove an interval,
2392 then this lock will be taken unless it is already taken. This
2393 mechanism means that the lock will be taken lazily by
2394 e.g. add_gtid_set() the first time that the list of free intervals
2395 is accessed, and automatically released when add_gtid_set()
2396 returns.
2397 */
2398 void add_gno_interval(Interval_iterator *ivitp, rpl_gno start, rpl_gno end,
2399 Free_intervals_lock *lock);
2400 /**
2401 Removes the interval (start, end) from the given
2402 Interval_iterator. This is the lowest-level function that removes
2403 gtids; this is where Interval objects are removed, truncated, or
2404 split.
2405
2406 It is not required that the gtids in the interval exist in this
2407 Gtid_set.
2408
2409 @param ivitp Pointer to iterator. After this function returns,
2410 the current_element of the iterator will be the next interval
2411 after end.
2412 @param start The first GNO in the interval.
2413 @param end The first GNO after the interval.
2414 @param lock If this function has to add or remove an interval,
2415 then this lock will be taken unless it is already taken. This
2416 mechanism means that the lock will be taken lazily by
2417 e.g. add_gtid_set() the first time that the list of free intervals
2418 is accessed, and automatically released when add_gtid_set()
2419 returns.
2420 */
2421 void remove_gno_interval(Interval_iterator *ivitp, rpl_gno start, rpl_gno end,
2422 Free_intervals_lock *lock);
2423 /**
2424 Adds a list of intervals to the given SIDNO.
2425
2426 The SIDNO must exist in the Gtid_set before this function is called.
2427
2428 @param sidno The SIDNO to which intervals will be added.
2429 @param ivit Iterator over the intervals to add. This is typically
2430 an iterator over some other Gtid_set.
2431 @param lock If this function has to add or remove an interval,
2432 then this lock will be taken unless it is already taken. This
2433 mechanism means that the lock will be taken lazily by
2434 e.g. add_gtid_set() the first time that the list of free intervals
2435 is accessed, and automatically released when add_gtid_set()
2436 returns.
2437 */
2438 void add_gno_intervals(rpl_sidno sidno, Const_interval_iterator ivit,
2439 Free_intervals_lock *lock);
2440 /**
2441 Removes a list of intervals from the given SIDNO.
2442
2443 It is not required that the intervals exist in this Gtid_set.
2444
2445 @param sidno The SIDNO from which intervals will be removed.
2446 @param ivit Iterator over the intervals to remove. This is typically
2447 an iterator over some other Gtid_set.
2448 @param lock If this function has to add or remove an interval,
2449 then this lock will be taken unless it is already taken. This
2450 mechanism means that the lock will be taken lazily by
2451 e.g. add_gtid_set() the first time that the list of free intervals
2452 is accessed, and automatically released when add_gtid_set()
2453 returns.
2454 */
2455 void remove_gno_intervals(rpl_sidno sidno, Const_interval_iterator ivit,
2456 Free_intervals_lock *lock);
2457
2458 /// Returns true if every interval of sub is a subset of some
2459 /// interval of super.
2460 static bool is_interval_subset(Const_interval_iterator *sub,
2461 Const_interval_iterator *super);
2462 /// Returns true if at least one sidno in ivit1 is also in ivit2.
2463 static bool is_interval_intersection_nonempty(Const_interval_iterator *ivit1,
2464 Const_interval_iterator *ivit2);
2465
2466 /// Tsid_map associated with this Gtid_set.
2468 /**
2469 Array where the N'th element contains the head pointer to the
2470 intervals of SIDNO N+1.
2471 */
2473 /// Linked list of free intervals.
2475 /// Linked list of chunks.
2477 /// If the string is cached.
2479 /// The string length.
2480 mutable size_t cached_string_length;
2481 /// The String_format that was used when cached_string_length was computed.
2483#ifndef NDEBUG
2484 /**
2485 The number of chunks. Used only to check some invariants when
2486 DBUG is on.
2487 */
2489#endif
2490 /// Used by unit tests that need to access private members.
2491#ifdef FRIEND_OF_GTID_SET
2492 friend FRIEND_OF_GTID_SET;
2493#endif
2494 /// Only Free_intervals_lock is allowed to access free_intervals_mutex.
2496};
2497
2498/**
2499 Holds information about a Gtid_set. Can also be NULL.
2500
2501 This is used as backend storage for @@session.gtid_next_list. The
2502 idea is that we allow the user to set this to NULL, but we keep the
2503 Gtid_set object so that we can re-use the allocated memory and
2504 avoid costly allocations later.
2505
2506 This is stored in struct system_variables (defined in sql_class.h),
2507 which is cleared using memset(0); hence the negated form of
2508 is_non_null.
2509
2510 The convention is: if is_non_null is false, then the value of the
2511 session variable is NULL, and the field gtid_set may be NULL or
2512 non-NULL. If is_non_null is true, then the value of the session
2513 variable is not NULL, and the field gtid_set has to be non-NULL.
2514
2515 This is a POD. It has to be a POD because it is stored in
2516 THD::variables.
2517*/
2519 /// Pointer to the Gtid_set.
2521 /// True if this Gtid_set is NULL.
2523 /// Return NULL if this is NULL, otherwise return the Gtid_set.
2524 inline Gtid_set *get_gtid_set() const {
2525 assert(!(is_non_null && gtid_set == nullptr));
2526 return is_non_null ? gtid_set : nullptr;
2527 }
2528 /**
2529 Do nothing if this object is non-null; set to empty set otherwise.
2530
2531 @return NULL if out of memory; Gtid_set otherwise.
2532 */
2534 if (!is_non_null) {
2535 if (gtid_set == nullptr)
2536 gtid_set = new Gtid_set(sm);
2537 else
2538 gtid_set->clear();
2539 }
2540 is_non_null = (gtid_set != nullptr);
2541 return gtid_set;
2542 }
2543 /// Set this Gtid_set to NULL.
2544 inline void set_null() { is_non_null = false; }
2545};
2546
2547/**
2548 Represents the set of GTIDs that are owned by some thread.
2549
2550 This data structure has a read-write lock that protects the number
2551 of SIDNOs. The lock is provided by the invoker of the constructor
2552 and it is generally the caller's responsibility to acquire the read
2553 lock. Access methods assert that the caller already holds the read
2554 (or write) lock. If a method of this class grows the number of
2555 SIDNOs, then the method temporarily upgrades this lock to a write
2556 lock and then degrades it to a read lock again; there will be a
2557 short period when the lock is not held at all.
2558
2559 The internal representation is a multi-valued map from GTIDs to
2560 threads, mapping GTIDs to one or more threads that owns it.
2561
2562 In Group Replication multiple threads can own a GTID whereas if GR
2563 is disabeld there is at most one owner per GTID.
2564*/
2566 public:
2567 /**
2568 Constructs a new, empty Owned_gtids object.
2569
2570 @param tsid_lock Read-write lock that protects updates to the
2571 number of TSIDs.
2572 */
2574 /// Destroys this Owned_gtids.
2575 ~Owned_gtids();
2576 /**
2577 Add a GTID to this Owned_gtids.
2578
2579 @param gtid The Gtid to add.
2580 @param owner The my_thread_id of the gtid to add.
2581 @return RETURN_STATUS_OK or RETURN_STATUS_REPORTED_ERROR.
2582 */
2584
2585 /*
2586 Fill all gtids into the given Gtid_set object. It doesn't clear the given
2587 gtid set before filling its owned gtids into it.
2588 */
2589 void get_gtids(Gtid_set &gtid_set) const;
2590 /**
2591 Removes the given GTID.
2592
2593 If the gtid does not exist in this Owned_gtids object, does
2594 nothing.
2595
2596 @param gtid The Gtid.
2597 @param owner thread_id of the owner thread
2598 */
2599 void remove_gtid(const Gtid &gtid, const my_thread_id owner);
2600 /**
2601 Ensures that this Owned_gtids object can accommodate SIDNOs up to
2602 the given SIDNO.
2603
2604 If this Owned_gtids object needs to be resized, then the lock
2605 will be temporarily upgraded to a write lock and then degraded to
2606 a read lock again; there will be a short period when the lock is
2607 not held at all.
2608
2609 @param sidno The SIDNO.
2610 @return RETURN_STATUS_OK or RETURN_STATUS_REPORTED_ERROR.
2611 */
2613 /// Returns true if there is a least one element of this Owned_gtids
2614 /// set in the other Gtid_set.
2615 bool is_intersection_nonempty(const Gtid_set *other) const;
2616 /// Returns true if this Owned_gtids is empty.
2617 bool is_empty() const {
2618 Gtid_iterator git(this);
2619 return git.get().sidno == 0;
2620 }
2621 /// Returns the maximal sidno that this Owned_gtids currently has space for.
2623 if (tsid_lock != nullptr) {
2625 }
2626 return static_cast<rpl_sidno>(sidno_to_hash.size());
2627 }
2628
2629 /**
2630 Write a string representation of this Owned_gtids to the given buffer.
2631
2632 @param out Buffer to write to.
2633 @return Number of characters written.
2634 */
2635 int to_string(char *out) const {
2636 char *p = out;
2637 rpl_sidno max_sidno = get_max_sidno();
2638 for (const auto &sid_it : global_tsid_map->get_sorted_sidno()) {
2639 rpl_sidno sidno = sid_it.second;
2640 if (sidno > max_sidno) continue;
2641 bool printed_sid = false;
2642 for (const auto &key_and_value : *get_hash(sidno)) {
2643 Node *node = key_and_value.second.get();
2644 assert(node != nullptr);
2645 if (!printed_sid) {
2647 printed_sid = true;
2648 }
2649 p += sprintf(p, ":%" PRId64 "#%u", node->gno, node->owner);
2650 }
2651 }
2652 *p = 0;
2653 return (int)(p - out);
2654 }
2655
2656 /**
2657 Return an upper bound on the length of the string representation
2658 of this Owned_gtids. The actual length may be smaller. This
2659 includes the trailing '\0'.
2660 */
2661 size_t get_max_string_length() const {
2662 rpl_sidno max_sidno = get_max_sidno();
2663 size_t ret = 0;
2664 for (rpl_sidno sidno = 1; sidno <= max_sidno; sidno++) {
2665 size_t records = get_hash(sidno)->size();
2666 if (records > 0)
2667 ret +=
2669 records * (1 + MAX_GNO_TEXT_LENGTH + 1 + MAX_THREAD_ID_TEXT_LENGTH);
2670 }
2671 return 1 + ret;
2672 }
2673
2674 /**
2675 Return true if the given thread is the owner of any gtids.
2676 */
2678 Gtid_iterator git(this);
2679 Node *node = git.get_node();
2680 while (node != nullptr) {
2681 if (node->owner == thd_id) return true;
2682 git.next();
2683 node = git.get_node();
2684 }
2685 return false;
2686 }
2687
2688#ifndef NDEBUG
2689 /**
2690 Debug only: return a newly allocated string representation of
2691 this Owned_gtids.
2692 */
2693 char *to_string() const {
2696 assert(str != nullptr);
2697 to_string(str);
2698 return str;
2699 }
2700 /// Debug only: print this Owned_gtids to stdout.
2701 void print() const {
2702 char *str = to_string();
2703 printf("%s\n", str);
2704 my_free(str);
2705 }
2706#endif
2707 /**
2708 Print this Owned_gtids to the trace file if debug is enabled; no-op
2709 otherwise.
2710 */
2711 void dbug_print(const char *text [[maybe_unused]] = "") const {
2712#ifndef NDEBUG
2713 char *str = to_string();
2714 DBUG_PRINT("info", ("%s%s%s", text, *text ? ": " : "", str));
2715 my_free(str);
2716#endif
2717 }
2718
2719 /**
2720 If thd_id==0, returns true when gtid is not owned by any thread.
2721 If thd_id!=0, returns true when gtid is owned by that thread.
2722 */
2723 bool is_owned_by(const Gtid &gtid, const my_thread_id thd_id) const;
2724
2725 private:
2726 /// Represents one owned GTID.
2727 struct Node {
2728 /// GNO of the GTID.
2730 /// Owner of the GTID.
2732 };
2733 /// Read-write lock that protects updates to the number of TSIDs.
2735 /// Returns the hash for the given SIDNO.
2737 rpl_sidno sidno) const {
2738 assert(sidno >= 1 && sidno <= get_max_sidno());
2739 if (tsid_lock != nullptr) {
2741 }
2742 return sidno_to_hash[sidno - 1];
2743 }
2744 /// Return true iff this Owned_gtids object contains the given gtid.
2745 bool contains_gtid(const Gtid &gtid) const;
2746
2747 /// Growable array of hashes.
2751
2752 public:
2753 /**
2754 Iterator over all gtids in a Owned_gtids set. This is a const
2755 iterator; it does not allow modification of the set.
2756 */
2758 public:
2760 : owned_gtids(og), sidno(1), hash(nullptr), node(nullptr) {
2762 if (sidno <= max_sidno) {
2764 node_it = hash->begin();
2765 }
2766 next();
2767 }
2768 /// Advance to next GTID.
2769 inline void next() {
2770#ifndef NDEBUG
2772#endif
2773
2774 while (sidno <= max_sidno) {
2775 assert(hash != nullptr);
2776 if (node_it != hash->end()) {
2777 node = node_it->second.get();
2778 assert(node != nullptr);
2779 // Jump to next node on next iteration.
2780 ++node_it;
2781 return;
2782 }
2783
2784 // hash is initialized on constructor or in previous iteration
2785 // for current SIDNO, so we must increment for next iteration.
2786 sidno++;
2787 if (sidno <= max_sidno) {
2789 node_it = hash->begin();
2790 }
2791 }
2792 node = nullptr;
2793 }
2794 /// Return next GTID, or {0,0} if we reached the end.
2795 inline Gtid get() const {
2796 Gtid ret = {0, 0};
2797 if (node) {
2798 ret.sidno = sidno;
2799 ret.gno = node->gno;
2800 }
2801 return ret;
2802 }
2803 /// Return the current GTID Node, or NULL if we reached the end.
2804 inline Node *get_node() const { return node; }
2805
2806 private:
2807 /// The Owned_gtids set we iterate over.
2809 /// The SIDNO of the current element, or 1 in the initial iteration.
2811 /// Max SIDNO of the current iterator.
2813 /// Current SIDNO hash.
2815 /// Current node iterator on current SIDNO hash.
2818 /// Current node on current SIDNO hash.
2820 };
2821};
2822
2823/**
2824 Represents the server's GTID state: the set of committed GTIDs, the
2825 set of lost gtids, the set of owned gtids, the owner of each owned
2826 gtid, and a Mutex_cond_array that protects updates to gtids of
2827 each SIDNO.
2828
2829 Locking:
2830
2831 This data structure has a read-write lock that protects the number
2832 of SIDNOs, and a Mutex_cond_array that contains one mutex per SIDNO.
2833 The rwlock is always the global_tsid_lock.
2834
2835 Access methods generally assert that the caller already holds the
2836 appropriate lock:
2837
2838 - before accessing any global data, hold at least the rdlock.
2839
2840 - before accessing a specific SIDNO in a Gtid_set or Owned_gtids
2841 (e.g., calling Gtid_set::_add_gtid(Gtid)), hold either the rdlock
2842 and the SIDNO's mutex lock; or the wrlock. If you need to hold
2843 multiple mutexes, they must be acquired in order of increasing
2844 SIDNO.
2845
2846 - before starting an operation that needs to access all SIDs
2847 (e.g. Gtid_set::to_string()), hold the wrlock.
2848
2849 The access type (read/write) does not matter; the write lock only
2850 implies that the entire data structure is locked whereas the read
2851 lock implies that everything except TSID-specific data is locked.
2852*/
2854 public:
2855 /**
2856 Constructs a new Gtid_state object.
2857
2858 @param _tsid_lock Read-write lock that protects updates to the
2859 number of TSIDs.
2860 @param _tsid_map Tsid_map used by this Gtid_state.
2861 */
2862 Gtid_state(Checkable_rwlock *_tsid_lock, Tsid_map *_tsid_map)
2863 : tsid_lock(_tsid_lock),
2864 tsid_map(_tsid_map),
2872 /**
2873 Add @@GLOBAL.SERVER_UUID to this binlog's Tsid_map.
2874
2875 This can't be done in the constructor because the constructor is
2876 invoked at server startup before SERVER_UUID is initialized.
2877
2878 The caller must hold the read lock or write lock on tsid_locks
2879 before invoking this function.
2880
2881 @retval 0 Success
2882 @retval 1 Error (out of memory or IO error).
2883 */
2884 int init();
2885 /**
2886 Reset the state and persistor after RESET BINARY LOGS AND GTIDS:
2887 remove all logged and lost gtids, but keep owned gtids as they are.
2888
2889 The caller must hold the write lock on tsid_lock before calling
2890 this function.
2891
2892 @param thd Thread requesting to reset the persistor
2893
2894 @retval 0 Success
2895 @retval -1 Error
2896 */
2897 int clear(THD *thd);
2898 /**
2899 Returns true if the given GTID is logged.
2900
2901 @param gtid The Gtid to check.
2902
2903 @retval true The gtid is logged in the binary log.
2904 @retval false The gtid is not logged in the binary log.
2905 */
2906 bool is_executed(const Gtid &gtid) const {
2907 DBUG_TRACE;
2909 bool ret = executed_gtids.contains_gtid(gtid);
2910 return ret;
2911 }
2912 /**
2913 Returns true if GTID is owned, otherwise returns 0.
2914
2915 @param gtid The Gtid to check.
2916 @return true if some thread owns the gtid, false if the gtid is
2917 not owned
2918 */
2919 bool is_owned(const Gtid &gtid) const {
2920 return !owned_gtids.is_owned_by(gtid, 0);
2921 }
2922#ifdef MYSQL_SERVER
2923 /**
2924 Acquires ownership of the given GTID, on behalf of the given thread.
2925
2926 The caller must lock the SIDNO before invoking this function.
2927
2928 @param thd The thread that will own the GTID.
2929 @param gtid The Gtid to acquire ownership of.
2930 @return RETURN_STATUS_OK or RETURN_STATUS_REPORTED_ERROR.
2931 */
2932 enum_return_status acquire_ownership(THD *thd, const Gtid &gtid);
2933 /**
2934 This function updates both the THD and the Gtid_state to reflect that
2935 the transaction set of transactions has ended, and it does this for the
2936 whole commit group (by following the thd->next_to_commit pointer).
2937
2938 It will:
2939
2940 - Clean up the thread state when a thread owned GTIDs is empty.
2941 - Release ownership of all GTIDs owned by the THDs. This removes
2942 the GTIDs from Owned_gtids and clears the ownership status in the
2943 THDs object.
2944 - Add the owned GTIDs to executed_gtids when the thread is committing.
2945 - Decrease counters of GTID-violating transactions.
2946 - Send a broadcast on the condition variable for every sidno for
2947 which we released ownership.
2948
2949 @param first_thd The first thread of the group commit that needs GTIDs to
2950 be updated.
2951 */
2952 void update_commit_group(THD *first_thd);
2953 /**
2954 Remove the GTID owned by thread from owned GTIDs, stating that
2955 thd->owned_gtid was committed.
2956
2957 This will:
2958 - remove owned GTID from owned_gtids;
2959 - remove all owned GTIDS from thd->owned_gtid and thd->owned_gtid_set;
2960
2961 @param thd Thread for which owned gtids are updated.
2962 */
2963 void update_on_commit(THD *thd);
2964 /**
2965 Update the state after the given thread has rollbacked.
2966
2967 This will:
2968 - release ownership of all GTIDs owned by the THD;
2969 - remove owned GTID from owned_gtids;
2970 - remove all owned GTIDS from thd->owned_gtid and thd->owned_gtid_set;
2971 - send a broadcast on the condition variable for every sidno for
2972 which we released ownership.
2973
2974 @param thd Thread for which owned gtids are updated.
2975 */
2976 void update_on_rollback(THD *thd);
2977
2978 /**
2979 Acquire anonymous ownership.
2980
2981 The caller must hold either tsid_lock.rdlock or
2982 tsid_lock.wrlock. (The caller must have taken the lock and checked
2983 that gtid_mode!=ON before calling this function, or else the
2984 gtid_mode could have changed to ON by a concurrent SET GTID_MODE.)
2985 */
2987 DBUG_TRACE;
2989 assert(global_gtid_mode.get() != Gtid_mode::ON);
2990#ifndef NDEBUG
2991 int32 new_value =
2992#endif
2994 DBUG_PRINT("info",
2995 ("atomic_anonymous_gtid_count increased to %d", new_value));
2996 assert(new_value >= 1);
2997 return;
2998 }
2999
3000 /// Release anonymous ownership.
3002 DBUG_TRACE;
3004 assert(global_gtid_mode.get() != Gtid_mode::ON);
3005#ifndef NDEBUG
3006 int32 new_value =
3007#endif
3009 DBUG_PRINT("info",
3010 ("atomic_anonymous_gtid_count decreased to %d", new_value));
3011 assert(new_value >= 0);
3012 return;
3013 }
3014
3015 /// Return the number of clients that hold anonymous ownership.
3017
3018 /**
3019 Increase the global counter when starting a GTID-violating
3020 transaction having GTID_NEXT=AUTOMATIC.
3021 */
3023 DBUG_TRACE;
3026#ifndef NDEBUG
3027 int32 new_value =
3028#endif
3030 DBUG_PRINT(
3031 "info",
3032 ("ongoing_automatic_gtid_violating_transaction_count increased to %d",
3033 new_value));
3034 assert(new_value >= 1);
3035 return;
3036 }
3037
3038 /**
3039 Decrease the global counter when ending a GTID-violating
3040 transaction having GTID_NEXT=AUTOMATIC.
3041 */
3043 DBUG_TRACE;
3044#ifndef NDEBUG
3049 int32 new_value =
3050#endif
3052 DBUG_PRINT(
3053 "info",
3054 ("ongoing_automatic_gtid_violating_transaction_count decreased to %d",
3055 new_value));
3056 assert(new_value >= 0);
3057 return;
3058 }
3059
3060 /**
3061 Return the number of ongoing GTID-violating transactions having
3062 GTID_NEXT=AUTOMATIC.
3063 */
3066 }
3067
3068 /**
3069 Increase the global counter when starting a GTID-violating
3070 transaction having GTID_NEXT=ANONYMOUS.
3071 */
3073 DBUG_TRACE;
3074 assert(global_gtid_mode.get() != Gtid_mode::ON);
3076#ifndef NDEBUG
3077 int32 new_value =
3078#endif
3080 DBUG_PRINT("info", ("atomic_anonymous_gtid_violation_count increased to %d",
3081 new_value));
3082 assert(new_value >= 1);
3083 return;
3084 }
3085
3086 /**
3087 Decrease the global counter when ending a GTID-violating
3088 transaction having GTID_NEXT=ANONYMOUS.
3089 */
3091 DBUG_TRACE;
3092#ifndef NDEBUG
3094 assert(global_gtid_mode.get() != Gtid_mode::ON);
3097 int32 new_value =
3098#endif
3100 DBUG_PRINT(
3101 "info",
3102 ("ongoing_anonymous_gtid_violating_transaction_count decreased to %d",
3103 new_value));
3104 assert(new_value >= 0);
3105 return;
3106 }
3107
3109
3110 /**
3111 Return the number of ongoing GTID-violating transactions having
3112 GTID_NEXT=AUTOMATIC.
3113 */
3116 }
3117
3118 /**
3119 Increase the global counter when starting a call to
3120 WAIT_FOR_EXECUTED_GTID_SET.
3121 */
3123 DBUG_TRACE;
3124 assert(global_gtid_mode.get() != Gtid_mode::OFF);
3125#ifndef NDEBUG
3126 int32 new_value =
3127#endif
3129 DBUG_PRINT("info", ("atomic_gtid_wait_count changed from %d to %d",
3130 new_value - 1, new_value));
3131 assert(new_value >= 1);
3132 return;
3133 }
3134
3135 /**
3136 Decrease the global counter when ending a call to
3137 WAIT_FOR_EXECUTED_GTID_SET.
3138 */
3140 DBUG_TRACE;
3141 assert(global_gtid_mode.get() != Gtid_mode::OFF);
3142#ifndef NDEBUG
3143 int32 new_value =
3144#endif
3146 DBUG_PRINT("info", ("atomic_gtid_wait_count changed from %d to %d",
3147 new_value + 1, new_value));
3148 assert(new_value >= 0);
3149 return;
3150 }
3151
3152 /**
3153 Return the number of clients that have an ongoing call to
3154 WAIT_FOR_EXECUTED_GTID_SET.
3155 */
3157
3158#endif // ifdef MYSQL_SERVER
3159 /**
3160 Computes the next available GNO.
3161
3162 @param sidno The GTID's SIDNO.
3163
3164 @retval -1 The range of GNOs was exhausted (i.e., more than 1<<63-1
3165 GTIDs with the same UUID have been generated).
3166 @retval >0 The GNO for the GTID.
3167 */
3168 rpl_gno get_automatic_gno(rpl_sidno sidno) const;
3169
3170 private:
3171 /**
3172 The next_free_gno map contains next_free_gno for recorded sidnos.
3173 The next_free_gno variable will be set with the supposed next free GNO
3174 every time a new GNO is delivered automatically or when a transaction is
3175 rolled back, releasing a GNO smaller than the last one delivered.
3176 It was introduced in an optimization of Gtid_state::get_automatic_gno and
3177 Gtid_state::generate_automatic_gtid functions.
3178
3179 Locking scheme
3180
3181 This variable can be read and modified in four places:
3182 - During server startup, holding global_tsid_lock.wrlock;
3183 - By a client thread holding global_tsid_lock.wrlock
3184 when executing RESET BINARY LOGS AND GTIDS
3185 - By a client thread calling MYSQL_BIN_LOG::write_transaction function
3186 (often the group commit FLUSH stage leader). It will call
3187 Gtid_state::generate_automatic_gtid, that will acquire
3188 global_tsid_lock.rdlock and lock_sidno(get_server_sidno()) when getting a
3189 new automatically generated GTID;
3190 - By a client thread rolling back, holding global_tsid_lock.rdlock
3191 and lock_sidno(get_server_sidno()).
3192 */
3193 std::unordered_map<rpl_sidno, rpl_gno> next_free_gno_map;
3194
3195 public:
3198 /**
3199 Return the last executed GNO for a given SIDNO, e.g.
3200 for the following set: UUID:1-10, UUID:12, UUID:15-20
3201 20 will be returned.
3202
3203 @param sidno The GTID's SIDNO.
3204
3205 @retval The GNO or 0 if set is empty.
3206 */
3208
3209 /**
3210 Generates the GTID (or ANONYMOUS, if GTID_MODE = OFF or
3211 OFF_PERMISSIVE) for the THD, and acquires ownership.
3212 Before this function, the caller needs to assign sidnos for automatic
3213 transactions and lock sidno_set (see specify_transaction_sidno).
3214
3215 @param thd The thread.
3216 @param specified_sidno Externally generated sidno.
3217 @param specified_gno Externally generated gno.
3218 @see Locked_sidno_set
3219
3220 @return RETURN_STATUS_OK or RETURN_STATUS_ERROR. Error can happen
3221 in case of out of memory or if the range of GNOs was exhausted.
3222 */
3224 rpl_sidno specified_sidno = 0,
3225 rpl_gno specified_gno = 0);
3226
3227 /// @brief Determines sidno for thd transaction. In case transaction
3228 /// is automatic, sidno is generated and added to sidno_set for future
3229 /// locking (after all transactions from binlog commit group have been added)
3230 /// @details The usage scheme is as follows: transaction for the binlog
3231 /// commit group are assigned a sidno. Sidnos are added to sidno_set in this
3232 /// function.
3233 /// Afterwards, sidno_set must be locked by the caller. This operation must
3234 /// be performed before the call to generate_automatic_gtid
3235 /// @returns sidno specified for thd transaction
3237 Gtid_state::Locked_sidno_set &sidno_set);
3238
3239 /// Locks a mutex for the given SIDNO.
3240 void lock_sidno(rpl_sidno sidno) { tsid_locks.lock(sidno); }
3241 /// Unlocks a mutex for the given SIDNO.
3242 void unlock_sidno(rpl_sidno sidno) { tsid_locks.unlock(sidno); }
3243 /// Broadcasts updates for the given SIDNO.
3245 /// Assert that we own the given SIDNO.
3247 tsid_locks.assert_owner(sidno);
3248 }
3249#ifdef MYSQL_SERVER
3250 /**
3251 Wait for a signal on the given SIDNO.
3252
3253 NOTE: This releases a lock!
3254
3255 This requires that the caller holds a read lock on tsid_lock. It
3256 will release the lock before waiting; neither global_tsid_lock nor
3257 the mutex lock on SIDNO will not be held when this function
3258 returns.
3259
3260 @param thd THD object of the caller.
3261 @param sidno Sidno to wait for.
3262 @param[in] abstime The absolute point in time when the wait times
3263 out and stops, or NULL to wait indefinitely.
3264 @param[in] update_thd_status when true updates the stage info with
3265 the new wait condition, when false keeps the current stage info.
3266
3267 @retval false Success.
3268 @retval true Failure: either timeout or thread was killed. If
3269 thread was killed, the error has been generated.
3270 */
3271 bool wait_for_sidno(THD *thd, rpl_sidno sidno, struct timespec *abstime,
3272 bool update_thd_status = true);
3273 /**
3274 This is only a shorthand for wait_for_sidno, which contains
3275 additional debug printouts and assertions for the case when the
3276 caller waits for one specific GTID.
3277 */
3278 bool wait_for_gtid(THD *thd, const Gtid &gtid,
3279 struct timespec *abstime = nullptr);
3280 /**
3281 Wait until the given Gtid_set is included in @@GLOBAL.GTID_EXECUTED.
3282
3283 @param thd The calling thread.
3284 @param gtid_set Gtid_set to wait for.
3285 @param[in] timeout The maximum number of milliseconds that the
3286 function should wait, or 0 to wait indefinitely.
3287 @param[in] update_thd_status when true updates the stage info with
3288 the new wait condition, when false keeps the current stage info.
3289
3290 @retval false Success.
3291 @retval true Failure: either timeout or thread was killed. If
3292 thread was killed, the error has been generated.
3293 */
3294 bool wait_for_gtid_set(THD *thd, Gtid_set *gtid_set, double timeout,
3295 bool update_thd_status = true);
3296#endif // ifdef MYSQL_SERVER
3297 /**
3298 Locks one mutex for each SIDNO where the given Gtid_set has at
3299 least one GTID. Locks are acquired in order of increasing SIDNO.
3300 */
3301 void lock_sidnos(const Gtid_set *set);
3302 /**
3303 Unlocks the mutex for each SIDNO where the given Gtid_set has at
3304 least one GTID.
3305 */
3306 void unlock_sidnos(const Gtid_set *set);
3307 /**
3308 Broadcasts the condition variable for each SIDNO where the given
3309 Gtid_set has at least one GTID.
3310 */
3311 void broadcast_sidnos(const Gtid_set *set);
3312 /**
3313 Ensure that owned_gtids, executed_gtids, lost_gtids, gtids_only_in_table,
3314 previous_gtids_logged and tsid_locks have room for at least as many SIDNOs
3315 as tsid_map.
3316
3317 This function must only be called in one place:
3318 Tsid_map::add_tsid().
3319
3320 Requires that the write lock on tsid_locks is held. If any object
3321 needs to be resized, then the lock will be temporarily upgraded to
3322 a write lock and then degraded to a read lock again; there will be
3323 a short period when the lock is not held at all.
3324
3325 @return RETURN_STATUS_OK or RETURN_STATUS_REPORTED_ERROR.
3326 */
3328
3329 /**
3330 Adds the given Gtid_set to lost_gtids and executed_gtids.
3331 lost_gtids must be a subset of executed_gtids.
3332 purged_gtid and executed_gtid sets are appended with the argument set
3333 provided the latter is disjoint with gtid_executed owned_gtids.
3334
3335 Requires that the caller holds global_tsid_lock.wrlock.
3336
3337 @param[in,out] gtid_set The gtid_set to add. If the gtid_set
3338 does not start with a plus sign (starts_with_plus is false),
3339 @@GLOBAL.GTID_PURGED will be removed from the gtid_set.
3340 @param starts_with_plus If true, the gtid_set passed is required to
3341 be disjoint from @@GLOBAL.GTID_PURGED; if false, the gtid_set passed
3342 is required to be a superset of @@GLOBAL.GTID_PURGED.
3343 @return RETURN_STATUS_OK or RETURN_STATUS_REPORTED_ERROR.
3344 */
3345 enum_return_status add_lost_gtids(Gtid_set *gtid_set, bool starts_with_plus);
3346
3347 /** Updates previously logged GTID set before writing to table. */
3348 void update_prev_gtids(Gtid_set *write_gtid_set);
3349
3350 /// Return a pointer to the Gtid_set that contains the lost gtids.
3351 const Gtid_set *get_lost_gtids() const { return &lost_gtids; }
3352 /*
3353 Return a pointer to the Gtid_set that contains the stored gtids
3354 in gtid_executed table.
3355 */
3356 const Gtid_set *get_executed_gtids() const { return &executed_gtids; }
3357 /*
3358 Return a pointer to the Gtid_set that contains the stored gtids
3359 only in gtid_executed table, not in binlog files.
3360 */
3362 return &gtids_only_in_table;
3363 }
3364 /*
3365 Return a pointer to the Gtid_set that contains the previous stored
3366 gtids in the last binlog file.
3367 */
3369 return &previous_gtids_logged;
3370 }
3371 /// Return a pointer to the Owned_gtids that contains the owned gtids.
3372 const Owned_gtids *get_owned_gtids() const { return &owned_gtids; }
3373 /// Return the server's SIDNO
3375 /// Return the server's TSID
3376 const Tsid &get_server_tsid() const {
3378 }
3379
3380 /// @brief Increments atomic_automatic_tagged_gtid_session_count
3383 }
3384
3385 /// @brief Decrements atomic_automatic_tagged_gtid_session_count
3388 }
3389
3390 /// @brief Checks whether there are ongoing sessions executing transactions
3391 /// with GTID_NEXT set to AUTOMATIC:tag
3392 /// @return true in case there are ongoing sessions with GTID_NEXT set
3393 /// to automatic, tagged
3396 }
3397
3398#ifndef NDEBUG
3399 /**
3400 Debug only: Returns an upper bound on the length of the string
3401 generated by to_string(), not counting '\0'. The actual length
3402 may be shorter.
3403 */
3404 size_t get_max_string_length() const {
3409 }
3410 /// Debug only: Generate a string in the given buffer and return the length.
3411 int to_string(char *buf) const {
3412 char *p = buf;
3413 p += sprintf(p, "Executed GTIDs:\n");
3415 p += sprintf(p, "\nOwned GTIDs:\n");
3417 p += sprintf(p, "\nLost GTIDs:\n");
3418 p += lost_gtids.to_string(p);
3419 p += sprintf(p, "\nGTIDs only_in_table:\n");
3420 p += lost_gtids.to_string(p);
3421 return (int)(p - buf);
3422 }
3423 /// Debug only: return a newly allocated string, or NULL on out-of-memory.
3424 char *to_string() const {
3427 to_string(str);
3428 return str;
3429 }
3430 /// Debug only: print this Gtid_state to stdout.
3431 void print() const {
3432 char *str = to_string();
3433 printf("%s", str);
3434 my_free(str);
3435 }
3436#endif
3437 /**
3438 Print this Gtid_state to the trace file if debug is enabled; no-op
3439 otherwise.
3440 */
3441 void dbug_print(const char *text [[maybe_unused]] = "") const {
3442#ifndef NDEBUG
3444 char *str = to_string();
3445 DBUG_PRINT("info", ("%s%s%s", text, *text ? ": " : "", str));
3446 my_free(str);
3447#endif
3448 }
3449 /**
3450 Save gtid owned by the thd into executed_gtids variable
3451 and gtid_executed table.
3452
3453 @param thd Session to commit
3454 @retval
3455 0 OK
3456 @retval
3457 -1 Error
3458 */
3459 int save(THD *thd);
3460 /**
3461 Insert the gtid set into table.
3462
3463 @param gtid_set contains a set of gtid, which holds
3464 the sidno and the gno.
3465
3466 @retval
3467 0 OK
3468 @retval
3469 -1 Error
3470 */
3471 int save(const Gtid_set *gtid_set);
3472 /**
3473 Save the set of gtids logged in the last binlog into gtid_executed table.
3474
3475 @retval
3476 0 OK
3477 @retval
3478 -1 Error
3479 */
3481 /**
3482 Fetch gtids from gtid_executed table and store them into
3483 gtid_executed set.
3484
3485 @retval
3486 0 OK
3487 @retval
3488 1 The table was not found.
3489 @retval
3490 -1 Error
3491 */
3493 /**
3494 Compress the gtid_executed table, read each row by the PK(sid, gno_start)
3495 in increasing order, compress the first consecutive gtids range
3496 (delete consecutive gtids from the second consecutive gtid, then
3497 update the first gtid) within a single transaction.
3498
3499 @param thd Thread requesting to compress the table
3500
3501 @retval
3502 0 OK
3503 @retval
3504 1 The table was not found.
3505 @retval
3506 -1 Error
3507 */
3508 int compress(THD *thd);
3509#ifdef MYSQL_SERVER
3510 /**
3511 Push a warning to client if user is modifying the gtid_executed
3512 table explicitly by a non-XA transaction. Push an error to client
3513 if user is modifying it explicitly by a XA transaction.
3514
3515 @param thd Thread requesting to access the table
3516 @param table The table is being accessed.
3517
3518 @retval 0 No warning or error was pushed to the client.
3519 @retval 1 Push a warning to client.
3520 @retval 2 Push an error to client.
3521 */
3523#endif
3524
3525 private:
3526 /**
3527 Remove the GTID owned by thread from owned GTIDs.
3528
3529 This will:
3530
3531 - Clean up the thread state if the thread owned GTIDs is empty.
3532 - Release ownership of all GTIDs owned by the THD. This removes
3533 the GTID from Owned_gtids and clears the ownership status in the
3534 THD object.
3535 - Add the owned GTID to executed_gtids if the is_commit flag is
3536 set.
3537 - Decrease counters of GTID-violating transactions.
3538 - Send a broadcast on the condition variable for every sidno for
3539 which we released ownership.
3540
3541 @param[in] thd Thread for which owned gtids are updated.
3542 @param[in] is_commit If true, the update is for a commit (not a rollback).
3543 */
3544 void update_gtids_impl(THD *thd, bool is_commit);
3545#ifdef HAVE_GTID_NEXT_LIST
3546 /// Lock all SIDNOs owned by the given THD.
3547 void lock_owned_sidnos(const THD *thd);
3548#endif
3549 /// Unlock all SIDNOs owned by the given THD.
3550 void unlock_owned_sidnos(const THD *thd);
3551 /// Broadcast the condition for all SIDNOs owned by the given THD.
3552 void broadcast_owned_sidnos(const THD *thd);
3553 /// Read-write lock that protects updates to the number of TSIDs.
3555 /// The Tsid_map used by this Gtid_state.
3557 /// Contains one mutex/cond pair for every SIDNO.
3559 /**
3560 The set of GTIDs that existed in some previously purged binary log.
3561 This is always a subset of executed_gtids.
3562 */
3564 /*
3565 The set of GTIDs that has been executed and
3566 stored into gtid_executed table.
3567 */
3569 /*
3570 The set of GTIDs that exists only in gtid_executed table, not in
3571 binlog files.
3572 */
3574 /* The previous GTIDs in the last binlog. */
3576 /// The set of GTIDs that are owned by some thread.
3578 /// The SIDNO for this server.
3580
3581 /// The number of anonymous transactions owned by any client.
3582 std::atomic<int32> atomic_anonymous_gtid_count{0};
3583 /// The number of GTID-violating transactions that use GTID_NEXT=AUTOMATIC.
3585 /// The number of GTID-violating transactions that use GTID_NEXT=AUTOMATIC.
3587 /// The number of clients that are executing
3588 /// WAIT_FOR_EXECUTED_GTID_SET.
3589 std::atomic<int32> atomic_gtid_wait_count{0};
3590 /// The number of sessions that have GTID_NEXT set to AUTOMATIC with tag
3591 /// assigned
3593
3594 /// Used by unit tests that need to access private members.
3595#ifdef FRIEND_OF_GTID_STATE
3596 friend FRIEND_OF_GTID_STATE;
3597#endif
3598
3599 /**
3600 This is a sub task of update_on_rollback responsible only to handle
3601 the case of a thread that needs to skip GTID operations when it has
3602 "failed to commit".
3603
3604 Administrative commands [CHECK|REPAIR|OPTIMIZE|ANALYZE] TABLE
3605 are written to the binary log even when they fail. When the
3606 commands fail, they will call update_on_rollback; later they will
3607 write the binary log. But we must not do any of the things in
3608 update_gtids_impl if we are going to write the binary log. So
3609 these statements set the skip_gtid_rollback flag, which tells
3610 update_on_rollback to return early. When the statements are
3611 written to the binary log they will call update_on_commit as
3612 usual.
3613
3614 @param[in] thd - Thread to be evaluated.
3615
3616 @retval true The transaction should skip the rollback, false otherwise.
3617 */
3619 /**
3620 This is a sub task of update_gtids_impl responsible only to handle
3621 the case of a thread that owns nothing and does not violate GTID
3622 consistency.
3623
3624 If the THD does not own anything, there is nothing to do, so we can do an
3625 early return of the update process. Except if there is a GTID consistency
3626 violation; then we need to decrease the counter, so then we can continue
3627 executing inside update_gtids_impl.
3628
3629 @param[in] thd - Thread to be evaluated.
3630 @retval true The transaction can be skipped because it owns nothing and
3631 does not violate GTID consistency, false otherwise.
3632 */
3634 /**
3635 This is a sub task of update_gtids_impl responsible only to evaluate
3636 if the thread is committing in the middle of a statement by checking
3637 THD's is_commit_in_middle_of_statement flag.
3638
3639 This flag is true for anonymous transactions, when the
3640 'transaction' has been split into multiple transactions in the
3641 binlog, and the present transaction is not the last one.
3642
3643 This means two things:
3644
3645 - We should not release anonymous ownership in case
3646 gtid_next=anonymous. If we did, it would be possible for user
3647 to set GTID_MODE=ON from a concurrent transaction, making it
3648 impossible to commit the current transaction.
3649
3650 - We should not decrease the counters for GTID-violating
3651 statements. If we did, it would be possible for a concurrent
3652 client to set ENFORCE_GTID_CONSISTENCY=ON despite there is an
3653 ongoing transaction that violates GTID consistency.
3654
3655 The flag is set in two cases:
3656
3657 1. We are committing the statement cache when there are more
3658 changes in the transaction cache.
3659
3660 This happens either because a single statement in the
3661 beginning of a transaction updates both transactional and
3662 non-transactional tables, or because we are committing a
3663 non-transactional update in the middle of a transaction when
3664 binlog_direct_non_transactional_updates=1.
3665
3666 In this case, the flag is set further down in this function.
3667
3668 2. The statement is one of the special statements that may
3669 generate multiple transactions: CREATE...SELECT, DROP TABLE,
3670 DROP DATABASE. See comment for THD::owned_gtid in
3671 sql/sql_class.h.
3672
3673 In this case, the THD::is_commit_in_middle_of_statement flag
3674 is set by the caller and the flag becomes true here.
3675
3676 @param[in] thd - Thread to be evaluated.
3677 @return The value of thread's is_commit_in_middle_of_statement flag.
3678 */
3679 bool update_gtids_impl_begin(THD *thd);
3680 /**
3681 Handle the case that the thread own a set of GTIDs.
3682
3683 This is a sub task of update_gtids_impl responsible only to handle
3684 the case of a thread with a set of GTIDs being updated.
3685
3686 - Release ownership of the GTIDs owned by the THD. This removes
3687 the GTID from Owned_gtids and clears the ownership status in the
3688 THD object.
3689 - Add the owned GTIDs to executed_gtids if the is_commit flag is set.
3690 - Send a broadcast on the condition variable for the sidno which we
3691 released ownership.
3692
3693 @param[in] thd - Thread for which owned GTID set should be updated.
3694 @param[in] is_commit - If the thread is being updated by a commit.
3695 */
3696 void update_gtids_impl_own_gtid_set(THD *thd, bool is_commit);
3697 /**
3698 Lock a given sidno of a transaction being updated.
3699
3700 This is a sub task of update_gtids_impl responsible only to lock the
3701 sidno of the GTID being updated.
3702
3703 @param[in] sidno - The sidno to be locked.
3704 */
3706 /**
3707
3708 Locks the sidnos of all the GTIDs of the commit group starting on the
3709 transaction passed as parameter.
3710
3711 This is a sub task of update_commit_group responsible only to lock the
3712 sidno(s) of the GTID(s) being updated.
3713
3714 The function should follow thd->next_to_commit to lock all sidnos of all
3715 transactions being updated in a group.
3716
3717 @param[in] thd - Thread that owns the GTID(s) to be updated or leader
3718 of the commit group in the case of a commit group
3719 update.
3720 */
3722 /**
3723 Handle the case that the thread own a single non-anonymous GTID.
3724
3725 This is a sub task of update_gtids_impl responsible only to handle
3726 the case of a thread with a single non-anonymous GTID being updated
3727 either for commit or rollback.
3728
3729 - Release ownership of the GTID owned by the THD. This removes
3730 the GTID from Owned_gtids and clears the ownership status in the
3731 THD object.
3732 - Add the owned GTID to executed_gtids if the is_commit flag is set.
3733 - Send a broadcast on the condition variable for the sidno which we
3734 released ownership.
3735
3736 @param[in] thd - Thread to be updated that owns single non-anonymous GTID.
3737 @param[in] is_commit - If the thread is being updated by a commit.
3738 */
3739 void update_gtids_impl_own_gtid(THD *thd, bool is_commit);
3740 /**
3741 Unlock a given sidno after broadcasting its changes.
3742
3743 This is a sub task of update_gtids_impl responsible only to
3744 unlock the sidno of the GTID being updated after broadcasting
3745 its changes.
3746
3747 @param[in] sidno - The sidno to be broadcasted and unlocked.
3748 */
3750 /**
3751 Unlocks all locked sidnos after broadcasting their changes.
3752
3753 This is a sub task of update_commit_group responsible only to
3754 unlock the sidno(s) of the GTID(s) being updated after broadcasting
3755 their changes.
3756 */
3758 /**
3759 Handle the case that the thread owns ANONYMOUS GTID.
3760
3761 This is a sub task of update_gtids_impl responsible only to handle
3762 the case of a thread with an ANONYMOUS GTID being updated.
3763
3764 - Release ownership of the anonymous GTID owned by the THD and clears
3765 the ownership status in the THD object.
3766 - Decrease counters of GTID-violating transactions.
3767
3768 @param[in] thd - Thread to be updated that owns anonymous GTID.
3769 @param[in,out] more_trx - If the 'transaction' has been split into
3770 multiple transactions in the binlog.
3771 This is firstly assigned with the return of
3772 Gtid_state::update_gtids_impl_begin function, and
3773 its value can be set to true when
3774 Gtid_state::update_gtids_impl_anonymous_gtid
3775 detects more content on the transaction cache.
3776 */
3777 void update_gtids_impl_own_anonymous(THD *thd, bool *more_trx);
3778 /**
3779 Handle the case that the thread owns nothing.
3780
3781 This is a sub task of update_gtids_impl responsible only to handle
3782 the case of a thread that owns nothing being updated.
3783
3784 There are two cases when this happens:
3785 - Normally, it is a rollback of an automatic transaction, so
3786 the is_commit is false and gtid_next=automatic.
3787 - There is also a corner case. This case may happen for a transaction
3788 that uses GTID_NEXT=AUTOMATIC, and violates GTID_CONSISTENCY, and
3789 commits changes to the database, but does not write to the binary log,
3790 so that no GTID is generated. An example is CREATE TEMPORARY TABLE
3791 inside a transaction when binlog_format=row. Despite the thread does
3792 not own anything, the GTID consistency violation makes it necessary to
3793 call end_gtid_violating_transaction. Therefore
3794 MYSQL_BIN_LOG::gtid_end_transaction will call
3795 gtid_state->update_on_commit in this case, and subsequently we will
3796 reach this case.
3797
3798 @param[in] thd - Thread to be updated that owns anonymous GTID.
3799 */
3801 /**
3802 Handle the final part of update_gtids_impl.
3803
3804 This is a sub task of update_gtids_impl responsible only to handle
3805 the call to end_gtid_violating_transaction function when there is no
3806 more transactions split after the current transaction.
3807
3808 @param[in] thd - Thread for which owned GTID is updated.
3809 @param[in] more_trx - This is the value returned from
3810 Gtid_state::update_gtids_impl_begin and can be
3811 changed for transactions owning anonymous GTID at
3812 Gtid_state::update_gtids_impl_own_anonymous.
3813 */
3814 void update_gtids_impl_end(THD *thd, bool more_trx);
3815 /**
3816 This array is used by Gtid_state_update_gtids_impl* functions.
3817
3818 The array items (one per sidno of the tsid_map) will be set as true for
3819 each sidno that requires to be locked when updating a set of GTIDs
3820 (at Gtid_set::update_gtids_impl_lock_sidnos).
3821
3822 The array items will be set false at
3823 Gtid_set::update_gtids_impl_broadcast_and_unlock_sidnos.
3824
3825 It is used to so that lock, unlock, and broadcast operations are only
3826 called once per sidno per commit group, instead of once per transaction.
3827
3828 Its access is protected by:
3829 - global_tsid_lock->wrlock when growing and cleaning up;
3830 - MYSQL_BIN_LOG::LOCK_commit when setting true/false on array items.
3831 */
3833 /**
3834 Ensure that commit_group_sidnos have room for the SIDNO passed as
3835 parameter.
3836
3837 This function must only be called in one place:
3838 Gtid_state::ensure_sidno().
3839
3840 @param sidno The SIDNO.
3841 @return RETURN_STATUS_OK or RETURN_STATUS_REPORTED_ERROR.
3842 */
3844};
3845
3846/*
3847 BUG# #18089914 - REFACTORING: RENAME GROUP TO GTID
3848 changed AUTOMATIC_GROUP to AUTOMATIC_GTID
3849 changed ANONYMOUS_GROUP to ANONYMOUS_GTID
3850 changed INVALID_GROUP to INVALID_GTID
3851 changed UNDEFINED_GROUP to UNDEFINED_GTID
3852 changed GTID_GROUPto ASSIGNED_GTID
3853 changed NOT_YET_DETERMINED_GROUP to NOT_YET_DETERMINED_GTID
3854*/
3855
3856/**
3857 Enumeration of different types of values for Gtid_specification,
3858 i.e, the different internal states that @@session.gtid_next can be in.
3859*/
3861 /**
3862 Specifies that the GTID has not been generated yet; it will be
3863 generated on commit. It will depend on the GTID_MODE: if
3864 GTID_MODE<=OFF_PERMISSIVE, then the transaction will be anonymous;
3865 if GTID_MODE>=ON_PERMISSIVE, then the transaction will be assigned
3866 a new GTID.
3867
3868 In the latter case, the Gtid_specification may hold a tag. Then,
3869 the new GTID will be generated with that tag.
3870
3871 AUTOMATIC_GTID with an empty tag is the default value:
3872 thd->variables.gtid_next has this state when GTID_NEXT="AUTOMATIC".
3873
3874 It is important that AUTOMATIC_GTID==0 so that the default value
3875 for thd->variables->gtid_next.type is AUTOMATIC_GTID.
3876 */
3878 /**
3879 Specifies that the transaction has been assigned a GTID (UUID:NUMBER).
3880
3881 thd->variables.gtid_next has this state when GTID_NEXT="UUID:NUMBER".
3882
3883 This is the state of GTID-transactions replicated to the slave.
3884 */
3886 /**
3887 Specifies that the transaction is anonymous, i.e., it does not
3888 have a GTID and will never be assigned one.
3889
3890 thd->variables.gtid_next has this state when GTID_NEXT="ANONYMOUS".
3891
3892 This is the state of any transaction generated on a pre-GTID
3893 server, or on a server with GTID_MODE==OFF.
3894 */
3896 /**
3897 GTID_NEXT is set to this state after a transaction with
3898 GTID_NEXT=='UUID:NUMBER' is committed.
3899
3900 This is used to protect against a special case of unsafe
3901 non-transactional updates.
3902
3903 Background: Non-transactional updates are allowed as long as they
3904 are sane. Non-transactional updates must be single-statement
3905 transactions; they must not be mixed with transactional updates in
3906 the same statement or in the same transaction. Since
3907 non-transactional updates must be logged separately from
3908 transactional updates, a single mixed statement would generate two
3909 different transactions.
3910
3911 Problematic case: Consider a transaction, Tx1, that updates two
3912 transactional tables on the master, t1 and t2. Then slave (s1) later
3913 replays Tx1. However, t2 is a non-transactional table at s1. As such, s1
3914 will report an error because it cannot split Tx1 into two different
3915 transactions. Had no error been reported, then Tx1 would be split into Tx1
3916 and Tx2, potentially causing severe harm in case some form of fail-over
3917 procedure is later engaged by s1.
3918
3919 To detect this case on the slave and generate an appropriate error
3920 message rather than causing an inconsistency in the GTID state, we
3921 do as follows. When committing a transaction that has
3922 GTID_NEXT==UUID:NUMBER, we set GTID_NEXT to UNDEFINED_GTID. When
3923 the next part of the transaction is being processed, an error is
3924 generated, because it is not allowed to execute a transaction when
3925 GTID_NEXT==UNDEFINED. In the normal case, the error is not
3926 generated, because there will always be a Gtid_log_event after the
3927 next transaction.
3928 */
3930 /**
3931 GTID_NEXT is set to this state by the slave applier thread when it
3932 reads a Format_description_log_event that does not originate from
3933 this server.
3934
3935 Background: when the slave applier thread reads a relay log that
3936 comes from a pre-GTID master, it must preserve the transactions as
3937 anonymous transactions, even if GTID_MODE>=ON_PERMISSIVE. This
3938 may happen, e.g., if the relay log was received when master and
3939 slave had GTID_MODE=OFF or when master and slave were old, and the
3940 relay log is applied when slave has GTID_MODE>=ON_PERMISSIVE.
3941
3942 So the slave thread should set GTID_NEXT=ANONYMOUS for the next
3943 transaction when it starts to process an old binary log. However,
3944 there is no way for the slave to tell if the binary log is old,
3945 until it sees the first transaction. If the first transaction
3946 begins with a Gtid_log_event, we have the GTID there; if it begins
3947 with query_log_event, row events, etc, then this is an old binary
3948log. So at the time the binary log begins, we just set
3949 GTID_NEXT=NOT_YET_DETERMINED_GTID. If it remains
3950 NOT_YET_DETERMINED when the next transaction begins,
3951 gtid_pre_statement_checks will automatically turn it into an
3952 anonymous transaction. If a Gtid_log_event comes across before
3953 the next transaction starts, then the Gtid_log_event will just set
3954 GTID_NEXT='UUID:NUMBER' accordingly.
3955 */
3957 /**
3958 The applier sets GTID_NEXT this state internally, when it
3959 processes an Anonymous_gtid_log_event on a channel having
3960 ASSIGN_GTIDS_TO_ANONYMOUS_TRANSACTIONS, before it calls
3961 set_gtid_next. This tells set_gtid_next to generate a new,
3962 sequential GTID, and acquire ownership for it. Thus, this state
3963 is only used for a very brief period of time. It is not
3964 user-visible.
3965 */
3967};
3968/// Global state of GTIDs.
3969extern Gtid_state *gtid_state;
3970
3971/**
3972 This struct represents a specification of a GTID for a statement to
3973 be executed: either "AUTOMATIC", "AUTOMATIC:<tag>", "ANONYMOUS" or "TSID:GNO".
3974
3975 This is a POD. It has to be a POD because it is used in THD::variables.
3976*/
3978 // Constants used in gtid specification
3979 static constexpr auto str_automatic = "AUTOMATIC";
3980 static constexpr auto str_automatic_tagged = "AUTOMATIC:";
3981 static constexpr auto str_automatic_sep = ":";
3982 static constexpr auto str_pre_generated = "PRE_GENERATE_GTID";
3983 static constexpr auto str_not_yet_determined = "NOT_YET_DETERMINED";
3984 static constexpr auto str_anonymous = "ANONYMOUS";
3985
3989 /// The type of this GTID
3991 /**
3992 The GTID:
3993 { SIDNO, GNO } if type == GTID;
3994 { 0, 0 } if type == AUTOMATIC or ANONYMOUS.
3995 */
3997
3998 /// @brief Tag defined by the user while specifying GTID_NEXT="AUTOMATIC:TAG".
3999 /// We must store here the information about tag, because automatic
4000 /// tagged GTID does not have sidno assigned
4002
4003 /// @brief Prints automatic tag specification to the given buffer
4004 /// @param[in,out] buf Buffer to write to, must be allocated
4005 /// @return The number of bytes written to the buffer
4006 std::size_t automatic_to_string(char *buf) const;
4007
4008 /// Set the type to ASSIGNED_GTID and SIDNO, GNO to the given values.
4009 void set(rpl_sidno sidno, rpl_gno gno) {
4010 gtid.set(sidno, gno);
4013 }
4014
4015 /// @brief Helper function indicating whether this is to-be-generated GTID
4016 /// @param[in] type Type of the GTID
4017 /// @retval true This GTID will be generated
4018 /// @retval false Other type of the GTID
4019 static bool is_automatic(const enum_gtid_type &type) {
4020 return type == AUTOMATIC_GTID;
4021 }
4022 /// @brief Helper function indicating whether this is to-be-generated GTID
4023 /// @retval true This GTID will be generated
4024 /// @retval false Other type of the GTID
4025 bool is_automatic() const { return is_automatic(type); }
4026
4027 /// @brief Helper function indicating whether this is an undefined GTID
4028 /// @return Returns true for undefined GTIDs
4029 bool is_undefined() const { return type == UNDEFINED_GTID; }
4030
4031 /// @brief Helper function indicating whether this is an assigned GTID
4032 /// @return Returns true for assigned GTIDs
4033 bool is_assigned() const { return type == ASSIGNED_GTID; }
4034
4035 /// @brief Returns tag object generated from internal tag data
4036 /// @return Tag object
4037 Tag generate_tag() const;
4038
4039 /// @brief Helper function indicating whether this is to-be-generated GTID
4040 /// with a tag assigned
4041 /// @retval true This GTID will be generated with assigned tag
4042 /// @retval false Other type of the GTID
4043 bool is_automatic_tagged() const;
4044
4045 /// Set the type to ASSIGNED_GTID and TSID, GNO to the given Gtid.
4046 /// @brief gtid_param GTID to copy from
4047 void set(const Gtid &gtid_param) { set(gtid_param.sidno, gtid_param.gno); }
4048 /// @brief Set the type to AUTOMATIC_GTID.
4052 }
4053 /// @brief Copy spec from other
4054 /// @param[in] other Pattern to copy from
4055 void set(const Gtid_specification &other);
4056
4057 /// Set the type to ANONYMOUS_GTID.
4061 }
4062 /// Set the type to NOT_YET_DETERMINED_GTID.
4066 }
4067 /// Set to undefined. Must only be called if the type is ASSIGNED_GTID.
4069 assert(type == ASSIGNED_GTID);
4072 }
4073 /// Return true if this Gtid_specification is equal to 'other'.
4074 bool equals(const Gtid_specification &other) const {
4075 return (type == other.type &&
4076 (type != ASSIGNED_GTID || gtid.equals(other.gtid)));
4077 }
4078 /**
4079 Return true if this Gtid_specification is a ASSIGNED_GTID with the
4080 same TSID, GNO as 'other_gtid'.
4081 */
4082 bool equals(const Gtid &other_gtid) const {
4083 return type == ASSIGNED_GTID && gtid.equals(other_gtid);
4084 }
4085#ifdef MYSQL_SERVER
4086 /**
4087 Parses the given string and stores in this Gtid_specification.
4088
4089 @param tsid_map tsid_map to use when converting TSID to a sidno.
4090 @param text The text to parse
4091 @return operation status
4092 */
4094 const char *text);
4095
4096 /// @brief Returns true if the given string is a valid Gtid_specification.
4097 /// @param[in] text Textual representation of the GTID specification
4098 static bool is_valid(const char *text);
4099
4100 /// @brief Returns true if the given string is a tagged Gtid_specification.
4101 /// @param[in] text Textual representation of the GTID specification
4102 static bool is_tagged(const char *text);
4103#endif
4105 /**
4106 Writes this Gtid_specification to the given string buffer.
4107
4108 @param tsid_map Tsid_map to use if the type of this
4109 Gtid_specification is ASSIGNED_GTID.
4110 @param [out] buf The buffer
4111 @param need_lock If true, this function acquires global_tsid_lock
4112 before looking up the sidno in tsid_map, and then releases it. If
4113 false, this function asserts that the lock is held by the caller.
4114 @retval The number of characters written.
4115 */
4116 int to_string(const Tsid_map *tsid_map, char *buf,
4117 bool need_lock = false) const;
4118 /**
4119 Writes this Gtid_specification to the given string buffer.
4120
4121 @param tsid TSID to use if the type of this Gtid_specification is
4122 ASSIGNED_GTID. Can be NULL if this Gtid_specification is
4123 ANONYMOUS_GTID or AUTOMATIC_GTID.
4124 @param[out] buf The buffer
4125 @retval The number of characters written.
4126 */
4127 int to_string(const Tsid &tsid, char *buf) const;
4128
4129#ifndef NDEBUG
4130 /// Debug only: print this Gtid_specification to stdout.
4131 void print() const {
4132 char buf[MAX_TEXT_LENGTH + 1];
4134 printf("%s\n", buf);
4135 }
4136#endif
4137 /**
4138 Print this Gtid_specification to the trace file if debug is
4139 enabled; no-op otherwise.
4140 */
4141 void dbug_print(const char *text [[maybe_unused]] = "",
4142 bool need_lock [[maybe_unused]] = false) const {
4143#ifndef NDEBUG
4144 char buf[MAX_TEXT_LENGTH + 1];
4145 to_string(global_tsid_map, buf, need_lock);
4146 DBUG_PRINT("info", ("%s%s%s", text, *text ? ": " : "", buf));
4147#endif
4148 }
4149};
4150
4151static_assert(std::is_trivial_v<Gtid_specification>);
4152static_assert(std::is_standard_layout_v<Gtid_specification>);
4153
4154/**
4155 Indicates if a statement should be skipped or not. Used as return
4156 value from gtid_before_statement.
4157*/
4159 /// Statement can execute.
4161 /// Statement should be cancelled.
4163 /**
4164 Statement should be skipped, but there may be an implicit commit
4165 after the statement if gtid_commit is set.
4166 */
4169
4170#ifdef MYSQL_SERVER
4171
4172/**
4173 Check if current transaction should be skipped, that is, if GTID_NEXT
4174 was already logged.
4175
4176 @param thd The calling thread.
4177
4178 @retval true Transaction was already logged.
4179 @retval false Transaction must be executed.
4180*/
4181bool is_already_logged_transaction(const THD *thd);
4182
4183/**
4184 Perform GTID-related checks before executing a statement:
4185
4186 - Check that the current statement does not contradict
4187 enforce_gtid_consistency.
4188
4189 - Check that there is no implicit commit in a transaction when
4190 GTID_NEXT==UUID:NUMBER.
4191
4192 - Change thd->variables.gtid_next.type to ANONYMOUS_GTID if it is
4193 currently NOT_YET_DETERMINED_GTID.
4194
4195 - Check whether the statement should be cancelled.
4196
4197 @param thd THD object for the session.
4198
4199 @retval GTID_STATEMENT_EXECUTE The normal case: the checks
4200 succeeded, and statement can execute.
4201
4202 @retval GTID_STATEMENT_CANCEL The checks failed; an
4203 error has be generated and the statement must stop.
4204
4205 @retval GTID_STATEMENT_SKIP The checks succeeded, but the GTID has
4206 already been executed (exists in GTID_EXECUTED). So the statement
4207 must not execute; however, if there are implicit commits, then the
4208 implicit commits must execute.
4209*/
4211
4212/**
4213 Perform GTID-related checks before executing a statement, but after
4214 executing an implicit commit before the statement, if any:
4215
4216 If gtid_next=anonymous, but the thread does not hold anonymous
4217 ownership, then acquire anonymous ownership. (Do this only if this
4218 is not an 'innocent' statement, i.e., SET/SHOW/DO/SELECT that does
4219 not invoke a stored function.)
4220
4221 It is important that this is done after the implicit commit, because
4222 the implicit commit may release anonymous ownership.
4223
4224 @param thd THD object for the session
4225
4226 @retval false Success.
4227
4228 @retval true Error. Error can happen if GTID_MODE=ON. The error has
4229 been reported by (a function called by) this function.
4230*/
4232
4233/**
4234 Acquire ownership of the given Gtid_specification.
4235
4236 The Gtid_specification must be of type ASSIGNED_GTID or ANONYMOUS_GTID.
4237
4238 The caller must hold global_tsid_lock (normally the rdlock). The
4239 lock may be temporarily released and acquired again. In the end,
4240 the lock will be released, so the caller should *not* release the
4241 lock.
4242
4243 The function will try to acquire ownership of the GTID and update
4244 both THD::gtid_next, Gtid_state::owned_gtids, and
4245 THD::owned_gtid / THD::owned_sid.
4246
4247 @param thd The thread that acquires ownership.
4248
4249 @param spec The Gtid_specification.
4250
4251 @retval false Success: either we have acquired ownership of the
4252 GTID, or it is already included in GTID_EXECUTED and will be
4253 skipped.
4254
4255 @retval true Failure; the thread was killed or an error occurred.
4256 The error has been reported using my_error.
4257*/
4258bool set_gtid_next(THD *thd, const Gtid_specification &spec);
4259#ifdef HAVE_GTID_NEXT_LIST
4260int gtid_acquire_ownership_multiple(THD *thd);
4261#endif
4262
4263/**
4264 Return sidno for a given tsid, see Tsid_map::add_sid() for details.
4265*/
4267
4268/**
4269 Return Tsid for a given sidno on the global_tsid_map.
4270 See Tsid_map::sidno_to_tsid() for details.
4271*/
4273
4274/**
4275 Return last gno for a given sidno, see
4276 Gtid_state::get_last_executed_gno() for details.
4277*/
4279
4281
4282/**
4283 If gtid_next=ANONYMOUS or NOT_YET_DETERMINED, but the thread does
4284 not hold anonymous ownership, acquire anonymous ownership.
4285
4286 @param thd Thread.
4287
4288 @retval true Error (can happen if gtid_mode=ON and
4289 gtid_next=anonymous). The error has already been reported using
4290 my_error.
4291
4292 @retval false Success.
4293*/
4295
4296/**
4297 The function commits or rolls back the gtid state if it needs to.
4298 It's supposed to be invoked at the end of transaction commit or
4299 rollback, as well as as at the end of XA prepare.
4300
4301 @param thd Thread context
4302 @param needs_to The actual work will be done when the parameter is true
4303 @param do_commit When true the gtid state changes are committed, otherwise
4304 they are rolled back.
4305*/
4306
4307inline void gtid_state_commit_or_rollback(THD *thd, bool needs_to,
4308 bool do_commit) {
4309 if (needs_to) {
4310 if (do_commit)
4312 else
4314 }
4315}
4316
4317#endif // ifdef MYSQL_SERVER
4318
4319#endif /* RPL_GTID_H_INCLUDED */
Kerberos Client Authentication nullptr
Definition: auth_kerberos_client_plugin.cc:251
RAII class to acquire a lock for the duration of a block.
Definition: rpl_gtid.h:358
void unlock_if_locked()
Unlock the lock, if it was acquired by this guard.
Definition: rpl_gtid.h:466
Guard(Checkable_rwlock &lock, enum_lock_type lock_type)
Create a guard, and optionally acquire a lock on it.
Definition: rpl_gtid.h:366
Checkable_rwlock & m_lock
Definition: rpl_gtid.h:359
Guard(Checkable_rwlock &lock, enum_lock_type lock_type, std::adopt_lock_t t)
Create a guard, assuming the caller already holds a lock on it.
Definition: rpl_gtid.h:390
bool is_locked() const
Return true if this object is either read locked or write locked.
Definition: rpl_gtid.h:481
bool is_wrlocked() const
Return true if this object is write locked.
Definition: rpl_gtid.h:478
int tryrdlock()
Try to acquire a read lock, and fail if it cannot be immediately granted.
Definition: rpl_gtid.h:450
enum_lock_type m_lock_type
Definition: rpl_gtid.h:360
Guard(Guard const &copy)=delete
Objects of this class should not be copied or moved.
void unlock()
Unlock the lock.
Definition: rpl_gtid.h:459
void rdlock()
Acquire the read lock.
Definition: rpl_gtid.h:419
Guard(Guard const &&copy)=delete
bool is_rdlocked() const
Return true if this object is read locked.
Definition: rpl_gtid.h:475
~Guard()
Unlock on destruct.
Definition: rpl_gtid.h:413
Checkable_rwlock & get_lock() const
Return the underlying Checkable_rwlock object.
Definition: rpl_gtid.h:472
int trywrlock()
Try to acquire the write lock, and fail if it cannot be immediately granted.
Definition: rpl_gtid.h:438
void wrlock()
Acquire the write lock.
Definition: rpl_gtid.h:427
This has the functionality of mysql_rwlock_t, with two differences:
Definition: rpl_gtid.h:324
int32 get_state() const
Read lock_state atomically and return the value.
Definition: rpl_gtid.h:597
void assert_no_rdlock() const
Assert that no thread holds the read lock.
Definition: rpl_gtid.h:579
int trywrlock()
Return 0 if the write lock is held, otherwise an error will be returned.
Definition: rpl_gtid.h:537
enum_lock_type
Definition: rpl_gtid.h:347
@ TRY_READ_LOCK
Definition: rpl_gtid.h:351
@ TRY_WRITE_LOCK
Definition: rpl_gtid.h:352
@ WRITE_LOCK
Definition: rpl_gtid.h:350
@ READ_LOCK
Definition: rpl_gtid.h:349
@ NO_LOCK
Definition: rpl_gtid.h:348
void rdlock()
Acquire the read lock.
Definition: rpl_gtid.h:485
void wrlock()
Acquire the write lock.
Definition: rpl_gtid.h:494
std::atomic< int32 > m_lock_state
The state of the lock: 0 - not locked -1 - write locked >0 - read locked by that many threads.
Definition: rpl_gtid.h:595
void assert_no_lock() const
Assert that no thread holds read or write lock.
Definition: rpl_gtid.h:581
int tryrdlock()
Return 0 if the read lock is held, otherwise an error will be returned.
Definition: rpl_gtid.h:556
~Checkable_rwlock()
Destroy this Checkable_lock.
Definition: rpl_gtid.h:345
void assert_no_wrlock() const
Assert that no thread holds the write lock.
Definition: rpl_gtid.h:577
void assert_some_lock() const
Assert that some thread holds either the read or the write lock.
Definition: rpl_gtid.h:571
Checkable_rwlock(PSI_rwlock_key psi_key=0)
Initialize this Checkable_rwlock.
Definition: rpl_gtid.h:327
void assert_some_rdlock() const
Assert that some thread holds the read lock.
Definition: rpl_gtid.h:573
void unlock()
Release the lock (whether it is a write or read lock).
Definition: rpl_gtid.h:505
bool m_dbug_trace
If enabled, print any lock/unlock operations to the DBUG trace.
Definition: rpl_gtid.h:586
bool is_wrlock()
Return true if the write lock is held.
Definition: rpl_gtid.h:525
void assert_some_wrlock() const
Assert that some thread holds the write lock.
Definition: rpl_gtid.h:575
mysql_rwlock_t m_rwlock
The rwlock.
Definition: rpl_gtid.h:606
Class to access the value of @global.gtid_mode in an efficient and thread-safe manner.
Definition: rpl_gtid.h:616
static std::pair< bool, value_type > from_string(std::string s)
Return the given string gtid_mode as an enumeration value.
Definition: rpl_gtid_mode.cc:56
static ulong sysvar_mode
The sys_var framework needs a variable of type ulong to store the value in.
Definition: rpl_gtid.h:629
std::atomic< int > m_atomic_mode
Definition: rpl_gtid.h:618
const char * get_string() const
Return the current gtid_mode as a string.
Definition: rpl_gtid_mode.cc:53
Gtid_mode()
Definition: rpl_gtid.h:621
static const char * names[]
Strings holding the enumeration values for gtid_mode.
Definition: rpl_gtid.h:661
void set(value_type value)
Set a new value for @global.gtid_mode.
Definition: rpl_gtid_mode.cc:44
value_type get() const
Return the current gtid_mode as an enumeration value.
Definition: rpl_gtid_mode.cc:48
static const char * to_string(value_type value)
Return the given gtid_mode as a string.
Definition: rpl_gtid_mode.cc:68
static Checkable_rwlock lock
Protects updates to @global.gtid_mode.
Definition: rpl_gtid.h:672
value_type
Possible values for @global.gtid_mode.
Definition: rpl_gtid.h:632
@ ON_PERMISSIVE
New transactions are GTID-transactions.
Definition: rpl_gtid.h:648
@ OFF
New transactions are anonymous.
Definition: rpl_gtid.h:637
@ OFF_PERMISSIVE
New transactions are anonyomus.
Definition: rpl_gtid.h:643
@ ON
New transactions are GTID-transactions.
Definition: rpl_gtid.h:654
@ DEFAULT
Definition: rpl_gtid.h:638
Stores information to monitor a transaction during the different replication stages.
Definition: rpl_gtid.h:1412
Trx_monitoring_info * last_processed_trx
Holds information about the last processed transaction.
Definition: rpl_gtid.h:1432
const Gtid * get_processing_trx_gtid()
Returns the GTID of the processing_trx.
Definition: rpl_gtid_misc.cc:602
void update(mysql::binlog::event::compression::type t, size_t payload_size, size_t uncompressed_size)
Definition: rpl_gtid_misc.cc:515
void clear()
Clear all monitoring information.
Definition: rpl_gtid_misc.cc:496
void finish()
Sets the final information, copy processing info to last_processed and clears processing info.
Definition: rpl_gtid_misc.cc:563
void start(Gtid gtid_arg, ulonglong original_ts_arg, ulonglong immediate_ts_arg, bool skipped_arg=false)
Sets the initial monitoring information.
Definition: rpl_gtid_misc.cc:523
~Gtid_monitoring_info()
Destroy this GTID monitoring info object.
Definition: rpl_gtid_misc.cc:455
Trx_monitoring_info * processing_trx
Holds information about transaction being processed.
Definition: rpl_gtid.h:1430
void atomic_lock()
Lock this object when no thread mutex is used to arbitrate the access.
Definition: rpl_gtid_misc.cc:460
void atomic_unlock()
Unlock this object when no thread mutex is used to arbitrate the access.
Definition: rpl_gtid_misc.cc:485
mysql_mutex_t * atomic_mutex
Mutex arbitrating the atomic access to the object.
Definition: rpl_gtid.h:1449
void copy_info_to(Trx_monitoring_info *processing_dest, Trx_monitoring_info *last_processed_dest)
Copies both processing_trx and last_processed_trx info to other Trx_monitoring_info structures.
Definition: rpl_gtid_misc.cc:580
std::atomic< bool > atomic_locked
The atomic locked flag.
Definition: rpl_gtid.h:1452
bool is_processing_trx_set()
Returns true if the processing_trx is set, false otherwise.
Definition: rpl_gtid_misc.cc:593
bool is_locked
Flag to assert the atomic lock behavior.
Definition: rpl_gtid.h:1455
void store_transient_error(uint transient_errno_arg, const char *transient_err_message_arg, ulong trans_retries_arg)
Stores the information about the last transient error in the current transaction, namely: the error n...
Definition: rpl_gtid_misc.cc:611
Gtid_monitoring_info(mysql_mutex_t *atomic_mutex_arg=nullptr)
Create this GTID monitoring info object.
Definition: rpl_gtid_misc.cc:449
void clear_last_processed_trx()
Clear only the last_processed_trx monitoring info.
Definition: rpl_gtid_misc.cc:509
void clear_processing_trx()
Clear only the processing_trx monitoring info.
Definition: rpl_gtid_misc.cc:503
Iterator over intervals of a const Gtid_set.
Definition: rpl_gtid.h:2100
Const_interval_iterator(const Gtid_set *gtid_set)
Create this Const_interval_iterator.
Definition: rpl_gtid.h:2107
Const_interval_iterator(const Gtid_set *gtid_set, rpl_sidno sidno)
Create this Const_interval_iterator.
Definition: rpl_gtid.h:2103
Class representing a lock on free_intervals_mutex.
Definition: rpl_gtid.h:2350
bool locked
Definition: rpl_gtid.h:2374
void unlock_if_locked()
Lock the lock if it is locked.
Definition: rpl_gtid.h:2363
Free_intervals_lock(Gtid_set *_gtid_set)
Create a new lock, but do not acquire it.
Definition: rpl_gtid.h:2353
~Free_intervals_lock()
Destroy this object and unlock the lock if it is locked.
Definition: rpl_gtid.h:2370
void lock_if_not_locked()
Lock the lock if it is not already locked.
Definition: rpl_gtid.h:2356
Gtid_set * gtid_set
Definition: rpl_gtid.h:2373
Iterator over all gtids in a Gtid_set.
Definition: rpl_gtid.h:2158
Gtid get() const
Return next gtid, or {0,0} if we reached the end.
Definition: rpl_gtid.h:2184
rpl_sidno sidno
The SIDNO of the current element, or 0 if the iterator is past the last element.
Definition: rpl_gtid.h:2211
const Gtid_set * gtid_set
The Gtid_set we iterate over.
Definition: rpl_gtid.h:2206
void next()
Advance to next gtid.
Definition: rpl_gtid.h:2165
rpl_gno gno
The GNO of the current element, or 0 if the iterator is past the last element.
Definition: rpl_gtid.h:2216
Const_interval_iterator ivit
Iterator over the intervals for the current SIDNO.
Definition: rpl_gtid.h:2218
Gtid_iterator(const Gtid_set *gs)
Definition: rpl_gtid.h:2160
void next_sidno()
Find the next sidno that has one or more intervals.
Definition: rpl_gtid.h:2191
Iterator over intervals for a given SIDNO.
Definition: rpl_gtid.h:2059
Interval_p get() const
Return current_elem.
Definition: rpl_gtid.h:2085
void init(Gtid_set_p gtid_set, rpl_sidno sidno)
Reset this iterator.
Definition: rpl_gtid.h:2076
Interval_iterator_base(Gtid_set_p gtid_set, rpl_sidno sidno)
Construct a new iterator over the GNO intervals for a given Gtid_set.
Definition: rpl_gtid.h:2067
Interval_p * p
Holds the address of the 'next' pointer of the previous element, or the address of the initial pointe...
Definition: rpl_gtid.h:2093
Interval_iterator_base(Gtid_set_p gtid_set)
Construct a new iterator over the free intervals of a Gtid_set.
Definition: rpl_gtid.h:2072
void next()
Advance current_elem one step.
Definition: rpl_gtid.h:2080
Iterator over intervals of a non-const Gtid_set, with additional methods to modify the Gtid_set.
Definition: rpl_gtid.h:2117
Interval_iterator(Gtid_set *gtid_set)
Destroy this Interval_iterator.
Definition: rpl_gtid.h:2123
void set(Interval *iv)
Set current_elem to the given Interval but do not touch the next pointer of the given Interval.
Definition: rpl_gtid.h:2131
void insert(Interval *iv)
Insert the given element before current_elem.
Definition: rpl_gtid.h:2133
void remove(Gtid_set *gtid_set)
Remove current_elem.
Definition: rpl_gtid.h:2138
Interval_iterator(Gtid_set *gtid_set, rpl_sidno sidno)
Create this Interval_iterator.
Definition: rpl_gtid.h:2120
Represents a set of GTIDs.
Definition: rpl_gtid.h:1556
void put_free_interval(Interval *iv)
Puts the given interval in the list of free intervals.
Definition: rpl_gtid_set.cc:257
rpl_gno get_last_gno(rpl_sidno sidno) const
Definition: rpl_gtid_set.cc:759
bool is_size_greater_than_or_equal(ulonglong num) const
Return true if the size of the set is greater than or equal to the given number.
Definition: rpl_gtid_set.cc:1326
bool is_intersection_nonempty(const Gtid_set *other) const
Returns true if there is a least one element of this Gtid_set in the other Gtid_set.
Definition: rpl_gtid_set.cc:1246
bool is_subset(const Gtid_set *super) const
Returns true if this Gtid_set is a subset of the other Gtid_set.
Definition: rpl_gtid_set.cc:1166
void add_interval_memory(int n_intervals, Interval *intervals_param)
Provides an array of Intervals that this Gtid_set can use when gtids are subsequently added.
Definition: rpl_gtid.h:2042
bool contains_sidno(rpl_sidno sidno) const
Returns true if this Gtid_set contains at least one GTID with the given SIDNO.
Definition: rpl_gtid.h:1865
void encode(uchar *buf, bool skip_tagged_gtids=false) const
Encodes this Gtid_set as a binary string.
Definition: rpl_gtid_set.cc:1377
Gtid_set(Tsid_map *tsid_map, Checkable_rwlock *tsid_lock=nullptr)
Constructs a new, empty Gtid_set.
Definition: rpl_gtid_set.cc:87
int get_n_intervals() const
Return the number of intervals in this Gtid_set.
Definition: rpl_gtid.h:2283
bool sidno_equals(rpl_sidno sidno, const Gtid_set *other, rpl_sidno other_sidno) const
Return true if the given sidno of this Gtid_set contains the same intervals as the given sidno of the...
Definition: rpl_gtid_set.cc:989
friend std::ostream & operator<<(std::ostream &os, const Gtid_set &in)
For use with C++ std::ostream
Definition: rpl_gtid.h:1966
void add_gno_intervals(rpl_sidno sidno, Const_interval_iterator ivit, Free_intervals_lock *lock)
Adds a list of intervals to the given SIDNO.
Definition: rpl_gtid_set.cc:649
static bool is_interval_subset(Const_interval_iterator *sub, Const_interval_iterator *super)
Returns true if every interval of sub is a subset of some interval of super.
Definition: rpl_gtid_set.cc:1062
void init()
Worker for the constructor.
Definition: rpl_gtid_set.cc:104
static const int CHUNK_GROW_SIZE
The default number of intervals in an Interval_chunk.
Definition: rpl_gtid.h:2258
Interval_chunk * chunks
Linked list of chunks.
Definition: rpl_gtid.h:2476
static const String_format commented_string_format
String_format for printing the Gtid_set commented: the string is not quote-wrapped,...
Definition: rpl_gtid.h:2010
bool contains_gtid(rpl_sidno sidno, rpl_gno gno) const
Return true iff the given GTID exists in this set.
Definition: rpl_gtid_set.cc:741
enum_return_status ensure_sidno(rpl_sidno sidno)
Allocates space for all sidnos up to the given sidno in the array of intervals.
Definition: rpl_gtid_set.cc:134
Tsid_map * tsid_map
Tsid_map associated with this Gtid_set.
Definition: rpl_gtid.h:2467
int get_n_intervals(rpl_sidno sidno) const
Return the number of intervals for the given sidno.
Definition: rpl_gtid.h:2273
bool contains_tags() const
Checks if this Gtid set contains any tagged GTIDs.
Definition: rpl_gtid_set.cc:914
void clear_set_and_tsid_map()
Removes all gtids from this Gtid_set and clear all the sidnos used by the Gtid_set and it's TSID map.
Definition: rpl_gtid_set.cc:290
enum_return_status add_gtid_set(const Gtid_set *other)
Adds all gtids from the given Gtid_set to this Gtid_set.
Definition: rpl_gtid_set.cc:684
Interval * free_intervals
Linked list of free intervals.
Definition: rpl_gtid.h:2474
bool contains_gtid(const Gtid &gtid) const
Return true iff the given GTID exists in this set.
Definition: rpl_gtid.h:1751
static const String_format default_string_format
The default String_format: the format understood by add_gtid_text(const char *).
Definition: rpl_gtid.h:2000
void add_interval_memory_lock_taken(int n_ivs, Interval *ivs)
Like add_interval_memory, but does not acquire free_intervals_mutex.
Definition: rpl_gtid_set.cc:182
void remove_gtid_set(const Gtid_set *other)
Removes all gtids in the given Gtid_set from this Gtid_set.
Definition: rpl_gtid_set.cc:715
void print(bool need_lock=false, const Gtid_set::String_format *sf=nullptr) const
Debug only: Print this Gtid_set to stdout.
Definition: rpl_gtid.h:1957
Prealloced_array< Interval *, 8 > m_intervals
Array where the N'th element contains the head pointer to the intervals of SIDNO N+1.
Definition: rpl_gtid.h:2472
mysql_mutex_t free_intervals_mutex
Lock protecting the list of free intervals.
Definition: rpl_gtid.h:2336
void dbug_print(const char *text="", bool need_lock=false, const Gtid_set::String_format *sf=nullptr) const
Print this Gtid_set to the trace file if debug is enabled; no-op otherwise.
Definition: rpl_gtid.h:1978
int n_chunks
The number of chunks.
Definition: rpl_gtid.h:2488
void remove_gno_intervals(rpl_sidno sidno, Const_interval_iterator ivit, Free_intervals_lock *lock)
Removes a list of intervals from the given SIDNO.
Definition: rpl_gtid_set.cc:662
static bool is_valid(const char *text)
Returns true if the given string is a valid specification of a Gtid_set, false otherwise.
Definition: rpl_gtid_set.cc:596
void clear()
Removes all gtids from this Gtid_set.
Definition: rpl_gtid_set.cc:265
static bool is_interval_intersection_nonempty(Const_interval_iterator *ivit1, Const_interval_iterator *ivit2)
Returns true if at least one sidno in ivit1 is also in ivit2.
Definition: rpl_gtid_set.cc:1209
bool is_subset_for_sid(const Gtid_set *super, const rpl_sid &sid) const
Returns true if this Gtid_set is a subset of the given gtid_set with respect to the given sid.
Definition: rpl_gtid_set.cc:1103
enum_return_status intersection(const Gtid_set *other, Gtid_set *result)
Add the intersection of this Gtid_set and the other Gtid_set to result.
Definition: rpl_gtid_set.cc:1300
void remove_gno_interval(Interval_iterator *ivitp, rpl_gno start, rpl_gno end, Free_intervals_lock *lock)
Removes the interval (start, end) from the given Interval_iterator.
Definition: rpl_gtid_set.cc:352
void get_gtid_intervals(std::list< Gtid_interval > *gtid_intervals) const
Gets all gtid intervals from this Gtid_set.
Definition: rpl_gtid_set.cc:873
bool is_subset_not_equals(const Gtid_set *super) const
Returns true if this Gtid_set is a non equal subset of the other Gtid_set.
Definition: rpl_gtid.h:1778
enum_return_status add_gtid(const mysql::gtid::Gtid &gtid)
Adds specified GTID (TSID+GNO) to this Gtid_set.
Definition: rpl_gtid_set.cc:426
size_t get_string_length(const String_format *string_format=nullptr) const
Returns the length of the output from to_string.
Definition: rpl_gtid_set.cc:931
size_t cached_string_length
The string length.
Definition: rpl_gtid.h:2480
enum_return_status add_gtid_text(const char *text, bool *anonymous=nullptr, bool *starts_with_plus=nullptr)
Adds the set of GTIDs represented by the given string to this Gtid_set.
Definition: rpl_gtid_set.cc:440
void add_gno_interval(Interval_iterator *ivitp, rpl_gno start, rpl_gno end, Free_intervals_lock *lock)
Adds the interval (start, end) to the given Interval_iterator.
Definition: rpl_gtid_set.cc:303
size_t get_encoded_length(bool skip_tagged_gtids=false) const
Returns the length of this Gtid_set when encoded using the encode() function.
Definition: rpl_gtid_set.cc:1546
void _add_gtid(rpl_sidno sidno, rpl_gno gno)
Adds the given GTID to this Gtid_set.
Definition: rpl_gtid.h:1618
bool is_empty() const
Returns true if this Gtid_set is empty.
Definition: rpl_gtid.h:1822
static PSI_mutex_key key_gtid_executed_free_intervals_mutex
Definition: rpl_gtid.h:1558
static const String_format sql_string_format
String_format useful to generate an SQL string: the string is wrapped in single quotes and there is a...
Definition: rpl_gtid.h:2005
bool equals(const Gtid_set *other) const
Returns true if this Gtid_set is equal to the other Gtid_set.
Definition: rpl_gtid_set.cc:1007
enum_return_status add_gtid_encoding(const uchar *encoded, size_t length, size_t *actual_length=nullptr)
Decodes a Gtid_set from the given string.
Definition: rpl_gtid_set.cc:1428
void remove_intervals_for_sidno(Gtid_set *other, rpl_sidno sidno)
Removes all intervals of 'other' for a given SIDNO, from 'this'.
Definition: rpl_gtid_set.cc:676
bool is_subset_for_sidno(const Gtid_set *super, rpl_sidno superset_sidno, rpl_sidno subset_sidno) const
Returns true if this Gtid_set is a subset of the given gtid_set on the given superset_sidno and subse...
Definition: rpl_gtid_set.cc:1128
~Gtid_set()
Destroy this Gtid_set.
Definition: rpl_gtid_set.cc:119
bool has_cached_string_length
If the string is cached.
Definition: rpl_gtid.h:2478
const String_format * cached_string_format
The String_format that was used when cached_string_length was computed.
Definition: rpl_gtid.h:2482
void get_free_interval(Interval **out)
Returns a fresh new Interval object.
Definition: rpl_gtid_set.cc:244
size_t to_string(char *buf, bool need_lock=false, const String_format *string_format=nullptr) const
Formats this Gtid_set as a string and saves in a given buffer.
Definition: rpl_gtid_set.cc:796
void _remove_gtid(const Gtid &gtid)
Removes the given GTID from this Gtid_set.
Definition: rpl_gtid.h:1656
Checkable_rwlock * tsid_lock
Read-write lock that protects updates to the number of TSIDs.
Definition: rpl_gtid.h:2331
mysql::gtid::Gtid_format analyze_encoding_format(bool skip_tagged_gtids) const
Goes through recorded tsids.
Definition: rpl_gtid_set.cc:1510
void _remove_gtid(rpl_sidno sidno, rpl_gno gno)
Removes the given GTID from this Gtid_set.
Definition: rpl_gtid.h:1634
Tsid_map * get_tsid_map() const
Return the Tsid_map associated with this Gtid_set.
Definition: rpl_gtid.h:2013
void create_new_chunk(int size)
Allocates a new chunk of Intervals and adds them to the list of unused intervals.
Definition: rpl_gtid_set.cc:193
void _add_gtid(const Gtid &gtid)
Adds the given GTID to this Gtid_set.
Definition: rpl_gtid.h:1650
rpl_sidno get_max_sidno() const
Returns the maximal sidno that this Gtid_set currently has space for.
Definition: rpl_gtid.h:1757
void assert_free_intervals_locked()
Definition: rpl_gtid.h:2376
ulonglong get_gtid_count(rpl_sidno sidno) const
What is the count of all the GTIDs in all intervals for a sidno.
Definition: rpl_gtid.h:1845
Represents the server's GTID state: the set of committed GTIDs, the set of lost gtids,...
Definition: rpl_gtid.h:2853
bool update_gtids_impl_check_skip_gtid_rollback(THD *thd)
Used by unit tests that need to access private members.
Definition: rpl_gtid_state.cc:760
int init()
Add @GLOBAL.SERVER_UUID to this binlog's Tsid_map.
Definition: rpl_gtid_state.cc:651
Tsid_map * tsid_map
The Tsid_map used by this Gtid_state.
Definition: rpl_gtid.h:3556
bool update_gtids_impl_do_nothing(THD *thd)
This is a sub task of update_gtids_impl responsible only to handle the case of a thread that owns not...
Definition: rpl_gtid_state.cc:769
void end_automatic_gtid_violating_transaction()
Decrease the global counter when ending a GTID-violating transaction having GTID_NEXT=AUTOMATIC.
Definition: rpl_gtid.h:3042
void broadcast_owned_sidnos(const THD *thd)
Broadcast the condition for all SIDNOs owned by the given THD.
Definition: rpl_gtid_state.cc:146
const Gtid_set * get_executed_gtids() const
Definition: rpl_gtid.h:3356
void unlock_sidnos(const Gtid_set *set)
Unlocks the mutex for each SIDNO where the given Gtid_set has at least one GTID.
Definition: rpl_gtid_state.cc:547
void print() const
Debug only: print this Gtid_state to stdout.
Definition: rpl_gtid.h:3431
const Gtid_set * get_lost_gtids() const
Return a pointer to the Gtid_set that contains the lost gtids.
Definition: rpl_gtid.h:3351
enum_return_status ensure_sidno()
Ensure that owned_gtids, executed_gtids, lost_gtids, gtids_only_in_table, previous_gtids_logged and t...
Definition: rpl_gtid_state.cc:561
void decrease_gtid_automatic_tagged_count()
Decrements atomic_automatic_tagged_gtid_session_count.
Definition: rpl_gtid.h:3386
void begin_anonymous_gtid_violating_transaction()
Increase the global counter when starting a GTID-violating transaction having GTID_NEXT=ANONYMOUS.
Definition: rpl_gtid.h:3072
const Tsid & get_server_tsid() const
Return the server's TSID.
Definition: rpl_gtid.h:3376
rpl_sidno server_sidno
The SIDNO for this server.
Definition: rpl_gtid.h:3579
bool wait_for_gtid_set(THD *thd, Gtid_set *gtid_set, double timeout, bool update_thd_status=true)
Wait until the given Gtid_set is included in @GLOBAL.GTID_EXECUTED.
Definition: rpl_gtid_state.cc:304
enum_return_status acquire_ownership(THD *thd, const Gtid &gtid)
Acquires ownership of the given GTID, on behalf of the given thread.
Definition: rpl_gtid_state.cc:80
void update_gtids_impl_own_nothing(THD *thd)
Handle the case that the thread owns nothing.
Definition: rpl_gtid_state.cc:943
void update_gtids_impl_lock_sidno(rpl_sidno sidno)
Lock a given sidno of a transaction being updated.
Definition: rpl_gtid_state.cc:820
std::atomic< int32 > atomic_anonymous_gtid_count
The number of anonymous transactions owned by any client.
Definition: rpl_gtid.h:3582
int save_gtids_of_last_binlog_into_table()
Save the set of gtids logged in the last binlog into gtid_executed table.
Definition: rpl_gtid_state.cc:694
void end_gtid_wait()
Decrease the global counter when ending a call to WAIT_FOR_EXECUTED_GTID_SET.
Definition: rpl_gtid.h:3139
bool is_any_session_assigning_automatic_tagged_gtids()
Checks whether there are ongoing sessions executing transactions with GTID_NEXT set to AUTOMATIC:tag.
Definition: rpl_gtid.h:3394
Gtid_set lost_gtids
The set of GTIDs that existed in some previously purged binary log.
Definition: rpl_gtid.h:3563
const Owned_gtids * get_owned_gtids() const
Return a pointer to the Owned_gtids that contains the owned gtids.
Definition: rpl_gtid.h:3372
void unlock_owned_sidnos(const THD *thd)
Unlock all SIDNOs owned by the given THD.
Definition: rpl_gtid_state.cc:134
Owned_gtids owned_gtids
The set of GTIDs that are owned by some thread.
Definition: rpl_gtid.h:3577
rpl_sidno get_server_sidno() const
Return the server's SIDNO.
Definition: rpl_gtid.h:3374
const Gtid_set * get_previous_gtids_logged() const
Definition: rpl_gtid.h:3368
void update_gtids_impl_broadcast_and_unlock_sidno(rpl_sidno sidno)
Unlock a given sidno after broadcasting its changes.
Definition: rpl_gtid_state.cc:905
Gtid_state(Checkable_rwlock *_tsid_lock, Tsid_map *_tsid_map)
Constructs a new Gtid_state object.
Definition: rpl_gtid.h:2862
int32 get_gtid_wait_count()
Return the number of clients that have an ongoing call to WAIT_FOR_EXECUTED_GTID_SET.
Definition: rpl_gtid.h:3156
char * to_string() const
Debug only: return a newly allocated string, or NULL on out-of-memory.
Definition: rpl_gtid.h:3424
int read_gtid_executed_from_table()
Fetch gtids from gtid_executed table and store them into gtid_executed set.
Definition: rpl_gtid_state.cc:745
Mutex_cond_array tsid_locks
Contains one mutex/cond pair for every SIDNO.
Definition: rpl_gtid.h:3558
Gtid_set previous_gtids_logged
Definition: rpl_gtid.h:3575
std::atomic< int64_t > atomic_automatic_tagged_gtid_session_count
The number of sessions that have GTID_NEXT set to AUTOMATIC with tag assigned.
Definition: rpl_gtid.h:3592
enum_return_status add_lost_gtids(Gtid_set *gtid_set, bool starts_with_plus)
Adds the given Gtid_set to lost_gtids and executed_gtids.
Definition: rpl_gtid_state.cc:607
bool wait_for_sidno(THD *thd, rpl_sidno sidno, struct timespec *abstime, bool update_thd_status=true)
Wait for a signal on the given SIDNO.
Definition: rpl_gtid_state.cc:269
const Gtid_set * get_gtids_only_in_table() const
Definition: rpl_gtid.h:3361
int compress(THD *thd)
Compress the gtid_executed table, read each row by the PK(sid, gno_start) in increasing order,...
Definition: rpl_gtid_state.cc:749
std::unordered_map< rpl_sidno, rpl_gno > next_free_gno_map
The next_free_gno map contains next_free_gno for recorded sidnos.
Definition: rpl_gtid.h:3193
int32 get_anonymous_gtid_violating_transaction_count()
Return the number of ongoing GTID-violating transactions having GTID_NEXT=AUTOMATIC.
Definition: rpl_gtid.h:3114
void lock_sidnos(const Gtid_set *set)
Locks one mutex for each SIDNO where the given Gtid_set has at least one GTID.
Definition: rpl_gtid_state.cc:540
void assert_sidno_lock_owner(rpl_sidno sidno) const
Assert that we own the given SIDNO.
Definition: rpl_gtid.h:3246
enum_return_status ensure_commit_group_sidnos(rpl_sidno sidno)
Ensure that commit_group_sidnos have room for the SIDNO passed as parameter.
Definition: rpl_gtid_state.cc:953
void update_gtids_impl_own_anonymous(THD *thd, bool *more_trx)
Handle the case that the thread owns ANONYMOUS GTID.
Definition: rpl_gtid_state.cc:919
bool wait_for_gtid(THD *thd, const Gtid &gtid, struct timespec *abstime=nullptr)
This is only a shorthand for wait_for_sidno, which contains additional debug printouts and assertions...
Definition: rpl_gtid_state.cc:292
void acquire_anonymous_ownership()
Acquire anonymous ownership.
Definition: rpl_gtid.h:2986
int warn_or_err_on_modify_gtid_table(THD *thd, Table_ref *table)
Push a warning to client if user is modifying the gtid_executed table explicitly by a non-XA transact...
Definition: rpl_gtid_state.cc:753
void update_on_rollback(THD *thd)
Update the state after the given thread has rollbacked.
Definition: rpl_gtid_state.cc:213
Gtid_set gtids_only_in_table
Definition: rpl_gtid.h:3573
rpl_gno get_automatic_gno(rpl_sidno sidno) const
Computes the next available GNO.
Definition: rpl_gtid_state.cc:415
Gtid_set executed_gtids
Definition: rpl_gtid.h:3568
void update_gtids_impl_own_gtid(THD *thd, bool is_commit)
Handle the case that the thread own a single non-anonymous GTID.
Definition: rpl_gtid_state.cc:847
int32 get_automatic_gtid_violating_transaction_count()
Return the number of ongoing GTID-violating transactions having GTID_NEXT=AUTOMATIC.
Definition: rpl_gtid.h:3064
void dbug_print(const char *text="") const
Print this Gtid_state to the trace file if debug is enabled; no-op otherwise.
Definition: rpl_gtid.h:3441
void update_commit_group(THD *first_thd)
This function updates both the THD and the Gtid_state to reflect that the transaction set of transact...
Definition: rpl_gtid_state.cc:158
void unlock_sidno(rpl_sidno sidno)
Unlocks a mutex for the given SIDNO.
Definition: rpl_gtid.h:3242
bool is_owned(const Gtid &gtid) const
Returns true if GTID is owned, otherwise returns 0.
Definition: rpl_gtid.h:2919
int to_string(char *buf) const
Debug only: Generate a string in the given buffer and return the length.
Definition: rpl_gtid.h:3411
bool is_executed(const Gtid &gtid) const
Returns true if the given GTID is logged.
Definition: rpl_gtid.h:2906
Prealloced_array< bool, 8 > commit_group_sidnos
This array is used by Gtid_state_update_gtids_impl* functions.
Definition: rpl_gtid.h:3832
void update_prev_gtids(Gtid_set *write_gtid_set)
Updates previously logged GTID set before writing to table.
Definition: rpl_gtid_state.cc:588
Checkable_rwlock * tsid_lock
Read-write lock that protects updates to the number of TSIDs.
Definition: rpl_gtid.h:3554
void end_gtid_violating_transaction(THD *thd)
Definition: rpl_gtid_state.cc:256
void begin_automatic_gtid_violating_transaction()
Increase the global counter when starting a GTID-violating transaction having GTID_NEXT=AUTOMATIC.
Definition: rpl_gtid.h:3022
rpl_sidno specify_transaction_sidno(THD *thd, Gtid_state::Locked_sidno_set &sidno_set)
Determines sidno for thd transaction.
Definition: rpl_gtid_state.cc:479
void update_gtids_impl_broadcast_and_unlock_sidnos()
Unlocks all locked sidnos after broadcasting their changes.
Definition: rpl_gtid_state.cc:911
void lock_sidno(rpl_sidno sidno)
Locks a mutex for the given SIDNO.
Definition: rpl_gtid.h:3240
void update_gtids_impl_own_gtid_set(THD *thd, bool is_commit)
Handle the case that the thread own a set of GTIDs.
Definition: rpl_gtid_state.cc:792
void broadcast_sidnos(const Gtid_set *set)
Broadcasts the condition variable for each SIDNO where the given Gtid_set has at least one GTID.
Definition: rpl_gtid_state.cc:554
size_t get_max_string_length() const
Debug only: Returns an upper bound on the length of the string generated by to_string(),...
Definition: rpl_gtid.h:3404
std::atomic< int32 > atomic_automatic_gtid_violation_count
The number of GTID-violating transactions that use GTID_NEXT=AUTOMATIC.
Definition: rpl_gtid.h:3584
std::atomic< int32 > atomic_gtid_wait_count
The number of clients that are executing WAIT_FOR_EXECUTED_GTID_SET.
Definition: rpl_gtid.h:3589
void end_anonymous_gtid_violating_transaction()
Decrease the global counter when ending a GTID-violating transaction having GTID_NEXT=ANONYMOUS.
Definition: rpl_gtid.h:3090
rpl_gno get_last_executed_gno(rpl_sidno sidno) const
Return the last executed GNO for a given SIDNO, e.g.
Definition: rpl_gtid_state.cc:468
int clear(THD *thd)
Reset the state and persistor after RESET BINARY LOGS AND GTIDS: remove all logged and lost gtids,...
Definition: rpl_gtid_state.cc:58
std::atomic< int32 > atomic_anonymous_gtid_violation_count
The number of GTID-violating transactions that use GTID_NEXT=AUTOMATIC.
Definition: rpl_gtid.h:3586
void update_on_commit(THD *thd)
Remove the GTID owned by thread from owned GTIDs, stating that thd->owned_gtid was committed.
Definition: rpl_gtid_state.cc:206
void update_gtids_impl_lock_sidnos(THD *thd)
Locks the sidnos of all the GTIDs of the commit group starting on the transaction passed as parameter...
Definition: rpl_gtid_state.cc:826
int32 get_anonymous_ownership_count()
Return the number of clients that hold anonymous ownership.
Definition: rpl_gtid.h:3016
void update_gtids_impl_end(THD *thd, bool more_trx)
Handle the final part of update_gtids_impl.
Definition: rpl_gtid_state.cc:949
int save(THD *thd)
Save gtid owned by the thd into executed_gtids variable and gtid_executed table.
Definition: rpl_gtid_state.cc:667
bool update_gtids_impl_begin(THD *thd)
This is a sub task of update_gtids_impl responsible only to evaluate if the thread is committing in t...
Definition: rpl_gtid_state.cc:782
void broadcast_sidno(rpl_sidno sidno)
Broadcasts updates for the given SIDNO.
Definition: rpl_gtid.h:3244
enum_return_status generate_automatic_gtid(THD *thd, rpl_sidno specified_sidno=0, rpl_gno specified_gno=0)
Generates the GTID (or ANONYMOUS, if GTID_MODE = OFF or OFF_PERMISSIVE) for the THD,...
Definition: rpl_gtid_state.cc:499
void release_anonymous_ownership()
Release anonymous ownership.
Definition: rpl_gtid.h:3001
void begin_gtid_wait()
Increase the global counter when starting a call to WAIT_FOR_EXECUTED_GTID_SET.
Definition: rpl_gtid.h:3122
void increase_gtid_automatic_tagged_count()
Increments atomic_automatic_tagged_gtid_session_count.
Definition: rpl_gtid.h:3381
void update_gtids_impl(THD *thd, bool is_commit)
Remove the GTID owned by thread from owned GTIDs.
Definition: rpl_gtid_state.cc:220
Malloc_allocator is a C++ STL memory allocator based on my_malloc/my_free.
Definition: malloc_allocator.h:63
Represents a growable array where each element contains a mutex and a condition variable.
Definition: rpl_gtid.h:947
Prealloced_array< Mutex_cond *, 8 > m_array
Definition: rpl_gtid.h:1070
bool is_thd_killed(const THD *thd) const
Return true if the given THD is killed.
Definition: rpl_gtid_mutex_cond_array.cc:96
void enter_cond(THD *thd, int n, PSI_stage_info *stage, PSI_stage_info *old_stage) const
Execute THD::enter_cond for the n'th condition variable.
Definition: rpl_gtid_mutex_cond_array.cc:66
Checkable_rwlock * global_lock
Read-write lock that protects updates to the number of elements.
Definition: rpl_gtid.h:1069
bool wait(const THD *thd, int sidno, struct timespec *abstime) const
Wait for signal on the n'th condition variable.
Definition: rpl_gtid.h:1009
Mutex_cond_array(Checkable_rwlock *global_lock)
Create a new Mutex_cond_array.
Definition: rpl_gtid_mutex_cond_array.cc:42
void broadcast(int n) const
Broadcast the n'th condition.
Definition: rpl_gtid.h:969
void assert_not_owner(int n) const
Assert that this thread does not own the n'th mutex.
Definition: rpl_gtid.h:985
~Mutex_cond_array()
Destroy this object.
Definition: rpl_gtid_mutex_cond_array.cc:48
void assert_owner(int n) const
Assert that this thread owns the n'th mutex.
Definition: rpl_gtid.h:976
void unlock(int n) const
Unlock the n'th mutex.
Definition: rpl_gtid.h:964
void lock(int n) const
Lock the n'th mutex.
Definition: rpl_gtid.h:959
Mutex_cond * get_mutex_cond(int n) const
Return the Nth Mutex_cond object.
Definition: rpl_gtid.h:1061
Iterator over all gtids in a Owned_gtids set.
Definition: rpl_gtid.h:2757
rpl_sidno max_sidno
Max SIDNO of the current iterator.
Definition: rpl_gtid.h:2812
Node * node
Current node on current SIDNO hash.
Definition: rpl_gtid.h:2819
malloc_unordered_multimap< rpl_gno, unique_ptr_my_free< Node > >::const_iterator node_it
Current node iterator on current SIDNO hash.
Definition: rpl_gtid.h:2817
const Owned_gtids * owned_gtids
The Owned_gtids set we iterate over.
Definition: rpl_gtid.h:2808
Node * get_node() const
Return the current GTID Node, or NULL if we reached the end.
Definition: rpl_gtid.h:2804
Gtid_iterator(const Owned_gtids *og)
Definition: rpl_gtid.h:2759
Gtid get() const
Return next GTID, or {0,0} if we reached the end.
Definition: rpl_gtid.h:2795
void next()
Advance to next GTID.
Definition: rpl_gtid.h:2769
rpl_sidno sidno
The SIDNO of the current element, or 1 in the initial iteration.
Definition: rpl_gtid.h:2810
malloc_unordered_multimap< rpl_gno, unique_ptr_my_free< Node > > * hash
Current SIDNO hash.
Definition: rpl_gtid.h:2814
Represents the set of GTIDs that are owned by some thread.
Definition: rpl_gtid.h:2565
size_t get_max_string_length() const
Return an upper bound on the length of the string representation of this Owned_gtids.
Definition: rpl_gtid.h:2661
Checkable_rwlock * tsid_lock
Read-write lock that protects updates to the number of TSIDs.
Definition: rpl_gtid.h:2734
char * to_string() const
Debug only: return a newly allocated string representation of this Owned_gtids.
Definition: rpl_gtid.h:2693
bool thread_owns_anything(my_thread_id thd_id) const
Return true if the given thread is the owner of any gtids.
Definition: rpl_gtid.h:2677
enum_return_status add_gtid_owner(const Gtid &gtid, my_thread_id owner)
Add a GTID to this Owned_gtids.
Definition: rpl_gtid_owned.cc:70
bool is_intersection_nonempty(const Gtid_set *other) const
Returns true if there is a least one element of this Owned_gtids set in the other Gtid_set.
Definition: rpl_gtid_owned.cc:104
bool is_owned_by(const Gtid &gtid, const my_thread_id thd_id) const
If thd_id==0, returns true when gtid is not owned by any thread.
Definition: rpl_gtid_owned.cc:138
bool is_empty() const
Returns true if this Owned_gtids is empty.
Definition: rpl_gtid.h:2617
int to_string(char *out) const
Write a string representation of this Owned_gtids to the given buffer.
Definition: rpl_gtid.h:2635
rpl_sidno get_max_sidno() const
Returns the maximal sidno that this Owned_gtids currently has space for.
Definition: rpl_gtid.h:2622
malloc_unordered_multimap< rpl_gno, unique_ptr_my_free< Node > > * get_hash(rpl_sidno sidno) const
Returns the hash for the given SIDNO.
Definition: rpl_gtid.h:2736
Prealloced_array< malloc_unordered_multimap< rpl_gno, unique_ptr_my_free< Node > > *, 8 > sidno_to_hash
Growable array of hashes.
Definition: rpl_gtid.h:2750
Owned_gtids(Checkable_rwlock *tsid_lock)
Constructs a new, empty Owned_gtids object.
Definition: rpl_gtid_owned.cc:39
void dbug_print(const char *text="") const
Print this Owned_gtids to the trace file if debug is enabled; no-op otherwise.
Definition: rpl_gtid.h:2711
void get_gtids(Gtid_set &gtid_set) const
Definition: rpl_gtid_owned.cc:117
bool contains_gtid(const Gtid &gtid) const
Return true iff this Owned_gtids object contains the given gtid.
Definition: rpl_gtid_owned.cc:131
~Owned_gtids()
Destroys this Owned_gtids.
Definition: rpl_gtid_owned.cc:43
void print() const
Debug only: print this Owned_gtids to stdout.
Definition: rpl_gtid.h:2701
enum_return_status ensure_sidno(rpl_sidno sidno)
Ensures that this Owned_gtids object can accommodate SIDNOs up to the given SIDNO.
Definition: rpl_gtid_owned.cc:56
void remove_gtid(const Gtid &gtid, const my_thread_id owner)
Removes the given GTID.
Definition: rpl_gtid_owned.cc:89
A typesafe replacement for DYNAMIC_ARRAY.
Definition: prealloced_array.h:71
Using this class is fraught with peril, and you need to be very careful when doing so.
Definition: sql_string.h:167
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_lexer_thd.h:36
Definition: table.h:2863
Represents a bidirectional map between TSID and SIDNO.
Definition: rpl_gtid.h:749
rpl_sidno add_tsid(const Tsid &tsid)
Add the given TSID to this map if it does not already exist.
Definition: rpl_gtid_tsid_map.cc:61
enum_return_status copy(Tsid_map *dest)
Deep copy this Tsid_map to dest.
Definition: rpl_gtid_tsid_map.cc:145
Tsid_to_sidno_map::const_iterator Tsid_to_sidno_it
Definition: rpl_gtid.h:773
~Tsid_map()
Destroy this Tsid_map.
Definition: rpl_gtid_tsid_map.cc:51
Tsid_map(Checkable_rwlock *tsid_lock)
Create this Tsid_map.
Definition: rpl_gtid_tsid_map.cc:46
rpl_sidno get_max_sidno() const
Return the biggest sidno in this Tsid_map.
Definition: rpl_gtid.h:866
Checkable_rwlock * get_tsid_lock() const
Return the tsid_lock.
Definition: rpl_gtid.h:872
Tsid_to_sidno_umap _tsid_to_sidno
Hash that maps TSID to SIDNO.
Definition: rpl_gtid.h:915
rpl_sidno tsid_to_sidno(const Tsid &tsid) const
Get the SIDNO for a given TSID.
Definition: rpl_gtid.h:801
Tsid_to_sidno_map _sorted
Data structure that maps numbers in the interval [0, get_max_sidno()-1] to SIDNOs,...
Definition: rpl_gtid.h:922
Sidno_to_tsid_cont _sidno_to_tsid
Array that maps SIDNO to TSID; the element at index N points to a Node with SIDNO N-1.
Definition: rpl_gtid.h:909
enum_return_status clear()
Clears this Tsid_map (for RESET REPLICA)
Definition: rpl_gtid_tsid_map.cc:53
const rpl_sidno & get_sidno(const Tsid_to_sidno_it &it) const
Definition: rpl_gtid.h:850
Checkable_rwlock * tsid_lock
Read-write lock that protects updates to the number of SIDNOs.
Definition: rpl_gtid.h:903
const Tsid & get_tsid(const Tsid_to_sidno_it &it) const
Definition: rpl_gtid.h:855
const Tsid & sidno_to_tsid(rpl_sidno sidno, bool need_lock=false) const
Get the TSID for a given SIDNO.
Definition: rpl_gtid.h:827
std::vector< Tsid_ref, Malloc_allocator< Tsid_ref > > Sidno_to_tsid_cont
Definition: rpl_gtid.h:775
std::reference_wrapper< const Tsid > Tsid_ref
Definition: rpl_gtid.h:774
enum_return_status add_node(rpl_sidno sidno, const Tsid &tsid)
Create a Node from the given SIDNO and a TSID and add it to _sidno_to_tsid, _tsid_to_sidno,...
Definition: rpl_gtid_tsid_map.cc:101
Map_myalloc< Tsid, rpl_sidno > Tsid_to_sidno_map
Definition: rpl_gtid.h:772
const Tsid_to_sidno_map & get_sorted_sidno() const
Returns TSID to SID map.
Definition: rpl_gtid.h:848
Set that keeps track of TSID locks taken in the current scope.
Definition: locked_sidno_set.h:45
std::unordered_multimap, but with my_malloc, so that you can track the memory used using PSI memory k...
Definition: map_helpers.h:198
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
std::string to_string() const
Returns textual representation of Transaction Source Identifier.
Definition: tsid.cpp:34
#define mysql_cond_wait(C, M)
Definition: mysql_cond.h:48
#define mysql_cond_timedwait(C, M, T)
Definition: mysql_cond.h:51
#define mysql_mutex_lock(M)
Definition: mysql_mutex.h:50
#define mysql_mutex_unlock(M)
Definition: mysql_mutex.h:57
#define mysql_rwlock_rdlock(T)
Definition: mysql_rwlock.h:61
#define mysql_rwlock_unlock(T)
Definition: mysql_rwlock.h:91
#define mysql_rwlock_init(K, T)
Definition: mysql_rwlock.h:41
#define mysql_rwlock_tryrdlock(T)
Definition: mysql_rwlock