From efebf6ee42b0297fda57c6ff03bd093ebfb169a4 Mon Sep 17 00:00:00 2001 From: MyonKeminta Date: Tue, 4 Jul 2023 16:10:07 +0800 Subject: [PATCH 1/2] txn: Update client-go to fix the issue that GC BatchResolveLcok may miss primary pessimistic locks Signed-off-by: MyonKeminta --- go.mod | 2 +- go.sum | 4 +- .../pessimistictest/pessimistic_test.go | 38 +++++++++++++++---- 3 files changed, 34 insertions(+), 10 deletions(-) diff --git a/go.mod b/go.mod index b3ee0878dbcb5..9006190a352c9 100644 --- a/go.mod +++ b/go.mod @@ -97,7 +97,7 @@ require ( github.com/stretchr/testify v1.8.4 github.com/tdakkota/asciicheck v0.2.0 github.com/tiancaiamao/appdash v0.0.0-20181126055449-889f96f722a2 - github.com/tikv/client-go/v2 v2.0.8-0.20230621031026-864a5fc0c482 + github.com/tikv/client-go/v2 v2.0.8-0.20230704071705-c0cf773917d9 github.com/tikv/pd/client v0.0.0-20230613052906-7158cb319935 github.com/timakin/bodyclose v0.0.0-20230421092635-574207250966 github.com/twmb/murmur3 v1.1.6 diff --git a/go.sum b/go.sum index 14749d2f1456f..6c2907197a522 100644 --- a/go.sum +++ b/go.sum @@ -977,8 +977,8 @@ github.com/tiancaiamao/appdash v0.0.0-20181126055449-889f96f722a2 h1:mbAskLJ0oJf github.com/tiancaiamao/appdash v0.0.0-20181126055449-889f96f722a2/go.mod h1:2PfKggNGDuadAa0LElHrByyrz4JPZ9fFx6Gs7nx7ZZU= github.com/tiancaiamao/gp v0.0.0-20221230034425-4025bc8a4d4a h1:J/YdBZ46WKpXsxsW93SG+q0F8KI+yFrcIDT4c/RNoc4= github.com/tiancaiamao/gp v0.0.0-20221230034425-4025bc8a4d4a/go.mod h1:h4xBhSNtOeEosLJ4P7JyKXX7Cabg7AVkWCK5gV2vOrM= -github.com/tikv/client-go/v2 v2.0.8-0.20230621031026-864a5fc0c482 h1:KIqPtZ+U4RLuqL6wjobTnNn+111esFtu7+bY/50KKP4= -github.com/tikv/client-go/v2 v2.0.8-0.20230621031026-864a5fc0c482/go.mod h1:4KkKqjJgKlvvWMyNqdnAlYFfV4QjEj1fEb5Hb/FoT88= +github.com/tikv/client-go/v2 v2.0.8-0.20230704071705-c0cf773917d9 h1:GqsAoNiOFxbCJ8U8Lnts8BvdYd6HDWDsIm/oJY1sIMM= +github.com/tikv/client-go/v2 v2.0.8-0.20230704071705-c0cf773917d9/go.mod h1:4KkKqjJgKlvvWMyNqdnAlYFfV4QjEj1fEb5Hb/FoT88= github.com/tikv/pd/client v0.0.0-20230613052906-7158cb319935 h1:a5SATBxu/0Z6qNnz4KXDN91gDA06waaYcHM6dkb6lz4= github.com/tikv/pd/client v0.0.0-20230613052906-7158cb319935/go.mod h1:YmNkj9UT8IjwFov9k3oquH0UgIUHniUaQT3jXKgZYbM= github.com/timakin/bodyclose v0.0.0-20230421092635-574207250966 h1:quvGphlmUVU+nhpFa4gg4yJyTRJ13reZMDHrKwYw53M= diff --git a/tests/realtikvtest/pessimistictest/pessimistic_test.go b/tests/realtikvtest/pessimistictest/pessimistic_test.go index 364711fe93643..19f6162cf17cc 100644 --- a/tests/realtikvtest/pessimistictest/pessimistic_test.go +++ b/tests/realtikvtest/pessimistictest/pessimistic_test.go @@ -3052,6 +3052,14 @@ func mustRecv[T interface{}](t *testing.T, ch <-chan T) T { panic("unreachable") } +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 TestFairLockingBasic(t *testing.T) { store := realtikvtest.CreateMockStoreAndSetup(t) tk := testkit.NewTestKit(t, store) @@ -3212,11 +3220,7 @@ func TestFairLockingRetry(t *testing.T) { tk2.MustExec("use test") mustLocked := func(stmt string) { - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test") - tk.MustExec("begin pessimistic") - tk.MustGetErrCode(stmt, errno.ErrLockAcquireFailAndNoWaitSet) - tk.MustExec("rollback") + mustLocked(t, store, stmt) } tk.MustExec("set @@tidb_pessimistic_txn_fair_locking = 1") @@ -3388,7 +3392,7 @@ func TestPointLockNonExistentKeyWithFairLockingUnderRC(t *testing.T) { tk.MustExec("commit") } -func TestIssue43243(t *testing.T) { +func TestIssueBatchResolveLocks(t *testing.T) { store, domain := realtikvtest.CreateMockStoreAndDomainAndSetup(t) if *realtikvtest.WithRealTiKV { @@ -3412,8 +3416,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") @@ -3434,12 +3440,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") @@ -3465,12 +3476,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")) } From 1910fa302b67f90e33a1d758d05eef572301a714 Mon Sep 17 00:00:00 2001 From: MyonKeminta Date: Tue, 4 Jul 2023 16:21:05 +0800 Subject: [PATCH 2/2] Fix bazel Signed-off-by: MyonKeminta --- DEPS.bzl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/DEPS.bzl b/DEPS.bzl index 1127f35470b1f..1a802a790bdd7 100644 --- a/DEPS.bzl +++ b/DEPS.bzl @@ -3871,8 +3871,8 @@ def go_deps(): name = "com_github_tikv_client_go_v2", build_file_proto_mode = "disable_global", importpath = "github.com/tikv/client-go/v2", - sum = "h1:KIqPtZ+U4RLuqL6wjobTnNn+111esFtu7+bY/50KKP4=", - version = "v2.0.8-0.20230621031026-864a5fc0c482", + sum = "h1:GqsAoNiOFxbCJ8U8Lnts8BvdYd6HDWDsIm/oJY1sIMM=", + version = "v2.0.8-0.20230704071705-c0cf773917d9", ) go_repository(