Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature add tcc branch report #190

Merged
merged 13 commits into from
Aug 9, 2022
2 changes: 0 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@ require (
dubbo.apache.org/dubbo-go/v3 v3.0.2-0.20220508105316-b27ec53b7bab
github.com/BurntSushi/toml v1.1.0 // indirect
github.com/apache/dubbo-getty v1.4.8
github.com/apache/dubbo-go-hessian2 v1.11.0
github.com/dubbogo/gost v1.12.5
github.com/natefinch/lumberjack v2.0.0+incompatible
github.com/pkg/errors v0.9.1
github.com/stretchr/testify v1.7.1
go.uber.org/atomic v1.9.0
go.uber.org/zap v1.21.0
golang.org/x/tools v0.1.11 // indirect
vimagination.zapto.org/byteio v0.0.0-20200222190125-d27cba0f0b10
vimagination.zapto.org/memio v0.0.0-20200222190306-588ebc67b97d // indirect
)
8 changes: 0 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -770,7 +770,6 @@ 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.1/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
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=
Expand Down Expand Up @@ -841,7 +840,6 @@ golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8U
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20201002170205-7f63de1d35b0/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3 h1:0es+/5331RGQPcXlMfP+WrnIIS6dNnNRe0WB02W0F4M=
golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/exp v0.0.0-20180321215751-8460e604b9de/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
Expand Down Expand Up @@ -881,8 +879,6 @@ golang.org/x/mod v0.1.1-0.20191107180719-034126e5016b/go.mod h1:QqPTAvyqsEbceGzB
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 h1:6zppjxzCulZykYSLyVDYbneBfbaBIQPYMevg0bEwv2s=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/net v0.0.0-20180530234432-1e491301e022/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
Expand Down Expand Up @@ -927,7 +923,6 @@ golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM=
golang.org/x/net v0.0.0-20210525063256-abc453219eb5/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20210917221730-978cfadd31cf/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20211015210444-4f30a5c0130f/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20211029224645-99673261e6eb/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20211105192438-b53810dc28af/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2 h1:CIJ76btIcR3eFI5EgSo6k1qKw9KJexJuRLI9G7Hp5wE=
Expand Down Expand Up @@ -1017,7 +1012,6 @@ golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210806184541-e5e7981a1069/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210816074244-15123e1e1f71/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211019181941-9d821ace8654/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211106132015-ebca88c72f68/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211117180635-dee7805ff2e1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220111092808-5a964db01320/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
Expand Down Expand Up @@ -1099,8 +1093,6 @@ 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.11 h1:loJ25fNOEhSXfHrpoGj91eCUThwdNX6u24rO1xnNteY=
golang.org/x/tools v0.1.11/go.mod h1:SgwaegtQh8clINPpECJMqnxLv9I09HLqnW3RMqW0CA4=
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=
Expand Down
45 changes: 25 additions & 20 deletions pkg/common/error/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down
33 changes: 29 additions & 4 deletions pkg/rm/rm_remoting.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package rm

import (
"github.com/pkg/errors"
"sync"

"github.com/seata/seata-go/pkg/protocol/resource"
Expand All @@ -33,6 +34,10 @@ var (
onceGettyRemoting = &sync.Once{}
)

var (
ErrBranchReportResponseFault = errors.New("branch report response fault")
)

func GetRMRemotingInstance() *RMRemoting {
if rmRemoting == nil {
onceGettyRemoting.Do(func() {
Expand Down Expand Up @@ -62,20 +67,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 = branchReportResultDecode(resp); err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

看原来已经有 isReportSuccess 方法来判断code是否为成功,这里还需要加branchReportResultDecode方法吗?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

嗯,从 功能上来看 isReportSuccess 是解析返回值并返回对应响应码,然后在外部根据错误码判断返回结果 ,branchReportResultDecode 是解析返回值并根据错误码判断从而返回最终成功或失败结果,我的想法是把branchReportResultDecode 里面的代码放到 isReportSuccess 里面是否更合适,然后删除branchReportResultDecode 方法。isReportSuccess 从命名含义上看像是返回一个最终的成功或失败结果,而不是返回错误码。

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

可以哈

log.Errorf("BranchReport response error: %v, res %v", err.Error(), resp)
return err
}

return nil
}

Expand Down Expand Up @@ -123,6 +135,19 @@ func isReportSuccess(response interface{}) message.ResultCode {
return message.ResultCodeFailed
}

// branchReportResultDecode analyze response result
func branchReportResultDecode(response interface{}) error {
if res, ok := response.(message.BranchReportResponse); ok {
if res.ResultCode == message.ResultCodeFailed {
return errors.New(res.Msg)
}
} else {
return ErrBranchReportResponseFault
}

return nil
}

func (r *RMRemoting) onRegisterRMSuccess(response message.RegisterRMResponse) {
log.Infof("register RM success. response: %#v", response)
}
Expand Down
5 changes: 2 additions & 3 deletions pkg/rm/tcc/tcc_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,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) {
Expand Down
15 changes: 15 additions & 0 deletions pkg/rm/tcc/tcc_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
package tcc

import (
"context"
"encoding/json"
"github.com/seata/seata-go/pkg/protocol/branch"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

提交前执行 go goimport.sh命令格式化下import格式

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -34,3 +36,16 @@ func TestActionContext(t *testing.T) {
assert.Nil(t, err)
assert.Equal(t, `{"zhangsan":"lisi"}`, string(bytes))
}

// TestBranchReport
func TestBranchReport(t *testing.T) {
err := GetTCCResourceManagerInstance().BranchReport(
context.Background(),
branch.BranchTypeTCC,
"1111111111",
2645276141,
branch.BranchStatusPhaseoneDone,
`{"actionContext":{"zhangsan":"lisi"}}`)

assert.Nil(t, err)
}