Skip to content

Commit

Permalink
feature add fence for tcc, and add fence sample in tcc local mode. (a…
Browse files Browse the repository at this point in the history
  • Loading branch information
106umao authored Sep 15, 2022
1 parent 5da5904 commit 3b395cf
Show file tree
Hide file tree
Showing 38 changed files with 1,750 additions and 172 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.16
require (
dubbo.apache.org/dubbo-go/v3 v3.0.2-0.20220508105316-b27ec53b7bab
github.com/BurntSushi/toml v1.1.0 // indirect
github.com/DATA-DOG/go-sqlmock v1.5.0
github.com/agiledragon/gomonkey v2.0.2+incompatible
github.com/apache/dubbo-getty v1.4.8
github.com/dubbogo/gost v1.12.5
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ github.com/BurntSushi/toml v1.1.0/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbi
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/CloudyKit/fastprinter v0.0.0-20170127035650-74b38d55f37a/go.mod h1:EFZQ978U7x8IRnstaskI3IysnWY5Ao3QgZUKOXlsAdw=
github.com/CloudyKit/jet v2.1.3-0.20180809161101-62edd43e4f88+incompatible/go.mod h1:HPYO+50pSWkPoj9Q/eq0aRGByCL6ScRlUmiEX5Zgm+w=
github.com/DATA-DOG/go-sqlmock v1.5.0 h1:Shsta01QNfFxHCfpW6YH2STWB0MudeXXEWMr20OEh60=
github.com/DATA-DOG/go-sqlmock v1.5.0/go.mod h1:f/Ixk793poVmq4qj/V1dPUg2JEAKC73Q5eFN3EC/SaM=
github.com/DataDog/datadog-go v3.2.0+incompatible/go.mod h1:LButxg5PwREeZtORoXG3tL4fMGNddJ+vMq1mwgfaqoQ=
github.com/DataDog/zstd v1.4.5/go.mod h1:1jcaCB/ufaK+sKp1NBhlGmpz41jOoPQ35bpF36t7BBo=
Expand Down
137 changes: 0 additions & 137 deletions pkg/common/error/error.go

This file was deleted.

56 changes: 56 additions & 0 deletions pkg/common/errors/error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package errors

import (
"fmt"

"github.com/pkg/errors"
)

var (
ErrorTooManySessions = errors.New("too many sessions")
ErrorHeartBeatTimeOut = errors.New("heart beat time out")
)

type TransactionError struct {
Code byte
Message string
}

func (e TransactionError) Error() string {
return fmt.Sprintf("TransactionError code %d, msg %s", e.Code, e.Message)
}

type TccFenceError struct {
Code TransactionErrorCode
Message string
Parent error
}

func (e TccFenceError) Error() string {
return fmt.Sprintf("TccFenceError code %d, msg %s, parent msg is %s", e.Code, e.Message, e.Parent)
}

func NewTccFenceError(code TransactionErrorCode, msg string, parent error) *TccFenceError {
return &TccFenceError{
code,
msg,
parent,
}
}
103 changes: 103 additions & 0 deletions pkg/common/errors/error_code.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package errors

type TransactionErrorCode int32

const (
// TransactionErrorCodeUnknown Unknown transaction errors code.
TransactionErrorCodeUnknown = TransactionErrorCode(0)

// TransactionErrorCodeBeginFailed BeginFailed
TransactionErrorCodeBeginFailed = TransactionErrorCode(1)

// TransactionErrorCodeLockKeyConflict Lock key conflict transaction errors code.
TransactionErrorCodeLockKeyConflict = TransactionErrorCode(2)

// Io transaction errors code.
IO = TransactionErrorCode(3)

// TransactionErrorCodeBranchRollbackFailedRetriable Branch rollback failed retriable transaction errors code.
TransactionErrorCodeBranchRollbackFailedRetriable = TransactionErrorCode(4)

// TransactionErrorCodeBranchRollbackFailedUnretriable Branch rollback failed unretriable transaction errors code.
TransactionErrorCodeBranchRollbackFailedUnretriable = TransactionErrorCode(5)

// TransactionErrorCodeBranchRegisterFailed Branch register failed transaction errors code.
TransactionErrorCodeBranchRegisterFailed = TransactionErrorCode(6)

// TransactionErrorCodeBranchReportFailed Branch report failed transaction errors code.
TransactionErrorCodeBranchReportFailed = TransactionErrorCode(7)

// TransactionErrorCodeLockableCheckFailed Lockable check failed transaction errors code.
TransactionErrorCodeLockableCheckFailed = TransactionErrorCode(8)

// TransactionErrorCodeBranchTransactionNotExist Branch transaction not exist transaction errors code.
TransactionErrorCodeBranchTransactionNotExist = TransactionErrorCode(9)

// TransactionErrorCodeGlobalTransactionNotExist Global transaction not exist transaction errors code.
TransactionErrorCodeGlobalTransactionNotExist = TransactionErrorCode(10)

// TransactionErrorCodeGlobalTransactionNotActive Global transaction not active transaction errors code.
TransactionErrorCodeGlobalTransactionNotActive = TransactionErrorCode(11)

// TransactionErrorCodeGlobalTransactionStatusInvalid Global transaction status invalid transaction errors code.
TransactionErrorCodeGlobalTransactionStatusInvalid = TransactionErrorCode(12)

// TransactionErrorCodeFailedToSendBranchCommitRequest Failed to send branch commit request transaction errors code.
TransactionErrorCodeFailedToSendBranchCommitRequest = TransactionErrorCode(13)

// TransactionErrorCodeFailedToSendBranchRollbackRequest Failed to send branch rollback request transaction errors code.
TransactionErrorCodeFailedToSendBranchRollbackRequest = TransactionErrorCode(14)

// TransactionErrorCodeFailedToAddBranch Failed to add branch transaction errors code.
TransactionErrorCodeFailedToAddBranch = TransactionErrorCode(15)

// TransactionErrorCodeFailedLockGlobalTranscation Failed to lock global transaction errors code.
TransactionErrorCodeFailedLockGlobalTranscation = TransactionErrorCode(16)

// TransactionErrorCodeFailedWriteSession FailedWriteSession
TransactionErrorCodeFailedWriteSession = TransactionErrorCode(17)

// FailedStore Failed to holder errors code
FailedStore = TransactionErrorCode(18)

// LockKeyConflictFailFast Lock key conflict fail fast transaction exception code.
LockKeyConflictFailFast = TransactionErrorCode(19)

// TccFenceDbDuplicateKeyError Insert tcc fence record duplicate key errors
TccFenceDbDuplicateKeyError = TransactionErrorCode(20)

// RollbackFenceError rollback tcc fence error
RollbackFenceError = TransactionErrorCode(21)

// CommitFenceError commit tcc fence error
CommitFenceError = TransactionErrorCode(22)

// TccFenceDbError query tcc fence prepare sql failed
TccFenceDbError = TransactionErrorCode(23)

// PrepareFenceError prepare tcc fence error
PrepareFenceError = TransactionErrorCode(24)

// FenceBusinessError callback business method maybe return this error type
FenceBusinessError = TransactionErrorCode(26)

// FencePhaseError have fence phase but is not illegal value
FencePhaseError = TransactionErrorCode(27)
)
7 changes: 4 additions & 3 deletions pkg/protocol/codec/branch_commit_response_codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ package codec
import (
"math"

serror "github.com/seata/seata-go/pkg/common/errors"

"github.com/seata/seata-go/pkg/common/bytes"
serror "github.com/seata/seata-go/pkg/common/error"
"github.com/seata/seata-go/pkg/protocol/branch"
"github.com/seata/seata-go/pkg/protocol/message"
)
Expand All @@ -41,7 +42,7 @@ func (g *BranchCommitResponseCodec) Decode(in []byte) interface{} {
if data.ResultCode == message.ResultCodeFailed {
data.Msg = bytes.ReadString8Length(buf)
}
data.TransactionExceptionCode = serror.TransactionExceptionCode(bytes.ReadByte(buf))
data.TransactionErrorCode = serror.TransactionErrorCode(bytes.ReadByte(buf))
data.Xid = bytes.ReadString16Length(buf)
data.BranchId = int64(bytes.ReadUInt64(buf))
data.BranchStatus = branch.BranchStatus(bytes.ReadByte(buf))
Expand All @@ -61,7 +62,7 @@ func (g *BranchCommitResponseCodec) Encode(in interface{}) []byte {
}
bytes.WriteString8Length(msg, buf)
}
buf.WriteByte(byte(data.TransactionExceptionCode))
buf.WriteByte(byte(data.TransactionErrorCode))
bytes.WriteString16Length(data.Xid, buf)
buf.WriteInt64(data.BranchId)
buf.WriteByte(byte(data.BranchStatus))
Expand Down
7 changes: 3 additions & 4 deletions pkg/protocol/codec/branch_commit_response_codec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,11 @@ package codec
import (
"testing"

serror "github.com/seata/seata-go/pkg/common/error"
"github.com/stretchr/testify/assert"

serror "github.com/seata/seata-go/pkg/common/errors"
model2 "github.com/seata/seata-go/pkg/protocol/branch"

"github.com/seata/seata-go/pkg/protocol/message"
"github.com/stretchr/testify/assert"
)

func TestBranchCommitResponseCodec(t *testing.T) {
Expand All @@ -35,7 +34,7 @@ func TestBranchCommitResponseCodec(t *testing.T) {
BranchId: 56678,
BranchStatus: model2.BranchStatusPhaseoneFailed,
AbstractTransactionResponse: message.AbstractTransactionResponse{
TransactionExceptionCode: serror.TransactionExceptionCodeBeginFailed,
TransactionErrorCode: serror.TransactionErrorCodeBeginFailed,
AbstractResultMessage: message.AbstractResultMessage{
ResultCode: message.ResultCodeFailed,
Msg: "FAILED",
Expand Down
6 changes: 3 additions & 3 deletions pkg/protocol/codec/branch_register_response_codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"math"

"github.com/seata/seata-go/pkg/common/bytes"
serror "github.com/seata/seata-go/pkg/common/error"
serror "github.com/seata/seata-go/pkg/common/errors"
"github.com/seata/seata-go/pkg/protocol/message"
)

Expand All @@ -40,7 +40,7 @@ func (g *BranchRegisterResponseCodec) Decode(in []byte) interface{} {
if data.ResultCode == message.ResultCodeFailed {
data.Msg = bytes.ReadString16Length(buf)
}
data.TransactionExceptionCode = serror.TransactionExceptionCode(bytes.ReadByte(buf))
data.TransactionErrorCode = serror.TransactionErrorCode(bytes.ReadByte(buf))
data.BranchId = int64(bytes.ReadUInt64(buf))

return data
Expand All @@ -58,7 +58,7 @@ func (c *BranchRegisterResponseCodec) Encode(in interface{}) []byte {
}
bytes.WriteString16Length(msg, buf)
}
buf.WriteByte(byte(data.TransactionExceptionCode))
buf.WriteByte(byte(data.TransactionErrorCode))
buf.WriteInt64(data.BranchId)

return buf.Bytes()
Expand Down
Loading

0 comments on commit 3b395cf

Please sign in to comment.