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

planner: in json explain make explaininfo an object instead of a string #47401

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 40 additions & 2 deletions pkg/planner/core/common_plans.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
"github.com/pingcap/tidb/pkg/planner/util"
"github.com/pingcap/tidb/pkg/planner/util/costusage"
"github.com/pingcap/tidb/pkg/planner/util/optimizetrace"
"github.com/pingcap/tidb/pkg/sessionctx"
"github.com/pingcap/tidb/pkg/sessionctx/variable"
"github.com/pingcap/tidb/pkg/statistics"
"github.com/pingcap/tidb/pkg/table"
Expand Down Expand Up @@ -786,14 +787,21 @@ func NewLineFieldsInfo(fieldsInfo *ast.FieldsClause, linesInfo *ast.LinesClause)
return e
}

// ExecuteInfo store explain info for JSON encode
type ExecuteInfo struct {
Time string `json:"time",omitempty`
Loops int32 `json:"loops",omitempty`
Concurrency int32 `json:omitempty`
}

// ExplainInfoForEncode store explain info for JSON encode
type ExplainInfoForEncode struct {
ID string `json:"id"`
EstRows string `json:"estRows"`
ActRows string `json:"actRows,omitempty"`
TaskType string `json:"taskType"`
AccessObject string `json:"accessObject,omitempty"`
ExecuteInfo string `json:"executeInfo,omitempty"`
ExecuteInfo ExecuteInfo `json:"executeInfo,omitempty"`
OperatorInfo string `json:"operatorInfo,omitempty"`
EstCost string `json:"estCost,omitempty"`
CostFormula string `json:"costFormula,omitempty"`
Expand Down Expand Up @@ -1080,6 +1088,36 @@ func (e *Explain) explainFlatOpInRowFormat(flatOp *FlatOperator) {
e.prepareOperatorInfo(flatOp.Origin, taskTp, textTreeExplainID)
}

func getRuntimeInfoJSON(ctx sessionctx.Context, p Plan, runtimeStatsColl *execdetails.RuntimeStatsColl) (actRows string, analyzeInfo ExecuteInfo, memoryInfo, diskInfo string) {
if runtimeStatsColl == nil {
runtimeStatsColl = ctx.GetSessionVars().StmtCtx.RuntimeStatsColl
if runtimeStatsColl == nil {
return
}
}
rootStats, copStats, memTracker, diskTracker := getRuntimeInfo(ctx, p, runtimeStatsColl)
actRows = "0"
memoryInfo = "N/A"
diskInfo = "N/A"
if rootStats != nil {
actRows = strconv.FormatInt(rootStats.GetActRows(), 10)
analyzeInfo.Time = rootStats.GetConsume()
analyzeInfo.Loops = rootStats.GetLoops()
}
if copStats != nil {
actRows = strconv.FormatInt(copStats.GetActRows(), 10)
// analyzeInfo.Time = copStats.String()
// analyzeInfo.Loops = 123456
}
if memTracker != nil {
memoryInfo = memTracker.FormatBytes(memTracker.MaxConsumed())
}
if diskTracker != nil {
diskInfo = diskTracker.FormatBytes(diskTracker.MaxConsumed())
}
return
}

func getRuntimeInfoStr(ctx base.PlanContext, p base.Plan, runtimeStatsColl *execdetails.RuntimeStatsColl) (actRows, analyzeInfo, memoryInfo, diskInfo string) {
if runtimeStatsColl == nil {
runtimeStatsColl = ctx.GetSessionVars().StmtCtx.RuntimeStatsColl
Expand Down Expand Up @@ -1184,7 +1222,7 @@ func (e *Explain) prepareOperatorInfoForJSONFormat(p base.Plan, taskType, id str
}

if e.Analyze || e.RuntimeStatsColl != nil {
jsonRow.ActRows, jsonRow.ExecuteInfo, jsonRow.MemoryInfo, jsonRow.DiskInfo = getRuntimeInfoStr(e.SCtx(), p, e.RuntimeStatsColl)
jsonRow.ActRows, jsonRow.ExecuteInfo, jsonRow.MemoryInfo, jsonRow.DiskInfo = getRuntimeInfoJSON(e.SCtx(), p, e.RuntimeStatsColl)
}
return jsonRow
}
Expand Down
14 changes: 14 additions & 0 deletions pkg/util/execdetails/execdetails.go
Original file line number Diff line number Diff line change
Expand Up @@ -1360,6 +1360,20 @@ func (e *RootRuntimeStats) GetActRows() int64 {
return e.basic.rows.Load()
}

func (e *RootRuntimeStats) GetLoops() int32 {
if e.basic == nil {
return 0
}
return e.basic.loop.Load()
}

func (e *RootRuntimeStats) GetConsume() string {
if e.basic == nil {
return ""
}
return FormatDuration(time.Duration(e.basic.consume.Load()))
}

// MergeStats merges stats in the RootRuntimeStats and return the stats suitable for display directly.
func (e *RootRuntimeStats) MergeStats() (basic *BasicRuntimeStats, groups []RuntimeStats) {
return e.basic, e.groupRss
Expand Down