[MySQL Bug] bug#61579碎碎念

简介:
changelog:

InnoDB: For an InnoDB table with a trigger, under the setting innodb_autoinc_lock_mode=1, sometimes auto-increment values could be interleaved when inserting into the table from two sessions concurrently. The sequence of auto-increment values could vary depending on timing, leading to data inconsistency in systems using replication. (Bug #12752572, Bug #61579)

根据changelog的描述,当某个表上存在触发器时,并且innodb_autoinc_lock_mode=1,当两个session并发插入表时,自增列的值可能会产生交错。当使用statement记录时,会导致duplicate key错误,导致复制停止。

把触发器移除掉,则一切正常。

简单的gdb一把可以看出来,对于一条简单的插入语句,在函数handler::update_auto_increment中,当有trigger时,预估值 estimation_rows_to_insert值为0, 当没有trigger时,则为1.

那么在哪里设置 estimation_rows_to_insert的值呢?

在handler.h里定义了以下两个函数

  void ha_start_bulk_insert(ha_rows rows)

  {

    estimation_rows_to_insert= rows;

    start_bulk_insert(rows);

  }

  int ha_end_bulk_insert()

  {

    estimation_rows_to_insert= 0;

    return end_bulk_insert();

  }

从mysql_insert的函数逻辑来看

822 #ifndef EMBEDDED_LIBRARY

 823   if (lock_type != TL_WRITE_DELAYED)

 824 #endif /* EMBEDDED_LIBRARY */

 825   {

 826     if (duplic != DUP_ERROR || ignore)

 827       table->file->extra(HA_EXTRA_IGNORE_DUP_KEY);

 828     /**

 829       This is a simple check for the case when the table has a trigger

 830       that reads from it, or when the statement invokes a stored function

 831       that reads from the table being inserted to.

 832       Engines can't handle a bulk insert in parallel with a read form the

 833       same table in the same connection.

 834     */

 835     if (thd->locked_tables_mode <= LTM_LOCK_TABLES)

 836       table->file->ha_start_bulk_insert(values_list.elements);

 837   }

 

对于没有触发器的表,

lock_type=TL_WRITE_CONCURRENT_INSERT

thd->locked_tables_mode=LTM_NONE

对于有触发器的表,

thd->locked_tables_mode=LTM_PRELOCKED  因此不会设置estimation_rows_to_insert

跑完test case后很快就可以观察到备库duplicate key错误(statement模式)

从Binlog来看

SET INSERT_ID=1/*!*/;

insert into t (a) values (1)                    —–sql1

SET INSERT_ID=65/*!*/;       

insert into t (a) values (2)                    —–sql2

SET INSERT_ID=1026

insert into t (a) values (3)                    —–sql3

SET INSERT_ID=2/*!*/;                         —–sql4

insert into t (a) values (10000),(10001),(10002),(10003),……(10938),(10939)

 

由于在存在trigger时,estimation_rows_to_insert值为0,因此在handler::update_auto_increment中走按批分配的逻辑,按照1,2,4,8,16……这样预留保留数

从binlog来看,执行了如下过程

sql1/sql2/sql3串行,并和sql4并行

sql1获得自增值 1

sql4获得自增值,2-64共63个值,也就是1+2+4+8+16+32=63

sql2获得自增值65

sql4继续获得自增值66-1025

sql3获得自增值1026-10004

另外这个bug,还会导致大量的自增列浪费

mysql> select max(id) from t;

+---------+

| max(id) |

+---------+

|   10004 |

+---------+

1 row in set (0.00 sec)

CREATE TABLE `t` (

  `id` int(11) NOT NULL AUTO_INCREMENT,

  `a` int(11) DEFAULT NULL,

  PRIMARY KEY (`id`)

) ENGINE=InnoDB AUTO_INCREMENT=16387 DEFAULT CHARSET=gbk

从官方补丁来看,在函数handler::update_auto_increment做了些修改,即对于这种特殊情况作了处理,对estimation_rows_to_insert赋予当前插入的记录数量

— sql/handler.cc  2012-04-12 11:04:12 +0000

+++ sql/handler.cc  2012-05-16 08:40:18 +0000

@@ -2528,6 +2528,17 @@

         reservation means potentially losing unused values).

         Note that in prelocked mode no estimation is given.

       */  

+

+      /*

+        For multi-row inserts, if the bulk inserts cannot be started, the

+        handler::estimation_rows_to_insert will not be set. Set it here.

+      */

+      if ((estimation_rows_to_insert == 0) &&

+          (thd->lex->many_values.elements > 0))

+      {

+        estimation_rows_to_insert= thd->lex->many_values.elements;

+      }

 


相关实践学习
每个IT人都想学的“Web应用上云经典架构”实战
本实验从Web应用上云这个最基本的、最普遍的需求出发,帮助IT从业者们通过“阿里云Web应用上云解决方案”,了解一个企业级Web应用上云的常见架构,了解如何构建一个高可用、可扩展的企业级应用架构。
MySQL数据库入门学习
本课程通过最流行的开源数据库MySQL带你了解数据库的世界。 &nbsp; 相关的阿里云产品:云数据库RDS MySQL 版 阿里云关系型数据库RDS(Relational Database Service)是一种稳定可靠、可弹性伸缩的在线数据库服务,提供容灾、备份、恢复、迁移等方面的全套解决方案,彻底解决数据库运维的烦恼。 了解产品详情:&nbsp;https://www.aliyun.com/product/rds/mysql&nbsp;
相关文章
|
SQL 关系型数据库 MySQL
(十八)MySQL排查篇:该如何定位并解决线上突发的Bug与疑难杂症?
前面《MySQL优化篇》、《SQL优化篇》两章中,聊到了关于数据库性能优化的话题,而本文则再来聊一聊关于MySQL线上排查方面的话题。线上排查、性能优化等内容是面试过程中的“常客”,而对于线上遇到的“疑难杂症”,需要通过理性的思维去分析问题、排查问题、定位问题,最后再着手解决问题,同时,如果解决掉所遇到的问题或瓶颈后,也可以在能力范围之内尝试最优解以及适当考虑拓展性。
2033 3
|
关系型数据库 MySQL PHP
【Bug解决】Thinkphp5 PDO::__construct(): MySQL server has gone away解决办法
【Bug解决】Thinkphp5 PDO::__construct(): MySQL server has gone away解决办法
507 0
|
SQL 关系型数据库 MySQL
MySQL 8.0.23上遇到一个FIND_IN_SET的BUG(一)
MySQL 8.0.23上遇到一个FIND_IN_SET的BUG(一)
383 0
MySQL 8.0.23上遇到一个FIND_IN_SET的BUG(一)
|
存储 固态存储 关系型数据库
MySQL 5.6 change buffer bug导致crash
Insert buffer 内部标识长度的位图没有正确更新,导致问题
469 0
|
SQL 运维 监控
一个诡异的MySQL查询超时问题,居然隐藏着存在了两年的BUG
一个诡异的MySQL查询超时问题,居然隐藏着存在了两年的BUG
418 0
|
SQL 关系型数据库 MySQL
看来,MySQL next-key lock 的 bug 并没有被修复!
在上一篇文章《MySQL next-key lock 加锁范围是什么?》中已经介绍了主键索引的加锁范围,现在来回顾一下
553 0
|
SQL 缓存 关系型数据库
故障案例:MySQL唯一索引有重复值,官方却说This is not a bug
故障案例:MySQL唯一索引有重复值,官方却说This is not a bug
658 0
|
SQL Oracle 关系型数据库
这次被坑惨了,MySQL的隐式转换导致了一个线上BUG
某一天,开发问我,为什么针对一个查询会有两条记录,且其中一条记录并不符合条件select * from tablea where xxno = 170325171202362928;xxno为 170325171202362928 和 170325171202362930的都出现在结果中。 一个等值查询为什么会有另外一个不同值的记录查询出来呢? 我们一起来看看究竟!
|
SQL 关系型数据库 MySQL
MySQL 8.0.23上遇到一个FIND_IN_SET的BUG(二)
MySQL 8.0.23上遇到一个FIND_IN_SET的BUG(二)
255 0

推荐镜像

更多