-
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
txn: introduce the statement buffer of pessimistic lock cache #43530
Merged
ti-chi-bot
merged 3 commits into
pingcap:master
from
ekexium:rc-inconsistency-failed-dml
May 11, 2023
Merged
Changes from all commits
Commits
Show all changes
3 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
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -246,6 +246,12 @@ type TxnCtxNoNeedToRestore struct { | |||||||
// fair locking mode, and it takes effect (which is determined according to whether lock-with-conflict | ||||||||
// has occurred during execution of any statement). | ||||||||
FairLockingEffective bool | ||||||||
|
||||||||
// CurrentStmtPessimisticLockCache is the cache for pessimistic locked keys in the current statement. | ||||||||
// It is merged into `pessimisticLockCache` after a statement finishes. | ||||||||
// Read results cannot be directly written into pessimisticLockCache because failed statement need to rollback | ||||||||
// its pessimistic locks. | ||||||||
CurrentStmtPessimisticLockCache map[string][]byte | ||||||||
} | ||||||||
|
||||||||
// SavepointRecord indicates a transaction's savepoint record. | ||||||||
|
@@ -317,22 +323,32 @@ func (tc *TransactionContext) UpdateDeltaForTable(physicalTableID int64, delta i | |||||||
|
||||||||
// GetKeyInPessimisticLockCache gets a key in pessimistic lock cache. | ||||||||
func (tc *TransactionContext) GetKeyInPessimisticLockCache(key kv.Key) (val []byte, ok bool) { | ||||||||
if tc.pessimisticLockCache == nil { | ||||||||
if tc.pessimisticLockCache == nil && tc.CurrentStmtPessimisticLockCache == nil { | ||||||||
return nil, false | ||||||||
} | ||||||||
val, ok = tc.pessimisticLockCache[string(key)] | ||||||||
if ok { | ||||||||
tc.PessimisticCacheHit++ | ||||||||
if tc.CurrentStmtPessimisticLockCache != nil { | ||||||||
val, ok = tc.CurrentStmtPessimisticLockCache[string(key)] | ||||||||
if ok { | ||||||||
tc.PessimisticCacheHit++ | ||||||||
return | ||||||||
} | ||||||||
} | ||||||||
if tc.pessimisticLockCache != nil { | ||||||||
val, ok = tc.pessimisticLockCache[string(key)] | ||||||||
if ok { | ||||||||
tc.PessimisticCacheHit++ | ||||||||
} | ||||||||
} | ||||||||
return | ||||||||
} | ||||||||
|
||||||||
// SetPessimisticLockCache sets a key value pair into pessimistic lock cache. | ||||||||
// SetPessimisticLockCache sets a key value pair in pessimistic lock cache. | ||||||||
// The value is buffered in the statement cache until the current statement finishes. | ||||||||
func (tc *TransactionContext) SetPessimisticLockCache(key kv.Key, val []byte) { | ||||||||
if tc.pessimisticLockCache == nil { | ||||||||
tc.pessimisticLockCache = map[string][]byte{} | ||||||||
if tc.CurrentStmtPessimisticLockCache == nil { | ||||||||
tc.CurrentStmtPessimisticLockCache = make(map[string][]byte) | ||||||||
} | ||||||||
tc.pessimisticLockCache[string(key)] = val | ||||||||
tc.CurrentStmtPessimisticLockCache[string(key)] = val | ||||||||
} | ||||||||
|
||||||||
// Cleanup clears up transaction info that no longer use. | ||||||||
|
@@ -345,6 +361,7 @@ func (tc *TransactionContext) Cleanup() { | |||||||
tc.relatedTableForMDL = nil | ||||||||
tc.tdmLock.Unlock() | ||||||||
tc.pessimisticLockCache = nil | ||||||||
tc.CurrentStmtPessimisticLockCache = nil | ||||||||
tc.IsStaleness = false | ||||||||
tc.Savepoints = nil | ||||||||
tc.EnableMDL = false | ||||||||
|
@@ -380,6 +397,8 @@ func (tc *TransactionContext) GetCurrentSavepoint() TxnCtxNeedToRestore { | |||||||
} | ||||||||
pessimisticLockCache := make(map[string][]byte, len(tc.pessimisticLockCache)) | ||||||||
maps.Copy(pessimisticLockCache, tc.pessimisticLockCache) | ||||||||
CurrentStmtPessimisticLockCache := make(map[string][]byte, len(tc.CurrentStmtPessimisticLockCache)) | ||||||||
maps.Copy(CurrentStmtPessimisticLockCache, tc.CurrentStmtPessimisticLockCache) | ||||||||
cachedTables := make(map[int64]interface{}, len(tc.CachedTables)) | ||||||||
maps.Copy(cachedTables, tc.CachedTables) | ||||||||
return TxnCtxNeedToRestore{ | ||||||||
|
@@ -448,6 +467,21 @@ func (tc *TransactionContext) RollbackToSavepoint(name string) *SavepointRecord | |||||||
return nil | ||||||||
} | ||||||||
|
||||||||
// FlushStmtPessimisticLockCache merges the current statement pessimistic lock cache into transaction pessimistic lock | ||||||||
// cache. The caller may need to clear the stmt cache itself. | ||||||||
func (tc *TransactionContext) FlushStmtPessimisticLockCache() { | ||||||||
if tc.CurrentStmtPessimisticLockCache == nil { | ||||||||
return | ||||||||
} | ||||||||
if tc.pessimisticLockCache == nil { | ||||||||
tc.pessimisticLockCache = make(map[string][]byte) | ||||||||
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.
Suggested change
Can we just assign |
||||||||
} | ||||||||
for key, val := range tc.CurrentStmtPessimisticLockCache { | ||||||||
tc.pessimisticLockCache[key] = val | ||||||||
} | ||||||||
tc.CurrentStmtPessimisticLockCache = nil | ||||||||
} | ||||||||
|
||||||||
// WriteStmtBufs can be used by insert/replace/delete/update statement. | ||||||||
// TODO: use a common memory pool to replace this. | ||||||||
type WriteStmtBufs struct { | ||||||||
|
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
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.
The map
CurrentStmtPessimisticLockCache
is never used? Should put it intoTxnCtxNeedToRestore
?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.
Oh, I think no need to put
CurrentStmtPessimisticLockCache
intoTxnCtxNeedToRestore
, then we can just remove those 2 line code?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.
Yes. It must be a mistake during refactoring.