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

store/tikv: fix lockTTL too large if local time is behind timestamp (#13865) #13868

Merged
merged 1 commit into from
Dec 3, 2019
Merged
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
store/tikv: fix lockTTL too large if local time is behind timestamp
  • Loading branch information
coocood committed Dec 3, 2019
commit a20ed12a27ebe37dc4e632acec0df56966119319
5 changes: 3 additions & 2 deletions store/tikv/2pc.go
Original file line number Diff line number Diff line change
@@ -708,6 +708,8 @@ func (tm *ttlManager) keepAlive(c *twoPhaseCommitter) {
}

newTTL := uptime + PessimisticLockTTL
logutil.Logger(context.Background()).Info("send TxnHeartBeat",
zap.Uint64("startTS", c.startTS), zap.Uint64("newTTL", newTTL))
startTime := time.Now()
_, err = sendTxnHeartBeat(bo, c.store, c.primary(), c.startTS, newTTL)
if err != nil {
@@ -736,8 +738,7 @@ func (action actionPessimisticLock) handleSingleBatch(c *twoPhaseCommitter, bo *
mutations[i] = mut
}

t0 := oracle.GetTimeFromTS(c.forUpdateTS)
elapsed := uint64(time.Since(t0) / time.Millisecond)
elapsed := uint64(time.Since(c.txn.startTime) / time.Millisecond)
req := &tikvrpc.Request{
Type: tikvrpc.CmdPessimisticLock,
PessimisticLock: &pb.PessimisticLockRequest{
15 changes: 15 additions & 0 deletions store/tikv/2pc_test.go
Original file line number Diff line number Diff line change
@@ -570,6 +570,21 @@ func (s *testCommitterSuite) TestPessimisticTTL(c *C) {
c.Assert(false, IsTrue, Commentf("update pessimistic ttl fail"))
}

// TestElapsedTTL tests that elapsed time is correct even if ts physical time is greater than local time.
func (s *testCommitterSuite) TestElapsedTTL(c *C) {
key := kv.Key("key")
txn := s.begin(c)
txn.startTS = oracle.ComposeTS(oracle.GetPhysical(time.Now().Add(time.Second*10)), 1)
txn.SetOption(kv.Pessimistic, true)
time.Sleep(time.Millisecond * 100)
forUpdateTS := oracle.ComposeTS(oracle.ExtractPhysical(txn.startTS)+100, 1)
err := txn.LockKeys(context.Background(), nil, forUpdateTS, kv.LockAlwaysWait, key)
c.Assert(err, IsNil)
lockInfo := s.getLockInfo(c, key)
c.Assert(lockInfo.LockTtl-PessimisticLockTTL, GreaterEqual, uint64(100))
c.Assert(lockInfo.LockTtl-PessimisticLockTTL, Less, uint64(150))
}

func (s *testCommitterSuite) getLockInfo(c *C, key []byte) *kvrpcpb.LockInfo {
txn := s.begin(c)
err := txn.Set(key, key)