Debian Codegraph @

storage/innobase/trx/trx0roll.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/trx0roll.cc
29 Transaction rollback
30
31 Created 3/26/1996 Heikki Tuuri
32 *******************************************************/
33
34 #include <sys/types.h>
35
36 #include "clone0clone.h"
37 #include "dict0dd.h"
38 #include "fsp0fsp.h"
39 #include "ha_prototypes.h"
40 #include "lock0lock.h"
41 #include "mach0data.h"
42 #include "os0thread-create.h"
43 #include "pars0pars.h"
44 #include "que0que.h"
45 #include "read0read.h"
46 #include "row0mysql.h"
47 #include "row0undo.h"
48 #include "sql_thd_internal_api.h"
49 #include "srv0mon.h"
50 #include "srv0start.h"
51 #include "trx0rec.h"
52 #include "trx0roll.h"
53 #include "trx0rseg.h"
54 #include "trx0sys.h"
55 #include "trx0trx.h"
56 #include "trx0undo.h"
57 #include "usr0sess.h"
58
59 #include "current_thd.h"
60
61 /** This many pages must be undone before a truncate is tried within
62 rollback */
63 static const ulint TRX_ROLL_TRUNC_THRESHOLD = 1;
64
65 /** In crash recovery, the current trx to be rolled back; NULL otherwise */
66 static const trx_t *trx_roll_crash_recv_trx = nullptr;
67
68 /** In crash recovery we set this to the undo n:o of the current trx to be
69 rolled back. Then we can print how many % the rollback has progressed. */
70 static undo_no_t trx_roll_max_undo_no;
71
72 /** Auxiliary variable which tells the previous progress % we printed */
73 static ulint trx_roll_progress_printed_pct;
74
75 /** Finishes a transaction rollback. */
76 static void trx_rollback_finish(trx_t *trx); /*!< in: transaction */
77
78 /** Rollback a transaction used in MySQL. */
79 static void trx_rollback_to_savepoint_low(
80 trx_t *trx, /*!< in: transaction handle */
81 trx_savept_t *savept) /*!< in: pointer to savepoint undo number, if
82 partial rollback requested, or NULL for
83 complete rollback */
84 {
85 que_thr_t *thr;
86 mem_heap_t *heap;
87 roll_node_t *roll_node;
88
89 heap = mem_heap_create(512, UT_LOCATION_HERE);
90
91 roll_node = roll_node_create(heap);
92
93 if (savept != nullptr) {
94 roll_node->partial = true;
95 roll_node->savept = *savept;
96 check_trx_state(trx);
97 } else {
98 assert_trx_nonlocking_or_in_list(trx);
99 }
100
101 trx->error_state = DB_SUCCESS;
102
103 if (trx_is_rseg_updated(trx)) {
104 ut_ad(trx->rsegs.m_redo.rseg != nullptr ||
105 trx->rsegs.m_noredo.rseg != nullptr);
106
107 thr = pars_complete_graph_for_exec(roll_node, trx, heap, nullptr);
108
109 ut_a(thr == que_fork_start_command(
110 static_cast<que_fork_t *>(que_node_get_parent(thr))));
111
112 que_run_threads(thr);
113
114 ut_a(roll_node->undo_thr != nullptr);
115 que_run_threads(roll_node->undo_thr);
116
117 /* Free the memory reserved by the undo graph. */
118 que_graph_free(static_cast<que_t *>(roll_node->undo_thr->common.parent));
119 }
120
121 if (savept == nullptr) {
122 trx_rollback_finish(trx);
123 MONITOR_INC(MONITOR_TRX_ROLLBACK);
124 } else {
125 trx->lock.que_state = TRX_QUE_RUNNING;
126 MONITOR_INC(MONITOR_TRX_ROLLBACK_SAVEPOINT);
127 }
128
129 ut_a(trx->error_state == DB_SUCCESS);
130 ut_a(trx->lock.que_state == TRX_QUE_RUNNING);
131
132 mem_heap_free(heap);
133
134 /* There might be work for utility threads.*/
135 srv_active_wake_master_thread();
136
137 MONITOR_DEC(MONITOR_TRX_ACTIVE);
138 }
139
140 /** Rollback a transaction to a given savepoint or do a complete rollback.
141 @return error code or DB_SUCCESS */
142 dberr_t trx_rollback_to_savepoint(
143 trx_t *trx, /*!< in: transaction handle */
144 trx_savept_t *savept) /*!< in: pointer to savepoint undo number, if
145 partial rollback requested, or NULL for
146 complete rollback */
147 {
148 ut_ad(!trx_mutex_own(trx));
149
150 trx_start_if_not_started_xa(trx, true, UT_LOCATION_HERE);
151
152 trx_rollback_to_savepoint_low(trx, savept);
153
154 return (trx->error_state);
155 }
156
157 /** Rollback a transaction used in MySQL.
158 @return error code or DB_SUCCESS */
159 static dberr_t trx_rollback_for_mysql_low(
160 trx_t *trx) /*!< in/out: transaction */
161 {
162 trx->op_info = "rollback";
163
164 /* If we are doing the XA recovery of prepared transactions,
165 then the transaction object does not have an InnoDB session
166 object, and we set a dummy session that we use for all MySQL
167 transactions. */
168
169 trx_rollback_to_savepoint_low(trx, nullptr);
170
171 trx->op_info = "";
172
173 ut_a(trx->error_state == DB_SUCCESS);
174
175 return (trx->error_state);
176 }
177
178 /** Rollback a transaction used in MySQL
179 @param[in, out] trx transaction
180 @return error code or DB_SUCCESS */
181 static dberr_t trx_rollback_low(trx_t *trx) {
182 /* We are reading trx->state without mutex protection here,
183 because the rollback should either be invoked for:
184 - a running active MySQL transaction associated
185 with the current thread,
186 - or a recovered prepared transaction,
187 - or a transaction which is a victim being killed by HP transaction
188 run by the current thread, in which case it is guaranteed that
189 thread owning the transaction, which is being killed, is not
190 inside InnoDB (thanks to TRX_FORCE_ROLLBACK and TrxInInnoDB::wait()). */
191 ut_ad(trx_can_be_handled_by_current_thread_or_is_hp_victim(trx));
192
193 switch (trx->state.load(std::memory_order_relaxed)) {
194 case TRX_STATE_FORCED_ROLLBACK:
195 case TRX_STATE_NOT_STARTED:
196 trx->will_lock = 0;
197 ut_ad(trx->in_mysql_trx_list);
198 return (DB_SUCCESS);
199
200 case TRX_STATE_ACTIVE:
201 ut_ad(trx->in_mysql_trx_list);
202 assert_trx_nonlocking_or_in_list(trx);
203 /* Check an validate that undo is available for GTID. */
204 trx_undo_gtid_add_update_undo(trx, false, true);
205 return (trx_rollback_for_mysql_low(trx));
206
207 case TRX_STATE_PREPARED:
208 /* Check an validate that undo is available for GTID. */
209 trx_undo_gtid_add_update_undo(trx, false, true);
210 ut_ad(!trx_is_autocommit_non_locking(trx));
211 if (trx->rsegs.m_redo.rseg != nullptr && trx_is_redo_rseg_updated(trx)) {
212 /* Change the undo log state back from
213 TRX_UNDO_PREPARED to TRX_UNDO_ACTIVE
214 so that if the system gets killed,
215 recovery will perform the rollback. */
216 trx_undo_ptr_t *undo_ptr = &trx->rsegs.m_redo;
217
218 mtr_t mtr;
219
220 mtr.start();
221
222 trx->rsegs.m_redo.rseg->latch();
223
224 if (undo_ptr->insert_undo != nullptr) {
225 trx_undo_set_state_at_prepare(trx, undo_ptr->insert_undo, true, &mtr);
226 }
227
228 if (undo_ptr->update_undo != nullptr) {
229 trx_undo_gtid_set(trx, undo_ptr->update_undo, false);
230 trx_undo_set_state_at_prepare(trx, undo_ptr->update_undo, true, &mtr);
231 }
232 trx->rsegs.m_redo.rseg->unlatch();
233
234 /* Persist the XA ROLLBACK, so that crash
235 recovery will replay the rollback in case
236 the redo log gets applied past this point. */
237 mtr.commit();
238 ut_ad(mtr.commit_lsn() > 0 || !mtr_t::s_logging.is_enabled());
239 }
240 #ifdef ENABLED_DEBUG_SYNC
241 if (trx->mysql_thd == nullptr) {
242 /* We could be executing XA ROLLBACK after
243 XA PREPARE and a server restart. */
244 } else if (!trx_is_redo_rseg_updated(trx)) {
245 /* innobase_close_connection() may roll back a
246 transaction that did not generate any
247 persistent undo log. The DEBUG_SYNC
248 would cause an assertion failure for a
249 disconnected thread.
250
251 NOTE: InnoDB will not know about the XID
252 if no persistent undo log was generated. */
253 } else {
254 DEBUG_SYNC_C("trx_xa_rollback");
255 }
256 #endif /* ENABLED_DEBUG_SYNC */
257 return (trx_rollback_for_mysql_low(trx));
258
259 case TRX_STATE_COMMITTED_IN_MEMORY:
260 check_trx_state(trx);
261 break;
262 }
263
264 ut_error;
265 }
266
267 /** Rollback a transaction used in MySQL.
268 @return error code or DB_SUCCESS */
269 dberr_t trx_rollback_for_mysql(trx_t *trx) /*!< in/out: transaction */
270 {
271 /* Avoid the tracking of async rollback killer
272 thread to enter into InnoDB. */
273 if (TrxInInnoDB::is_async_rollback(trx)) {
274 return (trx_rollback_low(trx));
275
276 } else {
277 TrxInInnoDB trx_in_innodb(trx, true);
278 return (trx_rollback_low(trx));
279 }
280 }
281
282 /** Rollback the latest SQL statement for MySQL.
283 @return error code or DB_SUCCESS */
284 dberr_t trx_rollback_last_sql_stat_for_mysql(
285 trx_t *trx) /*!< in/out: transaction */
286 {
287 dberr_t err;
288
289 ut_ad(trx->in_mysql_trx_list);
290
291 /* We are reading trx->state without mutex protection here,
292 because the rollback should either be invoked for:
293 - a running active MySQL transaction associated
294 with the current thread,
295 - or a recovered prepared transaction,
296 - or a transaction which is a victim being killed by HP transaction
297 run by the current thread, in which case it is guaranteed that
298 thread owning the transaction, which is being killed, is not
299 inside InnoDB (thanks to TRX_FORCE_ROLLBACK and TrxInInnoDB::wait()). */
300
301 ut_ad(trx_can_be_handled_by_current_thread_or_is_hp_victim(trx));
302
303 switch (trx->state.load(std::memory_order_relaxed)) {
304 case TRX_STATE_FORCED_ROLLBACK:
305 case TRX_STATE_NOT_STARTED:
306 return (DB_SUCCESS);
307
308 case TRX_STATE_ACTIVE:
309 assert_trx_nonlocking_or_in_list(trx);
310
311 trx->op_info = "rollback of SQL statement";
312
313 err = trx_rollback_to_savepoint(trx, &trx->last_sql_stat_start);
314
315 if (trx->fts_trx != nullptr) {
316 fts_savepoint_rollback_last_stmt(trx);
317 }
318
319 /* The following call should not be needed,
320 but we play it safe: */
321 trx_mark_sql_stat_end(trx);
322
323 trx->op_info = "";
324
325 return (err);
326
327 case TRX_STATE_PREPARED:
328 case TRX_STATE_COMMITTED_IN_MEMORY:
329 /* The statement rollback is only allowed on an ACTIVE
330 transaction, not a PREPARED or COMMITTED one. */
331 break;
332 }
333
334 ut_error;
335 }
336
337 /** Search for a savepoint using name.
338 @return savepoint if found else NULL */
339 static trx_named_savept_t *trx_savepoint_find(
340 trx_t *trx, /*!< in: transaction */
341 const char *name) /*!< in: savepoint name */
342 {
343 for (auto savep : trx->trx_savepoints) {
344 if (0 == ut_strcmp(savep->name, name)) {
345 return (savep);
346 }
347 }
348
349 return (nullptr);
350 }
351
352 /** Frees a single savepoint struct. */
353 static void trx_roll_savepoint_free(
354 trx_t *trx, /*!< in: transaction handle */
355 trx_named_savept_t *savep) /*!< in: savepoint to free */
356 {
357 UT_LIST_REMOVE(trx->trx_savepoints, savep);
358
359 ut::free(savep->name);
360 ut::free(savep);
361 }
362
363 /** Frees savepoint structs starting from savep.
364 @param[in] trx Transaction handle
365 @param[in] savep Free all savepoints starting with this savepoint i, if savep is
366 nullptr free all save points */
367 void trx_roll_savepoints_free(trx_t *trx, trx_named_savept_t *savep) {
368 while (savep != nullptr) {
369 trx_named_savept_t *next_savep;
370
371 next_savep = UT_LIST_GET_NEXT(trx_savepoints, savep);
372
373 trx_roll_savepoint_free(trx, savep);
374
375 savep = next_savep;
376 }
377 }
378
379 /** Rolls back a transaction back to a named savepoint. Modifications after the
380 savepoint are undone but InnoDB does NOT release the corresponding locks
381 which are stored in memory. If a lock is 'implicit', that is, a new inserted
382 row holds a lock where the lock information is carried by the trx id stored in
383 the row, these locks are naturally released in the rollback. Savepoints which
384 were set after this savepoint are deleted.
385 @return if no savepoint of the name found then DB_NO_SAVEPOINT,
386 otherwise DB_SUCCESS */
387 [[nodiscard]] static dberr_t trx_rollback_to_savepoint_for_mysql_low(
388 trx_t *trx, /*!< in/out: transaction */
389 trx_named_savept_t *savep, /*!< in/out: savepoint */
390 int64_t *mysql_binlog_cache_pos)
391 /*!< out: the MySQL binlog
392 cache position corresponding
393 to this savepoint; MySQL needs
394 this information to remove the
395 binlog entries of the queries
396 executed after the savepoint */
397 {
398 dberr_t err;
399
400 ut_ad(trx_state_eq(trx, TRX_STATE_ACTIVE));
401 ut_ad(trx->in_mysql_trx_list);
402
403 /* Free all savepoints strictly later than savep. */
404
405 trx_roll_savepoints_free(trx, UT_LIST_GET_NEXT(trx_savepoints, savep));
406
407 *mysql_binlog_cache_pos = savep->mysql_binlog_cache_pos;
408
409 trx->op_info = "rollback to a savepoint";
410
411 err = trx_rollback_to_savepoint(trx, &savep->savept);
412
413 /* Store the current undo_no of the transaction so that
414 we know where to roll back if we have to roll back the
415 next SQL statement: */
416
417 trx_mark_sql_stat_end(trx);
418
419 trx->op_info = "";
420
421 return (err);
422 }
423
424 /** Rolls back a transaction back to a named savepoint. Modifications after the
425 savepoint are undone but InnoDB does NOT release the corresponding locks
426 which are stored in memory. If a lock is 'implicit', that is, a new inserted
427 row holds a lock where the lock information is carried by the trx id stored in
428 the row, these locks are naturally released in the rollback. Savepoints which
429 were set after this savepoint are deleted.
430 @return if no savepoint of the name found then DB_NO_SAVEPOINT,
431 otherwise DB_SUCCESS */
432 dberr_t trx_rollback_to_savepoint_for_mysql(
433 trx_t *trx, /*!< in: transaction handle */
434 const char *savepoint_name, /*!< in: savepoint name */
435 int64_t *mysql_binlog_cache_pos) /*!< out: the MySQL binlog cache
436 position corresponding to this
437 savepoint; MySQL needs this
438 information to remove the
439 binlog entries of the queries
440 executed after the savepoint */
441 {
442 trx_named_savept_t *savep;
443
444 ut_ad(trx->in_mysql_trx_list);
445
446 savep = trx_savepoint_find(trx, savepoint_name);
447
448 if (savep == nullptr) {
449 return (DB_NO_SAVEPOINT);
450 }
451
452 /* We are reading trx->state without mutex protection here,
453 because the rollback should either be invoked for:
454 - a running active MySQL transaction associated
455 with the current thread,
456 - or a recovered prepared transaction,
457 - or a transaction which is a victim being killed by HP transaction
458 run by the current thread, in which case it is guaranteed that
459 thread owning the transaction, which is being killed, is not
460 inside InnoDB (thanks to TRX_FORCE_ROLLBACK and TrxInInnoDB::wait()). */
461
462 ut_ad(trx_can_be_handled_by_current_thread_or_is_hp_victim(trx));
463
464 switch (trx->state.load(std::memory_order_relaxed)) {
465 case TRX_STATE_NOT_STARTED:
466 case TRX_STATE_FORCED_ROLLBACK:
467
468 ib::error(ER_IB_MSG_1185) << "Transaction has a savepoint " << savep->name
469 << " though it is not started";
470
471 return (DB_ERROR);
472
473 case TRX_STATE_ACTIVE:
474
475 return (trx_rollback_to_savepoint_for_mysql_low(trx, savep,
476 mysql_binlog_cache_pos));
477
478 case TRX_STATE_PREPARED:
479 case TRX_STATE_COMMITTED_IN_MEMORY:
480 /* The savepoint rollback is only allowed on an ACTIVE
481 transaction, not a PREPARED or COMMITTED one. */
482 break;
483 }
484
485 ut_error;
486 }
487
488 /** Creates a named savepoint. If the transaction is not yet started, starts it.
489 If there is already a savepoint of the same name, this call erases that old
490 savepoint and replaces it with a new. Savepoints are deleted in a transaction
491 commit or rollback.
492 @return always DB_SUCCESS */
493 dberr_t trx_savepoint_for_mysql(
494 trx_t *trx, /*!< in: transaction handle */
495 const char *savepoint_name, /*!< in: savepoint name */
496 int64_t binlog_cache_pos) /*!< in: MySQL binlog cache
497 position corresponding to this
498 connection at the time of the
499 savepoint */
500 {
501 trx_named_savept_t *savep;
502
503 trx_start_if_not_started_xa(trx, false, UT_LOCATION_HERE);
504
505 savep = trx_savepoint_find(trx, savepoint_name);
506
507 if (savep) {
508 /* There is a savepoint with the same name: free that */
509
510 UT_LIST_REMOVE(trx->trx_savepoints, savep);
511
512 ut::free(savep->name);
513 ut::free(savep);
514 }
515
516 /* Create a new savepoint and add it as the last in the list */
517
518 savep = static_cast<trx_named_savept_t *>(
519 ut::malloc_withkey(UT_NEW_THIS_FILE_PSI_KEY, sizeof(*savep)));
520
521 savep->name = mem_strdup(savepoint_name);
522
523 savep->savept = trx_savept_take(trx);
524
525 savep->mysql_binlog_cache_pos = binlog_cache_pos;
526
527 UT_LIST_ADD_LAST(trx->trx_savepoints, savep);
528
529 return (DB_SUCCESS);
530 }
531
532 /** Releases only the named savepoint. Savepoints which were set after this
533 savepoint are left as is.
534 @return if no savepoint of the name found then DB_NO_SAVEPOINT,
535 otherwise DB_SUCCESS */
536 dberr_t trx_release_savepoint_for_mysql(
537 trx_t *trx, /*!< in: transaction handle */
538 const char *savepoint_name) /*!< in: savepoint name */
539 {
540 trx_named_savept_t *savep;
541
542 ut_ad(trx_state_eq(trx, TRX_STATE_ACTIVE));
543 ut_ad(trx->in_mysql_trx_list);
544
545 savep = trx_savepoint_find(trx, savepoint_name);
546
547 if (savep != nullptr) {
548 trx_roll_savepoint_free(trx, savep);
549 }
550
551 return (savep != nullptr ? DB_SUCCESS : DB_NO_SAVEPOINT);
552 }
553
554 /** Determines if this transaction is rolling back an incomplete transaction
555 in crash recovery.
556 @return true if trx is an incomplete transaction that is being rolled
557 back in crash recovery */
558 bool trx_is_recv(const trx_t *trx) /*!< in: transaction */
559 {
560 return (trx == trx_roll_crash_recv_trx);
561 }
562
563 /** Returns a transaction savepoint taken at this point in time.
564 @return savepoint */
565 trx_savept_t trx_savept_take(trx_t *trx) /*!< in: transaction */
566 {
567 trx_savept_t savept;
568
569 savept.least_undo_no = trx->undo_no;
570
571 return (savept);
572 }
573
574 /** Roll back an active transaction. */
575 static void trx_rollback_active(trx_t *trx) /*!< in/out: transaction */
576 {
577 mem_heap_t *heap;
578 que_fork_t *fork;
579 que_thr_t *thr;
580 roll_node_t *roll_node;
581 int64_t rows_to_undo;
582 const char *unit = "";
583
584 heap = mem_heap_create(512, UT_LOCATION_HERE);
585
586 fork = que_fork_create(nullptr, nullptr, QUE_FORK_RECOVERY, heap);
587 fork->trx = trx;
588
589 thr = que_thr_create(fork, heap, nullptr);
590
591 roll_node = roll_node_create(heap);
592
593 thr->child = roll_node;
594 roll_node->common.parent = thr;
595
596 trx->graph = fork;
597
598 ut_a(thr == que_fork_start_command(fork));
599
600 trx_sys_mutex_enter();
601
602 trx_roll_crash_recv_trx = trx;
603
604 trx_roll_max_undo_no = trx->undo_no;
605
606 trx_roll_progress_printed_pct = 0;
607
608 rows_to_undo = trx_roll_max_undo_no;
609
610 trx_sys_mutex_exit();
611
612 if (rows_to_undo > 1000000000) {
613 rows_to_undo = rows_to_undo / 1000000;
614 unit = "M";
615 }
616
617 const trx_id_t trx_id = trx_get_id_for_print(trx);
618
619 ib::info(ER_IB_MSG_1186) << "Rolling back trx with id " << trx_id << ", "
620 << rows_to_undo << unit << " rows to undo";
621
622 que_run_threads(thr);
623 ut_a(roll_node->undo_thr != nullptr);
624
625 que_run_threads(roll_node->undo_thr);
626
627 trx_rollback_finish(thr_get_trx(roll_node->undo_thr));
628
629 /* Free the memory reserved by the undo graph */
630 que_graph_free(static_cast<que_t *>(roll_node->undo_thr->common.parent));
631
632 ut_a(trx->lock.que_state == TRX_QUE_RUNNING);
633
634 ib::info(ER_IB_MSG_1187) << "Rollback of trx with id " << trx_id
635 << " completed";
636
637 mem_heap_free(heap);
638
639 trx_roll_crash_recv_trx = nullptr;
640 }
641
642 /** Rollback or clean up any resurrected incomplete transactions. It assumes
643 that the caller holds the trx_sys_t::mutex and it will release the
644 lock if it does a clean up or rollback.
645 @return true if the transaction was cleaned up or rolled back
646 and trx_sys->mutex was released. */
647 static bool trx_rollback_or_clean_resurrected(
648 trx_t *trx, /*!< in: transaction to rollback or clean */
649 bool all) /*!< in: false=roll back dictionary transactions;
650 true=roll back all non-PREPARED transactions */
651 {
652 ut_ad(trx_sys_mutex_own());
653 ut_ad(trx->in_rw_trx_list);
654
655 /* Generally, an HA transaction with is_recovered && state==TRX_STATE_PREPARED
656 can be committed or rolled back by a client who knows its XID at any time.
657 To prove that no such state transition is possible while our thread operates,
658 observe that we hold trx_sys->mutex which is required by both commit and
659 rollback to deregister the trx from trx_sys->rw_trx_list during
660 trx_release_impl_and_expl_locks() and we see the trx is still in this list.
661 Thus, if we see is_recovered==true, then the state can not change until we
662 release the trx_sys->mutex. Moreover for TRX_STATE_PREPARED we do nothing, so
663 we will not interfere with an HA COMMIT or ROLLBACK. So, if XA ROLLBACK or
664 COMMIT latches trx_sys->mutex before us, then we will not see the trx in the
665 rw_trx_list (so trx_rollback_or_clean_resurrected() would not be called for
666 this transaction in the first place), and if we latch first, then we will
667 leave the trx intact. */
668
669 trx_mutex_enter(trx);
670 const bool is_recovered = trx->is_recovered;
671 const trx_state_t state = trx->state.load(std::memory_order_relaxed);
672 trx_mutex_exit(trx);
673
674 if (!is_recovered) {
675 ut_ad(state != TRX_STATE_COMMITTED_IN_MEMORY);
676 return false;
677 }
678
679 switch (state) {
680 case TRX_STATE_COMMITTED_IN_MEMORY:
681 trx_sys_mutex_exit();
682 ib::info(ER_IB_MSG_1188)
683 << "Cleaning up trx with id " << trx_get_id_for_print(trx);
684
685 trx_cleanup_at_db_startup(trx);
686 trx_free_resurrected(trx);
687 ut_ad(!trx->is_recovered);
688 return true;
689 case TRX_STATE_ACTIVE:
690 if (all || trx->ddl_operation) {
691 trx_sys_mutex_exit();
692 trx_rollback_active(trx);
693 trx_free_for_background(trx);
694 ut_ad(!trx->is_recovered);
695 return true;
696 }
697 return false;
698 case TRX_STATE_PREPARED:
699 return false;
700 case TRX_STATE_NOT_STARTED:
701 case TRX_STATE_FORCED_ROLLBACK:
702 break;
703 }
704
705 ut_error;
706 }
707
708 /** Rollback or clean up any incomplete transactions which were
709 encountered in crash recovery. If the transaction already was
710 committed, then we clean up a possible insert undo log. If the
711 transaction was not yet committed, then we roll it back. */
712 void trx_rollback_or_clean_recovered(
713 bool all) /*!< in: false=roll back dictionary transactions;
714 true=roll back all non-PREPARED transactions */
715 {
716 ut_ad(!srv_read_only_mode);
717
718 ut_a(srv_force_recovery < SRV_FORCE_NO_TRX_UNDO);
719 ut_ad(!all || trx_sys_need_rollback());
720
721 if (all) {
722 ib::info(ER_IB_MSG_1189) << "Starting in background the rollback"
723 " of uncommitted transactions";
724 }
725
726 /* Note: For XA recovered transactions, we rely on MySQL to
727 do rollback. They will be in TRX_STATE_PREPARED state. If the server
728 is shutdown and they are still lingering in trx_sys_t::trx_list
729 then the shutdown will hang. */
730
731 /* Loop over the transaction list as long as there are
732 recovered transactions to clean up or recover. */
733
734 trx_sys_mutex_enter();
735 for (bool need_one_more_scan = true; need_one_more_scan;) {
736 need_one_more_scan = false;
737 for (auto trx : trx_sys->rw_trx_list) {
738 assert_trx_in_rw_list(trx);
739
740 /* In case of slow shutdown, we have to wait for the background
741 thread (trx_recovery_rollback) which is doing the rollbacks of
742 recovered transactions. Note that it can add undo to purge.
743 In case of fast shutdown we do not care if we left transactions
744 not rolled back. But still we want to stop the thread, so since
745 certain point of shutdown we might be sure there are no changes
746 to transactions / undo. */
747 if (srv_shutdown_state.load() >= SRV_SHUTDOWN_RECOVERY_ROLLBACK &&
748 srv_fast_shutdown != 0) {
749 ut_a(srv_shutdown_state_matches([](auto state) {
750 return state == SRV_SHUTDOWN_RECOVERY_ROLLBACK ||
751 state == SRV_SHUTDOWN_EXIT_THREADS;
752 }));
753
754 trx_sys_mutex_exit();
755
756 if (all) {
757 ib::info(ER_IB_MSG_TRX_RECOVERY_ROLLBACK_NOT_COMPLETED);
758 }
759 return;
760 }
761
762 /* If this function does a cleanup or rollback
763 then it will release the trx_sys->mutex, therefore
764 we need to reacquire it before retrying the loop. */
765 if (trx_rollback_or_clean_resurrected(trx, all)) {
766 trx_sys_mutex_enter();
767 need_one_more_scan = true;
768 break;
769 }
770 }
771 }
772 trx_sys_mutex_exit();
773
774 if (all) {
775 ib::info(ER_IB_MSG_TRX_RECOVERY_ROLLBACK_COMPLETED);
776 }
777 }
778
779 /** Rollback or clean up any incomplete transactions which were
780 encountered in crash recovery. If the transaction already was
781 committed, then we clean up a possible insert undo log. If the
782 transaction was not yet committed, then we roll it back.
783 Note: this is done in a background thread. */
784 void trx_recovery_rollback(THD *thd) {
785 std::vector<MDL_ticket *> shared_mdl_list;
786 ut_ad(!srv_read_only_mode);
787
788 // Take MDL locks
789 /* During this stage the server is not open for external connections, and
790 * there will not be any concurrent threads requesting MDL, hence we don't
791 * have risk of hitting a deadlock.
792 * TODO: We can improvize code by following some order while taking MDL locks.
793 */
794 for (const auto &element : to_rollback_trx_tables) {
795 trx_id_t trx_id = element.first;
796 table_id_t table_id = element.second;
797
798 /* Passing false as we only intend to validate if the transaction is already
799 * committed/rolled back during other stages of recovery. */
800 auto trx = trx_rw_is_active(trx_id, false);
801
802 /* Ignore transactions that has already finished. */
803 if (trx == nullptr) {
804 /* Currently these recovered transactions are not expected to finish
805 * earlier. Assert in Debug mode. */
806 ut_ad(false);
807 continue;
808 }
809 auto table = dd_table_open_on_id(table_id, nullptr, nullptr, false, true);
810 if (table == nullptr) {
811 continue;
812 }
813 std::string table_name;
814 std::string schema_name;
815 table->get_table_name(schema_name, table_name);
816 MDL_ticket *mdl_ticket;
817 if (dd_mdl_acquire(thd, &mdl_ticket, schema_name.data(),
818 table_name.data())) {
819 ut_error;
820 }
821 shared_mdl_list.push_back(mdl_ticket);
822 lock_table_ix_resurrect(table, trx);
823 ib::info(ER_IB_RESURRECT_ACQUIRE_TABLE_LOCK, ulong(table->id),
824 table->name.m_name);
825 dd_table_close(table, nullptr, nullptr, false);
826 }
827
828 /* Let the startup thread proceed now */
829 os_event_set(recovery_lock_taken);
830
831 while (DBUG_EVALUATE_IF("pause_rollback_on_recovery", true, false)) {
832 if (srv_shutdown_state.load() >= SRV_SHUTDOWN_RECOVERY_ROLLBACK) {
833 break;
834 }
835 std::this_thread::sleep_for(std::chrono::milliseconds(1));
836 }
837
838 trx_rollback_or_clean_recovered(true);
839
840 // Release MDL locks
841 for (auto mdl_ticket : shared_mdl_list) {
842 dd_release_mdl(mdl_ticket);
843 }
844 }
845
846 /** Rollback or clean up any incomplete transactions which were
847 encountered in crash recovery. If the transaction already was
848 committed, then we clean up a possible insert undo log. If the
849 transaction was not yet committed, then we roll it back.
850 Note: this is done in a background thread. */
851 void trx_recovery_rollback_thread() {
852 THD *thd = create_internal_thd();
853
854 trx_recovery_rollback(thd);
855
856 destroy_internal_thd(thd);
857 }
858
859 /** Tries truncate the undo logs. */
860 static void trx_roll_try_truncate(
861 trx_t *trx, /*!< in/out: transaction */
862 trx_undo_ptr_t *undo_ptr) /*!< in: rollback segment to look
863 for next undo log record. */
864 {
865 ut_ad(mutex_own(&trx->undo_mutex));
866 ut_ad(mutex_own(&undo_ptr->rseg->mutex));
867
868 trx->pages_undone = 0;
869
870 if (undo_ptr->insert_undo) {
871 trx_undo_truncate_end(trx, undo_ptr->insert_undo, trx->undo_no);
872 }
873
874 if (undo_ptr->update_undo) {
875 trx_undo_truncate_end(trx, undo_ptr->update_undo, trx->undo_no);
876 }
877 }
878
879 /** Pops the topmost undo log record in a single undo log and updates the info
880 about the topmost record in the undo log memory struct.
881 @param[in] trx transaction
882 @param[in] undo undo log
883 @param[in] mtr mtr
884 @param[out] undo_offset offset of undo record in the page
885 @return Undo page where undo log record resides, the page s-latched */
886 static const page_t *trx_roll_pop_top_rec(trx_t *trx, trx_undo_t *undo,
887 mtr_t *mtr, uint32_t *undo_offset) {
888 ut_ad(mutex_own(&trx->undo_mutex));
889
890 const page_t *undo_page = trx_undo_page_get_s_latched(
891 page_id_t(undo->space, undo->top_page_no), undo->page_size, mtr);
892
893 *undo_offset = static_cast<uint32_t>(undo->top_offset);
894
895 trx_undo_rec_t *prev_rec =
896 trx_undo_get_prev_rec((trx_undo_rec_t *)(undo_page + *undo_offset),
897 undo->hdr_page_no, undo->hdr_offset, true, mtr);
898
899 if (prev_rec == nullptr) {
900 undo->empty = true;
901 } else {
902 page_t *prev_rec_page = page_align(prev_rec);
903
904 if (prev_rec_page != undo_page) {
905 trx->pages_undone++;
906 }
907
908 undo->top_page_no = page_get_page_no(prev_rec_page);
909 undo->top_offset = prev_rec - prev_rec_page;
910 undo->top_undo_no = trx_undo_rec_get_undo_no(prev_rec);
911 }
912
913 return (undo_page);
914 }
915
916 /** Pops the topmost record when the two undo logs of a transaction are seen
917 as a single stack of records ordered by their undo numbers.
918 @return undo log record copied to heap, NULL if none left, or if the
919 undo number of the top record would be less than the limit */
920 static trx_undo_rec_t *trx_roll_pop_top_rec_of_trx_low(
921 trx_t *trx, /*!< in/out: transaction */
922 trx_undo_ptr_t *undo_ptr, /*!< in: rollback segment to look
923 for next undo log record. */
924 undo_no_t limit, /*!< in: least undo number we need */
925 roll_ptr_t *roll_ptr, /*!< out: roll pointer to undo record */
926 mem_heap_t *heap) /*!< in/out: memory heap where copied */
927 {
928 trx_undo_t *undo;
929 trx_undo_t *ins_undo;
930 trx_undo_t *upd_undo;
931 trx_undo_rec_t *undo_rec_copy;
932 const page_t *undo_page;
933 undo_no_t undo_no;
934 uint32_t undo_offset;
935 trx_rseg_t *rseg;
936 mtr_t mtr;
937
938 rseg = undo_ptr->rseg;
939
940 mutex_enter(&trx->undo_mutex);
941
942 if (trx->pages_undone >= TRX_ROLL_TRUNC_THRESHOLD) {
943 rseg->latch();
944
945 trx_roll_try_truncate(trx, undo_ptr);
946
947 rseg->unlatch();
948 }
949
950 ins_undo = undo_ptr->insert_undo;
951 upd_undo = undo_ptr->update_undo;
952
953 if (!ins_undo || ins_undo->empty) {
954 undo = upd_undo;
955 } else if (!upd_undo || upd_undo->empty) {
956 undo = ins_undo;
957 } else if (upd_undo->top_undo_no > ins_undo->top_undo_no) {
958 undo = upd_undo;
959 } else {
960 undo = ins_undo;
961 }
962
963 if (!undo || undo->empty || limit > undo->top_undo_no) {
964 rseg->latch();
965 trx_roll_try_truncate(trx, undo_ptr);
966 rseg->unlatch();
967 mutex_exit(&trx->undo_mutex);
968 return (nullptr);
969 }
970
971 auto is_insert = (undo == ins_undo);
972
973 *roll_ptr = trx_undo_build_roll_ptr(is_insert, undo->rseg->space_id,
974 undo->top_page_no, undo->top_offset);
975
976 mtr_start(&mtr);
977
978 undo_page = trx_roll_pop_top_rec(trx, undo, &mtr, &undo_offset);
979
980 undo_no = trx_undo_rec_get_undo_no(undo_page + undo_offset);
981
982 ut_ad(trx_roll_check_undo_rec_ordering(undo_no, undo->rseg->space_id, trx));
983
984 /* We print rollback progress info if we are in a crash recovery
985 and the transaction has at least 1000 row operations to undo. */
986
987 if (trx == trx_roll_crash_recv_trx && trx_roll_max_undo_no > 1000) {
988 ulint progress_pct = 100 - (ulint)((undo_no * 100) / trx_roll_max_undo_no);
989 if (progress_pct != trx_roll_progress_printed_pct) {
990 if (trx_roll_progress_printed_pct == 0) {
991 fprintf(stderr,
992 "\nInnoDB: Progress in percents:"
993 " %lu",
994 (ulong)progress_pct);
995 } else {
996 fprintf(stderr, " %lu", (ulong)progress_pct);
997 }
998 fflush(stderr);
999 trx_roll_progress_printed_pct = progress_pct;
1000 }
1001 }
1002
1003 trx->undo_no = undo_no;
1004 trx->undo_rseg_space = undo->rseg->space_id;
1005
1006 undo_rec_copy =
1007 trx_undo_rec_copy(undo_page, static_cast<uint32_t>(undo_offset), heap);
1008
1009 mutex_exit(&trx->undo_mutex);
1010
1011 mtr_commit(&mtr);
1012
1013 return (undo_rec_copy);
1014 }
1015
1016 /** Get next undo log record from redo and noredo rollback segments.
1017 @return undo log record copied to heap, NULL if none left, or if the
1018 undo number of the top record would be less than the limit */
1019 trx_undo_rec_t *trx_roll_pop_top_rec_of_trx(
1020 trx_t *trx, /*!< in: transaction */
1021 undo_no_t limit, /*!< in: least undo number we need */
1022 roll_ptr_t *roll_ptr, /*!< out: roll pointer to undo record */
1023 mem_heap_t *heap) /*!< in: memory heap where copied */
1024 {
1025 trx_undo_rec_t *undo_rec = nullptr;
1026
1027 if (trx_is_redo_rseg_updated(trx)) {
1028 undo_rec = trx_roll_pop_top_rec_of_trx_low(trx, &trx->rsegs.m_redo, limit,
1029 roll_ptr, heap);
1030 }
1031
1032 if (undo_rec == nullptr && trx_is_temp_rseg_updated(trx)) {
1033 undo_rec = trx_roll_pop_top_rec_of_trx_low(trx, &trx->rsegs.m_noredo, limit,
1034 roll_ptr, heap);
1035 }
1036
1037 return (undo_rec);
1038 }
1039
1040 /** Builds an undo 'query' graph for a transaction. The actual rollback is
1041 performed by executing this query graph like a query subprocedure call.
1042 The reply about the completion of the rollback will be sent by this
1043 graph.
1044 @param[in,out] trx transaction
1045 @param[in] partial_rollback true if partial rollback
1046 @return the query graph */
1047 static que_t *trx_roll_graph_build(trx_t *trx, bool partial_rollback) {
1048 mem_heap_t *heap;
1049 que_fork_t *fork;
1050 que_thr_t *thr;
1051
1052 ut_ad(trx_mutex_own(trx));
1053
1054 heap = mem_heap_create(512, UT_LOCATION_HERE);
1055 fork = que_fork_create(nullptr, nullptr, QUE_FORK_ROLLBACK, heap);
1056 fork->trx = trx;
1057
1058 thr = que_thr_create(fork, heap, nullptr);
1059
1060 thr->child = row_undo_node_create(trx, thr, heap, partial_rollback);
1061
1062 return (fork);
1063 }
1064
1065 /** Starts a rollback operation, creates the UNDO graph that will do the
1066 actual undo operation.
1067 @param[in] trx transaction
1068 @param[in] roll_limit rollback to undo no (for
1069 partial undo), 0 if we are rolling back
1070 the entire transaction
1071 @param[in] partial_rollback true if partial rollback
1072 @return query graph thread that will perform the UNDO operations. */
1073 static que_thr_t *trx_rollback_start(trx_t *trx, ib_id_t roll_limit,
1074 bool partial_rollback) {
1075 ut_ad(trx_mutex_own(trx));
1076
1077 /* Initialize the rollback field in the transaction */
1078
1079 ut_ad(!trx->roll_limit);
1080 ut_ad(!trx->in_rollback);
1081
1082 trx->roll_limit = roll_limit;
1083 ut_d(trx->in_rollback = true);
1084
1085 ut_a(trx->roll_limit <= trx->undo_no);
1086
1087 trx->pages_undone = 0;
1088
1089 /* Build a 'query' graph which will perform the undo operations */
1090
1091 que_t *roll_graph = trx_roll_graph_build(trx, partial_rollback);
1092
1093 trx->graph = roll_graph;
1094
1095 trx->lock.que_state = TRX_QUE_ROLLING_BACK;
1096
1097 return (que_fork_start_command(roll_graph));
1098 }
1099
1100 /** Finishes a transaction rollback. */
1101 static void trx_rollback_finish(trx_t *trx) /*!< in: transaction */
1102 {
1103 trx_commit(trx);
1104
1105 trx->mod_tables.clear();
1106
1107 trx->lock.que_state = TRX_QUE_RUNNING;
1108 }
1109
1110 /** Creates a rollback command node struct.
1111 @return own: rollback node struct */
1112 roll_node_t *roll_node_create(
1113 mem_heap_t *heap) /*!< in: mem heap where created */
1114 {
1115 roll_node_t *node;
1116
1117 node = static_cast<roll_node_t *>(mem_heap_zalloc(heap, sizeof(*node)));
1118
1119 node->state = ROLL_NODE_SEND;
1120
1121 node->common.type = QUE_NODE_ROLLBACK;
1122
1123 return (node);
1124 }
1125
1126 /** Performs an execution step for a rollback command node in a query graph.
1127 @return query thread to run next, or NULL */
1128 que_thr_t *trx_rollback_step(que_thr_t *thr) /*!< in: query thread */
1129 {
1130 roll_node_t *node;
1131
1132 node = static_cast<roll_node_t *>(thr->run_node);
1133
1134 ut_ad(que_node_get_type(node) == QUE_NODE_ROLLBACK);
1135
1136 if (thr->prev_node == que_node_get_parent(node)) {
1137 node->state = ROLL_NODE_SEND;
1138 }
1139
1140 if (node->state == ROLL_NODE_SEND) {
1141 trx_t *trx;
1142 ib_id_t roll_limit;
1143
1144 trx = thr_get_trx(thr);
1145
1146 trx_mutex_enter(trx);
1147
1148 node->state = ROLL_NODE_WAIT;
1149
1150 ut_a(node->undo_thr == nullptr);
1151
1152 roll_limit = node->partial ? node->savept.least_undo_no : 0;
1153
1154 trx_commit_or_rollback_prepare(trx);
1155
1156 node->undo_thr = trx_rollback_start(trx, roll_limit, node->partial);
1157
1158 trx_mutex_exit(trx);
1159
1160 } else {
1161 ut_ad(node->state == ROLL_NODE_WAIT);
1162
1163 thr->run_node = que_node_get_parent(node);
1164 }
1165
1166 return (thr);
1167 }