Skip to content

util, config: aggregate raw RUv2 in RUDetails#1941

Closed
disksing wants to merge 3 commits intomasterfrom
disksing/ru-details-ruv2
Closed

util, config: aggregate raw RUv2 in RUDetails#1941
disksing wants to merge 3 commits intomasterfrom
disksing/ru-details-ruv2

Conversation

@disksing
Copy link
Copy Markdown
Collaborator

@disksing disksing commented Apr 10, 2026

Summary

  • add raw RUv2 aggregation to util.RUDetails
  • merge ExecDetailsV2.RuV2 into RUDetails while keeping the existing scaled TiKV RUv2 accumulation
  • extend tests to cover RUv2 aggregation through util/config/client and cop stream paths

Why

TiDB needs to collect RUv2 metrics from multiple request paths, including coprocessor, point/batch reads, and 2PC writes. Aggregating the raw RUv2 counters in RUDetails gives TiDB one unified place to read them from, instead of having to read from ExecDetailsV2 or commit details separately on each path.

Test

  • go test ./util ./config ./tikvrpc ./internal/client

Summary by CodeRabbit

Release Notes

  • New Features

    • Enhanced Resource Unit (RU) v2 tracking for read and write operations across batch-get, point-get, prewrite, and commit flows.
  • Bug Fixes

    • Improved RU v2 accounting by always initializing missing counters and more accurately capturing resource usage metrics from server responses.

@ti-chi-bot ti-chi-bot bot added the dco-signoff: yes Indicates the PR's author has signed the dco. label Apr 10, 2026
@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Apr 10, 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: defaults

Review profile: CHILL

Plan: Pro

Run ID: 7f3543af-f2f0-4d49-ab5d-d5971a5c4b62

📥 Commits

Reviewing files that changed from the base of the PR and between f8f7bf9 and 38f53ee.

📒 Files selected for processing (10)
  • config/ruv2.go
  • config/ruv2_test.go
  • internal/client/client_async_test.go
  • tikvrpc/tikvrpc_test.go
  • txnkv/transaction/commit.go
  • txnkv/transaction/prewrite.go
  • txnkv/txnsnapshot/snapshot.go
  • txnkv/txnsnapshot/snapshot_async.go
  • util/execdetails.go
  • util/execdetails_test.go
🚧 Files skipped from review as they are similar to previous changes (3)
  • txnkv/txnsnapshot/snapshot_async.go
  • internal/client/client_async_test.go
  • tikvrpc/tikvrpc_test.go

📝 Walkthrough

Walkthrough

This PR refactors TiKV RU v2 tracking by introducing a new exported helper UpdateTiKVRUV2FromRUV2 and modifying UpdateTiKVRUV2FromExecDetailsV2 to always allocate missing details. It removes CommitDetails.WriteRUV2 and adds mutex-protected raw RU v2 field tracking in RUDetails. Multiple transaction call sites are updated to invoke RU v2 update functions with execution details.

Changes

Cohort / File(s) Summary
Core RU v2 refactoring
config/ruv2.go
Introduced new exported helper UpdateTiKVRUV2FromRUV2 to conditionally extract RUDetails from context and accumulate raw RU v2. Refactored UpdateTiKVRUV2FromExecDetailsV2 to always allocate missing details and delegate RUDetails updates to the new helper. Extracted weighted RU v2 computation into calculateTiKVRUV2.
RU Details tracking overhaul
util/execdetails.go
Removed CommitDetails.WriteRUV2 field and GetWriteRUV2() method. Added mutex-protected rawRUV2 field to RUDetails with new public methods RUV2(), DrainRUV2(), and AddRUV2(). Updated Clone() and Merge() to handle raw RU v2 accumulation.
Core RU v2 tests
config/ruv2_test.go, util/execdetails_test.go
Expanded TestUpdateTiKVRUV2FromExecDetailsV2 to verify computed RU values via weight factors. Added two new test cases for UpdateTiKVRUV2FromExecDetailsV2 with nil details and UpdateTiKVRUV2FromRUV2. Replaced WriteRUV2 coverage in merge/clone tests with new TestRUDetailsRUV2 and TestRUDetailsDrainRUV2 for raw RU v2 tracking.
RPC and client integration tests
internal/client/client_async_test.go, tikvrpc/tikvrpc_test.go
Added assertions to verify RUDetails.RUV2() is non-nil and counter values match expectations after async prewrite and cop stream response operations.
Transaction path integration
txnkv/txnsnapshot/snapshot.go, txnkv/txnsnapshot/snapshot_async.go, txnkv/transaction/commit.go, txnkv/transaction/prewrite.go
Updated batch-get and point-get flows to call updateTiKVRUV2FromReadResponse with context. Updated commit and prewrite handlers to call UpdateTiKVRUV2FromExecDetailsV2 with execution details and appropriate RPC count parameters.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~30 minutes

Possibly related PRs

Suggested labels

lgtm, approved, size/XL

Suggested reviewers

  • nolouch
  • ekexium

Poem

🐰 RU v2 counters dance with delight,
Raw tracking now holds them tight,
With helpers refactored, calls realigned,
Resource management redesigned! ✨

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 13.64% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'util, config: aggregate raw RUv2 in RUDetails' accurately and concisely summarizes the main change: adding raw RUv2 aggregation capabilities to the RUDetails structure across util and config packages.

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

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch disksing/ru-details-ruv2

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.

@ti-chi-bot ti-chi-bot bot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Apr 10, 2026
@ti-chi-bot
Copy link
Copy Markdown

ti-chi-bot bot commented Apr 10, 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 zyguan 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

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 the current code and only fix it if needed.

Inline comments:
In `@txnkv/txnsnapshot/snapshot.go`:
- Around line 450-451: The code currently skips calling
updateTiKVRUV2FromReadResponse when ExecDetailsV2 (variable details) is nil, so
ReadRpcCount metrics from responses without exec details are not aggregated;
change the logic to always call updateTiKVRUV2FromReadResponse(ctx, details)
without gating on details != nil (the function already handles details == nil),
and apply the same removal of the nil guard at the other two occurrences that
gate on ExecDetailsV2 (the blocks around where details is checked and where the
callers at the two other sites perform the same nil check) so snapshot reads
always contribute ReadRpcCount aggregation even when ExecDetailsV2 is absent.
🪄 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: defaults

Review profile: CHILL

Plan: Pro

Run ID: da28ce29-95da-4186-8a8c-d9f3615b1c56

📥 Commits

Reviewing files that changed from the base of the PR and between 3558d1d and c07272a.

📒 Files selected for processing (2)
  • txnkv/txnsnapshot/snapshot.go
  • txnkv/txnsnapshot/snapshot_async.go

Comment on lines +450 to +451
if details != nil {
updateTiKVRUV2FromReadResponse(ctx, details)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major

Count read RPCs even when ExecDetailsV2 is absent.

config.UpdateTiKVRUV2FromExecDetailsV2 already supports details == nil, but Line 467 and both callers still gate the backfill on a non-nil ExecDetailsV2. That means snapshot reads still miss raw ReadRpcCount aggregation on responses that omit exec details, which is the exact gap this change is trying to close.

💡 Proposed fix
@@
-	if details != nil {
-		updateTiKVRUV2FromReadResponse(ctx, details)
+	updateTiKVRUV2FromReadResponse(ctx, details)
+	if details != nil {
 		readKeys := len(pairs)
 		var readTime float64
@@
 func updateTiKVRUV2FromReadResponse(ctx context.Context, details *kvrpcpb.ExecDetailsV2) {
-	if details == nil || details.GetRuV2().GetReadRpcCount() != 0 {
+	if details != nil && details.GetRuV2().GetReadRpcCount() != 0 {
 		return
 	}
 	config.UpdateTiKVRUV2FromExecDetailsV2(ctx, details, 1, 0)
 }
@@
-		if cmdGetResp.ExecDetailsV2 != nil {
-			updateTiKVRUV2FromReadResponse(bo.GetCtx(), cmdGetResp.ExecDetailsV2)
+		updateTiKVRUV2FromReadResponse(bo.GetCtx(), cmdGetResp.ExecDetailsV2)
+		if cmdGetResp.ExecDetailsV2 != nil {
 			readKeys := len(cmdGetResp.Value)
 			var readTime float64

Also applies to: 466-470, 888-889

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@txnkv/txnsnapshot/snapshot.go` around lines 450 - 451, The code currently
skips calling updateTiKVRUV2FromReadResponse when ExecDetailsV2 (variable
details) is nil, so ReadRpcCount metrics from responses without exec details are
not aggregated; change the logic to always call
updateTiKVRUV2FromReadResponse(ctx, details) without gating on details != nil
(the function already handles details == nil), and apply the same removal of the
nil guard at the other two occurrences that gate on ExecDetailsV2 (the blocks
around where details is checked and where the callers at the two other sites
perform the same nil check) so snapshot reads always contribute ReadRpcCount
aggregation even when ExecDetailsV2 is absent.

…entally

Accumulate raw TiKV RU v2 counters (kvrpcpb.RUV2) in RUDetails so that
TiDB can drain them incrementally via DrainRUV2 without relying on RPC
interceptors.

- Add RUDetails.AddRUV2 / RUV2 / DrainRUV2 for accumulation and drain
- Synthesize RUV2 containers when response lacks ExecDetailsV2
- Backfill ReadRpcCount for get/batchGet when TiKV omits it
- Sync commit-path WriteRUV2 into RUDetails after Commit
- Extract UpdateTiKVRUV2FromRUV2 and calculateTiKVRUV2 helpers

Signed-off-by: disksing <i@disksing.com>
Move updateTiKVRUV2FromReadResponse calls outside the ExecDetailsV2 nil
guards so snapshot reads always contribute ReadRpcCount backfill. Remove
the redundant nil check inside updateTiKVRUV2FromReadResponse since the
downstream UpdateTiKVRUV2FromExecDetailsV2 already handles nil details.

Signed-off-by: disksing <i@disksing.com>
…entally on response

Instead of aggregating write RUV2 into CommitDetails and draining it
after commit, call config.UpdateTiKVRUV2FromExecDetailsV2 directly in
the prewrite and commit response handlers. This makes write RUV2
collection consistent with the read path's incremental approach.

Signed-off-by: disksing <i@disksing.com>
@disksing disksing force-pushed the disksing/ru-details-ruv2 branch from f8f7bf9 to 38f53ee Compare April 10, 2026 21:30
@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 Apr 10, 2026
@disksing disksing closed this Apr 10, 2026
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. 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.

1 participant