log is a very important module in InnoDB, there are two types of logs in InnoDB: Redo log and undo log. Where Redolog log is used to do data recovery and database restart when the page data synchronization recovery, redo log is built on the mini Transaction foundation. When the database executes a transaction, the minitransaction generates redo log to guarantee the persistence of the transaction.
1.mini transaction three Protocols
Mini-transcation is used to implement INNODB's physical logical log write and page recovery, through mini-transcation to ensure that concurrent transaction operations and database exceptions are page-consistent. To get the consistency of the page, Mini-transaction follows the following three protocols:
1. The FIX Rules
2. Write-ahead Log
3. Force-log-at-commit
1.1The FIX Rules
The FIX rules rules as follows:
Modify a page to get the X-latch of the page
Access to a page is required to obtain the S-latch or X-latch of the page
Hold the latch of the page until you modify or access the page by completing the operation
1.2write-ahead Log
Write-ahead log means that if a page operation is written to a persistent device, the corresponding log in memory must be written to the persistence device. Each page has an LSN, and each page modification requires that the LSN be maintained, and when a page needs to be written to a persisted device, the log that requires a small amount of memory to the page LSN is written first to the persistence device. After the log is finished, the latch of the page is fixed, and then the page in memory is brushed. After completing the brush disc, release the page latch. This follows the FIX rules agreement.
1.3 Force-log-at-commit
A transaction can modify multiple pages at the same time, write-aheadlog the consistency of a single data page, and cannot guarantee the persistence of a transaction. Force-log-at-commit requires that when a transaction commits, it generates all the mini-transaction logs that must be brushed onto the persistent device. This way, even when the page data brush disk downtime, you can also log for redo recovery.
2 mini-transaction of log implementations
InnoDB is a physical logical log that uses mini-transaction to construct the operation, and when the transaction is executed, the data consistency and persistence of the page is ensured through the MTR. Mini-transaction is the implementation of Mini-transaction's three protocols through a mtr_t structure. mtr_t is defined as follows:
typedef struct MTR_STRUCT { ulint state; The state of/*mtr, Mtr_active, mtr_commiting, mtr_committed*/ dyn_array_t memo; /* The list of latch being held */ dyn_array_t log; /*MTR generated log data */ ibool modifications; /* Whether to modify the page */ ulint n_log_recs; /*log number of operating pages */ ulint Log_mode; /*log operation mode, Mtr_log_all, Mtr_log_none, mtr_log_short_inserts*/ dulint start_lsn; lsn*/dulint End_lsn of/*mtr initiation ; /*mtr end of lsn*/ ulint magic_n; /* Magic word */ }mtr_t;
The member memo is an array list of latch holding states, using Dyn_array_t's dynamic memory structure to hold, and each cell stores a structure such as mtr_memo_slot_t. Defined as follows:
typedef struct MTR_MEMO_SLOT_STRUCT { ulint type; /*latch Type value */ void* object; /*latch object handle, can be rw_lock_t or buf_block_t*/ }mtr_memo_slot_t;
The latch types are as follows:
Mtr_memo_page_s_fix/*rw_locks-latch*/
Mtr_memo_page_x_fix/*rw_lockx-latch*/
Mtr_memo_buf_fix/*buf_block_t*/
Mtr_memo_s_lock/*rw_lock s-latch*/
Mtr_memo_x_lock/*rw_lock x-latch*/
memo Latch Management interface
Mtr_memo_push latch get a single, and deposit status information into MTR memo
mtr_release_s_latch_at_savepoint release Memo offset savepoint slot lock status
mtr_memo_contains Determine if the lock object is in Memo
mtr_memo_slot_release &NBSP ; release control of slot locks
Mtr_memo_pop_all release control of Locks in all memo
The log member in mt_t is also a dyn_array_t dynamic structure of memory used to hold the log information generated by the MTR. The write to the log is written by Mtr0log.h. Here refers to the log format, the log format is composed of a log header and the log body, the log header information is composed of type, space and page no, by the Mlog_write_initial_log_record_fast function written to the log of mtr_t. Here is a more specific:
The data write of the log body is written by the log write method in Mtr0log.h. N_log_recs will add 1 for each write-per-hop operation log.
The identity modifications is a data churn that identifies if there is a page, and if so, the Mtr->log brush disk is mtr_commit when called, and then the control of the MTR is released. The log will certainly be brushed at the end of the MTR, which complies with the FORCE-LOG-AT-COMMIT regulations. The log write call is the Log_write_low function.
2.1 mtr_t's memory structure diagram
3 SummaryMini Transaction is the smallest guaranteed unit for InnoDB durability in acid, all involving transaction execution, page data brush disk, redo log data recovery, and so on, which require mini transaction construction and execution. Almost all of the modules are related to mini transaction, such as: Btree, page, transaction, Inser Tbuffer, Redo-log, D to the mini transcaion understanding can not isolate to see the source code, should be combined with redo log, Page-related code understanding. It is the cornerstone of understanding how INNODB works.
MySQL Series: InnoDB source analysis of the mini transaction