txn: implement Lock encode/decode#19237
Conversation
There was a problem hiding this comment.
Pull request overview
This PR implements encode/decode functionality for shared locks in TiKV's MVCC system. The implementation adds support for a new LockType::Shared variant that can contain multiple transaction locks, enabling shared locking semantics.
- Introduces
SharedLocksstruct to represent multiple transaction locks under a single shared lock - Updates
parse_lock()to returnEither<Lock, SharedLocks>to handle both lock types - Adds serialization/deserialization logic for the new shared lock format with embedded transaction info segments
Reviewed changes
Copilot reviewed 38 out of 39 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| components/txn_types/src/lock.rs | Core implementation: adds LockType::Shared enum variant, SharedLocks struct with encoding/decoding, parse_lock() function, and helper methods |
| components/txn_types/src/lib.rs | Exports new LockOrSharedLocks type alias and parse_lock() function |
| components/txn_types/src/write.rs | Maps LockType::Shared to None for WriteType conversion |
| components/tikv_util/src/lib.rs | Adds PartialEq derive to Either enum |
| components/tikv_util/src/memory.rs | Implements HeapSize for Either and refactors tuple HeapSize implementations using macro |
| src/storage/types.rs | Maps LockType::Shared to kvrpcpb::Op::SharedLock in MvccInfo |
| src/storage/txn/store.rs | Handles Either return type from parse_lock() with unimplemented for SharedLocks case |
| src/storage/txn/commands/*.rs | Updates lock loading sites to handle Either<Lock, SharedLocks> return type |
| src/storage/txn/actions/*.rs | Updates transaction action handlers to pattern match on Either type |
| src/storage/mvcc/reader/*.rs | Updates all MVCC readers and scanners to handle new lock type with Either pattern matching |
| src/storage/mvcc/*.rs | Updates MVCC module to handle Either return type from load_lock() |
| src/storage/mod.rs | Renames futures::Either to avoid conflict with tikv_util::Either |
| src/server/*.rs | Updates debug and reset_to_version modules to handle Either type |
| components/resolved_ts/src/*.rs | Updates resolved timestamp components to handle new lock type |
| components/cdc/src/*.rs | Updates CDC components to pattern match on Either type |
| components/backup-stream/src/*.rs | Updates backup stream to handle shared locks and Either type |
| components/snap_recovery/src/*.rs | Updates snapshot recovery to handle Either type |
| Cargo.toml | Updates kvproto dependency to specific revision with SharedLock support |
| Cargo.lock | Updates dependency lock with new kvproto revision |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
components/resolved_ts/src/cmd.rs
Outdated
| let lock = match txn_types::parse_lock(value).ok()? { | ||
| Either::Left(lock) => lock, | ||
| Either::Right(_shared_locks) => unimplemented!( | ||
| "SharedLocks returned from txn_types::parse_lock is not supported here" | ||
| ), | ||
| }; | ||
| debug!("skip lock record"; | ||
| "type" => ?lock.lock_type, | ||
| "start_ts" => ?lock.ts, | ||
| "key" => log_wrappers::Value(key), | ||
| "for_update_ts" => ?lock.for_update_ts); |
There was a problem hiding this comment.
In the non-matching branch (line 178-194), when debug_assertions is enabled, the code calls txn_types::parse_lock() to fully parse the lock just to extract debugging information. However, this parsing is already skipped in release builds. For consistency and efficiency, consider using Lock::detect_lock_ts() instead of full parsing to get the timestamp for the debug log, or only parse if absolutely necessary for debugging.
| let lock = match txn_types::parse_lock(value).ok()? { | |
| Either::Left(lock) => lock, | |
| Either::Right(_shared_locks) => unimplemented!( | |
| "SharedLocks returned from txn_types::parse_lock is not supported here" | |
| ), | |
| }; | |
| debug!("skip lock record"; | |
| "type" => ?lock.lock_type, | |
| "start_ts" => ?lock.ts, | |
| "key" => log_wrappers::Value(key), | |
| "for_update_ts" => ?lock.for_update_ts); | |
| let ts = Lock::detect_lock_ts(value).ok(); | |
| debug!("skip lock record"; | |
| "type" => ?lock_type, | |
| "start_ts" => ?ts, | |
| "key" => log_wrappers::Value(key)); |
Signed-off-by: you06 <you1474600@gmail.com> split Lock Signed-off-by: you06 <you1474600@gmail.com> split Lock and SharedLocks into 2 types definition Signed-off-by: you06 <you1474600@gmail.com> remove unused changes Signed-off-by: you06 <you1474600@gmail.com> fix build & clippy Signed-off-by: you06 <you1474600@gmail.com> format code Signed-off-by: you06 <you1474600@gmail.com> refactor Signed-off-by: you06 <you1474600@gmail.com> simplify code Signed-off-by: you06 <you1474600@gmail.com>
be7c862 to
0aa8118
Compare
Signed-off-by: you06 <you1474600@gmail.com>
Signed-off-by: you06 <you1474600@gmail.com>
Signed-off-by: you06 <you1474600@gmail.com>
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 41 out of 42 changed files in this pull request and generated 9 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Signed-off-by: you06 <you1474600@gmail.com>
Signed-off-by: you06 <you1474600@gmail.com>
Signed-off-by: you06 <you1474600@gmail.com>
Signed-off-by: you06 <you1474600@gmail.com>
Signed-off-by: you06 <you1474600@gmail.com>
Signed-off-by: you06 <you1474600@gmail.com>
components/txn_types/src/lock.rs
Outdated
|
|
||
| #[derive(PartialEq, Clone, Debug, Default)] | ||
| pub struct SharedLocks { | ||
| pub txn_info_segments: HashMap<TimeStamp, Either<Vec<u8>, Lock>>, |
There was a problem hiding this comment.
For those common fields (like, rollback_ts, last_chang) associated with the key, do we plan to add them when it's necessary?
There was a problem hiding this comment.
I do think so, let's keep minimal change in the lock.
last_change_tsis an optimization which is not a must do.- Rollback records need to be written to write cf, so they are possible unnecessary here.
Signed-off-by: you06 <you1474600@gmail.com>
|
/retest |
|
[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 |
ref tikv#19087 Implemente encode/decode for shared lock. Signed-off-by: you06 <you1474600@gmail.com>
ref tikv#19087 Implemente encode/decode for shared lock. 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