Debian Codegraph @

storage/innobase/include/clone0repl.h

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