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 /**********************************************************//**
227 Sets and returns a savepoint in mtr.
228 @return	savepoint */
229 UNIV_INLINE
230 ulint
231 mtr_set_savepoint(
232 /*==============*/
233 	mtr_t*	mtr);	/*!< in: mtr */
234 #ifndef UNIV_HOTBACKUP
235 /**********************************************************//**
236 Releases the (index tree) s-latch stored in an mtr memo after a
237 savepoint. */
238 UNIV_INLINE
239 void
240 mtr_release_s_latch_at_savepoint(
241 /*=============================*/
242 	mtr_t*		mtr,		/*!< in: mtr */
243 	ulint		savepoint,	/*!< in: savepoint */
244 	prio_rw_lock_t*	lock);		/*!< in: latch to release */
245 #else /* !UNIV_HOTBACKUP */
246 # define mtr_release_s_latch_at_savepoint(mtr,savepoint,lock) ((void) 0)
247 #endif /* !UNIV_HOTBACKUP */
248 /***************************************************************//**
249 Gets the logging mode of a mini-transaction.
250 @return	logging mode: MTR_LOG_NONE, ... */
251 UNIV_INLINE
252 ulint
253 mtr_get_log_mode(
254 /*=============*/
255 	mtr_t*	mtr);	/*!< in: mtr */
256 /***************************************************************//**
257 Changes the logging mode of a mini-transaction.
258 @return	old mode */
259 UNIV_INLINE
260 ulint
261 mtr_set_log_mode(
262 /*=============*/
263 	mtr_t*	mtr,	/*!< in: mtr */
264 	ulint	mode);	/*!< in: logging mode: MTR_LOG_NONE, ... */
265 /********************************************************//**
266 Reads 1 - 4 bytes from a file page buffered in the buffer pool.
267 @return	value read */
268 UNIV_INTERN
269 ulint
270 mtr_read_ulint(
271 /*===========*/
272 	const byte*	ptr,	/*!< in: pointer from where to read */
273 	ulint		type,	/*!< in: MLOG_1BYTE, MLOG_2BYTES, MLOG_4BYTES */
274 	mtr_t*		mtr);	/*!< in: mini-transaction handle */
275 #ifndef UNIV_HOTBACKUP
276 /*********************************************************************//**
277 This macro locks an rw-lock in s-mode. */
278 #define mtr_s_lock(B, MTR)	mtr_s_lock_func((B), __FILE__, __LINE__,\
279 						(MTR))
280 /*********************************************************************//**
281 This macro locks an rw-lock in x-mode. */
282 #define mtr_x_lock(B, MTR)	mtr_x_lock_func((B), __FILE__, __LINE__,\
283 						(MTR))
284 /*********************************************************************//**
285 NOTE! Use the macro above!
286 Locks a lock in s-mode. */
287 UNIV_INLINE
288 void
289 mtr_s_lock_func(
290 /*============*/
291 	prio_rw_lock_t*	lock,	/*!< in: rw-lock */
292 	const char*	file,	/*!< in: file name */
293 	ulint		line,	/*!< in: line number */
294 	mtr_t*		mtr);	/*!< in: mtr */
295 /*********************************************************************//**
296 NOTE! Use the macro above!
297 Locks a lock in x-mode. */
298 UNIV_INLINE
299 void
300 mtr_x_lock_func(
301 /*============*/
302 	prio_rw_lock_t*	lock,	/*!< in: rw-lock */
303 	const char*	file,	/*!< in: file name */
304 	ulint		line,	/*!< in: line number */
305 	mtr_t*		mtr);	/*!< in: mtr */
306 #endif /* !UNIV_HOTBACKUP */
307 
308 /***************************************************//**
309 Releases an object in the memo stack.
310 @return true if released */
311 UNIV_INTERN
312 bool
313 mtr_memo_release(
314 /*=============*/
315 	mtr_t*	mtr,	/*!< in/out: mini-transaction */
316 	void*	object,	/*!< in: object */
317 	ulint	type)	/*!< in: object type: MTR_MEMO_S_LOCK, ... */
318 	MY_ATTRIBUTE((nonnull));
319 #ifdef UNIV_DEBUG
320 # ifndef UNIV_HOTBACKUP
321 /**********************************************************//**
322 Checks if memo contains the given item.
323 @return	TRUE if contains */
324 UNIV_INLINE
325 bool
326 mtr_memo_contains(
327 /*==============*/
328 	mtr_t*		mtr,	/*!< in: mtr */
329 	const void*	object,	/*!< in: object to search */
330 	ulint		type)	/*!< in: type of object */
331 	MY_ATTRIBUTE((warn_unused_result));
332 
333 /**********************************************************//**
334 Checks if memo contains the given page.
335 @return	TRUE if contains */
336 UNIV_INTERN
337 ibool
338 mtr_memo_contains_page(
339 /*===================*/
340 	mtr_t*		mtr,	/*!< in: mtr */
341 	const byte*	ptr,	/*!< in: pointer to buffer frame */
342 	ulint		type);	/*!< in: type of object */
343 /*********************************************************//**
344 Prints info of an mtr handle. */
345 UNIV_INTERN
346 void
347 mtr_print(
348 /*======*/
349 	mtr_t*	mtr);	/*!< in: mtr */
350 # else /* !UNIV_HOTBACKUP */
351 #  define mtr_memo_contains(mtr, object, type)		TRUE
352 #  define mtr_memo_contains_page(mtr, ptr, type)	TRUE
353 # endif /* !UNIV_HOTBACKUP */
354 #endif /* UNIV_DEBUG */
355 /*######################################################################*/
356 
357 #define	MTR_BUF_MEMO_SIZE	200	/* number of slots in memo */
358 
359 /***************************************************************//**
360 Returns the log object of a mini-transaction buffer.
361 @return	log */
362 UNIV_INLINE
363 dyn_array_t*
364 mtr_get_log(
365 /*========*/
366 	mtr_t*	mtr);	/*!< in: mini-transaction */
367 /***************************************************//**
368 Pushes an object to an mtr memo stack. */
369 UNIV_INLINE
370 void
371 mtr_memo_push(
372 /*==========*/
373 	mtr_t*	mtr,	/*!< in: mtr */
374 	void*	object,	/*!< in: object */
375 	ulint	type);	/*!< in: object type: MTR_MEMO_S_LOCK, ... */
376 
377 /** Mini-transaction memo stack slot. */
378 struct mtr_memo_slot_t{
379 	ulint	type;	/*!< type of the stored object (MTR_MEMO_S_LOCK, ...) */
380 	void*	object;	/*!< pointer to the object */
381 };
382 
383 /* Mini-transaction handle and buffer */
384 struct mtr_t{
385 #ifdef UNIV_DEBUG
386 	ulint		state;	/*!< MTR_ACTIVE, MTR_COMMITTING, MTR_COMMITTED */
387 #endif
388 	dyn_array_t	memo;	/*!< memo stack for locks etc. */
389 	dyn_array_t	log;	/*!< mini-transaction log */
390 	unsigned	inside_ibuf:1;
391 				/*!< TRUE if inside ibuf changes */
392 	unsigned	modifications:1;
393 				/*!< TRUE if the mini-transaction
394 				modified buffer pool pages */
395 	unsigned	made_dirty:1;
396 				/*!< TRUE if mtr has made at least
397 				one buffer pool page dirty */
398 	ulint		n_log_recs;
399 				/* count of how many page initial log records
400 				have been written to the mtr log */
401 	ulint		n_freed_pages;
402 				/* number of pages that have been freed in
403 				this mini-transaction */
404 	ulint		log_mode; /* specifies which operations should be
405 				logged; default value MTR_LOG_ALL */
406 	lsn_t		start_lsn;/* start lsn of the possible log entry for
407 				this mtr */
408 	lsn_t		end_lsn;/* end lsn of the possible log entry for
409 				this mtr */
410 #ifdef UNIV_DEBUG
411 	ulint		magic_n;
412 #endif /* UNIV_DEBUG */
413 };
414 
415 #ifdef UNIV_DEBUG
416 # define MTR_MAGIC_N		54551
417 #endif /* UNIV_DEBUG */
418 
419 #define MTR_ACTIVE		12231
420 #define MTR_COMMITTING		56456
421 #define MTR_COMMITTED		34676
422 
423 #ifndef UNIV_NONINL
424 #include "mtr0mtr.ic"
425 #endif
426 
427 #endif
428