MySQL 发生同步延迟时Seconds_Behind_Master还为0的原因


Posted in MySQL onJune 21, 2021
目录
  • 问题描述
  • 原理简析
  • 问题分析
  • 拓展一下
  • 总结一下

 

问题描述

用户在主库上执行了一个 alter 操作,持续约一小时。操作完成之后,从库发现存在同步延迟,但是监控图表中的 Seconds_Behind_Master 指标显示为 0,且 binlog 的延迟距离在不断上升。

 

原理简析

既然是分析延迟时间,那么自然先从延迟的计算方式开始入手。为了方便起见,此处引用官方版本 5.7.31 的源代码进行阅读。找到计算延迟时间的代码:

./sql/rpl_slave.cc

bool show_slave_status_send_data(THD *thd, Master_info *mi,
                                 char* io_gtid_set_buffer,
                                 char* sql_gtid_set_buffer)
......
if ((mi->get_master_log_pos() == mi->rli->get_group_master_log_pos()) &&
        (!strcmp(mi->get_master_log_name(), mi->rli->get_group_master_log_name())))
    {
      if (mi->slave_running == MYSQL_SLAVE_RUN_CONNECT)
        protocol->store(0LL);
      else
        protocol->store_null();
    }
    else
    {
      long time_diff= ((long)(time(0) - mi->rli->last_master_timestamp)
                       - mi->clock_diff_with_master);

      protocol->store((longlong)(mi->rli->last_master_timestamp ?
                                   max(0L, time_diff) : 0));
    }
......

从 time_diff 的计算方式来看,可以发现这个延迟基本上就是一个时间差值,然后再算上主从之间的时间差。不过 if 挺多的,所以借用源代码文件中的注释:

/*
     The pseudo code to compute Seconds_Behind_Master:
     if (SQL thread is running)
     {
       if (SQL thread processed all the available relay log)
       {
         if (IO thread is running)
            print 0;
         else
            print NULL;
       }
        else
          compute Seconds_Behind_Master;
      }
      else
       print NULL;
  */

可以知道,Seconds_Behind_Master的计算分为两个部分:

  • SQL 线程正常,且回放完所有的 relaylog 时,如果 IO 线程正常,那么直接置 0。
  • SQL 线程正常,且回放完所有的 relaylog 时,如果 IO 线程不正常,那么直接置 NULL。
  • SQL 线程正常,且没有回放完所有的 relaylog 时,计算延迟时间。

那么在最后计算延迟时间的时候,看看那几个变量代表的意义:

  • time(0):当前的时间戳,timestamp 格式的。
  • last_master_timestamp:这个 event 在主库上执行的时刻,timestamp 格式。
  • clock_diff_with_master:slave 和 master 的时间差,在 IO 线程启动时获取的。

由此可见,延迟计算的时候,实际上是以 slave 本地的时间来减掉回放的这个 event 在 master 执行的时刻,再补偿两者之间的时间差,最后得到的一个数值。从逻辑上看是没什么问题的,由于 time(0) 和 clock_diff_with_master 在大多数时候是没有什么出问题的机会的,所以这次的问题,应该是出在 last_master_timestamp 上了。

PS:虽说大部分时候没问题,但是 time(0) 取的是本地时间,因此 slave 的本地时间有问题的话,这个最终的值也会出错,不过不在本案例的问题讨论范围之内了。

那么找一下执行 event 的时候,计算last_master_timestamp的逻辑,结合注释可以发现普通复制和并行复制用了不同的计算方式,第一个是普通的复制,计算时间点在执行 event 之前:

./sql/rpl_slave.cc

......
  if (ev)
  {
    enum enum_slave_apply_event_and_update_pos_retval exec_res;

    ptr_ev= &ev;
    /*
      Even if we don't execute this event, we keep the master timestamp,
      so that seconds behind master shows correct delta (there are events
      that are not replayed, so we keep falling behind).

      If it is an artificial event, or a relay log event (IO thread generated
      event) or ev->when is set to 0, or a FD from master, or a heartbeat
      event with server_id '0' then  we don't update the last_master_timestamp.

      In case of parallel execution last_master_timestamp is only updated when
      a job is taken out of GAQ. Thus when last_master_timestamp is 0 (which
      indicates that GAQ is empty, all slave workers are waiting for events from
      the Coordinator), we need to initialize it with a timestamp from the first
      event to be executed in parallel.
    */
    if ((!rli->is_parallel_exec() || rli->last_master_timestamp == 0) &&
         !(ev->is_artificial_event() || ev->is_relay_log_event() ||
          (ev->common_header->when.tv_sec == 0) ||
          ev->get_type_code() == binary_log::FORMAT_DESCRIPTION_EVENT ||
          ev->server_id == 0))
    {
      rli->last_master_timestamp= ev->common_header->when.tv_sec +
                                  (time_t) ev->exec_time;
      DBUG_ASSERT(rli->last_master_timestamp >= 0);
    }
......

last_master_timestamp的值是取了 event 的开始时间并加上执行时间,在 5.7 中有不少 event 是没有执行时间这个数值的,8.0 给很多 event 添加了这个数值,因此也算是升级 8.0 之后带来的好处。

而并行复制的计算方式,参考如下这一段代码:

./sql/rpl\_slave.cc

......
  /*
    We need to ensure that this is never called at this point when
    cnt is zero. This value means that the checkpoint information
    will be completely reset.
  */

  /*
    Update the rli->last_master_timestamp for reporting correct Seconds_behind_master.

    If GAQ is empty, set it to zero.
    Else, update it with the timestamp of the first job of the Slave_job_queue
    which was assigned in the Log_event::get_slave_worker() function.
  */
  ts= rli->gaq->empty()
    ? 0
    : reinterpret_cast<Slave_job_group*>(rli->gaq->head_queue())->ts;
  rli->reset_notified_checkpoint(cnt, ts, need_data_lock, true);
  /* end-of "Coordinator::"commit_positions" */

......

在 Coordinator 的 commit_positions 这个逻辑中,如果 gaq 队列为空,那么last_master_timestamp直接置 0,否则会选择 gaq 队列的第一个 job 的时间戳。需要补充一点的是,这个计算并不是实时的,而是间歇性的,在计算逻辑前面,有如下的逻辑:

/*
    Currently, the checkpoint routine is being called by the SQL Thread.
    For that reason, this function is called call from appropriate points
    in the SQL Thread's execution path and the elapsed time is calculated
    here to check if it is time to execute it.
  */
  set_timespec_nsec(&curr_clock, 0);
  ulonglong diff= diff_timespec(&curr_clock, &rli->last_clock);
  if (!force && diff < period)
  {
    /*
      We do not need to execute the checkpoint now because
      the time elapsed is not enough.
    */
    DBUG_RETURN(FALSE);
  }

即在这个 period 的时间间隔之内,会直接 return,并不会更新这个last_master_timestamp,所以有时候也会发现并行复制会时不时出现 Seconds_Behind_Master 在数值上从 0 到 1 的变化。

而 gaq 队列的操作,估计是类似于入栈退栈的操作,所以留在 gaq 的总是没有执行完的事务,因此时间计算从一般场景的角度来看是没问题。

 

问题分析

原理简析中简要阐述了整个计算的逻辑,那么回到这个问题本身,腾讯云数据库 MySQL 默认是开启了并行复制的,因此会存在 gaq 队列,而 alter 操作耗时非常的长,不论 alter 操作是否会被放在一组并行事务中执行(大概率,DDL 永远是一个单独的事务组),最终都会出现 gaq 队列持续为空,那么就会把last_master_timestamp置 0,而参考 Seconds_Behind_Master 的计算逻辑,最终的 time_diff 也会被置 0,因此 alter 操作结束前的延迟时间一直会是 0。而当 alter 操作执行完之后,gaq 队列会填充新的 event 和事务,所以会出现延迟之前一直是 0,但是突然跳到非常高的现象。

 

拓展一下

对比普通复制和并行复制计算方式上的差异,可以知道以下几个特点:

  • 开启并行复制之后,延迟时间会经常性的在 0 和 1 之间跳变。
  • alter 操作,单个大事务等在并行复制的场景下容易导致延迟时间不准,而普通的复制方式不会。
  • 由于主从时间差是在 IO 线程启动时就计算好的,所以期间 slave 的时间出现偏差之后,延迟时间也会出现偏差。

 

总结一下

严谨的延迟判断,还是依靠 GTID 的差距和 binlog 的 position 差距会比较好,从 8.0 的 event 执行时间变化来看,至少 Oracle 官方还是在认真干活的,希望这些小毛病能尽快的修复吧。

以上就是MySQL 发生同步延迟时Seconds_Behind_Master还为0的原因的详细内容,更多关于MySQL 同步延迟Seconds_Behind_Master为0的资料请关注三水点靠木其它相关文章!

MySQL 相关文章推荐
数据库的高级查询六:表连接查询:外连接(左外连接,右外连接,UNION关键字,连接中ON与WHERE的不同)
Apr 05 MySQL
MySQL CHAR和VARCHAR该如何选择
May 31 MySQL
Navicat连接MySQL错误描述分析
Jun 02 MySQL
MySQL 时间类型的选择
Jun 05 MySQL
一篇文章带你深入了解Mysql触发器
Aug 02 MySQL
浅谈MySQL之select优化方案
Aug 07 MySQL
MySQL窗口函数的具体使用
Nov 17 MySQL
MySQL常见优化方案汇总
Jan 18 MySQL
利用JuiceFS使MySQL 备份验证性能提升 10 倍
Mar 17 MySQL
MySQL中rank() over、dense_rank() over、row_number() over用法介绍
Mar 23 MySQL
详解MySQL的主键查询为什么这么快
Apr 03 MySQL
MySQL查询日期时间
May 15 MySQL
分析mysql中一条SQL查询语句是如何执行的
MySQL如何使用使用Xtrabackup进行备份和恢复
Jun 21 #MySQL
MySQL 数据恢复的多种方法汇总
Jun 21 #MySQL
Mysql数据库值的添加、修改、删除及清空操作实例
Unity连接MySQL并读取表格数据的实现代码
新手入门Mysql--sql执行过程
MySQL 外键约束和表关系相关总结
Jun 20 #MySQL
You might like
杏林同学录(七)
2006/10/09 PHP
如何提高MYSQL数据库的查询统计速度 select 索引应用
2007/04/11 PHP
php5编程中的异常处理详细方法介绍
2008/07/29 PHP
PHP中通过加号合并数组的一个简单方法分享
2011/01/27 PHP
PHP压缩html网页代码(清除空格,换行符,制表符,注释标记)
2012/04/02 PHP
使用PHP实现蜘蛛访问日志统计
2013/07/05 PHP
PHP中防止SQL注入方法详解
2014/12/25 PHP
php实现微信企业号支付个人的方法详解
2017/07/26 PHP
JS获取scrollHeight问题想到的标准问题
2007/05/27 Javascript
Tinymce+jQuery.Validation使用产生的BUG
2010/03/29 Javascript
js实现GridView单选效果自动设置交替行、选中行、鼠标移动行背景色
2010/05/27 Javascript
jquery下动态显示jqGrid以及jqGrid的属性设置容易出现问题的解决方法
2010/10/22 Javascript
javascript中this做事件参数相关问题解答
2013/03/17 Javascript
jquery分页插件jpaginate在IE中不兼容问题
2014/04/22 Javascript
通过jquery 获取URL参数并进行转码
2014/08/18 Javascript
微信小程序switch组件使用详解
2018/01/31 Javascript
AjaxUpLoad.js实现文件上传功能
2018/03/02 Javascript
jQuery基于闭包实现的显示与隐藏div功能示例
2018/06/09 jQuery
微信小程序下拉框组件使用方法详解
2018/12/28 Javascript
JavaScript遍历数组的三种方法map、forEach与filter实例详解
2019/02/27 Javascript
JavaScript直接调用函数与call调用的区别实例分析
2020/05/22 Javascript
vue实现禁止浏览器记住密码功能的示例代码
2021/02/03 Vue.js
Python获取linux主机ip的简单实现方法
2016/04/18 Python
解决Python正则表达式匹配反斜杠''\''问题
2019/07/17 Python
python sqlite的Row对象操作示例
2019/09/11 Python
如何关掉pycharm中的python console(图解)
2019/10/31 Python
不到20行实现Python代码即可制作精美证件照
2020/04/24 Python
django rest framework 自定义返回方式
2020/07/12 Python
深入了解NumPy 高级索引
2020/07/24 Python
Django实现微信小程序支付的示例代码
2020/09/03 Python
写演讲稿要注意的六件事
2014/01/14 职场文书
房屋买卖委托公证书
2014/04/08 职场文书
公司离职证明范本
2014/10/17 职场文书
个人简历求职信范文
2015/03/20 职场文书
调研报告的主要写法
2019/04/18 职场文书
使用Golang的channel交叉打印两个数组的操作
2021/04/29 Golang