fix data race in the LockKeys (#655)

Signed-off-by: Weizhen Wang <wangweizhen@pingcap.com>
This commit is contained in:
Weizhen Wang 2023-01-06 13:31:13 +08:00 committed by GitHub
parent 1780ed1dca
commit 5a4b9accad
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 16 additions and 0 deletions

View File

@ -834,6 +834,17 @@ func (txn *KVTxn) filterAggressiveLockedKeys(lockCtx *tikv.LockCtx, allKeys [][]
// LockKeys tries to lock the entries with the keys in KV store. // LockKeys tries to lock the entries with the keys in KV store.
// lockCtx is the context for lock, lockCtx.lockWaitTime in ms // lockCtx is the context for lock, lockCtx.lockWaitTime in ms
func (txn *KVTxn) LockKeys(ctx context.Context, lockCtx *tikv.LockCtx, keysInput ...[]byte) error { func (txn *KVTxn) LockKeys(ctx context.Context, lockCtx *tikv.LockCtx, keysInput ...[]byte) error {
return txn.lockKeys(ctx, lockCtx, nil, keysInput...)
}
// LockKeysFunc tries to lock the entries with the keys in KV store.
// lockCtx is the context for lock, lockCtx.lockWaitTime in ms
// fn is a function which run before the lock is released.
func (txn *KVTxn) LockKeysFunc(ctx context.Context, lockCtx *tikv.LockCtx, fn func(), keysInput ...[]byte) error {
return txn.lockKeys(ctx, lockCtx, fn, keysInput...)
}
func (txn *KVTxn) lockKeys(ctx context.Context, lockCtx *tikv.LockCtx, fn func(), keysInput ...[]byte) error {
if txn.interceptor != nil { if txn.interceptor != nil {
// User has called txn.SetRPCInterceptor() to explicitly set an interceptor, we // User has called txn.SetRPCInterceptor() to explicitly set an interceptor, we
// need to bind it to ctx so that the internal client can perceive and execute // need to bind it to ctx so that the internal client can perceive and execute
@ -871,6 +882,11 @@ func (txn *KVTxn) LockKeys(ctx context.Context, lockCtx *tikv.LockCtx, keysInput
} }
} }
}() }()
defer func() {
if fn != nil {
fn()
}
}()
if !txn.IsPessimistic() && txn.aggressiveLockingContext != nil { if !txn.IsPessimistic() && txn.aggressiveLockingContext != nil {
return errors.New("trying to perform aggressive locking in optimistic transaction") return errors.New("trying to perform aggressive locking in optimistic transaction")