Skip to content

Commit 9fd6e81

Browse files
authored
topsql, pprof: use hex string but not binary sql/plan digest in goroutine label (#52216) (#52324)
close #52215
1 parent fde2234 commit 9fd6e81

File tree

4 files changed

+33
-19
lines changed

4 files changed

+33
-19
lines changed

pkg/util/cpuprofile/testutil/util.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package testutil
1616

1717
import (
1818
"context"
19+
"encoding/hex"
1920
"runtime/pprof"
2021
)
2122

@@ -24,7 +25,8 @@ func MockCPULoad(ctx context.Context, labels ...string) {
2425
lvs := []string{}
2526
for _, label := range labels {
2627
lvs = append(lvs, label)
27-
lvs = append(lvs, label+" value")
28+
val := hex.EncodeToString([]byte(label + " value"))
29+
lvs = append(lvs, val)
2830
// start goroutine with only 1 label.
2931
go mockCPULoadByGoroutineWithLabel(ctx, label, label+" value")
3032
}

pkg/util/topsql/collector/BUILD.bazel

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ go_library(
88
deps = [
99
"//pkg/util",
1010
"//pkg/util/cpuprofile",
11-
"//pkg/util/hack",
1211
"//pkg/util/logutil",
1312
"//pkg/util/topsql/state",
1413
"@com_github_google_pprof//profile",

pkg/util/topsql/collector/cpu.go

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ package collector
1616

1717
import (
1818
"context"
19+
"encoding/hex"
1920
"runtime/pprof"
2021
"sync"
2122
"time"
2223

2324
"github.com/google/pprof/profile"
2425
"github.com/pingcap/tidb/pkg/util"
2526
"github.com/pingcap/tidb/pkg/util/cpuprofile"
26-
"github.com/pingcap/tidb/pkg/util/hack"
2727
"github.com/pingcap/tidb/pkg/util/logutil"
2828
topsqlstate "github.com/pingcap/tidb/pkg/util/topsql/state"
2929
"go.uber.org/zap"
@@ -193,12 +193,25 @@ func (sp *SQLCPUCollector) parseCPUProfileBySQLLabels(p *profile.Profile) []SQLC
193193

194194
func (*SQLCPUCollector) createSQLStats(sqlMap map[string]*sqlStats) []SQLCPUTimeRecord {
195195
stats := make([]SQLCPUTimeRecord, 0, len(sqlMap))
196-
for sqlDigest, stmt := range sqlMap {
196+
for hexSQLDigest, stmt := range sqlMap {
197197
stmt.tune()
198-
for planDigest, val := range stmt.plans {
198+
199+
sqlDigest, err := hex.DecodeString(hexSQLDigest)
200+
if err != nil {
201+
logutil.BgLogger().Error("decode sql digest failed", zap.String("sqlDigest", hexSQLDigest), zap.Error(err))
202+
continue
203+
}
204+
205+
for hexPlanDigest, val := range stmt.plans {
206+
planDigest, err := hex.DecodeString(hexPlanDigest)
207+
if err != nil {
208+
logutil.BgLogger().Error("decode plan digest failed", zap.String("planDigest", hexPlanDigest), zap.Error(err))
209+
continue
210+
}
211+
199212
stats = append(stats, SQLCPUTimeRecord{
200-
SQLDigest: []byte(sqlDigest),
201-
PlanDigest: []byte(planDigest),
213+
SQLDigest: sqlDigest,
214+
PlanDigest: planDigest,
202215
CPUTimeMs: uint32(time.Duration(val).Milliseconds()),
203216
})
204217
}
@@ -255,12 +268,12 @@ func (s *sqlStats) tune() {
255268
}
256269

257270
// CtxWithSQLDigest wrap the ctx with sql digest.
258-
func CtxWithSQLDigest(ctx context.Context, sqlDigest []byte) context.Context {
259-
return pprof.WithLabels(ctx, pprof.Labels(labelSQLDigest, string(hack.String(sqlDigest))))
271+
func CtxWithSQLDigest(ctx context.Context, sqlDigest string) context.Context {
272+
return pprof.WithLabels(ctx, pprof.Labels(labelSQLDigest, sqlDigest))
260273
}
261274

262275
// CtxWithSQLAndPlanDigest wrap the ctx with sql digest and plan digest.
263-
func CtxWithSQLAndPlanDigest(ctx context.Context, sqlDigest, planDigest []byte) context.Context {
264-
return pprof.WithLabels(ctx, pprof.Labels(labelSQLDigest, string(hack.String(sqlDigest)),
265-
labelPlanDigest, string(hack.String(planDigest))))
276+
func CtxWithSQLAndPlanDigest(ctx context.Context, sqlDigest, planDigest string) context.Context {
277+
return pprof.WithLabels(ctx, pprof.Labels(labelSQLDigest, sqlDigest,
278+
labelPlanDigest, planDigest))
266279
}

pkg/util/topsql/topsql.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,11 @@ func RegisterPlan(normalizedPlan string, planDigest *parser.Digest) {
9797

9898
// AttachAndRegisterSQLInfo attach the sql information into Top SQL and register the SQL meta information.
9999
func AttachAndRegisterSQLInfo(ctx context.Context, normalizedSQL string, sqlDigest *parser.Digest, isInternal bool) context.Context {
100-
if sqlDigest == nil || len(sqlDigest.Bytes()) == 0 {
100+
if sqlDigest == nil || len(sqlDigest.String()) == 0 {
101101
return ctx
102102
}
103103
sqlDigestBytes := sqlDigest.Bytes()
104-
ctx = collector.CtxWithSQLDigest(ctx, sqlDigestBytes)
104+
ctx = collector.CtxWithSQLDigest(ctx, sqlDigest.String())
105105
pprof.SetGoroutineLabels(ctx)
106106

107107
linkSQLTextWithDigest(sqlDigestBytes, normalizedSQL, isInternal)
@@ -124,15 +124,15 @@ func AttachAndRegisterSQLInfo(ctx context.Context, normalizedSQL string, sqlDige
124124

125125
// AttachSQLAndPlanInfo attach the sql and plan information into Top SQL
126126
func AttachSQLAndPlanInfo(ctx context.Context, sqlDigest *parser.Digest, planDigest *parser.Digest) context.Context {
127-
if sqlDigest == nil || len(sqlDigest.Bytes()) == 0 {
127+
if sqlDigest == nil || len(sqlDigest.String()) == 0 {
128128
return ctx
129129
}
130-
var planDigestBytes []byte
131-
sqlDigestBytes := sqlDigest.Bytes()
130+
var planDigestStr string
131+
sqlDigestStr := sqlDigest.String()
132132
if planDigest != nil {
133-
planDigestBytes = planDigest.Bytes()
133+
planDigestStr = planDigest.String()
134134
}
135-
ctx = collector.CtxWithSQLAndPlanDigest(ctx, sqlDigestBytes, planDigestBytes)
135+
ctx = collector.CtxWithSQLAndPlanDigest(ctx, sqlDigestStr, planDigestStr)
136136
pprof.SetGoroutineLabels(ctx)
137137

138138
failpoint.Inject("mockHighLoadForEachPlan", func(val failpoint.Value) {

0 commit comments

Comments
 (0)