Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

executor: fix RU missing for DML stmtatement in explain analyze #52576

Merged
merged 3 commits into from
Apr 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 8 additions & 24 deletions pkg/executor/explain.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,25 +94,6 @@ func (e *ExplainExec) Next(ctx context.Context, req *chunk.Chunk) error {
return nil
}

func (e *ExplainExec) handleRUDetails(ctx context.Context, onlyRegister bool) {
if e.explain.Analyze && (e.analyzeExec == nil || !e.executed) {
return
}
if coll := e.Ctx().GetSessionVars().StmtCtx.RuntimeStatsColl; coll != nil {
if onlyRegister {
// Register RU stats to make sure the output of explain analyze doesn't change.
newRUDetails := clientutil.NewRUDetails()
coll.RegisterStats(e.explain.TargetPlan.ID(), &ruRuntimeStats{newRUDetails})
return
}

if ruDetailsRaw := ctx.Value(clientutil.RUDetailsCtxKey); ruDetailsRaw != nil {
ruDetails := ruDetailsRaw.(*clientutil.RUDetails)
coll.RegisterStats(e.explain.TargetPlan.ID(), &ruRuntimeStats{ruDetails})
}
}
}

func (e *ExplainExec) executeAnalyzeExec(ctx context.Context) (err error) {
if e.explain.Analyze && e.analyzeExec != nil && !e.executed {
defer func() {
Expand All @@ -124,10 +105,6 @@ func (e *ExplainExec) executeAnalyzeExec(ctx context.Context) (err error) {
err = err1
}
}

// Handle RU runtime stats after Close() to make sure all ru has been collected.
// For example, localMppCoordinator reports last ru consumption when Close().
e.handleRUDetails(ctx, false)
}()
if minHeapInUse, alarmRatio := e.Ctx().GetSessionVars().MemoryDebugModeMinHeapInUse, e.Ctx().GetSessionVars().MemoryDebugModeAlarmRatio; minHeapInUse != 0 && alarmRatio != 0 {
memoryDebugModeCtx, cancel := context.WithCancel(ctx)
Expand Down Expand Up @@ -156,7 +133,14 @@ func (e *ExplainExec) executeAnalyzeExec(ctx context.Context) (err error) {
}
}
}
e.handleRUDetails(ctx, true)
// Register the RU runtime stats to the runtime stats collection after the analyze executor has been executed.
if e.explain.Analyze && e.analyzeExec != nil && e.executed {
ruDetailsRaw := ctx.Value(clientutil.RUDetailsCtxKey)
if coll := e.Ctx().GetSessionVars().StmtCtx.RuntimeStatsColl; coll != nil && ruDetailsRaw != nil {
ruDetails := ruDetailsRaw.(*clientutil.RUDetails)
coll.RegisterStats(e.explain.TargetPlan.ID(), &ruRuntimeStats{ruDetails})
}
}
return err
}

Expand Down