diff --git a/dtmcli/dtmimp/utils.go b/dtmcli/dtmimp/utils.go index 97ca42c..386d834 100644 --- a/dtmcli/dtmimp/utils.go +++ b/dtmcli/dtmimp/utils.go @@ -253,6 +253,7 @@ func RespAsErrorByJSONRPC(resp *resty.Response) error { // DeferDo a common defer do used in dtmcli/dtmgrpc func DeferDo(rerr *error, success func() error, fail func() error) { if x := recover(); x != nil { + *rerr = AsError(x) _ = fail() panic(x) } else if *rerr != nil { diff --git a/dtmcli/tcc.go b/dtmcli/tcc.go index 6c2d191..a0b6ff9 100644 --- a/dtmcli/tcc.go +++ b/dtmcli/tcc.go @@ -41,7 +41,9 @@ func TccGlobalTransaction2(dtm string, gid string, custom func(*Tcc), tccFunc Tc defer dtmimp.DeferDo(&rerr, func() error { return dtmimp.TransCallDtm(&tcc.TransBase, tcc, "submit") }, func() error { - tcc.RollbackReason = rerr.Error() + if rerr != nil { + tcc.RollbackReason = rerr.Error() + } return dtmimp.TransCallDtm(&tcc.TransBase, tcc, "abort") }) _, rerr = tccFunc(tcc) diff --git a/dtmgrpc/dtmgimp/utils.go b/dtmgrpc/dtmgimp/utils.go index d10b321..532c5d5 100644 --- a/dtmgrpc/dtmgimp/utils.go +++ b/dtmgrpc/dtmgimp/utils.go @@ -37,6 +37,7 @@ func DtmGrpcCall(s *dtmimp.TransBase, operation string) error { PassthroughHeaders: s.PassthroughHeaders, BranchHeaders: s.BranchHeaders, RequestTimeout: s.RequestTimeout, + RollbackReason: s.RollbackReason, }, QueryPrepared: s.QueryPrepared, CustomedData: s.CustomData, diff --git a/dtmsvr/trans_class.go b/dtmsvr/trans_class.go index 1e239c0..35d0ee0 100644 --- a/dtmsvr/trans_class.go +++ b/dtmsvr/trans_class.go @@ -94,12 +94,13 @@ func TransFromDtmRequest(ctx context.Context, c *dtmgpb.DtmRequest) *TransGlobal o = c.TransOptions } r := TransGlobal{TransGlobalStore: storage.TransGlobalStore{ - Gid: c.Gid, - TransType: c.TransType, - QueryPrepared: c.QueryPrepared, - Protocol: "grpc", - BinPayloads: c.BinPayloads, - CustomData: c.CustomedData, + Gid: c.Gid, + TransType: c.TransType, + QueryPrepared: c.QueryPrepared, + Protocol: "grpc", + BinPayloads: c.BinPayloads, + CustomData: c.CustomedData, + RollbackReason: o.RollbackReason, TransOptions: dtmcli.TransOptions{ WaitResult: o.WaitResult, TimeoutToFail: o.TimeoutToFail, @@ -107,6 +108,7 @@ func TransFromDtmRequest(ctx context.Context, c *dtmgpb.DtmRequest) *TransGlobal PassthroughHeaders: o.PassthroughHeaders, BranchHeaders: o.BranchHeaders, RequestTimeout: o.RequestTimeout, + RollbackReason: o.RollbackReason, }, }} if c.Steps != "" { diff --git a/test/tcc_grpc_test.go b/test/tcc_grpc_test.go index 5a7c336..6e5e745 100644 --- a/test/tcc_grpc_test.go +++ b/test/tcc_grpc_test.go @@ -52,6 +52,7 @@ func TestTccGrpcRollback(t *testing.T) { cronTransOnce(t, gid) assert.Equal(t, StatusFailed, getTransStatus(gid)) assert.Equal(t, []string{StatusSucceed, StatusPrepared, StatusSucceed, StatusPrepared}, getBranchesStatus(gid)) + assert.Equal(t, "rpc error: code = Aborted desc = reason:", getTrans(gid).RollbackReason) } func TestTccGrpcNested(t *testing.T) {