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