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