MySQL series: mini transaction and mysqlinnodb for innodb source code analysis

Source: Internet
Author: User

MySQL series: mini transaction and mysqlinnodb for innodb source code analysis

Log is a very important module of innodb. There are two types of logs in innodb: redo log and undo log. The redolog is used for Data Exception recovery and Data Synchronization recovery of the number of pages when the database is restarted. The redo log is based on mini transaction. When a database executes a transaction, it generates a redo log through minitransaction to ensure transaction persistence.

1. Three mini transaction protocols

Mini-transcation is used to write and restore innodb physical logic logs, and ensures page consistency between concurrent transaction operations and database exceptions through mini-transcation. To get page consistency, 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 are as follows:

To modify a page, you need to obtain the x-latch of the page.

To access a page, you need to obtain the s-latch or x-latch of the page.

Hold the latch of the page until modification or access to the page is completed

1.2Write-Ahead Log

Write-Ahead Log means that if a page operation is written to a persistent device, the corresponding logs in the memory must be written to the persistent device. Each page has an LSN. Each page modification requires the LSN to be maintained. When a page needs to be written to a persistent device, logs smaller than the LSN of the page in the memory must be written to the persistent device first. After writing the log, fix the latch of the page and then fl the pages in the memory. After flushing the disk, release the page latch. The FIX Rules protocol is followed here.

1.3 Force-log-at-commit

A transaction can modify multiple pages at the same time. The consistency of a single data page in Write-AheadLog cannot guarantee the transaction persistence. Force-log-at-commit requires that when a transaction is committed, all the mini-transaction logs generated by the transaction must be flushed to the persistent device. In this way, even if the page data disk goes down, the redo can be restored through logs.

2 mini-transaction log implementation

Innodb uses mini-transaction to construct physical logic logs of operations. During transaction execution, mtr is used to ensure data consistency and durability of pages. Mini-transaction is to implement three mini-transaction protocols through a mtr_t structure. Mtr_t is defined as follows:

Typedef struct mtr_struct {ulint state;/* mtr status, MTR_ACTIVE, MTR_COMMITING, MTR_COMMITTED */dyn_array_t memo;/* latch list in progress */dyn_array_t log; /* log data generated by mtr */ibool modifications;/* Whether the page */ulint n_log_recs has been modified;/* Number of log operation pages */ulint log_mode; /* log operation mode: MTR_LOG_ALL, MTR_LOG_NONE, random */dulint start_lsn;/* LSN starting from mtr */dulint end_lsn;/* LSN ending from mtr */ulint magic_n; /* Magic word */} mtr_t;

Among them, memo is an array list of latch holding States, which is saved using the dynamic memory structure of dyn_array_t. Each unit stores a structure such as mtr_memo_slot_t. Definition:

Typedef struct mtr_memo_slot_struct {ulint type;/* latch type value */void * object;/* latch object handle, which can be rw_lock_t or buf_block_t */} mtr_memo_slot_t;

The latch type is 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's latch Management Interface
Mtr_memo_push gets a latch and stores the status information in mtr memo.
Mtr_release_s_latch_at_savepoint releases the slot lock status of the memo offset savepoint.
Mtr_memo_contains determines whether the lock object is in memo
Mtr_memo_slot_release releases control of the slot lock
Mtr_memo_pop_all releases control of all memo locks

The log member in mt_t is also a memory in the dynamic structure of dyn_array_t, used to save the log information generated by mtr. Logs are written through mtr0log. h. Here, we refer to the log format, which consists of a log header and a log body. The log header information is composed of type, space, and page no, the mlog_write_initial_log_record_fast function is written to the log of mtr_t. The following is a specific example:

The data written to the log body is written through the log Writing Method in mtr0log. h. For each write hop operation log, n_log_recs will add 1.

The identifier modifications indicates whether the page data has been changed. If yes, the mtr-> log is flushed to the disk when mtr_commit is called, and all the control permissions of the mtr are released. Logs will be flushed at the end of mtr, which complies with the Force-log-at-commit rules. Log writing calls the log_write_low function.

2.1 mtr_t memory structure diagram

3. Summary: mini transaction is the minimum guarantee unit for ACID durability in innodb. The construction and execution of mini transaction are required for transaction execution, page data flushing, and redo log data recovery. Almost all modules involve mini transaction, such as btree, page, transaction, inser tbuffer, and redo-log. d's understanding of mini transcaion cannot be isolated from the source code, the redo log and page-related code should be used to understand. It is the cornerstone of understanding how innodb works.


 

 

 

 



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.