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