Skip to content

Commit

Permalink
*: fix ManagedLockTTL race in test (pingcap#14407)
Browse files Browse the repository at this point in the history
  • Loading branch information
jackysp authored Jan 10, 2020
1 parent 1b34cc2 commit 2c256ba
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 9 deletions.
2 changes: 1 addition & 1 deletion session/pessimistic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ type testPessimisticSuite struct {
func (s *testPessimisticSuite) SetUpSuite(c *C) {
s.testSessionSuiteBase.SetUpSuite(c)
// Set it to 300ms for testing lock resolve.
tikv.ManagedLockTTL = 300
atomic.StoreUint64(&tikv.ManagedLockTTL, 300)
tikv.PrewriteMaxBackoff = 500
}

Expand Down
6 changes: 3 additions & 3 deletions store/tikv/2pc.go
Original file line number Diff line number Diff line change
Expand Up @@ -613,7 +613,7 @@ func (tm *ttlManager) close() {

func (tm *ttlManager) keepAlive(c *twoPhaseCommitter) {
// Ticker is set to 1/2 of the ManagedLockTTL.
ticker := time.NewTicker(time.Duration(ManagedLockTTL) * time.Millisecond / 2)
ticker := time.NewTicker(time.Duration(atomic.LoadUint64(&ManagedLockTTL)) * time.Millisecond / 2)
defer ticker.Stop()
for {
select {
Expand Down Expand Up @@ -647,7 +647,7 @@ func (tm *ttlManager) keepAlive(c *twoPhaseCommitter) {
return
}

newTTL := uptime + ManagedLockTTL
newTTL := uptime + atomic.LoadUint64(&ManagedLockTTL)
logutil.BgLogger().Info("send TxnHeartBeat",
zap.Uint64("startTS", c.startTS), zap.Uint64("newTTL", newTTL))
startTime := time.Now()
Expand Down Expand Up @@ -683,7 +683,7 @@ func (action actionPessimisticLock) handleSingleBatch(c *twoPhaseCommitter, bo *
PrimaryLock: c.primary(),
StartVersion: c.startTS,
ForUpdateTs: c.forUpdateTS,
LockTtl: elapsed + ManagedLockTTL,
LockTtl: elapsed + atomic.LoadUint64(&ManagedLockTTL),
IsFirstLock: c.isFirstLock,
WaitTimeout: action.LockWaitTime,
}, pb.Context{Priority: c.priority, SyncLog: c.syncLog})
Expand Down
11 changes: 6 additions & 5 deletions store/tikv/2pc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"math"
"math/rand"
"strings"
"sync/atomic"
"time"

. "github.com/pingcap/check"
Expand All @@ -39,7 +40,7 @@ type testCommitterSuite struct {
var _ = SerialSuites(&testCommitterSuite{})

func (s *testCommitterSuite) SetUpSuite(c *C) {
ManagedLockTTL = 3000 // 3s
atomic.StoreUint64(&ManagedLockTTL, 3000) // 3s
s.OneByOneSuite.SetUpSuite(c)
}

Expand Down Expand Up @@ -616,7 +617,7 @@ func (s *testCommitterSuite) TestPessimisticTTL(c *C) {
expire := oracle.ExtractPhysical(txn.startTS) + int64(lockInfoNew.LockTtl)
now := oracle.ExtractPhysical(currentTS)
c.Assert(expire > now, IsTrue)
c.Assert(uint64(expire-now) <= ManagedLockTTL, IsTrue)
c.Assert(uint64(expire-now) <= atomic.LoadUint64(&ManagedLockTTL), IsTrue)
return
}
time.Sleep(100 * time.Millisecond)
Expand All @@ -638,8 +639,8 @@ func (s *testCommitterSuite) TestElapsedTTL(c *C) {
err := txn.LockKeys(context.Background(), lockCtx, key)
c.Assert(err, IsNil)
lockInfo := s.getLockInfo(c, key)
c.Assert(lockInfo.LockTtl-ManagedLockTTL, GreaterEqual, uint64(100))
c.Assert(lockInfo.LockTtl-ManagedLockTTL, Less, uint64(150))
c.Assert(lockInfo.LockTtl-atomic.LoadUint64(&ManagedLockTTL), GreaterEqual, uint64(100))
c.Assert(lockInfo.LockTtl-atomic.LoadUint64(&ManagedLockTTL), Less, uint64(150))
}

// TestAcquireFalseTimeoutLock tests acquiring a key which is a secondary key of another transaction.
Expand All @@ -664,7 +665,7 @@ func (s *testCommitterSuite) TestAcquireFalseTimeoutLock(c *C) {
// Heartbeats will increase the TTL of the primary key

// wait until secondary key exceeds its own TTL
time.Sleep(time.Duration(ManagedLockTTL) * time.Millisecond)
time.Sleep(time.Duration(atomic.LoadUint64(&ManagedLockTTL)) * time.Millisecond)
txn2 := s.begin(c)
txn2.SetOption(kv.Pessimistic, true)

Expand Down

0 comments on commit 2c256ba

Please sign in to comment.