Browse Source

optimize: nested loop retries (#261)

tags/1.0.2-RC1
juzimao GitHub 3 years ago
parent
commit
cffa619f6d
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 12 additions and 24 deletions
  1. +12
    -24
      pkg/tm/transaction_executor.go

+ 12
- 24
pkg/tm/transaction_executor.go View File

@@ -74,42 +74,30 @@ func Begin(ctx context.Context, name string) context.Context {
} }


// CommitOrRollback commit global transaction // CommitOrRollback commit global transaction
func CommitOrRollback(ctx context.Context, isSuccess bool) error {
func CommitOrRollback(ctx context.Context, isSuccess bool) (re error) {
role := *GetTransactionRole(ctx) role := *GetTransactionRole(ctx)
if role == PARTICIPANT { if role == PARTICIPANT {
// Participant has no responsibility of rollback // Participant has no responsibility of rollback
log.Debugf("Ignore Rollback(): just involved in global transaction [%s]", GetXID(ctx)) log.Debugf("Ignore Rollback(): just involved in global transaction [%s]", GetXID(ctx))
return nil
return
} }

tx := &GlobalTransaction{ tx := &GlobalTransaction{
Xid: GetXID(ctx), Xid: GetXID(ctx),
Status: *GetTxStatus(ctx), Status: *GetTxStatus(ctx),
Role: role, Role: role,
} }
var (
err error
// todo retry and retryInterval should read from config
retry = 10
retryInterval = 200 * time.Millisecond
)
for ; retry > 0; retry-- {
if isSuccess {
err = GetGlobalTransactionManager().Commit(ctx, tx)
if err != nil {
log.Infof("transactionTemplate: commit transaction failed, error %v", err)
}
} else {
err = GetGlobalTransactionManager().Rollback(ctx, tx)
if err != nil {
log.Infof("transactionTemplate: Rollback transaction failed, error %v", err)
}

if isSuccess {
if re = GetGlobalTransactionManager().Commit(ctx, tx); re != nil {
log.Errorf("transactionTemplate: commit transaction failed, error %v", re)
} }
if err == nil {
break
} else {
time.Sleep(retryInterval)
} else {
if re = GetGlobalTransactionManager().Rollback(ctx, tx); re != nil {
log.Errorf("transactionTemplate: Rollback transaction failed, error %v", re)
} }
} }

// todo unbind xid // todo unbind xid
return err
return
} }

Loading…
Cancel
Save