diff --git a/Makefile b/Makefile deleted file mode 100644 index 0096486..0000000 --- a/Makefile +++ /dev/null @@ -1,23 +0,0 @@ -# dev env https://www.dtm.pub/other/develop.html -all: fmt lint test_all cover_test -.PHONY: all - -fmt: - @gofmt -s -w ./ - -lint: - @golangci-lint run - -.PHONY: test -test: - @go test ./... - -test_all: - TEST_STORE=redis go test ./... - TEST_STORE=boltdb go test ./... - TEST_STORE=mysql go test ./... - TEST_STORE=postgres go test ./... - -cover_test: - ./helper/test-cover.sh - diff --git a/dtmsvr/api.go b/dtmsvr/api.go index b2dbefa..5c3d118 100644 --- a/dtmsvr/api.go +++ b/dtmsvr/api.go @@ -61,11 +61,10 @@ func svcAbort(t *TransGlobal) interface{} { func svcForceStop(t *TransGlobal) interface{} { dbt := GetTransGlobal(t.Gid) if dbt.Status == dtmcli.StatusSucceed || dbt.Status == dtmcli.StatusFailed { - return nil + return fmt.Errorf("global transaction force stop error. status: %s. error: %w", dbt.Status, dtmcli.ErrFailure) } - dbt.statusFailed() - branches := GetStore().FindBranches(t.Gid) - return dbt.Process(branches) + dbt.changeStatus(dtmcli.StatusFailed) + return nil } func svcRegisterBranch(transType string, branch *TransBranch, data map[string]string) error { diff --git a/dtmsvr/storage/boltdb/boltdb.go b/dtmsvr/storage/boltdb/boltdb.go index 83d234c..1f53fb2 100644 --- a/dtmsvr/storage/boltdb/boltdb.go +++ b/dtmsvr/storage/boltdb/boltdb.go @@ -26,8 +26,6 @@ type Store struct { retryInterval int64 } -var _ storage.Store = &Store{} - // NewStore will return the boltdb implement // TODO: change to options func NewStore(dataExpire int64, retryInterval int64) *Store { diff --git a/dtmsvr/storage/redis/redis.go b/dtmsvr/storage/redis/redis.go index d2cca87..7e6bbfa 100644 --- a/dtmsvr/storage/redis/redis.go +++ b/dtmsvr/storage/redis/redis.go @@ -26,8 +26,6 @@ var ctx = context.Background() type Store struct { } -var _ storage.Store = &Store{} - // Ping execs ping cmd to redis func (s *Store) Ping() error { _, err := redisGet().Ping(ctx).Result() diff --git a/dtmsvr/storage/sql/sql.go b/dtmsvr/storage/sql/sql.go index fa8bcdd..d87297a 100644 --- a/dtmsvr/storage/sql/sql.go +++ b/dtmsvr/storage/sql/sql.go @@ -26,8 +26,6 @@ var conf = &config.Config type Store struct { } -var _ storage.Store = &Store{} - // Ping execs ping cmd to db func (s *Store) Ping() error { db, err := dtmimp.StandaloneDB(conf.Store.GetDBConf()) diff --git a/dtmsvr/trans_status.go b/dtmsvr/trans_status.go index 876703b..2c1c16a 100644 --- a/dtmsvr/trans_status.go +++ b/dtmsvr/trans_status.go @@ -58,15 +58,6 @@ func (t *TransGlobal) changeStatus(status string) { t.Status = status } -func (t *TransGlobal) statusFailed() { - updates := []string{"status", "update_time"} - now := time.Now() - t.UpdateTime = &now - GetStore().ChangeGlobalStatus(&t.TransGlobalStore, dtmcli.StatusFailed, updates, false) - logger.Infof("StatusFailed to %s ok for %s", dtmcli.StatusFailed, t.TransGlobalStore.String()) - t.Status = dtmcli.StatusFailed -} - func (t *TransGlobal) changeBranchStatus(b *TransBranch, status string, branchPos int) { now := time.Now() b.Status = status diff --git a/test/api_test.go b/test/api_test.go index 54e869d..ff3d5d6 100644 --- a/test/api_test.go +++ b/test/api_test.go @@ -8,11 +8,13 @@ package test import ( "fmt" + "net/http" "strconv" "testing" "github.com/dtm-labs/dtm/dtmcli/dtmimp" "github.com/dtm-labs/dtm/dtmutil" + "github.com/dtm-labs/dtm/test/busi" "github.com/stretchr/testify/assert" ) @@ -100,3 +102,32 @@ func TestAPIResetCronTime(t *testing.T) { return int64(succeedCount), hasRemaining, err }) } + +func TestAPIForceStoppedNormal(t *testing.T) { + saga := genSaga(dtmimp.GetFuncName(), false, false) + busi.MainSwitch.TransOutResult.SetOnce("ONGOING") + saga.Submit() + waitTransProcessed(saga.Gid) + assert.Equal(t, StatusSubmitted, getTransStatus(saga.Gid)) + + resp, err := dtmimp.RestyClient.R().SetBody(map[string]string{ + "gid": saga.Gid, + }).Post(dtmutil.DefaultHTTPServer + "/forceStop") + assert.Nil(t, err) + assert.Equal(t, resp.StatusCode(), http.StatusOK) + assert.Equal(t, StatusFailed, getTransStatus(saga.Gid)) +} + +func TestAPIForceStoppedAbnormal(t *testing.T) { + saga := genSaga(dtmimp.GetFuncName(), false, false) + saga.Submit() + waitTransProcessed(saga.Gid) + assert.Equal(t, []string{StatusPrepared, StatusSucceed, StatusPrepared, StatusSucceed}, getBranchesStatus(saga.Gid)) + assert.Equal(t, StatusSucceed, getTransStatus(saga.Gid)) + + resp, err := dtmimp.RestyClient.R().SetBody(map[string]string{ + "gid": saga.Gid, + }).Post(dtmutil.DefaultHTTPServer + "/forceStop") + assert.Nil(t, err) + assert.Equal(t, resp.StatusCode(), http.StatusConflict) +}