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/trx0rseg.h
29 Rollback segment
30 
31 Created 3/26/1996 Heikki Tuuri
32 *******************************************************/
33 
34 #ifndef trx0rseg_h
35 #define trx0rseg_h
36 
37 #include "univ.i"
38 #include "trx0types.h"
39 #include "trx0sys.h"
40 #include "ut0bh.h"
41 
42 /******************************************************************//**
43 Gets a rollback segment header.
44 @return	rollback segment header, page x-latched */
45 UNIV_INLINE
46 trx_rsegf_t*
47 trx_rsegf_get(
48 /*==========*/
49 	ulint	space,		/*!< in: space where placed */
50 	ulint	zip_size,	/*!< in: compressed page size in bytes
51 				or 0 for uncompressed pages */
52 	ulint	page_no,	/*!< in: page number of the header */
53 	mtr_t*	mtr);		/*!< in: mtr */
54 /******************************************************************//**
55 Gets a newly created rollback segment header.
56 @return	rollback segment header, page x-latched */
57 UNIV_INLINE
58 trx_rsegf_t*
59 trx_rsegf_get_new(
60 /*==============*/
61 	ulint	space,		/*!< in: space where placed */
62 	ulint	zip_size,	/*!< in: compressed page size in bytes
63 				or 0 for uncompressed pages */
64 	ulint	page_no,	/*!< in: page number of the header */
65 	mtr_t*	mtr);		/*!< in: mtr */
66 /***************************************************************//**
67 Gets the file page number of the nth undo log slot.
68 @return	page number of the undo log segment */
69 UNIV_INLINE
70 ulint
71 trx_rsegf_get_nth_undo(
72 /*===================*/
73 	trx_rsegf_t*	rsegf,	/*!< in: rollback segment header */
74 	ulint		n,	/*!< in: index of slot */
75 	mtr_t*		mtr);	/*!< in: mtr */
76 /***************************************************************//**
77 Sets the file page number of the nth undo log slot. */
78 UNIV_INLINE
79 void
80 trx_rsegf_set_nth_undo(
81 /*===================*/
82 	trx_rsegf_t*	rsegf,	/*!< in: rollback segment header */
83 	ulint		n,	/*!< in: index of slot */
84 	ulint		page_no,/*!< in: page number of the undo log segment */
85 	mtr_t*		mtr);	/*!< in: mtr */
86 /****************************************************************//**
87 Looks for a free slot for an undo log segment.
88 @return	slot index or ULINT_UNDEFINED if not found */
89 UNIV_INLINE
90 ulint
91 trx_rsegf_undo_find_free(
92 /*=====================*/
93 	trx_rsegf_t*	rsegf,	/*!< in: rollback segment header */
94 	mtr_t*		mtr);	/*!< in: mtr */
95 /******************************************************************//**
96 Looks for a rollback segment, based on the rollback segment id.
97 @return	rollback segment */
98 UNIV_INLINE
99 trx_rseg_t*
100 trx_rseg_get_on_id(
101 /*===============*/
102 	ulint	id);		/*!< in: rollback segment id */
103 /****************************************************************//**
104 Creates a rollback segment header. This function is called only when
105 a new rollback segment is created in the database.
106 @return	page number of the created segment, FIL_NULL if fail */
107 UNIV_INTERN
108 ulint
109 trx_rseg_header_create(
110 /*===================*/
111 	ulint	space,		/*!< in: space id */
112 	ulint	zip_size,	/*!< in: compressed page size in bytes
113 				or 0 for uncompressed pages */
114 	ulint	max_size,	/*!< in: max size in pages */
115 	ulint	rseg_slot_no,	/*!< in: rseg id == slot number in trx sys */
116 	mtr_t*	mtr);		/*!< in: mtr */
117 /*********************************************************************//**
118 Creates the memory copies for rollback segments and initializes the
119 rseg array in trx_sys at a database startup. */
120 UNIV_INTERN
121 void
122 trx_rseg_array_init(
123 /*================*/
124 	trx_sysf_t*	sys_header,	/*!< in/out: trx system header */
125 	ib_bh_t*	ib_bh,		/*!< in: rseg queue */
126 	mtr_t*		mtr);		/*!< in/out: mtr */
127 /***************************************************************************
128 Free's an instance of the rollback segment in memory. */
129 UNIV_INTERN
130 void
131 trx_rseg_mem_free(
132 /*==============*/
133 	trx_rseg_t*	rseg);		/*!< in, own: instance to free */
134 
135 /*********************************************************************
136 Creates a rollback segment. */
137 UNIV_INTERN
138 trx_rseg_t*
139 trx_rseg_create(
140 /*============*/
141 	ulint	space);			/*!< in: id of UNDO tablespace */
142 
143 /********************************************************************
144 Get the number of unique rollback tablespaces in use except space id 0.
145 The last space id will be the sentinel value ULINT_UNDEFINED. The array
146 will be sorted on space id. Note: space_ids should have have space for
147 TRX_SYS_N_RSEGS + 1 elements.
148 @return number of unique rollback tablespaces in use. */
149 UNIV_INTERN
150 ulint
151 trx_rseg_get_n_undo_tablespaces(
152 /*============================*/
153 	ulint*		space_ids);	/*!< out: array of space ids of
154 					UNDO tablespaces */
155 /* Number of undo log slots in a rollback segment file copy */
156 #define TRX_RSEG_N_SLOTS	(UNIV_PAGE_SIZE / 16)
157 
158 /* Maximum number of transactions supported by a single rollback segment */
159 #define TRX_RSEG_MAX_N_TRXS	(TRX_RSEG_N_SLOTS / 2)
160 
161 /* The rollback segment memory object */
162 struct trx_rseg_t{
163 	/*--------------------------------------------------------*/
164 	ulint		id;	/*!< rollback segment id == the index of
165 				its slot in the trx system file copy */
166 	ib_mutex_t		mutex;	/*!< mutex protecting the fields in this
167 				struct except id, which is constant */
168 	ulint		space;	/*!< space where the rollback segment is
169 				header is placed */
170 	ulint		zip_size;/* compressed page size of space
171 				in bytes, or 0 for uncompressed spaces */
172 	ulint		page_no;/* page number of the rollback segment
173 				header */
174 	ulint		max_size;/* maximum allowed size in pages */
175 	ulint		curr_size;/* current size in pages */
176 	/*--------------------------------------------------------*/
177 	/* Fields for update undo logs */
178 	UT_LIST_BASE_NODE_T(trx_undo_t) update_undo_list;
179 					/* List of update undo logs */
180 	UT_LIST_BASE_NODE_T(trx_undo_t) update_undo_cached;
181 					/* List of update undo log segments
182 					cached for fast reuse */
183 	/*--------------------------------------------------------*/
184 	/* Fields for insert undo logs */
185 	UT_LIST_BASE_NODE_T(trx_undo_t) insert_undo_list;
186 					/* List of insert undo logs */
187 	UT_LIST_BASE_NODE_T(trx_undo_t) insert_undo_cached;
188 					/* List of insert undo log segments
189 					cached for fast reuse */
190 	/*--------------------------------------------------------*/
191 	ulint		last_page_no;	/*!< Page number of the last not yet
192 					purged log header in the history list;
193 					FIL_NULL if all list purged */
194 	ulint		last_offset;	/*!< Byte offset of the last not yet
195 					purged log header */
196 	trx_id_t	last_trx_no;	/*!< Transaction number of the last not
197 					yet purged log */
198 	ibool		last_del_marks;	/*!< TRUE if the last not yet purged log
199 					needs purging */
200 };
201 
202 /** For prioritising the rollback segments for purge. */
203 struct rseg_queue_t {
204         trx_id_t	trx_no;         /*!< trx_rseg_t::last_trx_no */
205         trx_rseg_t*     rseg;           /*!< Rollback segment */
206 };
207 
208 /* Undo log segment slot in a rollback segment header */
209 /*-------------------------------------------------------------*/
210 #define	TRX_RSEG_SLOT_PAGE_NO	0	/* Page number of the header page of
211 					an undo log segment */
212 /*-------------------------------------------------------------*/
213 /* Slot size */
214 #define TRX_RSEG_SLOT_SIZE	4
215 
216 /* The offset of the rollback segment header on its page */
217 #define	TRX_RSEG		FSEG_PAGE_DATA
218 
219 /* Transaction rollback segment header */
220 /*-------------------------------------------------------------*/
221 #define	TRX_RSEG_MAX_SIZE	0	/* Maximum allowed size for rollback
222 					segment in pages */
223 #define	TRX_RSEG_HISTORY_SIZE	4	/* Number of file pages occupied
224 					by the logs in the history list */
225 #define	TRX_RSEG_HISTORY	8	/* The update undo logs for committed
226 					transactions */
227 #define	TRX_RSEG_FSEG_HEADER	(8 + FLST_BASE_NODE_SIZE)
228 					/* Header for the file segment where
229 					this page is placed */
230 #define TRX_RSEG_UNDO_SLOTS	(8 + FLST_BASE_NODE_SIZE + FSEG_HEADER_SIZE)
231 					/* Undo log segment slots */
232 /*-------------------------------------------------------------*/
233 
234 #ifndef UNIV_NONINL
235 #include "trx0rseg.ic"
236 #endif
237 
238 #endif
239