go build ok
This commit is contained in:
parent
c88792bba5
commit
c0222e6279
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,2 +1,3 @@
|
|||||||
dtmsvr/dtmsvr.yml
|
dtmsvr/dtmsvr.yml
|
||||||
*.out
|
*.out
|
||||||
|
main
|
||||||
|
|||||||
24
app/main.go
Normal file
24
app/main.go
Normal 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)
|
||||||
|
}
|
||||||
@ -37,6 +37,14 @@ func (f *formatter) Format(entry *logrus.Entry) ([]byte, error) {
|
|||||||
return b.Bytes(), nil
|
return b.Bytes(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type dtmsvrConfig struct {
|
||||||
|
PreparedExpire int64 `json:"prepare_expire"` // 单位秒,当prepared的状态超过该时间,才能够转变成canceled,避免cancel了之后,才进入prepared
|
||||||
|
}
|
||||||
|
|
||||||
|
var Config = &dtmsvrConfig{
|
||||||
|
PreparedExpire: 60,
|
||||||
|
}
|
||||||
|
|
||||||
var configLoaded = false
|
var configLoaded = false
|
||||||
|
|
||||||
func LoadConfig() {
|
func LoadConfig() {
|
||||||
@ -49,4 +57,6 @@ func LoadConfig() {
|
|||||||
viper.SetConfigFile(filepath.Dir(file) + "/dtmsvr.yml")
|
viper.SetConfigFile(filepath.Dir(file) + "/dtmsvr.yml")
|
||||||
err := viper.ReadInConfig()
|
err := viper.ReadInConfig()
|
||||||
common.PanicIfError(err)
|
common.PanicIfError(err)
|
||||||
|
err = viper.Unmarshal(&Config)
|
||||||
|
common.PanicIfError(err)
|
||||||
}
|
}
|
||||||
|
|||||||
@ -6,8 +6,8 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
|
"github.com/yedf/dtm"
|
||||||
"github.com/yedf/dtm/common"
|
"github.com/yedf/dtm/common"
|
||||||
"github.com/yedf/dtm/dtm"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func CronPreparedOnce(expire time.Duration) {
|
func CronPreparedOnce(expire time.Duration) {
|
||||||
@ -25,8 +25,11 @@ func CronPreparedOnce(expire time.Duration) {
|
|||||||
common.PanicIfError(err)
|
common.PanicIfError(err)
|
||||||
body := resp.String()
|
body := resp.String()
|
||||||
if strings.Contains(body, "FAIL") {
|
if strings.Contains(body, "FAIL") {
|
||||||
writeTransLog(sm.Gid, "saga canceled", "canceled", -1, "")
|
preparedExpire := time.Now().Add(time.Duration(-Config.PreparedExpire) * time.Second)
|
||||||
db.Must().Model(&sm).Where("status = ?", "prepared").Update("status", "canceled")
|
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") {
|
} else if strings.Contains(body, "SUCCESS") {
|
||||||
saveCommitedSagaModel(&sm)
|
saveCommitedSagaModel(&sm)
|
||||||
ProcessCommitedSaga(sm.Gid)
|
ProcessCommitedSaga(sm.Gid)
|
||||||
|
|||||||
@ -7,29 +7,21 @@ import (
|
|||||||
"github.com/go-playground/assert/v2"
|
"github.com/go-playground/assert/v2"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
|
"github.com/yedf/dtm"
|
||||||
"github.com/yedf/dtm/common"
|
"github.com/yedf/dtm/common"
|
||||||
"github.com/yedf/dtm/dtm"
|
|
||||||
"github.com/yedf/dtm/examples"
|
"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 {
|
var myinit int = func() int {
|
||||||
LoadConfig()
|
LoadConfig()
|
||||||
return 0
|
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) {
|
func TestDtmSvr(t *testing.T) {
|
||||||
SagaProcessedTestChan = make(chan string, 1)
|
SagaProcessedTestChan = make(chan string, 1)
|
||||||
// 清理数据
|
// 清理数据
|
||||||
@ -47,12 +39,15 @@ func TestDtmSvr(t *testing.T) {
|
|||||||
commitedPending(t)
|
commitedPending(t)
|
||||||
noramlSaga(t)
|
noramlSaga(t)
|
||||||
rollbackSaga2(t)
|
rollbackSaga2(t)
|
||||||
// assert.Equal(t, 1, 0)
|
}
|
||||||
// 开始测试
|
|
||||||
|
|
||||||
// 发送Prepare请求后,验证数据库
|
func TestCover(t *testing.T) {
|
||||||
// ConsumeHalfMsg 验证数据库
|
db := DbGet()
|
||||||
// ConsumeMsg 验证数据库
|
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 := genSaga("gid1-prepareCancel", false, true)
|
||||||
saga.Prepare()
|
saga.Prepare()
|
||||||
examples.TransQueryResult = "FAIL"
|
examples.TransQueryResult = "FAIL"
|
||||||
|
Config.PreparedExpire = 0
|
||||||
CronPreparedOnce(-10 * time.Second)
|
CronPreparedOnce(-10 * time.Second)
|
||||||
examples.TransQueryResult = ""
|
examples.TransQueryResult = ""
|
||||||
|
Config.PreparedExpire = 60
|
||||||
assert.Equal(t, "canceled", getSagaModel(saga.Gid).Status)
|
assert.Equal(t, "canceled", getSagaModel(saga.Gid).Status)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -6,8 +6,8 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
|
"github.com/yedf/dtm"
|
||||||
"github.com/yedf/dtm/common"
|
"github.com/yedf/dtm/common"
|
||||||
"github.com/yedf/dtm/dtm"
|
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
"gorm.io/gorm/clause"
|
"gorm.io/gorm/clause"
|
||||||
)
|
)
|
||||||
|
|||||||
@ -4,8 +4,8 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
|
"github.com/yedf/dtm"
|
||||||
"github.com/yedf/dtm/common"
|
"github.com/yedf/dtm/common"
|
||||||
"github.com/yedf/dtm/dtm"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func Main() {
|
func Main() {
|
||||||
|
|||||||
17
main.go
17
main.go
@ -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)
|
|
||||||
}
|
|
||||||
Loading…
x
Reference in New Issue
Block a user