Skip to content

Commit

Permalink
add unique switch hash id for switch_queue
Browse files Browse the repository at this point in the history
  • Loading branch information
xjxia committed Nov 20, 2024
1 parent c80b139 commit 9dbbf14
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 13 deletions.
2 changes: 1 addition & 1 deletion dbm-services/common/dbha/ha-module/client/hadb.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 3 additions & 0 deletions dbm-services/common/dbha/ha-module/gm/gcm.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gm

import (
"dbm-services/common/dbha/ha-module/util"
"dbm-services/common/dbha/hadb-api/model"
"fmt"
"time"
Expand Down Expand Up @@ -205,6 +206,8 @@ func (gcm *GCM) InsertSwitchQueue(instance dbutil.DataBaseSwitch) error {
SwitchStartTime: &currentTime,
DbRole: instance.GetRole(),
ConfirmResult: doubleCheckInfo,
SwitchHashID: util.GenerateHash(fmt.Sprintf("%#%d", ip, port),
int64(max(300, gcm.Conf.GMConf.ReportInterval))),
},
}

Expand Down
2 changes: 1 addition & 1 deletion dbm-services/common/dbha/ha-module/gm/gm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
27 changes: 19 additions & 8 deletions dbm-services/common/dbha/ha-module/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"bytes"
"encoding/json"
"fmt"
"hash/crc32"
"hash/fnv"
"net"
"os/exec"
"reflect"
Expand Down Expand Up @@ -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,
Expand All @@ -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()
Expand Down Expand Up @@ -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()
}
5 changes: 3 additions & 2 deletions dbm-services/common/dbha/hadb-api/model/HASwitchQueue.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand All @@ -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
Expand Down

0 comments on commit 9dbbf14

Please sign in to comment.