From 9dbbf149f6802205dfce7ccee5d096008351d6d8 Mon Sep 17 00:00:00 2001 From: xjxia Date: Wed, 20 Nov 2024 17:04:43 +0800 Subject: [PATCH] add unique switch hash id for switch_queue --- .../common/dbha/ha-module/client/hadb.go | 2 +- .../dbmodule/dbmysql/MySQL_common_switch.go | 2 +- dbm-services/common/dbha/ha-module/gm/gcm.go | 3 +++ dbm-services/common/dbha/ha-module/gm/gm.go | 2 +- .../common/dbha/ha-module/util/util.go | 27 +++++++++++++------ .../dbha/hadb-api/model/HASwitchQueue.go | 5 ++-- 6 files changed, 28 insertions(+), 13 deletions(-) diff --git a/dbm-services/common/dbha/ha-module/client/hadb.go b/dbm-services/common/dbha/ha-module/client/hadb.go index f208f54666..936c6dbbfa 100644 --- a/dbm-services/common/dbha/ha-module/client/hadb.go +++ b/dbm-services/common/dbha/ha-module/client/hadb.go @@ -239,7 +239,7 @@ func (c *HaDBClient) ReportDBStatus(app, agentIp, ip string, port int, dbType, s return nil } -// ReportHaLogRough report ha logs +// ReportHaLogRough report ha logs without return func (c *HaDBClient) ReportHaLogRough(monIP, app, ip string, port int, module, comment string) { _, _ = c.ReportHaLog(monIP, app, ip, port, module, comment) } 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 01305ef2c1..784b430359 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 @@ -526,7 +526,7 @@ func (ins *MySQLCommonSwitch) CheckSlaveSlow(ignoreDelay bool) error { } binlogSizeMByte := maxBinlogSize.VariableValue / (1024 * 1024) - log.Logger.Infof("the slave max_binlog_size value is %d M!", binlogSizeMByte) + log.Logger.Infof("the slave max_binlog_size value is %dM!", binlogSizeMByte) status, err := GetSlaveStatus(db) if err != nil { diff --git a/dbm-services/common/dbha/ha-module/gm/gcm.go b/dbm-services/common/dbha/ha-module/gm/gcm.go index 20900e0220..423f62e00b 100644 --- a/dbm-services/common/dbha/ha-module/gm/gcm.go +++ b/dbm-services/common/dbha/ha-module/gm/gcm.go @@ -1,6 +1,7 @@ package gm import ( + "dbm-services/common/dbha/ha-module/util" "dbm-services/common/dbha/hadb-api/model" "fmt" "time" @@ -205,6 +206,8 @@ func (gcm *GCM) InsertSwitchQueue(instance dbutil.DataBaseSwitch) error { SwitchStartTime: ¤tTime, DbRole: instance.GetRole(), ConfirmResult: doubleCheckInfo, + SwitchHashID: util.GenerateHash(fmt.Sprintf("%#%d", ip, port), + int64(max(300, gcm.Conf.GMConf.ReportInterval))), }, } diff --git a/dbm-services/common/dbha/ha-module/gm/gm.go b/dbm-services/common/dbha/ha-module/gm/gm.go index 582ae64928..fb76dac801 100644 --- a/dbm-services/common/dbha/ha-module/gm/gm.go +++ b/dbm-services/common/dbha/ha-module/gm/gm.go @@ -20,7 +20,7 @@ type DoubleCheckInstanceInfo struct { ConfirmTime time.Time //double check result ResultInfo string - //gmm double check id + //gmm double check id, ha_gm_logs's uid CheckID int64 } diff --git a/dbm-services/common/dbha/ha-module/util/util.go b/dbm-services/common/dbha/ha-module/util/util.go index 2d0efd7708..d3d6a40d97 100644 --- a/dbm-services/common/dbha/ha-module/util/util.go +++ b/dbm-services/common/dbha/ha-module/util/util.go @@ -5,7 +5,7 @@ import ( "bytes" "encoding/json" "fmt" - "hash/crc32" + "hash/fnv" "net" "os/exec" "reflect" @@ -73,11 +73,6 @@ func HostCheck(host string) bool { return true } -// CRC32 TODO -func CRC32(str string) uint32 { - return crc32.ChecksumIEEE([]byte(str)) -} - // CheckRedisErrIsAuthFail check if the return error of // // redis api is authentication failure, @@ -96,8 +91,7 @@ func CheckRedisErrIsAuthFail(err error) bool { return false } -// CheckSSHErrIsAuthFail check if the the return error of ssh api -// +// CheckSSHErrIsAuthFail check if the ssh return error of ssh api // is authentication failure. func CheckSSHErrIsAuthFail(err error) bool { errInfo := err.Error() @@ -157,3 +151,20 @@ func GraceStructString(v interface{}) string { } return string(data) } + +// GenerateHash generates a consistent hash value for a given factor within a specified time window (in seconds). +func GenerateHash(factor string, timeWindow int64) uint32 { + // Get the current Unix timestamp + now := time.Now().Unix() + + // Calculate the start of the time window + windowStart := now - (now % timeWindow) + + // Combine the factor and windowStart into a single input string + input := fmt.Sprintf("%s:%d", factor, windowStart) + + // Use FNV-1a to hash the input string + hasher := fnv.New32a() + _, _ = hasher.Write([]byte(input)) + return hasher.Sum32() +} diff --git a/dbm-services/common/dbha/hadb-api/model/HASwitchQueue.go b/dbm-services/common/dbha/hadb-api/model/HASwitchQueue.go index 97de7b02c2..de42984833 100644 --- a/dbm-services/common/dbha/hadb-api/model/HASwitchQueue.go +++ b/dbm-services/common/dbha/hadb-api/model/HASwitchQueue.go @@ -19,8 +19,8 @@ type HASwitchQueue struct { Uid int64 `gorm:"column:uid;type:bigint;primary_key;AUTO_INCREMENT" json:"uid,omitempty"` CheckID int64 `gorm:"column:check_id;type:bigint;" json:"check_id,omitempty"` App string `gorm:"column:app;type:varchar(32);index:idx_app_ip_port" json:"app,omitempty"` - IP string `gorm:"column:ip;type:varchar(32);index:idx_app_ip_port;NOT NULL" json:"ip,omitempty"` - Port int `gorm:"column:port;type:int(11);index:idx_app_ip_port;NOT NULL" json:"port,omitempty"` + IP string `gorm:"column:ip;type:varchar(32);uniqueIndex:uniq_ip_port_hashid;index:idx_app_ip_port;NOT NULL" json:"ip,omitempty"` + Port int `gorm:"column:port;type:int(11);uniqueIndex:uniq_ip_port_hashid;index:idx_app_ip_port;NOT NULL" json:"port,omitempty"` ConfirmCheckTime *time.Time `gorm:"column:confirm_check_time;type:datetime;default:CURRENT_TIMESTAMP" json:"confirm_check_time,omitempty"` DbRole string `gorm:"column:db_role;type:varchar(32);NOT NULL" json:"db_role,omitempty"` SlaveIP string `gorm:"column:slave_ip;type:varchar(32)" json:"slave_ip,omitempty"` @@ -35,6 +35,7 @@ type HASwitchQueue struct { IdcID int `gorm:"column:idc_id;type:int(11)" json:"idc_id,omitempty"` CloudID int `gorm:"column:cloud_id;type:int(11);default:0" json:"cloud_id,omitempty"` Cluster string `gorm:"column:cluster;type:varchar(64)" json:"cluster,omitempty"` + SwitchHashID uint32 `gorm:"column:switch_hash_id;type:bigint;uniqueIndex:uniq_ip_port_hashid" json:"switch_hash_id,omitempty"` } // TableName TODO