婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁 > 知識庫 > MySQL 發生同步延遲時Seconds_Behind_Master還為0的原因

MySQL 發生同步延遲時Seconds_Behind_Master還為0的原因

熱門標簽:話務外呼系統怎么樣 拉卡拉外呼系統 臨清電話機器人 高清地圖標注道路 外東北地圖標注 大眾點評星級酒店地圖標注 云南電商智能外呼系統價格 400電話可以辦理嗎 智能外呼系統復位

問題描述

用戶在主庫上執行了一個 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_castSlave_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的Seconds_Behind_Master
  • python3文件復制、延遲文件復制任務的實現方法
  • docker實現mysql主從復制的示例代碼
  • MySQL數據庫 Load Data 多種用法
  • MySQL數據庫Shell import_table數據導入
  • Mysql數據庫的主從同步配置
  • Mysql實現簡易版搜索引擎的示例代碼
  • MySQL命令無法輸入中文問題的解決方式
  • 當面試官問mysql中char與varchar的區別
  • MySQL的從庫Seconds_Behind_Master延遲總結

標簽:阿里 無錫 揚州 定西 福州 三明 山西 溫州

巨人網絡通訊聲明:本文標題《MySQL 發生同步延遲時Seconds_Behind_Master還為0的原因》,本文關鍵詞  MySQL,發生,同步,延遲,時,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《MySQL 發生同步延遲時Seconds_Behind_Master還為0的原因》相關的同類信息!
  • 本頁收集關于MySQL 發生同步延遲時Seconds_Behind_Master還為0的原因的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 淮滨县| 驻马店市| 东乡县| 南阳市| 含山县| 莲花县| 治多县| 吴堡县| 定安县| 周口市| 临西县| 荥经县| 保靖县| 台北县| 龙泉市| 铜梁县| 松江区| 赣州市| 孟连| 齐齐哈尔市| 衡南县| 济阳县| 庆城县| 介休市| 武穴市| 阳东县| 洛阳市| 故城县| 二连浩特市| 年辖:市辖区| 常宁市| 仁布县| 和田市| 瑞丽市| 台安县| 鸡东县| 绥化市| 临邑县| 汉中市| 青铜峡市| 玉门市|