From d19d5e6100dd06cc4f63d572fd237ca2639bed6a Mon Sep 17 00:00:00 2001 From: wang1309 <1028960576@qq.com> Date: Tue, 9 Aug 2022 08:22:52 +0800 Subject: [PATCH] Feature add tcc branch report (#190) feature: add tcc branch report --- go.mod | 1 + go.sum | 3 +++ pkg/common/error/error.go | 45 ++++++++++++++++++--------------- pkg/rm/rm_remoting.go | 35 ++++++++++++++++++++----- pkg/rm/tcc/tcc_resource.go | 5 ++-- pkg/rm/tcc/tcc_resource_test.go | 14 ++++++++++ 6 files changed, 73 insertions(+), 30 deletions(-) diff --git a/go.mod b/go.mod index e04c13223..3b258e09b 100644 --- a/go.mod +++ b/go.mod @@ -16,6 +16,7 @@ require ( golang.org/x/net v0.0.0-20220722155237-a158d28d115b // indirect golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4 // indirect golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f // indirect + golang.org/x/tools v0.1.12 // indirect vimagination.zapto.org/byteio v0.0.0-20200222190125-d27cba0f0b10 vimagination.zapto.org/memio v0.0.0-20200222190306-588ebc67b97d // indirect ) diff --git a/go.sum b/go.sum index a2f15438c..636c9fa39 100644 --- a/go.sum +++ b/go.sum @@ -770,6 +770,7 @@ github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9de github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= +github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= github.com/yusufpapurcu/wmi v1.2.2 h1:KBNDSne4vP5mbSWnJbO+51IMOXJB67QiYCSBrubbPRg= github.com/yusufpapurcu/wmi v1.2.2/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQmPyzfmi0= github.com/zouyx/agollo/v3 v3.4.5 h1:7YCxzY9ZYaH9TuVUBvmI6Tk0mwMggikah+cfbYogcHQ= @@ -1098,6 +1099,8 @@ golang.org/x/tools v0.0.0-20201014170642-d1624618ad65/go.mod h1:z6u4i615ZeAfBE4X golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= golang.org/x/tools v0.1.2/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= golang.org/x/tools v0.1.5/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= +golang.org/x/tools v0.1.12 h1:VveCTK38A2rkS8ZqFY25HIDFscX5X9OoEhJd3quQmXU= +golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= diff --git a/pkg/common/error/error.go b/pkg/common/error/error.go index dd4011e7f..ceccdd7f5 100644 --- a/pkg/common/error/error.go +++ b/pkg/common/error/error.go @@ -20,8 +20,8 @@ package error import "github.com/pkg/errors" var ( - Error_TooManySessions = errors.New("too many seeessions") - Error_HeartBeatTimeOut = errors.New("heart beat time out") + ErrorTooManySessions = errors.New("too many seeessions") + ErrorHeartBeatTimeOut = errors.New("heart beat time out") ) type TransactionExceptionCode byte @@ -35,91 +35,96 @@ const ( /** * BeginFailed */ - TransactionExceptionCodeBeginFailed = 1 + TransactionExceptionCodeBeginFailed = TransactionExceptionCode(1) /** * Lock key conflict transaction exception code. */ - TransactionExceptionCodeLockKeyConflict = 2 + TransactionExceptionCodeLockKeyConflict = TransactionExceptionCode(2) /** * Io transaction exception code. */ - + IO = TransactionExceptionCode(3) /** * Branch rollback failed retriable transaction exception code. */ - TransactionExceptionCodeBranchRollbackFailedRetriable = 3 + TransactionExceptionCodeBranchRollbackFailedRetriable = TransactionExceptionCode(4) /** * Branch rollback failed unretriable transaction exception code. */ - TransactionExceptionCodeBranchRollbackFailedUnretriable = 4 + TransactionExceptionCodeBranchRollbackFailedUnretriable = TransactionExceptionCode(5) /** * Branch register failed transaction exception code. */ - TransactionExceptionCodeBranchRegisterFailed = 5 + TransactionExceptionCodeBranchRegisterFailed = TransactionExceptionCode(6) /** * Branch report failed transaction exception code. */ - TransactionExceptionCodeBranchReportFailed = 6 + TransactionExceptionCodeBranchReportFailed = TransactionExceptionCode(7) /** * Lockable check failed transaction exception code. */ - TransactionExceptionCodeLockableCheckFailed = 7 + TransactionExceptionCodeLockableCheckFailed = TransactionExceptionCode(8) /** * Branch transaction not exist transaction exception code. */ - TransactionExceptionCodeBranchTransactionNotExist = 8 + TransactionExceptionCodeBranchTransactionNotExist = TransactionExceptionCode(9) /** * Global transaction not exist transaction exception code. */ - TransactionExceptionCodeGlobalTransactionNotExist = 9 + TransactionExceptionCodeGlobalTransactionNotExist = TransactionExceptionCode(10) /** * Global transaction not active transaction exception code. */ - TransactionExceptionCodeGlobalTransactionNotActive = 10 + TransactionExceptionCodeGlobalTransactionNotActive = TransactionExceptionCode(11) /** * Global transaction status invalid transaction exception code. */ - TransactionExceptionCodeGlobalTransactionStatusInvalid = 11 + TransactionExceptionCodeGlobalTransactionStatusInvalid = TransactionExceptionCode(12) /** * Failed to send branch commit request transaction exception code. */ - TransactionExceptionCodeFailedToSendBranchCommitRequest = 12 + TransactionExceptionCodeFailedToSendBranchCommitRequest = TransactionExceptionCode(13) /** * Failed to send branch rollback request transaction exception code. */ - TransactionExceptionCodeFailedToSendBranchRollbackRequest = 13 + TransactionExceptionCodeFailedToSendBranchRollbackRequest = TransactionExceptionCode(14) /** * Failed to add branch transaction exception code. */ - TransactionExceptionCodeFailedToAddBranch = 14 + TransactionExceptionCodeFailedToAddBranch = TransactionExceptionCode(15) /** * Failed to lock global transaction exception code. */ - TransactionExceptionCodeFailedLockGlobalTranscation = 15 + TransactionExceptionCodeFailedLockGlobalTranscation = TransactionExceptionCode(16) /** * FailedWriteSession */ - TransactionExceptionCodeFailedWriteSession = 16 + TransactionExceptionCodeFailedWriteSession = TransactionExceptionCode(17) /** * Failed to holder exception code */ - FailedStore = 17 + FailedStore = TransactionExceptionCode(18) + + /** + * Lock key conflict fail fast transaction exception code. + */ + LockKeyConflictFailFast = TransactionExceptionCode(19) ) type TransactionException struct { diff --git a/pkg/rm/rm_remoting.go b/pkg/rm/rm_remoting.go index 00f958bae..31c584fcb 100644 --- a/pkg/rm/rm_remoting.go +++ b/pkg/rm/rm_remoting.go @@ -20,6 +20,11 @@ package rm import ( "sync" + "github.com/pkg/errors" + + "github.com/seata/seata-go/pkg/protocol/resource" + + "github.com/seata/seata-go/pkg/common/log" "github.com/seata/seata-go/pkg/protocol/branch" "github.com/seata/seata-go/pkg/protocol/message" @@ -31,6 +36,10 @@ var ( onceGettyRemoting = &sync.Once{} ) +var ( + ErrBranchReportResponseFault = errors.New("branch report response fault") +) + func GetRMRemotingInstance() *RMRemoting { if rmRemoting == nil { onceGettyRemoting.Do(func() { @@ -60,20 +69,27 @@ func (RMRemoting) BranchRegister(branchType branch.BranchType, resourceId, clien return resp.(message.BranchRegisterResponse).BranchId, nil } -// Branch report +// BranchReport func (RMRemoting) BranchReport(branchType branch.BranchType, xid string, branchId int64, status branch.BranchStatus, applicationData string) error { request := message.BranchReportRequest{ Xid: xid, BranchId: branchId, Status: status, ApplicationData: []byte(applicationData), - BranchType: branch.BranchTypeAT, + BranchType: branchType, } + resp, err := getty.GetGettyRemotingClient().SendSyncRequest(request) - if err != nil || resp == nil || isReportSuccess(resp) == message.ResultCodeFailed { - log.Errorf("BranchReport error: %v, res %v", err.Error(), resp) + if err != nil { + log.Errorf("branch report request error: %+v", err) + return err + } + + if err = isReportSuccess(resp); err != nil { + log.Errorf("BranchReport response error: %v, res %v", err.Error(), resp) return err } + return nil } @@ -114,11 +130,16 @@ func isRegisterSuccess(response interface{}) bool { return false } -func isReportSuccess(response interface{}) message.ResultCode { +func isReportSuccess(response interface{}) error { if res, ok := response.(message.BranchReportResponse); ok { - return res.ResultCode + if res.ResultCode == message.ResultCodeFailed { + return errors.New(res.Msg) + } + } else { + return ErrBranchReportResponseFault } - return message.ResultCodeFailed + + return nil } func (r *RMRemoting) onRegisterRMSuccess(response message.RegisterRMResponse) { diff --git a/pkg/rm/tcc/tcc_resource.go b/pkg/rm/tcc/tcc_resource.go index 38343c4a0..e21faf2e5 100644 --- a/pkg/rm/tcc/tcc_resource.go +++ b/pkg/rm/tcc/tcc_resource.go @@ -94,9 +94,8 @@ func (t *TCCResourceManager) BranchRegister(ctx context.Context, branchType bran return t.rmRemoting.BranchRegister(branch.BranchTypeTCC, resourceId, clientId, xid, applicationData, lockKeys) } -func (t *TCCResourceManager) BranchReport(ctx context.Context, ranchType branch.BranchType, xid string, branchId int64, status branch.BranchStatus, applicationData string) error { - //TODO implement me - panic("implement me") +func (t *TCCResourceManager) BranchReport(ctx context.Context, branchType branch.BranchType, xid string, branchId int64, status branch.BranchStatus, applicationData string) error { + return t.rmRemoting.BranchReport(branchType, xid, branchId, status, applicationData) } func (t *TCCResourceManager) LockQuery(ctx context.Context, ranchType branch.BranchType, resourceId, xid, lockKeys string) (bool, error) { diff --git a/pkg/rm/tcc/tcc_resource_test.go b/pkg/rm/tcc/tcc_resource_test.go index f83b9a2ba..c963482b6 100644 --- a/pkg/rm/tcc/tcc_resource_test.go +++ b/pkg/rm/tcc/tcc_resource_test.go @@ -34,3 +34,17 @@ func TestActionContext(t *testing.T) { assert.Nil(t, err) assert.Equal(t, `{"zhangsan":"lisi"}`, string(bytes)) } + +// TestBranchReport +func TestBranchReport(t *testing.T) { + // todo add mock + /*err := GetTCCResourceManagerInstance().BranchReport( + context.Background(), + branch.BranchTypeTCC, + "1111111111", + 2645276141, + branch.BranchStatusPhaseoneDone, + `{"actionContext":{"zhangsan":"lisi"}}`) + + assert.Nil(t, err)*/ +}