From 4e418390cae7877602c04b92ce275ad05380426c Mon Sep 17 00:00:00 2001 From: xjxia Date: Tue, 10 Sep 2024 16:36:27 +0800 Subject: [PATCH] feat(dbm-services): dbha optimize spider switch close #6813 --- .../dbha/ha-module/constvar/constant.go | 4 ++ .../dbmodule/dbmysql/MySQL_common_switch.go | 2 + .../dbmysql/SpiderStorageLayer_switch.go | 11 ++++ .../common/dbha/ha-module/monitor/monitor.go | 66 ++++++++++++------- 4 files changed, 60 insertions(+), 23 deletions(-) diff --git a/dbm-services/common/dbha/ha-module/constvar/constant.go b/dbm-services/common/dbha/ha-module/constvar/constant.go index b4e2aab7d9..f466c185e8 100644 --- a/dbm-services/common/dbha/ha-module/constvar/constant.go +++ b/dbm-services/common/dbha/ha-module/constvar/constant.go @@ -413,6 +413,10 @@ const ( SlaveIpKey = "slave_ip" // SlavePortKey use to set slave port SlavePortKey = "slave_port" + //BinlogFile consistent switch binlog file + BinlogFile = "binlog_file" + //BinlogPos consistent switch binlog pos + BinlogPos = "binlog_pos" ) // checksum sql diff --git a/dbm-services/common/dbha/ha-module/dbmodule/dbmysql/MySQL_common_switch.go b/dbm-services/common/dbha/ha-module/dbmodule/dbmysql/MySQL_common_switch.go index af4ed16a81..a44f4f9a8e 100644 --- a/dbm-services/common/dbha/ha-module/dbmodule/dbmysql/MySQL_common_switch.go +++ b/dbm-services/common/dbha/ha-module/dbmodule/dbmysql/MySQL_common_switch.go @@ -616,6 +616,8 @@ func (ins *MySQLCommonSwitch) ResetSlave() (string, uint64, error) { return "", 0, fmt.Errorf("reset slave failed. err:%s", err.Error()) } log.Logger.Infof("executed %s on %s:%d successd", resetSql, slaveIp, slavePort) + ins.SetInfo(constvar.BinlogFile, masterStatus.File) + ins.SetInfo(constvar.BinlogPos, masterStatus.Position) return masterStatus.File, masterStatus.Position, nil } diff --git a/dbm-services/common/dbha/ha-module/dbmodule/dbmysql/SpiderStorageLayer_switch.go b/dbm-services/common/dbha/ha-module/dbmodule/dbmysql/SpiderStorageLayer_switch.go index 8e6f87cf94..a3a3aa19cb 100644 --- a/dbm-services/common/dbha/ha-module/dbmodule/dbmysql/SpiderStorageLayer_switch.go +++ b/dbm-services/common/dbha/ha-module/dbmodule/dbmysql/SpiderStorageLayer_switch.go @@ -105,6 +105,17 @@ func (ins *SpiderStorageSwitch) DoSwitch() error { _ = primaryConn.Close() }() + ins.ReportLogs(constvar.InfoResult, "try to reset slave") + binlogFile, binlogPosition, err := ins.ResetSlave() + if err != nil { + ins.ReportLogs(constvar.FailResult, fmt.Sprintf("reset slave failed:%s", err.Error())) + return fmt.Errorf("reset slave failed") + } + ins.StandBySlave.BinlogFile = binlogFile + ins.StandBySlave.BinlogPosition = binlogPosition + ins.ReportLogs(constvar.InfoResult, fmt.Sprintf("reset slave success, consistent binlog info:%s,%s", + ins.StandBySlave.BinlogFile, ins.StandBySlave.BinlogPosition)) + ins.ReportLogs(constvar.InfoResult, fmt.Sprintf("try to update route from old master to new master")) alterSQL := fmt.Sprintf(AlterNodeFormat, oldMaster.ServerName, newMaster.Host, newMaster.UserName, newMaster.Password, newMaster.Port) diff --git a/dbm-services/common/dbha/ha-module/monitor/monitor.go b/dbm-services/common/dbha/ha-module/monitor/monitor.go index a6cf7ca499..6e702b11c2 100644 --- a/dbm-services/common/dbha/ha-module/monitor/monitor.go +++ b/dbm-services/common/dbha/ha-module/monitor/monitor.go @@ -16,14 +16,16 @@ import ( // SwitchMonitor switch monitor information type SwitchMonitor struct { - ServerIp string - ServerPort int - Bzid string - MachineType string - Role string - Status string - Cluster string - IDC string + ServerIp string + ServerPort int + Bzid string + MachineType string + Role string + Status string + Cluster string + IDC string + BinlogFile string + BinlogPosition uint64 } // DetectMonitor detect monitor information @@ -107,6 +109,12 @@ func MonitorSend(content string, info MonitorInfo) error { addDimension["cluster"] = info.Switch.Cluster addDimension["machine_type"] = info.Switch.MachineType addDimension["idc"] = info.Switch.IDC + if info.EventName == constvar.DBHAEventMysqlSwitchSucc && + (info.Switch.Role == constvar.TenDBStorageMaster || + info.Switch.Role == constvar.TenDBClusterStorageMaster) { + addDimension["binlog_file"] = info.Switch.BinlogFile + addDimension["binlog_pos"] = info.Switch.BinlogPosition + } } else if info.MonitorInfoType == constvar.MonitorInfoDetect { // detect monitor information dimension add addDimension["appid"] = info.Detect.Bzid @@ -132,6 +140,22 @@ func MonitorSend(content string, info MonitorInfo) error { // GetMonitorInfoBySwitch get MonitorInfo by switch instance func GetMonitorInfoBySwitch(ins dbutil.DataBaseSwitch, succ bool) MonitorInfo { var eventName string + addr, port := ins.GetAddress() + monInfo := MonitorInfo{ + EventName: eventName, + MonitorInfoType: constvar.MonitorInfoSwitch, + Switch: SwitchMonitor{ + ServerIp: addr, + ServerPort: port, + Bzid: ins.GetApp(), + MachineType: ins.GetMetaType(), + Role: ins.GetRole(), + Status: ins.GetStatus(), + Cluster: ins.GetCluster(), + IDC: strconv.Itoa(ins.GetIdcID()), + }, + } + switch ins.GetMetaType() { case constvar.RedisMetaType, constvar.TwemproxyMetaType, constvar.TendisSSDMetaType: @@ -150,6 +174,15 @@ func GetMonitorInfoBySwitch(ins dbutil.DataBaseSwitch, succ bool) MonitorInfo { constvar.TenDBClusterStorageType, constvar.TenDBClusterProxyType: if succ { eventName = constvar.DBHAEventMysqlSwitchSucc + if ins.GetRole() == constvar.TenDBStorageMaster || + ins.GetRole() == constvar.TenDBClusterStorageMaster { + if ok, file := ins.GetInfo(constvar.BinlogFile); ok { + monInfo.Switch.BinlogFile = file.(string) + } + if ok, pos := ins.GetInfo(constvar.BinlogPos); ok { + monInfo.Switch.BinlogPosition = pos.(uint64) + } + } } else { eventName = constvar.DBHAEventMysqlSwitchErr } @@ -166,22 +199,9 @@ func GetMonitorInfoBySwitch(ins dbutil.DataBaseSwitch, succ bool) MonitorInfo { eventName = constvar.DBHAEventMysqlSwitchErr } } + monInfo.EventName = eventName - addr, port := ins.GetAddress() - return MonitorInfo{ - EventName: eventName, - MonitorInfoType: constvar.MonitorInfoSwitch, - Switch: SwitchMonitor{ - ServerIp: addr, - ServerPort: port, - Bzid: ins.GetApp(), - MachineType: ins.GetMetaType(), - Role: ins.GetRole(), - Status: ins.GetStatus(), - Cluster: ins.GetCluster(), - IDC: strconv.Itoa(ins.GetIdcID()), - }, - } + return monInfo } // GetMonitorInfoByDetect get MonitorInfo by detect instance