orchestrator核心参数之一致性相关参数

一、FailMasterPromotionIfSQLThreadNotUpToDate

类型: bool
默认值: false
when true, and a master failover takes place, if candidate master has not consumed all relay logs, promotion is aborted with error
如果在故障发生时, 所有的从库都是滞后的, 即使是拥有最新数据的、被选举为新主库的候选节点也可能有未应用的中继日志. 在这样的节点上执行reset slave all会丢失中继日志数据. 当这个参数为true是, promotion is aborted with error,此时如果候选主库(即某个从库)的SQL线程尚未消费完所有的relay log(即还有未应用的事务),那么该候选主库的提升(promotion)操作将被中止,并伴随一个错误消息。。
参数设置建议:
该参数保持默认,建议修改DelayMasterPromotionIfSQLThreadNotUpToDate参数为false,因为当主库发生故障,并且slave有延迟的时候,我们希望依旧可以故障切换,保持业务连续性,只不过故障切换的时间就延长;
二、DelayMasterPromotionIfSQLThreadNotUpToDate
类型: bool
默认值: false
when true, and a master failover takes place, if candidate master has not consumed all relay logs, delay promotion until the sql thread has caught up;
如果在故障发生时, 所有的从库都是滞后的, 即使是拥有最新数据的、被选举为新主库的候选节点也可能有未应用的中继日志. 当该参数为true 时, orchestrator 将等待SQL thread应用完所有relay log, 然后再将候选从库提升为新主库. FailMasterPromotionIfSQLThreadNotUpToDate和DelayMasterPromotionIfSQLThreadNotUpToDate是相互排斥的.
DelayMasterPromotionIfSQLThreadNotUpToDate参数设置为true的含义是,在故障切换过程中,如果检测到候选slave的SQL线程没有处于最新状态(即存在尚未应用完毕的relay log),则延迟将该slave提升为主库的操作,直到其SQL线程追上并应用完所有的relay log为止
将DelayMasterPromotionIfSQLThreadNotUpToDate参数设置为true,可以确保在故障切换过程中,只有那些已经追上主库并完全同步了数据的从库才有资格被提升为主库。这样可以最大限度地减少数据丢失的风险,并保证新主库的数据完整性和一致性。
参数设置建议:
这个参数的设置对于确保数据一致性至关重要,并且会影响到故障切换的速度和效率,因为需要等待从库的SQL线程都追上主库才能进行切换。因此,在实际应用中需要根据具体的业务需求和系统环境来权衡考虑;
1)如果您希望快速恢复,服务可用性高于数据一致性,例如登录日志等服务,那么建议将DelayMasterPromotionIfSQLThreadNotUpToDate设置成false来提高故障切换效率;
2)如果您的业务对数据一致性要求比较高,例如支付系统,不允许丢数据,那么建议将DelayMasterPromotionIfSQLThreadNotUpToDate设置成true来尽可能保证数据一致性;
三、PostponeSlaveRecoveryOnLagMinutes
类型: uint
默认值: 0
On crash recovery, replicas that are lagging more than given minutes are only resurrected late in the recovery process, after master/IM has been elected and processes executed. Value of 0 disables this feature
在崩溃恢复时,延迟超过给定分钟数的副本,在选择完副本并且执行完流程后才进行恢复(进行主从关系的建立)。 值为 0 禁用此功能。
PostponeReplicaRecoveryOnLagMinutes是 PostponeSlaveRecoveryOnLagMinutes的别名;
参数设置建议:

设置该参数成非0值,可以提高故障切换的时效;否则可能会因为某个slave延迟过大,建立主从关系的追加binlog这个步骤会拖慢整个恢复的操作;

如下所示为该参数相关源代码:

func shouldPostponeRelocatingReplica(replica *Instance, postponedFunctionsContainer *PostponedFunctionsContainer) bool {    if postponedFunctionsContainer == nil {        return false    }    if config.Config.PostponeReplicaRecoveryOnLagMinutes > 0 && // 设置了参数PostponeReplicaRecoveryOnLagMinutes        replica.SQLDelay > config.Config.PostponeReplicaRecoveryOnLagMinutes*60 { // 该实例的延迟大于参数PostponeReplicaRecoveryOnLagMinutes 设置的值        // This replica is lagging very much, AND        // we're configured to postpone operation on this replica so as not to delay everyone else.        // 即某个从副本(replica)的延迟非常严重,同时系统配置了推迟在这个复制上执行操作,以防止拖慢其他操作的进行。        return true    }    if replica.LastDiscoveryLatency > ReasonableDiscoveryLatency {        return true    }    return false }
四、FailMasterPromotionOnLagMinutes
默认值:0
当 该参数设置 > 0 时,如果候选副本滞后 >= 配置的分钟数,则阻止故障转移。
在启动的时候会检查 当FailMasterPromotionOnLagMinutes 设置大于0的值时 ,必须设置 ReplicationLagQuery 自定义查询。
if this.FailMasterPromotionOnLagMinutes > 0 && this.ReplicationLagQuery == "" {        return fmt.Errorf("nonzero FailMasterPromotionOnLagMinutes requires ReplicationLagQuery to be set")    }
在完成二次选主后,对新主库会检查延迟是否大于该参数,如果大于则取消这次提升
if config.Config.FailMasterPromotionOnLagMinutes > 0 && // FailMasterPromotionOnLagMinutes 设置了大于0 的值        // 被提升的实例有延迟  超过 FailMasterPromotionOnLagMinutes 设置            time.Duration(promotedReplica.ReplicationLagSeconds.Int64)*time.Second >= time.Duration(config.Config.FailMasterPromotionOnLagMinutes)*time.Minute {            // candidate replica lags too much 候选从副本延迟太多            return nil, fmt.Errorf("RecoverDeadMaster: failed promotion. FailMasterPromotionOnLagMinutes is set to %d (minutes) and promoted replica %+v 's lag is %d (seconds)", config.Config.FailMasterPromotionOnLagMinutes, promotedReplica.Key, promotedReplica.ReplicationLagSeconds.Int64)        }
如果取消提升 ,拓扑结构可能处于一个这种状态 ,即 新主还没有reset slave all; set read_only=0; 和执行选主后的钩子脚本;
参数设置建议:
建议保持默认值,因为当主库发生故障,并且slave有延迟的时候,我们希望依旧可以故障切换,保持业务连续性,只不过故障切换的时间就延长;
五、ReplicationLagQuery
解释 :自定义查询用来检查复制延迟 (e.g. heartbeat table). 必须返回一个单行的数字,就是指延迟,与 SlaveLagQuery 是同义词。

该参数支持配置执行脚本,例如执行python或者shell脚本来获取延迟的值;

如下所示脚本

cat  mysql_replica_lag.py import sys import mysql.connector from mysql.connector import Error # 连接到MySQL服务器 def get_replication_lag(connection_string):    try:        connection = mysql.connector.connect(**connection_string)        if connection.is_connected():            cursor = connection.cursor()            cursor.execute("SHOW SLAVE STATUS")            rows = cursor.fetchall()            if len(rows) > 0:                seconds_behind = rows[0][14]                return seconds_behind            else:                return -1  # 未配置复制或者复制出错        else:            return -1  # 连接失败    except Error as e:        print(f"Error: {e}")        return -1    finally:        if connection.is_connected():            connection.close() # 获取命令行参数作为连接字符串 if __name__ == '__main__':    if len(sys.argv) > 1:        config = sys.argv[1]        print(get_replication_lag(config))    else:        print("No connection string provided.") #参数配置如下所示: ReplicationLagQuery: python /path/to/replica_lag.py '{"user":"your_username","password":"your_password","host":"your_host","port":3306}'



请使用浏览器的分享功能分享到微信等