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.

1317 lines
30 KiB

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