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) #50005

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
28 changes: 19 additions & 9 deletions session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -1161,22 +1161,32 @@
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)

Check warning on line 1173 in session/session.go

View check run for this annotation

Codecov / codecov/patch

session/session.go#L1164-L1173

Added lines #L1164 - L1173 were not covered by tests
}
return false, nil

Check warning on line 1175 in session/session.go

View check run for this annotation

Codecov / codecov/patch

session/session.go#L1175

Added line #L1175 was not covered by tests
}

func (s *session) checkTxnAborted(stmt sqlexec.Statement) error {
var err error
if atomic.LoadUint32(&s.GetSessionVars().TxnCtx.LockExpire) > 0 {
err = kv.ErrLockExpire
} else {
if atomic.LoadUint32(&s.GetSessionVars().TxnCtx.LockExpire) == 0 {
return nil
}
// 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 {

Check warning on line 1184 in session/session.go

View check run for this annotation

Codecov / codecov/patch

session/session.go#L1184

Added line #L1184 was not covered by tests
return nil
} else if err != nil {
return err

Check warning on line 1187 in session/session.go

View check run for this annotation

Codecov / codecov/patch

session/session.go#L1186-L1187

Added lines #L1186 - L1187 were not covered by tests
}
return err
return kv.ErrLockExpire

Check warning on line 1189 in session/session.go

View check run for this annotation

Codecov / codecov/patch

session/session.go#L1189

Added line #L1189 was not covered by tests
}

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/util/codec"
"github.com/pingcap/tidb/util/deadlockhistory"
"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)
})
}
}