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

Fix TestLockTTL #123

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion store/tikv/2pc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
4 changes: 4 additions & 0 deletions store/tikv/lock_resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down
15 changes: 10 additions & 5 deletions store/tikv/lock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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"))
Expand All @@ -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()
Expand All @@ -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) {
Expand Down