Skip to content

Commit

Permalink
only one address under domain should skip release
Browse files Browse the repository at this point in the history
this strategy only suitable for MySQL
  • Loading branch information
xjxia committed Nov 14, 2024
1 parent d645697 commit e25651b
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 9 deletions.
26 changes: 26 additions & 0 deletions dbm-services/common/dbha/ha-module/client/name_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,32 @@ func (c *NameServiceClient) GetDomainInfoByIp(ip string) ([]DomainInfo, error) {
return res.Detail, nil
}

// GetAddressNumberByDomain get address number under this domain name
func (c *NameServiceClient) GetAddressNumberByDomain(domainName string) (int, error) {
var res DomainRes
req := map[string]interface{}{
"db_cloud_token": c.Conf.BKConf.BkToken,
"bk_cloud_id": c.CloudId,
"domain_name": []string{domainName},
}

response, err := c.DoNew(http.MethodPost,
c.SpliceUrlByPrefix(c.Conf.UrlPre, constvar.GetDomainInfoUrl, ""), req, nil)
if err != nil {
return 0, err
}
if response.Code != 0 {
return 0, fmt.Errorf("%s failed, return code:%d, msg:%s", util.AtWhere(), response.Code, response.Msg)
}

err = json.Unmarshal(response.Data, &res)
if err != nil {
return 0, err
}

return res.RowsNum, nil
}

// GetDomainInfoByDomain get address info from dns by domain
func (c *NameServiceClient) GetDomainInfoByDomain(domainName string) ([]DomainInfo, error) {
var res DomainRes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,20 @@ func (ins *MySQLProxySwitch) CheckSwitch() (bool, error) {
// DoSwitch mysql-proxy do switch
// delete ip under the entry
func (ins *MySQLProxySwitch) DoSwitch() error {
ins.ReportLogs(constvar.InfoResult, fmt.Sprintf("try to release ip[%s] from all cluster entery", ins.Ip))
return ins.DeleteNameService(ins.Entry)
//1. delete name service
isSingle, err := ins.SingleAddressUnderDomain(ins.Entry)
if err != nil {
ins.ReportLogs(constvar.FailResult, fmt.Sprintf("check whether single address under domain failed:%s",
err.Error()))
return err
}
if isSingle {
return fmt.Errorf("only single address under this domain, skip release domain")
} else {
ins.ReportLogs(constvar.InfoResult, fmt.Sprintf("try to release ip[%s#%d] from all cluster entry",
ins.Ip, ins.Port))
return ins.DeleteNameService(ins.Entry)
}
}

// ShowSwitchInstanceInfo display switch proxy info
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,14 +229,14 @@ type TdbctlNodes struct {
ReplicationInfo string `gorm:"column:REPLICATION_INFO;NOT NULL"`
}

// ReplicationInfo replication info in information_schema.TDBCTL_NODES's ReplicationInfo field
// ReplicationInfo replication info in information_schema.TDBCTL_NODES's ReplicationInfo field
type ReplicationInfo struct {
MasterHost string `json:"Master_Host"`
MasterPort int `json:"Master_Port"`
RelayMasterLogFile string `json:"Relay_Master_Log_File"`
SlaveIoRunning string `json:"Slave_IO_Running"`
SlaveSqlRunning string `json:"Slave_SQL_Running"`
//TODO: in the future, type should change to uint64
//TODO: in the future, type should change to uint64
ExecMasterLogPos string `json:"Exec_Master_Log_Pos"`
}

Expand Down Expand Up @@ -826,9 +826,9 @@ func (ins *SpiderCommonSwitch) RemoveNodeFromRoute(primaryConn *sql.DB, host str
// GetPrimary found primary node from any connected tdbctl node's route table
// If no primary found, return error.
// Any blow condition could get primary success
// 1) There is only one node: PrimaryRole, StatusOnline
// 1) There is only one node: PrimaryRole, StatusOnline
// 2) No primary role found, and all alive SecondaryRole node's ReplicationMaster are the same,
// then thought the ReplicationMaster must be the Primary node's ServerName
// then thought the ReplicationMaster must be the Primary node's ServerName
func (ins *SpiderCommonSwitch) GetPrimary() error {
replicaServer := ""
for _, spider := range ins.SpiderNodes {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,11 +211,21 @@ func (ins *SpiderProxyLayerSwitch) DoSwitch() error {
)

//1. delete name service
ins.ReportLogs(constvar.InfoResult, fmt.Sprintf("try to release ip[%s#%d] from all domain entry",
ins.Ip, ins.Port))
if err := ins.DeleteNameService(ins.Entry); err != nil {
isSingle, err := ins.SingleAddressUnderDomain(ins.Entry)
if err != nil {
ins.ReportLogs(constvar.FailResult, fmt.Sprintf("check whether single address under domain failed:%s",
err.Error()))
return err
}
if isSingle {
return fmt.Errorf("only single address under this domain, skip release domain")
} else {
ins.ReportLogs(constvar.InfoResult, fmt.Sprintf("try to release ip[%s#%d] from all domain entry",
ins.Ip, ins.Port))
if err := ins.DeleteNameService(ins.Entry); err != nil {
return err
}
}

//2. set all spider nodes
if err := ins.SetSpiderNodes(); err != nil {
Expand Down
24 changes: 24 additions & 0 deletions dbm-services/common/dbha/ha-module/dbutil/db_switch.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,30 @@ func (ins *BaseSwitch) GetInfo(infoKey string) (bool, interface{}) {
}
}

// SingleAddressUnderDomain check whether only one address under domain
// if only one address under dns entry, return true
// if no dns entry found, return false
func (ins *BaseSwitch) SingleAddressUnderDomain(entry BindEntry) (bool, error) {
if entry.Dns == nil {
return false, nil
}
conf := ins.Config
dnsClient := client.NewNameServiceClient(&conf.NameServices.DnsConf, conf.GetCloudId())
for _, dns := range entry.Dns {
number, err := dnsClient.GetAddressNumberByDomain(dns.DomainName)
if err != nil {
return false, err
}
ins.ReportLogs(constvar.InfoResult, fmt.Sprintf("found %d address under domain %s",
number, dns.DomainName))
if number == 1 {
return true, nil
}
}

return false, nil
}

// DeleteNameService delete broken-down ip from entry
func (ins *BaseSwitch) DeleteNameService(entry BindEntry) error {
//flag refer to whether release name-service success
Expand Down

0 comments on commit e25651b

Please sign in to comment.