Skip to content

txnkv: support shared lock upgrades#2014

Draft
wfxr wants to merge 7 commits into
tikv:masterfrom
wfxr:feat/shared-lock-upgrade-next-gen
Draft

txnkv: support shared lock upgrades#2014
wfxr wants to merge 7 commits into
tikv:masterfrom
wfxr:feat/shared-lock-upgrade-next-gen

Conversation

@wfxr

@wfxr wfxr commented Jul 1, 2026

Copy link
Copy Markdown
Member

Summary

Support upgrading a pessimistic shared lock to an exclusive lock when the caller explicitly opts in through LockCtx.AllowSharedLockUpgrade.

This is needed for TiDB foreign-key checking with tidb_foreign_key_check_in_shared_lock enabled. In that flow, a transaction may first acquire a shared lock while checking the parent row and then update the same parent row later in the same transaction.

Related to pingcap/tidb#68815.

Changes

  • Add LockCtx.AllowSharedLockUpgrade to gate shared-to-exclusive lock upgrades explicitly.
  • Detect keys already locked in shared mode and send upgrade requests separately from normal exclusive-lock requests.
  • Promote local lock flags only after a successful upgrade.
  • Preserve the existing shared-lock state when TiKV returns deterministic upgrade conflicts or validation errors.
  • Mark the transaction result as undetermined when an upgrade failure leaves the remote lock state uncertain, preventing later lock or commit attempts from continuing with divergent local/remote lock state.
  • Add typed handling for LockUpgradeConflict errors and API v2 key decoding support.
  • Reject shared-lock upgrade while aggressive/fair locking mode is active.

Dependency / TODO

This draft currently depends on kvproto support for kvrpcpb.LockUpgradeConflict:

Temporary TODO before marking this PR ready:

  • Wait for feat(kvrpcpb): add lock upgrade conflict error pingcap/kvproto#1495 to merge.
  • Update github.com/pingcap/kvproto in go.mod to the merged commit that contains LockUpgradeConflict.
  • Remove the temporary go.work / go.work.sum workspace files after the dependency is updated.
  • Rerun the related client-go tests without the local kvproto workspace.

Testing

Attempted:

go test ./error ./internal/apicodec ./txnkv/transaction

Blocked before running tests because the temporary go.work declares go 1.25.9 while go.mod requires go 1.25.10.

Also attempted:

GOWORK=off GOCACHE=/tmp/codex-go-cache go test ./error ./internal/apicodec ./txnkv/transaction

This confirmed the branch currently depends on the pending kvproto change and fails to compile without it:

undefined: kvrpcpb.LockUpgradeConflict
keyErr.LockUpgradeConflict undefined

wfxr added 7 commits July 1, 2026 11:59
Add a typed ErrLockUpgradeConflict, decode it from KeyError including API v2 key decoding, treat it as a known shared-lock upgrade failure, and keep the local go.work wiring committed so Task 5 remains buildable against the local kvproto update during rollout sequencing.
Rename the shared-lock-upgrade helper to describe the
undetermined-result case directly and invert its return value so the
upgrade error branch reads in positive form without changing behavior.
For non-upgrade pessimistic lock groups, the rollback path always uses the same keys that were locked. The shared-lock-upgrade path returns before any pessimistic rollback, so the extra parameter was unused there and can be removed without changing behavior.
Move the temporary lock key into the LockOnlyIfExists validation branch and rename it to lockKey so the variable name matches its diagnostic-only role.
@ti-chi-bot

ti-chi-bot Bot commented Jul 1, 2026

Copy link
Copy Markdown

Skipping CI for Draft Pull Request.
If you want CI signal for your change, please convert it to an actual PR.
You can still manually trigger a test run with /test all

@ti-chi-bot ti-chi-bot Bot added the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Jul 1, 2026
@ti-chi-bot

ti-chi-bot Bot commented Jul 1, 2026

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:
Once this PR has been reviewed and has the lgtm label, please assign ekexium for approval. For more information see the Code Review Process.
Please ensure that each of them provides their approval before proceeding.

The full list of commands accepted by this bot can be found 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 added the dco-signoff: yes Indicates the PR's author has signed the dco. label Jul 1, 2026
@coderabbitai

coderabbitai Bot commented Jul 1, 2026

Copy link
Copy Markdown

Important

Review skipped

Draft detected.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 34c55f63-65a4-4c2c-a62c-a37dbc18ac7a

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@ti-chi-bot ti-chi-bot Bot added the size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. label Jul 1, 2026
@wfxr wfxr changed the title txnkv: support shared lock upgrades for foreign key checks txnkv: support shared lock upgrades Jul 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

dco-signoff: yes Indicates the PR's author has signed the dco. do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. 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.

1 participant