事务组提交和多线程复制

在MySQL 5.7版本引入基于LOGICAL_CLOCK的多线程复制,依赖于BINLOG事件中的last_committed属性,该last_committed属性是否与事务组提交特性有关呢?

测试环境:

MySQL 版本: MySQL 5.7.19
MySQL 参数:
binlog_format=STATEMENT
binlog_group_commit_sync_delay=0
binlog_group_commit_sync_no_delay_count=0

虽然参数binlog_group_commit_sync_no_delay_count=0,但不代表关闭事务组提交特性。

测试示例:

1、在测试库上依次执行事务1-->事务2-->事务3,但均不提交,三个事务中的SQL不存在冲突,都可以正常执行。

## 事务1
BEGIN;
UPDATE tb002
SET c2=
WHERE c1=; ## 事务2
BEGIN;
UPDATE tb002
SET c2=
WHERE c1=; ## 事务3
BEGIN;
UPDATE tb002
SET c2=
WHERE c1=;

2、按照事务3-->事务1-->事务2的顺序提交事务。

3、解析binlog文件

/export/servers/mysql/bin/mysqlbinlog -vv /export/data/mysql/data/mysql-bin.000013

# at 21216
#190701 23:07:55 server id 1614520 end_log_pos 21281 CRC32 0xe0b32069 GTID last_committed=69 sequence_number=70 rbr_only=no
SET @@SESSION.GTID_NEXT= '025fd638-89ea-11e9-a749-40f2e9cf3aaa:133'/*!*/;
# at 21281
#190701 23:07:55 server id 1614520 end_log_pos 21362 CRC32 0xf970363b Query thread_id=13946 exec_time=0 error_code=0
SET TIMESTAMP=1561993675/*!*/;
BEGIN
/*!*/;
# at 21362
# at 21394
#190701 23:07:55 server id 1614520 end_log_pos 21394 CRC32 0x898abcdd Intvar
SET INSERT_ID=3/*!*/;
#190701 23:07:55 server id 1614520 end_log_pos 21513 CRC32 0xd4f17cf4 Query thread_id=13946 exec_time=0 error_code=0
SET TIMESTAMP=1561993675/*!*/;
insert into tb002(c2,c3) select 1430,''
/*!*/;
# at 21513
#190701 23:07:55 server id 1614520 end_log_pos 21544 CRC32 0xd04864cb Xid = 42219
COMMIT/*!*/;
# at 21544
#190708 15:58:58 server id 1614520 end_log_pos 21609 CRC32 0x6dce809a GTID last_committed=70 sequence_number=71 rbr_only=no
SET @@SESSION.GTID_NEXT= '025fd638-89ea-11e9-a749-40f2e9cf3aaa:134'/*!*/;
# at 21609
#190708 15:57:59 server id 1614520 end_log_pos 21690 CRC32 0x58c7f42e Query thread_id=13947 exec_time=0 error_code=0
SET TIMESTAMP=1562572679/*!*/;
BEGIN
/*!*/;
# at 21690
#190708 15:57:59 server id 1614520 end_log_pos 21798 CRC32 0x2c04a7a1 Query thread_id=13947 exec_time=0 error_code=0
SET TIMESTAMP=1562572679/*!*/;
UPDATE tb002 SET c2=1 WHERE c1=1
/*!*/;
# at 21798
#190708 15:58:58 server id 1614520 end_log_pos 21829 CRC32 0xa1d16066 Xid = 42244
COMMIT/*!*/;
# at 21829
#190708 16:00:11 server id 1614520 end_log_pos 21894 CRC32 0xc3247c44 GTID last_committed=71 sequence_number=72 rbr_only=no
SET @@SESSION.GTID_NEXT= '025fd638-89ea-11e9-a749-40f2e9cf3aaa:135'/*!*/;
# at 21894
#190708 16:00:05 server id 1614520 end_log_pos 21975 CRC32 0x497a029d Query thread_id=13949 exec_time=0 error_code=0
SET TIMESTAMP=1562572805/*!*/;
BEGIN
/*!*/;
# at 21975
#190708 16:00:05 server id 1614520 end_log_pos 22084 CRC32 0x1ad87709 Query thread_id=13949 exec_time=0 error_code=0
SET TIMESTAMP=1562572805/*!*/;
UPDATE tb002 SET c2=32 WHERE c1=3
/*!*/;
# at 22084
#190708 16:00:11 server id 1614520 end_log_pos 22115 CRC32 0x5dfd7cf0 Xid = 42266
COMMIT/*!*/;
# at 22115
#190708 16:00:18 server id 1614520 end_log_pos 22180 CRC32 0x6291b7e7 GTID last_committed=71 sequence_number=73 rbr_only=no
SET @@SESSION.GTID_NEXT= '025fd638-89ea-11e9-a749-40f2e9cf3aaa:136'/*!*/;
# at 22180
#190708 15:59:45 server id 1614520 end_log_pos 22261 CRC32 0x51f1f46a Query thread_id=13947 exec_time=0 error_code=0
SET TIMESTAMP=1562572785/*!*/;
BEGIN
/*!*/;
# at 22261
#190708 15:59:45 server id 1614520 end_log_pos 22370 CRC32 0x85218310 Query thread_id=13947 exec_time=0 error_code=0
SET TIMESTAMP=1562572785/*!*/;
UPDATE tb002 SET c2=11 WHERE c1=1
/*!*/;
# at 22370
#190708 16:00:18 server id 1614520 end_log_pos 22401 CRC32 0x895b75bf Xid = 42260
COMMIT/*!*/;
# at 22401
#190708 16:00:23 server id 1614520 end_log_pos 22466 CRC32 0x4bbabfe8 GTID last_committed=71 sequence_number=74 rbr_only=no
SET @@SESSION.GTID_NEXT= '025fd638-89ea-11e9-a749-40f2e9cf3aaa:137'/*!*/;
# at 22466
#190708 15:59:55 server id 1614520 end_log_pos 22547 CRC32 0x00e0c7c6 Query thread_id=13948 exec_time=0 error_code=0
SET TIMESTAMP=1562572795/*!*/;
BEGIN
/*!*/;
# at 22547
#190708 15:59:55 server id 1614520 end_log_pos 22656 CRC32 0xeb83bb3c Query thread_id=13948 exec_time=0 error_code=0
SET TIMESTAMP=1562572795/*!*/;
UPDATE tb002 SET c2=22 WHERE c1=2
/*!*/;
# at 22656
#190708 16:00:23 server id 1614520 end_log_pos 22687 CRC32 0x8240be81 Xid = 42263
COMMIT/*!*/;

通过上面的BINLOG可以发现,三个事务在三个不同时间点提交且提交时间相差数秒,不可能被放到同一个事务组中进行提交,三个事务提交时的系统last_committed为变化为:

但三个事务的BINLOG中的last_committed相同,都是这三个事务开始前的最后一个事务的sequence_number,因此可以证明BINLOG中的last_committed值取决于事务中最后一条语句执行完成的时间点,而与事务提交时间点无关。

当上面三个事务的BINLOG事件传递到从库执行时,从库SQL线程通过对比last_committed的值确认这三个事务的BINLOG事件可以并发执行,因此可以交给不同的线程进行处理。

总结:

在MySQL 5.7早期版本中,MySQL多线程复制依赖于MySQL事务组提交特性,但在后续版本中进行优化,在BINLOG中记录事务中最后一条语句执行完成时的系统last_committed,由于该语句已经完成(未被阻塞),因此拥有相同系统last_committed值的事务不会存在冲突,即使这些事务在后续不同时间点进行提交,但在从库上仍可以并行执行。

MySQL Replication--事务组提交和多线程复制的更多相关文章

  1. MySQL 5.7基于组提交的并行复制

    参考链接: http://mysql.taobao.org/monthly/2016/08/01/ https://www.kancloud.cn/thinkphp/mysql-parallel-ap ...

  2. MySQL5.7的组提交与并行复制

    从MySQL5.5版本以后,开始引入并行复制的机制,是MySQL的一个非常重要的特性. MySQL5.6开始支持以schema为维度的并行复制,即如果binlog row event操作的是不同的sc ...

  3. MySql数据库事务正常提交,回滚失败

    问题:在初次练习Mysql数据库事务时,事务正常提交,但是在遇到异常应当回滚时,回滚失败. 代码如下: //2.更新操作. public void update(Connection conn, St ...

  4. 【MySQL】事务没有提交导致 锁等待Lock wait timeout exceeded异常

    异常:Lock wait timeout exceeded; try restarting transaction 解决办法:(需要数据库最高权限) 执行select * from informati ...

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

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

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

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

  7. MySQL的并行复制多线程复制MTS(Multi-Threaded Slaves)

    MySQL的并行复制多线程复制MTS(Multi-Threaded Slaves) http://www.tuicool.com/articles/m2Unmeq 姜承饶 简称MTS:基于binlog ...

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

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

  9. mysql主从之多线程复制

    多线程复制 mysql 主从复制原理: 1. master 节点上的binlogdump 线程,在slave 与其正常连接的情况下,将binlog 发送到slave 上. 2. slave 节点的I/ ...

随机推荐

  1. WordtoPdfUtil word转pdf

    jar: <dependency> <groupId>com.jacob</groupId> <artifactId>jacob</artifac ...

  2. sql server数据库显示“单用户”的解决方法

    USE master; GO DECLARE @SQL VARCHAR(MAX); SET @SQL='' SELECT @SQL=@SQL+'; KILL '+RTRIM(SPID) --杀掉该进程 ...

  3. iOS - 在xib中UILabel文字如何换行

    在换行的位置按住Option + Enter键即可换行

  4. Maya编程——节点&命令

    代码写完出现问题: 查了一下原因:

  5. python安装 错误 “User installations are disabled via policy on the machine”

    解决方法一:  1.在运行里输入 gpedit.msc  2.计算机配置管理>>管理模板>>windows组件>>windows Installer>> ...

  6. spring boot的actuator

    actuator官方的介绍 Spring Boot includes a number of additional features to help you monitor and manage yo ...

  7. Linux 就该这么学 CH03 管道符、重定向和环境变量

    0 概述 1 输入输出重定向 重定向技术有5种模式: 标准覆盖输出重定向 标准追加输出重定向 错误覆盖输出重定向 错误追加输出重定向 输入重定向 输入重定向是将文件导入到命令中. 输出重定向是将输入到 ...

  8. Java调用动态链接库so文件(传参以及处理返回值问题)

    刚来到公司,屁股还没坐稳,老板把我叫到办公室,就让我做一个小程序.我瞬间懵逼了.对小程序一窍不通,还好通过学习小程序视频,两天的时间就做了一个云开发的小程序,但是领导不想核心的代码被别人看到,给了我一 ...

  9. Spring 通过@Import实现Bean的注册

    今天看到一个神奇的用法, Spring可以通过@Import导入实现了ImportBeanDefinitionRegistrar接口的类来注册那个类. ImportBeanDefinitionRegi ...

  10. Django框架之第四篇(视图层)--HttpRequest对象、HttpResponse对象、JsonResponse、CBV和FBV、文件上传

    视图层 一.视图函数 一个视图函数,简称视图,是一个简单的python函数,它接收web请求并且会返回web响应.响应可以是一张网页的html,一个重定向,或者是一张图片...任何东西都可以.无论是什 ...