Skip to content

txnkv, tikv: support Classic shared-read primaries, GC commit checks, and lock pagination - #2184

Draft
ekexium wants to merge 10 commits into
tikv:masterfrom
ekexium:sfs/client-go-shared-read-coordinator
Draft

ekexium wants to merge 10 commits into
tikv:masterfrom
ekexium:sfs/client-go-shared-read-coordinator

Conversation

@ekexium

@ekexium ekexium commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Note

Wave 2 Draft PR — depends on pingcap/kvproto#1594 and TiKV shared-lock support (tikv/tikv#20423..#20425) landing in pingcap/tikv:nightly for integration-tikv CI.

  • Before marking Ready: update go.mod, go.sum, and integration_tests/go.mod to the merged pingcap/kvproto#1594 commit SHA.

Problem

Tracking: pingcap/tidb#73623, tikv/tikv#20422

Supporting native SELECT ... FOR SHARE in Classic TiDB requires the transaction coordinator to manage shared primary locks, explicit S→X upgrades, deadlock-victim aborts, commit-time GC safety checks for missing-lock recovery, data-lock pagination past dense shared-lock wrappers, and pipelined write compatibility.

Changes

  • Shared primary coordination & explicit upgrades: Allow pessimistic transactions with shared reads to select a shared primary and stay on ordinary 2PC for the remainder of the transaction. Carry is_shared_lock_upgrade on upgrades, abort shared-read transactions chosen as deadlock victims, and charge elapsed retry time against the caller's NOWAIT / lock-wait budget when skipping live holders.
  • Opt-in missing-lock amendment with commit-time GC verification: Set allow_shared_lock_amend on prewrite requests for shared-read transactions. After all prewrites succeed and before committing the primary, query the authoritative transaction and data GC safe points from PD/storage and reject commit if startTS < txnSafePoint or startTS <= dataSafePoint (inclusive on the data boundary because TiKV data GC can reclaim a protected rollback written exactly at startTS; paired with pingcap/tidb#73624). Legacy callers leave the flag unset.
  • Data-lock checkpoint pagination (ResolveDataLocksForRange): Add ResolveDataLocksForRange for checkpoint repair callers (such as BR) to skip lock-only entries (Op_SharedLock, Op_Lock, Op_PessimisticLock) and advance past full pages of shared holders, while keeping ResolveLocksForRange unchanged so GC continues resolving every lock.
  • Pipelined flush compatibility: Expand shared-lock containers into individual holders during pipelined DML flush lock resolution so concurrent writes properly wait for all remaining shared readers.
  • Tests: Add unit and integration coverage for shared-primary replay, missing-lock recovery, GC safe-point boundaries (including equality and failure paths), pagination, pipelined/1PC/async-commit/2PC writers against shared holders, and scope integration test cleanup to each test's key range.

Planned for backport to tidb-8.5 once merged.

Validation

  • go test -tags=intest -count=1 ./tikv ./txnkv/transaction
  • go test -race -tags=intest -count=1 ./txnkv/transaction -run 'Test.*Shared'
  • End-to-end validation with real TiKV and GC safe-point advancement completed on the full local stack (including all 4 write protocols and missing-lock GC boundary tests).

Use the number of scanned holders when advancing pages. A live shared wrapper can occupy a whole page, so move past its physical key to reach later data locks. GC still resolves every holder and rescans the last key inclusively.

Signed-off-by: Ziqian Qin <eke@fastmail.com>
Opt in pessimistic shared reads to selecting a fixed shared primary and sending an explicit upgrade request. Keep ordinary 2PC and normal locking for the remainder of the transaction, even when all S holders subsequently upgrade or the session option changes. Legacy shared-secondary callers retain their previous behavior.

Signed-off-by: Ziqian Qin <eke@fastmail.com>
Remember the deadlock before returning it to SQL and skip statement retry. This also applies when the victim was waiting for a new exclusive key. Preserve the existing deadlock diagnostic and leave rollback available.

Signed-off-by: Ziqian Qin <eke@fastmail.com>
Check the authoritative GC state after all prewrites have been acknowledged and before issuing the first primary commit. A fresh local visibility cache may still predate collection of a lost shared lock’s conflict record. Check both transaction and data safe points, including the old PD/etcd representation, and propagate lookup failures before commit.

Signed-off-by: Ziqian Qin <eke@fastmail.com>
An upgrader does not need to resolve its own S or recently touched peers. Still return NOWAIT or an expired timeout in that path, instead of reissuing requests beyond the statement budget.

Signed-off-by: Ziqian Qin <eke@fastmail.com>
Use the transaction lifetime capability on every prewrite batch and retry. Existing shared-secondary clients leave the bit unset and keep strict missing-lock failure semantics.

Signed-off-by: Ziqian Qin <eke@fastmail.com>
Discard successful RC and upgrade replies and drive actual RPC retries against Classic TiKV. Separately remove a shared holder, commit an intervening lock-only X, and verify rejection on both the prewrite and explicit-upgrade paths.

Signed-off-by: Ziqian Qin <eke@fastmail.com>
A start timestamp equal to the transaction safe point remains readable when the data safe point is older. Data GC can collect the primary’s protected rollback at equality, so preserve the stricter check on the data safe point.

Signed-off-by: Ziqian Qin <eke@fastmail.com>
Shared lock cases only own the memcomparable keys under their test prefix. The whole suite should leave unrelated SQL rows and database metadata untouched when running against an isolated Classic cluster.

Signed-off-by: Ziqian Qin <eke@fastmail.com>
A shared lock conflict describes several owners in one Lock CF value. Feeding the envelope to ResolveLocks fails immediately and aborts a normal pipelined writer. Expand the members before applying optimism checks, logging and resolution. Exercise real 2PC, one-phase, async and flush requests while two independent shared readers release in order.

Signed-off-by: Ziqian Qin <eke@fastmail.com>
@ti-chi-bot

ti-chi-bot Bot commented Nov 23, 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 do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. dco-signoff: yes Indicates the PR's author has signed the dco. labels Nov 23, 2026
@coderabbitai

coderabbitai Bot commented Nov 23, 2026

Copy link
Copy Markdown

Important

Draft PR not reviewed

Draft PRs are not automatically reviewed by default.

  • Trigger a manual review

To automatically review draft PRs, update your CodeRabbit configuration:

reviews:
  auto_review:
    drafts: true
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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

@ti-chi-bot

ti-chi-bot Bot commented Nov 23, 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 myonkeminta 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

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