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.

305 lines
11 KiB

17 years ago
9 years ago
17 years ago
17 years ago
16 years ago
11 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
16 years ago
16 years ago
16 years ago
16 years ago
9 years ago
16 years ago
16 years ago
16 years ago
16 years ago
9 years ago
16 years ago
9 years ago
16 years ago
16 years ago
16 years ago
9 years ago
16 years ago
16 years ago
16 years ago
9 years ago
16 years ago
16 years ago
16 years ago
9 years ago
16 years ago
16 years ago
16 years ago
9 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
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
  1. /*****************************************************************************
  2. Copyright (c) 1996, 2016, Oracle and/or its affiliates. All Rights Reserved.
  3. Copyright (c) 2017, MariaDB Corporation.
  4. This program is free software; you can redistribute it and/or modify it under
  5. the terms of the GNU General Public License as published by the Free Software
  6. Foundation; version 2 of the License.
  7. This program is distributed in the hope that it will be useful, but WITHOUT
  8. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  9. FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License along with
  11. this program; if not, write to the Free Software Foundation, Inc.,
  12. 51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA
  13. *****************************************************************************/
  14. /**************************************************//**
  15. @file include/trx0roll.h
  16. Transaction rollback
  17. Created 3/26/1996 Heikki Tuuri
  18. *******************************************************/
  19. #ifndef trx0roll_h
  20. #define trx0roll_h
  21. #include "univ.i"
  22. #include "btr0types.h"
  23. #include "trx0trx.h"
  24. #include "trx0types.h"
  25. #include "mtr0mtr.h"
  26. #include "trx0sys.h"
  27. extern bool trx_rollback_or_clean_is_active;
  28. extern const trx_t* trx_roll_crash_recv_trx;
  29. /*******************************************************************//**
  30. Determines if this transaction is rolling back an incomplete transaction
  31. in crash recovery.
  32. @return TRUE if trx is an incomplete transaction that is being rolled
  33. back in crash recovery */
  34. UNIV_INTERN
  35. ibool
  36. trx_is_recv(
  37. /*========*/
  38. const trx_t* trx); /*!< in: transaction */
  39. /*******************************************************************//**
  40. Returns a transaction savepoint taken at this point in time.
  41. @return savepoint */
  42. UNIV_INTERN
  43. trx_savept_t
  44. trx_savept_take(
  45. /*============*/
  46. trx_t* trx); /*!< in: transaction */
  47. /*******************************************************************//**
  48. Frees an undo number array. */
  49. UNIV_INTERN
  50. void
  51. trx_undo_arr_free(
  52. /*==============*/
  53. trx_undo_arr_t* arr); /*!< in: undo number array */
  54. /*******************************************************************//**
  55. Returns pointer to nth element in an undo number array.
  56. @return pointer to the nth element */
  57. UNIV_INLINE
  58. trx_undo_inf_t*
  59. trx_undo_arr_get_nth_info(
  60. /*======================*/
  61. trx_undo_arr_t* arr, /*!< in: undo number array */
  62. ulint n); /*!< in: position */
  63. /********************************************************************//**
  64. Pops the topmost record when the two undo logs of a transaction are seen
  65. as a single stack of records ordered by their undo numbers. Inserts the
  66. undo number of the popped undo record to the array of currently processed
  67. undo numbers in the transaction. When the query thread finishes processing
  68. of this undo record, it must be released with trx_undo_rec_release.
  69. @return undo log record copied to heap, NULL if none left, or if the
  70. undo number of the top record would be less than the limit */
  71. UNIV_INTERN
  72. trx_undo_rec_t*
  73. trx_roll_pop_top_rec_of_trx(
  74. /*========================*/
  75. trx_t* trx, /*!< in: transaction */
  76. undo_no_t limit, /*!< in: least undo number we need */
  77. roll_ptr_t* roll_ptr,/*!< out: roll pointer to undo record */
  78. mem_heap_t* heap); /*!< in: memory heap where copied */
  79. /********************************************************************//**
  80. Reserves an undo log record for a query thread to undo. This should be
  81. called if the query thread gets the undo log record not using the pop
  82. function above.
  83. @return TRUE if succeeded */
  84. UNIV_INTERN
  85. ibool
  86. trx_undo_rec_reserve(
  87. /*=================*/
  88. trx_t* trx, /*!< in/out: transaction */
  89. undo_no_t undo_no);/*!< in: undo number of the record */
  90. /*******************************************************************//**
  91. Releases a reserved undo record. */
  92. UNIV_INTERN
  93. void
  94. trx_undo_rec_release(
  95. /*=================*/
  96. trx_t* trx, /*!< in/out: transaction */
  97. undo_no_t undo_no);/*!< in: undo number */
  98. /** Report progress when rolling back a row of a recovered transaction.
  99. @return whether the rollback should be aborted due to pending shutdown */
  100. UNIV_INTERN
  101. bool
  102. trx_roll_must_shutdown();
  103. /*******************************************************************//**
  104. Rollback or clean up any incomplete transactions which were
  105. encountered in crash recovery. If the transaction already was
  106. committed, then we clean up a possible insert undo log. If the
  107. transaction was not yet committed, then we roll it back. */
  108. UNIV_INTERN
  109. void
  110. trx_rollback_or_clean_recovered(
  111. /*============================*/
  112. ibool all); /*!< in: FALSE=roll back dictionary transactions;
  113. TRUE=roll back all non-PREPARED transactions */
  114. /*******************************************************************//**
  115. Rollback or clean up any incomplete transactions which were
  116. encountered in crash recovery. If the transaction already was
  117. committed, then we clean up a possible insert undo log. If the
  118. transaction was not yet committed, then we roll it back.
  119. Note: this is done in a background thread.
  120. @return a dummy parameter */
  121. extern "C" UNIV_INTERN
  122. os_thread_ret_t
  123. DECLARE_THREAD(trx_rollback_or_clean_all_recovered)(
  124. /*================================================*/
  125. void* arg MY_ATTRIBUTE((unused)));
  126. /*!< in: a dummy parameter required by
  127. os_thread_create */
  128. /*********************************************************************//**
  129. Creates a rollback command node struct.
  130. @return own: rollback node struct */
  131. UNIV_INTERN
  132. roll_node_t*
  133. roll_node_create(
  134. /*=============*/
  135. mem_heap_t* heap); /*!< in: mem heap where created */
  136. /***********************************************************//**
  137. Performs an execution step for a rollback command node in a query graph.
  138. @return query thread to run next, or NULL */
  139. UNIV_INTERN
  140. que_thr_t*
  141. trx_rollback_step(
  142. /*==============*/
  143. que_thr_t* thr); /*!< in: query thread */
  144. /*******************************************************************//**
  145. Rollback a transaction used in MySQL.
  146. @return error code or DB_SUCCESS */
  147. UNIV_INTERN
  148. dberr_t
  149. trx_rollback_for_mysql(
  150. /*===================*/
  151. trx_t* trx) /*!< in/out: transaction */
  152. MY_ATTRIBUTE((nonnull));
  153. /*******************************************************************//**
  154. Rollback the latest SQL statement for MySQL.
  155. @return error code or DB_SUCCESS */
  156. UNIV_INTERN
  157. dberr_t
  158. trx_rollback_last_sql_stat_for_mysql(
  159. /*=================================*/
  160. trx_t* trx) /*!< in/out: transaction */
  161. MY_ATTRIBUTE((nonnull));
  162. /*******************************************************************//**
  163. Rollback a transaction to a given savepoint or do a complete rollback.
  164. @return error code or DB_SUCCESS */
  165. UNIV_INTERN
  166. dberr_t
  167. trx_rollback_to_savepoint(
  168. /*======================*/
  169. trx_t* trx, /*!< in: transaction handle */
  170. trx_savept_t* savept) /*!< in: pointer to savepoint undo number, if
  171. partial rollback requested, or NULL for
  172. complete rollback */
  173. MY_ATTRIBUTE((nonnull(1)));
  174. /*******************************************************************//**
  175. Rolls back a transaction back to a named savepoint. Modifications after the
  176. savepoint are undone but InnoDB does NOT release the corresponding locks
  177. which are stored in memory. If a lock is 'implicit', that is, a new inserted
  178. row holds a lock where the lock information is carried by the trx id stored in
  179. the row, these locks are naturally released in the rollback. Savepoints which
  180. were set after this savepoint are deleted.
  181. @return if no savepoint of the name found then DB_NO_SAVEPOINT,
  182. otherwise DB_SUCCESS */
  183. UNIV_INTERN
  184. dberr_t
  185. trx_rollback_to_savepoint_for_mysql(
  186. /*================================*/
  187. trx_t* trx, /*!< in: transaction handle */
  188. const char* savepoint_name, /*!< in: savepoint name */
  189. ib_int64_t* mysql_binlog_cache_pos) /*!< out: the MySQL binlog cache
  190. position corresponding to this
  191. savepoint; MySQL needs this
  192. information to remove the
  193. binlog entries of the queries
  194. executed after the savepoint */
  195. MY_ATTRIBUTE((nonnull, warn_unused_result));
  196. /*******************************************************************//**
  197. Creates a named savepoint. If the transaction is not yet started, starts it.
  198. If there is already a savepoint of the same name, this call erases that old
  199. savepoint and replaces it with a new. Savepoints are deleted in a transaction
  200. commit or rollback.
  201. @return always DB_SUCCESS */
  202. UNIV_INTERN
  203. dberr_t
  204. trx_savepoint_for_mysql(
  205. /*====================*/
  206. trx_t* trx, /*!< in: transaction handle */
  207. const char* savepoint_name, /*!< in: savepoint name */
  208. ib_int64_t binlog_cache_pos) /*!< in: MySQL binlog cache
  209. position corresponding to this
  210. connection at the time of the
  211. savepoint */
  212. MY_ATTRIBUTE((nonnull));
  213. /*******************************************************************//**
  214. Releases a named savepoint. Savepoints which
  215. were set after this savepoint are deleted.
  216. @return if no savepoint of the name found then DB_NO_SAVEPOINT,
  217. otherwise DB_SUCCESS */
  218. UNIV_INTERN
  219. dberr_t
  220. trx_release_savepoint_for_mysql(
  221. /*============================*/
  222. trx_t* trx, /*!< in: transaction handle */
  223. const char* savepoint_name) /*!< in: savepoint name */
  224. MY_ATTRIBUTE((nonnull, warn_unused_result));
  225. /*******************************************************************//**
  226. Frees savepoint structs starting from savep. */
  227. UNIV_INTERN
  228. void
  229. trx_roll_savepoints_free(
  230. /*=====================*/
  231. trx_t* trx, /*!< in: transaction handle */
  232. trx_named_savept_t* savep); /*!< in: free all savepoints > this one;
  233. if this is NULL, free all savepoints
  234. of trx */
  235. /** A cell of trx_undo_arr_t; used during a rollback and a purge */
  236. struct trx_undo_inf_t{
  237. ibool in_use; /*!< true if cell is being used */
  238. trx_id_t trx_no; /*!< transaction number: not defined during
  239. a rollback */
  240. undo_no_t undo_no;/*!< undo number of an undo record */
  241. };
  242. /** During a rollback and a purge, undo numbers of undo records currently being
  243. processed are stored in this array */
  244. struct trx_undo_arr_t{
  245. ulint n_cells; /*!< number of cells in the array */
  246. ulint n_used; /*!< number of cells in use */
  247. trx_undo_inf_t* infos; /*!< the array of undo infos */
  248. mem_heap_t* heap; /*!< memory heap from which allocated */
  249. };
  250. /** Rollback node states */
  251. enum roll_node_state {
  252. ROLL_NODE_NONE = 0, /*!< Unknown state */
  253. ROLL_NODE_SEND, /*!< about to send a rollback signal to
  254. the transaction */
  255. ROLL_NODE_WAIT /*!< rollback signal sent to the
  256. transaction, waiting for completion */
  257. };
  258. /** Rollback command node in a query graph */
  259. struct roll_node_t{
  260. que_common_t common; /*!< node type: QUE_NODE_ROLLBACK */
  261. enum roll_node_state state; /*!< node execution state */
  262. ibool partial;/*!< TRUE if we want a partial
  263. rollback */
  264. trx_savept_t savept; /*!< savepoint to which to
  265. roll back, in the case of a
  266. partial rollback */
  267. que_thr_t* undo_thr;/*!< undo query graph */
  268. };
  269. /** A savepoint set with SQL's "SAVEPOINT savepoint_id" command */
  270. struct trx_named_savept_t{
  271. char* name; /*!< savepoint name */
  272. trx_savept_t savept; /*!< the undo number corresponding to
  273. the savepoint */
  274. ib_int64_t mysql_binlog_cache_pos;
  275. /*!< the MySQL binlog cache position
  276. corresponding to this savepoint, not
  277. defined if the MySQL binlogging is not
  278. enabled */
  279. UT_LIST_NODE_T(trx_named_savept_t)
  280. trx_savepoints; /*!< the list of savepoints of a
  281. transaction */
  282. };
  283. #ifndef UNIV_NONINL
  284. #include "trx0roll.ic"
  285. #endif
  286. #endif