From c0222e627915325934642f01738dfa4b245c9c81 Mon Sep 17 00:00:00 2001 From: yedongfu Date: Sat, 22 May 2021 11:50:38 +0800 Subject: [PATCH] go build ok --- .gitignore | 1 + app/main.go | 24 ++++++++++++++++++++++++ dtmsvr/config.go | 10 ++++++++++ dtmsvr/cron.go | 9 ++++++--- dtmsvr/dtmsvr_test.go | 35 ++++++++++++++++------------------- dtmsvr/service.go | 2 +- examples/main.go | 2 +- main.go | 17 ----------------- dtm/saga.go => saga.go | 0 9 files changed, 59 insertions(+), 41 deletions(-) create mode 100644 app/main.go delete mode 100644 main.go rename dtm/saga.go => saga.go (100%) diff --git a/.gitignore b/.gitignore index 1e4f102..07fd594 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ dtmsvr/dtmsvr.yml *.out +main diff --git a/app/main.go b/app/main.go new file mode 100644 index 0000000..481fd9a --- /dev/null +++ b/app/main.go @@ -0,0 +1,24 @@ +package main + +import ( + "os" + "time" + + "github.com/yedf/dtm/common" + "github.com/yedf/dtm/dtmsvr" + "github.com/yedf/dtm/examples" +) + +type M = map[string]interface{} + +func main() { + cmd := common.If(len(os.Args) > 1, os.Args[1], "").(string) + dtmsvr.LoadConfig() + if cmd == "" { // 所有服务都启动 + go dtmsvr.StartSvr() + go examples.StartSvr() + } else if cmd == "dtmsvr" { + go dtmsvr.StartSvr() + } + time.Sleep(1000 * 1000 * 1000 * 1000) +} diff --git a/dtmsvr/config.go b/dtmsvr/config.go index 262006d..fad69fc 100644 --- a/dtmsvr/config.go +++ b/dtmsvr/config.go @@ -37,6 +37,14 @@ func (f *formatter) Format(entry *logrus.Entry) ([]byte, error) { return b.Bytes(), nil } +type dtmsvrConfig struct { + PreparedExpire int64 `json:"prepare_expire"` // 单位秒,当prepared的状态超过该时间,才能够转变成canceled,避免cancel了之后,才进入prepared +} + +var Config = &dtmsvrConfig{ + PreparedExpire: 60, +} + var configLoaded = false func LoadConfig() { @@ -49,4 +57,6 @@ func LoadConfig() { viper.SetConfigFile(filepath.Dir(file) + "/dtmsvr.yml") err := viper.ReadInConfig() common.PanicIfError(err) + err = viper.Unmarshal(&Config) + common.PanicIfError(err) } diff --git a/dtmsvr/cron.go b/dtmsvr/cron.go index 8a3a5d9..d6bf24c 100644 --- a/dtmsvr/cron.go +++ b/dtmsvr/cron.go @@ -6,8 +6,8 @@ import ( "time" "github.com/sirupsen/logrus" + "github.com/yedf/dtm" "github.com/yedf/dtm/common" - "github.com/yedf/dtm/dtm" ) func CronPreparedOnce(expire time.Duration) { @@ -25,8 +25,11 @@ func CronPreparedOnce(expire time.Duration) { common.PanicIfError(err) body := resp.String() if strings.Contains(body, "FAIL") { - writeTransLog(sm.Gid, "saga canceled", "canceled", -1, "") - db.Must().Model(&sm).Where("status = ?", "prepared").Update("status", "canceled") + preparedExpire := time.Now().Add(time.Duration(-Config.PreparedExpire) * time.Second) + logrus.Printf("create time: %s prepared expire: %s ", sm.CreateTime.Local(), preparedExpire.Local()) + status := common.If(sm.CreateTime.Before(preparedExpire), "canceled", "prepared").(string) + writeTransLog(sm.Gid, "saga canceled", status, -1, "") + db.Must().Model(&sm).Where("status = ?", "prepared").Update("status", status) } else if strings.Contains(body, "SUCCESS") { saveCommitedSagaModel(&sm) ProcessCommitedSaga(sm.Gid) diff --git a/dtmsvr/dtmsvr_test.go b/dtmsvr/dtmsvr_test.go index b6694b0..fdebf34 100644 --- a/dtmsvr/dtmsvr_test.go +++ b/dtmsvr/dtmsvr_test.go @@ -7,29 +7,21 @@ import ( "github.com/go-playground/assert/v2" "github.com/sirupsen/logrus" "github.com/spf13/viper" + "github.com/yedf/dtm" "github.com/yedf/dtm/common" - "github.com/yedf/dtm/dtm" "github.com/yedf/dtm/examples" ) -func TestViper(t *testing.T) { - assert.Equal(t, true, viper.Get("mysql") != nil) -} - -func TestCover(t *testing.T) { - db := DbGet() - db.NoMust() - CronPreparedOnce(0) - CronCommitedOnce(0) - defer handlePanic() - checkAffected(db.DB) -} - var myinit int = func() int { LoadConfig() return 0 }() +func TestViper(t *testing.T) { + assert.Equal(t, true, viper.Get("mysql") != nil) + assert.Equal(t, int64(90), Config.PreparedExpire) +} + func TestDtmSvr(t *testing.T) { SagaProcessedTestChan = make(chan string, 1) // 清理数据 @@ -47,12 +39,15 @@ func TestDtmSvr(t *testing.T) { commitedPending(t) noramlSaga(t) rollbackSaga2(t) - // assert.Equal(t, 1, 0) - // 开始测试 +} - // 发送Prepare请求后,验证数据库 - // ConsumeHalfMsg 验证数据库 - // ConsumeMsg 验证数据库 +func TestCover(t *testing.T) { + db := DbGet() + db.NoMust() + CronPreparedOnce(0) + CronCommitedOnce(0) + defer handlePanic() + checkAffected(db.DB) } // 测试使用的全局对象 @@ -99,8 +94,10 @@ func prepareCancel(t *testing.T) { saga := genSaga("gid1-prepareCancel", false, true) saga.Prepare() examples.TransQueryResult = "FAIL" + Config.PreparedExpire = 0 CronPreparedOnce(-10 * time.Second) examples.TransQueryResult = "" + Config.PreparedExpire = 60 assert.Equal(t, "canceled", getSagaModel(saga.Gid).Status) } diff --git a/dtmsvr/service.go b/dtmsvr/service.go index bafb8dc..186a6c6 100644 --- a/dtmsvr/service.go +++ b/dtmsvr/service.go @@ -6,8 +6,8 @@ import ( "time" "github.com/sirupsen/logrus" + "github.com/yedf/dtm" "github.com/yedf/dtm/common" - "github.com/yedf/dtm/dtm" "gorm.io/gorm" "gorm.io/gorm/clause" ) diff --git a/examples/main.go b/examples/main.go index 9c5a9fa..584cab5 100644 --- a/examples/main.go +++ b/examples/main.go @@ -4,8 +4,8 @@ import ( "time" "github.com/sirupsen/logrus" + "github.com/yedf/dtm" "github.com/yedf/dtm/common" - "github.com/yedf/dtm/dtm" ) func Main() { diff --git a/main.go b/main.go deleted file mode 100644 index 9be05a1..0000000 --- a/main.go +++ /dev/null @@ -1,17 +0,0 @@ -package main - -import ( - "time" - - "github.com/yedf/dtm/dtmsvr" - "github.com/yedf/dtm/examples" -) - -type M = map[string]interface{} - -func main() { - dtmsvr.LoadConfig() - go dtmsvr.StartSvr() - go examples.StartSvr() - time.Sleep(1000 * 1000 * 1000 * 1000) -} diff --git a/dtm/saga.go b/saga.go similarity index 100% rename from dtm/saga.go rename to saga.go