-
Notifications
You must be signed in to change notification settings - Fork 5.9k
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
tikvclient: reduce wait backoff time when lock has be expired #10006
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,6 +37,7 @@ var ( | |
tikvLockResolverCountWithBatchResolve = metrics.TiKVLockResolverCounter.WithLabelValues("batch_resolve") | ||
tikvLockResolverCountWithExpired = metrics.TiKVLockResolverCounter.WithLabelValues("expired") | ||
tikvLockResolverCountWithNotExpired = metrics.TiKVLockResolverCounter.WithLabelValues("not_expired") | ||
tikvLockResolverCountWithWaitExpired = metrics.TiKVLockResolverCounter.WithLabelValues("wait_expired") | ||
tikvLockResolverCountWithResolve = metrics.TiKVLockResolverCounter.WithLabelValues("resolve") | ||
tikvLockResolverCountWithQueryTxnStatus = metrics.TiKVLockResolverCounter.WithLabelValues("query_txn_status") | ||
tikvLockResolverCountWithQueryTxnStatusCommitted = metrics.TiKVLockResolverCounter.WithLabelValues("query_txn_status_committed") | ||
|
@@ -265,47 +266,59 @@ func (lr *LockResolver) BatchResolveLocks(bo *Backoffer, locks []*Lock, loc Regi | |
// commit status. | ||
// 3) Send `ResolveLock` cmd to the lock's region to resolve all locks belong to | ||
// the same transaction. | ||
func (lr *LockResolver) ResolveLocks(bo *Backoffer, locks []*Lock) (ok bool, err error) { | ||
func (lr *LockResolver) ResolveLocks(bo *Backoffer, locks []*Lock) (msBeforeTxnExpired int64, err error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems yes. |
||
if len(locks) == 0 { | ||
return true, nil | ||
return | ||
} | ||
|
||
tikvLockResolverCountWithResolve.Inc() | ||
|
||
var expiredLocks []*Lock | ||
for _, l := range locks { | ||
if lr.store.GetOracle().IsExpired(l.TxnID, l.TTL) { | ||
msBeforeLockExpired := lr.store.GetOracle().UntilExpired(l.TxnID, l.TTL) | ||
if msBeforeLockExpired <= 0 { | ||
tikvLockResolverCountWithExpired.Inc() | ||
expiredLocks = append(expiredLocks, l) | ||
} else { | ||
if msBeforeTxnExpired == 0 || msBeforeLockExpired < msBeforeTxnExpired { | ||
msBeforeTxnExpired = msBeforeLockExpired | ||
} | ||
tikvLockResolverCountWithNotExpired.Inc() | ||
} | ||
} | ||
if len(expiredLocks) == 0 { | ||
return false, nil | ||
if msBeforeTxnExpired > 0 { | ||
tikvLockResolverCountWithWaitExpired.Inc() | ||
} | ||
return | ||
} | ||
|
||
// TxnID -> []Region, record resolved Regions. | ||
// TODO: Maybe put it in LockResolver and share by all txns. | ||
cleanTxns := make(map[uint64]map[RegionVerID]struct{}) | ||
for _, l := range expiredLocks { | ||
status, err := lr.getTxnStatus(bo, l.TxnID, l.Primary) | ||
var status TxnStatus | ||
status, err = lr.getTxnStatus(bo, l.TxnID, l.Primary) | ||
if err != nil { | ||
return false, errors.Trace(err) | ||
msBeforeTxnExpired = 0 | ||
err = errors.Trace(err) | ||
return | ||
} | ||
|
||
cleanRegions := cleanTxns[l.TxnID] | ||
if cleanRegions == nil { | ||
cleanRegions, exists := cleanTxns[l.TxnID] | ||
if !exists { | ||
cleanRegions = make(map[RegionVerID]struct{}) | ||
cleanTxns[l.TxnID] = cleanRegions | ||
} | ||
|
||
err = lr.resolveLock(bo, l, status, cleanRegions) | ||
if err != nil { | ||
return false, errors.Trace(err) | ||
msBeforeTxnExpired = 0 | ||
err = errors.Trace(err) | ||
return | ||
} | ||
} | ||
return len(expiredLocks) == len(locks), nil | ||
return | ||
} | ||
|
||
// GetTxnStatus queries tikv-server for a txn's status (commit/rollback). | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// Copyright 2019 PingCAP, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package oracles | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/pingcap/tidb/store/tikv/oracle" | ||
) | ||
|
||
// SetOracleHookCurrentTime exports localOracle's time hook to test. | ||
func SetOracleHookCurrentTime(oc oracle.Oracle, t time.Time) { | ||
switch o := oc.(type) { | ||
case *localOracle: | ||
if o.hook == nil { | ||
o.hook = &struct { | ||
currentTime time.Time | ||
}{} | ||
} | ||
o.hook.currentTime = t | ||
} | ||
} | ||
|
||
// ClearOracleHook exports localOracle's clear hook method | ||
func ClearOracleHook(oc oracle.Oracle) { | ||
switch o := oc.(type) { | ||
case *localOracle: | ||
o.hook = nil | ||
} | ||
} | ||
|
||
// NewEmptyPDOracle exports pdOracle struct to test | ||
func NewEmptyPDOracle() oracle.Oracle { | ||
return &pdOracle{} | ||
} | ||
|
||
// SetEmptyPDOracleLastTs exports PD oracle's last ts to test. | ||
func SetEmptyPDOracleLastTs(oc oracle.Oracle, ts uint64) { | ||
switch o := oc.(type) { | ||
case *pdOracle: | ||
o.lastTS = ts | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When the maxSleepMs will be 0?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maxSleepMs never should be 0 in current usage, but backoff is a common function and we add new maxSleepMs Param, so we should take care about it and in logic "force sleep zero" is useful.