trx0roll.c 31.6 KB
Newer Older
Vadim Tkachenko's avatar
Vadim Tkachenko committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*****************************************************************************

Copyright (c) 1996, 2009, Innobase Oy. All Rights Reserved.

This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation; version 2 of the License.

This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with
this program; if not, write to the Free Software Foundation, Inc., 59 Temple
Place, Suite 330, Boston, MA 02111-1307 USA

*****************************************************************************/

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
19 20
/**************************************************//**
@file trx/trx0roll.c
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
Transaction rollback

Created 3/26/1996 Heikki Tuuri
*******************************************************/

#include "trx0roll.h"

#ifdef UNIV_NONINL
#include "trx0roll.ic"
#endif

#include "fsp0fsp.h"
#include "mach0data.h"
#include "trx0rseg.h"
#include "trx0trx.h"
#include "trx0undo.h"
#include "trx0rec.h"
#include "que0que.h"
#include "usr0sess.h"
#include "srv0que.h"
#include "srv0start.h"
#include "row0undo.h"
#include "row0mysql.h"
#include "lock0lock.h"
#include "pars0pars.h"

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
47 48
/** This many pages must be undone before a truncate is tried within
rollback */
49 50
#define TRX_ROLL_TRUNC_THRESHOLD	1

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
51
/** In crash recovery, the current trx to be rolled back */
52 53
static trx_t*		trx_roll_crash_recv_trx	= NULL;

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
54
/** In crash recovery we set this to the undo n:o of the current trx to be
55 56 57
rolled back. Then we can print how many % the rollback has progressed. */
static ib_int64_t	trx_roll_max_undo_no;

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
58
/** Auxiliary variable which tells the previous progress % we printed */
59 60
static ulint		trx_roll_progress_printed_pct;

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
61 62 63
/*******************************************************************//**
Rollback a transaction used in MySQL.
@return	error code or DB_SUCCESS */
64 65 66 67
UNIV_INTERN
int
trx_general_rollback_for_mysql(
/*===========================*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
68 69 70
	trx_t*		trx,	/*!< in: transaction handle */
	ibool		partial,/*!< in: TRUE if partial rollback requested */
	trx_savept_t*	savept)	/*!< in: pointer to savepoint undo number, if
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
				partial rollback requested */
{
	mem_heap_t*	heap;
	que_thr_t*	thr;
	roll_node_t*	roll_node;

	/* Tell Innobase server that there might be work for
	utility threads: */

	srv_active_wake_master_thread();

	trx_start_if_not_started(trx);

	heap = mem_heap_create(512);

	roll_node = roll_node_create(heap);

	roll_node->partial = partial;

	if (partial) {
		roll_node->savept = *savept;
	}

	trx->error_state = DB_SUCCESS;

	thr = pars_complete_graph_for_exec(roll_node, trx, heap);

	ut_a(thr == que_fork_start_command(que_node_get_parent(thr)));
	que_run_threads(thr);

	mutex_enter(&kernel_mutex);

	while (trx->que_state != TRX_QUE_RUNNING) {

		mutex_exit(&kernel_mutex);

		os_thread_sleep(100000);

		mutex_enter(&kernel_mutex);
	}

	mutex_exit(&kernel_mutex);

	mem_heap_free(heap);

	ut_a(trx->error_state == DB_SUCCESS);

	/* Tell Innobase server that there might be work for
	utility threads: */

	srv_active_wake_master_thread();

	return((int) trx->error_state);
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
126 127 128
/*******************************************************************//**
Rollback a transaction used in MySQL.
@return	error code or DB_SUCCESS */
129 130 131 132
UNIV_INTERN
int
trx_rollback_for_mysql(
/*===================*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
133
	trx_t*	trx)	/*!< in: transaction handle */
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
{
	int	err;

	if (trx->conc_state == TRX_NOT_STARTED) {

		return(DB_SUCCESS);
	}

	trx->op_info = "rollback";

	/* If we are doing the XA recovery of prepared transactions, then
	the transaction object does not have an InnoDB session object, and we
	set a dummy session that we use for all MySQL transactions. */

	err = trx_general_rollback_for_mysql(trx, FALSE, NULL);

	trx->op_info = "";

	return(err);
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
155 156 157
/*******************************************************************//**
Rollback the latest SQL statement for MySQL.
@return	error code or DB_SUCCESS */
158 159 160 161
UNIV_INTERN
int
trx_rollback_last_sql_stat_for_mysql(
/*=================================*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
162
	trx_t*	trx)	/*!< in: transaction handle */
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
{
	int	err;

	if (trx->conc_state == TRX_NOT_STARTED) {

		return(DB_SUCCESS);
	}

	trx->op_info = "rollback of SQL statement";

	err = trx_general_rollback_for_mysql(trx, TRUE,
					     &(trx->last_sql_stat_start));
	/* The following call should not be needed, but we play safe: */
	trx_mark_sql_stat_end(trx);

	trx->op_info = "";

	return(err);
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
183
/*******************************************************************//**
Vadim Tkachenko's avatar
Vadim Tkachenko committed
184
Frees a single savepoint struct. */
185
UNIV_INTERN
Vadim Tkachenko's avatar
Vadim Tkachenko committed
186 187 188
void
trx_roll_savepoint_free(
/*=====================*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
189 190
	trx_t*			trx,	/*!< in: transaction handle */
	trx_named_savept_t*	savep)	/*!< in: savepoint to free */
Vadim Tkachenko's avatar
Vadim Tkachenko committed
191 192 193 194 195 196 197 198 199
{
	ut_a(savep != NULL);
	ut_a(UT_LIST_GET_LEN(trx->trx_savepoints) > 0);

	UT_LIST_REMOVE(trx_savepoints, trx->trx_savepoints, savep);
	mem_free(savep->name);
	mem_free(savep);
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
200
/*******************************************************************//**
Vadim Tkachenko's avatar
Vadim Tkachenko committed
201 202
Frees savepoint structs starting from savep, if savep == NULL then
free all savepoints. */
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
203
UNIV_INTERN
204 205 206
void
trx_roll_savepoints_free(
/*=====================*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
207 208
	trx_t*			trx,	/*!< in: transaction handle */
	trx_named_savept_t*	savep)	/*!< in: free all savepoints > this one;
209 210 211 212 213 214 215 216 217 218 219 220 221 222
					if this is NULL, free all savepoints
					of trx */
{
	trx_named_savept_t*	next_savep;

	if (savep == NULL) {
		savep = UT_LIST_GET_FIRST(trx->trx_savepoints);
	} else {
		savep = UT_LIST_GET_NEXT(trx_savepoints, savep);
	}

	while (savep != NULL) {
		next_savep = UT_LIST_GET_NEXT(trx_savepoints, savep);

Vadim Tkachenko's avatar
Vadim Tkachenko committed
223
		trx_roll_savepoint_free(trx, savep);
224 225 226 227 228

		savep = next_savep;
	}
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
229
/*******************************************************************//**
230 231 232 233 234
Rolls back a transaction back to a named savepoint. Modifications after the
savepoint are undone but InnoDB does NOT release the corresponding locks
which are stored in memory. If a lock is 'implicit', that is, a new inserted
row holds a lock where the lock information is carried by the trx id stored in
the row, these locks are naturally released in the rollback. Savepoints which
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
235 236 237
were set after this savepoint are deleted.
@return if no savepoint of the name found then DB_NO_SAVEPOINT,
otherwise DB_SUCCESS */
238 239 240 241
UNIV_INTERN
ulint
trx_rollback_to_savepoint_for_mysql(
/*================================*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
242 243 244
	trx_t*		trx,			/*!< in: transaction handle */
	const char*	savepoint_name,		/*!< in: savepoint name */
	ib_int64_t*	mysql_binlog_cache_pos)	/*!< out: the MySQL binlog cache
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
						position corresponding to this
						savepoint; MySQL needs this
						information to remove the
						binlog entries of the queries
						executed after the savepoint */
{
	trx_named_savept_t*	savep;
	ulint			err;

	savep = UT_LIST_GET_FIRST(trx->trx_savepoints);

	while (savep != NULL) {
		if (0 == ut_strcmp(savep->name, savepoint_name)) {
			/* Found */
			break;
		}
		savep = UT_LIST_GET_NEXT(trx_savepoints, savep);
	}

	if (savep == NULL) {

		return(DB_NO_SAVEPOINT);
	}

	if (trx->conc_state == TRX_NOT_STARTED) {
		ut_print_timestamp(stderr);
		fputs("  InnoDB: Error: transaction has a savepoint ", stderr);
		ut_print_name(stderr, trx, FALSE, savep->name);
		fputs(" though it is not started\n", stderr);
		return(DB_ERROR);
	}

	/* We can now free all savepoints strictly later than this one */

	trx_roll_savepoints_free(trx, savep);

	*mysql_binlog_cache_pos = savep->mysql_binlog_cache_pos;

	trx->op_info = "rollback to a savepoint";

	err = trx_general_rollback_for_mysql(trx, TRUE, &(savep->savept));

	/* Store the current undo_no of the transaction so that we know where
	to roll back if we have to roll back the next SQL statement: */

	trx_mark_sql_stat_end(trx);

	trx->op_info = "";

	return(err);
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
297
/*******************************************************************//**
298 299 300
Creates a named savepoint. If the transaction is not yet started, starts it.
If there is already a savepoint of the same name, this call erases that old
savepoint and replaces it with a new. Savepoints are deleted in a transaction
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
301 302
commit or rollback.
@return	always DB_SUCCESS */
303 304 305 306
UNIV_INTERN
ulint
trx_savepoint_for_mysql(
/*====================*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
307 308 309
	trx_t*		trx,			/*!< in: transaction handle */
	const char*	savepoint_name,		/*!< in: savepoint name */
	ib_int64_t	binlog_cache_pos)	/*!< in: MySQL binlog cache
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
						position corresponding to this
						connection at the time of the
						savepoint */
{
	trx_named_savept_t*	savep;

	ut_a(trx);
	ut_a(savepoint_name);

	trx_start_if_not_started(trx);

	savep = UT_LIST_GET_FIRST(trx->trx_savepoints);

	while (savep != NULL) {
		if (0 == ut_strcmp(savep->name, savepoint_name)) {
			/* Found */
			break;
		}
		savep = UT_LIST_GET_NEXT(trx_savepoints, savep);
	}

	if (savep) {
		/* There is a savepoint with the same name: free that */

		UT_LIST_REMOVE(trx_savepoints, trx->trx_savepoints, savep);

		mem_free(savep->name);
		mem_free(savep);
	}

	/* Create a new savepoint and add it as the last in the list */

	savep = mem_alloc(sizeof(trx_named_savept_t));

	savep->name = mem_strdup(savepoint_name);

	savep->savept = trx_savept_take(trx);

	savep->mysql_binlog_cache_pos = binlog_cache_pos;

	UT_LIST_ADD_LAST(trx_savepoints, trx->trx_savepoints, savep);

	return(DB_SUCCESS);
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
355
/*******************************************************************//**
Vadim Tkachenko's avatar
Vadim Tkachenko committed
356
Releases only the named savepoint. Savepoints which were set after this
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
357 358 359
savepoint are left as is.
@return if no savepoint of the name found then DB_NO_SAVEPOINT,
otherwise DB_SUCCESS */
360 361 362 363
UNIV_INTERN
ulint
trx_release_savepoint_for_mysql(
/*============================*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
364 365
	trx_t*		trx,			/*!< in: transaction handle */
	const char*	savepoint_name)		/*!< in: savepoint name */
366 367 368 369 370
{
	trx_named_savept_t*	savep;

	savep = UT_LIST_GET_FIRST(trx->trx_savepoints);

Vadim Tkachenko's avatar
Vadim Tkachenko committed
371
	/* Search for the savepoint by name and free if found. */
372 373
	while (savep != NULL) {
		if (0 == ut_strcmp(savep->name, savepoint_name)) {
Vadim Tkachenko's avatar
Vadim Tkachenko committed
374 375
			trx_roll_savepoint_free(trx, savep);
			return(DB_SUCCESS);
376 377 378 379
		}
		savep = UT_LIST_GET_NEXT(trx_savepoints, savep);
	}

Vadim Tkachenko's avatar
Vadim Tkachenko committed
380
	return(DB_NO_SAVEPOINT);
381 382
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
383
/*******************************************************************//**
Vadim Tkachenko's avatar
Vadim Tkachenko committed
384
Determines if this transaction is rolling back an incomplete transaction
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
385 386 387
in crash recovery.
@return TRUE if trx is an incomplete transaction that is being rolled
back in crash recovery */
Vadim Tkachenko's avatar
Vadim Tkachenko committed
388 389 390 391
UNIV_INTERN
ibool
trx_is_recv(
/*========*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
392
	const trx_t*	trx)	/*!< in: transaction */
Vadim Tkachenko's avatar
Vadim Tkachenko committed
393 394 395 396
{
	return(trx == trx_roll_crash_recv_trx);
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
397 398 399
/*******************************************************************//**
Returns a transaction savepoint taken at this point in time.
@return	savepoint */
400 401 402 403
UNIV_INTERN
trx_savept_t
trx_savept_take(
/*============*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
404
	trx_t*	trx)	/*!< in: transaction */
405 406 407 408 409 410 411 412
{
	trx_savept_t	savept;

	savept.least_undo_no = trx->undo_no;

	return(savept);
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
413
/*******************************************************************//**
414 415 416 417 418
Roll back an active transaction. */
static
void
trx_rollback_active(
/*================*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
419
	trx_t*	trx)	/*!< in/out: transaction */
420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514
{
	mem_heap_t*	heap;
	que_fork_t*	fork;
	que_thr_t*	thr;
	roll_node_t*	roll_node;
	dict_table_t*	table;
	ib_int64_t	rows_to_undo;
	const char*	unit		= "";
	ibool		dictionary_locked = FALSE;

	heap = mem_heap_create(512);

	fork = que_fork_create(NULL, NULL, QUE_FORK_RECOVERY, heap);
	fork->trx = trx;

	thr = que_thr_create(fork, heap);

	roll_node = roll_node_create(heap);

	thr->child = roll_node;
	roll_node->common.parent = thr;

	mutex_enter(&kernel_mutex);

	trx->graph = fork;

	ut_a(thr == que_fork_start_command(fork));

	trx_roll_crash_recv_trx	= trx;
	trx_roll_max_undo_no = ut_conv_dulint_to_longlong(trx->undo_no);
	trx_roll_progress_printed_pct = 0;
	rows_to_undo = trx_roll_max_undo_no;

	if (rows_to_undo > 1000000000) {
		rows_to_undo = rows_to_undo / 1000000;
		unit = "M";
	}

	ut_print_timestamp(stderr);
	fprintf(stderr,
		"  InnoDB: Rolling back trx with id " TRX_ID_FMT ", %lu%s"
		" rows to undo\n",
		TRX_ID_PREP_PRINTF(trx->id),
		(ulong) rows_to_undo, unit);
	mutex_exit(&kernel_mutex);

	trx->mysql_thread_id = os_thread_get_curr_id();

	trx->mysql_process_no = os_proc_get_number();

	if (trx_get_dict_operation(trx) != TRX_DICT_OP_NONE) {
		row_mysql_lock_data_dictionary(trx);
		dictionary_locked = TRUE;
	}

	que_run_threads(thr);

	mutex_enter(&kernel_mutex);

	while (trx->que_state != TRX_QUE_RUNNING) {

		mutex_exit(&kernel_mutex);

		fprintf(stderr,
			"InnoDB: Waiting for rollback of trx id %lu to end\n",
			(ulong) ut_dulint_get_low(trx->id));
		os_thread_sleep(100000);

		mutex_enter(&kernel_mutex);
	}

	mutex_exit(&kernel_mutex);

	if (trx_get_dict_operation(trx) != TRX_DICT_OP_NONE
	    && !ut_dulint_is_zero(trx->table_id)) {

		/* If the transaction was for a dictionary operation, we
		drop the relevant table, if it still exists */

		fprintf(stderr,
			"InnoDB: Dropping table with id %lu %lu"
			" in recovery if it exists\n",
			(ulong) ut_dulint_get_high(trx->table_id),
			(ulong) ut_dulint_get_low(trx->table_id));

		table = dict_table_get_on_id_low(trx->table_id);

		if (table) {
			ulint	err;

			fputs("InnoDB: Table found: dropping table ", stderr);
			ut_print_name(stderr, trx, TRUE, table->name);
			fputs(" in recovery\n", stderr);

			err = row_drop_table_for_mysql(table->name, trx, TRUE);
Vadim Tkachenko's avatar
Vadim Tkachenko committed
515
			trx_commit_for_mysql(trx);
516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532

			ut_a(err == (int) DB_SUCCESS);
		}
	}

	if (dictionary_locked) {
		row_mysql_unlock_data_dictionary(trx);
	}

	fprintf(stderr, "\nInnoDB: Rolling back of trx id " TRX_ID_FMT
		" completed\n",
		TRX_ID_PREP_PRINTF(trx->id));
	mem_heap_free(heap);

	trx_roll_crash_recv_trx	= NULL;
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
533
/*******************************************************************//**
534 535 536 537
Rollback or clean up any incomplete transactions which were
encountered in crash recovery.  If the transaction already was
committed, then we clean up a possible insert undo log. If the
transaction was not yet committed, then we roll it back.
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
538 539
Note: this is done in a background thread.
@return	a dummy parameter */
540 541 542 543 544
UNIV_INTERN
os_thread_ret_t
trx_rollback_or_clean_all_recovered(
/*================================*/
	void*	arg __attribute__((unused)))
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
545
			/*!< in: a dummy parameter required by
546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607
			os_thread_create */
{
	trx_t*	trx;

	mutex_enter(&kernel_mutex);

	if (UT_LIST_GET_FIRST(trx_sys->trx_list)) {

		fprintf(stderr,
			"InnoDB: Starting in background the rollback"
			" of uncommitted transactions\n");
	} else {
		goto leave_function;
	}

	mutex_exit(&kernel_mutex);

loop:
	mutex_enter(&kernel_mutex);

	for (trx = UT_LIST_GET_FIRST(trx_sys->trx_list); trx;
	     trx = UT_LIST_GET_NEXT(trx_list, trx)) {
		if (!trx->is_recovered) {
			continue;
		}

		switch (trx->conc_state) {
		case TRX_NOT_STARTED:
		case TRX_PREPARED:
			continue;

		case TRX_COMMITTED_IN_MEMORY:
			mutex_exit(&kernel_mutex);
			fprintf(stderr,
				"InnoDB: Cleaning up trx with id "
				TRX_ID_FMT "\n",
				TRX_ID_PREP_PRINTF(trx->id));
			trx_cleanup_at_db_startup(trx);
			goto loop;

		case TRX_ACTIVE:
			mutex_exit(&kernel_mutex);
			trx_rollback_active(trx);
			goto loop;
		}
	}

	ut_print_timestamp(stderr);
	fprintf(stderr,
		"  InnoDB: Rollback of non-prepared transactions completed\n");

leave_function:
	mutex_exit(&kernel_mutex);

	/* We count the number of threads in os_thread_exit(). A created
	thread should always use that to exit and not use return() to exit. */

	os_thread_exit(NULL);

	OS_THREAD_DUMMY_RETURN;
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
608 609 610
/*******************************************************************//**
Creates an undo number array.
@return	own: undo number array */
611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638
UNIV_INTERN
trx_undo_arr_t*
trx_undo_arr_create(void)
/*=====================*/
{
	trx_undo_arr_t*	arr;
	mem_heap_t*	heap;
	ulint		i;

	heap = mem_heap_create(1024);

	arr = mem_heap_alloc(heap, sizeof(trx_undo_arr_t));

	arr->infos = mem_heap_alloc(heap, sizeof(trx_undo_inf_t)
				    * UNIV_MAX_PARALLELISM);
	arr->n_cells = UNIV_MAX_PARALLELISM;
	arr->n_used = 0;

	arr->heap = heap;

	for (i = 0; i < UNIV_MAX_PARALLELISM; i++) {

		(trx_undo_arr_get_nth_info(arr, i))->in_use = FALSE;
	}

	return(arr);
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
639
/*******************************************************************//**
640 641 642 643 644
Frees an undo number array. */
UNIV_INTERN
void
trx_undo_arr_free(
/*==============*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
645
	trx_undo_arr_t*	arr)	/*!< in: undo number array */
646 647 648 649 650 651
{
	ut_ad(arr->n_used == 0);

	mem_heap_free(arr->heap);
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
652 653 654
/*******************************************************************//**
Stores info of an undo log record to the array if it is not stored yet.
@return	FALSE if the record already existed in the array */
655 656 657 658
static
ibool
trx_undo_arr_store_info(
/*====================*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
659 660
	trx_t*		trx,	/*!< in: transaction */
	undo_no_t	undo_no)/*!< in: undo number */
661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712
{
	trx_undo_inf_t*	cell;
	trx_undo_inf_t*	stored_here;
	trx_undo_arr_t*	arr;
	ulint		n_used;
	ulint		n;
	ulint		i;

	n = 0;
	arr = trx->undo_no_arr;
	n_used = arr->n_used;
	stored_here = NULL;

	for (i = 0;; i++) {
		cell = trx_undo_arr_get_nth_info(arr, i);

		if (!cell->in_use) {
			if (!stored_here) {
				/* Not in use, we may store here */
				cell->undo_no = undo_no;
				cell->in_use = TRUE;

				arr->n_used++;

				stored_here = cell;
			}
		} else {
			n++;

			if (0 == ut_dulint_cmp(cell->undo_no, undo_no)) {

				if (stored_here) {
					stored_here->in_use = FALSE;
					ut_ad(arr->n_used > 0);
					arr->n_used--;
				}

				ut_ad(arr->n_used == n_used);

				return(FALSE);
			}
		}

		if (n == n_used && stored_here) {

			ut_ad(arr->n_used == 1 + n_used);

			return(TRUE);
		}
	}
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
713
/*******************************************************************//**
714 715 716 717 718
Removes an undo number from the array. */
static
void
trx_undo_arr_remove_info(
/*=====================*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
719 720
	trx_undo_arr_t*	arr,	/*!< in: undo number array */
	undo_no_t	undo_no)/*!< in: undo number */
721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746
{
	trx_undo_inf_t*	cell;
	ulint		n_used;
	ulint		n;
	ulint		i;

	n_used = arr->n_used;
	n = 0;

	for (i = 0;; i++) {
		cell = trx_undo_arr_get_nth_info(arr, i);

		if (cell->in_use
		    && 0 == ut_dulint_cmp(cell->undo_no, undo_no)) {

			cell->in_use = FALSE;

			ut_ad(arr->n_used > 0);

			arr->n_used--;

			return;
		}
	}
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
747 748 749
/*******************************************************************//**
Gets the biggest undo number in an array.
@return	biggest value, ut_dulint_zero if the array is empty */
750
static
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
751
undo_no_t
752 753
trx_undo_arr_get_biggest(
/*=====================*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
754
	trx_undo_arr_t*	arr)	/*!< in: undo number array */
755 756 757
{
	trx_undo_inf_t*	cell;
	ulint		n_used;
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
758
	undo_no_t	biggest;
759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782
	ulint		n;
	ulint		i;

	n = 0;
	n_used = arr->n_used;
	biggest = ut_dulint_zero;

	for (i = 0;; i++) {
		cell = trx_undo_arr_get_nth_info(arr, i);

		if (cell->in_use) {
			n++;
			if (ut_dulint_cmp(cell->undo_no, biggest) > 0) {

				biggest = cell->undo_no;
			}
		}

		if (n == n_used) {
			return(biggest);
		}
	}
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
783
/***********************************************************************//**
784 785 786 787 788
Tries truncate the undo logs. */
UNIV_INTERN
void
trx_roll_try_truncate(
/*==================*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
789
	trx_t*	trx)	/*!< in/out: transaction */
790 791
{
	trx_undo_arr_t*	arr;
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
792 793
	undo_no_t	limit;
	undo_no_t	biggest;
794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821

	ut_ad(mutex_own(&(trx->undo_mutex)));
	ut_ad(mutex_own(&((trx->rseg)->mutex)));

	trx->pages_undone = 0;

	arr = trx->undo_no_arr;

	limit = trx->undo_no;

	if (arr->n_used > 0) {
		biggest = trx_undo_arr_get_biggest(arr);

		if (ut_dulint_cmp(biggest, limit) >= 0) {

			limit = ut_dulint_add(biggest, 1);
		}
	}

	if (trx->insert_undo) {
		trx_undo_truncate_end(trx, trx->insert_undo, limit);
	}

	if (trx->update_undo) {
		trx_undo_truncate_end(trx, trx->update_undo, limit);
	}
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
822
/***********************************************************************//**
823
Pops the topmost undo log record in a single undo log and updates the info
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
824 825
about the topmost record in the undo log memory struct.
@return	undo log record, the page s-latched */
826 827 828 829
static
trx_undo_rec_t*
trx_roll_pop_top_rec(
/*=================*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
830 831 832
	trx_t*		trx,	/*!< in: transaction */
	trx_undo_t*	undo,	/*!< in: undo log */
	mtr_t*		mtr)	/*!< in: mtr */
833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870
{
	page_t*		undo_page;
	ulint		offset;
	trx_undo_rec_t*	prev_rec;
	page_t*		prev_rec_page;

	ut_ad(mutex_own(&(trx->undo_mutex)));

	undo_page = trx_undo_page_get_s_latched(undo->space, undo->zip_size,
						undo->top_page_no, mtr);
	offset = undo->top_offset;

	/*	fprintf(stderr, "Thread %lu undoing trx %lu undo record %lu\n",
	os_thread_get_curr_id(), ut_dulint_get_low(trx->id),
	ut_dulint_get_low(undo->top_undo_no)); */

	prev_rec = trx_undo_get_prev_rec(undo_page + offset,
					 undo->hdr_page_no, undo->hdr_offset,
					 mtr);
	if (prev_rec == NULL) {

		undo->empty = TRUE;
	} else {
		prev_rec_page = page_align(prev_rec);

		if (prev_rec_page != undo_page) {

			trx->pages_undone++;
		}

		undo->top_page_no = page_get_page_no(prev_rec_page);
		undo->top_offset  = prev_rec - prev_rec_page;
		undo->top_undo_no = trx_undo_rec_get_undo_no(prev_rec);
	}

	return(undo_page + offset);
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
871
/********************************************************************//**
872 873 874 875
Pops the topmost record when the two undo logs of a transaction are seen
as a single stack of records ordered by their undo numbers. Inserts the
undo number of the popped undo record to the array of currently processed
undo numbers in the transaction. When the query thread finishes processing
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
876 877 878
of this undo record, it must be released with trx_undo_rec_release.
@return undo log record copied to heap, NULL if none left, or if the
undo number of the top record would be less than the limit */
879 880 881 882
UNIV_INTERN
trx_undo_rec_t*
trx_roll_pop_top_rec_of_trx(
/*========================*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
883 884 885 886
	trx_t*		trx,	/*!< in: transaction */
	undo_no_t	limit,	/*!< in: least undo number we need */
	roll_ptr_t*	roll_ptr,/*!< out: roll pointer to undo record */
	mem_heap_t*	heap)	/*!< in: memory heap where copied */
887 888 889 890 891 892
{
	trx_undo_t*	undo;
	trx_undo_t*	ins_undo;
	trx_undo_t*	upd_undo;
	trx_undo_rec_t*	undo_rec;
	trx_undo_rec_t*	undo_rec_copy;
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
893
	undo_no_t	undo_no;
894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002
	ibool		is_insert;
	trx_rseg_t*	rseg;
	ulint		progress_pct;
	mtr_t		mtr;

	rseg = trx->rseg;
try_again:
	mutex_enter(&(trx->undo_mutex));

	if (trx->pages_undone >= TRX_ROLL_TRUNC_THRESHOLD) {
		mutex_enter(&(rseg->mutex));

		trx_roll_try_truncate(trx);

		mutex_exit(&(rseg->mutex));
	}

	ins_undo = trx->insert_undo;
	upd_undo = trx->update_undo;

	if (!ins_undo || ins_undo->empty) {
		undo = upd_undo;
	} else if (!upd_undo || upd_undo->empty) {
		undo = ins_undo;
	} else if (ut_dulint_cmp(upd_undo->top_undo_no,
				 ins_undo->top_undo_no) > 0) {
		undo = upd_undo;
	} else {
		undo = ins_undo;
	}

	if (!undo || undo->empty
	    || (ut_dulint_cmp(limit, undo->top_undo_no) > 0)) {

		if ((trx->undo_no_arr)->n_used == 0) {
			/* Rollback is ending */

			mutex_enter(&(rseg->mutex));

			trx_roll_try_truncate(trx);

			mutex_exit(&(rseg->mutex));
		}

		mutex_exit(&(trx->undo_mutex));

		return(NULL);
	}

	if (undo == ins_undo) {
		is_insert = TRUE;
	} else {
		is_insert = FALSE;
	}

	*roll_ptr = trx_undo_build_roll_ptr(is_insert, (undo->rseg)->id,
					    undo->top_page_no,
					    undo->top_offset);
	mtr_start(&mtr);

	undo_rec = trx_roll_pop_top_rec(trx, undo, &mtr);

	undo_no = trx_undo_rec_get_undo_no(undo_rec);

	ut_ad(ut_dulint_cmp(ut_dulint_add(undo_no, 1), trx->undo_no) == 0);

	/* We print rollback progress info if we are in a crash recovery
	and the transaction has at least 1000 row operations to undo. */

	if (trx == trx_roll_crash_recv_trx && trx_roll_max_undo_no > 1000) {

		progress_pct = 100 - (ulint)
			((ut_conv_dulint_to_longlong(undo_no) * 100)
			 / trx_roll_max_undo_no);
		if (progress_pct != trx_roll_progress_printed_pct) {
			if (trx_roll_progress_printed_pct == 0) {
				fprintf(stderr,
					"\nInnoDB: Progress in percents:"
					" %lu", (ulong) progress_pct);
			} else {
				fprintf(stderr,
					" %lu", (ulong) progress_pct);
			}
			fflush(stderr);
			trx_roll_progress_printed_pct = progress_pct;
		}
	}

	trx->undo_no = undo_no;

	if (!trx_undo_arr_store_info(trx, undo_no)) {
		/* A query thread is already processing this undo log record */

		mutex_exit(&(trx->undo_mutex));

		mtr_commit(&mtr);

		goto try_again;
	}

	undo_rec_copy = trx_undo_rec_copy(undo_rec, heap);

	mutex_exit(&(trx->undo_mutex));

	mtr_commit(&mtr);

	return(undo_rec_copy);
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1003
/********************************************************************//**
1004 1005
Reserves an undo log record for a query thread to undo. This should be
called if the query thread gets the undo log record not using the pop
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1006 1007
function above.
@return	TRUE if succeeded */
1008 1009 1010 1011
UNIV_INTERN
ibool
trx_undo_rec_reserve(
/*=================*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1012 1013
	trx_t*		trx,	/*!< in/out: transaction */
	undo_no_t	undo_no)/*!< in: undo number of the record */
1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025
{
	ibool	ret;

	mutex_enter(&(trx->undo_mutex));

	ret = trx_undo_arr_store_info(trx, undo_no);

	mutex_exit(&(trx->undo_mutex));

	return(ret);
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1026
/*******************************************************************//**
1027 1028 1029 1030 1031
Releases a reserved undo record. */
UNIV_INTERN
void
trx_undo_rec_release(
/*=================*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1032 1033
	trx_t*		trx,	/*!< in/out: transaction */
	undo_no_t	undo_no)/*!< in: undo number */
1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045
{
	trx_undo_arr_t*	arr;

	mutex_enter(&(trx->undo_mutex));

	arr = trx->undo_no_arr;

	trx_undo_arr_remove_info(arr, undo_no);

	mutex_exit(&(trx->undo_mutex));
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1046
/*********************************************************************//**
1047 1048 1049 1050 1051
Starts a rollback operation. */
UNIV_INTERN
void
trx_rollback(
/*=========*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1052 1053 1054
	trx_t*		trx,	/*!< in: transaction */
	trx_sig_t*	sig,	/*!< in: signal starting the rollback */
	que_thr_t**	next_thr)/*!< in/out: next query thread to run;
1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116
				if the value which is passed in is
				a pointer to a NULL pointer, then the
				calling function can start running
				a new query thread; if the passed value is
				NULL, the parameter is ignored */
{
	que_t*		roll_graph;
	que_thr_t*	thr;
	/*	que_thr_t*	thr2; */

	ut_ad(mutex_own(&kernel_mutex));
	ut_ad((trx->undo_no_arr == NULL) || ((trx->undo_no_arr)->n_used == 0));

	/* Initialize the rollback field in the transaction */

	if (sig->type == TRX_SIG_TOTAL_ROLLBACK) {

		trx->roll_limit = ut_dulint_zero;

	} else if (sig->type == TRX_SIG_ROLLBACK_TO_SAVEPT) {

		trx->roll_limit = (sig->savept).least_undo_no;

	} else if (sig->type == TRX_SIG_ERROR_OCCURRED) {

		trx->roll_limit = trx->last_sql_stat_start.least_undo_no;
	} else {
		ut_error;
	}

	ut_a(ut_dulint_cmp(trx->roll_limit, trx->undo_no) <= 0);

	trx->pages_undone = 0;

	if (trx->undo_no_arr == NULL) {
		trx->undo_no_arr = trx_undo_arr_create();
	}

	/* Build a 'query' graph which will perform the undo operations */

	roll_graph = trx_roll_graph_build(trx);

	trx->graph = roll_graph;
	trx->que_state = TRX_QUE_ROLLING_BACK;

	thr = que_fork_start_command(roll_graph);

	ut_ad(thr);

	/*	thr2 = que_fork_start_command(roll_graph);

	ut_ad(thr2); */

	if (next_thr && (*next_thr == NULL)) {
		*next_thr = thr;
		/*		srv_que_task_enqueue_low(thr2); */
	} else {
		srv_que_task_enqueue_low(thr);
		/*		srv_que_task_enqueue_low(thr2); */
	}
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1117
/****************************************************************//**
1118 1119 1120
Builds an undo 'query' graph for a transaction. The actual rollback is
performed by executing this query graph like a query subprocedure call.
The reply about the completion of the rollback will be sent by this
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1121 1122
graph.
@return	own: the query graph */
1123 1124 1125 1126
UNIV_INTERN
que_t*
trx_roll_graph_build(
/*=================*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1127
	trx_t*	trx)	/*!< in: trx handle */
1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148
{
	mem_heap_t*	heap;
	que_fork_t*	fork;
	que_thr_t*	thr;
	/*	que_thr_t*	thr2; */

	ut_ad(mutex_own(&kernel_mutex));

	heap = mem_heap_create(512);
	fork = que_fork_create(NULL, NULL, QUE_FORK_ROLLBACK, heap);
	fork->trx = trx;

	thr = que_thr_create(fork, heap);
	/*	thr2 = que_thr_create(fork, heap); */

	thr->child = row_undo_node_create(trx, thr, heap);
	/*	thr2->child = row_undo_node_create(trx, thr2, heap); */

	return(fork);
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1149
/*********************************************************************//**
1150 1151 1152 1153 1154 1155
Finishes error processing after the necessary partial rollback has been
done. */
static
void
trx_finish_error_processing(
/*========================*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1156
	trx_t*	trx)	/*!< in: transaction */
1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178
{
	trx_sig_t*	sig;
	trx_sig_t*	next_sig;

	ut_ad(mutex_own(&kernel_mutex));

	sig = UT_LIST_GET_FIRST(trx->signals);

	while (sig != NULL) {
		next_sig = UT_LIST_GET_NEXT(signals, sig);

		if (sig->type == TRX_SIG_ERROR_OCCURRED) {

			trx_sig_remove(trx, sig);
		}

		sig = next_sig;
	}

	trx->que_state = TRX_QUE_RUNNING;
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1179
/*********************************************************************//**
1180 1181 1182 1183 1184
Finishes a partial rollback operation. */
static
void
trx_finish_partial_rollback_off_kernel(
/*===================================*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1185 1186
	trx_t*		trx,	/*!< in: transaction */
	que_thr_t**	next_thr)/*!< in/out: next query thread to run;
1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206
				if the value which is passed in is a pointer
				to a NULL pointer, then the calling function
				can start running a new query thread; if this
				parameter is NULL, it is ignored */
{
	trx_sig_t*	sig;

	ut_ad(mutex_own(&kernel_mutex));

	sig = UT_LIST_GET_FIRST(trx->signals);

	/* Remove the signal from the signal queue and send reply message
	to it */

	trx_sig_reply(sig, next_thr);
	trx_sig_remove(trx, sig);

	trx->que_state = TRX_QUE_RUNNING;
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1207
/****************************************************************//**
1208 1209 1210 1211 1212
Finishes a transaction rollback. */
UNIV_INTERN
void
trx_finish_rollback_off_kernel(
/*===========================*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1213 1214 1215
	que_t*		graph,	/*!< in: undo graph which can now be freed */
	trx_t*		trx,	/*!< in: transaction */
	que_thr_t**	next_thr)/*!< in/out: next query thread to run;
1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274
				if the value which is passed in is
				a pointer to a NULL pointer, then the
				calling function can start running
				a new query thread; if this parameter is
				NULL, it is ignored */
{
	trx_sig_t*	sig;
	trx_sig_t*	next_sig;

	ut_ad(mutex_own(&kernel_mutex));

	ut_a(trx->undo_no_arr == NULL || trx->undo_no_arr->n_used == 0);

	/* Free the memory reserved by the undo graph */
	que_graph_free(graph);

	sig = UT_LIST_GET_FIRST(trx->signals);

	if (sig->type == TRX_SIG_ROLLBACK_TO_SAVEPT) {

		trx_finish_partial_rollback_off_kernel(trx, next_thr);

		return;

	} else if (sig->type == TRX_SIG_ERROR_OCCURRED) {

		trx_finish_error_processing(trx);

		return;
	}

#ifdef UNIV_DEBUG
	if (lock_print_waits) {
		fprintf(stderr, "Trx %lu rollback finished\n",
			(ulong) ut_dulint_get_low(trx->id));
	}
#endif /* UNIV_DEBUG */

	trx_commit_off_kernel(trx);

	/* Remove all TRX_SIG_TOTAL_ROLLBACK signals from the signal queue and
	send reply messages to them */

	trx->que_state = TRX_QUE_RUNNING;

	while (sig != NULL) {
		next_sig = UT_LIST_GET_NEXT(signals, sig);

		if (sig->type == TRX_SIG_TOTAL_ROLLBACK) {

			trx_sig_reply(sig, next_thr);

			trx_sig_remove(trx, sig);
		}

		sig = next_sig;
	}
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1275 1276 1277
/*********************************************************************//**
Creates a rollback command node struct.
@return	own: rollback node struct */
1278 1279 1280 1281
UNIV_INTERN
roll_node_t*
roll_node_create(
/*=============*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1282
	mem_heap_t*	heap)	/*!< in: mem heap where created */
1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294
{
	roll_node_t*	node;

	node = mem_heap_alloc(heap, sizeof(roll_node_t));
	node->common.type = QUE_NODE_ROLLBACK;
	node->state = ROLL_NODE_SEND;

	node->partial = FALSE;

	return(node);
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1295 1296 1297
/***********************************************************//**
Performs an execution step for a rollback command node in a query graph.
@return	query thread to run next, or NULL */
1298 1299 1300 1301
UNIV_INTERN
que_thr_t*
trx_rollback_step(
/*==============*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1302
	que_thr_t*	thr)	/*!< in: query thread */
1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346
{
	roll_node_t*	node;
	ulint		sig_no;
	trx_savept_t*	savept;

	node = thr->run_node;

	ut_ad(que_node_get_type(node) == QUE_NODE_ROLLBACK);

	if (thr->prev_node == que_node_get_parent(node)) {
		node->state = ROLL_NODE_SEND;
	}

	if (node->state == ROLL_NODE_SEND) {
		mutex_enter(&kernel_mutex);

		node->state = ROLL_NODE_WAIT;

		if (node->partial) {
			sig_no = TRX_SIG_ROLLBACK_TO_SAVEPT;
			savept = &(node->savept);
		} else {
			sig_no = TRX_SIG_TOTAL_ROLLBACK;
			savept = NULL;
		}

		/* Send a rollback signal to the transaction */

		trx_sig_send(thr_get_trx(thr), sig_no, TRX_SIG_SELF, thr,
			     savept, NULL);

		thr->state = QUE_THR_SIG_REPLY_WAIT;

		mutex_exit(&kernel_mutex);

		return(NULL);
	}

	ut_ad(node->state == ROLL_NODE_WAIT);

	thr->run_node = que_node_get_parent(node);

	return(thr);
}