mysql中双1控制源码分析
1、innodb_flush_log_at_trx_commit参数讨论
参数含义:该参数主要是是来控制redo log的刷盘策略,有源码的调用栈可知,该参数生效的地方如下:
#0 log_buffer_flush_to_disk (sync=true) at /mysql/zxz/mysqlsourcetest/mysql-5.7.21/storage/innobase/log/log0log.cc:1419
#1 0x00000000019b1999 in innobase_flush_logs (hton=0x2e34060, binlog_group_flush=true) at /mysql/zxz/mysqlsourcetest/mysql-5.7.21/storage/innobase/handler/ha_innodb.cc:4214
#2 0x0000000000f25af3 in flush_handlerton (thd=0x0, plugin=0x7fffec12a128, arg=0x7fffec12a1c4) at /mysql/zxz/mysqlsourcetest/mysql-5.7.21/sql/handler.cc:2471
#3 0x000000000155fccf in plugin_foreach_with_mask (thd=0x0, funcs=0x7fffec12a1a0, type=1, state_mask=4294967287, arg=0x7fffec12a1c4) at /mysql/zxz/mysqlsourcetest/mysql-5.7.21/sql/sql_plugin.cc:2426
#4 0x000000000155fd8f in plugin_foreach_with_mask (thd=0x0, func=0xf25a95 <flush_handlerton(THD*, plugin_ref, void*)>, type=1, state_mask=8, arg=0x7fffec12a1c4) at /mysql/zxz/mysqlsourcetest/mysql-5.7.21/sql/sql_plugin.cc:2441
#5 0x0000000000f25b4f in ha_flush_logs (db_type=0x0, binlog_group_flush=true) at /mysql/zxz/mysqlsourcetest/mysql-5.7.21/sql/handler.cc:2481
#6 0x00000000017e1e07 in MYSQL_BIN_LOG::process_flush_stage_queue (this=0x2d850e0 <mysql_bin_log>, total_bytes_var=0x7fffec12a2e8, rotate_var=0x7fffec12a2e7, out_queue_var=0x7fffec12a2d8)
at /mysql/zxz/mysqlsourcetest/mysql-5.7.21/sql/binlog.cc:8652
#7 0x00000000017e33c2 in MYSQL_BIN_LOG::ordered_commit (this=0x2d850e0 <mysql_bin_log>, thd=0x7fff30000b70, all=false, skip_commit=false) at /mysql/zxz/mysqlsourcetest/mysql-5.7.21/sql/binlog.cc:9304
#8 0x00000000017e1ab8 in MYSQL_BIN_LOG::commit (this=0x2d850e0 <mysql_bin_log>, thd=0x7fff30000b70, all=false) at /mysql/zxz/mysqlsourcetest/mysql-5.7.21/sql/binlog.cc:8552
#9 0x0000000000f245e9 in ha_commit_trans (thd=0x7fff30000b70, all=false, ignore_global_read_lock=false) at /mysql/zxz/mysqlsourcetest/mysql-5.7.21/sql/handler.cc:1796
#10 0x0000000001634116 in trans_commit_stmt (thd=0x7fff30000b70) at /mysql/zxz/mysqlsourcetest/mysql-5.7.21/sql/transaction.cc:458
#11 0x0000000001535908 in mysql_execute_command (thd=0x7fff30000b70, first_level=true) at /mysql/zxz/mysqlsourcetest/mysql-5.7.21/sql/sql_parse.cc:5004
#12 0x0000000001536f3f in mysql_parse (thd=0x7fff30000b70, parser_state=0x7fffec12c690) at /mysql/zxz/mysqlsourcetest/mysql-5.7.21/sql/sql_parse.cc:5582
#13 0x000000000152c7ca in dispatch_command (thd=0x7fff30000b70, com_data=0x7fffec12cdf0, command=COM_QUERY) at /mysql/zxz/mysqlsourcetest/mysql-5.7.21/sql/sql_parse.cc:1458
#14 0x000000000152b6fe in do_command (thd=0x7fff30000b70) at /mysql/zxz/mysqlsourcetest/mysql-5.7.21/sql/sql_parse.cc:999
#15 0x0000000001663c68 in handle_connection (arg=0x6d99650) at /mysql/zxz/mysqlsourcetest/mysql-5.7.21/sql/conn_handler/connection_handler_per_thread.cc:303
#16 0x00000000018b6cb0 in pfs_spawn_thread (arg=0x6dd6e00) at /mysql/zxz/mysqlsourcetest/mysql-5.7.21/storage/perfschema/pfs.cc:2190
#17 0x00007ffff7bc6dd5 in start_thread () from /lib64/libpthread.so.0
#18 0x00007ffff6817ead in clone () from /lib64/libc.so.6
在 innobase_flush_log函数中innodb_flush_log_at_trx_commit有如下的组合策略y
/* If !binlog_group_flush, we got invoked by FLUSH LOGS or similar.
Else, we got invoked by binlog group commit during flush stage. */
if (binlog_group_flush && srv_flush_log_at_trx_commit == 0) {
/* innodb_flush_log_at_trx_commit=0
(write and sync once per second).
Do not flush the redo log during binlog group commit. */
DBUG_RETURN(false);
}
/* Flush the redo log buffer to the redo log file.
Sync it to disc if we are in FLUSH LOGS, or if
innodb_flush_log_at_trx_commit=1
(write and sync at each commit). */
log_buffer_flush_to_disk(!binlog_group_flush
|| srv_flush_log_at_trx_commit == 1);
由于binlog_group_flush值始终未为true,因此有以下三种刷盘策略:
A、innodb_flush_log_at_trx_commi为0
log_buffer_flush_to_disk函数不会被调用,因此不会做redo刷盘,更不会做sync,依赖于master线程
B、innodb_flush_log_at_trx_commi为1
会调用log_buffer_flush_to_disk(True),需要在事物每次提交时做redo刷盘,也需要做sync
C、innodb_flush_log_at_trx_commi为2
会调用log_buffer_flush_to_disk(False),因此需要做redo刷盘,不做sync,依赖os的刷盘机制;
2、sync_binlog参数讨论
参数含义:该参数主要用来控制binlog的刷盘机制
1、sync_binlog!=1
影响1:会在flush阶段结束后,唤醒dump线程发送binlog
2、sync_binlog=1或者sync_binlog=0
影响1:会进入stage_manager.wait_count_or_timeout的逻辑,该函数主要由两个参数来进行控制binlog_group_commit_sync_delay和binlog_group_commit_sync_no_delay_count来决定是否需要等待足够数量的组提交后进行提交;
影响2:根据binlog的设置来决定是否进行刷盘;依据的条件是(sync_period && ++sync_counter >= sync_period);
影响3:如果binlog=1,这里将会唤醒dump线程进行event的发送