Skip to content

Commit

Permalink
txn: Update client-go to fix the issue that GC BatchResolveLcok may m…
Browse files Browse the repository at this point in the history
…iss primary pessimistic locks (pingcap#45143)

close pingcap#45134
  • Loading branch information
MyonKeminta committed Aug 8, 2023
1 parent 74abc22 commit 116a0bc
Showing 1 changed file with 31 additions and 2 deletions.
33 changes: 31 additions & 2 deletions tests/realtikvtest/pessimistictest/pessimistic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/pingcap/failpoint"
"github.com/pingcap/tidb/config"
"github.com/pingcap/tidb/domain"
"github.com/pingcap/tidb/errno"
"github.com/pingcap/tidb/expression"
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/parser"
Expand Down Expand Up @@ -3610,7 +3611,15 @@ func mustRecv[T interface{}](t *testing.T, ch <-chan T) T {
panic("unreachable")
}

func TestIssue43243(t *testing.T) {
func mustLocked(t *testing.T, store kv.Storage, stmt string) {
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("begin pessimistic")
tk.MustGetErrCode(stmt, errno.ErrLockAcquireFailAndNoWaitSet)
tk.MustExec("rollback")
}

func TestBatchResolveLocks(t *testing.T) {
store, domain := realtikvtest.CreateMockStoreAndDomainAndSetup(t)

if *realtikvtest.WithRealTiKV {
Expand All @@ -3634,8 +3643,10 @@ func TestIssue43243(t *testing.T) {
tk.MustExec("use test")
tk.MustExec("create table t1 (id int primary key, v int)")
tk.MustExec("create table t2 (id int primary key, v int)")
tk.MustExec("create table t3 (id int primary key, v int)")
tk.MustExec("insert into t1 values (1, 1), (2, 2)")
tk.MustExec("insert into t2 values (1, 1), (2, 2), (3, 3), (4, 4), (5, 5)")
tk.MustExec("insert into t3 values (1, 1)")
tk.MustExec("set @@tidb_enable_async_commit=0")
tk.MustExec("set @@tidb_enable_1pc=0")

Expand All @@ -3656,12 +3667,17 @@ func TestIssue43243(t *testing.T) {
require.NoError(t, failpoint.Enable("tikvclient/beforeAsyncPessimisticRollback", `return("skip")`))
require.NoError(t, failpoint.Enable("tikvclient/beforeCommitSecondaries", `return("skip")`))
require.NoError(t, failpoint.Enable("tikvclient/twoPCRequestBatchSizeLimit", `return`))
require.NoError(t, failpoint.Enable("tikvclient/onRollback", `return("skipRollbackPessimisticLock")`))
defer func() {
require.NoError(t, failpoint.Disable("tikvclient/beforeAsyncPessimisticRollback"))
require.NoError(t, failpoint.Disable("tikvclient/beforeCommitSecondaries"))
require.NoError(t, failpoint.Disable("tikvclient/twoPCRequestBatchSizeLimit"))
require.NoError(t, failpoint.Disable("tikvclient/onRollback"))
}()

// ----------------
// Simulate issue https://github.com/pingcap/tidb/issues/43243

tk.MustExec("begin pessimistic")
tk2.MustExec("begin pessimistic")
tk2.MustExec("update t2 set v = v + 1 where id = 2")
Expand All @@ -3687,12 +3703,25 @@ func TestIssue43243(t *testing.T) {
tk.MustExec("update t2 set v = v + 10 where id = 3")
tk.MustExec("commit")

// ----------------
// Simulate issue https://github.com/pingcap/tidb/issues/45134
tk.MustExec("begin pessimistic")
tk.MustQuery("select * from t3 where id = 1 for update").Check(testkit.Rows("1 1"))
tk.MustExec("rollback")
// tk leaves a pessimistic lock on row 6. Try to ensure it.
mustLocked(t, store, "select * from t3 where id = 1 for update nowait")

// Simulate a later GC that should resolve all stale lock produced in above steps.
currentTS, err := store.CurrentVersion(kv.GlobalTxnScope)
require.NoError(t, err)
_, err = gcworker.RunResolveLocks(context.Background(), store.(tikv.Storage), domain.GetPDClient(), currentTS.Ver, "gc-worker-test-issue43243", 1, false)
_, err = gcworker.RunResolveLocks(context.Background(), store.(tikv.Storage), domain.GetPDClient(), currentTS.Ver, "gc-worker-test-batch-resolve-locks", 1, false)
require.NoError(t, err)

// Check row 6 unlocked
tk3.MustExec("begin pessimistic")
tk3.MustQuery("select * from t3 where id = 1 for update nowait").Check(testkit.Rows("1 1"))
tk3.MustExec("rollback")

// Check data consistency
tk.MustQuery("select * from t2 order by id").Check(testkit.Rows("1 1", "2 3", "3 13", "4 14", "5 15"))
}
Expand Down

0 comments on commit 116a0bc

Please sign in to comment.