Skip to content

txn: avoid xlock starvation by setting slock shrink-only#19313

Merged
ti-chi-bot[bot] merged 7 commits intotikv:masterfrom
zyguan:dev/slock-shrink-only
Jan 26, 2026
Merged

txn: avoid xlock starvation by setting slock shrink-only#19313
ti-chi-bot[bot] merged 7 commits intotikv:masterfrom
zyguan:dev/slock-shrink-only

Conversation

@zyguan
Copy link
Contributor

@zyguan zyguan commented Jan 23, 2026

What is changed and how it works?

Issue Number: ref #19087

What's Changed:

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.

Related changes

  • PR to update pingcap/docs/pingcap/docs-cn:
  • Need to cherry-pick to the release branch

Check List

Tests

  • Unit test
  • Integration test
  • Manual test (add detailed scripts or steps below)
  • No code

Side effects

  • Performance regression: Consumes more CPU
  • Performance regression: Consumes more Memory
  • Breaking backward compatibility

Release note

None

Signed-off-by: zyguan <zhongyangguan@gmail.com>
Copilot AI review requested due to automatic review settings January 23, 2026 03:35
@ti-chi-bot ti-chi-bot bot added dco-signoff: yes Indicates the PR's author has signed the dco. do-not-merge/release-note-label-needed Indicates that a PR should not merge because it's missing one of the release note labels. size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. labels Jan 23, 2026
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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/AcquirePessimisticLock to (a) surface NotInShrinkMode from the MVCC layer, (b) convert shared locks to shrink-only, persist them, and then return a KeyIsLocked error containing a shared-lock LockInfo.
  • Extends the scheduler to detect KeyIsLocked responses 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.

@zyguan zyguan requested review from cfzjywxk and you06 January 23, 2026 03:44
@you06 you06 mentioned this pull request Jan 23, 2026
13 tasks
Signed-off-by: zyguan <zhongyangguan@gmail.com>
Signed-off-by: zyguan <zhongyangguan@gmail.com>
@ti-chi-bot ti-chi-bot bot added needs-1-more-lgtm Indicates a PR needs 1 more LGTM. approved labels Jan 23, 2026
lock_info.lock_wait_token = lock_wait_token;
lock_info.req_states = Some(req_states);
res.push(PessimisticLockKeyResult::Waiting);
encountered_locks.push(lock_info);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would this lead to panic when resumable requests counter shared locks for the assert at scheduler.rs:2131?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed, PTAL.

@ti-chi-bot ti-chi-bot bot added release-note-none Denotes a PR that doesn't merit a release note. and removed do-not-merge/release-note-label-needed Indicates that a PR should not merge because it's missing one of the release note labels. labels Jan 26, 2026
@ti-chi-bot ti-chi-bot bot added size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. and removed size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. labels Jan 26, 2026
Signed-off-by: zyguan <zhongyangguan@gmail.com>
Signed-off-by: zyguan <zhongyangguan@gmail.com>
@ti-chi-bot ti-chi-bot bot added lgtm and removed needs-1-more-lgtm Indicates a PR needs 1 more LGTM. labels Jan 26, 2026
@ti-chi-bot
Copy link
Contributor

ti-chi-bot bot commented Jan 26, 2026

[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

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@ti-chi-bot
Copy link
Contributor

ti-chi-bot bot commented Jan 26, 2026

[LGTM Timeline notifier]

Timeline:

  • 2026-01-23 11:12:07.137523534 +0000 UTC m=+751554.751480390: ☑️ agreed by you06.
  • 2026-01-26 15:12:13.501420656 +0000 UTC m=+1025161.115377512: ☑️ agreed by cfzjywxk.

@ti-chi-bot ti-chi-bot bot merged commit 6182259 into tikv:master Jan 26, 2026
9 checks passed
@ti-chi-bot ti-chi-bot bot added this to the Pool milestone Jan 26, 2026
wshwsh12 pushed a commit to wshwsh12/tikv that referenced this pull request Feb 9, 2026
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approved dco-signoff: yes Indicates the PR's author has signed the dco. lgtm release-note-none Denotes a PR that doesn't merit a release note. size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants