txn: avoid xlock starvation by setting slock shrink-only#19313
txn: avoid xlock starvation by setting slock shrink-only#19313ti-chi-bot[bot] merged 7 commits intotikv:masterfrom
Conversation
Signed-off-by: zyguan <zhongyangguan@gmail.com>
There was a problem hiding this comment.
Pull request overview
This PR refines the interaction between exclusive (xlock) and shared (slock) pessimistic locks to avoid exclusive-lock starvation when shared locks are present, especially under in-memory pessimistic locking.
Changes:
- Introduces a new MVCC error
NotInShrinkMode(SharedLocks)to represent xlock encountering shared locks that are not yet marked shrink-only, and wires it through the error and cloning infrastructure. - Updates
acquire_pessimistic_lock/AcquirePessimisticLockto (a) surfaceNotInShrinkModefrom the MVCC layer, (b) convert shared locks to shrink-only, persist them, and then return aKeyIsLockederror containing a shared-lockLockInfo. - Extends the scheduler to detect
KeyIsLockedresponses carrying shared-lock info and, in that case, skip the in-memory pessimistic lock fast path to ensure the shared-lock shrink-only update is actually persisted; adds tests around shared-lock behavior and the in-memory mode skip.
Reviewed changes
Copilot reviewed 8 out of 8 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
src/storage/txn/scheduler.rs |
Adds detection of KeyIsLocked on a shared lock (LockInfoExt::is_shared_lock) and prevents in-memory pessimistic lock insertion when persisting a shared-lock shrink-only update; adds a unit test ensuring the scheduler writes such updates to storage and not the in-memory table. |
src/storage/txn/mod.rs |
Exposes ProcessResult::get_key_lock_info to extract the inner LockInfo from PessimisticLockRes errors, enabling the scheduler’s new shared-lock detection. |
src/storage/txn/commands/acquire_pessimistic_lock.rs |
On NotInShrinkMode, clears prior mutations, marks the shared locks as shrink-only, writes them back, and then turns the result into a KeyIsLocked error with shared-lock info; adds an integration-style test for shared-lock semantics and a helper for invoking the command. |
src/storage/txn/commands/acquire_pessimistic_lock_resumed.rs |
Handles NotInShrinkMode in the resumed path by converting the shared locks to a LockInfo and treating the key as waiting (without modifying the shared locks), so resumed requests queue properly while acknowledging shared-lock conflicts. |
src/storage/txn/actions/acquire_pessimistic_lock.rs |
Extends core lock-acquisition logic to distinguish between shrink-only and non–shrink-only shared locks: xlock vs. slock interactions now return NotInShrinkMode or KeyIsLocked as appropriate; adds tests covering xlock meeting non-shrink-only slocks and slock meeting shrink-only slocks. |
src/storage/mvcc/mod.rs |
Introduces ErrorInner::NotInShrinkMode(SharedLocks), makes it cloneable via maybe_clone, and maps it to the KEY_IS_LOCKED error code for external error reporting. |
components/txn_types/src/lock.rs |
Adds SharedLocks shrink-only flag infrastructure (already existing) and introduces LockInfoExt::is_shared_lock() to check whether a LockInfo is a shared lock. |
components/txn_types/src/lib.rs |
Re-exports LockInfoExt from the lock module so that transaction code (e.g., scheduler) can use it when inspecting LockInfo values. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Signed-off-by: zyguan <zhongyangguan@gmail.com>
Signed-off-by: zyguan <zhongyangguan@gmail.com>
| lock_info.lock_wait_token = lock_wait_token; | ||
| lock_info.req_states = Some(req_states); | ||
| res.push(PessimisticLockKeyResult::Waiting); | ||
| encountered_locks.push(lock_info); |
There was a problem hiding this comment.
Would this lead to panic when resumable requests counter shared locks for the assert at scheduler.rs:2131?
Signed-off-by: you06 <you1474600@gmail.com>
Signed-off-by: zyguan <zhongyangguan@gmail.com>
|
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: cfzjywxk, you06 The full list of commands accepted by this bot can be found here. The pull request process is described here DetailsNeeds approval from an approver in each of these files:
Approvers can indicate their approval by writing |
ref tikv#19087 Avoid exclusive lock starvation by setting shared locks shrink-only: - `acquire_pessimistic_lock` returns `NotInShrinkMode` when an exclusive lock meets non-shrink-only shared locks. - `AcquirePessimisticLock.process_write` converts shared locks to shrink-only, persists them, and returns `KeyIsLocked` to the client. - The scheduler skips in-memory pessimistic lock writes when a shared-lock update is being persisted. Signed-off-by: you06 <you1474600@gmail.com> Signed-off-by: zyguan <zhongyangguan@gmail.com> Co-authored-by: you06 <you1474600@gmail.com>
What is changed and how it works?
Issue Number: ref #19087
What's Changed:
Related changes
pingcap/docs/pingcap/docs-cn:Check List
Tests
Side effects
Release note