Skip to content

resource_control: add PredictedReadBytes hint for RC paging pre-charge#10599

Draft
YuhaoZhang00 wants to merge 6 commits intotikv:masterfrom
YuhaoZhang00:demo/ema-precharge
Draft

resource_control: add PredictedReadBytes hint for RC paging pre-charge#10599
YuhaoZhang00 wants to merge 6 commits intotikv:masterfrom
YuhaoZhang00:demo/ema-precharge

Conversation

@YuhaoZhang00
Copy link
Copy Markdown
Contributor

Summary

Add an optional predictedReadBytesProvider interface on RequestInfo.
When a caller (e.g. TiDB maintaining a per-logical-scan EMA across paging
cop RPCs) supplies a non-zero PredictedReadBytes, BeforeKVRequest and
AfterKVRequest use that value as the byte basis for the paging pre-charge
instead of PagingSizeBytes. PagingSizeBytes remains the fallback and
worst-case cap.

Existing RequestInfo implementations compile unchanged — the hint is an
optional interface, not a method on RequestInfo.

Why

The fixed 4 MiB paging pre-charge introduced in #10548 typically overshoots
actual scanned bytes and stalls concurrent workers at Phase 1 under tight
resource tiers. A learned per-scan estimate from the TiDB side eliminates
the over-estimation without changing kvproto or TiKV behavior.

Status

Draft. Part of a stacked change:

  • tikv/client-go: adds tikvrpc.Request.PredictedReadBytes + RequestInfo getter
  • pingcap/tidb: maintains per-logical-scan TAEMA on copTask, feeds prediction on every paging RPC

Not yet ready for review; e2e validation on the simulation cluster is pending.

Test plan

  • Unit tests in client/resource_group/controller/ cover hint-present and hint-absent paths, plus an excess-pre-charge refund case
  • e2e: FullScan + JOIN paging sweep on simulation cluster (pending)

JmPotato and others added 4 commits April 2, 2026 10:18
Add PagingSizeBytes() to RequestInfo interface. When a read request
carries a byte budget (from RC paging), BeforeKVRequest pre-charges
the estimated read RU in Phase 1, and AfterKVRequest subtracts it
in Phase 2 to maintain correct total cost.

This makes concurrent workers throttle at Phase 1 instead of all
hitting Phase 2 simultaneously.

Signed-off-by: JmPotato <github@ipotato.me>
Test that BeforeKVRequest pre-charges pagingSizeBytes * ReadBytesCost,
AfterKVRequest subtracts it, and the net total equals baseCost + actualCost.

Signed-off-by: JmPotato <github@ipotato.me>
…ment

When paging byte budget (pagingSizeBytes) is enabled, BeforeKVRequest
pre-charges ReadBytesCost * pagingSizeBytes RRU into the token limiter.
AfterKVRequest then computes the actual cost and subtracts the
pre-charge. If the actual cost is less than the pre-charge (common case,
since pagingSizeBytes is an upper bound), the settlement delta is
negative. Previously, the negative delta was correctly recorded in the
consumption counter but silently dropped by the token limiter due to a
`v > 0` guard — causing permanent token leakage proportional to
(pagingSizeBytes - actualReadBytes) per request.

Fix: add Limiter.RefundTokens (inverse of RemoveTokens) and call it from
both onResponseImpl and onResponseWaitImpl when the settlement delta is
negative. This ensures the limiter's available token balance accurately
reflects actual consumption after each request completes.

RefundTokens design notes:
- No burst cap applied (consistent with Reconfigure; getTokens handles
  lazy capping on the next limiter operation).
- No maybeNotify call (refunding moves balance away from the low-token
  threshold, never toward it).

Signed-off-by: JmPotato <github@ipotato.me>
Introduce an optional predictedReadBytesProvider interface on RequestInfo.
When a caller (e.g. TiDB maintaining a per-logical-scan EMA across paging
RPCs) supplies a non-zero PredictedReadBytes, BeforeKVRequest/AfterKVRequest
use that value as the byte basis for the paging pre-charge instead of
PagingSizeBytes. PagingSizeBytes remains the fallback and worst-case cap.

This lets TiDB replace the current fixed 4 MiB pre-charge (which matches
the paging byte budget but typically overshoots actual scanned bytes and
stalls concurrent workers at Phase 1) with a learned estimate, without
changing kvproto or TiKV behavior.

The hint is added as an optional interface (not a method on RequestInfo)
so existing RequestInfo implementations compile unchanged; they continue
to fall back to PagingSizeBytes.

Ref: per-logical-scan EMA pre-deduction design
Signed-off-by: Yuhao Zhang <yhzhang00@outlook.com>
@ti-chi-bot ti-chi-bot bot added do-not-merge/needs-linked-issue do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. labels Apr 14, 2026
@ti-chi-bot
Copy link
Copy Markdown
Contributor

ti-chi-bot bot commented Apr 14, 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 dco-signoff: yes Indicates the PR's author has signed the dco. do-not-merge/release-note-label-needed Indicates that a PR should not merge because it's missing one of the release note labels. labels Apr 14, 2026
@ti-chi-bot
Copy link
Copy Markdown
Contributor

ti-chi-bot bot commented Apr 14, 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 husharp 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

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Apr 14, 2026

Important

Review skipped

Draft detected.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: ed7d056f-5454-4752-bf28-7128f9c538ae

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ 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 and usage tips.

@ti-chi-bot ti-chi-bot bot added 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 Apr 14, 2026
@ti-chi-bot
Copy link
Copy Markdown
Contributor

ti-chi-bot bot commented Apr 14, 2026

Hi @YuhaoZhang00. Thanks for your PR.

I'm waiting for a tikv 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.

@ti-chi-bot ti-chi-bot bot added the size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. label Apr 14, 2026
@codecov
Copy link
Copy Markdown

codecov bot commented Apr 14, 2026

Codecov Report

❌ Patch coverage is 81.57895% with 7 lines in your changes missing coverage. Please review.
✅ Project coverage is 79.01%. Comparing base (3eb99ae) to head (f01d119).
⚠️ Report is 18 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master   #10599      +/-   ##
==========================================
+ Coverage   78.88%   79.01%   +0.13%     
==========================================
  Files         530      532       +2     
  Lines       71548    71992     +444     
==========================================
+ Hits        56439    56888     +449     
+ Misses      11092    11085       -7     
- Partials     4017     4019       +2     
Flag Coverage Δ
unittests 79.01% <81.57%> (+0.13%) ⬆️

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

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Adds four Prometheus metrics under namespace resource_manager_client to
make the PredictedReadBytes-based paging pre-charge fix directly
observable end-to-end:

- paging_precharge_source_total{source=predicted|fallback}
- paging_precharge_bytes_total{source}
- paging_actual_bytes_total{source}
- paging_prediction_residual_bytes (histogram, predicted path only)

They are wired into onRequestWaitImpl / onResponseImpl so no change to
the ResourceCalculator interface is needed. Labels are cached per group
to keep the hot path allocation-free.

Signed-off-by: Yuhao Zhang <yhzhang00@outlook.com>
@ti-chi-bot ti-chi-bot bot added size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. and removed size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. labels Apr 14, 2026
@YuhaoZhang00
Copy link
Copy Markdown
Contributor Author

Added 4 observability metrics on the PD client side for upcoming e2e validation of the paging pre-charge path:

  • resource_manager_client_request_paging_precharge_source_total{source=predicted|fallback} — does the PredictedReadBytes hint actually drive BeforeKVRequest
  • resource_manager_client_request_paging_precharge_bytes_total{source} — pre-charged byte volume per path
  • resource_manager_client_request_paging_actual_bytes_total{source} — observed ReadBytes per path (ratio with above ≈ over-charge factor; historical ≈4×, target ≈1×)
  • resource_manager_client_request_paging_prediction_residual_bytes histogram — signed actual − predicted distribution for EMA accuracy

No public interface change; instrumentation is at the onRequestWait / onResponse call sites in group_controller.go. Draft status unchanged; keeping this open pending Step E validation results.

The paging actual-bytes counter was only updated from onResponseImpl, but
most responses under RC throttling flow through onResponseWaitImpl, so the
counter stayed at 0 in practice and the over-charge ratio metric could
not be computed. Mirror the observePagingActual call into the wait path.

Signed-off-by: Yuhao Zhang <yhzhang00@outlook.com>
@ti-chi-bot
Copy link
Copy Markdown
Contributor

ti-chi-bot bot commented Apr 14, 2026

[FORMAT CHECKER NOTIFICATION]

Notice: To remove the do-not-merge/needs-linked-issue label, please provide the linked issue number on one line in the PR body, for example: Issue Number: close #123 or Issue Number: ref #456, multiple issues should use full syntax for each issue and be separated by a comma, like: Issue Number: close #123, ref #456.

📖 For more info, you can check the "Linking issues" section in the CONTRIBUTING.md.

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. dco-signoff: yes Indicates the PR's author has signed the dco. do-not-merge/needs-linked-issue do-not-merge/release-note-label-needed Indicates that a PR should not merge because it's missing one of the release note labels. do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. needs-ok-to-test Indicates a PR created by contributors and need ORG member send '/ok-to-test' to start testing. 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.

2 participants