Skip to content

executor: fix RU v2 executor accounting#68578

Open
meiking wants to merge 6 commits into
pingcap:masterfrom
meiking:fix/ruv2-executor-accounting-master
Open

executor: fix RU v2 executor accounting#68578
meiking wants to merge 6 commits into
pingcap:masterfrom
meiking:fix/ruv2-executor-accounting-master

Conversation

@meiking
Copy link
Copy Markdown

@meiking meiking commented May 22, 2026

What problem does this PR solve?

Issue Number: ref #68576

Problem Summary:

RU v2 executor accounting may undercount TiDB-side compute cost for some common executors because the executor-level mapping relies on concrete type strings, and some entries no longer match the current executor package/type names. In addition, the insert-specific L5 metric should account for inserted rows multiplied by inserted column count, but the old path used affected rows, which does not match the fitted executor-l5-insert-rows model input.

What changed and how does it work?

  • Update the RU v2 executor type mapping to use the current concrete executor types for projection, sort, TopN, hash join, index lookup joins, select lock, expand, window, and merge join executors.
  • Remove stale executor type keys that no longer match current executor concrete types.
  • Record insert L5 RU v2 accounting from InsertValues as rowCount * len(insertColumns) after successful insert execution, including batched insert and insert-select paths.
  • Remove the old affected-rows based insert L5 accounting path.
  • Clarify the executor-l5-insert-rows config/metric comments to describe row-column multiplication.
  • Add tests covering concrete executor type mapping and insert RowsColMultiply accounting.
  • Update the Bazel shard count for pkg/executor/internal/exec tests.

Check List

Tests

  • Unit test
  • Integration test
  • Manual test (add detailed scripts or steps below)
  • 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.

Fix RU v2 executor accounting for common executors and insert row-column cost.

<!-- This is an auto-generated comment: release notes by coderabbit.ai -->
## Summary by CodeRabbit

* **New Features**
  * Added RU v2 summary fields for commit write keys and write size.
* **Improvements**
  * INSERT/REPLACE/UPDATE/DELETE accounting now multiplies rows by affected column count for finer RU tracking.
  * Expanded executor-type mappings for more accurate RU attribution.
  * Default RU v2 weights now include an explicit write-keys default.
* **Documentation**
  * Clarified RU v2 config comments about insert-column multiplication and tiering.
* **Tests**
  * Added tests validating the new accounting and RU v2 summary fields.

<!-- review_stack_entry_start -->

[![Review Change Stack](https://storage.googleapis.com/coderabbit_public_assets/review-stack-in-coderabbit-ui.svg)](https://app.coderabbit.ai/change-stack/pingcap/tidb/pull/68578?utm_source=github_walkthrough&utm_medium=github&utm_campaign=change_stack)

<!-- review_stack_entry_end -->
<!-- end of auto-generated comment: release notes by coderabbit.ai -->

@ti-chi-bot
Copy link
Copy Markdown

ti-chi-bot Bot commented May 22, 2026

Adding the "do-not-merge/release-note-label-needed" label because no release-note block was detected, please follow our release note process to remove it.

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.

@ti-chi-bot ti-chi-bot Bot added the do-not-merge/release-note-label-needed Indicates that a PR should not merge because it's missing one of the release note labels. label May 22, 2026
@pantheon-ai
Copy link
Copy Markdown

pantheon-ai Bot commented May 22, 2026

@meiking I've received your pull request and will start the review. I'll conduct a thorough review covering code quality, potential issues, and implementation details.

⏳ This process typically takes 10-30 minutes depending on the complexity of the changes.

ℹ️ Learn more details on Pantheon AI.

@ti-chi-bot ti-chi-bot Bot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. contribution This PR is from a community contributor. needs-ok-to-test Indicates a PR created by contributors and need ORG member send '/ok-to-test' to start testing. labels May 22, 2026
@ti-chi-bot
Copy link
Copy Markdown

ti-chi-bot Bot commented May 22, 2026

Hi @meiking. Thanks for your PR.

I'm waiting for a pingcap member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

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.

@tiprow
Copy link
Copy Markdown

tiprow Bot commented May 22, 2026

Hi @meiking. Thanks for your PR.

PRs from untrusted users cannot be marked as trusted with /ok-to-test in this repo meaning untrusted PR authors can never trigger tests themselves. Collaborators can still trigger tests on the PR using /test all.

I understand the commands that are listed here.

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.

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 22, 2026

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: 450d18e4-3566-41ae-9f35-8821563dc78b

📥 Commits

Reviewing files that changed from the base of the PR and between a6248f2 and 4ef0f7b.

📒 Files selected for processing (7)
  • pkg/config/config.go
  • pkg/config/config.toml.example
  • pkg/metrics/ru_v2.go
  • pkg/sessionctx/variable/session.go
  • pkg/sessionctx/variable/tests/session_test.go
  • pkg/util/execdetails/execdetails_test.go
  • pkg/util/execdetails/ruv2_metrics.go
🚧 Files skipped from review as they are similar to previous changes (1)
  • pkg/util/execdetails/ruv2_metrics.go

📝 Walkthrough

Walkthrough

Records RUv2 insert work as "rows × inserted-column-count", adds commit write shadow counters to RUv2, replaces legacy insert accounting with DML column-multiply helpers, wires recording into INSERT/UPDATE/DELETE/REPLACE, expands executor-type metric mappings, and updates tests and metric registration.

Changes

RUv2 Insert Metrics Refactor: Column-Count Multiplication

Layer / File(s) Summary
Metric Definition & Documentation
pkg/config/config.go, pkg/config/config.toml.example, pkg/util/execdetails/ruv2_metrics.go, pkg/metrics/ru_v2.go, pkg/metrics/metrics.go, pkg/util/execdetails/execdetails_test.go
Docs and config comments updated to describe ExecutorL5InsertRows as "insert rows multiplied by inserted column count"; RUv2 metrics extended with write_keys/write_size shadow counters and Prometheus counters registered; execdetails tests updated.
Adapter helpers and finalization
pkg/executor/adapter.go, pkg/executor/explain.go, pkg/sessionctx/stmtctx/stmtctx.go
Removed legacy idempotent recordInsertRows2Metrics and StatementContext.InsertRowsAsRUV2Recorded; added recordDMLRowsColMultiply2Metrics/recordInsertRowsColMultiply2Metrics; finalizeStatementRUV2Metrics now updates metrics from commit details.
Insert & DML Execution Instrumentation
pkg/executor/insert.go, pkg/executor/insert_common.go, pkg/executor/update.go, pkg/executor/delete.go, pkg/executor/replace.go
InsertExec/InsertValues extended to enable and track rows×column-count recording (batch & final); overflow-safe multiplication and delta recording added and invoked; UPDATE/DELETE/REPLACE flows record DML rows×column multipliers.
Executor Metric Type Mappings
pkg/executor/internal/exec/executor.go, pkg/executor/internal/exec/executor_test.go, pkg/executor/internal/exec/BUILD.bazel
Expanded ruv2ExecutorMetricByType to include more concrete executor/operator types at level 2 and added a unit test validating concrete type keys presence/absence; increased exec_test shard_count from 7 to 8.
Tests & CI
pkg/executor/adapter_test.go, pkg/executor/explain_unit_test.go, pkg/util/execdetails/execdetails_test.go, pkg/executor/internal/exec/BUILD.bazel
Added SQL-path and unit tests for rows×column-count insert metrics, updated explain/analyze tests to use the new metric helpers, added execdetails tests for commit-derived counters, and adjusted CI shard count.

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Suggested labels

size/L

🐰 Rows hop in tidy stacks,
Columns counted, no more lacks,
Metrics multiply with cheer,
Write keys and sizes now appear,
Hop, record, the rabbit's near!

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 32.00% 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
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title directly aligns with the primary change: fixing RU v2 executor accounting by updating executor type mappings and insert row-column cost calculation.
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.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

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

Warning

Tools execution failed with the following error:

Failed to run tools: 13 INTERNAL: Received RST_STREAM with code 2 (Internal server error)


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 and usage tips.

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
pkg/executor/internal/exec/executor_test.go (1)

73-100: ⚡ Quick win

Expand this mapping test to cover all newly added concrete keys.

This test catches key-drift regressions, but it currently validates only part of the updated map. Please include the remaining newly introduced entries (ExpandExec, IndexReaderExecutor, MemTableReaderExec, SelectLockExec, TableReaderExecutor, UnionScanExec, WindowExec, etc.) so future renames fail fast.

Proposed test expansion
 func TestRUV2ExecutorMetricByTypeIncludesConcreteExecutorTypes(t *testing.T) {
 	cases := map[string]ruv2ExecutorMetric{
+		"*executor.ExpandExec":          {level: 2, label: "ExpandExec", useCells: false},
+		"*executor.IndexLookUpExecutor": {level: 2, label: "IndexLookUpExecutor", useCells: false},
+		"*executor.IndexReaderExecutor": {level: 2, label: "IndexReaderExecutor", useCells: false},
+		"*executor.MemTableReaderExec":  {level: 2, label: "MemTableReaderExec", useCells: false},
 		"*executor.ProjectionExec":      {level: 2, label: "ProjectionExec", useCells: true},
+		"*executor.SelectLockExec":      {level: 2, label: "SelectLockExec", useCells: true},
+		"*executor.TableDualExec":       {level: 2, label: "TableDualExec", useCells: false},
+		"*executor.TableReaderExecutor": {level: 2, label: "TableReaderExecutor", useCells: false},
+		"*executor.UnionScanExec":       {level: 2, label: "UnionScanExec", useCells: false},
+		"*executor.WindowExec":          {level: 2, label: "WindowExec", useCells: false},
 		"*join.HashJoinV1Exec":          {level: 2, label: "HashJoinV1Exec", useCells: false},
 		"*join.HashJoinV2Exec":          {level: 2, label: "HashJoinV2Exec", useCells: false},
 		"*join.IndexLookUpJoin":         {level: 2, label: "IndexLookUpJoin", useCells: true},
 		"*join.IndexLookUpMergeJoin":    {level: 2, label: "IndexLookUpMergeJoin", useCells: true},
 		"*join.IndexNestedLoopHashJoin": {level: 2, label: "IndexNestedLoopHashJoin", useCells: true},
 		"*join.MergeJoinExec":           {level: 2, label: "MergeJoinExec", useCells: false},
 		"*sortexec.SortExec":            {level: 3, label: "SortExec", useCells: true},
 		"*sortexec.TopNExec":            {level: 2, label: "TopNExec", useCells: true},
 	}
🤖 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 `@pkg/executor/internal/exec/executor_test.go` around lines 73 - 100, Update
the TestRUV2ExecutorMetricByTypeIncludesConcreteExecutorTypes test to assert all
newly added concrete keys exist and have correct ruv2ExecutorMetric values:
locate the ruv2ExecutorMetricByType map and extend the cases map in the test
(function TestRUV2ExecutorMetricByTypeIncludesConcreteExecutorTypes) to include
entries for ExpandExec, IndexReaderExecutor, MemTableReaderExec, SelectLockExec,
TableReaderExecutor, UnionScanExec, WindowExec (and any other new concrete type
names added to the map), providing their expected level, label and useCells
values, then run the same require.True/require.Equal checks as for existing
entries so renames or missing keys fail the test.
🤖 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 `@pkg/executor/internal/exec/executor_test.go`:
- Around line 73-100: Update the
TestRUV2ExecutorMetricByTypeIncludesConcreteExecutorTypes test to assert all
newly added concrete keys exist and have correct ruv2ExecutorMetric values:
locate the ruv2ExecutorMetricByType map and extend the cases map in the test
(function TestRUV2ExecutorMetricByTypeIncludesConcreteExecutorTypes) to include
entries for ExpandExec, IndexReaderExecutor, MemTableReaderExec, SelectLockExec,
TableReaderExecutor, UnionScanExec, WindowExec (and any other new concrete type
names added to the map), providing their expected level, label and useCells
values, then run the same require.True/require.Equal checks as for existing
entries so renames or missing keys fail the test.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 028bf31e-7c5b-4610-9ea5-ceb0b06c2cf9

📥 Commits

Reviewing files that changed from the base of the PR and between 26a4494 and 7220da8.

📒 Files selected for processing (13)
  • pkg/config/config.go
  • pkg/config/config.toml.example
  • pkg/executor/adapter.go
  • pkg/executor/adapter_test.go
  • pkg/executor/explain.go
  • pkg/executor/explain_unit_test.go
  • pkg/executor/insert.go
  • pkg/executor/insert_common.go
  • pkg/executor/internal/exec/BUILD.bazel
  • pkg/executor/internal/exec/executor.go
  • pkg/executor/internal/exec/executor_test.go
  • pkg/sessionctx/stmtctx/stmtctx.go
  • pkg/util/execdetails/ruv2_metrics.go
💤 Files with no reviewable changes (2)
  • pkg/sessionctx/stmtctx/stmtctx.go
  • pkg/executor/explain.go

@rleungx
Copy link
Copy Markdown
Member

rleungx commented May 22, 2026

/ok-to-test

@ti-chi-bot ti-chi-bot Bot added ok-to-test Indicates a PR is ready to be tested. and removed needs-ok-to-test Indicates a PR created by contributors and need ORG member send '/ok-to-test' to start testing. labels May 22, 2026
@codecov
Copy link
Copy Markdown

codecov Bot commented May 22, 2026

Codecov Report

❌ Patch coverage is 71.05263% with 33 lines in your changes missing coverage. Please review.
✅ Project coverage is 75.1595%. Comparing base (26a4494) to head (4ef0f7b).
⚠️ Report is 21 commits behind head on master.

Additional details and impacted files
@@               Coverage Diff                @@
##             master     #68578        +/-   ##
================================================
- Coverage   76.3752%   75.1595%   -1.2158%     
================================================
  Files          2039       2023        -16     
  Lines        562884     569949      +7065     
================================================
- Hits         429904     428371      -1533     
- Misses       132062     141372      +9310     
+ Partials        918        206       -712     
Flag Coverage Δ
integration 41.3078% <71.0526%> (+1.5367%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Components Coverage Δ
dumpling 60.4679% <ø> (ø)
parser ∅ <ø> (∅)
br 48.8776% <ø> (-14.0425%) ⬇️
🚀 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/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 May 24, 2026
Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
pkg/metrics/ru_v2.go (1)

47-147: ⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

Clarify the exported help for executor_l5_insert_rows.

After this PR, update/delete/replace paths also feed this counter, but the /metrics help still says “insert rows”. Please update the help text to describe the legacy name and the actual rows×columns semantics so operators do not misread it.

🤖 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 `@pkg/metrics/ru_v2.go` around lines 47 - 147, The exported help for
RUV2ExecutorL5InsertRows in InitRUV2Metrics is misleadingly "insert rows" even
though updates/deletes also increment it and it actually measures rows×columns;
update the prometheus.CounterOpts.Help for RUV2ExecutorL5InsertRows to mention
the legacy name and the true semantics (e.g. "Legacy 'insert rows' name; counts
affected rows × columns; includes inserts, updates, deletes") so operators
understand what the metric represents.
🤖 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/executor/update.go`:
- Around line 373-386: The metric currently multiplies
sum(content.End-content.Start) by numRows in recordRowsColMultiply2RUV2Metrics,
which counts columns from all table slices even when a particular row's slice is
not updatable; update the counting to only include columns from slices that were
marked updatable during row processing. In practice, in updateRows (after
calling e.prepare(datumRow) or wherever per-row updatable state is known)
accumulate rowsColMultiply by summing int64(content.End-content.Start) only for
indices where e.tableUpdatable[i] is true (using e.tblColPosInfos to get
Start/End), then pass that per-row-aware rowsColMultiply into
recordRowsColMultiply2RUV2Metrics (or adjust recordRowsColMultiply2RUV2Metrics
to accept the precomputed columnCount) so RU charge reflects only
actually-updatable columns.

---

Outside diff comments:
In `@pkg/metrics/ru_v2.go`:
- Around line 47-147: The exported help for RUV2ExecutorL5InsertRows in
InitRUV2Metrics is misleadingly "insert rows" even though updates/deletes also
increment it and it actually measures rows×columns; update the
prometheus.CounterOpts.Help for RUV2ExecutorL5InsertRows to mention the legacy
name and the true semantics (e.g. "Legacy 'insert rows' name; counts affected
rows × columns; includes inserts, updates, deletes") so operators understand
what the metric represents.
🪄 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: 549b6964-51b9-4ee5-bea7-e284541fd0a5

📥 Commits

Reviewing files that changed from the base of the PR and between 7220da8 and ca0ad70.

📒 Files selected for processing (9)
  • pkg/executor/adapter.go
  • pkg/executor/adapter_test.go
  • pkg/executor/delete.go
  • pkg/executor/replace.go
  • pkg/executor/update.go
  • pkg/metrics/metrics.go
  • pkg/metrics/ru_v2.go
  • pkg/util/execdetails/execdetails_test.go
  • pkg/util/execdetails/ruv2_metrics.go

Comment thread pkg/executor/update.go Outdated
Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

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/executor/update.go`:
- Around line 379-389: The RU overcount happens because
rowsColMultiplyForPreparedRow() sums column ranges for indices marked in
e.tableUpdatable without excluding rows already skipped via e.changed; update
rowsColMultiplyForPreparedRow() to also check the corresponding e.changed entry
(ensure i < len(e.changed)) and skip counting when e.changed[i] is true so only
actually-updated rows contribute to the rows×columns tally for UpdateExec.
🪄 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: 6eb4e3ce-3294-4778-9bcd-bccc2f408d84

📥 Commits

Reviewing files that changed from the base of the PR and between ca0ad70 and 9b0c43d.

📒 Files selected for processing (2)
  • pkg/executor/adapter_test.go
  • pkg/executor/update.go

Comment thread pkg/executor/update.go
@meiking
Copy link
Copy Markdown
Author

meiking commented May 25, 2026

/retest-required

@ti-chi-bot
Copy link
Copy Markdown

ti-chi-bot Bot commented May 27, 2026

[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 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

@meiking
Copy link
Copy Markdown
Author

meiking commented May 27, 2026

/retest-required

1 similar comment
@meiking
Copy link
Copy Markdown
Author

meiking commented May 27, 2026

/retest-required

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

Labels

contribution This PR is from a community contributor. do-not-merge/needs-triage-completed do-not-merge/release-note-label-needed Indicates that a PR should not merge because it's missing one of the release note labels. ok-to-test Indicates a PR is ready to be tested. 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.

2 participants