-
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
domain: add resolve lock logic for mvcc get key loading schema diff #48282
Merged
cfzjywxk
merged 2 commits into
pingcap:tidb-6.5-with-kv-timeout-feature
from
cfzjywxk:tidb-6.5-with-kv-timeout-feature-resolve
Nov 7, 2023
Merged
Changes from 1 commit
Commits
Show all changes
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -94,9 +94,11 @@ func NewHelper(store Storage) *Helper { | |
} | ||
} | ||
|
||
// GetMvccByEncodedKey get the MVCC value by the specific encoded key. | ||
func (h *Helper) GetMvccByEncodedKey(encodedKey kv.Key) (*kvrpcpb.MvccGetByKeyResponse, error) { | ||
bo := tikv.NewBackofferWithVars(context.Background(), 5000, nil) | ||
// GetMvccByEncodedKeyWithTS get the MVCC value by the specific encoded key, if lock is encountered it would be resolved. | ||
func (h *Helper) GetMvccByEncodedKeyWithTS(encodedKey kv.Key, startTS uint64) (*kvrpcpb.MvccGetByKeyResponse, error) { | ||
// A derived value from previous implementation possible experiencing value 5000ms. | ||
MaxBackoffTimeout := 5000 | ||
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. Extract a const? |
||
bo := tikv.NewBackofferWithVars(context.Background(), MaxBackoffTimeout, nil) | ||
tikvReq := tikvrpc.NewRequest(tikvrpc.CmdMvccGetByKey, &kvrpcpb.MvccGetByKeyRequest{Key: encodedKey}) | ||
for { | ||
keyLocation, err := h.RegionCache.LocateKey(bo, encodedKey) | ||
|
@@ -106,14 +108,15 @@ func (h *Helper) GetMvccByEncodedKey(encodedKey kv.Key) (*kvrpcpb.MvccGetByKeyRe | |
|
||
kvResp, err := h.Store.SendReq(bo, tikvReq, keyLocation.Region, time.Minute) | ||
if err != nil { | ||
logutil.BgLogger().Info("get MVCC by encoded key failed", | ||
logutil.BgLogger().Warn("get MVCC by encoded key failed", | ||
zap.Stringer("encodeKey", encodedKey), | ||
zap.Reflect("region", keyLocation.Region), | ||
zap.Stringer("keyLocation", keyLocation), | ||
zap.Reflect("kvResp", kvResp), | ||
zap.Error(err)) | ||
return nil, errors.Trace(err) | ||
} | ||
|
||
regionErr, err := kvResp.GetRegionError() | ||
if err != nil { | ||
return nil, errors.Trace(err) | ||
|
@@ -124,20 +127,70 @@ func (h *Helper) GetMvccByEncodedKey(encodedKey kv.Key) (*kvrpcpb.MvccGetByKeyRe | |
} | ||
continue | ||
} | ||
|
||
mvccResp := kvResp.Resp.(*kvrpcpb.MvccGetByKeyResponse) | ||
if errMsg := mvccResp.GetError(); errMsg != "" { | ||
logutil.BgLogger().Info("get MVCC by encoded key failed", | ||
logutil.BgLogger().Warn("get MVCC by encoded key failed", | ||
zap.Stringer("encodeKey", encodedKey), | ||
zap.Reflect("region", keyLocation.Region), | ||
zap.Stringer("keyLocation", keyLocation), | ||
zap.Reflect("kvResp", kvResp), | ||
zap.String("error", errMsg)) | ||
return nil, errors.New(errMsg) | ||
} | ||
if mvccResp.Info == nil { | ||
errMsg := "Invalid mvcc response result, the info field is nil" | ||
logutil.BgLogger().Warn(errMsg, | ||
zap.Stringer("encodeKey", encodedKey), | ||
zap.Reflect("region", keyLocation.Region), | ||
zap.Stringer("keyLocation", keyLocation), | ||
zap.Reflect("kvResp", kvResp)) | ||
return nil, errors.New(errMsg) | ||
} | ||
|
||
// Try to resolve the lock and retry mvcc get again if the input startTS is a valid value. | ||
if startTS > 0 && mvccResp.Info.GetLock() != nil { | ||
lockInfo := mvccResp.Info.GetLock() | ||
lock := &txnlock.Lock{ | ||
Key: []byte(encodedKey), | ||
Primary: lockInfo.GetPrimary(), | ||
TxnID: lockInfo.GetStartTs(), | ||
TTL: lockInfo.GetTtl(), | ||
TxnSize: lockInfo.GetTxnSize(), | ||
LockType: lockInfo.GetType(), | ||
UseAsyncCommit: lockInfo.GetUseAsyncCommit(), | ||
LockForUpdateTS: lockInfo.GetForUpdateTs(), | ||
} | ||
// Disable for read to avoid async resolve. | ||
resolveLocksOpts := txnlock.ResolveLocksOptions{ | ||
CallerStartTS: startTS, | ||
Locks: []*txnlock.Lock{lock}, | ||
Lite: true, | ||
ForRead: false, | ||
Detail: nil, | ||
} | ||
resolveLockRes, err := h.Store.GetLockResolver().ResolveLocksWithOpts(bo, resolveLocksOpts) | ||
if err != nil { | ||
return nil, err | ||
} | ||
msBeforeExpired := resolveLockRes.TTL | ||
if msBeforeExpired > 0 { | ||
if err = bo.BackoffWithCfgAndMaxSleep(tikv.BoTxnLock(), int(msBeforeExpired), | ||
errors.Errorf("resolve lock fails lock: %v", lock)); err != nil { | ||
return nil, err | ||
} | ||
} | ||
continue | ||
} | ||
return mvccResp, nil | ||
} | ||
} | ||
|
||
// GetMvccByEncodedKey get the MVCC value by the specific encoded key. | ||
func (h *Helper) GetMvccByEncodedKey(encodedKey kv.Key) (*kvrpcpb.MvccGetByKeyResponse, error) { | ||
return h.GetMvccByEncodedKeyWithTS(encodedKey, 0) | ||
} | ||
|
||
// MvccKV wraps the key's mvcc info in tikv. | ||
type MvccKV struct { | ||
Key string `json:"key"` | ||
|
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.
Can we just call another
GetMvccByEncodedKey(lock.primary)
to get the commit-ts? Resolving lock and retring makes GetMvcc complex and heavy.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.
It's possible the primary key is not committed or the ongoing internal transaction is invisible to the input
start_ts
.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.
Besides, it's better to use snapshot interfaces instead of the mvcc interfaces, I've filed an issue about it #48283