You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1357 lines
32 KiB

17 years ago
16 years ago
16 years ago
16 years ago
15 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
17 years ago
17 years ago
16 years ago
17 years ago
16 years ago
17 years ago
16 years ago
16 years ago
17 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
17 years ago
16 years ago
16 years ago
17 years ago
17 years ago
17 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
16 years ago
15 years ago
15 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
15 years ago
16 years ago
16 years ago
15 years ago
16 years ago
15 years ago
16 years ago
16 years ago
16 years ago
15 years ago
15 years ago
16 years ago
16 years ago
16 years ago
15 years ago
15 years ago
16 years ago
16 years ago
16 years ago
15 years ago
16 years ago
16 years ago
16 years ago
16 years ago
15 years ago
15 years ago
15 years ago
15 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
15 years ago
15 years ago
15 years ago
15 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
15 years ago
16 years ago
16 years ago
16 years ago
16 years ago
  1. /*****************************************************************************
  2. Copyright (c) 1996, 2009, Innobase Oy. All Rights Reserved.
  3. This program is free software; you can redistribute it and/or modify it under
  4. the terms of the GNU General Public License as published by the Free Software
  5. Foundation; version 2 of the License.
  6. This program is distributed in the hope that it will be useful, but WITHOUT
  7. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. You should have received a copy of the GNU General Public License along with
  10. this program; if not, write to the Free Software Foundation, Inc., 59 Temple
  11. Place, Suite 330, Boston, MA 02111-1307 USA
  12. *****************************************************************************/
  13. /**************************************************//**
  14. @file trx/trx0roll.c
  15. Transaction rollback
  16. Created 3/26/1996 Heikki Tuuri
  17. *******************************************************/
  18. #include "trx0roll.h"
  19. #ifdef UNIV_NONINL
  20. #include "trx0roll.ic"
  21. #endif
  22. #include "fsp0fsp.h"
  23. #include "mach0data.h"
  24. #include "trx0rseg.h"
  25. #include "trx0trx.h"
  26. #include "trx0undo.h"
  27. #include "trx0rec.h"
  28. #include "que0que.h"
  29. #include "usr0sess.h"
  30. #include "srv0start.h"
  31. #include "row0undo.h"
  32. #include "row0mysql.h"
  33. #include "lock0lock.h"
  34. #include "pars0pars.h"
  35. /** This many pages must be undone before a truncate is tried within
  36. rollback */
  37. #define TRX_ROLL_TRUNC_THRESHOLD 1
  38. /** In crash recovery, the current trx to be rolled back; NULL otherwise */
  39. static const trx_t* trx_roll_crash_recv_trx = NULL;
  40. /** In crash recovery we set this to the undo n:o of the current trx to be
  41. rolled back. Then we can print how many % the rollback has progressed. */
  42. static undo_no_t trx_roll_max_undo_no;
  43. /** Auxiliary variable which tells the previous progress % we printed */
  44. static ulint trx_roll_progress_printed_pct;
  45. /*******************************************************************//**
  46. Rollback a transaction used in MySQL.
  47. @return error code or DB_SUCCESS */
  48. UNIV_INTERN
  49. int
  50. trx_general_rollback_for_mysql(
  51. /*===========================*/
  52. trx_t* trx, /*!< in: transaction handle */
  53. trx_savept_t* savept) /*!< in: pointer to savepoint undo number, if
  54. partial rollback requested, or NULL for
  55. complete rollback */
  56. {
  57. mem_heap_t* heap;
  58. que_thr_t* thr;
  59. roll_node_t* roll_node;
  60. /* Tell Innobase server that there might be work for
  61. utility threads: */
  62. srv_active_wake_master_thread();
  63. trx_start_if_not_started(trx);
  64. heap = mem_heap_create(512);
  65. roll_node = roll_node_create(heap);
  66. if (savept) {
  67. roll_node->partial = TRUE;
  68. roll_node->savept = *savept;
  69. }
  70. trx->error_state = DB_SUCCESS;
  71. thr = pars_complete_graph_for_exec(roll_node, trx, heap);
  72. ut_a(thr == que_fork_start_command(que_node_get_parent(thr)));
  73. que_run_threads(thr);
  74. mutex_enter(&kernel_mutex);
  75. while (trx->que_state != TRX_QUE_RUNNING) {
  76. mutex_exit(&kernel_mutex);
  77. os_thread_sleep(100000);
  78. mutex_enter(&kernel_mutex);
  79. }
  80. mutex_exit(&kernel_mutex);
  81. mem_heap_free(heap);
  82. ut_a(trx->error_state == DB_SUCCESS);
  83. /* Tell Innobase server that there might be work for
  84. utility threads: */
  85. srv_active_wake_master_thread();
  86. return((int) trx->error_state);
  87. }
  88. /*******************************************************************//**
  89. Rollback a transaction used in MySQL.
  90. @return error code or DB_SUCCESS */
  91. UNIV_INTERN
  92. int
  93. trx_rollback_for_mysql(
  94. /*===================*/
  95. trx_t* trx) /*!< in: transaction handle */
  96. {
  97. int err;
  98. if (trx->state == TRX_NOT_STARTED) {
  99. return(DB_SUCCESS);
  100. }
  101. trx->op_info = "rollback";
  102. /* If we are doing the XA recovery of prepared transactions, then
  103. the transaction object does not have an InnoDB session object, and we
  104. set a dummy session that we use for all MySQL transactions. */
  105. err = trx_general_rollback_for_mysql(trx, NULL);
  106. trx->op_info = "";
  107. return(err);
  108. }
  109. /*******************************************************************//**
  110. Rollback the latest SQL statement for MySQL.
  111. @return error code or DB_SUCCESS */
  112. UNIV_INTERN
  113. int
  114. trx_rollback_last_sql_stat_for_mysql(
  115. /*=================================*/
  116. trx_t* trx) /*!< in: transaction handle */
  117. {
  118. int err;
  119. if (trx->state == TRX_NOT_STARTED) {
  120. return(DB_SUCCESS);
  121. }
  122. trx->op_info = "rollback of SQL statement";
  123. err = trx_general_rollback_for_mysql(trx, &trx->last_sql_stat_start);
  124. /* The following call should not be needed, but we play safe: */
  125. trx_mark_sql_stat_end(trx);
  126. trx->op_info = "";
  127. return(err);
  128. }
  129. /*******************************************************************//**
  130. Frees a single savepoint struct. */
  131. UNIV_INTERN
  132. void
  133. trx_roll_savepoint_free(
  134. /*=====================*/
  135. trx_t* trx, /*!< in: transaction handle */
  136. trx_named_savept_t* savep) /*!< in: savepoint to free */
  137. {
  138. ut_a(savep != NULL);
  139. ut_a(UT_LIST_GET_LEN(trx->trx_savepoints) > 0);
  140. UT_LIST_REMOVE(trx_savepoints, trx->trx_savepoints, savep);
  141. mem_free(savep->name);
  142. mem_free(savep);
  143. }
  144. /*******************************************************************//**
  145. Frees savepoint structs starting from savep, if savep == NULL then
  146. free all savepoints. */
  147. UNIV_INTERN
  148. void
  149. trx_roll_savepoints_free(
  150. /*=====================*/
  151. trx_t* trx, /*!< in: transaction handle */
  152. trx_named_savept_t* savep) /*!< in: free all savepoints > this one;
  153. if this is NULL, free all savepoints
  154. of trx */
  155. {
  156. trx_named_savept_t* next_savep;
  157. if (savep == NULL) {
  158. savep = UT_LIST_GET_FIRST(trx->trx_savepoints);
  159. } else {
  160. savep = UT_LIST_GET_NEXT(trx_savepoints, savep);
  161. }
  162. while (savep != NULL) {
  163. next_savep = UT_LIST_GET_NEXT(trx_savepoints, savep);
  164. trx_roll_savepoint_free(trx, savep);
  165. savep = next_savep;
  166. }
  167. }
  168. /*******************************************************************//**
  169. Rolls back a transaction back to a named savepoint. Modifications after the
  170. savepoint are undone but InnoDB does NOT release the corresponding locks
  171. which are stored in memory. If a lock is 'implicit', that is, a new inserted
  172. row holds a lock where the lock information is carried by the trx id stored in
  173. the row, these locks are naturally released in the rollback. Savepoints which
  174. were set after this savepoint are deleted.
  175. @return if no savepoint of the name found then DB_NO_SAVEPOINT,
  176. otherwise DB_SUCCESS */
  177. UNIV_INTERN
  178. ulint
  179. trx_rollback_to_savepoint_for_mysql(
  180. /*================================*/
  181. trx_t* trx, /*!< in: transaction handle */
  182. const char* savepoint_name, /*!< in: savepoint name */
  183. ib_int64_t* mysql_binlog_cache_pos) /*!< out: the MySQL binlog cache
  184. position corresponding to this
  185. savepoint; MySQL needs this
  186. information to remove the
  187. binlog entries of the queries
  188. executed after the savepoint */
  189. {
  190. trx_named_savept_t* savep;
  191. ulint err;
  192. savep = UT_LIST_GET_FIRST(trx->trx_savepoints);
  193. while (savep != NULL) {
  194. if (0 == ut_strcmp(savep->name, savepoint_name)) {
  195. /* Found */
  196. break;
  197. }
  198. savep = UT_LIST_GET_NEXT(trx_savepoints, savep);
  199. }
  200. if (savep == NULL) {
  201. return(DB_NO_SAVEPOINT);
  202. }
  203. if (trx->state == TRX_NOT_STARTED) {
  204. ut_print_timestamp(stderr);
  205. fputs(" InnoDB: Error: transaction has a savepoint ", stderr);
  206. ut_print_name(stderr, trx, FALSE, savep->name);
  207. fputs(" though it is not started\n", stderr);
  208. return(DB_ERROR);
  209. }
  210. /* We can now free all savepoints strictly later than this one */
  211. trx_roll_savepoints_free(trx, savep);
  212. *mysql_binlog_cache_pos = savep->mysql_binlog_cache_pos;
  213. trx->op_info = "rollback to a savepoint";
  214. err = trx_general_rollback_for_mysql(trx, &savep->savept);
  215. /* Store the current undo_no of the transaction so that we know where
  216. to roll back if we have to roll back the next SQL statement: */
  217. trx_mark_sql_stat_end(trx);
  218. trx->op_info = "";
  219. return(err);
  220. }
  221. /*******************************************************************//**
  222. Creates a named savepoint. If the transaction is not yet started, starts it.
  223. If there is already a savepoint of the same name, this call erases that old
  224. savepoint and replaces it with a new. Savepoints are deleted in a transaction
  225. commit or rollback.
  226. @return always DB_SUCCESS */
  227. UNIV_INTERN
  228. ulint
  229. trx_savepoint_for_mysql(
  230. /*====================*/
  231. trx_t* trx, /*!< in: transaction handle */
  232. const char* savepoint_name, /*!< in: savepoint name */
  233. ib_int64_t binlog_cache_pos) /*!< in: MySQL binlog cache
  234. position corresponding to this
  235. connection at the time of the
  236. savepoint */
  237. {
  238. trx_named_savept_t* savep;
  239. ut_a(trx);
  240. ut_a(savepoint_name);
  241. trx_start_if_not_started(trx);
  242. savep = UT_LIST_GET_FIRST(trx->trx_savepoints);
  243. while (savep != NULL) {
  244. if (0 == ut_strcmp(savep->name, savepoint_name)) {
  245. /* Found */
  246. break;
  247. }
  248. savep = UT_LIST_GET_NEXT(trx_savepoints, savep);
  249. }
  250. if (savep) {
  251. /* There is a savepoint with the same name: free that */
  252. UT_LIST_REMOVE(trx_savepoints, trx->trx_savepoints, savep);
  253. mem_free(savep->name);
  254. mem_free(savep);
  255. }
  256. /* Create a new savepoint and add it as the last in the list */
  257. savep = mem_alloc(sizeof(trx_named_savept_t));
  258. savep->name = mem_strdup(savepoint_name);
  259. savep->savept = trx_savept_take(trx);
  260. savep->mysql_binlog_cache_pos = binlog_cache_pos;
  261. UT_LIST_ADD_LAST(trx_savepoints, trx->trx_savepoints, savep);
  262. return(DB_SUCCESS);
  263. }
  264. /*******************************************************************//**
  265. Releases only the named savepoint. Savepoints which were set after this
  266. savepoint are left as is.
  267. @return if no savepoint of the name found then DB_NO_SAVEPOINT,
  268. otherwise DB_SUCCESS */
  269. UNIV_INTERN
  270. ulint
  271. trx_release_savepoint_for_mysql(
  272. /*============================*/
  273. trx_t* trx, /*!< in: transaction handle */
  274. const char* savepoint_name) /*!< in: savepoint name */
  275. {
  276. trx_named_savept_t* savep;
  277. savep = UT_LIST_GET_FIRST(trx->trx_savepoints);
  278. /* Search for the savepoint by name and free if found. */
  279. while (savep != NULL) {
  280. if (0 == ut_strcmp(savep->name, savepoint_name)) {
  281. trx_roll_savepoint_free(trx, savep);
  282. return(DB_SUCCESS);
  283. }
  284. savep = UT_LIST_GET_NEXT(trx_savepoints, savep);
  285. }
  286. return(DB_NO_SAVEPOINT);
  287. }
  288. /*******************************************************************//**
  289. Determines if this transaction is rolling back an incomplete transaction
  290. in crash recovery.
  291. @return TRUE if trx is an incomplete transaction that is being rolled
  292. back in crash recovery */
  293. UNIV_INTERN
  294. ibool
  295. trx_is_recv(
  296. /*========*/
  297. const trx_t* trx) /*!< in: transaction */
  298. {
  299. return(trx == trx_roll_crash_recv_trx);
  300. }
  301. /*******************************************************************//**
  302. Returns a transaction savepoint taken at this point in time.
  303. @return savepoint */
  304. UNIV_INTERN
  305. trx_savept_t
  306. trx_savept_take(
  307. /*============*/
  308. trx_t* trx) /*!< in: transaction */
  309. {
  310. trx_savept_t savept;
  311. savept.least_undo_no = trx->undo_no;
  312. return(savept);
  313. }
  314. /*******************************************************************//**
  315. Roll back an active transaction. */
  316. static
  317. void
  318. trx_rollback_active(
  319. /*================*/
  320. trx_t* trx) /*!< in/out: transaction */
  321. {
  322. mem_heap_t* heap;
  323. que_fork_t* fork;
  324. que_thr_t* thr;
  325. roll_node_t* roll_node;
  326. dict_table_t* table;
  327. ib_int64_t rows_to_undo;
  328. const char* unit = "";
  329. ibool dictionary_locked = FALSE;
  330. heap = mem_heap_create(512);
  331. fork = que_fork_create(NULL, NULL, QUE_FORK_RECOVERY, heap);
  332. fork->trx = trx;
  333. thr = que_thr_create(fork, heap);
  334. roll_node = roll_node_create(heap);
  335. thr->child = roll_node;
  336. roll_node->common.parent = thr;
  337. mutex_enter(&kernel_mutex);
  338. trx->graph = fork;
  339. ut_a(thr == que_fork_start_command(fork));
  340. trx_roll_crash_recv_trx = trx;
  341. trx_roll_max_undo_no = trx->undo_no;
  342. trx_roll_progress_printed_pct = 0;
  343. rows_to_undo = trx_roll_max_undo_no;
  344. if (rows_to_undo > 1000000000) {
  345. rows_to_undo = rows_to_undo / 1000000;
  346. unit = "M";
  347. }
  348. ut_print_timestamp(stderr);
  349. fprintf(stderr,
  350. " InnoDB: Rolling back trx with id " TRX_ID_FMT ", %lu%s"
  351. " rows to undo\n",
  352. (ullint) trx->id,
  353. (ulong) rows_to_undo, unit);
  354. mutex_exit(&kernel_mutex);
  355. if (trx_get_dict_operation(trx) != TRX_DICT_OP_NONE) {
  356. row_mysql_lock_data_dictionary(trx);
  357. dictionary_locked = TRUE;
  358. }
  359. que_run_threads(thr);
  360. mutex_enter(&kernel_mutex);
  361. while (trx->que_state != TRX_QUE_RUNNING) {
  362. mutex_exit(&kernel_mutex);
  363. fprintf(stderr,
  364. "InnoDB: Waiting for rollback of trx id "
  365. TRX_ID_FMT " to end\n",
  366. (ullint) trx->id);
  367. os_thread_sleep(100000);
  368. mutex_enter(&kernel_mutex);
  369. }
  370. mutex_exit(&kernel_mutex);
  371. if (trx_get_dict_operation(trx) != TRX_DICT_OP_NONE
  372. && trx->table_id != 0) {
  373. /* If the transaction was for a dictionary operation, we
  374. drop the relevant table, if it still exists */
  375. fprintf(stderr,
  376. "InnoDB: Dropping table with id %llu"
  377. " in recovery if it exists\n",
  378. (ullint) trx->table_id);
  379. table = dict_table_get_on_id_low(trx->table_id);
  380. if (table) {
  381. ulint err;
  382. fputs("InnoDB: Table found: dropping table ", stderr);
  383. ut_print_name(stderr, trx, TRUE, table->name);
  384. fputs(" in recovery\n", stderr);
  385. err = row_drop_table_for_mysql(table->name, trx, TRUE);
  386. trx_commit_for_mysql(trx);
  387. ut_a(err == (int) DB_SUCCESS);
  388. }
  389. }
  390. if (dictionary_locked) {
  391. row_mysql_unlock_data_dictionary(trx);
  392. }
  393. fprintf(stderr, "\nInnoDB: Rolling back of trx id " TRX_ID_FMT
  394. " completed\n",
  395. (ullint) trx->id);
  396. mem_heap_free(heap);
  397. trx_roll_crash_recv_trx = NULL;
  398. }
  399. /*******************************************************************//**
  400. Rollback or clean up any incomplete transactions which were
  401. encountered in crash recovery. If the transaction already was
  402. committed, then we clean up a possible insert undo log. If the
  403. transaction was not yet committed, then we roll it back. */
  404. UNIV_INTERN
  405. void
  406. trx_rollback_or_clean_recovered(
  407. /*============================*/
  408. ibool all) /*!< in: FALSE=roll back dictionary transactions;
  409. TRUE=roll back all non-PREPARED transactions */
  410. {
  411. trx_t* trx;
  412. mutex_enter(&kernel_mutex);
  413. if (!UT_LIST_GET_FIRST(trx_sys->trx_list)) {
  414. goto leave_function;
  415. }
  416. if (all) {
  417. fprintf(stderr,
  418. "InnoDB: Starting in background the rollback"
  419. " of uncommitted transactions\n");
  420. }
  421. mutex_exit(&kernel_mutex);
  422. loop:
  423. mutex_enter(&kernel_mutex);
  424. for (trx = UT_LIST_GET_FIRST(trx_sys->trx_list); trx;
  425. trx = UT_LIST_GET_NEXT(trx_list, trx)) {
  426. if (!trx->is_recovered) {
  427. continue;
  428. }
  429. switch (trx->state) {
  430. case TRX_NOT_STARTED:
  431. case TRX_PREPARED:
  432. continue;
  433. case TRX_COMMITTED_IN_MEMORY:
  434. mutex_exit(&kernel_mutex);
  435. fprintf(stderr,
  436. "InnoDB: Cleaning up trx with id "
  437. TRX_ID_FMT "\n",
  438. (ullint) trx->id);
  439. trx_cleanup_at_db_startup(trx);
  440. goto loop;
  441. case TRX_ACTIVE:
  442. if (all || trx_get_dict_operation(trx)
  443. != TRX_DICT_OP_NONE) {
  444. mutex_exit(&kernel_mutex);
  445. trx_rollback_active(trx);
  446. goto loop;
  447. }
  448. }
  449. }
  450. if (all) {
  451. ut_print_timestamp(stderr);
  452. fprintf(stderr,
  453. " InnoDB: Rollback of non-prepared"
  454. " transactions completed\n");
  455. }
  456. leave_function:
  457. mutex_exit(&kernel_mutex);
  458. }
  459. /*******************************************************************//**
  460. Rollback or clean up any incomplete transactions which were
  461. encountered in crash recovery. If the transaction already was
  462. committed, then we clean up a possible insert undo log. If the
  463. transaction was not yet committed, then we roll it back.
  464. Note: this is done in a background thread.
  465. @return a dummy parameter */
  466. UNIV_INTERN
  467. os_thread_ret_t
  468. trx_rollback_or_clean_all_recovered(
  469. /*================================*/
  470. void* arg __attribute__((unused)))
  471. /*!< in: a dummy parameter required by
  472. os_thread_create */
  473. {
  474. #ifdef UNIV_PFS_THREAD
  475. pfs_register_thread(trx_rollback_clean_thread_key);
  476. #endif /* UNIV_PFS_THREAD */
  477. trx_rollback_or_clean_recovered(TRUE);
  478. /* We count the number of threads in os_thread_exit(). A created
  479. thread should always use that to exit and not use return() to exit. */
  480. os_thread_exit(NULL);
  481. OS_THREAD_DUMMY_RETURN;
  482. }
  483. /*******************************************************************//**
  484. Creates an undo number array.
  485. @return own: undo number array */
  486. UNIV_INTERN
  487. trx_undo_arr_t*
  488. trx_undo_arr_create(void)
  489. /*=====================*/
  490. {
  491. trx_undo_arr_t* arr;
  492. mem_heap_t* heap;
  493. ulint i;
  494. heap = mem_heap_create(1024);
  495. arr = mem_heap_alloc(heap, sizeof(trx_undo_arr_t));
  496. arr->infos = mem_heap_alloc(heap, sizeof(trx_undo_inf_t)
  497. * UNIV_MAX_PARALLELISM);
  498. arr->n_cells = UNIV_MAX_PARALLELISM;
  499. arr->n_used = 0;
  500. arr->heap = heap;
  501. for (i = 0; i < UNIV_MAX_PARALLELISM; i++) {
  502. (trx_undo_arr_get_nth_info(arr, i))->in_use = FALSE;
  503. }
  504. return(arr);
  505. }
  506. /*******************************************************************//**
  507. Frees an undo number array. */
  508. UNIV_INTERN
  509. void
  510. trx_undo_arr_free(
  511. /*==============*/
  512. trx_undo_arr_t* arr) /*!< in: undo number array */
  513. {
  514. ut_ad(arr->n_used == 0);
  515. mem_heap_free(arr->heap);
  516. }
  517. /*******************************************************************//**
  518. Stores info of an undo log record to the array if it is not stored yet.
  519. @return FALSE if the record already existed in the array */
  520. static
  521. ibool
  522. trx_undo_arr_store_info(
  523. /*====================*/
  524. trx_t* trx, /*!< in: transaction */
  525. undo_no_t undo_no)/*!< in: undo number */
  526. {
  527. trx_undo_inf_t* cell;
  528. trx_undo_inf_t* stored_here;
  529. trx_undo_arr_t* arr;
  530. ulint n_used;
  531. ulint n;
  532. ulint i;
  533. n = 0;
  534. arr = trx->undo_no_arr;
  535. n_used = arr->n_used;
  536. stored_here = NULL;
  537. for (i = 0;; i++) {
  538. cell = trx_undo_arr_get_nth_info(arr, i);
  539. if (!cell->in_use) {
  540. if (!stored_here) {
  541. /* Not in use, we may store here */
  542. cell->undo_no = undo_no;
  543. cell->in_use = TRUE;
  544. arr->n_used++;
  545. stored_here = cell;
  546. }
  547. } else {
  548. n++;
  549. if (cell->undo_no == undo_no) {
  550. if (stored_here) {
  551. stored_here->in_use = FALSE;
  552. ut_ad(arr->n_used > 0);
  553. arr->n_used--;
  554. }
  555. ut_ad(arr->n_used == n_used);
  556. return(FALSE);
  557. }
  558. }
  559. if (n == n_used && stored_here) {
  560. ut_ad(arr->n_used == 1 + n_used);
  561. return(TRUE);
  562. }
  563. }
  564. }
  565. /*******************************************************************//**
  566. Removes an undo number from the array. */
  567. static
  568. void
  569. trx_undo_arr_remove_info(
  570. /*=====================*/
  571. trx_undo_arr_t* arr, /*!< in: undo number array */
  572. undo_no_t undo_no)/*!< in: undo number */
  573. {
  574. trx_undo_inf_t* cell;
  575. ulint i;
  576. for (i = 0;; i++) {
  577. cell = trx_undo_arr_get_nth_info(arr, i);
  578. if (cell->in_use
  579. && cell->undo_no == undo_no) {
  580. cell->in_use = FALSE;
  581. ut_ad(arr->n_used > 0);
  582. arr->n_used--;
  583. return;
  584. }
  585. }
  586. }
  587. /*******************************************************************//**
  588. Gets the biggest undo number in an array.
  589. @return biggest value, 0 if the array is empty */
  590. static
  591. undo_no_t
  592. trx_undo_arr_get_biggest(
  593. /*=====================*/
  594. trx_undo_arr_t* arr) /*!< in: undo number array */
  595. {
  596. trx_undo_inf_t* cell;
  597. ulint n_used;
  598. undo_no_t biggest;
  599. ulint n;
  600. ulint i;
  601. n = 0;
  602. n_used = arr->n_used;
  603. biggest = 0;
  604. for (i = 0;; i++) {
  605. cell = trx_undo_arr_get_nth_info(arr, i);
  606. if (cell->in_use) {
  607. n++;
  608. if (cell->undo_no > biggest) {
  609. biggest = cell->undo_no;
  610. }
  611. }
  612. if (n == n_used) {
  613. return(biggest);
  614. }
  615. }
  616. }
  617. /***********************************************************************//**
  618. Tries truncate the undo logs. */
  619. UNIV_INTERN
  620. void
  621. trx_roll_try_truncate(
  622. /*==================*/
  623. trx_t* trx) /*!< in/out: transaction */
  624. {
  625. trx_undo_arr_t* arr;
  626. undo_no_t limit;
  627. undo_no_t biggest;
  628. ut_ad(mutex_own(&(trx->undo_mutex)));
  629. ut_ad(mutex_own(&((trx->rseg)->mutex)));
  630. trx->pages_undone = 0;
  631. arr = trx->undo_no_arr;
  632. limit = trx->undo_no;
  633. if (arr->n_used > 0) {
  634. biggest = trx_undo_arr_get_biggest(arr);
  635. if (biggest >= limit) {
  636. limit = biggest + 1;
  637. }
  638. }
  639. if (trx->insert_undo) {
  640. trx_undo_truncate_end(trx, trx->insert_undo, limit);
  641. }
  642. if (trx->update_undo) {
  643. trx_undo_truncate_end(trx, trx->update_undo, limit);
  644. }
  645. }
  646. /***********************************************************************//**
  647. Pops the topmost undo log record in a single undo log and updates the info
  648. about the topmost record in the undo log memory struct.
  649. @return undo log record, the page s-latched */
  650. static
  651. trx_undo_rec_t*
  652. trx_roll_pop_top_rec(
  653. /*=================*/
  654. trx_t* trx, /*!< in: transaction */
  655. trx_undo_t* undo, /*!< in: undo log */
  656. mtr_t* mtr) /*!< in: mtr */
  657. {
  658. page_t* undo_page;
  659. ulint offset;
  660. trx_undo_rec_t* prev_rec;
  661. page_t* prev_rec_page;
  662. ut_ad(mutex_own(&(trx->undo_mutex)));
  663. undo_page = trx_undo_page_get_s_latched(undo->space, undo->zip_size,
  664. undo->top_page_no, mtr);
  665. offset = undo->top_offset;
  666. /* fprintf(stderr, "Thread %lu undoing trx " TRX_ID_FMT
  667. " undo record " TRX_ID_FMT "\n",
  668. os_thread_get_curr_id(), trx->id, undo->top_undo_no); */
  669. prev_rec = trx_undo_get_prev_rec(undo_page + offset,
  670. undo->hdr_page_no, undo->hdr_offset,
  671. mtr);
  672. if (prev_rec == NULL) {
  673. undo->empty = TRUE;
  674. } else {
  675. prev_rec_page = page_align(prev_rec);
  676. if (prev_rec_page != undo_page) {
  677. trx->pages_undone++;
  678. }
  679. undo->top_page_no = page_get_page_no(prev_rec_page);
  680. undo->top_offset = prev_rec - prev_rec_page;
  681. undo->top_undo_no = trx_undo_rec_get_undo_no(prev_rec);
  682. }
  683. return(undo_page + offset);
  684. }
  685. /********************************************************************//**
  686. Pops the topmost record when the two undo logs of a transaction are seen
  687. as a single stack of records ordered by their undo numbers. Inserts the
  688. undo number of the popped undo record to the array of currently processed
  689. undo numbers in the transaction. When the query thread finishes processing
  690. of this undo record, it must be released with trx_undo_rec_release.
  691. @return undo log record copied to heap, NULL if none left, or if the
  692. undo number of the top record would be less than the limit */
  693. UNIV_INTERN
  694. trx_undo_rec_t*
  695. trx_roll_pop_top_rec_of_trx(
  696. /*========================*/
  697. trx_t* trx, /*!< in: transaction */
  698. undo_no_t limit, /*!< in: least undo number we need */
  699. roll_ptr_t* roll_ptr,/*!< out: roll pointer to undo record */
  700. mem_heap_t* heap) /*!< in: memory heap where copied */
  701. {
  702. trx_undo_t* undo;
  703. trx_undo_t* ins_undo;
  704. trx_undo_t* upd_undo;
  705. trx_undo_rec_t* undo_rec;
  706. trx_undo_rec_t* undo_rec_copy;
  707. undo_no_t undo_no;
  708. ibool is_insert;
  709. trx_rseg_t* rseg;
  710. ulint progress_pct;
  711. mtr_t mtr;
  712. rseg = trx->rseg;
  713. try_again:
  714. mutex_enter(&(trx->undo_mutex));
  715. if (trx->pages_undone >= TRX_ROLL_TRUNC_THRESHOLD) {
  716. mutex_enter(&(rseg->mutex));
  717. trx_roll_try_truncate(trx);
  718. mutex_exit(&(rseg->mutex));
  719. }
  720. ins_undo = trx->insert_undo;
  721. upd_undo = trx->update_undo;
  722. if (!ins_undo || ins_undo->empty) {
  723. undo = upd_undo;
  724. } else if (!upd_undo || upd_undo->empty) {
  725. undo = ins_undo;
  726. } else if (upd_undo->top_undo_no > ins_undo->top_undo_no) {
  727. undo = upd_undo;
  728. } else {
  729. undo = ins_undo;
  730. }
  731. if (!undo || undo->empty
  732. || limit > undo->top_undo_no) {
  733. if ((trx->undo_no_arr)->n_used == 0) {
  734. /* Rollback is ending */
  735. mutex_enter(&(rseg->mutex));
  736. trx_roll_try_truncate(trx);
  737. mutex_exit(&(rseg->mutex));
  738. }
  739. mutex_exit(&(trx->undo_mutex));
  740. return(NULL);
  741. }
  742. if (undo == ins_undo) {
  743. is_insert = TRUE;
  744. } else {
  745. is_insert = FALSE;
  746. }
  747. *roll_ptr = trx_undo_build_roll_ptr(is_insert, (undo->rseg)->id,
  748. undo->top_page_no,
  749. undo->top_offset);
  750. mtr_start(&mtr);
  751. undo_rec = trx_roll_pop_top_rec(trx, undo, &mtr);
  752. undo_no = trx_undo_rec_get_undo_no(undo_rec);
  753. ut_ad(undo_no + 1 == trx->undo_no);
  754. /* We print rollback progress info if we are in a crash recovery
  755. and the transaction has at least 1000 row operations to undo. */
  756. if (trx == trx_roll_crash_recv_trx && trx_roll_max_undo_no > 1000) {
  757. progress_pct = 100 - (ulint)
  758. ((undo_no * 100) / trx_roll_max_undo_no);
  759. if (progress_pct != trx_roll_progress_printed_pct) {
  760. if (trx_roll_progress_printed_pct == 0) {
  761. fprintf(stderr,
  762. "\nInnoDB: Progress in percents:"
  763. " %lu", (ulong) progress_pct);
  764. } else {
  765. fprintf(stderr,
  766. " %lu", (ulong) progress_pct);
  767. }
  768. fflush(stderr);
  769. trx_roll_progress_printed_pct = progress_pct;
  770. }
  771. }
  772. trx->undo_no = undo_no;
  773. if (!trx_undo_arr_store_info(trx, undo_no)) {
  774. /* A query thread is already processing this undo log record */
  775. mutex_exit(&(trx->undo_mutex));
  776. mtr_commit(&mtr);
  777. goto try_again;
  778. }
  779. undo_rec_copy = trx_undo_rec_copy(undo_rec, heap);
  780. mutex_exit(&(trx->undo_mutex));
  781. mtr_commit(&mtr);
  782. return(undo_rec_copy);
  783. }
  784. /********************************************************************//**
  785. Reserves an undo log record for a query thread to undo. This should be
  786. called if the query thread gets the undo log record not using the pop
  787. function above.
  788. @return TRUE if succeeded */
  789. UNIV_INTERN
  790. ibool
  791. trx_undo_rec_reserve(
  792. /*=================*/
  793. trx_t* trx, /*!< in/out: transaction */
  794. undo_no_t undo_no)/*!< in: undo number of the record */
  795. {
  796. ibool ret;
  797. mutex_enter(&(trx->undo_mutex));
  798. ret = trx_undo_arr_store_info(trx, undo_no);
  799. mutex_exit(&(trx->undo_mutex));
  800. return(ret);
  801. }
  802. /*******************************************************************//**
  803. Releases a reserved undo record. */
  804. UNIV_INTERN
  805. void
  806. trx_undo_rec_release(
  807. /*=================*/
  808. trx_t* trx, /*!< in/out: transaction */
  809. undo_no_t undo_no)/*!< in: undo number */
  810. {
  811. trx_undo_arr_t* arr;
  812. mutex_enter(&(trx->undo_mutex));
  813. arr = trx->undo_no_arr;
  814. trx_undo_arr_remove_info(arr, undo_no);
  815. mutex_exit(&(trx->undo_mutex));
  816. }
  817. /*********************************************************************//**
  818. Starts a rollback operation. */
  819. UNIV_INTERN
  820. void
  821. trx_rollback(
  822. /*=========*/
  823. trx_t* trx, /*!< in: transaction */
  824. trx_sig_t* sig, /*!< in: signal starting the rollback */
  825. que_thr_t** next_thr)/*!< in/out: next query thread to run;
  826. if the value which is passed in is
  827. a pointer to a NULL pointer, then the
  828. calling function can start running
  829. a new query thread; if the passed value is
  830. NULL, the parameter is ignored */
  831. {
  832. que_t* roll_graph;
  833. que_thr_t* thr;
  834. /* que_thr_t* thr2; */
  835. ut_ad(mutex_own(&kernel_mutex));
  836. ut_ad((trx->undo_no_arr == NULL) || ((trx->undo_no_arr)->n_used == 0));
  837. /* Initialize the rollback field in the transaction */
  838. switch (sig->type) {
  839. case TRX_SIG_TOTAL_ROLLBACK:
  840. trx->roll_limit = 0;
  841. break;
  842. case TRX_SIG_ROLLBACK_TO_SAVEPT:
  843. trx->roll_limit = (sig->savept).least_undo_no;
  844. break;
  845. case TRX_SIG_ERROR_OCCURRED:
  846. trx->roll_limit = trx->last_sql_stat_start.least_undo_no;
  847. break;
  848. default:
  849. ut_error;
  850. }
  851. ut_a(trx->roll_limit <= trx->undo_no);
  852. trx->pages_undone = 0;
  853. if (trx->undo_no_arr == NULL) {
  854. trx->undo_no_arr = trx_undo_arr_create();
  855. }
  856. /* Build a 'query' graph which will perform the undo operations */
  857. roll_graph = trx_roll_graph_build(trx);
  858. trx->graph = roll_graph;
  859. trx->que_state = TRX_QUE_ROLLING_BACK;
  860. thr = que_fork_start_command(roll_graph);
  861. ut_ad(thr);
  862. /* thr2 = que_fork_start_command(roll_graph);
  863. ut_ad(thr2); */
  864. if (next_thr && (*next_thr == NULL)) {
  865. *next_thr = thr;
  866. /* srv_que_task_enqueue_low(thr2); */
  867. } else {
  868. srv_que_task_enqueue_low(thr);
  869. /* srv_que_task_enqueue_low(thr2); */
  870. }
  871. }
  872. /****************************************************************//**
  873. Builds an undo 'query' graph for a transaction. The actual rollback is
  874. performed by executing this query graph like a query subprocedure call.
  875. The reply about the completion of the rollback will be sent by this
  876. graph.
  877. @return own: the query graph */
  878. UNIV_INTERN
  879. que_t*
  880. trx_roll_graph_build(
  881. /*=================*/
  882. trx_t* trx) /*!< in: trx handle */
  883. {
  884. mem_heap_t* heap;
  885. que_fork_t* fork;
  886. que_thr_t* thr;
  887. /* que_thr_t* thr2; */
  888. ut_ad(mutex_own(&kernel_mutex));
  889. heap = mem_heap_create(512);
  890. fork = que_fork_create(NULL, NULL, QUE_FORK_ROLLBACK, heap);
  891. fork->trx = trx;
  892. thr = que_thr_create(fork, heap);
  893. /* thr2 = que_thr_create(fork, heap); */
  894. thr->child = row_undo_node_create(trx, thr, heap);
  895. /* thr2->child = row_undo_node_create(trx, thr2, heap); */
  896. return(fork);
  897. }
  898. /*********************************************************************//**
  899. Finishes error processing after the necessary partial rollback has been
  900. done. */
  901. static
  902. void
  903. trx_finish_error_processing(
  904. /*========================*/
  905. trx_t* trx) /*!< in: transaction */
  906. {
  907. trx_sig_t* sig;
  908. trx_sig_t* next_sig;
  909. ut_ad(mutex_own(&kernel_mutex));
  910. sig = UT_LIST_GET_FIRST(trx->signals);
  911. while (sig != NULL) {
  912. next_sig = UT_LIST_GET_NEXT(signals, sig);
  913. if (sig->type == TRX_SIG_ERROR_OCCURRED) {
  914. trx_sig_remove(trx, sig);
  915. }
  916. sig = next_sig;
  917. }
  918. trx->que_state = TRX_QUE_RUNNING;
  919. }
  920. /*********************************************************************//**
  921. Finishes a partial rollback operation. */
  922. static
  923. void
  924. trx_finish_partial_rollback_off_kernel(
  925. /*===================================*/
  926. trx_t* trx, /*!< in: transaction */
  927. que_thr_t** next_thr)/*!< in/out: next query thread to run;
  928. if the value which is passed in is a pointer
  929. to a NULL pointer, then the calling function
  930. can start running a new query thread; if this
  931. parameter is NULL, it is ignored */
  932. {
  933. trx_sig_t* sig;
  934. ut_ad(mutex_own(&kernel_mutex));
  935. sig = UT_LIST_GET_FIRST(trx->signals);
  936. /* Remove the signal from the signal queue and send reply message
  937. to it */
  938. trx_sig_reply(sig, next_thr);
  939. trx_sig_remove(trx, sig);
  940. trx->que_state = TRX_QUE_RUNNING;
  941. }
  942. /****************************************************************//**
  943. Finishes a transaction rollback. */
  944. UNIV_INTERN
  945. void
  946. trx_finish_rollback_off_kernel(
  947. /*===========================*/
  948. que_t* graph, /*!< in: undo graph which can now be freed */
  949. trx_t* trx, /*!< in: transaction */
  950. que_thr_t** next_thr)/*!< in/out: next query thread to run;
  951. if the value which is passed in is
  952. a pointer to a NULL pointer, then the
  953. calling function can start running
  954. a new query thread; if this parameter is
  955. NULL, it is ignored */
  956. {
  957. trx_sig_t* sig;
  958. trx_sig_t* next_sig;
  959. ut_ad(mutex_own(&kernel_mutex));
  960. ut_a(trx->undo_no_arr == NULL || trx->undo_no_arr->n_used == 0);
  961. /* Free the memory reserved by the undo graph */
  962. que_graph_free(graph);
  963. sig = UT_LIST_GET_FIRST(trx->signals);
  964. if (sig->type == TRX_SIG_ROLLBACK_TO_SAVEPT) {
  965. trx_finish_partial_rollback_off_kernel(trx, next_thr);
  966. return;
  967. } else if (sig->type == TRX_SIG_ERROR_OCCURRED) {
  968. trx_finish_error_processing(trx);
  969. return;
  970. }
  971. #ifdef UNIV_DEBUG
  972. if (lock_print_waits) {
  973. fprintf(stderr, "Trx " TRX_ID_FMT " rollback finished\n",
  974. (ullint) trx->id);
  975. }
  976. #endif /* UNIV_DEBUG */
  977. trx_commit_off_kernel(trx);
  978. /* Remove all TRX_SIG_TOTAL_ROLLBACK signals from the signal queue and
  979. send reply messages to them */
  980. trx->que_state = TRX_QUE_RUNNING;
  981. while (sig != NULL) {
  982. next_sig = UT_LIST_GET_NEXT(signals, sig);
  983. if (sig->type == TRX_SIG_TOTAL_ROLLBACK) {
  984. trx_sig_reply(sig, next_thr);
  985. trx_sig_remove(trx, sig);
  986. }
  987. sig = next_sig;
  988. }
  989. }
  990. /*********************************************************************//**
  991. Creates a rollback command node struct.
  992. @return own: rollback node struct */
  993. UNIV_INTERN
  994. roll_node_t*
  995. roll_node_create(
  996. /*=============*/
  997. mem_heap_t* heap) /*!< in: mem heap where created */
  998. {
  999. roll_node_t* node;
  1000. node = mem_heap_alloc(heap, sizeof(roll_node_t));
  1001. node->common.type = QUE_NODE_ROLLBACK;
  1002. node->state = ROLL_NODE_SEND;
  1003. node->partial = FALSE;
  1004. return(node);
  1005. }
  1006. /***********************************************************//**
  1007. Performs an execution step for a rollback command node in a query graph.
  1008. @return query thread to run next, or NULL */
  1009. UNIV_INTERN
  1010. que_thr_t*
  1011. trx_rollback_step(
  1012. /*==============*/
  1013. que_thr_t* thr) /*!< in: query thread */
  1014. {
  1015. roll_node_t* node;
  1016. ulint sig_no;
  1017. trx_savept_t* savept;
  1018. node = thr->run_node;
  1019. ut_ad(que_node_get_type(node) == QUE_NODE_ROLLBACK);
  1020. if (thr->prev_node == que_node_get_parent(node)) {
  1021. node->state = ROLL_NODE_SEND;
  1022. }
  1023. if (node->state == ROLL_NODE_SEND) {
  1024. mutex_enter(&kernel_mutex);
  1025. node->state = ROLL_NODE_WAIT;
  1026. if (node->partial) {
  1027. sig_no = TRX_SIG_ROLLBACK_TO_SAVEPT;
  1028. savept = &(node->savept);
  1029. } else {
  1030. sig_no = TRX_SIG_TOTAL_ROLLBACK;
  1031. savept = NULL;
  1032. }
  1033. /* Send a rollback signal to the transaction */
  1034. trx_sig_send(thr_get_trx(thr), sig_no, TRX_SIG_SELF, thr,
  1035. savept, NULL);
  1036. thr->state = QUE_THR_SIG_REPLY_WAIT;
  1037. mutex_exit(&kernel_mutex);
  1038. return(NULL);
  1039. }
  1040. ut_ad(node->state == ROLL_NODE_WAIT);
  1041. thr->run_node = que_node_get_parent(node);
  1042. return(thr);
  1043. }