Debian Codegraph @

storage/innobase/trx/trx0sys.cc

raw
1 /*****************************************************************************
2
3 Copyright (c) 1996, 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 trx/trx0sys.cc
29 Transaction system
30
31 Created 3/26/1996 Heikki Tuuri
32 *******************************************************/
33
34 #include <sys/types.h>
35 #include <new>
36
37 #include "current_thd.h"
38 #include "ha_prototypes.h"
39 #include "mysql/plugin.h"
40 #include "sql_error.h"
41 #include "trx0sys.h"
42
43 #ifndef UNIV_HOTBACKUP
44 #include "fsp0fsp.h"
45 #include "fsp0sysspace.h"
46 #include "log0recv.h"
47 #include "mtr0log.h"
48 #include "os0file.h"
49 #include "read0read.h"
50 #include "srv0srv.h"
51 #include "srv0start.h"
52 #include "trx0purge.h"
53 #include "trx0rseg.h"
54 #include "trx0trx.h"
55 #include "trx0undo.h"
56
57 /** The transaction system */
58 trx_sys_t *trx_sys = nullptr;
59
60 /** Check whether transaction id is valid.
61 @param[in] id transaction id to check
62 @param[in] name table name */
63 void ReadView::check_trx_id_sanity(trx_id_t id, const table_name_t &name) {
64 if (&name == &dict_sys->dynamic_metadata->name) {
65 /* The table mysql.innodb_dynamic_metadata uses a
66 constant DB_TRX_ID=~0. */
67 ut_ad(id == (1ULL << 48) - 1);
68 return;
69 }
70
71 if (id >= trx_sys_get_next_trx_id_or_no()) {
72 ib::warn(ER_IB_MSG_1196)
73 << "A transaction id"
74 << " in a record of table " << name << " is newer than the"
75 << " system-wide maximum.";
76 ut_d(ut_error);
77 #ifndef UNIV_DEBUG
78 THD *thd = current_thd;
79 if (thd != nullptr) {
80 char table_name[MAX_FULL_NAME_LEN + 1];
81
82 innobase_format_name(table_name, sizeof(table_name), name.m_name);
83
84 push_warning_printf(thd, Sql_condition::SL_WARNING, ER_SIGNAL_WARN,
85 "InnoDB: Transaction id"
86 " in a record of table"
87 " %s is newer than system-wide"
88 " maximum.",
89 table_name);
90 }
91 #endif
92 }
93 }
94
95 #ifdef UNIV_DEBUG
96 /* Flag to control TRX_RSEG_N_SLOTS behavior debugging. */
97 uint trx_rseg_n_slots_debug = 0;
98 #endif /* UNIV_DEBUG */
99
100 /** Writes the value of max_trx_id to the file based trx system header. */
101 void trx_sys_write_max_trx_id(void) {
102 mtr_t mtr;
103 trx_sysf_t *sys_header;
104
105 /* The final synchronization happens here between maximum 2 threads,
106 one holding the trx_sys_t::mutex and one holding the serialisation
107 mutex. They can concurrently enter here, and start their mtrs.
108 They will synchronize inside trx_sysf_get because only one of them
109 could succeed in acquiring the x-lock for the header page to modify.
110 That thread will then read the max_trx_id and write to the page.
111 After it finishes mtr_commit, the another thread will succeed in
112 acquiring the x-lock and it will again read the newest max_trx_id,
113 and possibly re-write it. */
114
115 ut_ad(trx_sys_mutex_own() || trx_sys_serialisation_mutex_own());
116
117 if (!srv_read_only_mode) {
118 DBUG_EXECUTE_IF(
119 "trx_sys_write_max_trx_id__all_blocked",
120 while (true) { std::this_thread::sleep_for(std::chrono::seconds(1)); });
121
122 #ifdef UNIV_DEBUG
123 if (trx_sys_serialisation_mutex_own()) {
124 DEBUG_SYNC_C("trx_sys_write_max_trx_id__ser");
125 }
126 #endif /* UNIV_DEBUG */
127
128 mtr_start(&mtr);
129
130 sys_header = trx_sysf_get(&mtr);
131
132 const trx_id_t max_trx_id = trx_sys->next_trx_id_or_no.load();
133
134 mlog_write_ull(sys_header + TRX_SYS_TRX_ID_STORE, max_trx_id, &mtr);
135
136 mtr_commit(&mtr);
137 }
138 }
139
140 void trx_sys_persist_gtid_num(trx_id_t gtid_trx_no) {
141 mtr_t mtr;
142 mtr.start();
143 auto sys_header = trx_sysf_get(&mtr);
144 auto page = sys_header - TRX_SYS;
145 /* Update GTID transaction number. All transactions with lower
146 transaction number are no longer processed for GTID. */
147 mlog_write_ull(page + TRX_SYS_TRX_NUM_GTID, gtid_trx_no, &mtr);
148 mtr.commit();
149 }
150
151 trx_id_t trx_sys_oldest_trx_no() {
152 ut_ad(trx_sys_serialisation_mutex_own());
153 /* Get the oldest transaction from serialisation list. */
154 if (UT_LIST_GET_LEN(trx_sys->serialisation_list) > 0) {
155 auto trx = UT_LIST_GET_FIRST(trx_sys->serialisation_list);
156 return (trx->no);
157 }
158 return trx_sys_get_next_trx_id_or_no();
159 }
160
161 void trx_sys_get_binlog_prepared(std::vector<trx_id_t> &trx_ids) {
162 trx_sys_mutex_enter();
163 /* Exit fast if no prepared transaction. */
164 if (trx_sys->n_prepared_trx == 0) {
165 trx_sys_mutex_exit();
166 return;
167 }
168 /* Check and find binary log prepared transaction. */
169 for (auto trx : trx_sys->rw_trx_list) {
170 assert_trx_in_rw_list(trx);
171 if (trx_state_eq(trx, TRX_STATE_PREPARED) && trx_is_mysql_xa(trx)) {
172 trx_ids.push_back(trx->id);
173 }
174 }
175 trx_sys_mutex_exit();
176 }
177
178 /** Read binary log positions from buffer passed.
179 @param[in] binlog_buf binary log buffer from trx sys page
180 @param[out] file_name binary log file name
181 @param[out] high offset part high order bytes
182 @param[out] low offset part low order bytes
183 @return true, if buffer has valid binary log position. */
184 static bool read_binlog_position(const byte *binlog_buf, const char *&file_name,
185 uint32_t &high, uint32_t &low) {
186 /* Initialize out parameters. */
187 file_name = nullptr;
188 high = low = 0;
189
190 /* Check if binary log position is stored. */
191 if (mach_read_from_4(binlog_buf + TRX_SYS_MYSQL_LOG_MAGIC_N_FLD) !=
192 TRX_SYS_MYSQL_LOG_MAGIC_N) {
193 return (true);
194 }
195
196 /* Read binary log file name. */
197 file_name =
198 reinterpret_cast<const char *>(binlog_buf + TRX_SYS_MYSQL_LOG_NAME);
199
200 /* read log file offset. */
201 high = mach_read_from_4(binlog_buf + TRX_SYS_MYSQL_LOG_OFFSET_HIGH);
202 low = mach_read_from_4(binlog_buf + TRX_SYS_MYSQL_LOG_OFFSET_LOW);
203
204 return (false);
205 }
206
207 /** Write binary log position into passed buffer.
208 @param[in] file_name Binary log file name
209 @param[in] offset Binary log offset
210 @param[out] binlog_buf Buffer from trx sys page to write to
211 @param[in,out] mtr Mini-transaction */
212 static void write_binlog_position(const char *file_name, uint64_t offset,
213 byte *binlog_buf, mtr_t *mtr) {
214 if (file_name == nullptr ||
215 ut_strlen(file_name) >= TRX_SYS_MYSQL_LOG_NAME_LEN) {
216 /* We cannot fit the name to the 512 bytes we have reserved */
217 return;
218 }
219 const char *current_name = nullptr;
220 uint32_t high = 0;
221 uint32_t low = 0;
222
223 auto empty = read_binlog_position(binlog_buf, current_name, high, low);
224
225 if (empty) {
226 mlog_write_ulint(binlog_buf + TRX_SYS_MYSQL_LOG_MAGIC_N_FLD,
227 TRX_SYS_MYSQL_LOG_MAGIC_N, MLOG_4BYTES, mtr);
228 }
229
230 if (empty || 0 != strcmp(current_name, file_name)) {
231 mlog_write_string(binlog_buf + TRX_SYS_MYSQL_LOG_NAME, (byte *)file_name,
232 1 + ut_strlen(file_name), mtr);
233 }
234 auto in_high = static_cast<ulint>(offset >> 32);
235 auto in_low = static_cast<ulint>(offset & 0xFFFFFFFFUL);
236
237 if (empty || high != in_high) {
238 mlog_write_ulint(binlog_buf + TRX_SYS_MYSQL_LOG_OFFSET_HIGH, in_high,
239 MLOG_4BYTES, mtr);
240 }
241 mlog_write_ulint(binlog_buf + TRX_SYS_MYSQL_LOG_OFFSET_LOW, in_low,
242 MLOG_4BYTES, mtr);
243 }
244
245 void trx_sys_read_binlog_position(char *file, uint64_t &offset) {
246 const char *current_name = nullptr;
247 uint32_t high = 0;
248 uint32_t low = 0;
249
250 mtr_t mtr;
251 mtr_start(&mtr);
252
253 byte *binlog_pos = trx_sysf_get(&mtr) + TRX_SYS_MYSQL_LOG_INFO;
254 auto empty = read_binlog_position(binlog_pos, current_name, high, low);
255
256 if (empty) {
257 file[0] = '\0';
258 offset = 0;
259 mtr_commit(&mtr);
260 return;
261 }
262
263 strncpy(file, current_name, TRX_SYS_MYSQL_LOG_NAME_LEN);
264 offset = static_cast<uint64_t>(high);
265 offset = (offset << 32);
266 offset |= static_cast<uint64_t>(low);
267
268 mtr_commit(&mtr);
269 }
270
271 /** Check if binary log position is changed.
272 @param[in] file_name previous binary log file name
273 @param[in] offset previous binary log file offset
274 @param[out] binlog_buf buffer from trx sys page to write to
275 @return true, iff binary log position is modified from previous position. */
276 static bool binlog_position_changed(const char *file_name, uint64_t offset,
277 byte *binlog_buf) {
278 const char *cur_name = nullptr;
279 uint32_t high = 0;
280 uint32_t low = 0;
281 bool empty = read_binlog_position(binlog_buf, cur_name, high, low);
282
283 if (empty) {
284 return (false);
285 }
286
287 if (0 != strcmp(cur_name, file_name)) {
288 return (true);
289 }
290
291 auto cur_offset = static_cast<uint64_t>(high);
292 cur_offset = (cur_offset << 32);
293 cur_offset |= static_cast<uint64_t>(low);
294 return (offset != cur_offset);
295 }
296
297 bool trx_sys_write_binlog_position(const char *last_file, uint64_t last_offset,
298 const char *file, uint64_t offset) {
299 mtr_t mtr;
300 mtr_start(&mtr);
301 byte *binlog_pos = trx_sysf_get(&mtr) + TRX_SYS_MYSQL_LOG_INFO;
302
303 /* Return If position is already updated. */
304 if (binlog_position_changed(last_file, last_offset, binlog_pos)) {
305 mtr_commit(&mtr);
306 return (false);
307 }
308 write_binlog_position(file, offset, binlog_pos, &mtr);
309 mtr_commit(&mtr);
310 return (true);
311 }
312
313 void trx_sys_update_mysql_binlog_offset(trx_t *trx, mtr_t *mtr) {
314 trx_sys_update_binlog_position(trx);
315
316 const char *file_name = trx->mysql_log_file_name;
317 uint64_t offset = trx->mysql_log_offset;
318
319 /* Reset log file name in transaction. */
320 trx->mysql_log_file_name = nullptr;
321
322 byte *binlog_pos = trx_sysf_get(mtr) + TRX_SYS_MYSQL_LOG_INFO;
323
324 if (file_name == nullptr || file_name[0] == '\0') {
325 /* Don't write blank name in binary log file position. */
326 return;
327 }
328 write_binlog_position(file_name, offset, binlog_pos, mtr);
329 }
330
331 /** Find the page number in the TRX_SYS page for a given slot/rseg_id
332 @param[in] rseg_id slot number in the TRX_SYS page rseg array
333 @return page number from the TRX_SYS page rseg array */
334 page_no_t trx_sysf_rseg_find_page_no(ulint rseg_id) {
335 page_no_t page_no;
336 mtr_t mtr;
337 mtr.start();
338
339 trx_sysf_t *sys_header = trx_sysf_get(&mtr);
340
341 page_no = trx_sysf_rseg_get_page_no(sys_header, rseg_id, &mtr);
342
343 mtr.commit();
344
345 return (page_no);
346 }
347
348 /** Look for a free slot for a rollback segment in the trx system file copy.
349 @param[in,out] mtr mtr
350 @return slot index or ULINT_UNDEFINED if not found */
351 ulint trx_sysf_rseg_find_free(mtr_t *mtr) {
352 trx_sysf_t *sys_header = trx_sysf_get(mtr);
353
354 for (ulint slot_no = 0; slot_no < TRX_SYS_N_RSEGS; slot_no++) {
355 page_no_t page_no = trx_sysf_rseg_get_page_no(sys_header, slot_no, mtr);
356
357 if (page_no == FIL_NULL) {
358 return (slot_no);
359 }
360 }
361
362 return (ULINT_UNDEFINED);
363 }
364
365 /** Creates the file page for the transaction system. This function is called
366 only at the database creation, before trx_sys_init. */
367 static void trx_sysf_create(mtr_t *mtr) /*!< in: mtr */
368 {
369 trx_sysf_t *sys_header;
370 ulint slot_no;
371 buf_block_t *block;
372 page_t *page;
373 ulint page_no;
374 byte *ptr;
375 ulint len;
376
377 ut_ad(mtr);
378
379 /* Note that below we first reserve the file space x-latch, and
380 then enter the kernel: we must do it in this order to conform
381 to the latching order rules. */
382
383 mtr_x_lock_space(fil_space_get_sys_space(), mtr);
384
385 /* Create the trx sys file block in a new allocated file segment */
386 block = fseg_create(TRX_SYS_SPACE, 0, TRX_SYS + TRX_SYS_FSEG_HEADER, mtr);
387 buf_block_dbg_add_level(block, SYNC_TRX_SYS_HEADER);
388
389 ut_a(block->page.id.page_no() == TRX_SYS_PAGE_NO);
390
391 page = buf_block_get_frame(block);
392
393 mlog_write_ulint(page + FIL_PAGE_TYPE, FIL_PAGE_TYPE_TRX_SYS, MLOG_2BYTES,
394 mtr);
395
396 /* Reset the doublewrite buffer magic number to zero so that we
397 know that the doublewrite buffer has not yet been created (this
398 suppresses a Valgrind warning) */
399
400 mlog_write_ulint(page + TRX_SYS_DOUBLEWRITE + TRX_SYS_DOUBLEWRITE_MAGIC, 0,
401 MLOG_4BYTES, mtr);
402
403 sys_header = trx_sysf_get(mtr);
404
405 /* Start counting transaction ids from number 1 up */
406 mach_write_to_8(sys_header + TRX_SYS_TRX_ID_STORE, 1);
407
408 /* Reset the rollback segment slots. Old versions of InnoDB
409 define TRX_SYS_N_RSEGS as 256 (TRX_SYS_OLD_N_RSEGS) and expect
410 that the whole array is initialized. */
411 ptr = TRX_SYS_RSEGS + sys_header;
412 len = std::max(TRX_SYS_OLD_N_RSEGS, TRX_SYS_N_RSEGS) * TRX_SYS_RSEG_SLOT_SIZE;
413 memset(ptr, 0xff, len);
414 ptr += len;
415 ut_a(ptr <= page + (UNIV_PAGE_SIZE - FIL_PAGE_DATA_END));
416
417 /* Initialize all of the page. This part used to be uninitialized. */
418 memset(ptr, 0, UNIV_PAGE_SIZE - FIL_PAGE_DATA_END + page - ptr);
419
420 mlog_log_string(sys_header,
421 UNIV_PAGE_SIZE - FIL_PAGE_DATA_END + page - sys_header, mtr);
422
423 /* Create the first rollback segment in the SYSTEM tablespace */
424 slot_no = trx_sysf_rseg_find_free(mtr);
425 page_no = trx_rseg_header_create(TRX_SYS_SPACE, univ_page_size, PAGE_NO_MAX,
426 slot_no, mtr);
427
428 ut_a(slot_no == TRX_SYS_SYSTEM_RSEG_ID);
429 ut_a(page_no == FSP_FIRST_RSEG_PAGE_NO);
430 }
431
432 const uint32_t max_rseg_init_threads = 4;
433
434 /** Creates and initializes the central memory structures for the transaction
435 system. This is called when the database is started.
436 @return min binary heap of rsegs to purge */
437 purge_pq_t *trx_sys_init_at_db_start(void) {
438 purge_pq_t *purge_queue;
439 trx_sysf_t *sys_header;
440 uint64_t rows_to_undo = 0;
441 const char *unit = "";
442
443 /* We create the min binary heap here and pass ownership to
444 purge when we init the purge sub-system. Purge is responsible
445 for freeing the binary heap. */
446 purge_queue = ut::new_withkey<purge_pq_t>(UT_NEW_THIS_FILE_PSI_KEY);
447 ut_a(purge_queue != nullptr);
448
449 if (srv_force_recovery < SRV_FORCE_NO_UNDO_LOG_SCAN) {
450 /* Create the memory objects for all the rollback segments
451 referred to in the TRX_SYS page or any undo tablespace
452 RSEG_ARRAY page. */
453 srv_rseg_init_threads =
454 std::min(std::thread::hardware_concurrency(), max_rseg_init_threads);
455
456 /* Test hook to initialize the rollback segments using a single
457 thread. */
458 DBUG_EXECUTE_IF("rseg_init_single_thread", srv_rseg_init_threads = 1;);
459
460 using Clock = std::chrono::high_resolution_clock;
461 using Clock_point = std::chrono::time_point<Clock>;
462 Clock_point start = Clock::now();
463 if (srv_rseg_init_threads > 1) {
464 trx_rsegs_parallel_init(purge_queue);
465 } else {
466 trx_rsegs_init(purge_queue);
467 }
468 Clock_point end = Clock::now();
469 const auto time_diff =
470 std::chrono::duration_cast<std::chrono::microseconds>(end - start)
471 .count();
472 ib::info(ER_IB_MSG_PAR_RSEG_INIT_TIME_MSG, srv_rseg_init_threads,
473 (uint32_t)time_diff);
474 }
475
476 mtr_t mtr;
477 mtr.start();
478
479 sys_header = trx_sysf_get(&mtr);
480
481 const trx_id_t max_trx_id =
482 mach_read_from_8(sys_header + TRX_SYS_TRX_ID_STORE);
483
484 /* VERY important: after the database is started, next_trx_id_or_no value
485 needs to be set to a higher value than the maximum of values that have ever
486 been used for either trx->id or trx->no. After that, it needs to be written
487 to the transaction system header page, before it is used the first time to
488 assign a new value for trx->id or trx->no. This way trx id values will not
489 overlap when the database is repeatedly crashed and restarted!
490
491 Note, that the factor 2 in 2 * TRX_SYS_TRX_ID_WRITE_MARGIN is required,
492 because the next_trx_id_or_no might be increased concurrently in two
493 threads:
494 - one that has acquired trx_sys_mutex,
495 - and one that has acquired the trx_sys_serialisation_mutex.
496 If you decreased the factor 2, the test innodb.max_trx_id should fail. */
497
498 trx_sys->next_trx_id_or_no.store(max_trx_id +
499 2 * trx_sys_get_trx_id_write_margin());
500
501 trx_sys->serialisation_min_trx_no.store(trx_sys->next_trx_id_or_no.load());
502
503 mtr.commit();
504
505 #ifdef UNIV_DEBUG
506 /* max_trx_id is the next transaction ID to assign. Initialize maximum
507 transaction number to one less if all transactions are already purged. */
508 if (trx_sys->rw_max_trx_no == 0) {
509 trx_sys->rw_max_trx_no = trx_sys_get_next_trx_id_or_no() - 1;
510 }
511 #endif /* UNIV_DEBUG */
512
513 trx_sys_mutex_enter();
514 trx_sys_write_max_trx_id();
515 trx_sys_mutex_exit();
516
517 trx_dummy_sess = sess_open();
518
519 trx_lists_init_at_db_start();
520
521 /* This mutex is not strictly required, it is here only to satisfy
522 the debug code (assertions). We are still running in single threaded
523 bootstrap mode. */
524
525 trx_sys_mutex_enter();
526
527 if (UT_LIST_GET_LEN(trx_sys->rw_trx_list) > 0) {
528 for (auto trx : trx_sys->rw_trx_list) {
529 ut_ad(trx->is_recovered);
530 assert_trx_in_rw_list(trx);
531
532 if (trx_state_eq(trx, TRX_STATE_ACTIVE)) {
533 rows_to_undo += trx->undo_no;
534 }
535 }
536
537 if (rows_to_undo > 1000000000) {
538 unit = "M";
539 rows_to_undo = rows_to_undo / 1000000;
540 }
541
542 ib::info(ER_IB_MSG_1198)
543 << UT_LIST_GET_LEN(trx_sys->rw_trx_list)
544 << " transaction(s) which must be rolled back or"
545 " cleaned up in total "
546 << rows_to_undo << unit << " row operations to undo";
547
548 ib::info(ER_IB_MSG_1199)
549 << "Trx id counter is " << trx_sys_get_next_trx_id_or_no();
550 }
551
552 trx_sys->found_prepared_trx = trx_sys->n_prepared_trx > 0;
553
554 trx_sys_mutex_exit();
555
556 return (purge_queue);
557 }
558
559 /** Creates the trx_sys instance and initializes purge_queue and mutex. */
560 void trx_sys_create(void) {
561 ut_ad(trx_sys == nullptr);
562
563 trx_sys = static_cast<trx_sys_t *>(
564 ut::zalloc_withkey(UT_NEW_THIS_FILE_PSI_KEY, sizeof(*trx_sys)));
565
566 mutex_create(LATCH_ID_TRX_SYS, &trx_sys->mutex);
567 mutex_create(LATCH_ID_TRX_SYS_SERIALISATION, &trx_sys->serialisation_mutex);
568
569 UT_LIST_INIT(trx_sys->serialisation_list);
570 UT_LIST_INIT(trx_sys->rw_trx_list);
571 UT_LIST_INIT(trx_sys->mysql_trx_list);
572
573 trx_sys->mvcc = ut::new_withkey<MVCC>(UT_NEW_THIS_FILE_PSI_KEY, 1024);
574
575 trx_sys->serialisation_min_trx_no.store(0);
576
577 ut_d(trx_sys->rw_max_trx_no = 0);
578
579 new (&trx_sys->rw_trx_ids)
580 trx_ids_t(ut::allocator<trx_id_t>(mem_key_trx_sys_t_rw_trx_ids));
581
582 for (auto &shard : trx_sys->shards) {
583 new (&shard) Trx_shard{};
584 }
585
586 new (&trx_sys->rsegs) Rsegs();
587 trx_sys->rsegs.set_empty();
588
589 new (&trx_sys->tmp_rsegs) Rsegs();
590 trx_sys->tmp_rsegs.set_empty();
591 }
592
593 /** Creates and initializes the transaction system at the database creation. */
594 void trx_sys_create_sys_pages(void) {
595 mtr_t mtr;
596
597 mtr_start(&mtr);
598
599 trx_sysf_create(&mtr);
600
601 mtr_commit(&mtr);
602 }
603
604 /*********************************************************************
605 Shutdown/Close the transaction system. */
606 void trx_sys_close(void) {
607 ut_ad(srv_shutdown_state.load() == SRV_SHUTDOWN_EXIT_THREADS);
608
609 if (trx_sys == nullptr) {
610 return;
611 }
612
613 ulint size = trx_sys->mvcc->size();
614
615 if (size > 0) {
616 ib::error(ER_IB_MSG_1201) << "All read views were not closed before"
617 " shutdown: "
618 << size << " read views open";
619 }
620
621 sess_close(trx_dummy_sess);
622 trx_dummy_sess = nullptr;
623
624 trx_purge_sys_close();
625
626 /* Only prepared or active-recovered transactions may be left in the system.
627 The active-recovered transactions are allowed only if we did not force to
628 rollback them during shutdown (which might happen if e.g. it is fast
629 shutdown). Free all of them. */
630 ut_d(trx_sys_after_background_threads_shutdown_validate());
631
632 while (auto trx = UT_LIST_GET_FIRST(trx_sys->rw_trx_list)) {
633 trx_free_prepared_or_active_recovered(trx);
634 }
635
636 /* There can't be any active transactions. */
637 trx_sys->rsegs.~Rsegs();
638
639 trx_sys->tmp_rsegs.~Rsegs();
640
641 ut::delete_(trx_sys->mvcc);
642
643 ut_a(UT_LIST_GET_LEN(trx_sys->rw_trx_list) == 0);
644 ut_a(UT_LIST_GET_LEN(trx_sys->mysql_trx_list) == 0);
645 ut_a(UT_LIST_GET_LEN(trx_sys->serialisation_list) == 0);
646
647 for (auto &shard : trx_sys->shards) {
648 shard.~Trx_shard();
649 }
650
651 /* We used placement new to create this mutex. Call the destructor. */
652 mutex_free(&trx_sys->serialisation_mutex);
653 mutex_free(&trx_sys->mutex);
654
655 trx_sys->rw_trx_ids.~trx_ids_t();
656
657 ut::free(trx_sys);
658
659 trx_sys = nullptr;
660 }
661
662 void trx_sys_before_pre_dd_shutdown_validate() {
663 /** All connections are closed and close_connection unregisters
664 associated trx from mysql_trx_list. We still might have some non
665 started transactions in mysql_trx_list.
666 Purge threads are an exception, they create their own trx_t objects which are
667 not real transactions but are needed for the purge sys to use the query
668 threads framework. Purge sys shutdown happens at a later point in the shutdown
669 sequence so we need to skip its transactions here.
670 IMPORTANT: If allocating transactions for background threads, please use
671 trx_allocate_for_background. This function does not add the trx to the
672 mysql_trx_list so we don't have to add logic to skip these at shutdown.
673 */
674 trx_sys_mutex_enter();
675 for (auto trx : trx_sys->mysql_trx_list) {
676 /** Skip purge thread trx, it will be cleared after purge sys shutdown */
677 if (trx->purge_sys_trx) {
678 continue;
679 }
680 ut_a(trx->state.load(std::memory_order_relaxed) == TRX_STATE_NOT_STARTED);
681 }
682 trx_sys_mutex_exit();
683 }
684
685 void trx_sys_after_pre_dd_shutdown_validate() {
686 trx_sys_mutex_enter();
687 /** At this point we check the mysql_trx_list again, now we don't expect purge
688 thread transactions in the list */
689 for (auto trx : trx_sys->mysql_trx_list) {
690 ut_a(trx->state.load(std::memory_order_relaxed) == TRX_STATE_NOT_STARTED);
691 }
692 trx_sys_mutex_exit();
693
694 /* We assert that all transactions are rolled back if
695 [1] Not force recovery mode.
696 [2] Not fast shutdown
697 [3] The rollback thread has started and stopped gracefully.
698
699 The only left transactions are those that have state == TRX_STATE_PREPARED.
700
701 Above, [3] could be false during error exit, when the rollback thread might
702 never have started and we don't rollback the recovered transactions in that
703 case. */
704
705 const auto active_recovered_trxs = trx_sys_recovered_active_trxs_count();
706 if (srv_shutdown_waits_for_rollback_of_recovered_transactions() &&
707 srv_thread_is_stopped(srv_threads.m_trx_recovery_rollback)) {
708 ut_a(active_recovered_trxs == 0);
709 }
710
711 trx_sys_mutex_enter();
712 ut_a(UT_LIST_GET_LEN(trx_sys->rw_trx_list) ==
713 trx_sys->n_prepared_trx + active_recovered_trxs);
714 trx_sys_mutex_exit();
715 }
716
717 void trx_sys_after_background_threads_shutdown_validate() {
718 trx_sys_after_pre_dd_shutdown_validate();
719
720 ut_a(UT_LIST_GET_LEN(trx_sys->mysql_trx_list) == 0);
721 }
722
723 size_t trx_sys_recovered_active_trxs_count() {
724 size_t total_trx = 0;
725 trx_sys_mutex_enter();
726 /* Recovered transactions are never citizens of mysql_trx_list,
727 so it's enough to check rw_trx_list. */
728 for (auto trx : trx_sys->rw_trx_list) {
729 if (trx_state_eq(trx, TRX_STATE_ACTIVE) && trx->is_recovered) {
730 total_trx++;
731 }
732 }
733 trx_sys_mutex_exit();
734 return (total_trx);
735 }
736
737 #ifdef UNIV_DEBUG
738 /** Validate the trx_sys_t::rw_trx_list.
739 @return true if the list is valid. */
740 bool trx_sys_validate_trx_list() {
741 ut_ad(trx_sys_mutex_own());
742
743 const trx_t *prev_trx = nullptr;
744
745 for (auto trx : trx_sys->rw_trx_list) {
746 check_trx_state(trx);
747 ut_a(prev_trx == nullptr || prev_trx->id > trx->id);
748 prev_trx = trx;
749 }
750
751 return (true);
752 }
753 #endif /* UNIV_DEBUG */
754
755 #endif /* !UNIV_HOTBACKUP */
756
757 /** A list of undo tablespace IDs found in the TRX_SYS page. These are the
758 old type of undo tablespaces that do not have space_IDs in the reserved
759 range nor contain an RSEG_ARRAY page. This cannot be part of the trx_sys_t
760 object because it must be built before that is initialized. */
761 Space_Ids *trx_sys_undo_spaces;
762
763 /** Initialize trx_sys_undo_spaces, called once during srv_start(). */
764 void trx_sys_undo_spaces_init() {
765 trx_sys_undo_spaces =
766 ut::new_withkey<Space_Ids>(ut::make_psi_memory_key(mem_key_undo_spaces));
767
768 trx_sys_undo_spaces->reserve(TRX_SYS_N_RSEGS);
769 }
770
771 /** Free the resources occupied by trx_sys_undo_spaces,
772 called once during thread de-initialization. */
773 void trx_sys_undo_spaces_deinit() {
774 if (trx_sys_undo_spaces != nullptr) {
775 trx_sys_undo_spaces->clear();
776 ut::delete_(trx_sys_undo_spaces);
777 trx_sys_undo_spaces = nullptr;
778 }
779 }