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

txn: Update client-go to fix the issue that GC BatchResolveLcok may miss primary pessimistic locks #45143

Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
38 changes: 31 additions & 7 deletions tests/realtikvtest/pessimistictest/pessimistic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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 {
Expand All @@ -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")

Expand All @@ -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")
Expand All @@ -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"))
}
Expand Down