txn: support shared lock for pessimistic / prewrite / commit schedulers#19110
Conversation
Signed-off-by: you06 <you1474600@gmail.com>
| need_old_value: bool, | ||
| lock_only_if_exists: bool, | ||
| allow_lock_with_conflict: bool, | ||
| is_shared_lock: bool, |
There was a problem hiding this comment.
| 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(); |
There was a problem hiding this comment.
Can we seperate the pessimistic lock and shared lock handling into two different functions?
| } else { | ||
| None | ||
| } | ||
| } |
There was a problem hiding this comment.
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);
}
}There was a problem hiding this comment.
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, _) => { |
There was a problem hiding this comment.
It seems pingcap/kvproto#1377 is not supported in this PR?
Signed-off-by: you06 <you1474600@gmail.com>
| if !lock.is_shared() { | ||
| return Err(ErrorInner::KeyIsLocked( | ||
| current_lock.unwrap().into_lock_info(key.into_raw()?), | ||
| ) | ||
| .into()); | ||
| } |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
Yes, in this case, the kv client won't send a shared lock request when the key has already been locked.
| 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()); | ||
| } | ||
| } |
There was a problem hiding this comment.
Let txn1 aquire a shared lock on k first, then optimistic txn2 tries to prewrite k. Txn2 will fail with OtherError instead of KeyIsLocked ?
There was a problem hiding this comment.
Nice catch, when an optimistic prewrite seeing a shared lock, it should return all the txn segments inside it.
There was a problem hiding this comment.
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.
Signed-off-by: you06 <you1474600@gmail.com>
|
[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 DetailsNeeds approval from an approver in each of these files:
Approvers can indicate their approval by writing |
9777e06
into
tikv:tikv-8.5-with-shared-lock
…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>
…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>
Signed-off-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