rename
This commit is contained in:
parent
ff6b6e79b4
commit
9f8ee90998
@ -34,7 +34,7 @@ func Commit(c *gin.Context) (interface{}, error) {
|
|||||||
|
|
||||||
func Rollback(c *gin.Context) (interface{}, error) {
|
func Rollback(c *gin.Context) (interface{}, error) {
|
||||||
m := getTransFromContext(c)
|
m := getTransFromContext(c)
|
||||||
trans := TransGlobalModel{}
|
trans := TransGlobal{}
|
||||||
dbGet().Must().Model(&m).First(&trans)
|
dbGet().Must().Model(&m).First(&trans)
|
||||||
// 当前xa trans的状态为prepared,直接处理,则是回滚
|
// 当前xa trans的状态为prepared,直接处理,则是回滚
|
||||||
go ProcessTrans(&trans)
|
go ProcessTrans(&trans)
|
||||||
@ -42,7 +42,7 @@ func Rollback(c *gin.Context) (interface{}, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Branch(c *gin.Context) (interface{}, error) {
|
func Branch(c *gin.Context) (interface{}, error) {
|
||||||
branch := TransBranchModel{}
|
branch := TransBranch{}
|
||||||
err := c.BindJSON(&branch)
|
err := c.BindJSON(&branch)
|
||||||
e2p(err)
|
e2p(err)
|
||||||
db := dbGet()
|
db := dbGet()
|
||||||
@ -52,7 +52,7 @@ func Branch(c *gin.Context) (interface{}, error) {
|
|||||||
return M{"message": "SUCCESS"}, nil
|
return M{"message": "SUCCESS"}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func getTransFromContext(c *gin.Context) *TransGlobalModel {
|
func getTransFromContext(c *gin.Context) *TransGlobal {
|
||||||
data := M{}
|
data := M{}
|
||||||
b, err := c.GetRawData()
|
b, err := c.GetRawData()
|
||||||
e2p(err)
|
e2p(err)
|
||||||
@ -61,7 +61,7 @@ func getTransFromContext(c *gin.Context) *TransGlobalModel {
|
|||||||
if data["trans_type"].(string) == "saga" {
|
if data["trans_type"].(string) == "saga" {
|
||||||
data["data"] = common.MustMarshalString(data["steps"])
|
data["data"] = common.MustMarshalString(data["steps"])
|
||||||
}
|
}
|
||||||
m := TransGlobalModel{}
|
m := TransGlobal{}
|
||||||
common.MustRemarshal(data, &m)
|
common.MustRemarshal(data, &m)
|
||||||
return &m
|
return &m
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,10 +1,12 @@
|
|||||||
package dtmsvr
|
package dtmsvr
|
||||||
|
|
||||||
type dtmsvrConfig struct {
|
type dtmsvrConfig struct {
|
||||||
PreparedExpire int64 `json:"prepare_expire"` // 单位秒,当prepared的状态超过该时间,才能够转变成canceled,避免cancel了之后,才进入prepared
|
PreparedExpire int64 // 单位秒,处于prepared中的任务,过了这个时间,查询结果还是PENDING的话,则会被cancel
|
||||||
Mysql map[string]string
|
JobCronInterval int64 // 单位秒 当事务等待这个时间之后,还没有变化,则进行一轮处理,包括prepared中的任务和commited的任务
|
||||||
|
Mysql map[string]string
|
||||||
}
|
}
|
||||||
|
|
||||||
var config = &dtmsvrConfig{
|
var config = &dtmsvrConfig{
|
||||||
PreparedExpire: 60,
|
PreparedExpire: 60,
|
||||||
|
JobCronInterval: 20,
|
||||||
}
|
}
|
||||||
|
|||||||
@ -2,6 +2,8 @@ package dtmsvr
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"math"
|
||||||
|
"math/rand"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@ -11,8 +13,8 @@ import (
|
|||||||
|
|
||||||
func CronPreparedOnce(expire time.Duration) {
|
func CronPreparedOnce(expire time.Duration) {
|
||||||
db := dbGet()
|
db := dbGet()
|
||||||
ss := []TransGlobalModel{}
|
ss := []TransGlobal{}
|
||||||
db.Must().Model(&TransGlobalModel{}).Where("update_time < date_sub(now(), interval ? second)", int(expire/time.Second)).Where("status = ?", "prepared").Find(&ss)
|
db.Must().Model(&TransGlobal{}).Where("update_time < date_sub(now(), interval ? second)", int(expire/time.Second)).Where("status = ?", "prepared").Find(&ss)
|
||||||
writeTransLog("", "saga fetch prepared", fmt.Sprint(len(ss)), "", "")
|
writeTransLog("", "saga fetch prepared", fmt.Sprint(len(ss)), "", "")
|
||||||
if len(ss) == 0 {
|
if len(ss) == 0 {
|
||||||
return
|
return
|
||||||
@ -39,34 +41,60 @@ func CronPreparedOnce(expire time.Duration) {
|
|||||||
func CronPrepared() {
|
func CronPrepared() {
|
||||||
for {
|
for {
|
||||||
defer handlePanic()
|
defer handlePanic()
|
||||||
CronPreparedOnce(10 * time.Second)
|
CronTransOnce(time.Duration(config.JobCronInterval)*time.Second, "prepared")
|
||||||
|
sleepCronTime()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func CronCommittedOnce(expire time.Duration) {
|
func CronTransOnce(expire time.Duration, status string) bool {
|
||||||
|
trans := lockOneTrans(expire, status)
|
||||||
|
if trans == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
trans.touch(dbGet())
|
||||||
|
branches := []TransBranch{}
|
||||||
db := dbGet()
|
db := dbGet()
|
||||||
ss := []TransGlobalModel{}
|
db.Must().Where("gid=?", trans.Gid).Order("id asc").Find(&branches)
|
||||||
db.Must().Model(&TransGlobalModel{}).Where("update_time < date_sub(now(), interval ? second)", int(expire/time.Second)).Where("status = ?", "committed").Find(&ss)
|
trans.getProcessor().ProcessOnce(db, branches)
|
||||||
writeTransLog("", "saga fetch committed", fmt.Sprint(len(ss)), "", "")
|
if TransProcessedTestChan != nil {
|
||||||
if len(ss) == 0 {
|
TransProcessedTestChan <- trans.Gid
|
||||||
return
|
|
||||||
}
|
|
||||||
for _, sm := range ss {
|
|
||||||
writeTransLog(sm.Gid, "saga touch committed", "", "", "")
|
|
||||||
db.Must().Model(&sm).Update("id", sm.ID)
|
|
||||||
ProcessTrans(&sm)
|
|
||||||
}
|
}
|
||||||
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func CronCommitted() {
|
func CronCommitted() {
|
||||||
for {
|
for {
|
||||||
defer handlePanic()
|
defer handlePanic()
|
||||||
CronCommittedOnce(10 * time.Second)
|
processed := CronTransOnce(time.Duration(config.JobCronInterval)*time.Second, "commitetd")
|
||||||
|
if !processed {
|
||||||
|
sleepCronTime()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func lockOneTrans(expire time.Duration, status string) *TransGlobal {
|
||||||
|
trans := TransGlobal{}
|
||||||
|
owner := common.GenGid()
|
||||||
|
db := dbGet()
|
||||||
|
dbr := db.Must().Model(&trans).
|
||||||
|
Where("update_time < date_sub(now(), interval ? second) and satus=?", int(expire/time.Second), status).
|
||||||
|
Limit(1).Update("owner", owner)
|
||||||
|
if dbr.RowsAffected == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
dbr = db.Must().Where("owner=?", owner).Find(&trans)
|
||||||
|
return &trans
|
||||||
|
}
|
||||||
|
|
||||||
func handlePanic() {
|
func handlePanic() {
|
||||||
if err := recover(); err != nil {
|
if err := recover(); err != nil {
|
||||||
logrus.Printf("----panic %s handlered", err.(error).Error())
|
logrus.Printf("----panic %s handlered", err.(error).Error())
|
||||||
|
time.Sleep(3 * time.Second) // 出错后睡眠3s,避免无限循环
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func sleepCronTime() {
|
||||||
|
delta := math.Min(3, float64(config.JobCronInterval))
|
||||||
|
interval := time.Duration(rand.Float64() * delta * float64(time.Second))
|
||||||
|
time.Sleep(interval)
|
||||||
|
}
|
||||||
|
|||||||
@ -50,7 +50,7 @@ func TestCover(t *testing.T) {
|
|||||||
db := dbGet()
|
db := dbGet()
|
||||||
db.NoMust()
|
db.NoMust()
|
||||||
CronPreparedOnce(0)
|
CronPreparedOnce(0)
|
||||||
CronCommittedOnce(0)
|
CronTransOnce(0, "committed")
|
||||||
defer handlePanic()
|
defer handlePanic()
|
||||||
checkAffected(db.DB)
|
checkAffected(db.DB)
|
||||||
}
|
}
|
||||||
@ -58,16 +58,16 @@ func TestCover(t *testing.T) {
|
|||||||
// 测试使用的全局对象
|
// 测试使用的全局对象
|
||||||
var initdb = dbGet()
|
var initdb = dbGet()
|
||||||
|
|
||||||
func getSagaModel(gid string) *TransGlobalModel {
|
func getSagaModel(gid string) *TransGlobal {
|
||||||
sm := TransGlobalModel{}
|
sm := TransGlobal{}
|
||||||
dbr := dbGet().Model(&sm).Where("gid=?", gid).First(&sm)
|
dbr := dbGet().Model(&sm).Where("gid=?", gid).First(&sm)
|
||||||
e2p(dbr.Error)
|
e2p(dbr.Error)
|
||||||
return &sm
|
return &sm
|
||||||
}
|
}
|
||||||
|
|
||||||
func getBranchesStatus(gid string) []string {
|
func getBranchesStatus(gid string) []string {
|
||||||
steps := []TransBranchModel{}
|
steps := []TransBranch{}
|
||||||
dbr := dbGet().Model(&TransBranchModel{}).Where("gid=?", gid).Find(&steps)
|
dbr := dbGet().Model(&TransBranch{}).Where("gid=?", gid).Find(&steps)
|
||||||
e2p(dbr.Error)
|
e2p(dbr.Error)
|
||||||
status := []string{}
|
status := []string{}
|
||||||
for _, step := range steps {
|
for _, step := range steps {
|
||||||
@ -172,7 +172,7 @@ func sagaCommittedPending(t *testing.T) {
|
|||||||
WaitTransProcessed(saga.Gid)
|
WaitTransProcessed(saga.Gid)
|
||||||
examples.TransInResult = ""
|
examples.TransInResult = ""
|
||||||
assert.Equal(t, []string{"prepared", "finished", "prepared", "prepared"}, getBranchesStatus(saga.Gid))
|
assert.Equal(t, []string{"prepared", "finished", "prepared", "prepared"}, getBranchesStatus(saga.Gid))
|
||||||
CronCommittedOnce(-10 * time.Second)
|
CronTransOnce(-10*time.Second, "committed")
|
||||||
WaitTransProcessed(saga.Gid)
|
WaitTransProcessed(saga.Gid)
|
||||||
assert.Equal(t, []string{"prepared", "finished", "prepared", "finished"}, getBranchesStatus(saga.Gid))
|
assert.Equal(t, []string{"prepared", "finished", "prepared", "finished"}, getBranchesStatus(saga.Gid))
|
||||||
assert.Equal(t, "finished", getSagaModel(saga.Gid).Status)
|
assert.Equal(t, "finished", getSagaModel(saga.Gid).Status)
|
||||||
|
|||||||
@ -7,7 +7,7 @@ import (
|
|||||||
"gorm.io/gorm/clause"
|
"gorm.io/gorm/clause"
|
||||||
)
|
)
|
||||||
|
|
||||||
func saveCommitted(m *TransGlobalModel) {
|
func saveCommitted(m *TransGlobal) {
|
||||||
db := dbGet()
|
db := dbGet()
|
||||||
m.Status = "committed"
|
m.Status = "committed"
|
||||||
err := db.Transaction(func(db1 *gorm.DB) error {
|
err := db.Transaction(func(db1 *gorm.DB) error {
|
||||||
@ -20,7 +20,7 @@ func saveCommitted(m *TransGlobalModel) {
|
|||||||
writeTransLog(m.Gid, "change status", m.Status, "", "")
|
writeTransLog(m.Gid, "change status", m.Status, "", "")
|
||||||
db.Must().Model(m).Where("status=?", "prepared").Update("status", "committed")
|
db.Must().Model(m).Where("status=?", "prepared").Update("status", "committed")
|
||||||
}
|
}
|
||||||
nsteps := GetTrans(m).GetDataBranches()
|
nsteps := m.getProcessor().GenBranches()
|
||||||
if len(nsteps) > 0 {
|
if len(nsteps) > 0 {
|
||||||
writeTransLog(m.Gid, "save steps", m.Status, "", common.MustMarshalString(nsteps))
|
writeTransLog(m.Gid, "save steps", m.Status, "", common.MustMarshalString(nsteps))
|
||||||
db.Must().Clauses(clause.OnConflict{
|
db.Must().Clauses(clause.OnConflict{
|
||||||
@ -42,18 +42,12 @@ func WaitTransProcessed(gid string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func ProcessTrans(trans *TransGlobalModel) {
|
func ProcessTrans(trans *TransGlobal) {
|
||||||
err := innerProcessTrans(trans)
|
branches := []TransBranch{}
|
||||||
if err != nil {
|
db := dbGet()
|
||||||
logrus.Errorf("process trans ignore error: %s", err.Error())
|
db.Must().Where("gid=?", trans.Gid).Order("id asc").Find(&branches)
|
||||||
}
|
trans.getProcessor().ProcessOnce(db, branches)
|
||||||
if TransProcessedTestChan != nil {
|
if TransProcessedTestChan != nil {
|
||||||
TransProcessedTestChan <- trans.Gid
|
TransProcessedTestChan <- trans.Gid
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
func innerProcessTrans(trans *TransGlobalModel) (rerr error) {
|
|
||||||
branches := []TransBranchModel{}
|
|
||||||
db := dbGet()
|
|
||||||
db.Must().Where("gid=?", trans.Gid).Order("id asc").Find(&branches)
|
|
||||||
return GetTrans(trans).ProcessOnce(db, branches)
|
|
||||||
}
|
|
||||||
|
|||||||
@ -8,33 +8,22 @@ import (
|
|||||||
"github.com/yedf/dtm/common"
|
"github.com/yedf/dtm/common"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Trans interface {
|
type TransProcessor interface {
|
||||||
GetDataBranches() []TransBranchModel
|
GenBranches() []TransBranch
|
||||||
ProcessOnce(db *common.MyDb, branches []TransBranchModel) error
|
ProcessOnce(db *common.MyDb, branches []TransBranch) error
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetTrans(trans *TransGlobalModel) Trans {
|
type TransSagaProcessor struct {
|
||||||
if trans.TransType == "saga" {
|
*TransGlobal
|
||||||
return &TransSaga{TransGlobalModel: trans}
|
|
||||||
} else if trans.TransType == "tcc" {
|
|
||||||
return &TransTcc{TransGlobalModel: trans}
|
|
||||||
} else if trans.TransType == "xa" {
|
|
||||||
return &TransXa{TransGlobalModel: trans}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type TransSaga struct {
|
func (t *TransSagaProcessor) GenBranches() []TransBranch {
|
||||||
*TransGlobalModel
|
nsteps := []TransBranch{}
|
||||||
}
|
|
||||||
|
|
||||||
func (t *TransSaga) GetDataBranches() []TransBranchModel {
|
|
||||||
nsteps := []TransBranchModel{}
|
|
||||||
steps := []M{}
|
steps := []M{}
|
||||||
common.MustUnmarshalString(t.Data, &steps)
|
common.MustUnmarshalString(t.Data, &steps)
|
||||||
for _, step := range steps {
|
for _, step := range steps {
|
||||||
for _, branchType := range []string{"compensate", "action"} {
|
for _, branchType := range []string{"compensate", "action"} {
|
||||||
nsteps = append(nsteps, TransBranchModel{
|
nsteps = append(nsteps, TransBranch{
|
||||||
Gid: t.Gid,
|
Gid: t.Gid,
|
||||||
Branch: fmt.Sprintf("%d", len(nsteps)+1),
|
Branch: fmt.Sprintf("%d", len(nsteps)+1),
|
||||||
Data: step["data"].(string),
|
Data: step["data"].(string),
|
||||||
@ -47,7 +36,7 @@ func (t *TransSaga) GetDataBranches() []TransBranchModel {
|
|||||||
return nsteps
|
return nsteps
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *TransSaga) ProcessOnce(db *common.MyDb, branches []TransBranchModel) error {
|
func (t *TransSagaProcessor) ProcessOnce(db *common.MyDb, branches []TransBranch) error {
|
||||||
current := 0 // 当前正在处理的步骤
|
current := 0 // 当前正在处理的步骤
|
||||||
for ; current < len(branches); current++ {
|
for ; current < len(branches); current++ {
|
||||||
step := branches[current]
|
step := branches[current]
|
||||||
@ -99,17 +88,17 @@ func (t *TransSaga) ProcessOnce(db *common.MyDb, branches []TransBranchModel) er
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
type TransTcc struct {
|
type TransTccProcessor struct {
|
||||||
*TransGlobalModel
|
*TransGlobal
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *TransTcc) GetDataBranches() []TransBranchModel {
|
func (t *TransTccProcessor) GenBranches() []TransBranch {
|
||||||
nsteps := []TransBranchModel{}
|
nsteps := []TransBranch{}
|
||||||
steps := []M{}
|
steps := []M{}
|
||||||
common.MustUnmarshalString(t.Data, &steps)
|
common.MustUnmarshalString(t.Data, &steps)
|
||||||
for _, step := range steps {
|
for _, step := range steps {
|
||||||
for _, branchType := range []string{"rollback", "commit", "prepare"} {
|
for _, branchType := range []string{"rollback", "commit", "prepare"} {
|
||||||
nsteps = append(nsteps, TransBranchModel{
|
nsteps = append(nsteps, TransBranch{
|
||||||
Gid: t.Gid,
|
Gid: t.Gid,
|
||||||
Branch: fmt.Sprintf("%d", len(nsteps)+1),
|
Branch: fmt.Sprintf("%d", len(nsteps)+1),
|
||||||
Data: step["data"].(string),
|
Data: step["data"].(string),
|
||||||
@ -122,7 +111,7 @@ func (t *TransTcc) GetDataBranches() []TransBranchModel {
|
|||||||
return nsteps
|
return nsteps
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *TransTcc) ProcessOnce(db *common.MyDb, branches []TransBranchModel) error {
|
func (t *TransTccProcessor) ProcessOnce(db *common.MyDb, branches []TransBranch) error {
|
||||||
gid := t.Gid
|
gid := t.Gid
|
||||||
current := 0 // 当前正在处理的步骤
|
current := 0 // 当前正在处理的步骤
|
||||||
for ; current < len(branches); current++ {
|
for ; current < len(branches); current++ {
|
||||||
@ -136,7 +125,7 @@ func (t *TransTcc) ProcessOnce(db *common.MyDb, branches []TransBranchModel) err
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
body := resp.String()
|
body := resp.String()
|
||||||
db.Must().Model(&TransGlobalModel{}).Where("gid=?", gid).Update("gid", gid) // 更新update_time,避免被定时任务再次
|
db.Must().Model(&TransGlobal{}).Where("gid=?", gid).Update("gid", gid) // 更新update_time,避免被定时任务再次
|
||||||
if strings.Contains(body, "SUCCESS") {
|
if strings.Contains(body, "SUCCESS") {
|
||||||
writeTransLog(gid, "step finished", "finished", step.Branch, "")
|
writeTransLog(gid, "step finished", "finished", step.Branch, "")
|
||||||
dbr := db.Must().Model(&step).Where("status=?", "prepared").Updates(M{
|
dbr := db.Must().Model(&step).Where("status=?", "prepared").Updates(M{
|
||||||
@ -160,7 +149,7 @@ func (t *TransTcc) ProcessOnce(db *common.MyDb, branches []TransBranchModel) err
|
|||||||
//////////////////////////////////////////////////
|
//////////////////////////////////////////////////
|
||||||
if current == len(branches) { // tcc 事务完成
|
if current == len(branches) { // tcc 事务完成
|
||||||
writeTransLog(gid, "saga finished", "finished", "", "")
|
writeTransLog(gid, "saga finished", "finished", "", "")
|
||||||
dbr := db.Must().Model(&TransGlobalModel{}).Where("gid=? and status=?", gid, "committed").Updates(M{
|
dbr := db.Must().Model(&TransGlobal{}).Where("gid=? and status=?", gid, "committed").Updates(M{
|
||||||
"status": "finished",
|
"status": "finished",
|
||||||
"finish_time": time.Now(),
|
"finish_time": time.Now(),
|
||||||
})
|
})
|
||||||
@ -192,7 +181,7 @@ func (t *TransTcc) ProcessOnce(db *common.MyDb, branches []TransBranchModel) err
|
|||||||
return fmt.Errorf("saga current not -1")
|
return fmt.Errorf("saga current not -1")
|
||||||
}
|
}
|
||||||
writeTransLog(gid, "saga rollbacked", "rollbacked", "", "")
|
writeTransLog(gid, "saga rollbacked", "rollbacked", "", "")
|
||||||
dbr := db.Must().Model(&TransGlobalModel{}).Where("status=? and gid=?", "committed", gid).Updates(M{
|
dbr := db.Must().Model(&TransGlobal{}).Where("status=? and gid=?", "committed", gid).Updates(M{
|
||||||
"status": "rollbacked",
|
"status": "rollbacked",
|
||||||
"rollback_time": time.Now(),
|
"rollback_time": time.Now(),
|
||||||
})
|
})
|
||||||
@ -200,15 +189,15 @@ func (t *TransTcc) ProcessOnce(db *common.MyDb, branches []TransBranchModel) err
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
type TransXa struct {
|
type TransXaProcessor struct {
|
||||||
*TransGlobalModel
|
*TransGlobal
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *TransXa) GetDataBranches() []TransBranchModel {
|
func (t *TransXaProcessor) GenBranches() []TransBranch {
|
||||||
return []TransBranchModel{}
|
return []TransBranch{}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *TransXa) ProcessOnce(db *common.MyDb, branches []TransBranchModel) error {
|
func (t *TransXaProcessor) ProcessOnce(db *common.MyDb, branches []TransBranch) error {
|
||||||
gid := t.Gid
|
gid := t.Gid
|
||||||
if t.Status == "finished" {
|
if t.Status == "finished" {
|
||||||
return nil
|
return nil
|
||||||
@ -218,7 +207,7 @@ func (t *TransXa) ProcessOnce(db *common.MyDb, branches []TransBranchModel) erro
|
|||||||
if branch.Status == "finished" {
|
if branch.Status == "finished" {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
db.Must().Model(&TransGlobalModel{}).Where("gid=?", gid).Update("gid", gid) // 更新update_time,避免被定时任务再次
|
db.Must().Model(&TransGlobal{}).Where("gid=?", gid).Update("gid", gid) // 更新update_time,避免被定时任务再次
|
||||||
resp, err := common.RestyClient.R().SetBody(M{
|
resp, err := common.RestyClient.R().SetBody(M{
|
||||||
"branch": branch.Branch,
|
"branch": branch.Branch,
|
||||||
"action": "commit",
|
"action": "commit",
|
||||||
@ -238,7 +227,7 @@ func (t *TransXa) ProcessOnce(db *common.MyDb, branches []TransBranchModel) erro
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
writeTransLog(gid, "xa finished", "finished", "", "")
|
writeTransLog(gid, "xa finished", "finished", "", "")
|
||||||
db.Must().Model(&TransGlobalModel{}).Where("gid=? and status=?", gid, "committed").Updates(M{
|
db.Must().Model(&TransGlobal{}).Where("gid=? and status=?", gid, "committed").Updates(M{
|
||||||
"status": "finished",
|
"status": "finished",
|
||||||
"finish_time": time.Now(),
|
"finish_time": time.Now(),
|
||||||
})
|
})
|
||||||
@ -247,7 +236,7 @@ func (t *TransXa) ProcessOnce(db *common.MyDb, branches []TransBranchModel) erro
|
|||||||
if branch.Status == "rollbacked" {
|
if branch.Status == "rollbacked" {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
db.Must().Model(&TransGlobalModel{}).Where("gid=?", gid).Update("gid", gid) // 更新update_time,避免被定时任务再次
|
db.Must().Model(&TransGlobal{}).Where("gid=?", gid).Update("gid", gid) // 更新update_time,避免被定时任务再次
|
||||||
resp, err := common.RestyClient.R().SetBody(M{
|
resp, err := common.RestyClient.R().SetBody(M{
|
||||||
"branch": branch.Branch,
|
"branch": branch.Branch,
|
||||||
"action": "rollback",
|
"action": "rollback",
|
||||||
@ -267,7 +256,7 @@ func (t *TransXa) ProcessOnce(db *common.MyDb, branches []TransBranchModel) erro
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
writeTransLog(gid, "xa rollbacked", "rollbacked", "", "")
|
writeTransLog(gid, "xa rollbacked", "rollbacked", "", "")
|
||||||
db.Must().Model(&TransGlobalModel{}).Where("gid=? and status=?", gid, "prepared").Updates(M{
|
db.Must().Model(&TransGlobal{}).Where("gid=? and status=?", gid, "prepared").Updates(M{
|
||||||
"status": "rollbacked",
|
"status": "rollbacked",
|
||||||
"finish_time": time.Now(),
|
"finish_time": time.Now(),
|
||||||
})
|
})
|
||||||
|
|||||||
@ -13,7 +13,7 @@ type M = map[string]interface{}
|
|||||||
var p2e = common.P2E
|
var p2e = common.P2E
|
||||||
var e2p = common.E2P
|
var e2p = common.E2P
|
||||||
|
|
||||||
type TransGlobalModel struct {
|
type TransGlobal struct {
|
||||||
common.ModelBase
|
common.ModelBase
|
||||||
Gid string `json:"gid"`
|
Gid string `json:"gid"`
|
||||||
TransType string `json:"trans_type"`
|
TransType string `json:"trans_type"`
|
||||||
@ -25,16 +25,16 @@ type TransGlobalModel struct {
|
|||||||
RollbackTime *time.Time
|
RollbackTime *time.Time
|
||||||
}
|
}
|
||||||
|
|
||||||
func (*TransGlobalModel) TableName() string {
|
func (*TransGlobal) TableName() string {
|
||||||
return "trans_global"
|
return "trans_global"
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *TransGlobalModel) touch(db *common.MyDb) *gorm.DB {
|
func (t *TransGlobal) touch(db *common.MyDb) *gorm.DB {
|
||||||
writeTransLog(t.Gid, "touch trans", "", "", "")
|
writeTransLog(t.Gid, "touch trans", "", "", "")
|
||||||
return db.Model(&TransGlobalModel{}).Where("gid=?", t.Gid).Update("gid", t.Gid) // 更新update_time,避免被定时任务再次
|
return db.Model(&TransGlobal{}).Where("gid=?", t.Gid).Update("gid", t.Gid) // 更新update_time,避免被定时任务再次
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *TransGlobalModel) saveStatus(db *common.MyDb, status string) *gorm.DB {
|
func (t *TransGlobal) saveStatus(db *common.MyDb, status string) *gorm.DB {
|
||||||
writeTransLog(t.Gid, "step change", status, "", "")
|
writeTransLog(t.Gid, "step change", status, "", "")
|
||||||
dbr := db.Must().Model(t).Where("status=?", t.Status).Updates(M{
|
dbr := db.Must().Model(t).Where("status=?", t.Status).Updates(M{
|
||||||
"status": status,
|
"status": status,
|
||||||
@ -45,7 +45,7 @@ func (t *TransGlobalModel) saveStatus(db *common.MyDb, status string) *gorm.DB {
|
|||||||
return dbr
|
return dbr
|
||||||
}
|
}
|
||||||
|
|
||||||
type TransBranchModel struct {
|
type TransBranch struct {
|
||||||
common.ModelBase
|
common.ModelBase
|
||||||
Gid string
|
Gid string
|
||||||
Url string
|
Url string
|
||||||
@ -57,11 +57,11 @@ type TransBranchModel struct {
|
|||||||
RollbackTime *time.Time
|
RollbackTime *time.Time
|
||||||
}
|
}
|
||||||
|
|
||||||
func (*TransBranchModel) TableName() string {
|
func (*TransBranch) TableName() string {
|
||||||
return "trans_branch"
|
return "trans_branch"
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *TransBranchModel) saveStatus(db *common.MyDb, status string) *gorm.DB {
|
func (t *TransBranch) saveStatus(db *common.MyDb, status string) *gorm.DB {
|
||||||
writeTransLog(t.Gid, "step change", status, t.Branch, "")
|
writeTransLog(t.Gid, "step change", status, t.Branch, "")
|
||||||
dbr := db.Must().Model(t).Where("status=?", t.Status).Updates(M{
|
dbr := db.Must().Model(t).Where("status=?", t.Status).Updates(M{
|
||||||
"status": status,
|
"status": status,
|
||||||
@ -77,3 +77,14 @@ func checkAffected(db1 *gorm.DB) {
|
|||||||
panic(fmt.Errorf("duplicate updating"))
|
panic(fmt.Errorf("duplicate updating"))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (trans *TransGlobal) getProcessor() TransProcessor {
|
||||||
|
if trans.TransType == "saga" {
|
||||||
|
return &TransSagaProcessor{TransGlobal: trans}
|
||||||
|
} else if trans.TransType == "tcc" {
|
||||||
|
return &TransTccProcessor{TransGlobal: trans}
|
||||||
|
} else if trans.TransType == "xa" {
|
||||||
|
return &TransXaProcessor{TransGlobal: trans}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user