Skip to content

Optimize lock handling in MPP local read#10992

Merged
ti-chi-bot[bot] merged 5 commits into
pingcap:feature/release-8.5-materialized-viewfrom
windtalker:optimize_lock_handling_in_mpp
Jul 21, 2026
Merged

Optimize lock handling in MPP local read#10992
ti-chi-bot[bot] merged 5 commits into
pingcap:feature/release-8.5-materialized-viewfrom
windtalker:optimize_lock_handling_in_mpp

Conversation

@windtalker

@windtalker windtalker commented Jul 20, 2026

Copy link
Copy Markdown
Contributor

What problem does this PR solve?

Issue Number: N/A

Problem Summary:

Optimize TiFlash MPP/batch cop local learner read lock handling to reduce unnecessary remote reads when local LockCF locks can be handled by a local retry.

What is changed and how it works?

Optimize lock handling in MPP local read
  • Add a best-effort local retry path for lock regions hit after local wait-index succeeds.
  • Keep read-index locks as unavailable regions because current read-index requests cannot carry bypass lock information to the leader-side memory lock check.
  • Retry local learner read with collected bypass lock information when the lock can be safely bypassed.
  • Wait briefly before retrying only when the lock needs to be physically resolved first.

Check List

Tests

  • Unit test
  • Integration test
  • Manual test (add detailed scripts or steps below)
  • No code

Built affected objects locally:

  • dbms/src/Flash/CMakeFiles/flash_service.dir/Coprocessor/DAGStorageInterpreter.cpp.o
  • dbms/src/Storages/KVStore/CMakeFiles/kvstore.dir/Read/LearnerReadWorker.cpp.o

Ran learner read unit tests locally:

  • /Users/feixu/dev/pingcap/tiflash/cmake-build-debug/dbms/gtests_dbms --gtest_filter=LearnerReadTest.*

Side effects

  • Performance regression: Consumes more CPU
  • Performance regression: Consumes more Memory
  • Breaking backward compatibility

Documentation

  • Affects user behaviors
  • Contains syntax changes
  • Contains variable changes
  • Contains experimental features
  • Changes MySQL compatibility

Release note

None

Summary by CodeRabbit

  • Bug Fixes
    • Improved learner read error handling by separating unavailable regions from lock-related regions and ensuring lock details are included in thrown region exceptions for more accurate retry decisions.
    • Enhanced batch/cop learner read retries with a bypass-lock-based local retry path (with remote fallback when bypass locks aren’t applicable).
  • Tests
    • Added/extended learner read tests to validate read-index lock handling and local bypass-lock retry classification.
  • Chores
    • Updated the bundled client C component to a newer revision.

@ti-chi-bot ti-chi-bot Bot added release-note-none Denotes a PR that doesn't merit a release note. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Jul 20, 2026
@coderabbitai

coderabbitai Bot commented Jul 20, 2026

Copy link
Copy Markdown

Review Change Stack

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 00505c24-abd7-4f1f-82f5-59d6270c6ca6

📥 Commits

Reviewing files that changed from the base of the PR and between af90ca7 and 593e1fc.

📒 Files selected for processing (12)
  • contrib/client-c
  • dbms/src/Flash/BatchCoprocessorHandler.cpp
  • dbms/src/Flash/Coprocessor/DAGContext.h
  • dbms/src/Flash/Coprocessor/DAGStorageInterpreter.cpp
  • dbms/src/Flash/Coprocessor/TablesRegionsInfo.cpp
  • dbms/src/Flash/Coprocessor/TablesRegionsInfo.h
  • dbms/src/Flash/Disaggregated/WNEstablishDisaggTaskHandler.cpp
  • dbms/src/Flash/Mpp/MPPTask.cpp
  • dbms/src/Storages/KVStore/Read/LearnerReadWorker.cpp
  • dbms/src/Storages/KVStore/Read/LearnerReadWorker.h
  • dbms/src/Storages/KVStore/Read/RegionException.h
  • dbms/src/Storages/KVStore/tests/gtest_learner_read.cpp
🚧 Files skipped from review as they are similar to previous changes (11)
  • contrib/client-c
  • dbms/src/Flash/Disaggregated/WNEstablishDisaggTaskHandler.cpp
  • dbms/src/Storages/KVStore/Read/RegionException.h
  • dbms/src/Flash/BatchCoprocessorHandler.cpp
  • dbms/src/Flash/Coprocessor/TablesRegionsInfo.h
  • dbms/src/Flash/Mpp/MPPTask.cpp
  • dbms/src/Flash/Coprocessor/TablesRegionsInfo.cpp
  • dbms/src/Flash/Coprocessor/DAGContext.h
  • dbms/src/Storages/KVStore/Read/LearnerReadWorker.h
  • dbms/src/Storages/KVStore/tests/gtest_learner_read.cpp
  • dbms/src/Flash/Coprocessor/DAGStorageInterpreter.cpp

📝 Walkthrough

Walkthrough

The change adds lock-aware learner-read exceptions, propagates bypass-lock timestamps through DAG contexts, and enables one local lock-bypass retry before remote fallback. It also adds learner-read tests and advances the contrib/client-c submodule pointer.

Changes

Bypass-lock learner retry

Layer / File(s) Summary
Lock-aware learner exceptions
dbms/src/Storages/KVStore/Read/LearnerReadWorker.*, dbms/src/Storages/KVStore/Read/RegionException.h
Learner read failures separate unavailable and lock regions, retain lock details, and pass both sets through RegionException.
Bypass timestamp propagation
dbms/src/Flash/Coprocessor/TablesRegionsInfo.*, dbms/src/Flash/Coprocessor/DAGContext.h, dbms/src/Flash/BatchCoprocessorHandler.cpp, dbms/src/Flash/Mpp/MPPTask.cpp, dbms/src/Flash/Disaggregated/WNEstablishDisaggTaskHandler.cpp
Region metadata receives bypass-lock timestamps, which are transferred into DAG contexts across execution paths.
Local bypass-lock retry
dbms/src/Flash/Coprocessor/DAGStorageInterpreter.cpp
Learner reads attempt one lock-resolver-based local retry before falling back to remote reads.
Learner retry validation
dbms/src/Storages/KVStore/tests/gtest_learner_read.cpp
Tests verify unavailable-region and local-lock exception metadata.

Client submodule revision

Layer / File(s) Summary
Client submodule pointer
contrib/client-c
The submodule reference advances to commit 4f031654.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Sequence Diagram(s)

sequenceDiagram
  participant LearnerReadWorker
  participant RegionException
  participant DAGStorageInterpreter
  participant LockResolver
  participant DAGContext
  LearnerReadWorker->>RegionException: carry lock regions and lock details
  RegionException->>DAGStorageInterpreter: report lock_region
  DAGStorageInterpreter->>LockResolver: tryGetBypassLock
  LockResolver-->>DAGStorageInterpreter: return bypass timestamps
  DAGStorageInterpreter->>DAGContext: store bypass timestamps
  DAGStorageInterpreter-->>LearnerReadWorker: retry locally or remotely
Loading

Possibly related issues

Suggested reviewers: jayson-huang, lloyd-pottiger, yongman

Poem

I’m a rabbit with locks in my paws,
Bypassing waits with clever new laws.
Timestamps hop through the DAG,
Local retries chase locks away—
Remote paths stand by, just because.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 18.75% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title matches the main change: improved local read lock handling, though it omits the batch cop aspect.
Description check ✅ Passed The description follows the template closely and includes the problem, change summary, tests, side effects, and release note sections.
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.

@ti-chi-bot ti-chi-bot Bot added size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. and removed size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Jul 20, 2026

@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)
dbms/src/Storages/KVStore/tests/gtest_learner_read.cpp (1)

44-55: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Use camelCase for variable names to adhere to coding guidelines.

The coding guidelines explicitly state that "Method and variable names should use camelCase". However, several new variables in these code segments use snake_case. While the surrounding test file already heavily uses snake_case (e.g., region_id_200), please consider adopting the mandated convention for newly introduced variables to progressively improve compliance.

  • dbms/src/Storages/KVStore/tests/gtest_learner_read.cpp#L44-L55: Rename variables such as lock_version and lock_info to lockVersion and lockInfo.
  • dbms/src/Storages/KVStore/tests/gtest_learner_read.cpp#L196-L256: Rename variables such as region_id, lock_version, mvcc_query_info, read_index_result, resp, and unavailable_regions to their camelCase equivalents (e.g., regionId, lockVersion, etc.).
🤖 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 `@dbms/src/Storages/KVStore/tests/gtest_learner_read.cpp` around lines 44 - 55,
Rename the newly introduced snake_case variables to camelCase throughout
dbms/src/Storages/KVStore/tests/gtest_learner_read.cpp: lines 44-55 update
lock_version and lock_info, and lines 196-256 update region_id, lock_version,
mvcc_query_info, read_index_result, resp, and unavailable_regions, including all
references while preserving behavior.

Source: Coding guidelines

🤖 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 `@dbms/src/Storages/KVStore/tests/gtest_learner_read.cpp`:
- Around line 44-55: Rename the newly introduced snake_case variables to
camelCase throughout dbms/src/Storages/KVStore/tests/gtest_learner_read.cpp:
lines 44-55 update lock_version and lock_info, and lines 196-256 update
region_id, lock_version, mvcc_query_info, read_index_result, resp, and
unavailable_regions, including all references while preserving behavior.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: cea8fba9-8e33-474a-b859-23ef7f6db22e

📥 Commits

Reviewing files that changed from the base of the PR and between 4a15b4e and f3c54c2.

📒 Files selected for processing (1)
  • dbms/src/Storages/KVStore/tests/gtest_learner_read.cpp

Signed-off-by: xufei <xufeixw@mail.ustc.edu.cn>
Signed-off-by: xufei <xufeixw@mail.ustc.edu.cn>
Signed-off-by: xufei <xufeixw@mail.ustc.edu.cn>
Signed-off-by: xufei <xufeixw@mail.ustc.edu.cn>
@windtalker
windtalker force-pushed the optimize_lock_handling_in_mpp branch from af90ca7 to 593e1fc Compare July 21, 2026 08:53
@ti-chi-bot ti-chi-bot Bot added approved needs-1-more-lgtm Indicates a PR needs 1 more LGTM. labels Jul 21, 2026

@gengliqi gengliqi left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM

try
{
auto * cluster = tmt.getKVCluster();
if (cluster == nullptr || cluster->lock_resolver == nullptr)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
if (cluster == nullptr || cluster->lock_resolver == nullptr)
if unlikely (cluster == nullptr || cluster->lock_resolver == nullptr)

@ti-chi-bot

ti-chi-bot Bot commented Jul 21, 2026

Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: gengliqi, solotzg

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 lgtm and removed needs-1-more-lgtm Indicates a PR needs 1 more LGTM. labels Jul 21, 2026
@ti-chi-bot

ti-chi-bot Bot commented Jul 21, 2026

Copy link
Copy Markdown
Contributor

[LGTM Timeline notifier]

Timeline:

  • 2026-07-21 09:31:15.979298669 +0000 UTC m=+1310862.015393715: ☑️ agreed by solotzg.
  • 2026-07-21 10:02:18.914202436 +0000 UTC m=+1312724.950297512: ☑️ agreed by gengliqi.

@windtalker

Copy link
Copy Markdown
Contributor Author

/hold

@ti-chi-bot ti-chi-bot Bot added the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Jul 21, 2026
@ti-chi-bot
ti-chi-bot Bot merged commit 8b22d94 into pingcap:feature/release-8.5-materialized-view Jul 21, 2026
5 checks passed

@JaySon-Huang JaySon-Huang left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

We can optimize the tiflash shutdown wait timeout when there are many locks to be resolved. tikv/client-c#245 (comment)

Rest LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approved do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. lgtm release-note-none Denotes a PR that doesn't merit a release note. size/XL Denotes a PR that changes 500-999 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants