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

session: allow end aborted txn via binary protocal (#49384) #50006

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
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
25 changes: 18 additions & 7 deletions pkg/session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -1138,21 +1138,32 @@ func (*session) isTxnRetryableError(err error) bool {
return kv.IsTxnRetryableError(err) || domain.ErrInfoSchemaChanged.Equal(err)
}

func isEndTxnStmt(stmt ast.StmtNode, vars *variable.SessionVars) (bool, error) {
switch n := stmt.(type) {
case *ast.RollbackStmt, *ast.CommitStmt:
return true, nil
case *ast.ExecuteStmt:
ps, err := plannercore.GetPreparedStmt(n, vars)
if err != nil {
return false, err
}
return isEndTxnStmt(ps.PreparedAst.Stmt, vars)
}
return false, nil
}

func (s *session) checkTxnAborted(stmt sqlexec.Statement) error {
var err error
if atomic.LoadUint32(&s.GetSessionVars().TxnCtx.LockExpire) == 0 {
return nil
}
err = kv.ErrLockExpire
// If the transaction is aborted, the following statements do not need to execute, except `commit` and `rollback`,
// because they are used to finish the aborted transaction.
if _, ok := stmt.(*executor.ExecStmt).StmtNode.(*ast.CommitStmt); ok {
return nil
}
if _, ok := stmt.(*executor.ExecStmt).StmtNode.(*ast.RollbackStmt); ok {
if ok, err := isEndTxnStmt(stmt.(*executor.ExecStmt).StmtNode, s.sessionVars); err == nil && ok {
return nil
} else if err != nil {
return err
}
return err
return kv.ErrLockExpire
}

func (s *session) retry(ctx context.Context, maxCnt uint) (err error) {
Expand Down
1 change: 1 addition & 0 deletions tests/realtikvtest/pessimistictest/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ go_test(
"@com_github_pingcap_errors//:errors",
"@com_github_pingcap_failpoint//:failpoint",
"@com_github_stretchr_testify//require",
"@com_github_tikv_client_go_v2//config",
"@com_github_tikv_client_go_v2//oracle",
"@com_github_tikv_client_go_v2//testutils",
"@com_github_tikv_client_go_v2//tikv",
Expand Down
35 changes: 35 additions & 0 deletions tests/realtikvtest/pessimistictest/pessimistic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import (
"github.com/pingcap/tidb/pkg/util/deadlockhistory"
"github.com/pingcap/tidb/tests/realtikvtest"
"github.com/stretchr/testify/require"
tikvcfg "github.com/tikv/client-go/v2/config"
"github.com/tikv/client-go/v2/oracle"
"github.com/tikv/client-go/v2/testutils"
"github.com/tikv/client-go/v2/tikv"
Expand Down Expand Up @@ -3566,3 +3567,37 @@ func TestIssue42937(t *testing.T) {
"5 11",
))
}

func TestEndTxnOnLockExpire(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("create table t(a int, b int)")
tk.MustExec("prepare ps_commit from 'commit'")
tk.MustExec("prepare ps_rollback from 'rollback'")

defer setLockTTL(300).restore()
defer tikvcfg.UpdateGlobal(func(conf *tikvcfg.Config) {
conf.MaxTxnTTL = 500
})()

for _, tt := range []struct {
name string
endTxnSQL string
}{
{"CommitTxt", "commit"},
{"CommitBin", "execute ps_commit"},
{"RollbackTxt", "rollback"},
{"RollbackBin", "execute ps_rollback"},
} {
t.Run(tt.name, func(t *testing.T) {
tk.Exec("delete from t")
tk.Exec("insert into t values (1, 1)")
tk.Exec("begin pessimistic")
tk.Exec("update t set b = 10 where a = 1")
time.Sleep(time.Second)
tk.MustContainErrMsg("select * from t", "TTL manager has timed out")
tk.MustExec(tt.endTxnSQL)
})
}
}