executor: use statement context for pre-analyze stats flush#68146
executor: use statement context for pre-analyze stats flush#68146ti-chi-bot[bot] merged 4 commits intopingcap:masterfrom
Conversation
|
Review Complete Findings: 0 issues ℹ️ Learn more details on Pantheon AI. |
|
Note Reviews pausedIt 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 Use the following commands to manage reviews:
Use the checkboxes below for quick actions:
📝 WalkthroughWalkthroughThreads a statement-scoped context through executor construction, splitting executorBuilder into a statement ChangesExecutor builder + analyze cancellation
Sequence Diagram(s)sequenceDiagram
participant Client as Client (user / KILL QUERY)
participant Exec as ExecStmt
participant Builder as ExecutorBuilder (stmtCtx, sctx)
participant Stats as StatsFlush RPC
Client->>Exec: run ANALYZE (with stmtCtx)
Exec->>Builder: newExecutorBuilder(stmtCtx, sctx)
Builder->>Stats: flushStatsDeltaForAnalyze(ctx = stmtCtx)
alt ctx canceled before broadcast
Stats-->>Builder: aborted (ctx.Err)
Builder-->>Exec: return error (context.Canceled)
else proceed
Stats-->>Builder: broadcast/flush complete
Builder-->>Exec: continue building analyzers
end
Estimated code review effort🎯 4 (Complex) | ⏱️ ~45 minutes Possibly related PRs
Suggested labels
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (2)
pkg/executor/adapter.go (2)
1399-1414: ⚡ Quick winDocument that
ctxis the statement-scoped cancellation context.Now that
buildExecutortakes bothctxandsctx, the critical invariant is easy to miss. A short comment here would make it harder to reintroduce a detached background context in future call sites.Suggested comment update
-// buildExecutor build an executor from plan, prepared statement may need additional procedure. +// buildExecutor builds an executor from plan using the statement-scoped ctx for +// cancellation-sensitive build steps (for example ANALYZE pre-work). Prepared +// statements may need additional build steps. func (a *ExecStmt) buildExecutor(ctx context.Context) (exec.Executor, error) {As per coding guidelines, "Comments SHOULD explain non-obvious intent, constraints, invariants, concurrency guarantees, SQL/compatibility contracts, or important performance trade-offs, and SHOULD NOT restate what the code already makes clear."
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@pkg/executor/adapter.go` around lines 1399 - 1414, Add a short comment at the top of the buildExecutor function clarifying that the ctx parameter is the statement-scoped cancellation context (tied to this statement's lifetime) and must not be replaced with a background or long-lived context; reference the function name buildExecutor and the local variable sctx to make the invariant explicit so future changes won't detach cancellation from the session/statement. Ensure the comment sits immediately above the function signature or the first lines of the body where ctx and sctx are used.
681-684: 🏗️ Heavy liftPlease add or point to an integration test for the real
KILL QUERYpath.The referenced unit coverage proves a pre-canceled build context fails fast, but the regression here was a user-visible ANALYZE cancellation path. An end-to-end test that interrupts ANALYZE through the actual kill/cancel wiring would protect that contract better.
Please confirm an existing integration test already covers this path, or add one if it does not.
Based on learnings, "For SQL behavior changes in executor, perform targeted unit test plus relevant integration test."
Also applies to: 1373-1375
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@pkg/executor/adapter.go` around lines 681 - 684, The change needs an integration test that exercises the real KILL QUERY cancellation path for ANALYZE rather than only the pre-canceled build context unit test; add (or point to) an end-to-end test that starts an ANALYZE query, sends a KILL QUERY for that session/statement and asserts the executor returns the expected cancellation error/behavior (exercise the same codepath that calls buildExecutor and the cancellation handling around lines referencing buildExecutor and the ANALYZE cancel logic), or explicitly confirm an existing integration test already does this; ensure the test targets the executor path that builds the executor (buildExecutor) and the SQL-level kill wiring so the regression is caught.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@pkg/executor/adapter.go`:
- Around line 1399-1414: Add a short comment at the top of the buildExecutor
function clarifying that the ctx parameter is the statement-scoped cancellation
context (tied to this statement's lifetime) and must not be replaced with a
background or long-lived context; reference the function name buildExecutor and
the local variable sctx to make the invariant explicit so future changes won't
detach cancellation from the session/statement. Ensure the comment sits
immediately above the function signature or the first lines of the body where
ctx and sctx are used.
- Around line 681-684: The change needs an integration test that exercises the
real KILL QUERY cancellation path for ANALYZE rather than only the pre-canceled
build context unit test; add (or point to) an end-to-end test that starts an
ANALYZE query, sends a KILL QUERY for that session/statement and asserts the
executor returns the expected cancellation error/behavior (exercise the same
codepath that calls buildExecutor and the cancellation handling around lines
referencing buildExecutor and the ANALYZE cancel logic), or explicitly confirm
an existing integration test already does this; ensure the test targets the
executor path that builds the executor (buildExecutor) and the SQL-level kill
wiring so the regression is caught.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro
Run ID: e4b1d00a-fab5-4794-adba-2e00bbfe784a
📒 Files selected for processing (10)
pkg/executor/adapter.gopkg/executor/analyze.gopkg/executor/analyze_test.gopkg/executor/analyze_utils_test.gopkg/executor/benchmark_test.gopkg/executor/builder.gopkg/executor/builder_index_join_cleanup_test.gopkg/executor/coprocessor.gopkg/executor/executor_required_rows_test.gopkg/executor/select.go
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## master #68146 +/- ##
================================================
- Coverage 77.7620% 77.0600% -0.7021%
================================================
Files 1990 1972 -18
Lines 551774 552612 +838
================================================
- Hits 429071 425843 -3228
- Misses 121783 126756 +4973
+ Partials 920 13 -907
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Actionable comments posted: 2
🧹 Nitpick comments (1)
pkg/executor/builder.go (1)
3319-3324: ⚡ Quick winPlease add a relevant integration test for the SQL-level cancellation path.
The regression test described in the PR covers a pre-canceled statement context before executor build, but this is still an executor-layer SQL behavior change. It should also be exercised end-to-end with a real
ANALYZEinterrupted via statement cancel /KILL QUERY.Based on learnings: "Applies to pkg/executor/** : For SQL behavior changes in executor, perform targeted unit test plus relevant integration test."
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@pkg/executor/builder.go` around lines 3319 - 3324, Add an end-to-end integration test that exercises the SQL-level cancellation path for ANALYZE: simulate a running ANALYZE statement that enters the executor path where buildAnalyzeSamplingPushdown calls flushStatsDeltaForAnalyze (use a real TiDB server test harness or cluster test that issues ANALYZE on a table), then cancel it via statement cancel / KILL QUERY and assert that the server handles the cancellation cleanly (no double-counting or leaked state) and that the error path in flushStatsDeltaForAnalyze is exercised; locate the executor behavior around buildAnalyzeSamplingPushdown, b.stmtCtx and kv.WithInternalSourceType to ensure the test actually cancels while flushStatsDeltaForAnalyze is running.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@pkg/executor/builder.go`:
- Around line 3300-3301: The call in getApproximateTableCountFromStorage
currently passes context.Background() to
pdhelper.GlobalPDHelper.GetApproximateTableCountFromStorage which ignores
statement cancellation; change it to use the builder's statement-scoped context
(use b.sctx's statement context) instead of context.Background() so ANALYZE
cancellations/KILL QUERY are respected—mirror the approach used by
flushStatsDeltaForAnalyze and pass the statement-scoped context from b.sctx into
GetApproximateTableCountFromStorage.
In `@pkg/executor/index_merge_reader.go`:
- Around line 194-195: The call to distsql.IndexRangesToKVRanges currently
ignores its error and proceeds to use keyRange; change this to capture the
returned error (e.g. kr, err := distsql.IndexRangesToKVRanges(...)) and handle
it instead of discarding it—either propagate the error up from the enclosing
method, or log and return an actionable error with context including
e.table.Meta().ID, idx.ID and which range (e.ranges[i]) failed; only assign to
e.partitionKeyRanges[i] when err is nil and keyRange is valid.
---
Nitpick comments:
In `@pkg/executor/builder.go`:
- Around line 3319-3324: Add an end-to-end integration test that exercises the
SQL-level cancellation path for ANALYZE: simulate a running ANALYZE statement
that enters the executor path where buildAnalyzeSamplingPushdown calls
flushStatsDeltaForAnalyze (use a real TiDB server test harness or cluster test
that issues ANALYZE on a table), then cancel it via statement cancel / KILL
QUERY and assert that the server handles the cancellation cleanly (no
double-counting or leaked state) and that the error path in
flushStatsDeltaForAnalyze is exercised; locate the executor behavior around
buildAnalyzeSamplingPushdown, b.stmtCtx and kv.WithInternalSourceType to ensure
the test actually cancels while flushStatsDeltaForAnalyze is running.
🪄 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: 874023d4-ce44-4d07-9e23-af07747a74a1
📒 Files selected for processing (7)
pkg/executor/adapter.gopkg/executor/brie.gopkg/executor/builder.gopkg/executor/foreign_key.gopkg/executor/index_merge_reader.gopkg/executor/point_get.gopkg/executor/traffic_test.go
b417c70 to
da1090b
Compare
6208e3f to
9ff5f21
Compare
0xPoe
left a comment
There was a problem hiding this comment.
🔢 Self-check (PR reviewed by myself and ready for feedback)
-
Code compiles successfully
-
Unit tests added
-
No AI-generated elegant nonsense in PR.
-
Bazel files updated
-
Comments added where necessary
-
PR title and description updated
-
Documentation PR created (or confirmed not needed)
-
PR size is reasonable
|
/retest |
mjonss
left a comment
There was a problem hiding this comment.
LGTM, but I would like a different, not as overloaded, name for stmtCtx. Maybe simply 'ctx'?
9ff5f21 to
2b71025
Compare
2b71025 to
f55a870
Compare
|
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: mjonss, terry1purcell The full list of commands accepted by this bot can be found here. The pull request process is described here DetailsNeeds approval from an approver in each of these files:
Approvers can indicate their approval by writing |
[LGTM Timeline notifier]Timeline:
|
…68146) Backport pingcap#68146 to release-8.5. Squashes the PR commits and its required pending-stats-delta flush prerequisite into a single backport commit. The cluster broadcast path is intentionally adapted to release-8.5's local DumpStatsDeltaToKV implementation.
…68146) Backport pingcap#68146 to release-8.5.
…68146) Backport pingcap#68146 to release-8.5.
…68146) Backport pingcap#68146 to release-8.5.
…68146) Backport pingcap#68146 to release-8.5.
…68146) Backport pingcap#68146 to release-8.5.
…68146) Backport pingcap#68146 to release-8.5.
…68146) Backport pingcap#68146 to release-8.5.
…68146) Backport pingcap#68146 to release-8.5.
…68146) Backport pingcap#68146 to release-8.5.
…68146) Backport pingcap#68146 to release-8.5.
…68146) Backport pingcap#68146 to release-8.5.
What problem does this PR solve?
Issue Number: close #68074
Problem Summary:
Pre-ANALYZE stats-delta flush used a detached context, so statement cancellation could be missed during executor build.
What changed and how does it work?
Pass the statement context into
executorBuilderand use it for context-sensitive ANALYZE build work:FLUSH STATS_DELTAduring executor build;buildAnalyzeIndexPushdown,buildAnalyzeSamplingPushdown, and special-index NDV sub-jobs;getApproximateTableCountFromStorage.Added regression coverage for canceled statement contexts during executor build.
Check List
Tests
Side effects
Documentation
Release note
Please refer to Release Notes Language Style Guide to write a quality release note.
Summary by CodeRabbit