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

tikv: eliminate unnecessary pessimistic rollback #12561

Merged
merged 7 commits into from
Oct 15, 2019
17 changes: 10 additions & 7 deletions store/tikv/txn.go
Original file line number Diff line number Diff line change
Expand Up @@ -428,13 +428,16 @@ func (txn *tikvTxn) LockKeys(ctx context.Context, forUpdateTS uint64, keysInput
for _, key := range keys {
txn.us.DeleteKeyExistErrInfo(key)
}
wg := txn.asyncPessimisticRollback(ctx, keys)
if dl, ok := errors.Cause(err).(*ErrDeadlock); ok && hashInKeys(dl.DeadlockKeyHash, keys) {
dl.IsRetryable = true
// Wait for the pessimistic rollback to finish before we retry the statement.
wg.Wait()
// Sleep a little, wait for the other transaction that blocked by this transaction to acquire the lock.
time.Sleep(time.Millisecond * 5)
// If there is only 1 key and lock failed, no need to do pessimistic rollback.
if len(keys) > 1 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to make sure the err indicates the lock must not be locked to skip pessimistic rollback.

wg := txn.asyncPessimisticRollback(ctx, keys)
if dl, ok := errors.Cause(err).(*ErrDeadlock); ok && hashInKeys(dl.DeadlockKeyHash, keys) {
dl.IsRetryable = true
// Wait for the pessimistic rollback to finish before we retry the statement.
wg.Wait()
// Sleep a little, wait for the other transaction that blocked by this transaction to acquire the lock.
time.Sleep(time.Millisecond * 5)
}
}
if assignedPrimaryKey {
// unset the primary key if we assigned primary key when failed to lock it.
Expand Down