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

executor: do not acqurie pessimistic lock for non-unique index keys (#36229) #36562

Merged
merged 2 commits into from
Jul 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
25 changes: 14 additions & 11 deletions session/tidb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ func TestParseErrorWarn(t *testing.T) {

func TestKeysNeedLock(t *testing.T) {
rowKey := tablecodec.EncodeRowKeyWithHandle(1, kv.IntHandle(1))
indexKey := tablecodec.EncodeIndexSeekKey(1, 1, []byte{1})
uniqueIndexKey := tablecodec.EncodeIndexSeekKey(1, 1, []byte{1})
nonUniqueIndexKey := tablecodec.EncodeIndexSeekKey(1, 2, []byte{1})
uniqueValue := make([]byte, 8)
uniqueUntouched := append(uniqueValue, '1')
nonUniqueVal := []byte{'0'}
Expand All @@ -83,18 +84,20 @@ func TestKeysNeedLock(t *testing.T) {
}{
{rowKey, rowVal, true},
{rowKey, deleteVal, true},
{indexKey, nonUniqueVal, false},
{indexKey, nonUniqueUntouched, false},
{indexKey, uniqueValue, true},
{indexKey, uniqueUntouched, false},
{indexKey, deleteVal, false},
{nonUniqueIndexKey, nonUniqueVal, false},
{nonUniqueIndexKey, nonUniqueUntouched, false},
{uniqueIndexKey, uniqueValue, true},
{uniqueIndexKey, uniqueUntouched, false},
{uniqueIndexKey, deleteVal, false},
}

for _, test := range tests {
require.Equal(t, test.need, keyNeedToLock(test.key, test.val, 0))
}
need := keyNeedToLock(test.key, test.val, 0)
require.Equal(t, test.need, need)

flag := kv.KeyFlags(1)
require.True(t, flag.HasPresumeKeyNotExists())
require.True(t, keyNeedToLock(indexKey, deleteVal, flag))
flag := kv.KeyFlags(1)
need = keyNeedToLock(test.key, test.val, flag)
require.True(t, flag.HasPresumeKeyNotExists())
require.True(t, need)
}
}
9 changes: 6 additions & 3 deletions session/txn.go
Original file line number Diff line number Diff line change
Expand Up @@ -506,9 +506,12 @@ func keyNeedToLock(k, v []byte, flags kv.KeyFlags) bool {
if tablecodec.IsUntouchedIndexKValue(k, v) {
return false
}
isNonUniqueIndex := tablecodec.IsIndexKey(k) && len(v) == 1
// Put row key and unique index need to lock.
return !isNonUniqueIndex

if !tablecodec.IsIndexKey(k) {
return true
}

return tablecodec.IndexKVIsUnique(v)
}

func getBinlogMutation(ctx sessionctx.Context, tableID int64) *binlog.TableMutation {
Expand Down
13 changes: 13 additions & 0 deletions tablecodec/tablecodec.go
Original file line number Diff line number Diff line change
Expand Up @@ -1580,3 +1580,16 @@ func decodeIndexKvGeneral(key, value []byte, colsLen int, hdStatus HandleStatus,
}
return resultValues, nil
}

// IndexKVIsUnique uses to judge if an index is unique, it can handle the KV committed by txn already, it doesn't consider the untouched flag.
func IndexKVIsUnique(value []byte) bool {
if len(value) <= MaxOldEncodeValueLen {
return len(value) == 8
}
if getIndexVersion(value) == 1 {
segs := SplitIndexValueForClusteredIndexVersion1(value)
return segs.CommonHandle != nil
}
segs := SplitIndexValue(value)
return segs.IntHandle != nil || segs.CommonHandle != nil
}