Skip to content

txn: implement Lock encode/decode#19237

Merged
ti-chi-bot[bot] merged 11 commits intotikv:masterfrom
you06:master-shared-lock/lock
Jan 6, 2026
Merged

txn: implement Lock encode/decode#19237
ti-chi-bot[bot] merged 11 commits intotikv:masterfrom
you06:master-shared-lock/lock

Conversation

@you06
Copy link
Contributor

@you06 you06 commented Dec 25, 2025

What is changed and how it works?

Issue Number: ref #19087

What's Changed:

Implemente encode/decode 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

Copilot AI review requested due to automatic review settings December 25, 2025 16:31
@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 Dec 25, 2025
@you06 you06 mentioned this pull request Dec 25, 2025
13 tasks
@you06 you06 changed the title Master shared lock/lock txn: implement Lock encode/decode Dec 25, 2025
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 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 SharedLocks struct to represent multiple transaction locks under a single shared lock
  • Updates parse_lock() to return Either<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.

Comment on lines +181 to +191
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);
Copy link

Copilot AI Dec 25, 2025

Choose a reason for hiding this comment

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

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.

Suggested change
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));

Copilot uses AI. Check for mistakes.
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>
@you06-pingcap you06-pingcap force-pushed the master-shared-lock/lock branch from be7c862 to 0aa8118 Compare December 26, 2025 11:32
Signed-off-by: you06 <you1474600@gmail.com>
Signed-off-by: you06 <you1474600@gmail.com>
Signed-off-by: you06 <you1474600@gmail.com>
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

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>
you06 added 4 commits January 5, 2026 15:08
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>
Copy link
Contributor

@zyguan zyguan left a comment

Choose a reason for hiding this comment

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

Generally LGTM


#[derive(PartialEq, Clone, Debug, Default)]
pub struct SharedLocks {
pub txn_info_segments: HashMap<TimeStamp, Either<Vec<u8>, Lock>>,
Copy link
Contributor

Choose a reason for hiding this comment

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

For those common fields (like, rollback_ts, last_chang) associated with the key, do we plan to add them when it's necessary?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I do think so, let's keep minimal change in the lock.

  • last_change_ts is 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>
@you06
Copy link
Contributor Author

you06 commented Jan 6, 2026

/retest

@ti-chi-bot ti-chi-bot bot added the needs-1-more-lgtm Indicates a PR needs 1 more LGTM. label Jan 6, 2026
@ti-chi-bot ti-chi-bot bot added the approved label Jan 6, 2026
@ti-chi-bot ti-chi-bot bot added the lgtm label Jan 6, 2026
@ti-chi-bot
Copy link
Contributor

ti-chi-bot bot commented Jan 6, 2026

[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 ti-chi-bot bot removed the needs-1-more-lgtm Indicates a PR needs 1 more LGTM. label Jan 6, 2026
@ti-chi-bot
Copy link
Contributor

ti-chi-bot bot commented Jan 6, 2026

[LGTM Timeline notifier]

Timeline:

  • 2026-01-06 06:20:53.193974288 +0000 UTC m=+683209.012282740: ☑️ agreed by zyguan.
  • 2026-01-06 06:26:24.39057791 +0000 UTC m=+683540.208886342: ☑️ agreed by cfzjywxk.

@ti-chi-bot ti-chi-bot bot merged commit 607a79a into tikv:master Jan 6, 2026
9 checks passed
@ti-chi-bot ti-chi-bot bot added this to the Pool milestone Jan 6, 2026
you06-pingcap pushed a commit to you06/tikv that referenced this pull request Feb 24, 2026
ref tikv#19087

Implemente encode/decode for shared lock.

Signed-off-by: you06 <you1474600@gmail.com>
you06-pingcap pushed a commit to you06/tikv that referenced this pull request Feb 27, 2026
ref tikv#19087

Implemente encode/decode for shared lock.

Signed-off-by: you06 <you1474600@gmail.com>
ti-chi-bot bot pushed a commit that referenced this pull request Feb 27, 2026
ref #19087

Implemente encode/decode for shared lock.

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

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.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