Browse Source

refactor forceStop

pull/251/head
yedf2 4 years ago
parent
commit
830ac1ede8
  1. 23
      Makefile
  2. 7
      dtmsvr/api.go
  3. 2
      dtmsvr/storage/boltdb/boltdb.go
  4. 2
      dtmsvr/storage/redis/redis.go
  5. 2
      dtmsvr/storage/sql/sql.go
  6. 9
      dtmsvr/trans_status.go
  7. 31
      test/api_test.go

23
Makefile

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

7
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 {

2
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 {

2
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()

2
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())

9
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

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

Loading…
Cancel
Save