Skip to content

Commit

Permalink
txnkv: Fix a bug that OnDeadlock callback is called before setting `I…
Browse files Browse the repository at this point in the history
…sRetryable` field

Signed-off-by: MyonKeminta <MyonKeminta@users.noreply.github.com>
  • Loading branch information
MyonKeminta committed Aug 19, 2021
1 parent b440ea2 commit 0a8c38d
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions txnkv/transaction/txn.go
Original file line number Diff line number Diff line change
Expand Up @@ -623,16 +623,22 @@ func (txn *KVTxn) LockKeys(ctx context.Context, lockCtx *tikv.LockCtx, keysInput
keyMayBeLocked := !(tikverr.IsErrWriteConflict(err) || tikverr.IsErrKeyExist(err))
// If there is only 1 key and lock fails, no need to do pessimistic rollback.
if len(keys) > 1 || keyMayBeLocked {
dl, ok := errors.Cause(err).(*tikverr.ErrDeadlock)
if ok && lockCtx.OnDeadlock != nil {
// Call OnDeadlock before pessimistic rollback.
lockCtx.OnDeadlock(dl)
dl, isDeadlock := errors.Cause(err).(*tikverr.ErrDeadlock)
if isDeadlock {
if hashInKeys(dl.DeadlockKeyHash, keys) {
dl.IsRetryable = true
}
if lockCtx.OnDeadlock != nil {
// Call OnDeadlock before pessimistic rollback.
lockCtx.OnDeadlock(dl)
}
}

wg := txn.asyncPessimisticRollback(ctx, keys)
if ok {

if isDeadlock {
logutil.Logger(ctx).Debug("deadlock error received", zap.Uint64("startTS", txn.startTS), zap.Stringer("deadlockInfo", dl))
if hashInKeys(dl.DeadlockKeyHash, keys) {
dl.IsRetryable = true
if dl.IsRetryable {
// 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.
Expand Down

0 comments on commit 0a8c38d

Please sign in to comment.