http://blog.itpub.net/15480802/viewspace-1411356/

Redo组提交

Redo提交流程大致如下

lock log->mutex

write redo log buffer to disk

unlock log->mutex

fsync

Fsync写磁盘耗时较长且不占用log->mutex,也就是其执行期间其他线程可以write log buffer;

假定一次fsync需要10ms,而写buffer只需要1ms,则fsync执行期间最多可以有10条redo record写入buffer,则下次调用fsync时可一次性写10条记录;

binlog组提交

Mysql 从 5.0 开始支持2PC,在代码实现时为了保证Binlog中的事务顺序和事务commit顺序一致,放弃了Group Commit。

如果Binlog顺序不一致,那么备库就无法确保和主库有一致的数据。这个问题直到 mysql 5.5 才开始部分修复,到 mysql 5.6 完全修复。

在 mysql 5.5 中,只有当 sync_binlog = 0 时,才能使用 group commit,在 mysql 5.6中都可以进行 group commit。

2PC下的事务提交流程

1. Prepare Innodb:

a) Write prepare record to Innodb's log buffer

b) Sync log file to disk  -- redo组提交

c) Take prepare_commit_mutex

2. "Prepare" binary log:

a) Write transaction to binary log

b) Sync binary log based on sync_binlog

3. Commit Innodb:

a) Write commit record to log

b) Release prepare_commit_mutex

c) Sync log file to disk

d) Innodb locks are released

4. "Commit" binary log:

a) Nothing necessary to do here.

不足

为保证binlog按顺序写,prepare redo阶段获取prepare_commit_mutex,直到sync redo前才释放;一次只能有一个事务可获取该mutex,阻碍了group commit;

另外,一次完整的事务提交需要调用3次fsync,效率很低;

改进

1 减少fsync

crash recovery时先查看redo log,找出prepared但没有commited或aborted的事务列表,然后检查binlog,如果binlog记录了该事务,则将其commit否则rollback;

由此可见,事务恢复时取决于binlog是否有记录,因此commit innodb时无须调用立即fsync(此时binlog已写入,就算crash也能保证事务提交);

2 细化binlog commit代码,实现组提交

在Oracle MySQL 5.6.15中,binlog group commit模块被重写,这个过程分为3个stage:flush/sync/commit;

5.6的2PC提交流程如下

1. Ask binary log (i.e. coordinator to prepare

a) Request to release locks earlier

b) Prepare Innodb (Callback to (2.a))

2. Prepare Innodb:

a) Write prepare record to Innodb log buffer

b) Sync log file to disk

3. Ask binary log (i.e. coordinator) to commit

a) Lock access to flush stage

b) Write a set of transactions to the binary log

c) Unlock access to flush stage

d) Lock access to sync stage

e) Flush the binary log to disk

f) Unlock access to sync stage

g) Lock access to commit stage

h) Commit Innodb (Callback to (4.a))

i) Unlock access to commit stage

4. Commit Innodb

a) Write a commit record to Innodb log buffer

binlog提交被细化为3个处理阶段,每一阶段都有lock保护(此时redo已经调用fsync,事务尚未提交);

这3个阶段负责批量读取binlog并调用fsync,而后以同样顺序提交事务(可选);

第一个进入处理阶段的事务担当Leader的角色,剩余的为follower,后者释放所有的latch并等待,直至leader完成commit;

leader获取所有排队等待的事务并处理,进入下一个处理阶段时,如果队列为空则仍是leader,否则降级为follower;

1. Flush Stage

leader会不断读取flush queue直到队列为空或者超时,这样允许处理过程中新加入的事务也能得到及时处理;

leader将排队的事务写入binlog buffer,当队列为空时则进入下一阶段;

超时机制避免了事务长时间等待,

2. Sync Stage

调用fsyc,一次刷新多个事务;

3. Commit Stage

提交事务,保证所有事务提交顺序同写入binlog一致(innodb hot backup); 为了提升性能,也可选择不按次序提交;

代码实现

Binlog原本实现了handlerton接口,包括commit()/rollback()等方法,5.6引入新机制

public class MYSQL_BIN_LOG: public TC_LOG

{

int open_connection(THD* thd);

int close_connection(THD* thd);

int commit(THD *thd, bool all);

int rollback(THD *thd, bool all);

int savepoint_set(THD* thd, SAVEPOINT *sv);

int savepoint_release(THD* thd, SAVEPOINT *sv);

int savepoint_rollback(THD* thd, SAVEPOINT *sv);

};

int MYSQL_BIN_LOG::commit(THD *thd, bool all)

{

/* Call batch_commit(). */

}

int MYSQL_BIN_LOG::batch_commit(THD* thd, bool all)

{

--将事务加入flush queue,第一个事务为leader,follower阻塞直至完成commit;

if (change_stage(thd, Stage_manager::FLUSH_STAGE, thd, NULL, &LOCK_log))

return finish_commit(thd->commit_error);

--将事务写入binlog

THD *flush_queue= NULL; /* Gets a pointer to the flush_queue */

error= flush_stage_queue(&wait_queue);

if (change_stage(thd, Stage_manager::SYNC_STAGE, wait_queue, &LOCK_log, &LOCK_sync))

return finish_commit(thd->commit_error);

--依据sync_binlog选项调用fsync,5.5却只能将sync_binlog=0

THD *sync_queue= NULL; /* Gets a pointer to the sync_queue */

error= sync_stage_queue(&sync_queue);

--根据opt_binlog_order_commits,可以按binlog写入顺序提交事务,也可以让线程调用handlerton->commit各自提交;

if (opt_binlog_order_commits)

{

if (change_stage(thd, Stage_manager::COMMIT_STAGE,

final_queue, &LOCK_sync, &LOCK_commit))

return finish_commit(thd);

THD *commit_queue= NULL;

error= commit_stage_queue(&commit_queue);

mysql_mutex_unlock(&LOCK_commit);

final_queue= commit_queue;

}

else

{

final_queue= sync_queue;

mysql_mutex_unlock(&LOCK_sync);

}

--通知follower,要么提交事务(opt_binlog_order_commits=false)要么通知客户端;

stage_manager.signal_done(final_queue);

return finish_commit(thd);

}

参考资料

http://dev.mysql.com/worklog/task/?id=5223

http://mysqlmusings.blogspot.co.uk/2012/06/binary-log-group-commit-in-mysql-56.html?_sm_au_=iDV88W54k66P05L7

后记:
看到淘宝分享的一篇帖子,在5.6的基础上还有优化的空间,要保证一个事务能成功恢复,只需要保证在binlog commit前将对应事务的redo
entry写入磁盘即可,则redo commit/sync完全可以从redo prepare后移到binlog
prepare,将其放于flush stage和commit
stage之间,将原本N次的log_sys->mutex获取次数降为1次,fsync也变为1次;
问题
每个事务都要保证其Prepare的事务被write/fsync到redo log文件。尽管某个事务可能会帮助其他事务完成redo 写入,但这种行为是随机的,并且依然会产生明显的log_sys->mutex开销。
优化
从XA恢复的逻辑我们可以知道,只要保证InnoDB Prepare的redo日志在写Binlog前完成write/sync即可。因此我们对Group Commit的第一个stage的逻辑做了些许修改,大概描述如下:
Step1. InnoDB Prepare,记录当前的LSN到thd中; 
注:原本此阶段需要获取log->mutex进行的写文件取消,延迟到下一阶段;在原有fsync组提交的基础上实现写文件组提交。
Step2. 进入Group Commit的flush stage;Leader搜集队列,同时算出队列中最大的LSN。
Step3. 将InnoDB的redo log write/fsync到指定的LSN 
Step4. 写Binlog并进行随后的工作(sync Binlog, InnoDB commit , etc)
通过延迟写redo log的方式,显式的为redo log做了一次组写入,并减少了log_sys->mutex的竞争。
目前官方MySQL已经根据我们report的bug#73202锁提供的思路,对5.7.6的代码进行了优化,对应的Release Note如下:
When using InnoDB with binary logging enabled, concurrent transactions
written in the InnoDB redo log are now grouped together before
synchronizing to disk when innodb_flush_log_at_trx_commit is set to 1,
which reduces the amount of synchronization operations. This can lead to
improved performance.
简单测试了下,使用sysbench, update_non_index.lua, 100张表,每张10w行记录,innodb_flush_log_at_trx_commit=2, sync_binlog=1000,关闭Gtid
并发线程        原生                  修改后
32             25600                27000
64             30000                35000
128            33000                39000
256            29800                38000

mysql 5.6 binlog组提交实现原理(转载)的更多相关文章

  1. mysql 5.6 binlog组提交

    mysql 5.6 binlog组提交实现原理 http://blog.itpub.net/15480802/viewspace-1411356 Redo组提交 Redo提交流程大致如下 lock l ...

  2. mysql 5.6 binlog组提交1

    [MySQL 5.6] MySQL 5.6 group commit 性能测试及内部实现流程   尽管Mariadb以及Facebook在long long time ago就fix掉了这个臭名昭著的 ...

  3. mysql复制那点事(2)-binlog组提交源码分析和实现

    mysql复制那点事(2)-binlog组提交源码分析和实现 [TOC] 0. 参考文献 序号 文献 1 MySQL 5.7 MTS源码分析 2 MySQL 组提交 3 MySQL Redo/Binl ...

  4. MySQL binlog 组提交与 XA(两阶段提交)

    1. XA-2PC (two phase commit, 两阶段提交 ) XA是由X/Open组织提出的分布式事务的规范(X代表transaction; A代表accordant?).XA规范主要定义 ...

  5. MySQL binlog 组提交与 XA(两阶段提交)--1

    参考了网上几篇比较靠谱的文章 http://www.linuxidc.com/Linux/2015-11/124942.htm http://blog.csdn.net/woqutechteam/ar ...

  6. MySQL binlog 组提交与 XA(分布式事务、两阶段提交)【转】

    概念: XA(分布式事务)规范主要定义了(全局)事务管理器(TM: Transaction Manager)和(局部)资源管理器(RM: Resource Manager)之间的接口.XA为了实现分布 ...

  7. MySQL崩溃恢复与组提交

      Ⅰ.binlog与redo的一致性(原子) 由内部分布式事务保证 我们先来了解下,当一个commit敲下后,内部会发生什么? 步骤 操作 step1 InnoDB做prepare redo log ...

  8. mysql并发复制系列 一:binlog组提交

    http://blog.itpub.net/28218939/viewspace-1975809/ 作者:沃趣科技MySQL数据库工程师  麻鹏飞 MySQL  Binary log在MySQL 5. ...

  9. MySQL并发复制系列一:binlog组提交 (转载)

    http://blog.csdn.net/woqutechteam/article/details/51178803 MySQL  Binary log在MySQL 5.1版本后推出主要用于主备复制的 ...

随机推荐

  1. Python的方法和语法解释

    ---------------------------------------------------------------------------------------------------- ...

  2. LeetCode - Baseball Game

    You're now a baseball game point recorder. Given a list of strings, each string can be one of the 4 ...

  3. MongoDB内存配置 --wiredTigerCacheSizeGB

    用top命令查看系统占用内存的情况 top -p $(pidof mongod),发现mongod占用了8G内存的35.6%.在服务器上运行两个mongod进程,很容易导致mongod异常退出. 一度 ...

  4. gunicorn工作原理

    gunicorn工作原理 Gunicorn“绿色独角兽”是一个被广泛使用的高性能的Python WSGI UNIX HTTP服务器,移植自Ruby的独角兽(Unicorn )项目,使用pre-fork ...

  5. MySQL Lock--并发插入导致的死锁

    ============================================================================ 测试脚本: 表结构: CREATE TABLE ...

  6. Day 35数据库(Day1)

    创建表. create table student( id int not null auto_increment PRIMARY key, name archar(250) not null, ag ...

  7. 安装Redis的PHP扩展

    1.安装phpize(php如果升级到php7,这步会报错,报错参考:https://www.cnblogs.com/clubs/p/10091103.html) yum install php-de ...

  8. nginx下js文件修改后访问不更新问题解决

    今天遇到一个问题,nginx下js修改后不更新,加版本号,刷新浏览器缓存都不行,重启服务器才行,修改后又不更新了而且加载的js文件会有乱码或者文件加载不全的问题. 解决办法:修改nginx.conf, ...

  9. iis上的aps.net1.1程序池如何添加

    http://www.jb51.net/article/84668.htm iis上的aps.net1.1 的程序池是默认有的,如果不小心将其删掉,或者改成其他版本,将没办法在iis工具上还原或新建一 ...

  10. javaweb下载中的一个问题

    如果你发现,response头以及contentType都已经设置没错,但出现浏览器将下载的文件内容显示到新窗口 那么解决方案就是在请求的时候不要产生新的窗口