|
|
|
@ -6,107 +6,145 @@ import ( |
|
|
|
"time" |
|
|
|
|
|
|
|
"github.com/sirupsen/logrus" |
|
|
|
"github.com/yedf/dtm" |
|
|
|
"github.com/yedf/dtm/common" |
|
|
|
"gorm.io/gorm" |
|
|
|
"gorm.io/gorm/clause" |
|
|
|
) |
|
|
|
|
|
|
|
func saveCommitedSagaModel(m *SagaModel) { |
|
|
|
db := DbGet() |
|
|
|
m.Status = "commited" |
|
|
|
func saveCommitted(m *TransGlobalModel) { |
|
|
|
db := dbGet() |
|
|
|
m.Status = "committed" |
|
|
|
err := db.Transaction(func(db1 *gorm.DB) error { |
|
|
|
db := &MyDb{DB: db1} |
|
|
|
writeTransLog(m.Gid, "save commited", m.Status, -1, m.Steps) |
|
|
|
db := &common.MyDb{DB: db1} |
|
|
|
writeTransLog(m.Gid, "save committed", m.Status, "", m.Data) |
|
|
|
dbr := db.Must().Clauses(clause.OnConflict{ |
|
|
|
DoNothing: true, |
|
|
|
}).Create(&m) |
|
|
|
if dbr.RowsAffected == 0 { |
|
|
|
writeTransLog(m.Gid, "change status", m.Status, -1, "") |
|
|
|
db.Must().Model(&m).Where("status=?", "prepared").Update("status", "commited") |
|
|
|
writeTransLog(m.Gid, "change status", m.Status, "", "") |
|
|
|
db.Must().Model(&m).Where("status=?", "prepared").Update("status", "committed") |
|
|
|
} |
|
|
|
nsteps := []SagaStepModel{} |
|
|
|
steps := []M{} |
|
|
|
common.MustUnmarshalString(m.Steps, &steps) |
|
|
|
for _, step := range steps { |
|
|
|
nsteps = append(nsteps, SagaStepModel{ |
|
|
|
Gid: m.Gid, |
|
|
|
Step: len(nsteps) + 1, |
|
|
|
Data: step["post_data"].(string), |
|
|
|
Url: step["compensate"].(string), |
|
|
|
Type: "compensate", |
|
|
|
Status: "pending", |
|
|
|
}) |
|
|
|
nsteps = append(nsteps, SagaStepModel{ |
|
|
|
Gid: m.Gid, |
|
|
|
Step: len(nsteps) + 1, |
|
|
|
Data: step["post_data"].(string), |
|
|
|
Url: step["action"].(string), |
|
|
|
Type: "action", |
|
|
|
Status: "pending", |
|
|
|
}) |
|
|
|
if m.TransType == "saga" { |
|
|
|
nsteps := []TransBranchModel{} |
|
|
|
steps := []M{} |
|
|
|
common.MustUnmarshalString(m.Data, &steps) |
|
|
|
for _, step := range steps { |
|
|
|
nsteps = append(nsteps, TransBranchModel{ |
|
|
|
Gid: m.Gid, |
|
|
|
Branch: fmt.Sprintf("%d", len(nsteps)+1), |
|
|
|
Data: step["data"].(string), |
|
|
|
Url: step["compensate"].(string), |
|
|
|
BranchType: "compensate", |
|
|
|
Status: "prepared", |
|
|
|
}) |
|
|
|
nsteps = append(nsteps, TransBranchModel{ |
|
|
|
Gid: m.Gid, |
|
|
|
Branch: fmt.Sprintf("%d", len(nsteps)+1), |
|
|
|
Data: step["data"].(string), |
|
|
|
Url: step["action"].(string), |
|
|
|
BranchType: "action", |
|
|
|
Status: "prepared", |
|
|
|
}) |
|
|
|
} |
|
|
|
writeTransLog(m.Gid, "save steps", m.Status, "", common.MustMarshalString(nsteps)) |
|
|
|
db.Must().Clauses(clause.OnConflict{ |
|
|
|
DoNothing: true, |
|
|
|
}).Create(&nsteps) |
|
|
|
} |
|
|
|
writeTransLog(m.Gid, "save steps", m.Status, -1, common.MustMarshalString(nsteps)) |
|
|
|
db.Must().Clauses(clause.OnConflict{ |
|
|
|
DoNothing: true, |
|
|
|
}).Create(&nsteps) |
|
|
|
return nil |
|
|
|
}) |
|
|
|
common.PanicIfError(err) |
|
|
|
} |
|
|
|
|
|
|
|
var SagaProcessedTestChan chan string = nil // 用于测试时,通知处理结束
|
|
|
|
var TransProcessedTestChan chan string = nil // 用于测试时,通知处理结束
|
|
|
|
|
|
|
|
func WaitCommitedSaga(gid string) { |
|
|
|
id := <-SagaProcessedTestChan |
|
|
|
func WaitTransCommitted(gid string) { |
|
|
|
id := <-TransProcessedTestChan |
|
|
|
for id != gid { |
|
|
|
logrus.Errorf("-------id %s not match gid %s", id, gid) |
|
|
|
id = <-SagaProcessedTestChan |
|
|
|
id = <-TransProcessedTestChan |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
func ProcessCommitedSaga(gid string) { |
|
|
|
err := innerProcessCommitedSaga(gid) |
|
|
|
func ProcessCommitted(trans *TransGlobalModel) { |
|
|
|
err := innerProcessCommitted(trans) |
|
|
|
if err != nil { |
|
|
|
logrus.Errorf("process commited saga error: %s", err.Error()) |
|
|
|
logrus.Errorf("process committed error: %s", err.Error()) |
|
|
|
} |
|
|
|
if SagaProcessedTestChan != nil { |
|
|
|
SagaProcessedTestChan <- gid |
|
|
|
if TransProcessedTestChan != nil { |
|
|
|
TransProcessedTestChan <- trans.Gid |
|
|
|
} |
|
|
|
} |
|
|
|
func checkAffected(db1 *gorm.DB) { |
|
|
|
if db1.RowsAffected == 0 { |
|
|
|
panic(fmt.Errorf("duplicate updating")) |
|
|
|
func innerProcessCommitted(trans *TransGlobalModel) (rerr error) { |
|
|
|
branches := []TransBranchModel{} |
|
|
|
db := dbGet() |
|
|
|
db.Must().Order("id asc").Find(&branches) |
|
|
|
if trans.TransType == "saga" { |
|
|
|
return innerProcessCommittedSaga(trans, db, branches) |
|
|
|
} else if trans.TransType == "xa" { |
|
|
|
return innerProcessCommittedXa(trans, db, branches) |
|
|
|
} |
|
|
|
panic(fmt.Errorf("unkown trans type: %s", trans.TransType)) |
|
|
|
} |
|
|
|
|
|
|
|
func innerProcessCommittedXa(trans *TransGlobalModel, db *common.MyDb, branches []TransBranchModel) error { |
|
|
|
gid := trans.Gid |
|
|
|
for _, branch := range branches { |
|
|
|
if branch.Status == "finished" { |
|
|
|
continue |
|
|
|
} |
|
|
|
db.Must().Model(&TransGlobalModel{}).Where("gid=?", gid).Update("gid", gid) // 更新update_time,避免被定时任务再次
|
|
|
|
resp, err := common.RestyClient.R().SetBody(M{ |
|
|
|
"branch": branch.Branch, |
|
|
|
"action": "commit", |
|
|
|
"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 finished", "finished", branch.Branch, "") |
|
|
|
db.Must().Model(&branch).Where("status=?", "prepared").Updates(M{ |
|
|
|
"status": "finished", |
|
|
|
"finish_time": time.Now(), |
|
|
|
}) |
|
|
|
} |
|
|
|
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 |
|
|
|
} |
|
|
|
|
|
|
|
func innerProcessCommitedSaga(gid string) (rerr error) { |
|
|
|
steps := []SagaStepModel{} |
|
|
|
db := DbGet() |
|
|
|
db.Must().Order("id asc").Find(&steps) |
|
|
|
func innerProcessCommittedSaga(trans *TransGlobalModel, db *common.MyDb, branches []TransBranchModel) error { |
|
|
|
gid := trans.Gid |
|
|
|
current := 0 // 当前正在处理的步骤
|
|
|
|
for ; current < len(steps); current++ { |
|
|
|
step := steps[current] |
|
|
|
if step.Type == "compensate" && step.Status == "pending" || step.Type == "action" && step.Status == "finished" { |
|
|
|
for ; current < len(branches); current++ { |
|
|
|
step := branches[current] |
|
|
|
if step.BranchType == "compensate" && step.Status == "prepared" || step.BranchType == "action" && step.Status == "finished" { |
|
|
|
continue |
|
|
|
} |
|
|
|
if step.Type == "action" && step.Status == "pending" { |
|
|
|
resp, err := dtm.RestyClient.R().SetBody(step.Data).SetQueryParam("gid", step.Gid).Post(step.Url) |
|
|
|
if step.BranchType == "action" && step.Status == "prepared" { |
|
|
|
resp, err := common.RestyClient.R().SetBody(step.Data).SetQueryParam("gid", step.Gid).Post(step.Url) |
|
|
|
if err != nil { |
|
|
|
return err |
|
|
|
} |
|
|
|
body := resp.String() |
|
|
|
db.Must().Model(&SagaModel{}).Where("gid=?", gid).Update("gid", gid) // 更新update_time,避免被定时任务再次
|
|
|
|
db.Must().Model(&TransGlobalModel{}).Where("gid=?", gid).Update("gid", gid) // 更新update_time,避免被定时任务再次
|
|
|
|
if strings.Contains(body, "SUCCESS") { |
|
|
|
writeTransLog(gid, "step finished", "finished", step.Step, "") |
|
|
|
dbr := db.Must().Model(&step).Where("status=?", "pending").Updates(M{ |
|
|
|
writeTransLog(gid, "step finished", "finished", step.Branch, "") |
|
|
|
dbr := db.Must().Model(&step).Where("status=?", "prepared").Updates(M{ |
|
|
|
"status": "finished", |
|
|
|
"finish_time": time.Now(), |
|
|
|
}) |
|
|
|
checkAffected(dbr) |
|
|
|
} else if strings.Contains(body, "FAIL") { |
|
|
|
writeTransLog(gid, "step rollbacked", "rollbacked", step.Step, "") |
|
|
|
dbr := db.Must().Model(&step).Where("status=?", "pending").Updates(M{ |
|
|
|
writeTransLog(gid, "step rollbacked", "rollbacked", step.Branch, "") |
|
|
|
dbr := db.Must().Model(&step).Where("status=?", "prepared").Updates(M{ |
|
|
|
"status": "rollbacked", |
|
|
|
"rollback_time": time.Now(), |
|
|
|
}) |
|
|
|
@ -117,9 +155,9 @@ func innerProcessCommitedSaga(gid string) (rerr error) { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
if current == len(steps) { // saga 事务完成
|
|
|
|
writeTransLog(gid, "saga finished", "finished", -1, "") |
|
|
|
dbr := db.Must().Model(&SagaModel{}).Where("gid=? and status=?", gid, "commited").Updates(M{ |
|
|
|
if current == len(branches) { // saga 事务完成
|
|
|
|
writeTransLog(gid, "saga finished", "finished", "", "") |
|
|
|
dbr := db.Must().Model(&TransGlobalModel{}).Where("gid=? and status=?", gid, "committed").Updates(M{ |
|
|
|
"status": "finished", |
|
|
|
"finish_time": time.Now(), |
|
|
|
}) |
|
|
|
@ -127,17 +165,17 @@ func innerProcessCommitedSaga(gid string) (rerr error) { |
|
|
|
return nil |
|
|
|
} |
|
|
|
for current = current - 1; current >= 0; current-- { |
|
|
|
step := steps[current] |
|
|
|
if step.Type != "compensate" || step.Status != "pending" { |
|
|
|
step := branches[current] |
|
|
|
if step.BranchType != "compensate" || step.Status != "prepared" { |
|
|
|
continue |
|
|
|
} |
|
|
|
resp, err := dtm.RestyClient.R().SetBody(step.Data).SetQueryParam("gid", step.Gid).Post(step.Url) |
|
|
|
resp, err := common.RestyClient.R().SetBody(step.Data).SetQueryParam("gid", step.Gid).Post(step.Url) |
|
|
|
if err != nil { |
|
|
|
return err |
|
|
|
} |
|
|
|
body := resp.String() |
|
|
|
if strings.Contains(body, "SUCCESS") { |
|
|
|
writeTransLog(gid, "step rollbacked", "rollbacked", step.Step, "") |
|
|
|
writeTransLog(gid, "step rollbacked", "rollbacked", step.Branch, "") |
|
|
|
dbr := db.Must().Model(&step).Where("status=?", step.Status).Updates(M{ |
|
|
|
"status": "rollbacked", |
|
|
|
"rollback_time": time.Now(), |
|
|
|
@ -150,11 +188,17 @@ func innerProcessCommitedSaga(gid string) (rerr error) { |
|
|
|
if current != -1 { |
|
|
|
return fmt.Errorf("saga current not -1") |
|
|
|
} |
|
|
|
writeTransLog(gid, "saga rollbacked", "rollbacked", -1, "") |
|
|
|
dbr := db.Must().Model(&SagaModel{}).Where("status=? and gid=?", "commited", gid).Updates(M{ |
|
|
|
writeTransLog(gid, "saga rollbacked", "rollbacked", "", "") |
|
|
|
dbr := db.Must().Model(&TransGlobalModel{}).Where("status=? and gid=?", "committed", gid).Updates(M{ |
|
|
|
"status": "rollbacked", |
|
|
|
"rollback_time": time.Now(), |
|
|
|
}) |
|
|
|
checkAffected(dbr) |
|
|
|
return nil |
|
|
|
} |
|
|
|
|
|
|
|
func checkAffected(db1 *gorm.DB) { |
|
|
|
if db1.RowsAffected == 0 { |
|
|
|
panic(fmt.Errorf("duplicate updating")) |
|
|
|
} |
|
|
|
} |
|
|
|
|