zoukankan      html  css  js  c++  java
  • 浅谈mysql的两阶段提交协议

    转自:

    http://www.cnblogs.com/hustcat/p/3577584.html

    http://www.cnblogs.com/cchust/p/4439107.html

     

     

    binlog组提交的基本思想是,引入队列机制保证innodb commit顺序与binlog落盘顺序一致,并将事务分组,组内的binlog刷盘动作交给一个事务进行,实现组提交目的。binlog提交将提交分为了3个阶段,FLUSH阶段,SYNC阶段和COMMIT阶段。每个阶段都有一个队列,每个队列有一个mutex保护,约定进入队列第一个线程为leader,其他线程为follower,所有事情交由leader去做,leader做完所有动作后,通知follower刷盘结束。binlog组提交基本流程如下:

    FLUSH 阶段

    1)      持有Lock_log mutex [leader持有,follower等待]

    2)      获取队列中的一组binlog(队列中的所有事务)

    3)      将binlog buffer到I/O cache

    4)      通知dump线程dump binlog

    SYNC阶段

    1)      释放Lock_log mutex,持有Lock_sync mutex[leader持有,follower等待]

    2)      将一组binlog 落盘(sync动作,最耗时,假设sync_binlog为1)

    COMMIT阶段

    1)      释放Lock_sync mutex,持有Lock_commit mutex[leader持有,follower等待]

    2)      遍历队列中的事务,逐一进行innodb commit

    3)      释放Lock_commit mutex

    4)      唤醒队列中等待的线程

    说明:由于有多个队列,每个队列各自有mutex保护,队列之间是顺序的,约定进入队列的一个线程为leader,因此FLUSH阶段的leader可能是SYNC阶段的follower,但是follower永远是follower。

          通过上文分析,我们知道MYSQL目前的组提交方式解决了一致性和性能的问题。通过二阶段提交解决一致性,通过redo log和binlog的组提交解决磁盘IO的性能。下面我整理了Prepare阶段和Commit阶段的框架图供各位参考。

     

    5.3.1事务提交流程

    MySQL的事务提交逻辑主要在函数ha_commit_trans中完成。事务的提交涉及到binlog及具体的存储的引擎的事务提交。所以MySQL用2PC来保证的事务的完整性。MySQL的2PC过程如下:

    clip_image002

    (1)先调用binglog_hton和innobase_hton的prepare方法完成第一阶段,binlog_hton的papare方法实际上什么也没做,innodb的prepare将事务状态设为TRX_PREPARED,并将redo log刷磁盘 (innobase_xa_prepare à trx_prepare_for_mysql à trx_prepare_off_kernel)。

    (2)如果事务涉及的所有存储引擎的prepare都执行成功,则调用TC_LOG_BINLOG::log_xid将SQL语句写到binlog,此时,事务已经铁定要提交了。否则,调用ha_rollback_trans回滚事务,而SQL语句实际上也不会写到binlog。

    (3)最后,调用引擎的commit完成事务的提交。实际上binlog_hton->commit什么也不会做(因为(2)已经将binlog写入磁盘),innobase_hton->commit则清除undo信息,刷redo日志,将事务设为TRX_NOT_STARTED状态(innobase_commit à innobase_commit_low à trx_commit_for_mysql à trx_commit_off_kernel)。

    //ha_innodb.cc

    static

    int

    innobase_commit(

    /*============*/

    /* out: 0 */

    THD* thd, /* in: MySQL thread handle of the user for whom

    the transaction should be committed */

    bool all) /* in: TRUE - commit transaction

    FALSE - the current SQL statement ended */

    {

    ...

    trx->mysql_log_file_name = mysql_bin_log.get_log_fname();

    trx->mysql_log_offset =

    (ib_longlong)mysql_bin_log.get_log_file()->pos_in_file;

    ...

    }

    函数innobase_commit提交事务,先得到当前的binlog的位置,然后再写入事务系统PAGE(trx_commit_off_kernel à trx_sys_update_mysql_binlog_offset)。

    InnoDB将MySQL binlog的位置记录到trx system header中:

    //trx0sys.h

    /* The offset of the MySQL binlog offset info in the trx system header */

    #define TRX_SYS_MYSQL_LOG_INFO (UNIV_PAGE_SIZE - 1000)

    #define TRX_SYS_MYSQL_LOG_MAGIC_N_FLD 0 /* magic number which shows

    if we have valid data in the

    MySQL binlog info; the value

    is ..._MAGIC_N if yes */

    #define TRX_SYS_MYSQL_LOG_OFFSET_HIGH 4 /* high 4 bytes of the offset

    within that file */

    #define TRX_SYS_MYSQL_LOG_OFFSET_LOW 8 /* low 4 bytes of the offset

    within that file */

    #define TRX_SYS_MYSQL_LOG_NAME 12 /* MySQL log file name */

    5.3.2 事务恢复流程

    Innodb在恢复的时候,不同状态的事务,会进行不同的处理(见trx_rollback_or_clean_all_without_sess函数):

    <1>对于TRX_COMMITTED_IN_MEMORY的事务,清除回滚段,然后将事务设为TRX_NOT_STARTED;

    <2>对于TRX_NOT_STARTED的事务,表示事务已经提交,跳过;

    <3>对于TRX_PREPARED的事务,要根据binlog来决定事务的命运,暂时跳过;

    <4>对于TRX_ACTIVE的事务,回滚。

    MySQL在打开binlog时,会检查binlog的状态(TC_LOG_BINLOG::open)。如果binlog没有正常关闭(LOG_EVENT_BINLOG_IN_USE_F为1),则进行恢复操作,基本流程如下:

    clip_image004

    <1>扫描binlog,读取XID_EVENT事务,得到所有已经提交的XA事务列表(实际上事务在innodb可能处于prepare或者commit);

    <2>对每个XA事务,调用handlerton::recover,检查存储引擎是否存在处于prepare状态的该事务(见innobase_xa_recover),也就是检查该XA事务在存储引擎中的状态;

    <3>如果存在处于prepare状态的该XA事务,则调用handlerton::commit_by_xid提交事务;

    <4>否则,调用handlerton::rollback_by_xid回滚XA事务。

    5.3.3 几个参数讨论

    (1)sync_binlog

    Mysql在提交事务时调用MYSQL_LOG::write完成写binlog,并根据sync_binlog决定是否进行刷盘。默认值是0,即不刷盘,从而把控制权让给OS。如果设为1,则每次提交事务,就会进行一次刷盘;这对性能有影响(5.6已经支持binlog group),所以很多人将其设置为100。

    bool MYSQL_LOG::flush_and_sync()

    {

    int err=0, fd=log_file.file;

    safe_mutex_assert_owner(&LOCK_log);

    if (flush_io_cache(&log_file))

    return 1;

    if (++sync_binlog_counter >= sync_binlog_period && sync_binlog_period)

    {

    sync_binlog_counter= 0;

    err=my_sync(fd, MYF(MY_WME));

    }

    return err;

    }

    (2) innodb_flush_log_at_trx_commit

    该参数控制innodb在提交事务时刷redo log的行为。默认值为1,即每次提交事务,都进行刷盘操作。为了降低对性能的影响,在很多生产环境设置为2,甚至0。

    clip_image006

    If the value of innodb_flush_log_at_trx_commit is 0, the log buffer is written out to the log file once per second and the flush to disk operation is performed on the log file, but nothing is done at a transaction commit. When the value is 1 (the default), the log buffer is written out to the log file at each transaction commit and the flush to disk operation is performed on the log file. When the value is 2, the log buffer is written out to the file at each commit, but the flush to disk operation is not performed on it. However, the flushing on the log file takes place once per second also when the value is 2. Note that the once-per-second flushing is not 100% guaranteed to happen every second, due to process scheduling issues.

    The default value of 1 is required for full ACID compliance. You can achieve better performance by setting the value different from 1, but then you can lose up to one second worth of transactions in a crash. With a value of 0, any mysqld process crash can erase the last second of transactions. With a value of 2, only an operating system crash or a power outage can erase the last second of transactions.

    (3) innodb_support_xa

    用于控制innodb是否支持XA事务的2PC,默认是TRUE。如果关闭,则innodb在prepare阶段就什么也不做;这可能会导致binlog的顺序与innodb提交的顺序不一致(比如A事务比B事务先写binlog,但是在innodb内部却可能A事务比B事务后提交),这会导致在恢复或者slave产生不同的数据。

    int

    innobase_xa_prepare(

    /*================*/

    /* out: 0 or error number */

    THD* thd, /* in: handle to the MySQL thread of the user

    whose XA transaction should be prepared */

    bool all) /* in: TRUE - commit transaction

    FALSE - the current SQL statement ended */

    {

    if (!thd->variables.innodb_support_xa) {

    return(0);

    }

    5.3.4 安全性/性能讨论

    上面3个参数不同的值会带来不同的效果。三者都设置为1(TRUE),数据才能真正安全。sync_binlog非1,可能导致binlog丢失(OS挂掉),从而与innodb层面的数据不一致。innodb_flush_log_at_trx_commit非1,可能会导致innodb层面的数据丢失(OS挂掉),从而与binlog不一致。

    关于性能分析,可以参考

    http://www.mysqlperformanceblog.com/2011/03/02/what-is-innodb_support_xa/

    http://www.mysqlperformanceblog.com/2009/01/21/beware-ext3-and-sync-binlog-do-not-play-well-together/

  • 相关阅读:
    FLEX布局做响应式页面
    vscode 设置指南
    js原生事件委托的实现
    fiddler相关功能和命令
    JavaScript常见问题
    Nodejs命令学习
    ES6和babel的恩怨纠葛
    js模块编程
    Mac-Python 从2.7版本升级到3.7版本
    Java-Mac版Idea安装TestNG框架
  • 原文地址:https://www.cnblogs.com/yuyue2014/p/6121114.html
Copyright © 2011-2022 走看看