Skip to content

Commit

Permalink
cherry pick pingcap#36229 to release-5.1
Browse files Browse the repository at this point in the history
Signed-off-by: ti-srebot <ti-srebot@pingcap.com>
  • Loading branch information
ekexium authored and ti-srebot committed Jul 26, 2022
1 parent 1c57ffd commit 05df470
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 9 deletions.
26 changes: 20 additions & 6 deletions session/tidb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,8 @@ func match(c *C, row []types.Datum, expected ...interface{}) {

func (s *testMainSuite) TestKeysNeedLock(c *C) {
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 @@ -201,12 +202,13 @@ func (s *testMainSuite) TestKeysNeedLock(c *C) {
}{
{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},
}
<<<<<<< HEAD
for _, tt := range tests {
c.Assert(keyNeedToLock(tt.key, tt.val, 0), Equals, tt.need)
}
Expand Down Expand Up @@ -238,4 +240,16 @@ func (s *testMainSuite) TestIndexUsageSyncLease(c *C) {
do.Close()
err = store.Close()
c.Assert(err, IsNil)
=======

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

flag := kv.KeyFlags(1)
need = keyNeedToLock(test.key, test.val, flag)
require.True(t, flag.HasPresumeKeyNotExists())
require.True(t, need)
}
>>>>>>> 87c5b5068... executor: do not acqurie pessimistic lock for non-unique index keys (#36229)
}
9 changes: 6 additions & 3 deletions session/txn.go
Original file line number Diff line number Diff line change
Expand Up @@ -436,9 +436,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)
}

// Info dump the TxnState to Datum for displaying in `TIDB_TRX`
Expand Down
13 changes: 13 additions & 0 deletions tablecodec/tablecodec.go
Original file line number Diff line number Diff line change
Expand Up @@ -1525,3 +1525,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
}

0 comments on commit 05df470

Please sign in to comment.