1 /*****************************************************************************
2 
3 Copyright (c) 1996, 2011, 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/trx0purge.h
29 Purge old versions
30 
31 Created 3/26/1996 Heikki Tuuri
32 *******************************************************/
33 
34 #ifndef trx0purge_h
35 #define trx0purge_h
36 
37 #include "univ.i"
38 #include "trx0types.h"
39 #include "mtr0mtr.h"
40 #include "trx0sys.h"
41 #include "que0types.h"
42 #include "page0page.h"
43 #include "usr0sess.h"
44 #include "fil0fil.h"
45 
46 /** The global data structure coordinating a purge */
47 extern trx_purge_t*	purge_sys;
48 
49 /** A dummy undo record used as a return value when we have a whole undo log
50 which needs no purge */
51 extern trx_undo_rec_t	trx_purge_dummy_rec;
52 
53 /********************************************************************//**
54 Calculates the file address of an undo log header when we have the file
55 address of its history list node.
56 @return	file address of the log */
57 UNIV_INLINE
58 fil_addr_t
59 trx_purge_get_log_from_hist(
60 /*========================*/
61 	fil_addr_t	node_addr);	/*!< in: file address of the history
62 					list node of the log */
63 /********************************************************************//**
64 Creates the global purge system control structure and inits the history
65 mutex. */
66 UNIV_INTERN
67 void
68 trx_purge_sys_create(
69 /*=================*/
70 	ulint		n_purge_threads,/*!< in: number of purge threads */
71 	ib_bh_t*	ib_bh);		/*!< in/own: UNDO log min binary heap*/
72 /********************************************************************//**
73 Frees the global purge system control structure. */
74 UNIV_INTERN
75 void
76 trx_purge_sys_close(void);
77 /*======================*/
78 /************************************************************************
79 Adds the update undo log as the first log in the history list. Removes the
80 update undo log segment from the rseg slot if it is too big for reuse. */
81 UNIV_INTERN
82 void
83 trx_purge_add_update_undo_to_history(
84 /*=================================*/
85 	trx_t*	trx,		/*!< in: transaction */
86 	page_t*	undo_page,	/*!< in: update undo log header page,
87 				x-latched */
88 	mtr_t*	mtr);		/*!< in: mtr */
89 /*******************************************************************//**
90 This function runs a purge batch.
91 @return	number of undo log pages handled in the batch */
92 UNIV_INTERN
93 ulint
94 trx_purge(
95 /*======*/
96 	ulint	n_purge_threads,	/*!< in: number of purge tasks to
97 					submit to task queue. */
98 	ulint	limit,			/*!< in: the maximum number of
99 					records to purge in one batch */
100 	bool	truncate);		/*!< in: truncate history if true */
101 /*******************************************************************//**
102 Stop purge and wait for it to stop, move to PURGE_STATE_STOP. */
103 UNIV_INTERN
104 void
105 trx_purge_stop(void);
106 /*================*/
107 /*******************************************************************//**
108 Resume purge, move to PURGE_STATE_RUN. */
109 UNIV_INTERN
110 void
111 trx_purge_run(void);
112 /*================*/
113 
114 /** Purge states */
115 enum purge_state_t {
116 	PURGE_STATE_INIT,		/*!< Purge instance created */
117 	PURGE_STATE_RUN,		/*!< Purge should be running */
118 	PURGE_STATE_STOP,		/*!< Purge should be stopped */
119 	PURGE_STATE_EXIT,		/*!< Purge has been shutdown */
120 	PURGE_STATE_DISABLED		/*!< Purge was never started */
121 };
122 
123 /*******************************************************************//**
124 Get the purge state.
125 @return purge state. */
126 UNIV_INTERN
127 purge_state_t
128 trx_purge_state(void);
129 /*=================*/
130 
131 /** This is the purge pointer/iterator. We need both the undo no and the
132 transaction no up to which purge has parsed and applied the records. */
133 struct purge_iter_t {
134 	trx_id_t	trx_no;		/*!< Purge has advanced past all
135 					transactions whose number is less
136 					than this */
137 	undo_no_t	undo_no;	/*!< Purge has advanced past all records
138 					whose undo number is less than this */
139 };
140 
141 /** The control structure used in the purge operation */
142 struct trx_purge_t{
143 	sess_t*		sess;		/*!< System session running the purge
144 					query */
145 	trx_t*		trx;		/*!< System transaction running the
146 					purge query: this trx is not in the
147 					trx list of the trx system and it
148 					never ends */
149 	rw_lock_t	latch;		/*!< The latch protecting the purge
150 					view. A purge operation must acquire an
151 					x-latch here for the instant at which
152 					it changes the purge view: an undo
153 					log operation can prevent this by
154 					obtaining an s-latch here. It also
155 					protects state and running */
156 	os_event_t	event;		/*!< State signal event */
157 	ulint		n_stop;		/*!< Counter to track number stops */
158 	volatile bool	running;	/*!< true, if purge is active,
159 					we check this without the latch too */
160 	volatile purge_state_t	state;	/*!< Purge coordinator thread states,
161 					we check this in several places
162 					without holding the latch. */
163 	que_t*		query;		/*!< The query graph which will do the
164 					parallelized purge operation */
165 	read_view_t*	view;		/*!< The purge will not remove undo logs
166 					which are >= this view (purge view) */
167 	volatile ulint	n_submitted;	/*!< Count of total tasks submitted
168 					to the task queue */
169 	volatile ulint	n_completed;	/*!< Count of total tasks completed */
170 
171 	/*------------------------------*/
172 	/* The following two fields form the 'purge pointer' which advances
173 	during a purge, and which is used in history list truncation */
174 
175 	purge_iter_t	iter;		/* Limit up to which we have read and
176 					parsed the UNDO log records.  Not
177 					necessarily purged from the indexes.
178 					Note that this can never be less than
179 					the limit below, we check for this
180 					invariant in trx0purge.cc */
181 	purge_iter_t	limit;		/* The 'purge pointer' which advances
182 					during a purge, and which is used in
183 					history list truncation */
184 #ifdef UNIV_DEBUG
185 	purge_iter_t	done;		/* Indicate 'purge pointer' which have
186 					purged already accurately. */
187 #endif /* UNIV_DEBUG */
188 	/*-----------------------------*/
189 	ibool		next_stored;	/*!< TRUE if the info of the next record
190 					to purge is stored below: if yes, then
191 					the transaction number and the undo
192 					number of the record are stored in
193 					purge_trx_no and purge_undo_no above */
194 	trx_rseg_t*	rseg;		/*!< Rollback segment for the next undo
195 					record to purge */
196 	ulint		page_no;	/*!< Page number for the next undo
197 					record to purge, page number of the
198 					log header, if dummy record */
199 	ulint		offset;		/*!< Page offset for the next undo
200 					record to purge, 0 if the dummy
201 					record */
202 	ulint		hdr_page_no;	/*!< Header page of the undo log where
203 					the next record to purge belongs */
204 	ulint		hdr_offset;	/*!< Header byte offset on the page */
205 	/*-----------------------------*/
206 	mem_heap_t*	heap;		/*!< Temporary storage used during a
207 					purge: can be emptied after purge
208 					completes */
209 	/*-----------------------------*/
210 	ib_bh_t*	ib_bh;		/*!< Binary min-heap, ordered on
211 					rseg_queue_t::trx_no. It is protected
212 					by the bh_mutex */
213 	ib_mutex_t		bh_mutex;	/*!< Mutex protecting ib_bh */
214 };
215 
216 /** Info required to purge a record */
217 struct trx_purge_rec_t {
218 	trx_undo_rec_t*	undo_rec;	/*!< Record to purge */
219 	roll_ptr_t	roll_ptr;	/*!< File pointr to UNDO record */
220 };
221 
222 #ifndef UNIV_NONINL
223 #include "trx0purge.ic"
224 #endif
225 
226 #endif
227