Skip to content

txn: support shared lock for pessimistic / prewrite / commit schedulers#19110

Merged
cfzjywxk merged 6 commits intotikv:tikv-8.5-with-shared-lockfrom
you06:8.5-shared-lock/schedulers-1
Nov 18, 2025
Merged

txn: support shared lock for pessimistic / prewrite / commit schedulers#19110
cfzjywxk merged 6 commits intotikv:tikv-8.5-with-shared-lockfrom
you06:8.5-shared-lock/schedulers-1

Conversation

@you06
Copy link
Contributor

@you06 you06 commented Nov 13, 2025

What is changed and how it works?

Issue Number: ref #19087

What's Changed:

This PR implement the acquire_pessimistic_lock / prewrite / commit schedulers for shared lock.

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: you06 <you1474600@gmail.com>
Signed-off-by: you06 <you1474600@gmail.com>
@ti-chi-bot ti-chi-bot bot added release-note-none Denotes a PR that doesn't merit a release note. dco-signoff: yes Indicates the PR's author has signed the dco. size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. labels Nov 13, 2025
@you06 you06 requested review from cfzjywxk and zyguan November 13, 2025 14:13
need_old_value: bool,
lock_only_if_exists: bool,
allow_lock_with_conflict: bool,
is_shared_lock: bool,
Copy link
Collaborator

@cfzjywxk cfzjywxk Nov 14, 2025

Choose a reason for hiding this comment

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

Suggested change
is_shared_lock: bool,
is_shared_lock_req: bool,

seems better.

pessimistic: false,
let mut current_lock = reader.load_lock(&key)?;
if let Some(lock) = current_lock.as_mut() {
let can_add_shared_lock = is_shared_lock && lock.is_shared();
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can we seperate the pessimistic lock and shared lock handling into two different functions?

} else {
None
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe extract a function for these boilerplate code, eg.

fn must_decode_lock_into(either: &mut Either<Vec<u8>, Lock>) {
    if let Either::Left(encoded) = either {
        let lock = Lock::parse(encoded).expect("failed to parse shared lock txn info");
        *either = Either::Right(lock);
    }
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed the must-decode implementation and return Result now.

Signed-off-by: you06 <you1474600@gmail.com>
Signed-off-by: you06 <you1474600@gmail.com>
Mutation::Delete(ref key, _) | Mutation::Lock(ref key, _) => {
Mutation::Delete(ref key, _)
| Mutation::Lock(ref key, _)
| Mutation::SharedLock(ref key, _) => {
Copy link
Collaborator

Choose a reason for hiding this comment

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

It seems pingcap/kvproto#1377 is not supported in this PR?

Signed-off-by: you06 <you1474600@gmail.com>
Comment on lines +116 to +121
if !lock.is_shared() {
return Err(ErrorInner::KeyIsLocked(
current_lock.unwrap().into_lock_info(key.into_raw()?),
)
.into());
}
Copy link
Contributor

Choose a reason for hiding this comment

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

So we will reject a shared lock req when there is an existing exclusive pessimistic lock even they come from the same txn (with same ts)? I see the previous impl allows this case.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Reject the exclusive lock downgrade is more strict. Since we haven't well design the desired behavior, I think it's ok to return error here.

The client of TiKV may not send shared lock request when adding shared lock above an exclusive lock (and prewrite with Op::Lock as well).

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, in this case, the kv client won't send a shared lock request when the key has already been locked.

@ti-chi-bot ti-chi-bot bot added needs-1-more-lgtm Indicates a PR needs 1 more LGTM. approved labels Nov 18, 2025
Comment on lines +97 to +114
let (lock, shared_lock) = if lock.is_shared() {
if mutation.mutation_type != MutationType::SharedLock {
return Err(ErrorInner::Other(box_err!(
"use Op::SharedLock to prewrite on a shared lock"
))
.into());
}
match lock.remove_shared_lock(reader.start_ts)? {
Some(l) => (l, Some(lock)),
None => {
return Err(ErrorInner::PessimisticLockNotFound {
start_ts: reader.start_ts,
key: mutation.key.into_raw()?,
reason: PessimisticLockNotFoundReason::LockMissingAmendFail,
}
.into());
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Let txn1 aquire a shared lock on k first, then optimistic txn2 tries to prewrite k. Txn2 will fail with OtherError instead of KeyIsLocked ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nice catch, when an optimistic prewrite seeing a shared lock, it should return all the txn segments inside it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added a test that optimistic transaction will return KeyIsLocked error.

But it needs more changes to return multi locks for one mutation prewrite, I think we can do it in another PR.

Copy link
Contributor

Choose a reason for hiding this comment

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

OK

Signed-off-by: you06 <you1474600@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 Nov 18, 2025
@ti-chi-bot
Copy link
Contributor

ti-chi-bot bot commented Nov 18, 2025

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: cfzjywxk, zyguan

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 Nov 18, 2025

[LGTM Timeline notifier]

Timeline:

  • 2025-11-18 03:02:43.475415835 +0000 UTC m=+1362412.918445714: ☑️ agreed by cfzjywxk.
  • 2025-11-18 11:42:32.249059857 +0000 UTC m=+11915.898254304: ☑️ agreed by zyguan.

@cfzjywxk cfzjywxk merged commit 9777e06 into tikv:tikv-8.5-with-shared-lock Nov 18, 2025
2 of 3 checks passed
you06-pingcap pushed a commit to you06/tikv that referenced this pull request Nov 27, 2025
…rs (tikv#19110)

* impl pessimistic_lock/prewrite/commit schedulers

Signed-off-by: you06 <you1474600@gmail.com>

* add tests

Signed-off-by: you06 <you1474600@gmail.com>

* address comment

Signed-off-by: you06 <you1474600@gmail.com>

* address comment

Signed-off-by: you06 <you1474600@gmail.com>

* update kvproto

Signed-off-by: you06 <you1474600@gmail.com>

* address comment

Signed-off-by: you06 <you1474600@gmail.com>

---------

Signed-off-by: you06 <you1474600@gmail.com>
you06-pingcap pushed a commit to you06/tikv that referenced this pull request Jan 13, 2026
…rs (tikv#19110)

* impl pessimistic_lock/prewrite/commit schedulers

Signed-off-by: you06 <you1474600@gmail.com>

* add tests

Signed-off-by: you06 <you1474600@gmail.com>

* address comment

Signed-off-by: you06 <you1474600@gmail.com>

* address comment

Signed-off-by: you06 <you1474600@gmail.com>

* update kvproto

Signed-off-by: you06 <you1474600@gmail.com>

* address comment

Signed-off-by: you06 <you1474600@gmail.com>

---------

Signed-off-by: you06 <you1474600@gmail.com>
you06-pingcap pushed a commit to you06/tikv that referenced this pull request Jan 13, 2026
Signed-off-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.

3 participants