Skip to content

Commit

Permalink
txn: get txn status does not block if txn not found (pingcap#15300)
Browse files Browse the repository at this point in the history
  • Loading branch information
cfzjywxk authored Mar 12, 2020
1 parent d7a8eab commit 35468b1
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
37 changes: 37 additions & 0 deletions session/pessimistic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1096,3 +1096,40 @@ func (s *testPessimisticSuite) TestBatchPointGetLockIndex(c *C) {
tk.MustExec("rollback")
tk2.MustExec("rollback")
}

func (s *testPessimisticSuite) TestRollbackWakeupBlockedTxn(c *C) {
tk := testkit.NewTestKitWithInit(c, s.store)
tk2 := testkit.NewTestKitWithInit(c, s.store)
tk2.MustExec("use test")
tk.MustExec("use test")
tk.MustExec("drop table if exists t1")
tk.MustExec("create table t1 (c1 int primary key, c2 int, c3 int, unique key uk(c2))")
tk.MustExec("insert into t1 values (1, 1, 1)")
tk.MustExec("insert into t1 values (5, 5, 5)")
tk.MustExec("insert into t1 values (10, 10, 10)")

c.Assert(failpoint.Enable("github.com/pingcap/tidb/store/tikv/txnExpireRetTTL", "return"), IsNil)
c.Assert(failpoint.Enable("github.com/pingcap/tidb/store/tikv/getTxnStatusDelay", "return"), IsNil)
tk.MustExec("begin pessimistic")
tk2.MustExec("set innodb_lock_wait_timeout = 1")
tk2.MustExec("begin pessimistic")
tk.MustExec("update t1 set c3 = c3 + 1")
errCh := make(chan error)
go func() {
var err error
defer func() {
errCh <- err
}()
_, err = tk2.Exec("update t1 set c3 = 100 where c1 = 1")
if err != nil {
return
}
}()
time.Sleep(time.Millisecond * 30)
tk.MustExec("rollback")
err := <-errCh
c.Assert(err, IsNil)
tk2.MustExec("rollback")
c.Assert(failpoint.Disable("github.com/pingcap/tidb/store/tikv/txnExpireRetTTL"), IsNil)
c.Assert(failpoint.Disable("github.com/pingcap/tidb/store/tikv/getTxnStatusDelay"), IsNil)
}
11 changes: 11 additions & 0 deletions store/tikv/lock_resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,9 @@ func (lr *LockResolver) getTxnStatusFromLock(bo *Backoffer, l *Lock, callerStart
}

rollbackIfNotExist := false
failpoint.Inject("getTxnStatusDelay", func() {
time.Sleep(100 * time.Millisecond)
})
for {
status, err = lr.getTxnStatus(bo, l.TxnID, l.Primary, callerStartTS, currentTS, rollbackIfNotExist)
if err == nil {
Expand Down Expand Up @@ -460,9 +463,17 @@ func (lr *LockResolver) getTxnStatusFromLock(bo *Backoffer, l *Lock, callerStart
zap.Uint64("CallerStartTs", callerStartTS),
zap.Stringer("lock str", l))
if l.LockType == kvrpcpb.Op_PessimisticLock {
failpoint.Inject("txnExpireRetTTL", func() {
failpoint.Return(TxnStatus{l.TTL, 0, kvrpcpb.Action_NoAction},
errors.New("error txn not found and lock expired"))
})
return TxnStatus{}, nil
}
rollbackIfNotExist = true
} else {
if l.LockType == kvrpcpb.Op_PessimisticLock {
return TxnStatus{ttl: l.TTL}, nil
}
}
}
}
Expand Down

0 comments on commit 35468b1

Please sign in to comment.