Skip to content

Support bypassing resolved locks for read - #249

Open
windtalker wants to merge 4 commits into
tikv:masterfrom
windtalker:add_try_bypass_lock
Open

Support bypassing resolved locks for read#249
windtalker wants to merge 4 commits into
tikv:masterfrom
windtalker:add_try_bypass_lock

Conversation

@windtalker

@windtalker windtalker commented Jul 22, 2026

Copy link
Copy Markdown
Contributor

What problem does this PR solve?

This PR adds a best-effort path for read requests to bypass locks whose transaction status has already been determined, reducing repeated lock handling in read paths.

What is changed and how it works?

  • Add a LockResolver helper to collect transaction IDs that can be bypassed by reads.
  • Schedule committed or rolled-back locks for bounded background resolution instead of always resolving them synchronously on the caller thread.
  • Stop the background resolve worker during Cluster shutdown and avoid continuing to drain swapped pending work after shutdown starts.

Summary by CodeRabbit

  • New Features

    • Added background lock resolution to process eligible locks asynchronously.
    • Read operations can bypass certain locks when safe, reducing unnecessary waiting.
    • Added safeguards to limit queued background lock-resolution work.
  • Bug Fixes

    • Ensured background lock-resolution tasks stop cleanly during cluster shutdown.

Signed-off-by: xufei <xufeixw@mail.ustc.edu.cn>
Signed-off-by: xufei <xufeixw@mail.ustc.edu.cn>
@ti-chi-bot ti-chi-bot Bot added dco-signoff: yes Indicates the PR's author has signed the dco. contribution This PR is from a community contributor. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Jul 22, 2026
@coderabbitai

coderabbitai Bot commented Jul 22, 2026

Copy link
Copy Markdown

Review Change Stack

Warning

Review limit reached

@windtalker, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 40 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 0be1e500-70f7-4f1d-91ca-5e0345df7072

📥 Commits

Reviewing files that changed from the base of the PR and between 4aa7ef3 and 584b1ff.

📒 Files selected for processing (2)
  • include/pingcap/kv/LockResolver.h
  • src/kv/LockResolver.cc
📝 Walkthrough

Walkthrough

LockResolver now supports read-lock bypass decisions and asynchronous cleanup of eligible locks. Cluster starts the resolver worker on its thread pool and stops it during destruction. Pending work is bounded and coordinated with mutex, condition-variable, and atomic state.

Changes

Lock resolution lifecycle

Layer / File(s) Summary
Resolution contracts and state
include/pingcap/kv/Backoff.h, include/pingcap/kv/LockResolver.h
Adds the background-resolution backoff and queue limits, bypass result type, public lifecycle methods, bypass API, parameterized resolution declaration, and synchronization state.
Bypass decisions and background processing
src/kv/LockResolver.cc
Adds bypass evaluation, queues eligible locks for asynchronous cleanup, routes resolution through resolveLocksImpl, updates pushed-lock handling, and processes pending batches in the background worker.
Cluster worker lifecycle
src/kv/Cluster.cc, include/pingcap/kv/Cluster.h
Schedules backgroundResolve() on the thread pool and stops the lock resolver during Cluster destruction.

Estimated code review effort: 4 (Complex) | ~45 minutes

Possibly related PRs

  • tikv/client-c#244: Extends LockResolver read-path bypass logic for locks outside the caller’s read timestamp.

Suggested reviewers: gengliqi

Sequence Diagram(s)

sequenceDiagram
  participant Cluster
  participant ThreadPool
  participant LockResolver
  participant resolveLocksImpl
  Cluster->>ThreadPool: enqueue backgroundResolve()
  LockResolver->>LockResolver: inspect locks and queue eligible cleanup
  ThreadPool->>LockResolver: run backgroundResolve()
  LockResolver->>resolveLocksImpl: resolve queued lock batches
  Cluster->>LockResolver: stopBgResolve() during destruction
Loading
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly matches the main change: adding a read path that bypasses already-resolved locks.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ 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.

Signed-off-by: xufei <xufeixw@mail.ustc.edu.cn>
Signed-off-by: xufei <xufeixw@mail.ustc.edu.cn>

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
include/pingcap/kv/Cluster.h (1)

65-74: 🩺 Stability & Availability | 🔵 Trivial | ⚡ Quick win

Consider stopping the background resolver before its dependencies during shutdown.

lock_resolver->stopBgResolve() runs after rpc_client->stop() and region_cache->stop(), but backgroundResolve() depends on both (via resolveLocksImpl/RegionClient). If the worker is mid-batch when the destructor starts, it may keep issuing RPCs/region lookups against already-stopping components until it next observes stopped — caught by backgroundResolve's try/catch, but potentially producing spurious warnings and adding shutdown latency bounded by bgResolveLockMaxBackoff (5s). Moving stopBgResolve() earlier (before rpc_client->stop()/region_cache->stop()) lets the worker exit promptly.

♻️ Suggested reordering
     ~Cluster()
     {
-        rpc_client->stop();
-        mpp_prober->stop();
-        if (region_cache)
-            region_cache->stop();
         if (lock_resolver)
             lock_resolver->stopBgResolve();
+        rpc_client->stop();
+        mpp_prober->stop();
+        if (region_cache)
+            region_cache->stop();
         thread_pool->stop();
     }
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@include/pingcap/kv/Cluster.h` around lines 65 - 74, Reorder the Cluster
destructor shutdown sequence so lock_resolver->stopBgResolve() runs before
rpc_client->stop() and region_cache->stop(). Keep the existing conditional
checks and stop the thread_pool afterward, ensuring the background resolver
exits before its RPC and region-cache dependencies are stopped.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@include/pingcap/kv/Cluster.h`:
- Around line 65-74: Reorder the Cluster destructor shutdown sequence so
lock_resolver->stopBgResolve() runs before rpc_client->stop() and
region_cache->stop(). Keep the existing conditional checks and stop the
thread_pool afterward, ensuring the background resolver exits before its RPC and
region-cache dependencies are stopped.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 8ea8fd8c-019e-4747-8041-dae48440094b

📥 Commits

Reviewing files that changed from the base of the PR and between 78a557e and 4aa7ef3.

📒 Files selected for processing (5)
  • include/pingcap/kv/Backoff.h
  • include/pingcap/kv/Cluster.h
  • include/pingcap/kv/LockResolver.h
  • src/kv/Cluster.cc
  • src/kv/LockResolver.cc

@ti-chi-bot

ti-chi-bot Bot commented Jul 24, 2026

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: gengliqi

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 added the needs-1-more-lgtm Indicates a PR needs 1 more LGTM. label Jul 24, 2026
@ti-chi-bot

ti-chi-bot Bot commented Jul 24, 2026

Copy link
Copy Markdown

[LGTM Timeline notifier]

Timeline:

  • 2026-07-24 10:02:32.873786111 +0000 UTC m=+1571938.909881167: ☑️ agreed by gengliqi.

@ti-chi-bot ti-chi-bot Bot added the approved label Jul 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approved contribution This PR is from a community contributor. dco-signoff: yes Indicates the PR's author has signed the dco. needs-1-more-lgtm Indicates a PR needs 1 more LGTM. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants