MySQL 26.7.0
Source Code Documentation
clone0repl.h
Go to the documentation of this file.
1/*****************************************************************************
2
3Copyright (c) 2018, 2026, Oracle and/or its affiliates.
4
5This program is free software; you can redistribute it and/or modify it under
6the terms of the GNU General Public License, version 2.0, as published by the
7Free Software Foundation.
8
9This program is designed to work with certain software (including
10but not limited to OpenSSL) that is licensed under separate terms,
11as designated in a particular file or component or in included license
12documentation. The authors of MySQL hereby grant you an additional
13permission to link the program and your derivative works with the
14separately licensed software that they have either included with
15the program or referenced in the documentation.
16
17This program is distributed in the hope that it will be useful, but WITHOUT
18ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
20for more details.
21
22You should have received a copy of the GNU General Public License along with
23this program; if not, write to the Free Software Foundation, Inc.,
2451 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25
26*****************************************************************************/
27
28/** @file include/clone0repl.h
29 GTID persistence interface
30
31 *******************************************************/
32
33#ifndef CLONE_REPL_INCLUDE
34#define CLONE_REPL_INCLUDE
35
36#include <vector>
37#include "clone0monitor.h"
38#include "os0thread-create.h"
39#include "sql/rpl_gtid.h"
40#include "srv0srv.h"
41#include "srv0start.h"
42#include "trx0sys.h"
43#include "trx0undo.h"
44
46
47/** Serialized GTID information size */
48inline constexpr size_t GTID_INFO_SIZE = 64;
49
50/** GTID format version. */
51inline constexpr uint32_t GTID_VERSION = 2;
52
53/** Serialized GTID */
54using Gtid_info = std::array<unsigned char, GTID_INFO_SIZE>;
55
56struct Gtid_desc;
57
58/** List of GTIDs */
59using Gtid_info_list = std::vector<Gtid_desc>;
60
61/** GTID descriptor with version information. */
62struct Gtid_desc {
63 /** If GTID descriptor is set. */
65 /** Serialized GTID information. */
67 /* GTID version. */
68 uint32_t m_version;
69};
70
71/** Persist GTID along with transaction commit */
73 public:
74 /** Constructor: start gtid thread */
77 /* No background is created yet. */
78 m_thread_active.store(false);
79 m_gtid_trx_no.store(0);
80 m_flush_number.store(0);
81 m_explicit_request.store(false);
82 m_active_number.store(m_flush_number.load() + 1);
83 /* We accept GTID even before the background service is started. This
84 is needed because we add GTIDs from undo log during recovery. */
85 m_active.store(true);
86 m_num_gtid_mem.store(0);
87 m_flush_in_progress.store(false);
88 m_close_thread.store(false);
89 }
90
91 /** Destructor: stop gtid thread */
93 ut_ad(!m_thread_active.load());
94 stop();
96 }
97
98 /** Start GTID persistence and background thread.
99 @return true, if successful. */
100 bool start();
101
102 /* Stop GTID persistence. */
103 void stop();
104
105 /* Wait for immediate flush.
106 @param[in] compress_gtid request GTID compression.
107 @param[in] early_timeout don't wait long if flush is blocked.
108 @param[in] cbk alert callback for long wait. */
109 void wait_flush(bool compress_gtid, bool early_timeout, Clone_Alert_Func cbk);
110
111 /**@return true, if GTID persistence is active. */
112 bool is_active() const { return (m_active.load()); }
113
114 /**@return true, if GTID thread is active. */
115 bool is_thread_active() const { return (m_thread_active.load()); }
116
117 /** Get oldest transaction number for which GTID is not persisted to table.
118 Transactions committed after this point should not be purged.
119 @return oldest transaction number. */
121 trx_id_t ret_no = m_gtid_trx_no.load();
122 /* Should never be zero. It can be set to max only before
123 GTID persister is active and no GTID is persisted. */
125 if (ret_no == TRX_ID_MAX) {
127 ut_ad(m_num_gtid_mem.load() == 0);
128 } else if (m_num_gtid_mem.load() == 0) {
129 /* For all transactions that are committed before this function is called
130 have their GTID flushed if flush is not in progress. "flush not in
131 progress" is sufficient but not necessary condition here. This is mainly
132 for cases when there is no GTID and purge doesn't need to wait. */
133 if (!m_flush_in_progress.load()) {
134 ret_no = TRX_ID_MAX;
135 }
136 }
137 return (ret_no);
138 }
139
140 /** Set oldest transaction number for which GTID is not persisted to table.
141 This is set during recovery from persisted value.
142 @param[in] max_trx_no transaction number */
144 ib::info(ER_IB_CLONE_GTID_PERSIST)
145 << "GTID recovery trx_no: " << max_trx_no;
146 /* Zero is special value. It is from old database without GTID
147 persistence. */
148 if (max_trx_no == 0) {
149 max_trx_no = TRX_ID_MAX;
150 }
151 m_gtid_trx_no.store(max_trx_no);
152 }
153
154 /** Get transaction GTID information.
155 @param[in,out] trx innodb transaction
156 @param[out] gtid_desc descriptor with serialized GTID */
157 void get_gtid_info(trx_t *trx, Gtid_desc &gtid_desc);
158
159 /** Set transaction flag to persist GTID and check if space need to be
160 allocated for GTID.
161 @param[in,out] trx current innodb transaction
162 @param[in] prepare if operation is Prepare
163 @param[in] rollback if operation is Rollback
164 @param[out] set_explicit if explicitly set to persist GTID
165 @return true, if undo space needs to be allocated. */
166 bool trx_check_set(trx_t *trx, bool prepare, bool rollback,
167 bool &set_explicit);
168
169 /** Check if current transaction has GTID.
170 @param[in] trx innodb transaction
171 @param[in,out] thd session THD
172 @param[out] passed_check true if transaction is good for GTID
173 @return true, if transaction has valid GTID. */
174 bool has_gtid(trx_t *trx, THD *&thd, bool &passed_check);
175
176 /** Check if GTID persistence is set
177 @param[in] trx current innnodb transaction
178 @return GTID storage type. */
180
181 /** Set or reset GTID persist flag in THD.
182 @param[in,out] trx current innnodb transaction
183 @param[in] set true, if need to set */
184 void set_persist_gtid(trx_t *trx, bool set);
185
186 /** Add GTID to in memory list.
187 @param[in] gtid_desc Descriptor with serialized GTID */
188 void add(const Gtid_desc &gtid_desc);
189
190 /** Write GTIDs periodically to disk table. */
191 void periodic_write();
192
193 /** Write GTIDs of non Innodb transactions to table. */
194 int write_other_gtids();
195
196 /** Disable copy construction */
198
199 /** Disable assignment */
201
202 private:
203 /** Check if GTID needs to persist at XA prepare.
204 @param[in] thd session THD
205 @param[in,out] trx current innnodb transaction
206 @param[in] found_gtid session is owning GTID
207 @param[in,out] alloc in:transaction checks are passed
208 out:GTID space need to be allocated
209 @return true, if GTID needs to be persisted */
210 bool check_gtid_prepare(THD *thd, trx_t *trx, bool found_gtid, bool &alloc);
211
212 /** Check if GTID needs to persist at commit.
213 @param[in] thd session THD
214 @param[in] found_gtid session is owning GTID
215 @param[out] set_explicit if explicitly set to persist GTID
216 @return true, if GTID needs to be persisted */
217 bool check_gtid_commit(THD *thd, bool found_gtid, bool &set_explicit);
218
219 /** Check if GTID needs to persist at rollback.
220 @param[in] thd session THD
221 @param[in,out] trx current innnodb transaction
222 @param[in] found_gtid session is owning GTID
223 @return true, if GTID needs to be persisted */
224 bool check_gtid_rollback(THD *thd, trx_t *trx, bool found_gtid);
225
226 /** Wait for gtid thread to start, finish or flush.
227 @param[in] start if waiting for start
228 @param[in] flush wait for immediate flush
229 @param[in] flush_number wait flush to reach this number
230 @param[in] compress wait also for compression
231 @param[in] early_timeout don't wait long if flush is blocked
232 @param[in] cbk alert callback for long wait
233 @return true if successful. */
234 bool wait_thread(bool start, bool flush, uint64_t flush_number, bool compress,
235 bool early_timeout, Clone_Alert_Func cbk);
236
237 /** @return current active GTID list */
240 return (get_list(m_active_number));
241 }
242
243 /** @return GTID list by number.
244 @param[in] list_number list number
245 @return GTID list reference. */
246 Gtid_info_list &get_list(uint64_t list_number) {
247 int list_index = (list_number & static_cast<uint64_t>(1));
248 return (m_gtids[list_index]);
249 }
250
251 /** Check if we need to skip write or compression based on debug variables.
252 @param[in] compression check for compression
253 @return true, if we should skip. */
254 bool debug_skip_write(bool compression);
255
256 /** Request immediate flush of all GTIDs accumulated.
257 @param[in] compress request compression of GTID table
258 @return flush list number to track and wait for flush to complete. */
261 /* We want to flush all GTIDs. */
262 uint64_t request_number = m_active_number.load();
263 /* If no GTIDs added to active, wait for previous index. */
264 if (m_num_gtid_mem.load() == 0) {
265 ut_a(request_number > 0);
266 --request_number;
267 }
268 m_flush_request_number = request_number;
270
271 if (compress) {
272 m_explicit_request.store(true);
273 }
274 return (request_number);
275 }
276
277 /** Check if flush has finished up to a list number.
278 @param[in] request_number flush request number
279 @return true, if it is already flushed. */
280 bool check_flushed(uint64_t request_number) const {
281 return (m_flush_number >= request_number);
282 }
283
284 /** @return true, iff background needs to flush immediately. */
285 bool flush_immediate() const {
287 }
288
289 /** Check if GTID compression is necessary based on threshold.
290 @return true, if GTID table needs to be compressed. */
291 bool check_compress();
292
293 /** Switch active GTID list. */
295 /* Switch active list under transaction system mutex. */
297 uint64_t flush_number = m_active_number;
300 m_num_gtid_mem.store(0);
301#ifdef UNIV_DEBUG
302 /* The new active list must have no elements. */
303 auto &active_list = get_active_list();
304 ut_ad(active_list.size() == 0);
305#endif
306 return (flush_number);
307 }
308
309 /** Persist GTID to gtid_executed table.
310 @param[in] flush_list_number list number to flush
311 @param[in,out] table_gtid_set GTIDs in table during recovery
312 @param[in,out] tsid_map TSID map for GTIDs
313 @return mysql error code. */
314 int write_to_table(uint64_t flush_list_number, Gtid_set &table_gtid_set,
315 Tsid_map &tsid_map);
316
317 /** Update transaction number up to which GTIDs are flushed to table.
318 @param[in] new_gtid_trx_no GTID transaction number */
319 void update_gtid_trx_no(trx_id_t new_gtid_trx_no);
320
321 /** Write all GTIDs to table and update GTID transaction number.
322 @param[in,out] thd current session thread */
323 void flush_gtids(THD *thd);
324
325 /** @return true iff number of GTIDs in active list exceeded threshold. */
327
328 private:
329 /** Time threshold to trigger persisting GTID. Insert GTID once per 1k
330 transactions or every 100 millisecond. */
332
333 /** Threshold for the count for compressing GTID. */
334 const static uint32_t s_compression_threshold = 50;
335
336 /** Number of transaction/GTID threshold for writing to disk table. */
337 const static int s_gtid_threshold = 1024;
338
339 /** Maximum Number of transaction/GTID to hold. Transaction commits
340 must wait beyond this point. Not expected to happen as GTIDs are
341 compressed and written together. */
342 const static int s_max_gtid_threshold = 1024 * 1024;
343
344 /** Two lists of GTID. One of them is active where running transactions
345 add their GTIDs. Other list is used to persist them to table from time
346 to time. */
348
349 /** Number of the current GTID list. Increased when list is switched */
350 std::atomic<uint64_t> m_active_number;
351
352 /** Number up to which GTIDs are flushed. Increased when list is flushed.*/
353 std::atomic<uint64_t> m_flush_number;
354
355 /** If explicit request to flush is made. */
356 std::atomic<bool> m_explicit_request;
357
358 /** Number for which last flush request was made. */
360
361 /** Event for GTID background thread. */
363
364 /** Counter to keep track of the number of writes till it reaches
365 compression threshold. */
367
368 /** Counter to keep number of GTIDs flushed before compression. */
370
371 /* Oldest transaction number for which GTID is not persisted. */
372 std::atomic<uint64_t> m_gtid_trx_no;
373
374 /** Number of GTID accumulated in memory */
375 std::atomic<int> m_num_gtid_mem;
376
377 /** Flush of GTID is in progress. */
378 std::atomic<bool> m_flush_in_progress;
379
380 /** Set to true, when the background thread is asked to exit. */
381 std::atomic<bool> m_close_thread;
382
383 /** true, if background thread is active.*/
384 std::atomic<bool> m_thread_active;
385
386 /** true, if GTID persistence is active.*/
387 std::atomic<bool> m_active;
388};
389
390#endif /* CLONE_REPL_INCLUDE */
Persist GTID along with transaction commit.
Definition: clone0repl.h:72
std::atomic< bool > m_flush_in_progress
Flush of GTID is in progress.
Definition: clone0repl.h:378
trx_id_t get_oldest_trx_no()
Get oldest transaction number for which GTID is not persisted to table.
Definition: clone0repl.h:120
static const int s_max_gtid_threshold
Maximum Number of transaction/GTID to hold.
Definition: clone0repl.h:342
Clone_persist_gtid & operator=(Clone_persist_gtid const &)=delete
Disable assignment.
bool wait_thread(bool start, bool flush, uint64_t flush_number, bool compress, bool early_timeout, Clone_Alert_Func cbk)
Wait for gtid thread to start, finish or flush.
Definition: clone0repl.cc:658
bool start()
Start GTID persistence and background thread.
Definition: clone0repl.cc:726
void stop()
Definition: clone0repl.cc:745
bool has_gtid(trx_t *trx, THD *&thd, bool &passed_check)
Check if current transaction has GTID.
Definition: clone0repl.cc:281
trx_undo_t::Gtid_storage persists_gtid(const trx_t *trx)
Check if GTID persistence is set.
Definition: clone0repl.cc:88
static const int s_gtid_threshold
Number of transaction/GTID threshold for writing to disk table.
Definition: clone0repl.h:337
void set_persist_gtid(trx_t *trx, bool set)
Set or reset GTID persist flag in THD.
Definition: clone0repl.cc:109
std::atomic< bool > m_explicit_request
If explicit request to flush is made.
Definition: clone0repl.h:356
bool is_active() const
Definition: clone0repl.h:112
void update_gtid_trx_no(trx_id_t new_gtid_trx_no)
Update transaction number up to which GTIDs are flushed to table.
Definition: clone0repl.cc:485
static constexpr std::chrono::milliseconds s_time_threshold
Time threshold to trigger persisting GTID.
Definition: clone0repl.h:331
std::atomic< int > m_num_gtid_mem
Number of GTID accumulated in memory.
Definition: clone0repl.h:375
Gtid_info_list & get_active_list()
Definition: clone0repl.h:238
Gtid_info_list m_gtids[2]
Two lists of GTID.
Definition: clone0repl.h:347
bool check_gtid_rollback(THD *thd, trx_t *trx, bool found_gtid)
Check if GTID needs to persist at rollback.
Definition: clone0repl.cc:243
bool check_flushed(uint64_t request_number) const
Check if flush has finished up to a list number.
Definition: clone0repl.h:280
bool debug_skip_write(bool compression)
Check if we need to skip write or compression based on debug variables.
Definition: clone0repl.cc:409
void periodic_write()
Write GTIDs periodically to disk table.
Definition: clone0repl.cc:588
Clone_persist_gtid()
Constructor: start gtid thread.
Definition: clone0repl.h:75
std::atomic< uint64_t > m_active_number
Number of the current GTID list.
Definition: clone0repl.h:350
os_event_t m_event
Event for GTID background thread.
Definition: clone0repl.h:362
uint64_t m_flush_request_number
Number for which last flush request was made.
Definition: clone0repl.h:359
bool check_compress()
Check if GTID compression is necessary based on threshold.
Definition: clone0repl.cc:385
void set_oldest_trx_no_recovery(trx_id_t max_trx_no)
Set oldest transaction number for which GTID is not persisted to table.
Definition: clone0repl.h:143
uint32_t m_compression_counter
Counter to keep track of the number of writes till it reaches compression threshold.
Definition: clone0repl.h:366
static const uint32_t s_compression_threshold
Threshold for the count for compressing GTID.
Definition: clone0repl.h:334
void flush_gtids(THD *thd)
Write all GTIDs to table and update GTID transaction number.
Definition: clone0repl.cc:502
bool check_max_gtid_threshold()
Definition: clone0repl.cc:580
bool trx_check_set(trx_t *trx, bool prepare, bool rollback, bool &set_explicit)
Set transaction flag to persist GTID and check if space need to be allocated for GTID.
Definition: clone0repl.cc:163
bool check_gtid_prepare(THD *thd, trx_t *trx, bool found_gtid, bool &alloc)
Check if GTID needs to persist at XA prepare.
Definition: clone0repl.cc:192
std::atomic< bool > m_thread_active
true, if background thread is active.
Definition: clone0repl.h:384
std::atomic< uint64_t > m_gtid_trx_no
Definition: clone0repl.h:372
~Clone_persist_gtid()
Destructor: stop gtid thread.
Definition: clone0repl.h:92
Clone_persist_gtid(Clone_persist_gtid const &)=delete
Disable copy construction.
void add(const Gtid_desc &gtid_desc)
Add GTID to in memory list.
Definition: clone0repl.cc:46
std::atomic< bool > m_close_thread
Set to true, when the background thread is asked to exit.
Definition: clone0repl.h:381
uint64_t request_immediate_flush(bool compress)
Request immediate flush of all GTIDs accumulated.
Definition: clone0repl.h:259
uint64_t switch_active_list()
Switch active GTID list.
Definition: clone0repl.h:294
void get_gtid_info(trx_t *trx, Gtid_desc &gtid_desc)
Get transaction GTID information.
Definition: clone0repl.cc:337
int write_other_gtids()
Write GTIDs of non Innodb transactions to table.
Definition: clone0repl.cc:377
bool flush_immediate() const
Definition: clone0repl.h:285
std::atomic< bool > m_active
true, if GTID persistence is active.
Definition: clone0repl.h:387
Gtid_info_list & get_list(uint64_t list_number)
Definition: clone0repl.h:246
int write_to_table(uint64_t flush_list_number, Gtid_set &table_gtid_set, Tsid_map &tsid_map)
Persist GTID to gtid_executed table.
Definition: clone0repl.cc:420
std::atomic< uint64_t > m_flush_number
Number up to which GTIDs are flushed.
Definition: clone0repl.h:353
void wait_flush(bool compress_gtid, bool early_timeout, Clone_Alert_Func cbk)
Definition: clone0repl.cc:754
bool check_gtid_commit(THD *thd, bool found_gtid, bool &set_explicit)
Check if GTID needs to persist at commit.
Definition: clone0repl.cc:230
uint32_t m_compression_gtid_counter
Counter to keep number of GTIDs flushed before compression.
Definition: clone0repl.h:369
bool is_thread_active() const
Definition: clone0repl.h:115
Represents a set of GTIDs.
Definition: rpl_gtid.h:1558
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_lexer_thd.h:36
Represents a bidirectional map between TSID and SIDNO.
Definition: rpl_gtid.h:751
The class info is used to emit informational log messages.
Definition: ut0log.h:183
Performance Schema stage instrumentation to monitor clone progress.
std::function< int()> Clone_Alert_Func
Function to alert caller for long wait.
Definition: clone0monitor.h:43
constexpr uint32_t GTID_VERSION
GTID format version.
Definition: clone0repl.h:51
std::vector< Gtid_desc > Gtid_info_list
List of GTIDs.
Definition: clone0repl.h:59
constexpr size_t GTID_INFO_SIZE
Serialized GTID information size.
Definition: clone0repl.h:48
std::array< unsigned char, GTID_INFO_SIZE > Gtid_info
Serialized GTID.
Definition: clone0repl.h:54
static int compress(PACK_MRG_INFO *file, char *join_name)
Definition: myisampack.cc:466
std::chrono::milliseconds milliseconds
Definition: authorize_manager.cc:69
static mysql_service_status_t flush(reference_caching_cache cache) noexcept
Definition: component.cc:114
std::set< Key, Compare, ut::allocator< Key > > set
Specialization of set which uses ut_allocator.
Definition: ut0new.h:2732
os_event_t os_event_create()
Creates an event semaphore, i.e., a semaphore which may just have two states: signaled and nonsignale...
Definition: os0event.cc:514
void os_event_destroy(os_event_t &event)
Frees an event object.
Definition: os0event.cc:579
The interface to the threading wrapper.
static bool rollback(THD *thd)
Abort the current statement and transaction.
Definition: sql_cmd_srs.cc:140
The server main program.
ulong srv_force_recovery
Normally 0.
Definition: srv0srv.cc:527
@ SRV_FORCE_NO_UNDO_LOG_SCAN
do not look at undo logs when starting the database: InnoDB will treat even incomplete transactions a...
Definition: srv0srv.h:923
Starts the Innobase database server.
GTID descriptor with version information.
Definition: clone0repl.h:62
uint32_t m_version
Definition: clone0repl.h:68
bool m_is_set
If GTID descriptor is set.
Definition: clone0repl.h:64
Gtid_info m_info
Serialized GTID information.
Definition: clone0repl.h:66
InnoDB condition variable.
Definition: os0event.cc:63
Definition: trx0trx.h:670
Gtid_storage
Undo log may could be allocated to store transaction GTIDs.
Definition: trx0undo.h:355
Transaction system.
static void trx_sys_serialisation_mutex_exit()
Release the trx_sys->serialisation_mutex.
Definition: trx0sys.h:841
static void trx_sys_serialisation_mutex_enter()
Acquire the trx_sys->serialisation_mutex.
Definition: trx0sys.h:836
static bool trx_sys_serialisation_mutex_own()
Test if trx_sys->serialisation_mutex is owned.
Definition: trx0sys.h:830
constexpr trx_id_t TRX_ID_MAX
Maximum transaction identifier.
Definition: trx0types.h:145
ib_id_t trx_id_t
Transaction identifier (DB_TRX_ID, DATA_TRX_ID)
Definition: trx0types.h:138
Transaction undo log.
#define ut_ad(EXPR)
Debug assertion.
Definition: ut0dbg.h:109
#define ut_a(EXPR)
Abort execution if EXPR does not evaluate to nonzero.
Definition: ut0dbg.h:97
static void prepare(pax_msg *p, pax_op op)
Definition: xcom_base.cc:1592