Drizzled Public API Documentation

log0log.h
Go to the documentation of this file.
00001 /*****************************************************************************
00002 
00003 Copyright (C) 1995, 2010, Innobase Oy. All Rights Reserved.
00004 Copyright (C) 2009 Google Inc.
00005 
00006 Portions of this file contain modifications contributed and copyrighted by
00007 Google, Inc. Those modifications are gratefully acknowledged and are described
00008 briefly in the InnoDB documentation. The contributions by Google are
00009 incorporated with their permission, and subject to the conditions contained in
00010 the file COPYING.Google.
00011 
00012 This program is free software; you can redistribute it and/or modify it under
00013 the terms of the GNU General Public License as published by the Free Software
00014 Foundation; version 2 of the License.
00015 
00016 This program is distributed in the hope that it will be useful, but WITHOUT
00017 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00018 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
00019 
00020 You should have received a copy of the GNU General Public License along with
00021 this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
00022 St, Fifth Floor, Boston, MA 02110-1301 USA
00023 
00024 *****************************************************************************/
00025 
00026 /**************************************************/
00033 #pragma once
00034 #ifndef log0log_h
00035 #define log0log_h
00036 
00037 #include "univ.i"
00038 #include "ut0byte.h"
00039 #include "ut0lst.h"
00040 #ifndef UNIV_HOTBACKUP
00041 #include "sync0sync.h"
00042 #include "sync0rw.h"
00043 #endif /* !UNIV_HOTBACKUP */
00044 
00046 typedef struct log_struct log_t;
00048 typedef struct log_group_struct log_group_t;
00049 
00050 #ifdef UNIV_DEBUG
00051 
00052 extern  ibool log_do_write;
00054 extern  ibool log_debug_writes;
00055 #else /* UNIV_DEBUG */
00056 
00057 # define log_do_write TRUE
00058 #endif /* UNIV_DEBUG */
00059 
00061 #define LOG_NO_WAIT   91
00062 #define LOG_WAIT_ONE_GROUP  92
00063 #define LOG_WAIT_ALL_GROUPS 93
00064 /* @} */
00066 #define LOG_MAX_N_GROUPS  32
00067 
00068 #ifndef UNIV_HOTBACKUP
00069 /****************************************************************/
00073 UNIV_INTERN
00074 void
00075 log_fsp_current_free_limit_set_and_checkpoint(
00076 /*==========================================*/
00077   ulint limit); 
00078 #endif /* !UNIV_HOTBACKUP */
00079 /*******************************************************************/
00082 UNIV_INTERN
00083 ulint
00084 log_calc_where_lsn_is(
00085 /*==================*/
00086   ib_int64_t* log_file_offset,  
00088   ib_uint64_t first_header_lsn, 
00090   ib_uint64_t lsn,      
00092   ulint   n_log_files,    
00094   ib_int64_t  log_file_size);   
00096 #ifndef UNIV_HOTBACKUP
00097 /************************************************************/
00101 UNIV_INLINE
00102 ib_uint64_t
00103 log_reserve_and_write_fast(
00104 /*=======================*/
00105   const void* str,  
00106   ulint   len,  
00107   ib_uint64_t*  start_lsn);
00108 /***********************************************************************/
00110 UNIV_INLINE
00111 void
00112 log_release(void);
00113 /*=============*/
00114 /***********************************************************************/
00119 UNIV_INLINE
00120 void
00121 log_free_check(void);
00122 /*================*/
00123 /************************************************************/
00127 UNIV_INTERN
00128 ib_uint64_t
00129 log_reserve_and_open(
00130 /*=================*/
00131   ulint len); 
00132 /************************************************************/
00135 UNIV_INTERN
00136 void
00137 log_write_low(
00138 /*==========*/
00139   byte* str,    
00140   ulint str_len); 
00141 /************************************************************/
00144 UNIV_INTERN
00145 ib_uint64_t
00146 log_close(void);
00147 /*===========*/
00148 /************************************************************/
00151 UNIV_INLINE
00152 ib_uint64_t
00153 log_get_lsn(void);
00154 /*=============*/
00155 /****************************************************************
00156 Gets the log group capacity. It is OK to read the value without
00157 holding log_sys->mutex because it is constant.
00158 @return log group capacity */
00159 UNIV_INLINE
00160 ulint
00161 log_get_capacity(void);
00162 /*==================*/
00163 /******************************************************/
00165 UNIV_INTERN
00166 void
00167 log_init(void);
00168 /*==========*/
00169 /******************************************************************/
00171 UNIV_INTERN
00172 void
00173 log_group_init(
00174 /*===========*/
00175   ulint id,     
00176   ulint n_files,    
00177   ulint file_size,    
00178   ulint space_id,   
00181   ulint archive_space_id);  
00186 /******************************************************/
00188 UNIV_INTERN
00189 void
00190 log_io_complete(
00191 /*============*/
00192   log_group_t*  group); 
00193 /******************************************************/
00198 UNIV_INTERN
00199 void
00200 log_write_up_to(
00201 /*============*/
00202   ib_uint64_t lsn,  
00205   ulint   wait, 
00207   ibool   flush_to_disk);
00210 /****************************************************************/
00212 UNIV_INTERN
00213 void
00214 log_buffer_flush_to_disk(void);
00215 /*==========================*/
00216 /****************************************************************/
00221 UNIV_INTERN
00222 void
00223 log_buffer_sync_in_background(
00224 /*==========================*/
00225   ibool flush); /*<! in: flush the logs to disk */
00226 /****************************************************************/
00232 UNIV_INTERN
00233 ibool
00234 log_preflush_pool_modified_pages(
00235 /*=============================*/
00236   ib_uint64_t new_oldest, 
00239   ibool   sync);    
00241 /******************************************************/
00247 UNIV_INTERN
00248 ibool
00249 log_checkpoint(
00250 /*===========*/
00251   ibool sync,   
00253   ibool write_always);  
00259 /****************************************************************/
00261 UNIV_INTERN
00262 void
00263 log_make_checkpoint_at(
00264 /*===================*/
00265   ib_uint64_t lsn,    
00268   ibool   write_always);  
00275 /****************************************************************/
00280 UNIV_INTERN
00281 void
00282 logs_empty_and_mark_files_at_shutdown(void);
00283 /*=======================================*/
00284 /******************************************************/
00286 UNIV_INTERN
00287 void
00288 log_group_read_checkpoint_info(
00289 /*===========================*/
00290   log_group_t*  group,  
00291   ulint   field); 
00292 /*******************************************************************/
00294 UNIV_INTERN
00295 void
00296 log_checkpoint_get_nth_group_info(
00297 /*==============================*/
00298   const byte* buf,  
00299   ulint   n,  
00300   ulint*    file_no,
00301   ulint*    offset);
00302 /******************************************************/
00304 UNIV_INTERN
00305 void
00306 log_groups_write_checkpoint_info(void);
00307 /*==================================*/
00308 /********************************************************************/
00311 UNIV_INTERN
00312 ibool
00313 log_archive_do(
00314 /*===========*/
00315   ibool sync, 
00316   ulint*  n_bytes);
00318 /****************************************************************/
00325 UNIV_INTERN
00326 ulint
00327 log_archive_stop(void);
00328 /*==================*/
00329 /****************************************************************/
00332 UNIV_INTERN
00333 ulint
00334 log_archive_start(void);
00335 /*===================*/
00336 /****************************************************************/
00339 UNIV_INTERN
00340 ulint
00341 log_archive_noarchivelog(void);
00342 /*==========================*/
00343 /****************************************************************/
00346 UNIV_INTERN
00347 ulint
00348 log_archive_archivelog(void);
00349 /*========================*/
00350 /******************************************************/
00352 UNIV_INTERN
00353 void
00354 log_archived_file_name_gen(
00355 /*=======================*/
00356   char* buf,  
00357   ulint id, 
00358   ulint file_no);
00359 #else /* !UNIV_HOTBACKUP */
00360 /******************************************************/
00363 UNIV_INTERN
00364 void
00365 log_reset_first_header_and_checkpoint(
00366 /*==================================*/
00367   byte*   hdr_buf,
00369   ib_uint64_t start); 
00372 #endif /* !UNIV_HOTBACKUP */
00373 /********************************************************************/
00378 UNIV_INTERN
00379 void
00380 log_check_margins(void);
00381 /*===================*/
00382 #ifndef UNIV_HOTBACKUP
00383 /******************************************************/
00385 UNIV_INTERN
00386 void
00387 log_group_read_log_seg(
00388 /*===================*/
00389   ulint   type,   
00390   byte*   buf,    
00391   log_group_t*  group,    
00392   ib_uint64_t start_lsn,  
00393   ib_uint64_t end_lsn); 
00394 /******************************************************/
00396 UNIV_INTERN
00397 void
00398 log_group_write_buf(
00399 /*================*/
00400   log_group_t*  group,    
00401   byte*   buf,    
00402   ulint   len,    
00404   ib_uint64_t start_lsn,  
00407   ulint   new_data_offset);
00411 /********************************************************/
00415 UNIV_INTERN
00416 void
00417 log_group_set_fields(
00418 /*=================*/
00419   log_group_t*  group,  
00420   ib_uint64_t lsn); 
00422 /******************************************************/
00426 UNIV_INTERN
00427 ulint
00428 log_group_get_capacity(
00429 /*===================*/
00430   const log_group_t*  group); 
00431 #endif /* !UNIV_HOTBACKUP */
00432 /************************************************************/
00435 UNIV_INLINE
00436 ibool
00437 log_block_get_flush_bit(
00438 /*====================*/
00439   const byte* log_block); 
00440 /************************************************************/
00443 UNIV_INLINE
00444 ulint
00445 log_block_get_hdr_no(
00446 /*=================*/
00447   const byte* log_block); 
00448 /************************************************************/
00451 UNIV_INLINE
00452 ulint
00453 log_block_get_data_len(
00454 /*===================*/
00455   const byte* log_block); 
00456 /************************************************************/
00458 UNIV_INLINE
00459 void
00460 log_block_set_data_len(
00461 /*===================*/
00462   byte* log_block,  
00463   ulint len);   
00464 /************************************************************/
00467 UNIV_INLINE
00468 ulint
00469 log_block_calc_checksum(
00470 /*====================*/
00471   const byte* block); 
00472 /************************************************************/
00475 UNIV_INLINE
00476 ulint
00477 log_block_get_checksum(
00478 /*===================*/
00479   const byte* log_block); 
00480 /************************************************************/
00482 UNIV_INLINE
00483 void
00484 log_block_set_checksum(
00485 /*===================*/
00486   byte* log_block,  
00487   ulint checksum);  
00488 /************************************************************/
00492 UNIV_INLINE
00493 ulint
00494 log_block_get_first_rec_group(
00495 /*==========================*/
00496   const byte* log_block); 
00497 /************************************************************/
00499 UNIV_INLINE
00500 void
00501 log_block_set_first_rec_group(
00502 /*==========================*/
00503   byte* log_block,  
00504   ulint offset);  
00505 /************************************************************/
00508 UNIV_INLINE
00509 ulint
00510 log_block_get_checkpoint_no(
00511 /*========================*/
00512   const byte* log_block); 
00513 /************************************************************/
00515 UNIV_INLINE
00516 void
00517 log_block_init(
00518 /*===========*/
00519   byte*   log_block,  
00520   ib_uint64_t lsn);   
00521 /************************************************************/
00524 UNIV_INLINE
00525 void
00526 log_block_init_in_old_format(
00527 /*=========================*/
00528   byte*   log_block,  
00529   ib_uint64_t lsn);   
00530 /************************************************************/
00533 UNIV_INLINE
00534 ulint
00535 log_block_convert_lsn_to_no(
00536 /*========================*/
00537   ib_uint64_t lsn); 
00538 /******************************************************/
00540 UNIV_INTERN
00541 void
00542 log_print(
00543 /*======*/
00544   FILE* file);  
00545 /******************************************************/
00548 UNIV_INTERN
00549 ibool
00550 log_peek_lsn(
00551 /*=========*/
00552   ib_uint64_t*  lsn); 
00553 /**********************************************************************/
00555 UNIV_INTERN
00556 void
00557 log_refresh_stats(void);
00558 /*===================*/
00559 /**********************************************************
00560 Shutdown the log system but do not release all the memory. */
00561 UNIV_INTERN
00562 void
00563 log_shutdown(void);
00564 /*==============*/
00565 /**********************************************************
00566 Free the log system data structures. */
00567 UNIV_INTERN
00568 void
00569 log_mem_free(void);
00570 /*==============*/
00571 
00572 extern log_t* log_sys;
00573 
00574 /* Values used as flags */
00575 #define LOG_FLUSH 7652559
00576 #define LOG_CHECKPOINT  78656949
00577 #ifdef UNIV_LOG_ARCHIVE
00578 # define LOG_ARCHIVE  11122331
00579 #endif /* UNIV_LOG_ARCHIVE */
00580 #define LOG_RECOVER 98887331
00581 
00582 /* The counting of lsn's starts from this value: this must be non-zero */
00583 #define LOG_START_LSN   ((ib_uint64_t) (16 * OS_FILE_LOG_BLOCK_SIZE))
00584 
00585 #define LOG_BUFFER_SIZE   (srv_log_buffer_size * UNIV_PAGE_SIZE)
00586 #define LOG_ARCHIVE_BUF_SIZE  (srv_log_buffer_size * UNIV_PAGE_SIZE / 4)
00587 
00588 /* Offsets of a log block header */
00589 #define LOG_BLOCK_HDR_NO  0 /* block number which must be > 0 and
00590           is allowed to wrap around at 2G; the
00591           highest bit is set to 1 if this is the
00592           first log block in a log flush write
00593           segment */
00594 #define LOG_BLOCK_FLUSH_BIT_MASK 0x80000000UL
00595           /* mask used to get the highest bit in
00596           the preceding field */
00597 #define LOG_BLOCK_HDR_DATA_LEN  4 /* number of bytes of log written to
00598           this block */
00599 #define LOG_BLOCK_FIRST_REC_GROUP 6 /* offset of the first start of an
00600           mtr log record group in this log block,
00601           0 if none; if the value is the same
00602           as LOG_BLOCK_HDR_DATA_LEN, it means
00603           that the first rec group has not yet
00604           been catenated to this log block, but
00605           if it will, it will start at this
00606           offset; an archive recovery can
00607           start parsing the log records starting
00608           from this offset in this log block,
00609           if value not 0 */
00610 #define LOG_BLOCK_CHECKPOINT_NO 8 /* 4 lower bytes of the value of
00611           log_sys->next_checkpoint_no when the
00612           log block was last written to: if the
00613           block has not yet been written full,
00614           this value is only updated before a
00615           log buffer flush */
00616 #define LOG_BLOCK_HDR_SIZE  12  /* size of the log block header in
00617           bytes */
00618 
00619 /* Offsets of a log block trailer from the end of the block */
00620 #define LOG_BLOCK_CHECKSUM  4 /* 4 byte checksum of the log block
00621           contents; in InnoDB versions
00622           < 3.23.52 this did not contain the
00623           checksum but the same value as
00624           .._HDR_NO */
00625 #define LOG_BLOCK_TRL_SIZE  4 /* trailer size in bytes */
00626 
00627 /* Offsets for a checkpoint field */
00628 #define LOG_CHECKPOINT_NO   0
00629 #define LOG_CHECKPOINT_LSN    8
00630 #define LOG_CHECKPOINT_OFFSET   16
00631 #define LOG_CHECKPOINT_LOG_BUF_SIZE 20
00632 #define LOG_CHECKPOINT_ARCHIVED_LSN 24
00633 #define LOG_CHECKPOINT_GROUP_ARRAY  32
00634 
00635 /* For each value smaller than LOG_MAX_N_GROUPS the following 8 bytes: */
00636 
00637 #define LOG_CHECKPOINT_ARCHIVED_FILE_NO 0
00638 #define LOG_CHECKPOINT_ARCHIVED_OFFSET  4
00639 
00640 #define LOG_CHECKPOINT_ARRAY_END  (LOG_CHECKPOINT_GROUP_ARRAY\
00641               + LOG_MAX_N_GROUPS * 8)
00642 #define LOG_CHECKPOINT_CHECKSUM_1 LOG_CHECKPOINT_ARRAY_END
00643 #define LOG_CHECKPOINT_CHECKSUM_2 (4 + LOG_CHECKPOINT_ARRAY_END)
00644 #define LOG_CHECKPOINT_FSP_FREE_LIMIT (8 + LOG_CHECKPOINT_ARRAY_END)
00645           /* current fsp free limit in
00646           tablespace 0, in units of one
00647           megabyte; this information is only used
00648           by ibbackup to decide if it can
00649           truncate unused ends of
00650           non-auto-extending data files in space
00651           0 */
00652 #define LOG_CHECKPOINT_FSP_MAGIC_N  (12 + LOG_CHECKPOINT_ARRAY_END)
00653           /* this magic number tells if the
00654           checkpoint contains the above field:
00655           the field was added to
00656           InnoDB-3.23.50 */
00657 #define LOG_CHECKPOINT_SIZE   (16 + LOG_CHECKPOINT_ARRAY_END)
00658 
00659 #define LOG_CHECKPOINT_FSP_MAGIC_N_VAL  1441231243
00660 
00661 /* Offsets of a log file header */
00662 #define LOG_GROUP_ID    0 /* log group number */
00663 #define LOG_FILE_START_LSN  4 /* lsn of the start of data in this
00664           log file */
00665 #define LOG_FILE_NO   12  /* 4-byte archived log file number;
00666           this field is only defined in an
00667           archived log file */
00668 #define LOG_FILE_WAS_CREATED_BY_HOT_BACKUP 16
00669           /* a 32-byte field which contains
00670           the string 'ibbackup' and the
00671           creation time if the log file was
00672           created by ibbackup --restore;
00673           when mysqld is first time started
00674           on the restored database, it can
00675           print helpful info for the user */
00676 #define LOG_FILE_ARCH_COMPLETED OS_FILE_LOG_BLOCK_SIZE
00677           /* this 4-byte field is TRUE when
00678           the writing of an archived log file
00679           has been completed; this field is
00680           only defined in an archived log file */
00681 #define LOG_FILE_END_LSN  (OS_FILE_LOG_BLOCK_SIZE + 4)
00682           /* lsn where the archived log file
00683           at least extends: actually the
00684           archived log file may extend to a
00685           later lsn, as long as it is within the
00686           same log block as this lsn; this field
00687           is defined only when an archived log
00688           file has been completely written */
00689 #define LOG_CHECKPOINT_1  OS_FILE_LOG_BLOCK_SIZE
00690           /* first checkpoint field in the log
00691           header; we write alternately to the
00692           checkpoint fields when we make new
00693           checkpoints; this field is only defined
00694           in the first log file of a log group */
00695 #define LOG_CHECKPOINT_2  (3 * OS_FILE_LOG_BLOCK_SIZE)
00696           /* second checkpoint field in the log
00697           header */
00698 #define LOG_FILE_HDR_SIZE (4 * OS_FILE_LOG_BLOCK_SIZE)
00699 
00700 #define LOG_GROUP_OK    301
00701 #define LOG_GROUP_CORRUPTED 302
00702 
00705 struct log_group_struct{
00706   /* The following fields are protected by log_sys->mutex */
00707   ulint   id;   
00708   ulint   n_files;  
00709   ulint   file_size;  
00711   ulint   space_id; 
00713   ulint   state;    
00715   ib_uint64_t lsn;    
00717   ulint   lsn_offset; 
00718   ulint   n_pending_writes;
00720   byte**    file_header_bufs_ptr;
00721   byte**    file_header_bufs;
00723 #ifdef UNIV_LOG_ARCHIVE
00724   /*-----------------------------*/
00725   byte**    archive_file_header_bufs_ptr;
00726   byte**    archive_file_header_bufs;
00728   ulint   archive_space_id;
00731   ulint   archived_file_no;
00733   ulint   archived_offset;
00737   ulint   next_archived_file_no;
00743   ulint   next_archived_offset; 
00744 #endif /* UNIV_LOG_ARCHIVE */
00745   /*-----------------------------*/
00746   ib_uint64_t scanned_lsn;  
00749   byte*   checkpoint_buf_ptr;
00750   byte*   checkpoint_buf; 
00752   UT_LIST_NODE_T(log_group_t)
00753       log_groups; 
00754 };
00755 
00757 struct log_struct{
00758   byte    pad[64];  
00761   ib_uint64_t lsn;    
00762   ulint   buf_free; 
00764 #ifndef UNIV_HOTBACKUP
00765   mutex_t   mutex;    
00766 #endif /* !UNIV_HOTBACKUP */
00767 
00768   mutex_t   log_flush_order_mutex;
00776   byte*   buf_ptr;  /* unaligned log buffer */
00777   byte*   buf;    
00778   ulint   buf_size; 
00779   ulint   max_buf_free; 
00782   ulint   old_buf_free; 
00785   ib_uint64_t old_lsn;  
00788   ibool   check_flush_or_checkpoint;
00797   UT_LIST_BASE_NODE_T(log_group_t)
00798       log_groups; 
00800 #ifndef UNIV_HOTBACKUP
00801 
00803   ulint   buf_next_to_write;
00810   ib_uint64_t written_to_some_lsn;
00816   ib_uint64_t written_to_all_lsn;
00832   ib_uint64_t write_lsn;  
00834   ulint   write_end_offset;
00839   ib_uint64_t current_flush_lsn;
00841   ib_uint64_t flushed_to_disk_lsn;
00844   ulint   n_pending_writes;
00846   /* NOTE on the 'flush' in names of the fields below: starting from
00847   4.0.14, we separate the write of the log file and the actual fsync()
00848   or other method to flush it to disk. The names below shhould really
00849   be 'flush_or_write'! */
00850   os_event_t  no_flush_event; 
00856   ibool   one_flushed;  
00860   os_event_t  one_flushed_event;
00869   ulint   n_log_ios;  
00871   ulint   n_log_ios_old;  
00873   time_t    last_printout_time;
00875   /* @} */
00876 
00878   ulint   log_group_capacity; 
00883   ulint   max_modified_age_async;
00889   ulint   max_modified_age_sync;
00895   ulint   adm_checkpoint_interval;
00900   ulint   max_checkpoint_age_async;
00905   ulint   max_checkpoint_age;
00909   ib_uint64_t next_checkpoint_no;
00911   ib_uint64_t last_checkpoint_lsn;
00913   ib_uint64_t next_checkpoint_lsn;
00915   ulint   n_pending_checkpoint_writes;
00918   rw_lock_t checkpoint_lock;
00922 #endif /* !UNIV_HOTBACKUP */
00923   byte*   checkpoint_buf_ptr;/* unaligned checkpoint header */
00924   byte*   checkpoint_buf; 
00926   /* @} */
00927 #ifdef UNIV_LOG_ARCHIVE
00928 
00929   ulint   archiving_state;
00931   ib_uint64_t archived_lsn; 
00933   ulint   max_archived_lsn_age_async;
00937   ulint   max_archived_lsn_age;
00940   ib_uint64_t next_archived_lsn;
00946   ulint   archiving_phase;
00948   ulint   n_pending_archive_ios;
00951   rw_lock_t archive_lock; 
00955   ulint   archive_buf_size;
00956   byte*   archive_buf;  
00958   os_event_t  archiving_on; 
00961   /* @} */
00962 #endif /* UNIV_LOG_ARCHIVE */
00963 };
00964 
00966 #define log_flush_order_mutex_own() \
00967   mutex_own(&log_sys->log_flush_order_mutex)
00968 
00970 #define log_flush_order_mutex_enter() do {    \
00971   mutex_enter(&log_sys->log_flush_order_mutex); \
00972 } while (0)
00973 
00974 # define log_flush_order_mutex_exit() do {    \
00975   mutex_exit(&log_sys->log_flush_order_mutex);  \
00976 } while (0)
00977 
00978 #ifdef UNIV_LOG_ARCHIVE
00979 
00980 #define LOG_ARCH_ON   71
00981 #define LOG_ARCH_STOPPING 72
00982 #define LOG_ARCH_STOPPING2  73
00983 #define LOG_ARCH_STOPPED  74
00984 #define LOG_ARCH_OFF    75
00985 /* @} */
00986 #endif /* UNIV_LOG_ARCHIVE */
00987 
00988 #ifndef UNIV_NONINL
00989 #include "log0log.ic"
00990 #endif
00991 
00992 #endif