Skip to content

Commit

Permalink
Revert "executor: also collect unchanged unique keys for lock (pingca…
Browse files Browse the repository at this point in the history
…p#36498)"

This reverts commit c80026e.
  • Loading branch information
MyonKeminta committed Aug 10, 2022
1 parent 4979123 commit da6c7d4
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 59 deletions.
2 changes: 1 addition & 1 deletion executor/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -780,7 +780,7 @@ func (a *ExecStmt) handlePessimisticDML(ctx context.Context, e Executor) error {
tryCancelAggressiveLocking(ctx, txn)
return err1
}
keys = txnCtx.CollectUnchangedLockKeys(keys)
keys = txnCtx.CollectUnchangedRowKeys(keys)
if len(keys) == 0 {
tryDoneAggressiveLocking(ctx, txn)
return nil
Expand Down
16 changes: 1 addition & 15 deletions executor/write.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,21 +158,7 @@ func updateRecord(ctx context.Context, sctx sessionctx.Context, h kv.Handle, old
unchangedRowKey := tablecodec.EncodeRowKeyWithHandle(physicalID, h)
txnCtx := sctx.GetSessionVars().TxnCtx
if txnCtx.IsPessimistic {
txnCtx.AddUnchangedLockKey(unchangedRowKey)
for _, idx := range t.Indices() {
if !idx.Meta().Unique {
continue
}
ukVals, err := idx.FetchValues(oldData, nil)
if err != nil {
return false, err
}
unchangedUniqueKey, _, err := idx.GenIndexKey(sc, ukVals, h, nil)
if err != nil {
return false, err
}
txnCtx.AddUnchangedLockKey(unchangedUniqueKey)
}
txnCtx.AddUnchangedRowKey(unchangedRowKey)
}
return false, nil
}
Expand Down
23 changes: 11 additions & 12 deletions sessionctx/variable/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,8 @@ type TxnCtxNoNeedToRestore struct {
currentShard int64
shardRand *rand.Rand

// unchangedLockKeys is used to store the unchanged keys that needs to lock for pessimistic transaction, including
// row keys and unique keys.
unchangedLockKeys map[string]struct{}
// unchangedRowKeys is used to store the unchanged rows that needs to lock for pessimistic transaction.
unchangedRowKeys map[string]struct{}

PessimisticCacheHit int

Expand Down Expand Up @@ -239,20 +238,20 @@ func (tc *TransactionContext) updateShard() {
tc.currentShard = int64(murmur3.Sum32(buf[:]))
}

// AddUnchangedLockKey adds an unchanged key in update statement for pessimistic lock.
func (tc *TransactionContext) AddUnchangedLockKey(key []byte) {
if tc.unchangedLockKeys == nil {
tc.unchangedLockKeys = map[string]struct{}{}
// AddUnchangedRowKey adds an unchanged row key in update statement for pessimistic lock.
func (tc *TransactionContext) AddUnchangedRowKey(key []byte) {
if tc.unchangedRowKeys == nil {
tc.unchangedRowKeys = map[string]struct{}{}
}
tc.unchangedLockKeys[string(key)] = struct{}{}
tc.unchangedRowKeys[string(key)] = struct{}{}
}

// CollectUnchangedLockKeys collects unchanged keys for pessimistic lock.
func (tc *TransactionContext) CollectUnchangedLockKeys(buf []kv.Key) []kv.Key {
for key := range tc.unchangedLockKeys {
// CollectUnchangedRowKeys collects unchanged row keys for pessimistic lock.
func (tc *TransactionContext) CollectUnchangedRowKeys(buf []kv.Key) []kv.Key {
for key := range tc.unchangedRowKeys {
buf = append(buf, kv.Key(key))
}
tc.unchangedLockKeys = nil
tc.unchangedRowKeys = nil
return buf
}

Expand Down
31 changes: 0 additions & 31 deletions tests/realtikvtest/pessimistictest/pessimistic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -495,37 +495,6 @@ func TestLockUnchangedRowKey(t *testing.T) {
tk2.MustExec("rollback")
}

func TestLockUnchangedUniqueKey(t *testing.T) {
store := realtikvtest.CreateMockStoreAndSetup(t)

tk := testkit.NewTestKit(t, store)
tk2 := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk2.MustExec("use test")

// ref https://github.com/pingcap/tidb/issues/36438
tk.MustExec("drop table if exists t")
tk.MustExec("create table t (i varchar(10), unique key(i))")
tk.MustExec("insert into t values ('a')")
tk.MustExec("begin pessimistic")
tk.MustExec("update t set i = 'a'")

errCh := make(chan error, 1)
go func() {
_, err := tk2.Exec("insert into t values ('a')")
errCh <- err
}()

select {
case <-errCh:
require.Fail(t, "insert is not blocked by update")
case <-time.After(500 * time.Millisecond):
tk.MustExec("rollback")
}

require.Error(t, <-errCh)
}

func TestOptimisticConflicts(t *testing.T) {
store := realtikvtest.CreateMockStoreAndSetup(t)

Expand Down

0 comments on commit da6c7d4

Please sign in to comment.