Browse Source

Merge pull request #316 from xyctruth/main

Support rollback_reason for tcc transaction
pull/317/head
yedf2 4 years ago
committed by GitHub
parent
commit
d88f6e4394
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 1
      dtmcli/dtmimp/trans_base.go
  2. 1
      dtmcli/tcc.go
  3. 1
      dtmgrpc/dtmgpb/dtmgimp.proto
  4. 1
      dtmgrpc/tcc.go
  5. 2
      dtmsvr/api.go
  6. 1
      test/tcc_test.go

1
dtmcli/dtmimp/trans_base.go

@ -50,6 +50,7 @@ type TransOptions struct {
PassthroughHeaders []string `json:"passthrough_headers,omitempty" gorm:"-"` // for inherit the specified gin context headers
BranchHeaders map[string]string `json:"branch_headers,omitempty" gorm:"-"` // custom branch headers, dtm server => service api
Concurrent bool `json:"concurrent" gorm:"-"` // for trans type: saga msg
RollbackReason string `json:"rollback_reason,omitempty" gorm:"-"`
}
// TransBase base for all trans

1
dtmcli/tcc.go

@ -41,6 +41,7 @@ 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()
return dtmimp.TransCallDtm(&tcc.TransBase, tcc, "abort")
})
_, rerr = tccFunc(tcc)

1
dtmgrpc/dtmgpb/dtmgimp.proto

@ -21,6 +21,7 @@ message DtmTransOptions {
repeated string PassthroughHeaders = 4;
map<string, string> BranchHeaders = 5;
int64 RequestTimeout = 6;
string RollbackReason = 7;
}
// DtmRequest request sent to dtm server

1
dtmgrpc/tcc.go

@ -43,6 +43,7 @@ func TccGlobalTransaction2(dtm string, gid string, custom func(*TccGrpc), tccFun
defer dtmimp.DeferDo(&rerr, func() error {
return dtmgimp.DtmGrpcCall(&tcc.TransBase, "Submit")
}, func() error {
tcc.RollbackReason = rerr.Error()
return dtmgimp.DtmGrpcCall(&tcc.TransBase, "Abort")
})
return tccFunc(tcc)

2
dtmsvr/api.go

@ -56,7 +56,7 @@ func svcAbort(t *TransGlobal) interface{} {
if t.TransType != "xa" && t.TransType != "tcc" || dbt.Status != dtmcli.StatusPrepared && dbt.Status != dtmcli.StatusAborting {
return fmt.Errorf("trans type: '%s' current status '%s', cannot abort. %w", dbt.TransType, dbt.Status, dtmcli.ErrFailure)
}
dbt.changeStatus(dtmcli.StatusAborting)
dbt.changeStatus(dtmcli.StatusAborting, withRollbackReason(t.RollbackReason))
branches := GetStore().FindBranches(t.Gid)
return dbt.Process(branches)
}

1
test/tcc_test.go

@ -46,6 +46,7 @@ func TestTccRollback(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, "{\"error\":\"reason:. FAILURE\"}. FAILURE", getTrans(gid).RollbackReason)
}
func TestTccTimeout(t *testing.T) {

Loading…
Cancel
Save