Skip to content

Commit 781742b

Browse files
XH-2019ylgeeker
authored andcommitted
feat(backend): Add switching logic for Spider Slave nodes. issue: #15620
1 parent 52bd79f commit 781742b

File tree

3 files changed

+33
-12
lines changed

3 files changed

+33
-12
lines changed

dbm-services/common/dbha-v2/internal/analysis/switcher/mysql.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,12 @@ func (m *Mysql) Switch(ctx context.Context, req *Request) *Response {
120120
swInst.SetSwitchLogger(switchLoggers)
121121

122122
logger.Info("start to switch the single mysql instance: %s", instKey)
123-
if swErr := SwitchSingleInstance(swInst); swErr != nil {
124-
logger.Warn("failed to switch the single mysql instance: %s, errmsg: %s", instKey, swErr)
123+
if switchSuccess, swErr := SwitchSingleInstance(swInst); !switchSuccess {
124+
errStr := "nil"
125+
if swErr != nil {
126+
errStr = swErr.Error()
127+
}
128+
logger.Warn("failed to switch the single mysql instance: %s, errmsg: %s", instKey, errStr)
125129
rsp.MySqlFailureInsts[instKey] = inst
126130
continue
127131
}

dbm-services/common/dbha-v2/internal/analysis/switcher/switchable_instance.go

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,12 @@ type SwitchableInstance interface {
7777
}
7878

7979
// SwitchSingleInstance executes the standardized switching procedure for a single database instance.
80-
func SwitchSingleInstance(ins SwitchableInstance) (retErr error) {
80+
func SwitchSingleInstance(ins SwitchableInstance) (switchSuccess bool, retErr error) {
8181
ins.ReportLogf(SwitchInfo, "start to switch single instance: %s", ins.GetInstanceInfo())
8282

8383
// rollback when error occurs
8484
defer func() {
85-
if retErr == nil {
85+
if switchSuccess {
8686
ins.ReportLogf(SwitchSuccess, "successfully switch single instance: %s", ins.GetInstanceInfo())
8787
return
8888
}
@@ -98,14 +98,14 @@ func SwitchSingleInstance(ins SwitchableInstance) (retErr error) {
9898
if (ins.GetStatus() != dbm.Running) && (ins.GetStatus() != dbm.Available) {
9999
retErr = gerrors.Newf(gerrors.Failure, "pre-status check unpass for wrong status:%s", ins.GetStatus())
100100
ins.ReportLogf(SwitchFail, "%s", retErr.Error())
101-
return
101+
return false, retErr
102102
}
103103
ins.ReportLogf(SwitchInfo, "pre-status check pass with status:%s", ins.GetStatus())
104104

105105
if err := ins.SetInstanceUnavailable(); err != nil {
106106
retErr = gerrors.Newf(gerrors.Failure, "failed to set instance unavailable: %s", err.Error())
107107
ins.ReportLogf(SwitchFail, "%s", retErr.Error())
108-
return
108+
return false, retErr
109109
}
110110
ins.ReportLogf(SwitchInfo, "successfully set instance unavailable")
111111

@@ -115,36 +115,38 @@ func SwitchSingleInstance(ins SwitchableInstance) (retErr error) {
115115
ins.ReportLogf(SwitchInfo, "check result before switch: switch required")
116116
case SwitchNotNeeded:
117117
ins.ReportLogf(SwitchInfo, "check result before switch: no need to switch")
118-
return
118+
return true, nil
119119
default:
120120
errMsg := "check result before switch: check unpass"
121121
if checkErr != nil {
122122
errMsg += fmt.Sprintf(", errmsg: %s", checkErr.Error())
123123
}
124+
125+
retErr = gerrors.Newf(gerrors.Failure, "%s", errMsg)
124126
ins.ReportLogf(SwitchFail, "%s", errMsg)
125-
return
127+
return false, retErr
126128
}
127129

128130
if err := ins.DoSwitch(); err != nil {
129131
retErr = gerrors.Newf(gerrors.Failure, "failed to do switch: %s", err.Error())
130132
ins.ReportLogf(SwitchFail, "%s", retErr.Error())
131-
return
133+
return false, retErr
132134
}
133135
ins.ReportLogf(SwitchInfo, "successfully do switch")
134136

135137
if err := ins.UpdateMetaInfo(); err != nil {
136138
retErr = gerrors.Newf(gerrors.Failure, "failed to update meta info: %s", err.Error())
137139
ins.ReportLogf(SwitchFail, "%s", retErr.Error())
138-
return
140+
return false, retErr
139141
}
140142
ins.ReportLogf(SwitchInfo, "successfully update meta info")
141143

142144
if err := ins.DoFinal(); err != nil {
143145
retErr = gerrors.Newf(gerrors.Failure, "failed to do final step: %s", err.Error())
144146
ins.ReportLogf(SwitchFail, "%s", retErr.Error())
145-
return
147+
return false, retErr
146148
}
147149
ins.ReportLogf(SwitchInfo, "successfully do final step")
148150

149-
return
151+
return true, nil
150152
}

dbm-services/common/dbha-v2/internal/analysis/switcher/tendbcluster_switch_instance.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,21 @@ func (sw *TenDBClusterSpiderSwitchInstance) UnmarshalTdbctlReplInfo(replicationI
588588
return replInfo, nil
589589
}
590590

591+
// CheckBeforeSwitch check slave before switch
592+
func (sw *TenDBClusterSpiderSwitchInstance) CheckBeforeSwitch() (SwitchCheckCode, error) {
593+
switch sw.SpiderRole {
594+
case dbm.TenDBClusterSpiderMaster:
595+
return SwitchRequired, nil
596+
case dbm.TenDBClusterSpiderSlave:
597+
sw.ReportLogf(SwitchInfo, "this is a spider slave node, no need to check")
598+
return SwitchNotNeeded, nil
599+
default:
600+
err := gerrors.Newf(gerrors.Failure, "invalid instance role: %s", sw.SpiderRole)
601+
sw.ReportLogf(SwitchWarn, "%s", err.Error())
602+
return SwitchCheckUnpass, err
603+
}
604+
}
605+
591606
// TdbctlEnablePrimary connect tdbctl and execute TDBCTL ENABLE PRIMARY [FORCE]
592607
func (sw *TenDBClusterSpiderSwitchInstance) TdbctlEnablePrimary(tdbctlHost string, tdbctlPort int, force bool) error {
593608
tdbctlDB, connErr := sw.ConnectTdbctlNode(tdbctlHost, tdbctlPort)

0 commit comments

Comments
 (0)