diff --git a/store/tikv/2pc.go b/store/tikv/2pc.go index 4cf91653d..93fa58fc7 100644 --- a/store/tikv/2pc.go +++ b/store/tikv/2pc.go @@ -237,7 +237,7 @@ func txnLockTTL(startTime time.Time, txnSize int) uint64 { // Increase lockTTL by the transaction's read time. // When resolving a lock, we compare current ts and startTS+lockTTL to decide whether to clean up. If a txn // takes a long time to read, increasing its TTL will help to prevent it from been aborted soon after prewrite. - elapsed := time.Since(startTime) / time.Millisecond + elapsed := time.Since(startTime) / elapsedUnit return lockTTL + uint64(elapsed) } diff --git a/store/tikv/lock_resolver.go b/store/tikv/lock_resolver.go index 28159630e..ff37424b4 100644 --- a/store/tikv/lock_resolver.go +++ b/store/tikv/lock_resolver.go @@ -25,6 +25,7 @@ import ( "github.com/pingcap/tidb/util/logutil" "go.uber.org/zap" "sync" + "time" ) // ResolvedCacheSize is max number of cached txn status. @@ -102,6 +103,9 @@ var maxLockTTL uint64 = 120000 // ttl = ttlFactor * sqrt(writeSizeInMiB) var ttlFactor = 6000 +// unit of elapsed time for increasing ttl +var elapsedUnit = time.Millisecond + // Lock represents a lock from tikv server. type Lock struct { Key []byte diff --git a/store/tikv/lock_test.go b/store/tikv/lock_test.go index 0b7c0506a..32182177b 100644 --- a/store/tikv/lock_test.go +++ b/store/tikv/lock_test.go @@ -260,7 +260,7 @@ func (s *testLockSuite) prewriteTxnWithTTL(c *C, txn *tikvTxn, ttl uint64) { committer, err := newTwoPhaseCommitterWithInit(txn, 0) c.Assert(err, IsNil) if ttl > 0 { - elapsed := time.Since(txn.startTime) / time.Millisecond + elapsed := time.Since(txn.startTime) / elapsedUnit committer.lockTTL = uint64(elapsed) + ttl } err = committer.prewriteKeys(NewBackoffer(context.Background(), PrewriteMaxBackoff), committer.keys) @@ -291,14 +291,17 @@ func (s *testLockSuite) ttlEquals(c *C, x, y uint64) { // NOTE: On ppc64le, all integers are by default unsigned integers, // hence we have to separately cast the value returned by "math.Abs()" function for ppc64le. if runtime.GOARCH == "ppc64le" { - c.Assert(int(-math.Abs(float64(x-y))), LessEqual, 2) + c.Assert(int(-math.Abs(float64(x)-float64(y))), LessEqual, 2) } else { - c.Assert(int(math.Abs(float64(x-y))), LessEqual, 2) + c.Assert(int(math.Abs(float64(x)-float64(y))), LessEqual, 2) } } func (s *testLockSuite) TestLockTTL(c *C) { + // NOTE: `time.Second` is only applied for scaling `elapsedUnit` at the right proportion and only for this test + elapsedUnit = time.Second + txn, err := s.store.Begin() c.Assert(err, IsNil) txn.Set(kv.Key("key"), []byte("value")) @@ -318,7 +321,7 @@ func (s *testLockSuite) TestLockTTL(c *C) { } s.prewriteTxn(c, txn.(*tikvTxn)) l = s.mustGetLock(c, []byte("key")) - s.ttlEquals(c, l.TTL, uint64(ttlFactor*2)+uint64(time.Since(start)/time.Millisecond)) + s.ttlEquals(c, l.TTL, uint64(ttlFactor*2)+uint64(time.Since(start)/elapsedUnit)) // Txn with long read time. start = time.Now() @@ -328,7 +331,9 @@ func (s *testLockSuite) TestLockTTL(c *C) { txn.Set(kv.Key("key"), []byte("value")) s.prewriteTxn(c, txn.(*tikvTxn)) l = s.mustGetLock(c, []byte("key")) - s.ttlEquals(c, l.TTL, defaultLockTTL+uint64(time.Since(start)/time.Millisecond)) + s.ttlEquals(c, l.TTL, defaultLockTTL+uint64(time.Since(start)/elapsedUnit)) + + elapsedUnit = time.Millisecond } func (s *testLockSuite) TestNewLockZeroTTL(c *C) {