-
Notifications
You must be signed in to change notification settings - Fork 233
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: record resolving locks #473
Merged
Merged
Changes from 5 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
4e5f60a
record resolving locks
longfangsong af5f586
rename to ResolvingLock
longfangsong d423b91
Add test probe for resolving
longfangsong b761265
refactor according to comments
longfangsong 0bd2e8f
Merge branch 'master' into lock_wait_resolving
longfangsong 3f28622
Merge branch 'master' into lock_wait_resolving
longfangsong 49ca14d
Add ResolveLocksDone for other call of ResolveLocks
longfangsong 5eb48e6
Update txnkv/txnlock/lock_resolver.go
longfangsong 35484dd
Update to new design
longfangsong 123746b
Merge branch 'master' into lock_wait_resolving
sticnarf 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 |
---|---|---|
|
@@ -62,6 +62,8 @@ type LockResolver struct { | |
resolveLockLiteThreshold uint64 | ||
mu struct { | ||
sync.RWMutex | ||
// currentStartTS resolving locks | ||
longfangsong marked this conversation as resolved.
Show resolved
Hide resolved
|
||
resolving map[uint64][]Lock | ||
// resolved caches resolved txns (FIFO, txn id -> txnStatus). | ||
resolved map[uint64]TxnStatus | ||
recentResolved *list.List | ||
|
@@ -76,13 +78,22 @@ type LockResolver struct { | |
asyncResolveCancel func() | ||
} | ||
|
||
// ResolvingLock stands for current resolving locks' information | ||
type ResolvingLock struct { | ||
TxnID uint64 | ||
LockTxnID uint64 | ||
Key []byte | ||
Primary []byte | ||
} | ||
|
||
// NewLockResolver creates a new LockResolver instance. | ||
func NewLockResolver(store storage) *LockResolver { | ||
r := &LockResolver{ | ||
store: store, | ||
resolveLockLiteThreshold: config.GetGlobalConfig().TiKVClient.ResolveLockLiteThreshold, | ||
} | ||
r.mu.resolved = make(map[uint64]TxnStatus) | ||
r.mu.resolving = make(map[uint64][]Lock) | ||
r.mu.recentResolved = list.New() | ||
r.asyncResolveCtx, r.asyncResolveCancel = context.WithCancel(context.Background()) | ||
return r | ||
|
@@ -311,6 +322,7 @@ func (lr *LockResolver) BatchResolveLocks(bo *retry.Backoffer, locks []*Lock, lo | |
// 3) Send `ResolveLock` cmd to the lock's region to resolve all locks belong to | ||
// the same transaction. | ||
func (lr *LockResolver) ResolveLocks(bo *retry.Backoffer, callerStartTS uint64, locks []*Lock) (int64, error) { | ||
lr.saveResolvingLocks(locks, callerStartTS) | ||
ttl, _, _, err := lr.resolveLocks(bo, callerStartTS, locks, false, false) | ||
return ttl, err | ||
} | ||
|
@@ -319,9 +331,20 @@ func (lr *LockResolver) ResolveLocks(bo *retry.Backoffer, callerStartTS uint64, | |
// Read operations needn't wait for resolve secondary locks and can read through(the lock's transaction is committed | ||
// and its commitTS is less than or equal to callerStartTS) or ignore(the lock's transaction is rolled back or its minCommitTS is pushed) the lock . | ||
func (lr *LockResolver) ResolveLocksForRead(bo *retry.Backoffer, callerStartTS uint64, locks []*Lock, lite bool) (int64, []uint64 /* canIgnore */, []uint64 /* canAccess */, error) { | ||
lr.saveResolvingLocks(locks, callerStartTS) | ||
return lr.resolveLocks(bo, callerStartTS, locks, true, lite) | ||
} | ||
|
||
func (lr *LockResolver) saveResolvingLocks(locks []*Lock, callerStartTS uint64) { | ||
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. What if For example, a batch get involves multiple regions and it encounters locks in more than one regions. But the |
||
resolving := []Lock{} | ||
longfangsong marked this conversation as resolved.
Show resolved
Hide resolved
|
||
for _, lock := range locks { | ||
resolving = append(resolving, *lock) | ||
} | ||
lr.mu.Lock() | ||
lr.mu.resolving[callerStartTS] = resolving | ||
lr.mu.Unlock() | ||
longfangsong marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
func (lr *LockResolver) resolveLocks(bo *retry.Backoffer, callerStartTS uint64, locks []*Lock, forRead bool, lite bool) (int64, []uint64 /* canIgnore */, []uint64 /* canAccess */, error) { | ||
if lr.testingKnobs.meetLock != nil { | ||
lr.testingKnobs.meetLock(locks) | ||
|
@@ -425,6 +448,31 @@ func (lr *LockResolver) resolveLocks(bo *retry.Backoffer, callerStartTS uint64, | |
return msBeforeTxnExpired.value(), canIgnore, canAccess, nil | ||
} | ||
|
||
// ResolveLocksDone will remove resolving locks information related with callerStartTS | ||
func (lr *LockResolver) ResolveLocksDone(callerStartTS uint64) { | ||
lr.mu.Lock() | ||
delete(lr.mu.resolving, callerStartTS) | ||
lr.mu.Unlock() | ||
} | ||
|
||
// Resolving returns the locks' information we are resolving currently. | ||
func (lr *LockResolver) Resolving() []ResolvingLock { | ||
result := []ResolvingLock{} | ||
lr.mu.RLock() | ||
defer lr.mu.RUnlock() | ||
for txnID, item := range lr.mu.resolving { | ||
for _, lock := range item { | ||
result = append(result, ResolvingLock{ | ||
TxnID: txnID, | ||
LockTxnID: lock.TxnID, | ||
Key: lock.Key, | ||
Primary: lock.Primary, | ||
}) | ||
} | ||
} | ||
return result | ||
} | ||
|
||
type txnExpireTime struct { | ||
initialized bool | ||
txnExpire int64 | ||
|
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.
Why is the
defer
written in prewrite instead of inResolveLocks
?ResolveLocksDone
looks missing if resolve lock is triggered by other commands.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 because we need to keep the resolving infomation during backoffing. From the user's POV, we are doing the lock resolving work during backoffing.
I've added them now.