go build ok

This commit is contained in:
yedongfu 2021-05-22 11:50:38 +08:00
parent c88792bba5
commit c0222e6279
9 changed files with 59 additions and 41 deletions

1
.gitignore vendored
View File

@ -1,2 +1,3 @@
dtmsvr/dtmsvr.yml
*.out
main

24
app/main.go Normal file
View File

@ -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)
}

View File

@ -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)
}

View File

@ -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)

View File

@ -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)
}

View File

@ -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"
)

View File

@ -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() {

17
main.go
View File

@ -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)
}