From 116a0bcd23b67e257eb0db7200021c0a2df14782 Mon Sep 17 00:00:00 2001 From: MyonKeminta <9948422+MyonKeminta@users.noreply.github.com> Date: Tue, 4 Jul 2023 18:08:14 +0800 Subject: [PATCH] txn: Update client-go to fix the issue that GC BatchResolveLcok may miss primary pessimistic locks (#45143) close pingcap/tidb#45134 --- .../pessimistictest/pessimistic_test.go | 33 +++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/tests/realtikvtest/pessimistictest/pessimistic_test.go b/tests/realtikvtest/pessimistictest/pessimistic_test.go index cdef7b5eb7388..0db67fefc9fff 100644 --- a/tests/realtikvtest/pessimistictest/pessimistic_test.go +++ b/tests/realtikvtest/pessimistictest/pessimistic_test.go @@ -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" @@ -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 { @@ -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") @@ -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") @@ -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")) }