1 | /***************************************************************************** |
2 | |
3 | Copyright (c) 1996, 2016, Oracle and/or its affiliates. All Rights Reserved. |
4 | Copyright (c) 2015, 2018, MariaDB Corporation. |
5 | |
6 | This program is free software; you can redistribute it and/or modify it under |
7 | the terms of the GNU General Public License as published by the Free Software |
8 | Foundation; version 2 of the License. |
9 | |
10 | This program is distributed in the hope that it will be useful, but WITHOUT |
11 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
12 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
13 | |
14 | You should have received a copy of the GNU General Public License along with |
15 | this program; if not, write to the Free Software Foundation, Inc., |
16 | 51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA |
17 | |
18 | *****************************************************************************/ |
19 | |
20 | /**************************************************//** |
21 | @file include/trx0roll.h |
22 | Transaction rollback |
23 | |
24 | Created 3/26/1996 Heikki Tuuri |
25 | *******************************************************/ |
26 | |
27 | #ifndef trx0roll_h |
28 | #define trx0roll_h |
29 | |
30 | #include "univ.i" |
31 | #include "trx0trx.h" |
32 | #include "trx0types.h" |
33 | #include "mtr0mtr.h" |
34 | #include "trx0sys.h" |
35 | |
36 | extern bool trx_rollback_is_active; |
37 | extern const trx_t* trx_roll_crash_recv_trx; |
38 | |
39 | /*******************************************************************//** |
40 | Determines if this transaction is rolling back an incomplete transaction |
41 | in crash recovery. |
42 | @return TRUE if trx is an incomplete transaction that is being rolled |
43 | back in crash recovery */ |
44 | ibool |
45 | trx_is_recv( |
46 | /*========*/ |
47 | const trx_t* trx); /*!< in: transaction */ |
48 | /*******************************************************************//** |
49 | Returns a transaction savepoint taken at this point in time. |
50 | @return savepoint */ |
51 | trx_savept_t |
52 | trx_savept_take( |
53 | /*============*/ |
54 | trx_t* trx); /*!< in: transaction */ |
55 | |
56 | /** Get the last undo log record of a transaction (for rollback). |
57 | @param[in,out] trx transaction |
58 | @param[out] roll_ptr DB_ROLL_PTR to the undo record |
59 | @param[in,out] heap memory heap for allocation |
60 | @return undo log record copied to heap |
61 | @retval NULL if none left or the roll_limit (savepoint) was reached */ |
62 | trx_undo_rec_t* |
63 | trx_roll_pop_top_rec_of_trx(trx_t* trx, roll_ptr_t* roll_ptr, mem_heap_t* heap) |
64 | MY_ATTRIBUTE((nonnull, warn_unused_result)); |
65 | |
66 | /** Report progress when rolling back a row of a recovered transaction. */ |
67 | void trx_roll_report_progress(); |
68 | /*******************************************************************//** |
69 | Rollback or clean up any incomplete transactions which were |
70 | encountered in crash recovery. If the transaction already was |
71 | committed, then we clean up a possible insert undo log. If the |
72 | transaction was not yet committed, then we roll it back. |
73 | @param all true=roll back all recovered active transactions; |
74 | false=roll back any incomplete dictionary transaction */ |
75 | void |
76 | trx_rollback_recovered(bool all); |
77 | /*******************************************************************//** |
78 | Rollback or clean up any incomplete transactions which were |
79 | encountered in crash recovery. If the transaction already was |
80 | committed, then we clean up a possible insert undo log. If the |
81 | transaction was not yet committed, then we roll it back. |
82 | Note: this is done in a background thread. |
83 | @return a dummy parameter */ |
84 | extern "C" |
85 | os_thread_ret_t |
86 | DECLARE_THREAD(trx_rollback_all_recovered)(void*); |
87 | /*********************************************************************//** |
88 | Creates a rollback command node struct. |
89 | @return own: rollback node struct */ |
90 | roll_node_t* |
91 | roll_node_create( |
92 | /*=============*/ |
93 | mem_heap_t* heap); /*!< in: mem heap where created */ |
94 | /***********************************************************//** |
95 | Performs an execution step for a rollback command node in a query graph. |
96 | @return query thread to run next, or NULL */ |
97 | que_thr_t* |
98 | trx_rollback_step( |
99 | /*==============*/ |
100 | que_thr_t* thr); /*!< in: query thread */ |
101 | /*******************************************************************//** |
102 | Rollback a transaction used in MySQL. |
103 | @return error code or DB_SUCCESS */ |
104 | dberr_t |
105 | trx_rollback_for_mysql( |
106 | /*===================*/ |
107 | trx_t* trx) /*!< in/out: transaction */ |
108 | MY_ATTRIBUTE((nonnull)); |
109 | /*******************************************************************//** |
110 | Rollback the latest SQL statement for MySQL. |
111 | @return error code or DB_SUCCESS */ |
112 | dberr_t |
113 | trx_rollback_last_sql_stat_for_mysql( |
114 | /*=================================*/ |
115 | trx_t* trx) /*!< in/out: transaction */ |
116 | MY_ATTRIBUTE((nonnull)); |
117 | /*******************************************************************//** |
118 | Rollback a transaction to a given savepoint or do a complete rollback. |
119 | @return error code or DB_SUCCESS */ |
120 | dberr_t |
121 | trx_rollback_to_savepoint( |
122 | /*======================*/ |
123 | trx_t* trx, /*!< in: transaction handle */ |
124 | trx_savept_t* savept) /*!< in: pointer to savepoint undo number, if |
125 | partial rollback requested, or NULL for |
126 | complete rollback */ |
127 | MY_ATTRIBUTE((nonnull(1))); |
128 | /*******************************************************************//** |
129 | Rolls back a transaction back to a named savepoint. Modifications after the |
130 | savepoint are undone but InnoDB does NOT release the corresponding locks |
131 | which are stored in memory. If a lock is 'implicit', that is, a new inserted |
132 | row holds a lock where the lock information is carried by the trx id stored in |
133 | the row, these locks are naturally released in the rollback. Savepoints which |
134 | were set after this savepoint are deleted. |
135 | @return if no savepoint of the name found then DB_NO_SAVEPOINT, |
136 | otherwise DB_SUCCESS */ |
137 | dberr_t |
138 | trx_rollback_to_savepoint_for_mysql( |
139 | /*================================*/ |
140 | trx_t* trx, /*!< in: transaction handle */ |
141 | const char* savepoint_name, /*!< in: savepoint name */ |
142 | int64_t* mysql_binlog_cache_pos) /*!< out: the MySQL binlog cache |
143 | position corresponding to this |
144 | savepoint; MySQL needs this |
145 | information to remove the |
146 | binlog entries of the queries |
147 | executed after the savepoint */ |
148 | MY_ATTRIBUTE((nonnull, warn_unused_result)); |
149 | /*******************************************************************//** |
150 | Creates a named savepoint. If the transaction is not yet started, starts it. |
151 | If there is already a savepoint of the same name, this call erases that old |
152 | savepoint and replaces it with a new. Savepoints are deleted in a transaction |
153 | commit or rollback. |
154 | @return always DB_SUCCESS */ |
155 | dberr_t |
156 | trx_savepoint_for_mysql( |
157 | /*====================*/ |
158 | trx_t* trx, /*!< in: transaction handle */ |
159 | const char* savepoint_name, /*!< in: savepoint name */ |
160 | int64_t binlog_cache_pos) /*!< in: MySQL binlog cache |
161 | position corresponding to this |
162 | connection at the time of the |
163 | savepoint */ |
164 | MY_ATTRIBUTE((nonnull)); |
165 | /*******************************************************************//** |
166 | Releases a named savepoint. Savepoints which |
167 | were set after this savepoint are deleted. |
168 | @return if no savepoint of the name found then DB_NO_SAVEPOINT, |
169 | otherwise DB_SUCCESS */ |
170 | dberr_t |
171 | trx_release_savepoint_for_mysql( |
172 | /*============================*/ |
173 | trx_t* trx, /*!< in: transaction handle */ |
174 | const char* savepoint_name) /*!< in: savepoint name */ |
175 | MY_ATTRIBUTE((nonnull, warn_unused_result)); |
176 | /*******************************************************************//** |
177 | Frees savepoint structs starting from savep. */ |
178 | void |
179 | trx_roll_savepoints_free( |
180 | /*=====================*/ |
181 | trx_t* trx, /*!< in: transaction handle */ |
182 | trx_named_savept_t* savep); /*!< in: free all savepoints > this one; |
183 | if this is NULL, free all savepoints |
184 | of trx */ |
185 | /** Rollback node states */ |
186 | enum roll_node_state { |
187 | ROLL_NODE_NONE = 0, /*!< Unknown state */ |
188 | ROLL_NODE_SEND, /*!< about to send a rollback signal to |
189 | the transaction */ |
190 | ROLL_NODE_WAIT /*!< rollback signal sent to the |
191 | transaction, waiting for completion */ |
192 | }; |
193 | |
194 | /** Rollback command node in a query graph */ |
195 | struct roll_node_t{ |
196 | que_common_t common; /*!< node type: QUE_NODE_ROLLBACK */ |
197 | enum roll_node_state state; /*!< node execution state */ |
198 | bool partial;/*!< TRUE if we want a partial |
199 | rollback */ |
200 | trx_savept_t savept; /*!< savepoint to which to |
201 | roll back, in the case of a |
202 | partial rollback */ |
203 | que_thr_t* undo_thr;/*!< undo query graph */ |
204 | }; |
205 | |
206 | /** A savepoint set with SQL's "SAVEPOINT savepoint_id" command */ |
207 | struct trx_named_savept_t{ |
208 | char* name; /*!< savepoint name */ |
209 | trx_savept_t savept; /*!< the undo number corresponding to |
210 | the savepoint */ |
211 | int64_t mysql_binlog_cache_pos; |
212 | /*!< the MySQL binlog cache position |
213 | corresponding to this savepoint, not |
214 | defined if the MySQL binlogging is not |
215 | enabled */ |
216 | UT_LIST_NODE_T(trx_named_savept_t) |
217 | trx_savepoints; /*!< the list of savepoints of a |
218 | transaction */ |
219 | }; |
220 | |
221 | #endif |
222 | |