innodb_flush_log_at_trx_commit 三种取值 0 、1、2 对性能有很大影响,现在从源码瞧瞧是如何实现定义。涉及的文件以及函数调用如下:
innobase/srv/srv0srv.c中srv_master_thread()
innodbase/trx/trx0trx.c 中 trx_commit_off_kernel() ( 查看log_write_up_to()函数调用)
关键代码
/* Depending on the my.cnf options, we may now write the log buffer to the log files, making the transaction durable if the OS does not crash. We may also flush the log files to disk, making the transaction durable also at an OS crash or a power outage. The idea in InnoDB's group commit is that a group of transactions gather behind a trx doing a physical disk write to log files, and when that physical write has been completed, one of those transactions does a write which commits the whole group. Note that this group commit will only bring benefit if there are > 2 users in the database. Then at least 2 users can gather behind one doing the physical log write to disk. If we are calling trx_commit() under PRepare_commit_mutex, we will delay possible log write and flush to a separate function trx_commit_complete_for_MySQL(), which is only called when the thread has released the mutex. This is to make the group commit algorithm to work. Otherwise, the prepare_commit mutex would serialize all commits and prevent a group of transactions from gathering. */ if (trx->flush_log_later) { /* Do nothing yet */ trx->must_flush_log_later = TRUE; } else if (srv_flush_log_at_trx_commit == 0) { /* Do nothing */ } else if (srv_flush_log_at_trx_commit == 1) { if (srv_unix_file_flush_method == SRV_UNIX_NOSYNC) { /* Write the log but do not flush it to disk */ log_write_up_to(lsn, LOG_WAIT_ONE_GROUP, FALSE); } else { /* Write the log to the log files AND flush them to disk */ log_write_up_to(lsn, LOG_WAIT_ONE_GROUP, TRUE); } } else if (srv_flush_log_at_trx_commit == 2) { /* Write the log but do not flush it to disk */ log_write_up_to(lsn, LOG_WAIT_ONE_GROUP, FALSE); } else { ut_error; } trx->commit_lsn = lsn; log_write_up_to定义在innobase/include/log0log.h中
/******************************************************//**This function is called, e.g., when a transaction wants to commit. It checksthat the log has been written to the log file up to the last log entry writtenby the transaction. If there is a flush running, it waits and checks if theflush flushed enough. If not, starts a new flush. */UNIV_INTERNvoidlog_write_up_to(/*============*/ ib_uint64_t lsn, /*!< in: log sequence number up to which the log should be written, IB_ULONGLONG_MAX if not specified */ ulint wait, /*!< in: LOG_NO_WAIT, LOG_WAIT_ONE_GROUP, or LOG_WAIT_ALL_GROUPS */ ibool flush_to_disk); /*!< in: TRUE if we want the written log also to be flushed to disk *//****************************************************************//**
wait 参数三种变量值:
log_no_wait:不等待
log_wait_one_group:等待刷新到一个日志组
log_wait_all_group:等待刷新到所有日志组
实现 innobase/log/log0log.c中
新闻热点
疑难解答