美文网首页
Xtrabackup:和FTWRL相关参数的说明

Xtrabackup:和FTWRL相关参数的说明

作者: 重庆八怪 | 来源:发表于2022-10-18 10:48 被阅读0次

    可能还是经常遇到FTWRL堵塞备份的情况,这里稍微理了一下这部分,当然内容不多,仅供参考。
    当然这部分主要是5.7先关比较紧密,到了8.0,如果没有MyISAM表使用是新的备份锁,可参考
    https://www.jianshu.com/p/9007883b58b8 MySQL:8.0新的备份锁的浅析及其重要BUG


    相关参数

    当前看起来xtrabackup一共是5个参数进行控制,一共分为2类,分为执行前和执行后,如果都不设置FTWRL不会进行任何判定,且无限期等待lock_wait_timeout=31536000。

    第一类 执行FTWRL之前的判断包含3个参数,如果判定失败就不备份了,也不会发起FTWRL
    1. ftwrl-wait-timeout:
      默认为0,代表不开启本类判定,可以设置为秒,主要用于在执行FTWRL之前根据数据库中是否有长语句(包含DML/DDL/SELECT)可能导致堵塞FTWRL。如果从准备执行FTWRL的时间点+ftwrl-wait-timeout设置的时间,这些长查询依旧没有结束则停止备份退出。
      官方解释:
      This option specifies time in seconds that xtrabackup should wait for queries that would block FLUSH TABLES WITH READ LOCK before running it. If there are still such queries when the timeout expires, xtrabackup terminates with an error. Default is 0, in which case it does not wait for queries to complete and starts FLUSH TABLES WITH READ LOCK immediately. Where supported xtrabackup will automatically use Backup Locks as a lightweight alternative to FLUSH TABLES WITH READ LOCK to copy non-InnoDB data to avoid blocking DML queries that modify InnoDB tables.
    2. ftwrl-wait-threshold:
      默认为60秒,也就是通过show processlist中语句执行时间的time进行判定,大于多少秒的语句定义为长查询。
      官方解释:
      This option specifies the query run time threshold which is used by xtrabackup to detect long-running queries with a non-zero value of --ftwrl-wait-timeout. FLUSH TABLES WITH READ LOCK is not started until such long-running queries exist. This option has no effect if --ftwrl-wait-timeout is 0. Default value is 60 seconds. Where supported xtrabackup will automatically use Backup Locks as a lightweight alternative to FLUSH TABLES WITH READ LOCK to copy non-InnoDB data to avoid blocking DML queries that
      modify InnoDB tables.
    3. ftwrl-wait-query-type:
      默认为ALL,就是包含了DDL/DML/SELECT等语句都计入长查询判定的范畴。可以设置为SELECT,则排除其他类型的语句。这个选项保证为ALL比较好,因为还没有实际的执行FTWRL,只是影响判定的范围而已。
      This option specifies which types of queries are allowed to complete before xtrabackup will issue the globallock. Default is all.
    第二类 执行FTWRL期间的判定,包含2个参数,也就是已经发起了FTWRL
    1. kill-long-queries-timeout
      默认为0,代表不开启这类判定,可以设置为秒,主要是如果监控FTWRL是否被堵塞,如果从发起FTWRL开始+kill-long-queries-timeout依旧没有完成FTWRL,则说明被堵塞了,就需要杀掉数据库中的长SQL,也就是可能堵塞FTWRL的语句,查杀的范围很较广,判定时间的标准依旧为show processlist中语句执行时间的time大于了kill-long-queries-timeout设置则会杀掉。
      官方解释:
      This option specifies the number of seconds xtrabackup waits between starting FLUSH TABLES WITH READ LOCK and killing those queries that block it. Default is 0 seconds, which means xtrabackup will not attempt to kill any queries. In order to use this option xtrabackup user should have the PROCESS and SUPER privileges. Where supported, xtrabackup automatically uses Backup Locks as a lightweight alternative to FLUSH TABLES WITH READ LOCK to copy non-InnoDB data to avoid blocking DML queries that modify
      InnoDB tables.
      2、kill-long-query-type
      默认为为ALL(8.0.26的XTRABACKUP默认修改为了SELECT),就是包含了DDL/DML/SELECT等语句都计入被杀掉的范畴。可以设置为SELECT,则排除其他类型的语句。这个选项保险起见就是设置为SELECT,排除杀掉其他类的语句。
      官方解释:
      This option specifies which types of queries should be killed to unblock the global lock. Default is “all”

    相关逻辑

    lock_tables_ftwrl   
       ->先设置wait lock timeout 时间为最大时间
         SET SESSION lock_wait_timeout=31536000
       ->如果没有设置ftwrl-wait-timeout和kill-long-queries-timeout   
         ->打印日志Executing FLUSH NO_WRITE_TO_BINLOG TABLES...
         ->直接发起FTWRL,也就是不管当前是否有长查询执行       
       ->如果设置ftwrl-wait-timeout   
         ->wait_for_no_updates 带入参数ftwrl-wait-timeout和ftwrl-wait-threshold 
           ->循环等待,
             ->have_queries_to_wait_for 带入参数ftwrl-wait-threshold 
               ->执行show processlist,获取其中的时间time和info
               ->循环每行数据
                 A:判定1 time是否超过了ftwrl-wait-threshold设置的时间(默认60秒)   
                 AND(注意&&,则两个条件都需要满足)
                 B:判定2 info中的语句是否为
                 "insert",  "update", "delete", "replace", 
                 "alter","load", "select", "do",     
                 "handler", "call","execute", "begin"   
                 中的一种,采用strncasecmp比较info的前几个字符是否和
                 列举出来的相同
                 
                 如果两个条件都满足则,表示需要等待,打印日志
                 Waiting for query * duration * sec          
                 如果一个条件不满足则不需要等待了,因为没有长时间(超过ftwrl-wait-threshold)的语句了
             如果等待时间大于了ftwrl-wait-timeout设置的时间,则超时,不继续备份,打印日志
             Unable to obtain lock. Please try again later.
             如果等待时间小于ftwrl-wait-timeout设置的时间,则继续备份
       打印日志Executing FLUSH TABLES WITH READ LOCK
       ->如果设置了kill-long-queries-timeout
         ->start_query_killer
           ->新启动一个线程kill_query_thread用于监控是否出现了堵塞的情况
             ->如果等待时间超过kill-long-queries-timeout,则根据kill-long-query-type
               设置的类型进行判定杀什么样的会话,比如DML/SELECT,注意这些会话依旧要
               超过kill-long-queries-timeout设置的时长,并且记录日志
               Killing query * (duration * sec)       
         ->本线程执行FTWRL
         ->本线程执行FTWRL完成
    

    相关文章

      网友评论

          本文标题:Xtrabackup:和FTWRL相关参数的说明

          本文链接:https://www.haomeiwen.com/subject/jfvjzrtx.html