Skip to content

WIP: *: batching analyze requests - #69686

Open
0xPoe wants to merge 9 commits into
pingcap:masterfrom
0xPoe:poe-batch-analyze
Open

WIP: *: batching analyze requests#69686
0xPoe wants to merge 9 commits into
pingcap:masterfrom
0xPoe:poe-batch-analyze

Conversation

@0xPoe

@0xPoe 0xPoe commented Jul 6, 2026

Copy link
Copy Markdown
Member

What problem does this PR solve?

Issue Number: ref #67449

Problem Summary:

Full-sampling ANALYZE currently sends and receives one coprocessor result per region, causing avoidable RPC and response-merging overhead.

What changed and how does it work?

  • Prepare V2 full-sampling ANALYZE to use tidb_store_batch_size without row-count hints once large-table NDV sampling explicitly opts in, while preserving Analyze scan concurrency; executor: remove unnecessary ordering from full-sampling Analyze requests #70275 removed the obsolete KeepOrder requirement.
  • Negotiate TiKV-side result merging with allow_batch_task_data_merge and explicit data_merged_into_response acknowledgements.
  • Retry every batched task without an explicit response or acknowledgement, preserving mixed-version compatibility.

Depends on #70275, pingcap/kvproto#1497, and tikv/tikv#19854.

Check List

Tests

  • Unit test
  • Integration test (TiKV: batched full-sampling ANALYZE through the real endpoint)
  • Manual test (20k rows, 8 regions, 1 store; batched and unbatched exact statistics matched)
  • No need to test
    • I checked and no code files have been changed.

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

Please refer to Release Notes Language Style Guide to write a quality release note.

None

@ti-chi-bot ti-chi-bot Bot added do-not-merge/needs-linked-issue release-note-none Denotes a PR that doesn't merit a release note. size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Jul 6, 2026
@0xPoe 0xPoe changed the title *: batching analyze requests WIP: *: batching analyze requests Jul 6, 2026
@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 6, 2026
@coderabbitai

coderabbitai Bot commented Jul 6, 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
📝 Walkthrough

Walkthrough

This PR enables store-batched coprocessor execution for Analyze requests by wiring StoreBatchSize into request construction, updating task and response handling, adjusting kvproto replacements, and adding batching and acknowledgement tests.

Changes

Analyze Request Store Batching

Layer / File(s) Summary
RequestBuilder setter and Analyze wiring
pkg/distsql/request_builder.go, pkg/executor/analyze_col.go
Adds SetStoreBatchSize and applies the session store batch size to Analyze requests while leaving KeepOrder unset.
Coprocessor Analyze batching and response handling
pkg/store/copr/coprocessor.go, DEPS.bzl, go.mod
Allows Analyze requests to use store batching, applies Analyze-specific task checks, handles merged responses, and updates kvproto replacement wiring.
Batching and acknowledgement validation
pkg/store/copr/copr_test/coprocessor_test.go, pkg/store/copr/coprocessor_ack_test.go
Verifies Analyze task grouping, merged acknowledgements, and redispatch of unanswered Analyze and DAG tasks.

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

Sequence Diagram(s)

sequenceDiagram
  participant AnalyzeColumnsExec
  participant RequestBuilder
  participant Coprocessor
  participant TiKV
  AnalyzeColumnsExec->>RequestBuilder: SetStoreBatchSize(SessionVars.StoreBatchSize)
  RequestBuilder->>Coprocessor: Build Analyze request
  Coprocessor->>Coprocessor: Group Analyze tasks for store batching
  Coprocessor->>TiKV: Send batched Analyze request
  TiKV-->>Coprocessor: Return merged acknowledgement or no task response
  Coprocessor-->>AnalyzeColumnsExec: Complete merged tasks or redispatch unanswered tasks
Loading

Possibly related PRs

  • pingcap/tidb#67676: Both affect Analyze request ordering and store-batched coprocessor behavior.
  • pingcap/tidb#69085: Both modify store-batched coprocessor task and response handling.

Suggested reviewers: cfzjywxk, wjhuang2016, zyguan

Poem

A rabbit hops through cop task queues,
Analyze batches bring good news.
Merged replies and retries align,
Three small tasks march in time.
Hop, hop, hooray—the tests agree! 🐇

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
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.
Title check ✅ Passed The title clearly identifies the main change: batching ANALYZE requests.
Description check ✅ Passed The description covers the issue, implementation, tests, side effects, documentation, dependencies, and release note.
✨ 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.

@codecov

codecov Bot commented Jul 6, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 76.3254%. Comparing base (5e1901b) to head (820f8d1).

Additional details and impacted files
@@               Coverage Diff                @@
##             master     #69686        +/-   ##
================================================
- Coverage   76.3256%   76.3254%   -0.0003%     
================================================
  Files          2041       2041                
  Lines        558170     558161         -9     
================================================
- Hits         426027     426019         -8     
+ Misses       131243     131242         -1     
  Partials        900        900                
Components Coverage Δ
dumpling 59.8974% <ø> (ø)
parser ∅ <ø> (∅)
br 62.7090% <ø> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@ti-chi-bot ti-chi-bot Bot added size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. do-not-merge/needs-tests-checked and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. do-not-merge/needs-linked-issue labels Jul 14, 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.

Actionable comments posted: 1

🤖 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.

Inline comments:
In `@pkg/store/copr/coprocessor_ack_test.go`:
- Around line 94-103: Extend the test around the merged task response to assert
that the worker’s collected execution metrics include the batched response’s
ScanDetail values, specifically ProcessedVersions of 7. Use the existing
worker.stats.ScanDetail or corresponding metrics container, ensuring the
assertion verifies metrics come from the batch ExecDetailsV2 rather than the
main response.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 659b7034-dd42-46e1-9c6b-fd140d8150e3

📥 Commits

Reviewing files that changed from the base of the PR and between 602f946 and d5904a1.

⛔ Files ignored due to path filters (1)
  • go.sum is excluded by !**/*.sum
📒 Files selected for processing (5)
  • DEPS.bzl
  • go.mod
  • pkg/executor/analyze_col.go
  • pkg/store/copr/coprocessor.go
  • pkg/store/copr/coprocessor_ack_test.go
🚧 Files skipped from review as they are similar to previous changes (3)
  • go.mod
  • pkg/executor/analyze_col.go
  • DEPS.bzl

Comment thread pkg/store/copr/coprocessor_ack_test.go Outdated
@0xPoe
0xPoe force-pushed the poe-batch-analyze branch from bb70719 to 8169233 Compare July 20, 2026 08:35
@0xPoe
0xPoe force-pushed the poe-batch-analyze branch 2 times, most recently from 6d5651b to 763e499 Compare July 23, 2026 09:10
@ti-chi-bot ti-chi-bot Bot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. labels Jul 23, 2026
@0xPoe
0xPoe force-pushed the poe-batch-analyze branch from 763e499 to ecba620 Compare July 23, 2026 10:29
@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 23, 2026
@0xPoe
0xPoe force-pushed the poe-batch-analyze branch from f75bea4 to af66b65 Compare July 23, 2026 23:21
@ti-chi-bot ti-chi-bot Bot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. labels Jul 23, 2026
@0xPoe
0xPoe force-pushed the poe-batch-analyze branch from af66b65 to 0a16f89 Compare August 3, 2026 10:05
@ti-chi-bot ti-chi-bot Bot added the size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. label Aug 10, 2026
@0xPoe
0xPoe force-pushed the poe-batch-analyze branch 10 times, most recently from aa750cf to 47435ea Compare August 13, 2026 13:19
@ti-chi-bot ti-chi-bot Bot removed the size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. label Aug 14, 2026
@ti-chi-bot

ti-chi-bot Bot commented Aug 14, 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 bb7133, terry1purcell, xuhuaiyu 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 size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. component/statistics sig/planner SIG: Planner labels Aug 14, 2026
@0xPoe
0xPoe force-pushed the poe-batch-analyze branch 4 times, most recently from 30c611a to 72cf2a8 Compare August 17, 2026 09:58
0xPoe added 9 commits August 17, 2026 15:12
handleBatchCopResponse passed the parent response's lock to
handleLockErr, but that field is always nil in the batched-response
loop, so a child's lock was never resolved and the task retried
without backoff. Pass the child's lock instead.

Signed-off-by: 0xPoe <[email protected]>
Record that an internal Analyze request without row-count hints stays on
the flat builder until its caller opts into merged child responses. This
gives the later batching change a clear before-and-after test.

Signed-off-by: 0xPoe <[email protected]>
Record that a batched child without a task response is retried and
counted as a fallback. This is the compatibility baseline for stores
that do not return merge acknowledgements.

Signed-off-by: 0xPoe <[email protected]>
Route every append of unconsumed coprocessor runtime stats through one
nil-safe helper that holds the stats lock. This lets merged child
responses retain execution details without duplicating synchronization.

Signed-off-by: 0xPoe <[email protected]>
Use the temporary 0xPoe/kvproto revision that adds the
allow_batch_task_data_merge request capability, the per-task
data_merged_into_response acknowledgement, and the
execute_batch_tasks_serially execution control.

Return to pingcap/kvproto after pingcap/kvproto#1497 merges.

Signed-off-by: 0xPoe <[email protected]>
Add an explicit opt-in for store batching when a request is internal,
non-DAG, or lacks row-count hints. Callers must bound response size and
accept both merged and per-task response shapes.

Send the capability to TiKV. Treat merge acknowledgements as completed
children, retain each child's execution details, and retry children with
no response or acknowledgement for mixed-version compatibility.

Signed-off-by: 0xPoe <[email protected]>
Record that client-go's empty synthetic EpochNotMatch flattens a store
batch after Region cache invalidation. The next commit changes this
retry shape while preserving the same ranges.

Signed-off-by: 0xPoe <[email protected]>
Recognize client-go's empty synthetic EpochNotMatch only when no RPC
context or task result exists. In that case, relocate and regroup the
whole batch without changing the iterator's shared request.

Keep RPC-backed and partial responses on the existing flat
reconciliation path.

Signed-off-by: 0xPoe <[email protected]>
Add request-builder controls for same-store batching, merged child responses, and serial child-task execution, then use them for full-sampling column Analyze without changing its scan concurrency.

Introduce tidb_analyze_store_batch_size as an independent global and session variable. Default it to four, let zero disable batching, and refresh the global value before Auto Analyze runs on a pooled internal session.

Signed-off-by: 0xPoe <[email protected]>
@ti-chi-bot

ti-chi-bot Bot commented Aug 17, 2026

Copy link
Copy Markdown

@0xPoe: The following tests failed, say /retest to rerun all failed tests or /retest-required to rerun all mandatory failed tests:

Test name Commit Details Required Rerun command
pull-br-integration-test 9a82b7d link true /test pull-br-integration-test
pull-unit-test-ddlv1 9a82b7d link true /test pull-unit-test-ddlv1
pull-build-next-gen 5465d5d link true /test pull-build-next-gen
idc-jenkins-ci-tidb/build 820f8d1 link true /test build

Full PR test history. Your PR dashboard.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository. I understand the commands that are listed here.

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

Labels

component/statistics do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. release-note-none Denotes a PR that doesn't merit a release note. sig/planner SIG: Planner 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