📄 trx0sys.c
字号:
/******************************************************Transaction system(c) 1996 Innobase OyCreated 3/26/1996 Heikki Tuuri*******************************************************/#include "trx0sys.h"#ifdef UNIV_NONINL#include "trx0sys.ic"#endif#include "fsp0fsp.h"#include "mtr0mtr.h"#include "trx0trx.h"#include "trx0rseg.h"#include "trx0undo.h"#include "srv0srv.h"#include "trx0purge.h"#include "log0log.h"#include "os0file.h"/* The transaction system */trx_sys_t* trx_sys = NULL;trx_doublewrite_t* trx_doublewrite = NULL;/* The following is set to TRUE when we are upgrading from the old format datafiles to the new >= 4.1.x format multiple tablespaces format data files */ibool trx_doublewrite_must_reset_space_ids = FALSE;/* The following is TRUE when we are using the database in the new format,i.e., we have successfully upgraded, or have created a new databaseinstallation */ibool trx_sys_multiple_tablespace_format = FALSE;/* In a MySQL replication slave, in crash recovery we store the master logfile name and position here. We have successfully got the updates to InnoDBup to this position. If .._pos is -1, it means no crash recovery was needed,or there was no master log position info inside InnoDB. */char trx_sys_mysql_master_log_name[TRX_SYS_MYSQL_LOG_NAME_LEN];ib_longlong trx_sys_mysql_master_log_pos = -1;/* If this MySQL server uses binary logging, after InnoDB has been initedand if it has done a crash recovery, we store the binlog file name and positionhere. If .._pos is -1, it means there was no binlog position info insideInnoDB. */char trx_sys_mysql_bin_log_name[TRX_SYS_MYSQL_LOG_NAME_LEN];ib_longlong trx_sys_mysql_bin_log_pos = -1;/********************************************************************Determines if a page number is located inside the doublewrite buffer. */ibooltrx_doublewrite_page_inside(/*========================*/ /* out: TRUE if the location is inside the two blocks of the doublewrite buffer */ ulint page_no) /* in: page number */{ if (trx_doublewrite == NULL) { return(FALSE); } if (page_no >= trx_doublewrite->block1 && page_no < trx_doublewrite->block1 + TRX_SYS_DOUBLEWRITE_BLOCK_SIZE) { return(TRUE); } if (page_no >= trx_doublewrite->block2 && page_no < trx_doublewrite->block2 + TRX_SYS_DOUBLEWRITE_BLOCK_SIZE) { return(TRUE); } return(FALSE);}/********************************************************************Creates or initialializes the doublewrite buffer at a database start. */staticvoidtrx_doublewrite_init(/*=================*/ byte* doublewrite) /* in: pointer to the doublewrite buf header on trx sys page */{ trx_doublewrite = mem_alloc(sizeof(trx_doublewrite_t)); /* Since we now start to use the doublewrite buffer, no need to call fsync() after every write to a data file */#ifdef UNIV_DO_FLUSH os_do_not_call_flush_at_each_write = TRUE;#endif /* UNIV_DO_FLUSH */ mutex_create(&(trx_doublewrite->mutex)); mutex_set_level(&(trx_doublewrite->mutex), SYNC_DOUBLEWRITE); trx_doublewrite->first_free = 0; trx_doublewrite->block1 = mach_read_from_4( doublewrite + TRX_SYS_DOUBLEWRITE_BLOCK1); trx_doublewrite->block2 = mach_read_from_4( doublewrite + TRX_SYS_DOUBLEWRITE_BLOCK2); trx_doublewrite->write_buf_unaligned = ut_malloc( (1 + 2 * TRX_SYS_DOUBLEWRITE_BLOCK_SIZE) * UNIV_PAGE_SIZE); trx_doublewrite->write_buf = ut_align( trx_doublewrite->write_buf_unaligned, UNIV_PAGE_SIZE); trx_doublewrite->buf_block_arr = mem_alloc( 2 * TRX_SYS_DOUBLEWRITE_BLOCK_SIZE * sizeof(void*));}/********************************************************************Marks the trx sys header when we have successfully upgraded to the >= 4.1.xmultiple tablespace format. */voidtrx_sys_mark_upgraded_to_multiple_tablespaces(void)/*===============================================*/{ page_t* page; byte* doublewrite; mtr_t mtr; /* We upgraded to 4.1.x and reset the space id fields in the doublewrite buffer. Let us mark to the trx_sys header that the upgrade has been done. */ mtr_start(&mtr); page = buf_page_get(TRX_SYS_SPACE, TRX_SYS_PAGE_NO, RW_X_LATCH, &mtr);#ifdef UNIV_SYNC_DEBUG buf_page_dbg_add_level(page, SYNC_NO_ORDER_CHECK);#endif /* UNIV_SYNC_DEBUG */ doublewrite = page + TRX_SYS_DOUBLEWRITE; mlog_write_ulint(doublewrite + TRX_SYS_DOUBLEWRITE_SPACE_ID_STORED, TRX_SYS_DOUBLEWRITE_SPACE_ID_STORED_N, MLOG_4BYTES, &mtr); mtr_commit(&mtr); /* Flush the modified pages to disk and make a checkpoint */ log_make_checkpoint_at(ut_dulint_max, TRUE); trx_sys_multiple_tablespace_format = TRUE;}/********************************************************************Creates the doublewrite buffer to a new InnoDB installation. The header of thedoublewrite buffer is placed on the trx system header page. */voidtrx_sys_create_doublewrite_buf(void)/*================================*/{ page_t* page; page_t* page2; page_t* new_page; byte* doublewrite; byte* fseg_header; ulint page_no; ulint prev_page_no; ulint i; mtr_t mtr; if (trx_doublewrite) { /* Already inited */ return; }start_again: mtr_start(&mtr); page = buf_page_get(TRX_SYS_SPACE, TRX_SYS_PAGE_NO, RW_X_LATCH, &mtr);#ifdef UNIV_SYNC_DEBUG buf_page_dbg_add_level(page, SYNC_NO_ORDER_CHECK);#endif /* UNIV_SYNC_DEBUG */ doublewrite = page + TRX_SYS_DOUBLEWRITE; if (mach_read_from_4(doublewrite + TRX_SYS_DOUBLEWRITE_MAGIC) == TRX_SYS_DOUBLEWRITE_MAGIC_N) { /* The doublewrite buffer has already been created: just read in some numbers */ trx_doublewrite_init(doublewrite); mtr_commit(&mtr); } else { fprintf(stderr, "InnoDB: Doublewrite buffer not found: creating new\n"); if (buf_pool_get_curr_size() < (2 * TRX_SYS_DOUBLEWRITE_BLOCK_SIZE + FSP_EXTENT_SIZE / 2 + 100) * UNIV_PAGE_SIZE) { fprintf(stderr, "InnoDB: Cannot create doublewrite buffer: you must\n" "InnoDB: increase your buffer pool size.\n" "InnoDB: Cannot continue operation.\n"); exit(1); } page2 = fseg_create(TRX_SYS_SPACE, TRX_SYS_PAGE_NO, TRX_SYS_DOUBLEWRITE + TRX_SYS_DOUBLEWRITE_FSEG, &mtr); /* fseg_create acquires a second latch on the page, therefore we must declare it: */#ifdef UNIV_SYNC_DEBUG buf_page_dbg_add_level(page2, SYNC_NO_ORDER_CHECK);#endif /* UNIV_SYNC_DEBUG */ if (page2 == NULL) { fprintf(stderr, "InnoDB: Cannot create doublewrite buffer: you must\n" "InnoDB: increase your tablespace size.\n" "InnoDB: Cannot continue operation.\n"); /* We exit without committing the mtr to prevent its modifications to the database getting to disk */ exit(1); } fseg_header = page + TRX_SYS_DOUBLEWRITE + TRX_SYS_DOUBLEWRITE_FSEG; prev_page_no = 0; for (i = 0; i < 2 * TRX_SYS_DOUBLEWRITE_BLOCK_SIZE + FSP_EXTENT_SIZE / 2; i++) { page_no = fseg_alloc_free_page(fseg_header, prev_page_no + 1, FSP_UP, &mtr); if (page_no == FIL_NULL) { fprintf(stderr, "InnoDB: Cannot create doublewrite buffer: you must\n" "InnoDB: increase your tablespace size.\n" "InnoDB: Cannot continue operation.\n"); exit(1); } /* We read the allocated pages to the buffer pool; when they are written to disk in a flush, the space id and page number fields are also written to the pages. When we at database startup read pages from the doublewrite buffer, we know that if the space id and page number in them are the same as the page position in the tablespace, then the page has not been written to in doublewrite. */ new_page = buf_page_get(TRX_SYS_SPACE, page_no, RW_X_LATCH, &mtr);#ifdef UNIV_SYNC_DEBUG buf_page_dbg_add_level(new_page, SYNC_NO_ORDER_CHECK);#endif /* UNIV_SYNC_DEBUG */ /* Make a dummy change to the page to ensure it will be written to disk in a flush */ mlog_write_ulint(new_page + FIL_PAGE_DATA, TRX_SYS_DOUBLEWRITE_MAGIC_N, MLOG_4BYTES, &mtr); if (i == FSP_EXTENT_SIZE / 2) { mlog_write_ulint(doublewrite + TRX_SYS_DOUBLEWRITE_BLOCK1, page_no, MLOG_4BYTES, &mtr); mlog_write_ulint(doublewrite + TRX_SYS_DOUBLEWRITE_REPEAT + TRX_SYS_DOUBLEWRITE_BLOCK1, page_no, MLOG_4BYTES, &mtr); } else if (i == FSP_EXTENT_SIZE / 2 + TRX_SYS_DOUBLEWRITE_BLOCK_SIZE) { mlog_write_ulint(doublewrite + TRX_SYS_DOUBLEWRITE_BLOCK2, page_no, MLOG_4BYTES, &mtr); mlog_write_ulint(doublewrite + TRX_SYS_DOUBLEWRITE_REPEAT + TRX_SYS_DOUBLEWRITE_BLOCK2, page_no, MLOG_4BYTES, &mtr); } else if (i > FSP_EXTENT_SIZE / 2) { ut_a(page_no == prev_page_no + 1); } prev_page_no = page_no; } mlog_write_ulint(doublewrite + TRX_SYS_DOUBLEWRITE_MAGIC, TRX_SYS_DOUBLEWRITE_MAGIC_N, MLOG_4BYTES, &mtr); mlog_write_ulint(doublewrite + TRX_SYS_DOUBLEWRITE_MAGIC + TRX_SYS_DOUBLEWRITE_REPEAT, TRX_SYS_DOUBLEWRITE_MAGIC_N, MLOG_4BYTES, &mtr); mlog_write_ulint(doublewrite + TRX_SYS_DOUBLEWRITE_SPACE_ID_STORED, TRX_SYS_DOUBLEWRITE_SPACE_ID_STORED_N, MLOG_4BYTES, &mtr); mtr_commit(&mtr); /* Flush the modified pages to disk and make a checkpoint */ log_make_checkpoint_at(ut_dulint_max, TRUE); fprintf(stderr, "InnoDB: Doublewrite buffer created\n"); trx_sys_multiple_tablespace_format = TRUE; goto start_again; }}/********************************************************************At a database startup initializes the doublewrite buffer memory structure ifwe already have a doublewrite buffer created in the data files. If we areupgrading to an InnoDB version which supports multiple tablespaces, then thisfunction performs the necessary update operations. If we are in a crashrecovery, this function uses a possible doublewrite buffer to restorehalf-written pages in the data files. */voidtrx_sys_doublewrite_init_or_restore_pages(/*======================================*/ ibool restore_corrupt_pages){ byte* buf; byte* read_buf; byte* unaligned_read_buf; ulint block1; ulint block2; ulint source_page_no; byte* page; byte* doublewrite; ulint space_id; ulint page_no; ulint i; /* We do the file i/o past the buffer pool */ unaligned_read_buf = ut_malloc(2 * UNIV_PAGE_SIZE); read_buf = ut_align(unaligned_read_buf, UNIV_PAGE_SIZE); /* Read the trx sys header to check if we are using the doublewrite buffer */ fil_io(OS_FILE_READ, TRUE, TRX_SYS_SPACE, TRX_SYS_PAGE_NO, 0, UNIV_PAGE_SIZE, read_buf, NULL); doublewrite = read_buf + TRX_SYS_DOUBLEWRITE; if (mach_read_from_4(doublewrite + TRX_SYS_DOUBLEWRITE_MAGIC) == TRX_SYS_DOUBLEWRITE_MAGIC_N) { /* The doublewrite buffer has been created */ trx_doublewrite_init(doublewrite); block1 = trx_doublewrite->block1; block2 = trx_doublewrite->block2; buf = trx_doublewrite->write_buf; } else { goto leave_func; } if (mach_read_from_4(doublewrite + TRX_SYS_DOUBLEWRITE_SPACE_ID_STORED) != TRX_SYS_DOUBLEWRITE_SPACE_ID_STORED_N) { /* We are upgrading from a version < 4.1.x to a version where multiple tablespaces are supported. We must reset the space id field in the pages in the doublewrite buffer because starting from this version the space id is stored to FIL_PAGE_ARCH_LOG_NO_OR_SPACE_ID. */ trx_doublewrite_must_reset_space_ids = TRUE; fprintf(stderr,"InnoDB: Resetting space id's in the doublewrite buffer\n"); } else { trx_sys_multiple_tablespace_format = TRUE; } /* Read the pages from the doublewrite buffer to memory */ fil_io(OS_FILE_READ, TRUE, TRX_SYS_SPACE, block1, 0, TRX_SYS_DOUBLEWRITE_BLOCK_SIZE * UNIV_PAGE_SIZE, buf, NULL); fil_io(OS_FILE_READ, TRUE, TRX_SYS_SPACE, block2, 0, TRX_SYS_DOUBLEWRITE_BLOCK_SIZE * UNIV_PAGE_SIZE, buf + TRX_SYS_DOUBLEWRITE_BLOCK_SIZE * UNIV_PAGE_SIZE, NULL); /* Check if any of these pages is half-written in data files, in the intended position */ page = buf; for (i = 0; i < TRX_SYS_DOUBLEWRITE_BLOCK_SIZE * 2; i++) { page_no = mach_read_from_4(page + FIL_PAGE_OFFSET); if (trx_doublewrite_must_reset_space_ids) { space_id = 0; mach_write_to_4(page + FIL_PAGE_ARCH_LOG_NO_OR_SPACE_ID, 0); /* We do not need to calculate new checksums for the pages because the field .._SPACE_ID does not affect them. Write the page back to where we read it from. */ if (i < TRX_SYS_DOUBLEWRITE_BLOCK_SIZE) { source_page_no = block1 + i; } else { source_page_no = block2 + i - TRX_SYS_DOUBLEWRITE_BLOCK_SIZE; } fil_io(OS_FILE_WRITE, TRUE, 0, source_page_no, 0, UNIV_PAGE_SIZE, page, NULL); /* printf("Resetting space id in page %lu\n", source_page_no); */ } else { space_id = mach_read_from_4( page + FIL_PAGE_ARCH_LOG_NO_OR_SPACE_ID); } if (!restore_corrupt_pages) { /* The database was shut down gracefully: no need to restore pages */ } else if (!fil_tablespace_exists_in_mem(space_id)) { /* Maybe we have dropped the single-table tablespace and this page once belonged to it: do nothing */ } else if (!fil_check_adress_in_tablespace(space_id, page_no)) { fprintf(stderr,"InnoDB: Warning: a page in the doublewrite buffer is not within space\n""InnoDB: bounds; space id %lu page number %lu, page %lu in doublewrite buf.\n", (ulong) space_id, (ulong) page_no, (ulong) i); } else if (space_id == TRX_SYS_SPACE && ( (page_no >= block1 && page_no < block1 + TRX_SYS_DOUBLEWRITE_BLOCK_SIZE) || (page_no >= block2 && page_no < block2 + TRX_SYS_DOUBLEWRITE_BLOCK_SIZE))) { /* It is an unwritten doublewrite buffer page: do nothing */ } else { /* Read in the actual page from the data files */ fil_io(OS_FILE_READ, TRUE, space_id, page_no, 0, UNIV_PAGE_SIZE, read_buf, NULL); /* Check if the page is corrupt */ if (buf_page_is_corrupted(read_buf)) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -