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

fix:timeout config & panic log lost #350

Merged
merged 2 commits into from
Nov 19, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions pkg/datasource/sql/conn_at.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"context"
gosql "database/sql"
"database/sql/driver"
"github.com/seata/seata-go/pkg/util/log"
Code-Fight marked this conversation as resolved.
Show resolved Hide resolved

"github.com/seata/seata-go/pkg/datasource/sql/exec"
"github.com/seata/seata-go/pkg/datasource/sql/types"
Expand Down Expand Up @@ -165,10 +166,13 @@ func (c *ATConn) createNewTxOnExecIfNeed(ctx context.Context, f func() (types.Ex
return nil, err
}
}

defer func() {
if tx != nil {
tx.Rollback()
if recoverErr := recover(); recoverErr != nil {
log.Errorf("conn at rollback panic:%v", recoverErr)
if tx != nil {
rollbackErr := tx.Rollback()
log.Errorf("conn at rollback error:%v", rollbackErr)
}
}
}()

Expand Down
2 changes: 1 addition & 1 deletion pkg/rm/tcc/tcc_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ func obtainStructValueType(o interface{}) (bool, reflect.Value, reflect.Type) {
func (t *TCCServiceProxy) GetTransactionInfo() tm.TransactionInfo {
// todo replace with config
return tm.TransactionInfo{
TimeOut: 10000,
TimeOut: time.Second * 10,
Name: t.GetActionName(),
// Propagation, Propagation
// LockRetryInternal, int64
Expand Down
2 changes: 1 addition & 1 deletion pkg/rm/tcc/tcc_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ func TestTCCGetTransactionInfo(t1 *testing.T) {
TwoPhaseAction: twoPhaseAction1,
},
},
tm.TransactionInfo{Name: "TwoPhaseDemoService", TimeOut: 10000, Propagation: 0, LockRetryInternal: 0, LockRetryTimes: 0},
tm.TransactionInfo{Name: "TwoPhaseDemoService", TimeOut: time.Second * 10, Propagation: 0, LockRetryInternal: 0, LockRetryTimes: 0},
}

t1.Run(tests.name, func(t1 *testing.T) {
Expand Down
25 changes: 18 additions & 7 deletions pkg/tm/transaction_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ import (
"github.com/seata/seata-go/pkg/util/log"
)

const DefaultTimeOut = time.Second * 30

type TransactionInfo struct {
TimeOut int32
TimeOut time.Duration
Name string
Propagation Propagation
LockRetryInternal int64
Expand All @@ -48,12 +50,16 @@ func WithGlobalTx(ctx context.Context, ti *TransactionInfo, business CallbackWit
return errors.New("global transaction name is required.")
}

if ctx, re = begin(ctx, ti.Name); re != nil {
if ctx, re = begin(ctx, ti); re != nil {
return
}
defer func() {
// business maybe to throw panic, so need to recover it here.
re = commitOrRollback(ctx, recover() == nil && re == nil)
err := recover()
if err != nil {
log.Errorf("business callback panic:%v", err)
}
re = commitOrRollback(ctx, err == nil && re == nil)
log.Infof("global transaction result %v", re)
}()

Expand All @@ -63,12 +69,18 @@ func WithGlobalTx(ctx context.Context, ti *TransactionInfo, business CallbackWit
}

// begin a global transaction, it will obtain a xid from tc in tcp call.
func begin(ctx context.Context, name string) (rc context.Context, re error) {
func begin(ctx context.Context, ti *TransactionInfo) (rc context.Context, re error) {
if ti == nil {
return nil, errors.New("transaction info is nil")
}
if ti.TimeOut == 0 {
ti.TimeOut = DefaultTimeOut
}
if !IsSeataContext(ctx) {
ctx = InitSeataContext(ctx)
}

SetTxName(ctx, name)
SetTxName(ctx, ti.Name)
if GetTransactionRole(ctx) == nil {
SetTransactionRole(ctx, LAUNCHER)
}
Expand All @@ -93,8 +105,7 @@ func begin(ctx context.Context, name string) (rc context.Context, re error) {
SetTxStatus(ctx, message.GlobalStatusUnKnown)
}

// todo timeout should read from config
err := GetGlobalTransactionManager().Begin(ctx, tx, time.Second*30, name)
err := GetGlobalTransactionManager().Begin(ctx, tx, ti.TimeOut, ti.Name)
if err != nil {
re = fmt.Errorf("transactionTemplate: begin transaction failed, error %v", err)
}
Expand Down
4 changes: 3 additions & 1 deletion pkg/tm/transaction_executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,9 @@ func TestTransactionExecutorBegin(t *testing.T) {
assert.Equal(t, v.wantErrString, err.Error)
}
}(v)
begin(v.ctx, v.name)
begin(v.ctx, &TransactionInfo{
Name: v.name,
})
}()

// rest up stub
Expand Down
3 changes: 2 additions & 1 deletion sample/at/basic/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ func main() {
initService()
selectData()
tm.WithGlobalTx(context.Background(), &tm.TransactionInfo{
Name: "ATSampleLocalGlobalTx",
Name: "ATSampleLocalGlobalTx",
Code-Fight marked this conversation as resolved.
Show resolved Hide resolved
TimeOut: time.Second * 30,
}, updateData)
<-make(chan struct{})
}
Expand Down