log0log.h 35.1 KB
Newer Older
vasil's avatar
vasil committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*****************************************************************************

Copyright (c) 1995, 2009, Innobase Oy. All Rights Reserved.

This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation; version 2 of the License.

This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with
this program; if not, write to the Free Software Foundation, Inc., 59 Temple
Place, Suite 330, Boston, MA 02111-1307 USA

inaam's avatar
inaam committed
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
*****************************************************************************/
/*****************************************************************************

Copyright (c) 1995, 2009, Innobase Oy. All Rights Reserved.
Copyright (c) 2009, Google Inc.

Portions of this file contain modifications contributed and copyrighted by
Google, Inc. Those modifications are gratefully acknowledged and are described
briefly in the InnoDB documentation. The contributions by Google are
incorporated with their permission, and subject to the conditions contained in
the file COPYING.Google.

This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation; version 2 of the License.

This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with
this program; if not, write to the Free Software Foundation, Inc., 59 Temple
Place, Suite 330, Boston, MA 02111-1307 USA

vasil's avatar
vasil committed
41 42
*****************************************************************************/

43 44
/**************************************************//**
@file include/log0log.h
osku's avatar
osku committed
45 46 47 48 49 50 51 52 53 54
Database log

Created 12/9/1995 Heikki Tuuri
*******************************************************/

#ifndef log0log_h
#define log0log_h

#include "univ.i"
#include "ut0byte.h"
55 56
#include "ut0lst.h"
#ifndef UNIV_HOTBACKUP
osku's avatar
osku committed
57 58
#include "sync0sync.h"
#include "sync0rw.h"
59
#endif /* !UNIV_HOTBACKUP */
osku's avatar
osku committed
60

61
/** Redo log buffer */
osku's avatar
osku committed
62
typedef struct log_struct	log_t;
63
/** Redo log group */
osku's avatar
osku committed
64 65 66
typedef struct log_group_struct	log_group_t;

#ifdef UNIV_DEBUG
67
/** Flag: write to log file? */
osku's avatar
osku committed
68
extern	ibool	log_do_write;
69
/** Flag: enable debug output when writing to the log? */
70
extern	ibool	log_debug_writes;
osku's avatar
osku committed
71
#else /* UNIV_DEBUG */
72
/** Write to log */
osku's avatar
osku committed
73 74 75
# define log_do_write TRUE
#endif /* UNIV_DEBUG */

76
/** Wait modes for log_write_up_to @{ */
osku's avatar
osku committed
77 78 79
#define LOG_NO_WAIT		91
#define LOG_WAIT_ONE_GROUP	92
#define	LOG_WAIT_ALL_GROUPS	93
80 81
/* @} */
/** Maximum number of log groups in log_group_struct::checkpoint_buf */
osku's avatar
osku committed
82 83
#define LOG_MAX_N_GROUPS	32

84
#ifndef UNIV_HOTBACKUP
85
/****************************************************************//**
osku's avatar
osku committed
86 87 88
Sets the global variable log_fsp_current_free_limit. Also makes a checkpoint,
so that we know that the limit has been written to a log checkpoint field
on disk. */
89
UNIV_INTERN
osku's avatar
osku committed
90 91 92
void
log_fsp_current_free_limit_set_and_checkpoint(
/*==========================================*/
93
	ulint	limit);	/*!< in: limit to set */
94
#endif /* !UNIV_HOTBACKUP */
95
/*******************************************************************//**
96 97
Calculates where in log files we find a specified lsn.
@return	log file number */
98
UNIV_INTERN
osku's avatar
osku committed
99 100 101
ulint
log_calc_where_lsn_is(
/*==================*/
102
	ib_int64_t*	log_file_offset,	/*!< out: offset in that file
osku's avatar
osku committed
103
						(including the header) */
104
	ib_uint64_t	first_header_lsn,	/*!< in: first log file start
osku's avatar
osku committed
105
						lsn */
106
	ib_uint64_t	lsn,			/*!< in: lsn whose position to
osku's avatar
osku committed
107
						determine */
108
	ulint		n_log_files,		/*!< in: total number of log
osku's avatar
osku committed
109
						files */
110
	ib_int64_t	log_file_size);		/*!< in: log file size
osku's avatar
osku committed
111
						(including the header) */
112
#ifndef UNIV_HOTBACKUP
113
/************************************************************//**
osku's avatar
osku committed
114
Writes to the log the string given. The log must be released with
115 116
log_release.
@return	end lsn of the log record, zero if did not succeed */
osku's avatar
osku committed
117
UNIV_INLINE
118
ib_uint64_t
osku's avatar
osku committed
119 120
log_reserve_and_write_fast(
/*=======================*/
121
	const void*	str,	/*!< in: string */
122
	ulint		len,	/*!< in: string length */
123
	ib_uint64_t*	start_lsn);/*!< out: start lsn of the log record */
124
/***********************************************************************//**
osku's avatar
osku committed
125 126 127 128 129
Releases the log mutex. */
UNIV_INLINE
void
log_release(void);
/*=============*/
130
/***********************************************************************//**
osku's avatar
osku committed
131 132 133 134 135 136 137 138
Checks if there is need for a log buffer flush or a new checkpoint, and does
this if yes. Any database operation should call this when it has modified
more than about 4 pages. NOTE that this function may only be called when the
OS thread owns no synchronization objects except the dictionary mutex. */
UNIV_INLINE
void
log_free_check(void);
/*================*/
139
/************************************************************//**
osku's avatar
osku committed
140
Opens the log for log_write_low. The log must be closed with log_close and
141 142
released with log_release.
@return	start lsn of the log record */
143
UNIV_INTERN
144
ib_uint64_t
osku's avatar
osku committed
145 146
log_reserve_and_open(
/*=================*/
147
	ulint	len);	/*!< in: length of data to be catenated */
148
/************************************************************//**
osku's avatar
osku committed
149 150
Writes to the log the string given. It is assumed that the caller holds the
log mutex. */
151
UNIV_INTERN
osku's avatar
osku committed
152 153 154
void
log_write_low(
/*==========*/
155 156
	byte*	str,		/*!< in: string */
	ulint	str_len);	/*!< in: string length */
157
/************************************************************//**
158 159
Closes the log.
@return	lsn */
160
UNIV_INTERN
161
ib_uint64_t
osku's avatar
osku committed
162 163
log_close(void);
/*===========*/
164
/************************************************************//**
165 166
Gets the current lsn.
@return	current lsn */
osku's avatar
osku committed
167
UNIV_INLINE
168
ib_uint64_t
osku's avatar
osku committed
169 170
log_get_lsn(void);
/*=============*/
inaam's avatar
inaam committed
171 172 173 174 175
/****************************************************************
Gets the log group capacity. It is OK to read the value without
holding log_sys->mutex because it is constant.
@return	log group capacity */
UNIV_INLINE
inaam's avatar
inaam committed
176
ulint
inaam's avatar
inaam committed
177 178
log_get_capacity(void);
/*==================*/
179
/******************************************************//**
osku's avatar
osku committed
180
Initializes the log. */
181
UNIV_INTERN
osku's avatar
osku committed
182 183 184
void
log_init(void);
/*==========*/
185
/******************************************************************//**
osku's avatar
osku committed
186
Inits a log group to the log system. */
187
UNIV_INTERN
osku's avatar
osku committed
188 189 190
void
log_group_init(
/*===========*/
191 192 193 194
	ulint	id,			/*!< in: group id */
	ulint	n_files,		/*!< in: number of log files */
	ulint	file_size,		/*!< in: log file size in bytes */
	ulint	space_id,		/*!< in: space id of the file space
osku's avatar
osku committed
195 196
					which contains the log files of this
					group */
197
	ulint	archive_space_id);	/*!< in: space id of the file space
osku's avatar
osku committed
198 199 200 201
					which contains some archived log
					files for this group; currently, only
					for the first log group this is
					used */
202
/******************************************************//**
osku's avatar
osku committed
203
Completes an i/o to a log file. */
204
UNIV_INTERN
osku's avatar
osku committed
205 206 207
void
log_io_complete(
/*============*/
208
	log_group_t*	group);	/*!< in: log group */
209
/******************************************************//**
osku's avatar
osku committed
210 211 212 213
This function is called, e.g., when a transaction wants to commit. It checks
that the log has been written to the log file up to the last log entry written
by the transaction. If there is a flush running, it waits and checks if the
flush flushed enough. If not, starts a new flush. */
214
UNIV_INTERN
osku's avatar
osku committed
215 216 217
void
log_write_up_to(
/*============*/
218
	ib_uint64_t	lsn,	/*!< in: log sequence number up to which
219 220
				the log should be written,
				IB_ULONGLONG_MAX if not specified */
221
	ulint		wait,	/*!< in: LOG_NO_WAIT, LOG_WAIT_ONE_GROUP,
222 223
				or LOG_WAIT_ALL_GROUPS */
	ibool		flush_to_disk);
224
				/*!< in: TRUE if we want the written log
225
				also to be flushed to disk */
226
/****************************************************************//**
osku's avatar
osku committed
227
Does a syncronous flush of the log buffer to disk. */
228
UNIV_INTERN
osku's avatar
osku committed
229 230 231
void
log_buffer_flush_to_disk(void);
/*==========================*/
232
/****************************************************************//**
inaam's avatar
inaam committed
233 234 235 236 237 238 239 240 241 242
This functions writes the log buffer to the log file and if 'flush'
is set it forces a flush of the log file as well. This is meant to be
called from background master thread only as it does not wait for
the write (+ possible flush) to finish. */
UNIV_INTERN
void
log_buffer_sync_in_background(
/*==========================*/
	ibool	flush);	/*<! in: flush the logs to disk */
/****************************************************************//**
osku's avatar
osku committed
243 244
Advances the smallest lsn for which there are unflushed dirty blocks in the
buffer pool and also may make a new checkpoint. NOTE: this function may only
245
be called if the calling thread owns no synchronization objects!
246 247
@return FALSE if there was a flush batch of the same type running,
which means that we could not start this flush batch */
248
UNIV_INTERN
osku's avatar
osku committed
249 250 251
ibool
log_preflush_pool_modified_pages(
/*=============================*/
252
	ib_uint64_t	new_oldest,	/*!< in: try to advance
253 254
					oldest_modified_lsn at least
					to this lsn */
255
	ibool		sync);		/*!< in: TRUE if synchronous
256
					operation is desired */
257
/******************************************************//**
osku's avatar
osku committed
258 259 260
Makes a checkpoint. Note that this function does not flush dirty
blocks from the buffer pool: it only checks what is lsn of the oldest
modification in the pool, and writes information about the lsn in
261 262
log files. Use log_make_checkpoint_at to flush also the pool.
@return	TRUE if success, FALSE if a checkpoint write was already running */
263
UNIV_INTERN
osku's avatar
osku committed
264 265 266
ibool
log_checkpoint(
/*===========*/
267
	ibool	sync,		/*!< in: TRUE if synchronous operation is
osku's avatar
osku committed
268
				desired */
269
	ibool	write_always);	/*!< in: the function normally checks if the
osku's avatar
osku committed
270 271 272 273 274
				the new checkpoint would have a greater
				lsn than the previous one: if not, then no
				physical write is done; by setting this
				parameter TRUE, a physical write will always be
				made to log files */
275
/****************************************************************//**
osku's avatar
osku committed
276
Makes a checkpoint at a given lsn or later. */
277
UNIV_INTERN
osku's avatar
osku committed
278 279 280
void
log_make_checkpoint_at(
/*===================*/
281
	ib_uint64_t	lsn,		/*!< in: make a checkpoint at this or a
282 283
					later lsn, if IB_ULONGLONG_MAX, makes
					a checkpoint at the latest lsn */
284
	ibool		write_always);	/*!< in: the function normally checks if
285
					the new checkpoint would have a
286 287 288 289 290
					greater lsn than the previous one: if
					not, then no physical write is done;
					by setting this parameter TRUE, a
					physical write will always be made to
					log files */
291
/****************************************************************//**
osku's avatar
osku committed
292 293 294 295
Makes a checkpoint at the latest lsn and writes it to first page of each
data file in the database, so that we know that the file spaces contain
all modifications up to that lsn. This can only be called at database
shutdown. This function also writes all log in log files to the log archive. */
296
UNIV_INTERN
osku's avatar
osku committed
297 298 299
void
logs_empty_and_mark_files_at_shutdown(void);
/*=======================================*/
300
/******************************************************//**
osku's avatar
osku committed
301
Reads a checkpoint info from a log group header to log_sys->checkpoint_buf. */
302
UNIV_INTERN
osku's avatar
osku committed
303 304 305
void
log_group_read_checkpoint_info(
/*===========================*/
306 307
	log_group_t*	group,	/*!< in: log group */
	ulint		field);	/*!< in: LOG_CHECKPOINT_1 or LOG_CHECKPOINT_2 */
308
/*******************************************************************//**
osku's avatar
osku committed
309
Gets info from a checkpoint about a log group. */
310
UNIV_INTERN
osku's avatar
osku committed
311 312 313
void
log_checkpoint_get_nth_group_info(
/*==============================*/
314 315 316 317
	const byte*	buf,	/*!< in: buffer containing checkpoint info */
	ulint		n,	/*!< in: nth slot */
	ulint*		file_no,/*!< out: archived file number */
	ulint*		offset);/*!< out: archived file offset */
318
/******************************************************//**
osku's avatar
osku committed
319
Writes checkpoint info to groups. */
320
UNIV_INTERN
osku's avatar
osku committed
321 322 323
void
log_groups_write_checkpoint_info(void);
/*==================================*/
324
/********************************************************************//**
325 326
Starts an archiving operation.
@return	TRUE if succeed, FALSE if an archiving operation was already running */
327
UNIV_INTERN
osku's avatar
osku committed
328 329 330
ibool
log_archive_do(
/*===========*/
331 332
	ibool	sync,	/*!< in: TRUE if synchronous operation is desired */
	ulint*	n_bytes);/*!< out: archive log buffer size, 0 if nothing to
osku's avatar
osku committed
333
			archive */
334
/****************************************************************//**
osku's avatar
osku committed
335 336 337 338
Writes the log contents to the archive up to the lsn when this function was
called, and stops the archiving. When archiving is started again, the archived
log file numbers start from a number one higher, so that the archiving will
not write again to the archived log files which exist when this function
339 340
returns.
@return	DB_SUCCESS or DB_ERROR */
341
UNIV_INTERN
osku's avatar
osku committed
342 343 344
ulint
log_archive_stop(void);
/*==================*/
345
/****************************************************************//**
346 347
Starts again archiving which has been stopped.
@return	DB_SUCCESS or DB_ERROR */
348
UNIV_INTERN
osku's avatar
osku committed
349 350 351
ulint
log_archive_start(void);
/*===================*/
352
/****************************************************************//**
353 354
Stop archiving the log so that a gap may occur in the archived log files.
@return	DB_SUCCESS or DB_ERROR */
355
UNIV_INTERN
osku's avatar
osku committed
356 357 358
ulint
log_archive_noarchivelog(void);
/*==========================*/
359
/****************************************************************//**
360 361
Start archiving the log so that a gap may occur in the archived log files.
@return	DB_SUCCESS or DB_ERROR */
362
UNIV_INTERN
osku's avatar
osku committed
363 364 365
ulint
log_archive_archivelog(void);
/*========================*/
366
/******************************************************//**
osku's avatar
osku committed
367
Generates an archived log file name. */
368
UNIV_INTERN
osku's avatar
osku committed
369 370 371
void
log_archived_file_name_gen(
/*=======================*/
372 373 374
	char*	buf,	/*!< in: buffer where to write */
	ulint	id,	/*!< in: group id */
	ulint	file_no);/*!< in: file number */
375
#else /* !UNIV_HOTBACKUP */
376
/******************************************************//**
377 378 379 380 381 382
Writes info to a buffer of a log group when log files are created in
backup restoration. */
UNIV_INTERN
void
log_reset_first_header_and_checkpoint(
/*==================================*/
383
	byte*		hdr_buf,/*!< in: buffer which will be written to the
384
				start of the first log file */
385
	ib_uint64_t	start);	/*!< in: lsn of the start of the first log file;
386 387 388
				we pretend that there is a checkpoint at
				start + LOG_BLOCK_HDR_SIZE */
#endif /* !UNIV_HOTBACKUP */
389
/********************************************************************//**
osku's avatar
osku committed
390 391 392 393
Checks that there is enough free space in the log to start a new query step.
Flushes the log buffer or makes a new checkpoint if necessary. NOTE: this
function may only be called if the calling thread owns no synchronization
objects! */
394
UNIV_INTERN
osku's avatar
osku committed
395 396 397
void
log_check_margins(void);
/*===================*/
398
#ifndef UNIV_HOTBACKUP
399
/******************************************************//**
osku's avatar
osku committed
400
Reads a specified log segment to a buffer. */
401
UNIV_INTERN
osku's avatar
osku committed
402 403 404
void
log_group_read_log_seg(
/*===================*/
405 406 407 408 409
	ulint		type,		/*!< in: LOG_ARCHIVE or LOG_RECOVER */
	byte*		buf,		/*!< in: buffer where to read */
	log_group_t*	group,		/*!< in: log group */
	ib_uint64_t	start_lsn,	/*!< in: read area start */
	ib_uint64_t	end_lsn);	/*!< in: read area end */
410
/******************************************************//**
osku's avatar
osku committed
411
Writes a buffer to a log file group. */
412
UNIV_INTERN
osku's avatar
osku committed
413 414 415
void
log_group_write_buf(
/*================*/
416 417 418
	log_group_t*	group,		/*!< in: log group */
	byte*		buf,		/*!< in: buffer */
	ulint		len,		/*!< in: buffer len; must be divisible
osku's avatar
osku committed
419
					by OS_FILE_LOG_BLOCK_SIZE */
420
	ib_uint64_t	start_lsn,	/*!< in: start lsn of the buffer; must
osku's avatar
osku committed
421 422
					be divisible by
					OS_FILE_LOG_BLOCK_SIZE */
423
	ulint		new_data_offset);/*!< in: start offset of new data in
osku's avatar
osku committed
424 425 426
					buf: this parameter is used to decide
					if we have to write a new log file
					header */
427
/********************************************************//**
osku's avatar
osku committed
428 429 430
Sets the field values in group to correspond to a given lsn. For this function
to work, the values must already be correctly initialized to correspond to
some lsn, for instance, a checkpoint lsn. */
431
UNIV_INTERN
osku's avatar
osku committed
432 433 434
void
log_group_set_fields(
/*=================*/
435 436
	log_group_t*	group,	/*!< in/out: group */
	ib_uint64_t	lsn);	/*!< in: lsn for which the values should be
osku's avatar
osku committed
437
				set */
438
/******************************************************//**
osku's avatar
osku committed
439
Calculates the data capacity of a log group, when the log file headers are not
440 441
included.
@return	capacity in bytes */
442
UNIV_INTERN
osku's avatar
osku committed
443 444 445
ulint
log_group_get_capacity(
/*===================*/
446
	const log_group_t*	group);	/*!< in: log group */
447
#endif /* !UNIV_HOTBACKUP */
448
/************************************************************//**
449 450
Gets a log block flush bit.
@return	TRUE if this block was the first to be written in a log flush */
osku's avatar
osku committed
451 452 453 454
UNIV_INLINE
ibool
log_block_get_flush_bit(
/*====================*/
455
	const byte*	log_block);	/*!< in: log block */
456
/************************************************************//**
457 458
Gets a log block number stored in the header.
@return	log block number stored in the block header */
osku's avatar
osku committed
459 460 461 462
UNIV_INLINE
ulint
log_block_get_hdr_no(
/*=================*/
463
	const byte*	log_block);	/*!< in: log block */
464
/************************************************************//**
465 466
Gets a log block data length.
@return	log block data length measured as a byte offset from the block start */
osku's avatar
osku committed
467 468 469 470
UNIV_INLINE
ulint
log_block_get_data_len(
/*===================*/
471
	const byte*	log_block);	/*!< in: log block */
472
/************************************************************//**
osku's avatar
osku committed
473 474 475 476 477
Sets the log block data length. */
UNIV_INLINE
void
log_block_set_data_len(
/*===================*/
478 479
	byte*	log_block,	/*!< in/out: log block */
	ulint	len);		/*!< in: data length */
480
/************************************************************//**
481 482
Calculates the checksum for a log block.
@return	checksum */
osku's avatar
osku committed
483 484 485 486
UNIV_INLINE
ulint
log_block_calc_checksum(
/*====================*/
487
	const byte*	block);	/*!< in: log block */
488
/************************************************************//**
489 490
Gets a log block checksum field value.
@return	checksum */
osku's avatar
osku committed
491 492 493 494
UNIV_INLINE
ulint
log_block_get_checksum(
/*===================*/
495
	const byte*	log_block);	/*!< in: log block */
496
/************************************************************//**
osku's avatar
osku committed
497 498 499 500 501
Sets a log block checksum field value. */
UNIV_INLINE
void
log_block_set_checksum(
/*===================*/
502 503
	byte*	log_block,	/*!< in/out: log block */
	ulint	checksum);	/*!< in: checksum */
504
/************************************************************//**
505
Gets a log block first mtr log record group offset.
506 507
@return first mtr log record group byte offset from the block start, 0
if none */
osku's avatar
osku committed
508 509 510 511
UNIV_INLINE
ulint
log_block_get_first_rec_group(
/*==========================*/
512
	const byte*	log_block);	/*!< in: log block */
513
/************************************************************//**
osku's avatar
osku committed
514 515 516 517 518
Sets the log block first mtr log record group offset. */
UNIV_INLINE
void
log_block_set_first_rec_group(
/*==========================*/
519 520
	byte*	log_block,	/*!< in/out: log block */
	ulint	offset);	/*!< in: offset, 0 if none */
521
/************************************************************//**
522 523
Gets a log block checkpoint number field (4 lowest bytes).
@return	checkpoint no (4 lowest bytes) */
osku's avatar
osku committed
524 525 526 527
UNIV_INLINE
ulint
log_block_get_checkpoint_no(
/*========================*/
528
	const byte*	log_block);	/*!< in: log block */
529
/************************************************************//**
osku's avatar
osku committed
530 531 532 533 534
Initializes a log block in the log buffer. */
UNIV_INLINE
void
log_block_init(
/*===========*/
535 536
	byte*		log_block,	/*!< in: pointer to the log buffer */
	ib_uint64_t	lsn);		/*!< in: lsn within the log block */
537
/************************************************************//**
osku's avatar
osku committed
538 539 540 541 542 543
Initializes a log block in the log buffer in the old, < 3.23.52 format, where
there was no checksum yet. */
UNIV_INLINE
void
log_block_init_in_old_format(
/*=========================*/
544 545
	byte*		log_block,	/*!< in: pointer to the log buffer */
	ib_uint64_t	lsn);		/*!< in: lsn within the log block */
546
/************************************************************//**
547 548
Converts a lsn to a log block number.
@return	log block number, it is > 0 and <= 1G */
osku's avatar
osku committed
549 550 551 552
UNIV_INLINE
ulint
log_block_convert_lsn_to_no(
/*========================*/
553
	ib_uint64_t	lsn);	/*!< in: lsn of a byte within the block */
554
/******************************************************//**
osku's avatar
osku committed
555
Prints info of the log. */
556
UNIV_INTERN
osku's avatar
osku committed
557 558 559
void
log_print(
/*======*/
560
	FILE*	file);	/*!< in: file where to print */
561
/******************************************************//**
562 563
Peeks the current lsn.
@return	TRUE if success, FALSE if could not get the log system mutex */
564
UNIV_INTERN
osku's avatar
osku committed
565 566 567
ibool
log_peek_lsn(
/*=========*/
568
	ib_uint64_t*	lsn);	/*!< out: if returns TRUE, current lsn is here */
569
/**********************************************************************//**
osku's avatar
osku committed
570
Refreshes the statistics used to print per-second averages. */
571
UNIV_INTERN
osku's avatar
osku committed
572 573 574
void
log_refresh_stats(void);
/*===================*/
575 576 577 578 579 580 581 582 583 584 585 586
/**********************************************************
Shutdown the log system but do not release all the memory. */
UNIV_INTERN
void
log_shutdown(void);
/*==============*/
/**********************************************************
Free the log system data structures. */
UNIV_INTERN
void
log_mem_free(void);
/*==============*/
osku's avatar
osku committed
587 588 589 590 591 592

extern log_t*	log_sys;

/* Values used as flags */
#define LOG_FLUSH	7652559
#define LOG_CHECKPOINT	78656949
593 594 595
#ifdef UNIV_LOG_ARCHIVE
# define LOG_ARCHIVE	11122331
#endif /* UNIV_LOG_ARCHIVE */
osku's avatar
osku committed
596 597 598
#define LOG_RECOVER	98887331

/* The counting of lsn's starts from this value: this must be non-zero */
599
#define LOG_START_LSN		((ib_uint64_t) (16 * OS_FILE_LOG_BLOCK_SIZE))
osku's avatar
osku committed
600

601
#define LOG_BUFFER_SIZE		(srv_log_buffer_size * UNIV_PAGE_SIZE)
osku's avatar
osku committed
602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650
#define LOG_ARCHIVE_BUF_SIZE	(srv_log_buffer_size * UNIV_PAGE_SIZE / 4)

/* Offsets of a log block header */
#define	LOG_BLOCK_HDR_NO	0	/* block number which must be > 0 and
					is allowed to wrap around at 2G; the
					highest bit is set to 1 if this is the
					first log block in a log flush write
					segment */
#define LOG_BLOCK_FLUSH_BIT_MASK 0x80000000UL
					/* mask used to get the highest bit in
					the preceding field */
#define	LOG_BLOCK_HDR_DATA_LEN	4	/* number of bytes of log written to
					this block */
#define	LOG_BLOCK_FIRST_REC_GROUP 6	/* offset of the first start of an
					mtr log record group in this log block,
					0 if none; if the value is the same
					as LOG_BLOCK_HDR_DATA_LEN, it means
					that the first rec group has not yet
					been catenated to this log block, but
					if it will, it will start at this
					offset; an archive recovery can
					start parsing the log records starting
					from this offset in this log block,
					if value not 0 */
#define LOG_BLOCK_CHECKPOINT_NO	8	/* 4 lower bytes of the value of
					log_sys->next_checkpoint_no when the
					log block was last written to: if the
					block has not yet been written full,
					this value is only updated before a
					log buffer flush */
#define LOG_BLOCK_HDR_SIZE	12	/* size of the log block header in
					bytes */

/* Offsets of a log block trailer from the end of the block */
#define	LOG_BLOCK_CHECKSUM	4	/* 4 byte checksum of the log block
					contents; in InnoDB versions
					< 3.23.52 this did not contain the
					checksum but the same value as
					.._HDR_NO */
#define	LOG_BLOCK_TRL_SIZE	4	/* trailer size in bytes */

/* Offsets for a checkpoint field */
#define LOG_CHECKPOINT_NO		0
#define LOG_CHECKPOINT_LSN		8
#define LOG_CHECKPOINT_OFFSET		16
#define LOG_CHECKPOINT_LOG_BUF_SIZE	20
#define	LOG_CHECKPOINT_ARCHIVED_LSN	24
#define	LOG_CHECKPOINT_GROUP_ARRAY	32

651
/* For each value smaller than LOG_MAX_N_GROUPS the following 8 bytes: */
osku's avatar
osku committed
652 653 654 655 656 657

#define LOG_CHECKPOINT_ARCHIVED_FILE_NO	0
#define LOG_CHECKPOINT_ARCHIVED_OFFSET	4

#define	LOG_CHECKPOINT_ARRAY_END	(LOG_CHECKPOINT_GROUP_ARRAY\
							+ LOG_MAX_N_GROUPS * 8)
658 659
#define LOG_CHECKPOINT_CHECKSUM_1	LOG_CHECKPOINT_ARRAY_END
#define LOG_CHECKPOINT_CHECKSUM_2	(4 + LOG_CHECKPOINT_ARRAY_END)
osku's avatar
osku committed
660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718
#define LOG_CHECKPOINT_FSP_FREE_LIMIT	(8 + LOG_CHECKPOINT_ARRAY_END)
					/* current fsp free limit in
					tablespace 0, in units of one
					megabyte; this information is only used
					by ibbackup to decide if it can
					truncate unused ends of
					non-auto-extending data files in space
					0 */
#define LOG_CHECKPOINT_FSP_MAGIC_N	(12 + LOG_CHECKPOINT_ARRAY_END)
					/* this magic number tells if the
					checkpoint contains the above field:
					the field was added to
					InnoDB-3.23.50 */
#define LOG_CHECKPOINT_SIZE		(16 + LOG_CHECKPOINT_ARRAY_END)

#define LOG_CHECKPOINT_FSP_MAGIC_N_VAL	1441231243

/* Offsets of a log file header */
#define LOG_GROUP_ID		0	/* log group number */
#define LOG_FILE_START_LSN	4	/* lsn of the start of data in this
					log file */
#define LOG_FILE_NO		12	/* 4-byte archived log file number;
					this field is only defined in an
					archived log file */
#define LOG_FILE_WAS_CREATED_BY_HOT_BACKUP 16
					/* a 32-byte field which contains
					the string 'ibbackup' and the
					creation time if the log file was
					created by ibbackup --restore;
					when mysqld is first time started
					on the restored database, it can
					print helpful info for the user */
#define	LOG_FILE_ARCH_COMPLETED	OS_FILE_LOG_BLOCK_SIZE
					/* this 4-byte field is TRUE when
					the writing of an archived log file
					has been completed; this field is
					only defined in an archived log file */
#define LOG_FILE_END_LSN	(OS_FILE_LOG_BLOCK_SIZE + 4)
					/* lsn where the archived log file
					at least extends: actually the
					archived log file may extend to a
					later lsn, as long as it is within the
					same log block as this lsn; this field
					is defined only when an archived log
					file has been completely written */
#define LOG_CHECKPOINT_1	OS_FILE_LOG_BLOCK_SIZE
					/* first checkpoint field in the log
					header; we write alternately to the
					checkpoint fields when we make new
					checkpoints; this field is only defined
					in the first log file of a log group */
#define LOG_CHECKPOINT_2	(3 * OS_FILE_LOG_BLOCK_SIZE)
					/* second checkpoint field in the log
					header */
#define LOG_FILE_HDR_SIZE	(4 * OS_FILE_LOG_BLOCK_SIZE)

#define LOG_GROUP_OK		301
#define LOG_GROUP_CORRUPTED	302

719
/** Log group consists of a number of log files, each of the same size; a log
osku's avatar
osku committed
720 721 722
group is implemented as a space in the sense of the module fil0fil. */
struct log_group_struct{
	/* The following fields are protected by log_sys->mutex */
723 724 725
	ulint		id;		/*!< log group id */
	ulint		n_files;	/*!< number of files in the group */
	ulint		file_size;	/*!< individual log file size in bytes,
osku's avatar
osku committed
726
					including the log file header */
727
	ulint		space_id;	/*!< file space which implements the log
osku's avatar
osku committed
728
					group */
729
	ulint		state;		/*!< LOG_GROUP_OK or
osku's avatar
osku committed
730
					LOG_GROUP_CORRUPTED */
731
	ib_uint64_t	lsn;		/*!< lsn used to fix coordinates within
osku's avatar
osku committed
732
					the log group */
733 734
	ulint		lsn_offset;	/*!< the offset of the above lsn */
	ulint		n_pending_writes;/*!< number of currently pending flush
osku's avatar
osku committed
735
					writes for this log group */
736
	byte**		file_header_bufs_ptr;/*!< unaligned buffers */
737 738
	byte**		file_header_bufs;/*!< buffers for each file
					header in the group */
739
#ifdef UNIV_LOG_ARCHIVE
osku's avatar
osku committed
740
	/*-----------------------------*/
741
	byte**		archive_file_header_bufs_ptr;/*!< unaligned buffers */
742
	byte**		archive_file_header_bufs;/*!< buffers for each file
osku's avatar
osku committed
743
					header in the group */
744 745 746 747
	ulint		archive_space_id;/*!< file space which
					implements the log group
					archive */
	ulint		archived_file_no;/*!< file number corresponding to
osku's avatar
osku committed
748
					log_sys->archived_lsn */
749
	ulint		archived_offset;/*!< file offset corresponding to
osku's avatar
osku committed
750 751 752
					log_sys->archived_lsn, 0 if we have
					not yet written to the archive file
					number archived_file_no */
753
	ulint		next_archived_file_no;/*!< during an archive write,
osku's avatar
osku committed
754 755 756 757 758
					until the write is completed, we
					store the next value for
					archived_file_no here: the write
					completion function then sets the new
					value to ..._file_no */
759
	ulint		next_archived_offset; /*!< like the preceding field */
760
#endif /* UNIV_LOG_ARCHIVE */
osku's avatar
osku committed
761
	/*-----------------------------*/
762
	ib_uint64_t	scanned_lsn;	/*!< used only in recovery: recovery scan
osku's avatar
osku committed
763 764
					succeeded up to this lsn in this log
					group */
765
	byte*		checkpoint_buf_ptr;/*!< unaligned checkpoint header */
766
	byte*		checkpoint_buf;	/*!< checkpoint header is written from
osku's avatar
osku committed
767 768
					this buffer to the group */
	UT_LIST_NODE_T(log_group_t)
769
			log_groups;	/*!< list of log groups */
770
};
osku's avatar
osku committed
771

772
/** Redo log buffer */
osku's avatar
osku committed
773
struct log_struct{
774
	byte		pad[64];	/*!< padding to prevent other memory
osku's avatar
osku committed
775 776
					update hotspots from residing on the
					same memory cache line */
777 778
	ib_uint64_t	lsn;		/*!< log sequence number */
	ulint		buf_free;	/*!< first free offset within the log
osku's avatar
osku committed
779
					buffer */
780
#ifndef UNIV_HOTBACKUP
781
	mutex_t		mutex;		/*!< mutex protecting the log */
782
#endif /* !UNIV_HOTBACKUP */
783
	byte*		buf_ptr;	/* unaligned log buffer */
784 785 786
	byte*		buf;		/*!< log buffer */
	ulint		buf_size;	/*!< log buffer size in bytes */
	ulint		max_buf_free;	/*!< recommended maximum value of
osku's avatar
osku committed
787 788
					buf_free, after which the buffer is
					flushed */
789
	ulint		old_buf_free;	/*!< value of buf free when log was
osku's avatar
osku committed
790 791
					last time opened; only in the debug
					version */
792 793 794
	ib_uint64_t	old_lsn;	/*!< value of lsn when log was
					last time opened; only in the
					debug version */
osku's avatar
osku committed
795
	ibool		check_flush_or_checkpoint;
796
					/*!< this is set to TRUE when there may
osku's avatar
osku committed
797 798 799 800 801 802 803 804
					be need to flush the log buffer, or
					preflush buffer pool pages, or make
					a checkpoint; this MUST be TRUE when
					lsn - last_checkpoint_lsn >
					max_checkpoint_age; this flag is
					peeked at by log_free_check(), which
					does not reserve the log mutex */
	UT_LIST_BASE_NODE_T(log_group_t)
805
			log_groups;	/*!< log groups */
osku's avatar
osku committed
806

807
#ifndef UNIV_HOTBACKUP
808
	/** The fields involved in the log buffer flush @{ */
osku's avatar
osku committed
809

810
	ulint		buf_next_to_write;/*!< first offset in the log buffer
osku's avatar
osku committed
811 812 813 814 815 816
					where the byte content may not exist
					written to file, e.g., the start
					offset of a log record catenated
					later; this is advanced when a flush
					operation is completed to all the log
					groups */
817
	ib_uint64_t	written_to_some_lsn;
818
					/*!< first log sequence number not yet
osku's avatar
osku committed
819 820 821 822
					written to any log group; for this to
					be advanced, it is enough that the
					write i/o has been completed for any
					one log group */
823
	ib_uint64_t	written_to_all_lsn;
824
					/*!< first log sequence number not yet
osku's avatar
osku committed
825 826 827 828
					written to some log group; for this to
					be advanced, it is enough that the
					write i/o has been completed for all
					log groups */
829
	ib_uint64_t	write_lsn;	/*!< end lsn for the current running
osku's avatar
osku committed
830
					write */
831 832 833 834 835 836
	ulint		write_end_offset;/*!< the data in buffer has
					been written up to this offset
					when the current write ends:
					this field will then be copied
					to buf_next_to_write */
	ib_uint64_t	current_flush_lsn;/*!< end lsn for the current running
osku's avatar
osku committed
837
					write + flush operation */
838
	ib_uint64_t	flushed_to_disk_lsn;
839
					/*!< how far we have written the log
osku's avatar
osku committed
840
					AND flushed to disk */
841 842
	ulint		n_pending_writes;/*!< number of currently
					pending flushes or writes */
osku's avatar
osku committed
843 844 845 846
	/* NOTE on the 'flush' in names of the fields below: starting from
	4.0.14, we separate the write of the log file and the actual fsync()
	or other method to flush it to disk. The names below shhould really
	be 'flush_or_write'! */
847
	os_event_t	no_flush_event;	/*!< this event is in the reset state
osku's avatar
osku committed
848 849 850 851 852
					when a flush or a write is running;
					a thread should wait for this without
					owning the log mutex, but NOTE that
					to set or reset this event, the
					thread MUST own the log mutex! */
853 854 855 856 857
	ibool		one_flushed;	/*!< during a flush, this is
					first FALSE and becomes TRUE
					when one log group has been
					written or flushed */
	os_event_t	one_flushed_event;/*!< this event is reset when the
osku's avatar
osku committed
858 859 860 861 862 863 864 865
					flush or write has not yet completed
					for any log group; e.g., this means
					that a transaction has been committed
					when this is set; a thread should wait
					for this without owning the log mutex,
					but NOTE that to set or reset this
					event, the thread MUST own the log
					mutex! */
866
	ulint		n_log_ios;	/*!< number of log i/os initiated thus
osku's avatar
osku committed
867
					far */
868
	ulint		n_log_ios_old;	/*!< number of log i/o's at the
osku's avatar
osku committed
869
					previous printout */
870
	time_t		last_printout_time;/*!< when log_print was last time
osku's avatar
osku committed
871
					called */
872
	/* @} */
osku's avatar
osku committed
873

874 875
	/** Fields involved in checkpoints @{ */
	ulint		log_group_capacity; /*!< capacity of the log group; if
876 877 878 879
					the checkpoint age exceeds this, it is
					a serious error because it is possible
					we will then overwrite log and spoil
					crash recovery */
osku's avatar
osku committed
880
	ulint		max_modified_age_async;
881 882 883 884 885
					/*!< when this recommended
					value for lsn -
					buf_pool_get_oldest_modification()
					is exceeded, we start an
					asynchronous preflush of pool pages */
osku's avatar
osku committed
886
	ulint		max_modified_age_sync;
887 888 889 890 891
					/*!< when this recommended
					value for lsn -
					buf_pool_get_oldest_modification()
					is exceeded, we start a
					synchronous preflush of pool pages */
osku's avatar
osku committed
892
	ulint		adm_checkpoint_interval;
893
					/*!< administrator-specified checkpoint
osku's avatar
osku committed
894 895 896 897
					interval in terms of log growth in
					bytes; the interval actually used by
					the database can be smaller */
	ulint		max_checkpoint_age_async;
898 899 900 901
					/*!< when this checkpoint age
					is exceeded we start an
					asynchronous writing of a new
					checkpoint */
osku's avatar
osku committed
902
	ulint		max_checkpoint_age;
903
					/*!< this is the maximum allowed value
osku's avatar
osku committed
904 905
					for lsn - last_checkpoint_lsn when a
					new query step is started */
906
	ib_uint64_t	next_checkpoint_no;
907
					/*!< next checkpoint number */
908
	ib_uint64_t	last_checkpoint_lsn;
909
					/*!< latest checkpoint lsn */
910
	ib_uint64_t	next_checkpoint_lsn;
911
					/*!< next checkpoint lsn */
osku's avatar
osku committed
912
	ulint		n_pending_checkpoint_writes;
913
					/*!< number of currently pending
osku's avatar
osku committed
914
					checkpoint writes */
915
	rw_lock_t	checkpoint_lock;/*!< this latch is x-locked when a
osku's avatar
osku committed
916 917 918
					checkpoint write is running; a thread
					should wait for this without owning
					the log mutex */
919
#endif /* !UNIV_HOTBACKUP */
920
	byte*		checkpoint_buf_ptr;/* unaligned checkpoint header */
921
	byte*		checkpoint_buf;	/*!< checkpoint header is read to this
osku's avatar
osku committed
922
					buffer */
923
	/* @} */
924
#ifdef UNIV_LOG_ARCHIVE
925 926
	/** Fields involved in archiving @{ */
	ulint		archiving_state;/*!< LOG_ARCH_ON, LOG_ARCH_STOPPING
osku's avatar
osku committed
927
					LOG_ARCH_STOPPED, LOG_ARCH_OFF */
928
	ib_uint64_t	archived_lsn;	/*!< archiving has advanced to this
osku's avatar
osku committed
929 930
					lsn */
	ulint		max_archived_lsn_age_async;
931
					/*!< recommended maximum age of
osku's avatar
osku committed
932 933 934
					archived_lsn, before we start
					asynchronous copying to the archive */
	ulint		max_archived_lsn_age;
935
					/*!< maximum allowed age for
osku's avatar
osku committed
936
					archived_lsn */
937
	ib_uint64_t	next_archived_lsn;/*!< during an archive write,
osku's avatar
osku committed
938 939 940 941 942
					until the write is completed, we
					store the next value for
					archived_lsn here: the write
					completion function then sets the new
					value to archived_lsn */
943
	ulint		archiving_phase;/*!< LOG_ARCHIVE_READ or
osku's avatar
osku committed
944 945
					LOG_ARCHIVE_WRITE */
	ulint		n_pending_archive_ios;
946
					/*!< number of currently pending reads
osku's avatar
osku committed
947
					or writes in archiving */
948
	rw_lock_t	archive_lock;	/*!< this latch is x-locked when an
osku's avatar
osku committed
949 950 951
					archive write is running; a thread
					should wait for this without owning
					the log mutex */
952 953
	ulint		archive_buf_size;/*!< size of archive_buf */
	byte*		archive_buf;	/*!< log segment is written to the
osku's avatar
osku committed
954
					archive from this buffer */
955
	os_event_t	archiving_on;	/*!< if archiving has been stopped,
osku's avatar
osku committed
956 957
					a thread can wait for this event to
					become signaled */
958
	/* @} */
959
#endif /* UNIV_LOG_ARCHIVE */
osku's avatar
osku committed
960 961
};

962 963
#ifdef UNIV_LOG_ARCHIVE
/** Archiving state @{ */
osku's avatar
osku committed
964 965 966 967 968
#define LOG_ARCH_ON		71
#define LOG_ARCH_STOPPING	72
#define LOG_ARCH_STOPPING2	73
#define LOG_ARCH_STOPPED	74
#define LOG_ARCH_OFF		75
969 970
/* @} */
#endif /* UNIV_LOG_ARCHIVE */
osku's avatar
osku committed
971 972 973 974 975 976

#ifndef UNIV_NONINL
#include "log0log.ic"
#endif

#endif