|
|
@ -58,7 +58,7 @@ func saveCommitted(m *TransGlobalModel) { |
|
|
|
|
|
|
|
|
var TransProcessedTestChan chan string = nil // 用于测试时,通知处理结束
|
|
|
var TransProcessedTestChan chan string = nil // 用于测试时,通知处理结束
|
|
|
|
|
|
|
|
|
func WaitTransCommitted(gid string) { |
|
|
func WaitTransProcessed(gid string) { |
|
|
id := <-TransProcessedTestChan |
|
|
id := <-TransProcessedTestChan |
|
|
for id != gid { |
|
|
for id != gid { |
|
|
logrus.Errorf("-------id %s not match gid %s", id, gid) |
|
|
logrus.Errorf("-------id %s not match gid %s", id, gid) |
|
|
@ -66,19 +66,19 @@ func WaitTransCommitted(gid string) { |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func ProcessCommitted(trans *TransGlobalModel) { |
|
|
func ProcessTrans(trans *TransGlobalModel) { |
|
|
err := innerProcessCommitted(trans) |
|
|
err := innerProcessTrans(trans) |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
logrus.Errorf("process committed error: %s", err.Error()) |
|
|
logrus.Errorf("process trans ignore error: %s", err.Error()) |
|
|
} |
|
|
} |
|
|
if TransProcessedTestChan != nil { |
|
|
if TransProcessedTestChan != nil { |
|
|
TransProcessedTestChan <- trans.Gid |
|
|
TransProcessedTestChan <- trans.Gid |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
func innerProcessCommitted(trans *TransGlobalModel) (rerr error) { |
|
|
func innerProcessTrans(trans *TransGlobalModel) (rerr error) { |
|
|
branches := []TransBranchModel{} |
|
|
branches := []TransBranchModel{} |
|
|
db := dbGet() |
|
|
db := dbGet() |
|
|
db.Must().Order("id asc").Find(&branches) |
|
|
db.Must().Where("gid=?", trans.Gid).Order("id asc").Find(&branches) |
|
|
if trans.TransType == "saga" { |
|
|
if trans.TransType == "saga" { |
|
|
return innerProcessCommittedSaga(trans, db, branches) |
|
|
return innerProcessCommittedSaga(trans, db, branches) |
|
|
} else if trans.TransType == "xa" { |
|
|
} else if trans.TransType == "xa" { |
|
|
@ -89,34 +89,70 @@ func innerProcessCommitted(trans *TransGlobalModel) (rerr error) { |
|
|
|
|
|
|
|
|
func innerProcessCommittedXa(trans *TransGlobalModel, db *common.MyDb, branches []TransBranchModel) error { |
|
|
func innerProcessCommittedXa(trans *TransGlobalModel, db *common.MyDb, branches []TransBranchModel) error { |
|
|
gid := trans.Gid |
|
|
gid := trans.Gid |
|
|
for _, branch := range branches { |
|
|
if trans.Status == "finished" { |
|
|
if branch.Status == "finished" { |
|
|
return nil |
|
|
continue |
|
|
} |
|
|
} |
|
|
if trans.Status == "committed" { |
|
|
db.Must().Model(&TransGlobalModel{}).Where("gid=?", gid).Update("gid", gid) // 更新update_time,避免被定时任务再次
|
|
|
for _, branch := range branches { |
|
|
resp, err := common.RestyClient.R().SetBody(M{ |
|
|
if branch.Status == "finished" { |
|
|
"branch": branch.Branch, |
|
|
continue |
|
|
"action": "commit", |
|
|
} |
|
|
"gid": branch.Gid, |
|
|
db.Must().Model(&TransGlobalModel{}).Where("gid=?", gid).Update("gid", gid) // 更新update_time,避免被定时任务再次
|
|
|
}).Post(branch.Url) |
|
|
resp, err := common.RestyClient.R().SetBody(M{ |
|
|
if err != nil { |
|
|
"branch": branch.Branch, |
|
|
return err |
|
|
"action": "commit", |
|
|
} |
|
|
"gid": branch.Gid, |
|
|
body := resp.String() |
|
|
}).Post(branch.Url) |
|
|
if !strings.Contains(body, "SUCCESS") { |
|
|
if err != nil { |
|
|
return fmt.Errorf("bad response: %s", body) |
|
|
return err |
|
|
|
|
|
} |
|
|
|
|
|
body := resp.String() |
|
|
|
|
|
if !strings.Contains(body, "SUCCESS") { |
|
|
|
|
|
return fmt.Errorf("bad response: %s", body) |
|
|
|
|
|
} |
|
|
|
|
|
writeTransLog(gid, "step finished", "finished", branch.Branch, "") |
|
|
|
|
|
db.Must().Model(&branch).Where("status=?", "prepared").Updates(M{ |
|
|
|
|
|
"status": "finished", |
|
|
|
|
|
"finish_time": time.Now(), |
|
|
|
|
|
}) |
|
|
} |
|
|
} |
|
|
writeTransLog(gid, "step finished", "finished", branch.Branch, "") |
|
|
writeTransLog(gid, "xa finished", "finished", "", "") |
|
|
db.Must().Model(&branch).Where("status=?", "prepared").Updates(M{ |
|
|
db.Must().Model(&TransGlobalModel{}).Where("gid=? and status=?", gid, "committed").Updates(M{ |
|
|
"status": "finished", |
|
|
"status": "finished", |
|
|
"finish_time": time.Now(), |
|
|
"finish_time": time.Now(), |
|
|
}) |
|
|
}) |
|
|
|
|
|
} else if trans.Status == "prepared" { // 未commit直接处理的情况为回滚场景
|
|
|
|
|
|
for _, branch := range branches { |
|
|
|
|
|
if branch.Status == "rollbacked" { |
|
|
|
|
|
continue |
|
|
|
|
|
} |
|
|
|
|
|
db.Must().Model(&TransGlobalModel{}).Where("gid=?", gid).Update("gid", gid) // 更新update_time,避免被定时任务再次
|
|
|
|
|
|
resp, err := common.RestyClient.R().SetBody(M{ |
|
|
|
|
|
"branch": branch.Branch, |
|
|
|
|
|
"action": "rollback", |
|
|
|
|
|
"gid": branch.Gid, |
|
|
|
|
|
}).Post(branch.Url) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
return err |
|
|
|
|
|
} |
|
|
|
|
|
body := resp.String() |
|
|
|
|
|
if !strings.Contains(body, "SUCCESS") { |
|
|
|
|
|
return fmt.Errorf("bad response: %s", body) |
|
|
|
|
|
} |
|
|
|
|
|
writeTransLog(gid, "step rollbacked", "rollbacked", branch.Branch, "") |
|
|
|
|
|
db.Must().Model(&branch).Where("status=?", "prepared").Updates(M{ |
|
|
|
|
|
"status": "rollbacked", |
|
|
|
|
|
"finish_time": time.Now(), |
|
|
|
|
|
}) |
|
|
|
|
|
} |
|
|
|
|
|
writeTransLog(gid, "xa rollbacked", "rollbacked", "", "") |
|
|
|
|
|
db.Must().Model(&TransGlobalModel{}).Where("gid=? and status=?", gid, "prepared").Updates(M{ |
|
|
|
|
|
"status": "rollbacked", |
|
|
|
|
|
"finish_time": time.Now(), |
|
|
|
|
|
}) |
|
|
|
|
|
} else { |
|
|
|
|
|
return fmt.Errorf("bad trans status: %s", trans.Status) |
|
|
} |
|
|
} |
|
|
writeTransLog(gid, "xa finished", "finished", "", "") |
|
|
|
|
|
db.Must().Model(&TransGlobalModel{}).Where("gid=? and status=?", gid, "committed").Updates(M{ |
|
|
|
|
|
"status": "finished", |
|
|
|
|
|
"finish_time": time.Now(), |
|
|
|
|
|
}) |
|
|
|
|
|
return nil |
|
|
return nil |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|