From 0a653d0972b04e953a88e616ae25abb77b80d9be Mon Sep 17 00:00:00 2001 From: yedongfu Date: Wed, 21 Jul 2021 19:38:55 +0800 Subject: [PATCH] check more return values --- dtmcli/barrier.go | 2 +- dtmcli/message.go | 5 +++-- dtmcli/saga.go | 3 ++- dtmcli/tcc.go | 9 ++++++++- dtmsvr/dtmsvr_test.go | 3 ++- 5 files changed, 16 insertions(+), 6 deletions(-) diff --git a/dtmcli/barrier.go b/dtmcli/barrier.go index 8348e23..8051dfc 100644 --- a/dtmcli/barrier.go +++ b/dtmcli/barrier.go @@ -63,7 +63,7 @@ func insertBarrier(tx *sql.Tx, transType string, gid string, branchID string, br if branchType == "" { return 0, nil } - res, err := logExec(tx, "insert into dtm_barrier.barrier(trans_type, gid, branch_id, branch_type, reason) values(?,?,?,?,?)", transType, gid, branchID, branchType, reason) + res, err := logExec(tx, "insert ignore into dtm_barrier.barrier(trans_type, gid, branch_id, branch_type, reason) values(?,?,?,?,?)", transType, gid, branchID, branchType, reason) if err != nil { return 0, err } diff --git a/dtmcli/message.go b/dtmcli/message.go index 933bbe3..e4a1c29 100644 --- a/dtmcli/message.go +++ b/dtmcli/message.go @@ -2,6 +2,7 @@ package dtmcli import ( "fmt" + "strings" jsonitor "github.com/json-iterator/go" "github.com/sirupsen/logrus" @@ -57,7 +58,7 @@ func (s *Msg) Submit() error { if err != nil { return err } - if resp.StatusCode() != 200 { + if !strings.Contains(resp.String(), "SUCCESS") { return fmt.Errorf("submit failed: %v", resp.Body()) } s.Gid = jsonitor.Get(resp.Body(), "gid").ToString() @@ -72,7 +73,7 @@ func (s *Msg) Prepare(queryPrepared string) error { if err != nil { return err } - if resp.StatusCode() != 200 { + if !strings.Contains(resp.String(), "SUCCESS") { return fmt.Errorf("prepare failed: %v", resp.Body()) } return nil diff --git a/dtmcli/saga.go b/dtmcli/saga.go index 0d4deb1..8e34667 100644 --- a/dtmcli/saga.go +++ b/dtmcli/saga.go @@ -2,6 +2,7 @@ package dtmcli import ( "fmt" + "strings" jsonitor "github.com/json-iterator/go" "github.com/sirupsen/logrus" @@ -58,7 +59,7 @@ func (s *Saga) Submit() error { if err != nil { return err } - if resp.StatusCode() != 200 { + if !strings.Contains(resp.String(), "SUCCESS") { return fmt.Errorf("submit failed: %v", resp.Body()) } s.Gid = jsonitor.Get(resp.Body(), "gid").ToString() diff --git a/dtmcli/tcc.go b/dtmcli/tcc.go index 302b66b..84ef449 100644 --- a/dtmcli/tcc.go +++ b/dtmcli/tcc.go @@ -2,6 +2,7 @@ package dtmcli import ( "fmt" + "strings" "github.com/gin-gonic/gin" "github.com/go-resty/resty/v2" @@ -38,10 +39,13 @@ func TccGlobalTransaction(dtm string, tccFunc TccGlobalFunc) (gid string, rerr e } }() tcc := &Tcc{Dtm: dtm, Gid: gid} - _, rerr = common.RestyClient.R().SetBody(data).Post(tcc.Dtm + "/prepare") + resp, rerr := common.RestyClient.R().SetBody(data).Post(tcc.Dtm + "/prepare") if rerr != nil { return } + if !strings.Contains(resp.String(), "SUCCESS") { + rerr = fmt.Errorf("bad response: %s", resp.String()) + } rerr = tccFunc(tcc) return } @@ -77,6 +81,9 @@ func (t *Tcc) CallBranch(body interface{}, tryURL string, confirmURL string, can if err != nil { return resp, err } + if !strings.Contains(resp.String(), "SUCCESS") { + return nil, fmt.Errorf("registerTccBranch failed: %s", resp.String()) + } return common.RestyClient.R(). SetBody(body). SetQueryParams(common.MS{ diff --git a/dtmsvr/dtmsvr_test.go b/dtmsvr/dtmsvr_test.go index dfbca0a..ecb3a38 100644 --- a/dtmsvr/dtmsvr_test.go +++ b/dtmsvr/dtmsvr_test.go @@ -383,7 +383,7 @@ func tccBarrierDisorder(t *testing.T) { go func() { logrus.Printf("sleeping to wait for tcc try timeout") <-timeoutChan - _, _ = common.RestyClient.R(). + r, _ = common.RestyClient.R(). SetBody(body). SetQueryParams(common.MS{ "dtm": tcc.Dtm, @@ -393,6 +393,7 @@ func tccBarrierDisorder(t *testing.T) { "branch_type": "try", }). Post(tryURL) + assert.True(t, strings.Contains(r.String(), "FAILURE")) finishedChan <- "1" }() logrus.Printf("cron to timeout and then call cancel")