Skip to content

Commit

Permalink
Feature add tcc branch report (#190)
Browse files Browse the repository at this point in the history
feature: add tcc branch report
  • Loading branch information
wang1309 authored Aug 9, 2022
1 parent 8a9164f commit d19d5e6
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 30 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down Expand Up @@ -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=
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
35 changes: 28 additions & 7 deletions pkg/rm/rm_remoting.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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() {
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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) {
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 @@ -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) {
Expand Down
14 changes: 14 additions & 0 deletions pkg/rm/tcc/tcc_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)*/
}

0 comments on commit d19d5e6

Please sign in to comment.