Skip to content

Commit

Permalink
builder: support EXPLAIN ANALYZE to display RU statistical information (
Browse files Browse the repository at this point in the history
  • Loading branch information
JmPotato authored Mar 17, 2023
1 parent 5aceb2e commit 55d2944
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 16 deletions.
8 changes: 4 additions & 4 deletions DEPS.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -4101,8 +4101,8 @@ def go_deps():
name = "com_github_tikv_client_go_v2",
build_file_proto_mode = "disable_global",
importpath = "github.com/tikv/client-go/v2",
sum = "h1:1Xqx7UgNloQqQvR8k3oHGrPDlgYVK4XQnwZacfPbAQA=",
version = "v2.0.7-0.20230313133219-c9119d02cef7",
sum = "h1:KFKjXBwDCfmPyNjMgNE2YAS+ZtwryVSYSlCSNhzpbig=",
version = "v2.0.7-0.20230316080603-d19741b3ed77",
)
go_repository(
name = "com_github_tikv_pd",
Expand All @@ -4116,8 +4116,8 @@ def go_deps():
name = "com_github_tikv_pd_client",
build_file_proto_mode = "disable_global",
importpath = "github.com/tikv/pd/client",
sum = "h1:AXgc/Ij348pp0TsMPq/tmQA4O0EOAGntTKzB1imhpcU=",
version = "v0.0.0-20230309025512-47cd76ae5d67",
sum = "h1:CYU+awkq5ykKyWV2e2Z+qtRveWMttV4N3r0lyk/z4/M=",
version = "v0.0.0-20230316082839-7a0ce101c243",
)
go_repository(
name = "com_github_timakin_bodyclose",
Expand Down
17 changes: 17 additions & 0 deletions executor/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ import (
"github.com/tikv/client-go/v2/tikv"
"github.com/tikv/client-go/v2/txnkv"
"github.com/tikv/client-go/v2/txnkv/txnsnapshot"
clientutil "github.com/tikv/client-go/v2/util"
"golang.org/x/exp/slices"
)

Expand Down Expand Up @@ -1203,6 +1204,22 @@ func (b *executorBuilder) buildExplain(v *plannercore.Explain) Executor {
if b.ctx.GetSessionVars().StmtCtx.RuntimeStatsColl == nil {
b.ctx.GetSessionVars().StmtCtx.RuntimeStatsColl = execdetails.NewRuntimeStatsColl(nil)
}
// If the resource group name is not empty, we could collect and display the RU
// runtime stats for analyze executor.
resourceGroupName := b.ctx.GetSessionVars().ResourceGroupName
// Try to register the RU runtime stats for analyze executor.
if store, ok := b.ctx.GetStore().(interface {
CreateRURuntimeStats(uint64) *clientutil.RURuntimeStats
}); len(resourceGroupName) > 0 && ok {
// StartTS will be used to identify this SQL, so that the runtime stats could
// aggregate the RU stats beneath the KV storage client.
startTS, err := b.getSnapshotTS()
if err != nil {
b.err = err
return nil
}
explainExec.ruRuntimeStats = store.CreateRURuntimeStats(startTS)
}
explainExec.analyzeExec = b.build(v.TargetPlan)
}
return explainExec
Expand Down
62 changes: 57 additions & 5 deletions executor/explain.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,24 @@ import (
"github.com/pingcap/tidb/config"
"github.com/pingcap/tidb/planner/core"
"github.com/pingcap/tidb/util/chunk"
"github.com/pingcap/tidb/util/execdetails"
"github.com/pingcap/tidb/util/logutil"
"github.com/pingcap/tidb/util/mathutil"
"github.com/pingcap/tidb/util/memory"
clientutil "github.com/tikv/client-go/v2/util"
"go.uber.org/zap"
)

// ExplainExec represents an explain executor.
type ExplainExec struct {
baseExecutor

explain *core.Explain
analyzeExec Executor
executed bool
rows [][]string
cursor int
explain *core.Explain
analyzeExec Executor
executed bool
ruRuntimeStats *clientutil.RURuntimeStats
rows [][]string
cursor int
}

// Open implements the Executor Open interface.
Expand Down Expand Up @@ -128,6 +131,12 @@ func (e *ExplainExec) executeAnalyzeExec(ctx context.Context) (err error) {
}
}
}
// Register the RU runtime stats to the runtime stats collection after the analyze executor has been executed.
if e.analyzeExec != nil && e.executed {
if coll := e.ctx.GetSessionVars().StmtCtx.RuntimeStatsColl; coll != nil {
coll.RegisterStats(e.explain.TargetPlan.ID(), &ruRuntimeStats{e.ruRuntimeStats})
}
}
return err
}

Expand Down Expand Up @@ -305,3 +314,46 @@ func getHeapProfile() (fileName string, err error) {
}
return fileName, nil
}

// ruRuntimeStats is a wrapper of clientutil.RURuntimeStats,
// which implements the RuntimeStats interface.
type ruRuntimeStats struct {
*clientutil.RURuntimeStats
}

// String implements the RuntimeStats interface.
func (e *ruRuntimeStats) String() string {
if e.RURuntimeStats != nil {
return e.RURuntimeStats.String()
}
return ""
}

// Clone implements the RuntimeStats interface.
func (e *ruRuntimeStats) Clone() execdetails.RuntimeStats {
newRs := &ruRuntimeStats{}
if e.RURuntimeStats != nil {
newRs.RURuntimeStats = e.RURuntimeStats.Clone()
}
return newRs
}

// Merge implements the RuntimeStats interface.
func (e *ruRuntimeStats) Merge(other execdetails.RuntimeStats) {
tmp, ok := other.(*ruRuntimeStats)
if !ok {
return
}
if tmp.RURuntimeStats != nil {
if e.RURuntimeStats == nil {
e.RURuntimeStats = tmp.RURuntimeStats.Clone()
return
}
e.RURuntimeStats.Merge(tmp.RURuntimeStats)
}
}

// Tp implements the RuntimeStats interface.
func (e *ruRuntimeStats) Tp() int {
return execdetails.TpRURuntimeStats
}
2 changes: 1 addition & 1 deletion executor/explainfor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func TestExplainFor(t *testing.T) {
buf.WriteString(fmt.Sprintf("%v", v))
}
}
require.Regexp(t, "TableReader_5 10000.00 0 root time:.*, loops:1, cop_task: {num:.*, max:.*, proc_keys:.* rpc_num: 1, rpc_time:.*} data:TableFullScan_4 N/A N/A\n"+
require.Regexp(t, "TableReader_5 10000.00 0 root time:.*, loops:1,( RRU:.*, WRU:.*,)? cop_task: {num:.*, max:.*, proc_keys:.* rpc_num: 1, rpc_time:.*} data:TableFullScan_4 N/A N/A\n"+
"└─TableFullScan_4 10000.00 0 cop.* table:t1 tikv_task:{time:.*, loops:0} keep order:false, stats:pseudo N/A N/A",
buf.String())
}
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ require (
github.com/stretchr/testify v1.8.2
github.com/tdakkota/asciicheck v0.1.1
github.com/tiancaiamao/appdash v0.0.0-20181126055449-889f96f722a2
github.com/tikv/client-go/v2 v2.0.7-0.20230313133219-c9119d02cef7
github.com/tikv/pd/client v0.0.0-20230309025512-47cd76ae5d67
github.com/tikv/client-go/v2 v2.0.7-0.20230316080603-d19741b3ed77
github.com/tikv/pd/client v0.0.0-20230316082839-7a0ce101c243
github.com/timakin/bodyclose v0.0.0-20221125081123-e39cf3fc478e
github.com/twmb/murmur3 v1.1.6
github.com/uber/jaeger-client-go v2.22.1+incompatible
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -941,10 +941,10 @@ github.com/tiancaiamao/appdash v0.0.0-20181126055449-889f96f722a2 h1:mbAskLJ0oJf
github.com/tiancaiamao/appdash v0.0.0-20181126055449-889f96f722a2/go.mod h1:2PfKggNGDuadAa0LElHrByyrz4JPZ9fFx6Gs7nx7ZZU=
github.com/tiancaiamao/gp v0.0.0-20221230034425-4025bc8a4d4a h1:J/YdBZ46WKpXsxsW93SG+q0F8KI+yFrcIDT4c/RNoc4=
github.com/tiancaiamao/gp v0.0.0-20221230034425-4025bc8a4d4a/go.mod h1:h4xBhSNtOeEosLJ4P7JyKXX7Cabg7AVkWCK5gV2vOrM=
github.com/tikv/client-go/v2 v2.0.7-0.20230313133219-c9119d02cef7 h1:1Xqx7UgNloQqQvR8k3oHGrPDlgYVK4XQnwZacfPbAQA=
github.com/tikv/client-go/v2 v2.0.7-0.20230313133219-c9119d02cef7/go.mod h1:61YdH33t2SXn7kOGgwHHbyif+O9PfFIVdLezu7gDQ6c=
github.com/tikv/pd/client v0.0.0-20230309025512-47cd76ae5d67 h1:AXgc/Ij348pp0TsMPq/tmQA4O0EOAGntTKzB1imhpcU=
github.com/tikv/pd/client v0.0.0-20230309025512-47cd76ae5d67/go.mod h1:N2QHc05Vll8CofXQor47lpW5d22WDosFC8WPVx9BsbU=
github.com/tikv/client-go/v2 v2.0.7-0.20230316080603-d19741b3ed77 h1:KFKjXBwDCfmPyNjMgNE2YAS+ZtwryVSYSlCSNhzpbig=
github.com/tikv/client-go/v2 v2.0.7-0.20230316080603-d19741b3ed77/go.mod h1:DPL03G+QwLmypNjDIl+B02UltorBMx3WzSh4yJbp+cw=
github.com/tikv/pd/client v0.0.0-20230316082839-7a0ce101c243 h1:CYU+awkq5ykKyWV2e2Z+qtRveWMttV4N3r0lyk/z4/M=
github.com/tikv/pd/client v0.0.0-20230316082839-7a0ce101c243/go.mod h1:N2QHc05Vll8CofXQor47lpW5d22WDosFC8WPVx9BsbU=
github.com/timakin/bodyclose v0.0.0-20221125081123-e39cf3fc478e h1:MV6KaVu/hzByHP0UvJ4HcMGE/8a6A4Rggc/0wx2AvJo=
github.com/timakin/bodyclose v0.0.0-20221125081123-e39cf3fc478e/go.mod h1:27bSVNWSBOHm+qRp1T9qzaIpsWEP6TbUnei/43HK+PQ=
github.com/tklauser/go-sysconf v0.3.9/go.mod h1:11DU/5sG7UexIrp/O6g35hrWzu0JxlwQ3LSFUzyeuhs=
Expand Down
2 changes: 2 additions & 0 deletions util/execdetails/execdetails.go
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,8 @@ const (
TpFKCheckRuntimeStats
// TpFKCascadeRuntimeStats is the tp for FKCascadeRuntimeStats
TpFKCascadeRuntimeStats
// TpRURuntimeStats is the tp for RURuntimeStats
TpRURuntimeStats
)

// RuntimeStats is used to express the executor runtime information.
Expand Down

0 comments on commit 55d2944

Please sign in to comment.