1 /*****************************************************************************
2 
3 Copyright (c) 1995, 2013, Oracle and/or its affiliates. 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 Street, Suite 500, Boston, MA 02110-1335 USA
16 
17 *****************************************************************************/
18 
19 /**************************************************//**
20 @file include/mtr0mtr.h
21 Mini-transaction buffer
22 
23 Created 11/26/1995 Heikki Tuuri
24 *******************************************************/
25 
26 #ifndef mtr0mtr_h
27 #define mtr0mtr_h
28 
29 #include "univ.i"
30 #include "mem0mem.h"
31 #include "dyn0dyn.h"
32 #include "buf0types.h"
33 #include "sync0rw.h"
34 #include "ut0byte.h"
35 #include "mtr0types.h"
36 #include "page0types.h"
37 
38 /* Logging modes for a mini-transaction */
39 #define MTR_LOG_ALL		21	/* default mode: log all operations
40 					modifying disk-based data */
41 #define	MTR_LOG_NONE		22	/* log no operations */
42 /*#define	MTR_LOG_SPACE	23 */	/* log only operations modifying
43 					file space page allocation data
44 					(operations in fsp0fsp.* ) */
45 #define	MTR_LOG_SHORT_INSERTS	24	/* inserts are logged in a shorter
46 					form */
47 
48 /* Types for the mlock objects to store in the mtr memo; NOTE that the
49 first 3 values must be RW_S_LATCH, RW_X_LATCH, RW_NO_LATCH */
50 #define	MTR_MEMO_PAGE_S_FIX	RW_S_LATCH
51 #define	MTR_MEMO_PAGE_X_FIX	RW_X_LATCH
52 #define	MTR_MEMO_BUF_FIX	RW_NO_LATCH
53 #ifdef UNIV_DEBUG
54 # define MTR_MEMO_MODIFY	54
55 #endif /* UNIV_DEBUG */
56 #define	MTR_MEMO_S_LOCK		55
57 #define	MTR_MEMO_X_LOCK		56
58 
59 /** @name Log item types
60 The log items are declared 'byte' so that the compiler can warn if val
61 and type parameters are switched in a call to mlog_write_ulint. NOTE!
62 For 1 - 8 bytes, the flag value must give the length also! @{ */
63 #define	MLOG_SINGLE_REC_FLAG	128		/*!< if the mtr contains only
64 						one log record for one page,
65 						i.e., write_initial_log_record
66 						has been called only once,
67 						this flag is ORed to the type
68 						of that first log record */
69 #define	MLOG_1BYTE		(1)		/*!< one byte is written */
70 #define	MLOG_2BYTES		(2)		/*!< 2 bytes ... */
71 #define	MLOG_4BYTES		(4)		/*!< 4 bytes ... */
72 #define	MLOG_8BYTES		(8)		/*!< 8 bytes ... */
73 #define	MLOG_REC_INSERT		((byte)9)	/*!< record insert */
74 #define	MLOG_REC_CLUST_DELETE_MARK ((byte)10)	/*!< mark clustered index record
75 						deleted */
76 #define	MLOG_REC_SEC_DELETE_MARK ((byte)11)	/*!< mark secondary index record
77 						deleted */
78 #define MLOG_REC_UPDATE_IN_PLACE ((byte)13)	/*!< update of a record,
79 						preserves record field sizes */
80 #define MLOG_REC_DELETE		((byte)14)	/*!< delete a record from a
81 						page */
82 #define	MLOG_LIST_END_DELETE	((byte)15)	/*!< delete record list end on
83 						index page */
84 #define	MLOG_LIST_START_DELETE	((byte)16)	/*!< delete record list start on
85 						index page */
86 #define	MLOG_LIST_END_COPY_CREATED ((byte)17)	/*!< copy record list end to a
87 						new created index page */
88 #define	MLOG_PAGE_REORGANIZE	((byte)18)	/*!< reorganize an
89 						index page in
90 						ROW_FORMAT=REDUNDANT */
91 #define MLOG_PAGE_CREATE	((byte)19)	/*!< create an index page */
92 #define	MLOG_UNDO_INSERT	((byte)20)	/*!< insert entry in an undo
93 						log */
94 #define MLOG_UNDO_ERASE_END	((byte)21)	/*!< erase an undo log
95 						page end */
96 #define	MLOG_UNDO_INIT		((byte)22)	/*!< initialize a page in an
97 						undo log */
98 #define MLOG_UNDO_HDR_DISCARD	((byte)23)	/*!< discard an update undo log
99 						header */
100 #define	MLOG_UNDO_HDR_REUSE	((byte)24)	/*!< reuse an insert undo log
101 						header */
102 #define MLOG_UNDO_HDR_CREATE	((byte)25)	/*!< create an undo
103 						log header */
104 #define MLOG_REC_MIN_MARK	((byte)26)	/*!< mark an index
105 						record as the
106 						predefined minimum
107 						record */
108 #define MLOG_IBUF_BITMAP_INIT	((byte)27)	/*!< initialize an
109 						ibuf bitmap page */
110 /*#define	MLOG_FULL_PAGE	((byte)28)	full contents of a page */
111 #ifdef UNIV_LOG_LSN_DEBUG
112 # define MLOG_LSN		((byte)28)	/* current LSN */
113 #endif
114 #define MLOG_INIT_FILE_PAGE	((byte)29)	/*!< this means that a
115 						file page is taken
116 						into use and the prior
117 						contents of the page
118 						should be ignored: in
119 						recovery we must not
120 						trust the lsn values
121 						stored to the file
122 						page */
123 #define MLOG_WRITE_STRING	((byte)30)	/*!< write a string to
124 						a page */
125 #define	MLOG_MULTI_REC_END	((byte)31)	/*!< if a single mtr writes
126 						several log records,
127 						this log record ends the
128 						sequence of these records */
129 #define MLOG_DUMMY_RECORD	((byte)32)	/*!< dummy log record used to
130 						pad a log block full */
131 #define MLOG_FILE_CREATE	((byte)33)	/*!< log record about an .ibd
132 						file creation */
133 #define MLOG_FILE_RENAME	((byte)34)	/*!< log record about an .ibd
134 						file rename */
135 #define MLOG_FILE_DELETE	((byte)35)	/*!< log record about an .ibd
136 						file deletion */
137 #define MLOG_COMP_REC_MIN_MARK	((byte)36)	/*!< mark a compact
138 						index record as the
139 						predefined minimum
140 						record */
141 #define MLOG_COMP_PAGE_CREATE	((byte)37)	/*!< create a compact
142 						index page */
143 #define MLOG_COMP_REC_INSERT	((byte)38)	/*!< compact record insert */
144 #define MLOG_COMP_REC_CLUST_DELETE_MARK ((byte)39)
145 						/*!< mark compact
146 						clustered index record
147 						deleted */
148 #define MLOG_COMP_REC_SEC_DELETE_MARK ((byte)40)/*!< mark compact
149 						secondary index record
150 						deleted; this log
151 						record type is
152 						redundant, as
153 						MLOG_REC_SEC_DELETE_MARK
154 						is independent of the
155 						record format. */
156 #define MLOG_COMP_REC_UPDATE_IN_PLACE ((byte)41)/*!< update of a
157 						compact record,
158 						preserves record field
159 						sizes */
160 #define MLOG_COMP_REC_DELETE	((byte)42)	/*!< delete a compact record
161 						from a page */
162 #define MLOG_COMP_LIST_END_DELETE ((byte)43)	/*!< delete compact record list
163 						end on index page */
164 #define MLOG_COMP_LIST_START_DELETE ((byte)44)	/*!< delete compact record list
165 						start on index page */
166 #define MLOG_COMP_LIST_END_COPY_CREATED ((byte)45)
167 						/*!< copy compact
168 						record list end to a
169 						new created index
170 						page */
171 #define MLOG_COMP_PAGE_REORGANIZE ((byte)46)	/*!< reorganize an index page */
172 #define MLOG_FILE_CREATE2	((byte)47)	/*!< log record about creating
173 						an .ibd file, with format */
174 #define MLOG_ZIP_WRITE_NODE_PTR	((byte)48)	/*!< write the node pointer of
175 						a record on a compressed
176 						non-leaf B-tree page */
177 #define MLOG_ZIP_WRITE_BLOB_PTR	((byte)49)	/*!< write the BLOB pointer
178 						of an externally stored column
179 						on a compressed page */
180 #define MLOG_ZIP_WRITE_HEADER	((byte)50)	/*!< write to compressed page
181 						header */
182 #define MLOG_ZIP_PAGE_COMPRESS	((byte)51)	/*!< compress an index page */
183 #define MLOG_BIGGEST_TYPE	((byte)51)	/*!< biggest value (used in
184 						assertions) */
185 /* @} */
186 
187 /** @name Flags for MLOG_FILE operations
188 (stored in the page number parameter, called log_flags in the
189 functions).  The page number parameter was originally written as 0. @{ */
190 #define MLOG_FILE_FLAG_TEMP	1	/*!< identifies TEMPORARY TABLE in
191 					MLOG_FILE_CREATE, MLOG_FILE_CREATE2 */
192 /* @} */
193 
194 /***************************************************************//**
195 Starts a mini-transaction. */
196 UNIV_INLINE
197 void
198 mtr_start(
199 /*======*/
200 	mtr_t*	mtr)	/*!< out: mini-transaction */
201 	__attribute__((nonnull));
202 /***************************************************************//**
203 Commits a mini-transaction. */
204 UNIV_INTERN
205 void
206 mtr_commit(
207 /*=======*/
208 	mtr_t*	mtr)	/*!< in/out: mini-transaction */
209 	__attribute__((nonnull));
210 /**********************************************************//**
211 Sets and returns a savepoint in mtr.
212 @return	savepoint */
213 UNIV_INLINE
214 ulint
215 mtr_set_savepoint(
216 /*==============*/
217 	mtr_t*	mtr);	/*!< in: mtr */
218 #ifndef UNIV_HOTBACKUP
219 /**********************************************************//**
220 Releases the (index tree) s-latch stored in an mtr memo after a
221 savepoint. */
222 UNIV_INLINE
223 void
224 mtr_release_s_latch_at_savepoint(
225 /*=============================*/
226 	mtr_t*		mtr,		/*!< in: mtr */
227 	ulint		savepoint,	/*!< in: savepoint */
228 	rw_lock_t*	lock);		/*!< in: latch to release */
229 #else /* !UNIV_HOTBACKUP */
230 # define mtr_release_s_latch_at_savepoint(mtr,savepoint,lock) ((void) 0)
231 #endif /* !UNIV_HOTBACKUP */
232 /***************************************************************//**
233 Gets the logging mode of a mini-transaction.
234 @return	logging mode: MTR_LOG_NONE, ... */
235 UNIV_INLINE
236 ulint
237 mtr_get_log_mode(
238 /*=============*/
239 	mtr_t*	mtr);	/*!< in: mtr */
240 /***************************************************************//**
241 Changes the logging mode of a mini-transaction.
242 @return	old mode */
243 UNIV_INLINE
244 ulint
245 mtr_set_log_mode(
246 /*=============*/
247 	mtr_t*	mtr,	/*!< in: mtr */
248 	ulint	mode);	/*!< in: logging mode: MTR_LOG_NONE, ... */
249 /********************************************************//**
250 Reads 1 - 4 bytes from a file page buffered in the buffer pool.
251 @return	value read */
252 UNIV_INTERN
253 ulint
254 mtr_read_ulint(
255 /*===========*/
256 	const byte*	ptr,	/*!< in: pointer from where to read */
257 	ulint		type,	/*!< in: MLOG_1BYTE, MLOG_2BYTES, MLOG_4BYTES */
258 	mtr_t*		mtr);	/*!< in: mini-transaction handle */
259 #ifndef UNIV_HOTBACKUP
260 /*********************************************************************//**
261 This macro locks an rw-lock in s-mode. */
262 #define mtr_s_lock(B, MTR)	mtr_s_lock_func((B), __FILE__, __LINE__,\
263 						(MTR))
264 /*********************************************************************//**
265 This macro locks an rw-lock in x-mode. */
266 #define mtr_x_lock(B, MTR)	mtr_x_lock_func((B), __FILE__, __LINE__,\
267 						(MTR))
268 /*********************************************************************//**
269 NOTE! Use the macro above!
270 Locks a lock in s-mode. */
271 UNIV_INLINE
272 void
273 mtr_s_lock_func(
274 /*============*/
275 	rw_lock_t*	lock,	/*!< in: rw-lock */
276 	const char*	file,	/*!< in: file name */
277 	ulint		line,	/*!< in: line number */
278 	mtr_t*		mtr);	/*!< in: mtr */
279 /*********************************************************************//**
280 NOTE! Use the macro above!
281 Locks a lock in x-mode. */
282 UNIV_INLINE
283 void
284 mtr_x_lock_func(
285 /*============*/
286 	rw_lock_t*	lock,	/*!< in: rw-lock */
287 	const char*	file,	/*!< in: file name */
288 	ulint		line,	/*!< in: line number */
289 	mtr_t*		mtr);	/*!< in: mtr */
290 #endif /* !UNIV_HOTBACKUP */
291 
292 /***************************************************//**
293 Releases an object in the memo stack. */
294 UNIV_INTERN
295 void
296 mtr_memo_release(
297 /*=============*/
298 	mtr_t*	mtr,	/*!< in/out: mini-transaction */
299 	void*	object,	/*!< in: object */
300 	ulint	type)	/*!< in: object type: MTR_MEMO_S_LOCK, ... */
301 	__attribute__((nonnull));
302 #ifdef UNIV_DEBUG
303 # ifndef UNIV_HOTBACKUP
304 /**********************************************************//**
305 Checks if memo contains the given item.
306 @return	TRUE if contains */
307 UNIV_INLINE
308 ibool
309 mtr_memo_contains(
310 /*==============*/
311 	mtr_t*		mtr,	/*!< in: mtr */
312 	const void*	object,	/*!< in: object to search */
313 	ulint		type)	/*!< in: type of object */
314 	__attribute__((warn_unused_result, nonnull));
315 
316 /**********************************************************//**
317 Checks if memo contains the given page.
318 @return	TRUE if contains */
319 UNIV_INTERN
320 ibool
321 mtr_memo_contains_page(
322 /*===================*/
323 	mtr_t*		mtr,	/*!< in: mtr */
324 	const byte*	ptr,	/*!< in: pointer to buffer frame */
325 	ulint		type);	/*!< in: type of object */
326 /*********************************************************//**
327 Prints info of an mtr handle. */
328 UNIV_INTERN
329 void
330 mtr_print(
331 /*======*/
332 	mtr_t*	mtr);	/*!< in: mtr */
333 # else /* !UNIV_HOTBACKUP */
334 #  define mtr_memo_contains(mtr, object, type)		TRUE
335 #  define mtr_memo_contains_page(mtr, ptr, type)	TRUE
336 # endif /* !UNIV_HOTBACKUP */
337 #endif /* UNIV_DEBUG */
338 /*######################################################################*/
339 
340 #define	MTR_BUF_MEMO_SIZE	200	/* number of slots in memo */
341 
342 /***************************************************************//**
343 Returns the log object of a mini-transaction buffer.
344 @return	log */
345 UNIV_INLINE
346 dyn_array_t*
347 mtr_get_log(
348 /*========*/
349 	mtr_t*	mtr);	/*!< in: mini-transaction */
350 /***************************************************//**
351 Pushes an object to an mtr memo stack. */
352 UNIV_INLINE
353 void
354 mtr_memo_push(
355 /*==========*/
356 	mtr_t*	mtr,	/*!< in: mtr */
357 	void*	object,	/*!< in: object */
358 	ulint	type);	/*!< in: object type: MTR_MEMO_S_LOCK, ... */
359 
360 
361 /* Type definition of a mini-transaction memo stack slot. */
362 typedef	struct mtr_memo_slot_struct	mtr_memo_slot_t;
363 struct mtr_memo_slot_struct{
364 	ulint	type;	/*!< type of the stored object (MTR_MEMO_S_LOCK, ...) */
365 	void*	object;	/*!< pointer to the object */
366 };
367 
368 /* Mini-transaction handle and buffer */
369 struct mtr_struct{
370 #ifdef UNIV_DEBUG
371 	ulint		state;	/*!< MTR_ACTIVE, MTR_COMMITTING, MTR_COMMITTED */
372 #endif
373 	dyn_array_t	memo;	/*!< memo stack for locks etc. */
374 	dyn_array_t	log;	/*!< mini-transaction log */
375 	ibool		inside_ibuf;
376 				/*!< TRUE if inside ibuf changes */
377 	ibool		modifications;
378 				/* TRUE if the mtr made modifications to
379 				buffer pool pages */
380 	ulint		n_log_recs;
381 				/* count of how many page initial log records
382 				have been written to the mtr log */
383 	ulint		n_freed_pages;
384 				/* number of pages that have been freed in
385 				this mini-transaction */
386 	ulint		log_mode; /* specifies which operations should be
387 				logged; default value MTR_LOG_ALL */
388 	ib_uint64_t	start_lsn;/* start lsn of the possible log entry for
389 				this mtr */
390 	ib_uint64_t	end_lsn;/* end lsn of the possible log entry for
391 				this mtr */
392 #ifdef UNIV_DEBUG
393 	ulint		magic_n;
394 #endif /* UNIV_DEBUG */
395 };
396 
397 #ifdef UNIV_DEBUG
398 # define MTR_MAGIC_N		54551
399 #endif /* UNIV_DEBUG */
400 
401 #define MTR_ACTIVE		12231
402 #define MTR_COMMITTING		56456
403 #define MTR_COMMITTED		34676
404 
405 #ifndef UNIV_NONINL
406 #include "mtr0mtr.ic"
407 #endif
408 
409 #endif
410