Skip to content

Commit

Permalink
spider switch should do two-phase update
Browse files Browse the repository at this point in the history
  • Loading branch information
xjxia committed Oct 12, 2024
1 parent a2fab90 commit 8940aa1
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ const (
GetPrimarySQL = "TDBCTL GET PRIMARY"
// GetRouteSQL TendbCluster used to get all routes
GetRouteSQL = "SELECT Server_name, Host, Username, Password, Port, Wrapper FROM mysql.servers"
//FlushRouteSQL TendbCluster used to flush routes
FlushRouteSQL = "TDBCTL FLUSH ROUTING FORCE"
//FlushRouteForceSQL TendbCluster used to flush routes
FlushRouteForceSQL = "TDBCTL FLUSH ROUTING FORCE"
// ForcePrimarySQL enable primary tdbctl force
ForcePrimarySQL = "TDBCTL ENABLE PRIMARY FORCE"
// AlterNodeFormat TDBCTL alter node sql format
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,9 @@ func (ins *SpiderProxyLayerSwitch) DoSwitch() error {

//5. flush routing
ins.ReportLogs(constvar.InfoResult, "flush route table")
if _, err = primaryConn.Exec(FlushRouteSQL); err != nil {
if _, err = primaryConn.Exec(FlushRouteForceSQL); err != nil {
ins.ReportLogs(constvar.FailResult, fmt.Sprintf("flush route failed:%s", err.Error()))
return fmt.Errorf("execute[%s] failed:%s", FlushRouteSQL, err.Error())
return fmt.Errorf("execute[%s] failed:%s", FlushRouteForceSQL, err.Error())
}
ins.ReportLogs(constvar.SuccessResult, "flush ok, switch success")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,25 @@ func (ins *SpiderStorageSwitch) DoSwitch() error {
_ = primaryConn.Close()
}()

ins.ReportLogs(constvar.InfoResult, fmt.Sprintf("one-phase: update route from old master to 1.1.1.1"))
alterSQL := fmt.Sprintf(AlterNodeFormat, oldMaster.ServerName, "1.1.1.1",
newMaster.UserName, newMaster.Password, newMaster.Port)
if result, err := primaryConn.Exec(alterSQL); err != nil {
return fmt.Errorf("TDBCTL ALTER NODE to 1.1.1.1 failed:%s", err.Error())
} else {
if rowCnt, _ := result.RowsAffected(); rowCnt == 0 {
//TODO: current tdbctl server's rowsAffected incorrect. Next version, should return error instead
log.Logger.Warnf("TDBCTL ALTER NODE to 1.1.1.1 failed, rowsAffected 0")
}
}
ins.ReportLogs(constvar.InfoResult, "one-phase: update route info to 1.1.1.1 success, do flush next")

if _, err = primaryConn.Exec(FlushRouteForceSQL); err != nil {
ins.ReportLogs(constvar.FailResult, fmt.Sprintf("one-phase flush route failed:%s", err.Error()))
return fmt.Errorf("execute[%s] failed:%s", FlushRouteForceSQL, err.Error())
}
ins.ReportLogs(constvar.InfoResult, "one-phase: flush 1.1.1.1 to all spider success")

ins.ReportLogs(constvar.InfoResult, "try to reset slave")
binlogFile, binlogPosition, err := ins.ResetSlave()
if err != nil {
Expand All @@ -116,8 +135,8 @@ func (ins *SpiderStorageSwitch) DoSwitch() error {
ins.ReportLogs(constvar.InfoResult, fmt.Sprintf("reset slave success, consistent binlog info:%s,%d",
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,
ins.ReportLogs(constvar.InfoResult, fmt.Sprintf("two-phase: update route from old master to new master"))
alterSQL = fmt.Sprintf(AlterNodeFormat, oldMaster.ServerName, newMaster.Host,
newMaster.UserName, newMaster.Password, newMaster.Port)
if result, err := primaryConn.Exec(alterSQL); err != nil {
return fmt.Errorf("execute TDBCTL ALTER NODE failed:%s", err.Error())
Expand All @@ -127,11 +146,11 @@ func (ins *SpiderStorageSwitch) DoSwitch() error {
log.Logger.Warnf("execute TDBCTL ALTER NODE failed, rowsAffected 0")
}
}
ins.ReportLogs(constvar.InfoResult, "update route info success, do flush next")
ins.ReportLogs(constvar.InfoResult, "two-phase: update route info success, do flush next")

if _, err = primaryConn.Exec(FlushRouteSQL); err != nil {
if _, err = primaryConn.Exec(FlushRouteForceSQL); err != nil {
ins.ReportLogs(constvar.FailResult, fmt.Sprintf("flush route failed:%s", err.Error()))
return fmt.Errorf("execute[%s] failed:%s", FlushRouteSQL, err.Error())
return fmt.Errorf("execute[%s] failed:%s", FlushRouteForceSQL, err.Error())
}
ins.ReportLogs(constvar.InfoResult, "flush route ok, switch success")

Expand Down

0 comments on commit 8940aa1

Please sign in to comment.