Skip to content

Commit 3acb646

Browse files
authored
*: adapt TopSQL naming for TopProfiling (prepare for TopRU) (#65820)
close #65817
1 parent 06da9d7 commit 3acb646

File tree

19 files changed

+85
-62
lines changed

19 files changed

+85
-62
lines changed

cmd/tidb-server/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ func main() {
411411
executor.Stop()
412412
close(exited)
413413
})
414-
topsql.SetupTopSQL(keyspace.GetKeyspaceNameBytesBySettings(), svr)
414+
topsql.SetupTopProfiling(keyspace.GetKeyspaceNameBytesBySettings(), svr)
415415
terror.MustNil(svr.Run(dom))
416416
<-exited
417417
syncLog()

pkg/ddl/backfilling.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@ func (w *backfillWorker) run(d *ddlCtx, bf backfiller, job *model.Job) {
442442
return
443443
}
444444
curTaskID = task.id
445-
d.setDDLLabelForTopSQL(job.ID, job.Query)
445+
d.attachTopProfilingInfo(job.ID, job.Query)
446446

447447
logger.Debug("backfill worker got task", zap.Int("workerID", w.GetCtx().id), zap.Stringer("task", task))
448448
failpoint.Inject("mockBackfillRunErr", func() {

pkg/ddl/backfilling_dist_executor.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ func (s *backfillDistExecutor) newBackfillStepExecutor(
174174
switch stage {
175175
case proto.BackfillStepReadIndex:
176176
jc := ddlObj.jobContext(jobMeta.ID, jobMeta.ReorgMeta)
177-
ddlObj.setDDLLabelForTopSQL(jobMeta.ID, jobMeta.Query)
177+
ddlObj.attachTopProfilingInfo(jobMeta.ID, jobMeta.Query)
178178
ddlObj.setDDLSourceForDiagnosis(jobMeta.ID, jobMeta.Type)
179179
return newReadIndexExecutor(store, sessPool, ddlObj.etcdCli, jobMeta, indexInfos, tbl, jc, cloudStorageURI, estRowSize)
180180
case proto.BackfillStepMergeSort:

pkg/ddl/ddl.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -453,15 +453,15 @@ func (dc *ddlCtx) isOwner() bool {
453453
return isOwner
454454
}
455455

456-
func (dc *ddlCtx) setDDLLabelForTopSQL(jobID int64, jobQuery string) {
456+
func (dc *ddlCtx) attachTopProfilingInfo(jobID int64, jobQuery string) {
457457
dc.jobCtx.Lock()
458458
defer dc.jobCtx.Unlock()
459459
ctx, exists := dc.jobCtx.jobCtxMap[jobID]
460460
if !exists {
461461
ctx = NewReorgContext()
462462
dc.jobCtx.jobCtxMap[jobID] = ctx
463463
}
464-
ctx.setDDLLabelForTopSQL(jobQuery)
464+
ctx.attachTopProfilingInfo(jobQuery)
465465
}
466466

467467
func (dc *ddlCtx) setDDLSourceForDiagnosis(jobID int64, jobType model.ActionType) {

pkg/ddl/delete_range.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ func (dr *delRange) doTask(sctx sessionctx.Context, r util.DelRangeTask) error {
215215
dr.keys = dr.keys[:0]
216216
ctx := kv.WithInternalSourceType(context.Background(), kv.InternalTxnDDL)
217217
err := kv.RunInNewTxn(ctx, dr.store, false, func(_ context.Context, txn kv.Transaction) error {
218-
if topsqlstate.TopSQLEnabled() {
218+
if topsqlstate.TopProfilingEnabled() {
219219
// Only when TiDB run without PD(use unistore as storage for test) will run into here, so just set a mock internal resource tagger.
220220
txn.SetOption(kv.ResourceGroupTagger, util.GetInternalResourceGroupTaggerForTopSQL())
221221
}

pkg/ddl/index.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1571,7 +1571,7 @@ func checkIfTableReorgWorkCanSkip(
15711571
startTS := txn.StartTS()
15721572
ctx := NewReorgContext()
15731573
ctx.resourceGroupName = job.ReorgMeta.ResourceGroupName
1574-
ctx.setDDLLabelForTopSQL(job.Query)
1574+
ctx.attachTopProfilingInfo(job.Query)
15751575
if isEmpty, err := checkIfTableIsEmpty(ctx, store, tbl, startTS); err != nil || !isEmpty {
15761576
return false
15771577
}
@@ -1667,7 +1667,7 @@ func checkIfTempIndexReorgWorkCanSkip(
16671667
startTS := txn.StartTS()
16681668
ctx := NewReorgContext()
16691669
ctx.resourceGroupName = job.ReorgMeta.ResourceGroupName
1670-
ctx.setDDLLabelForTopSQL(job.Query)
1670+
ctx.attachTopProfilingInfo(job.Query)
16711671
firstIdxID := allIndexInfos[0].ID
16721672
lastIdxID := allIndexInfos[len(allIndexInfos)-1].ID
16731673
var globalIdxIDs []int64

pkg/ddl/job_worker.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -484,8 +484,8 @@ func finishRecoverSchema(w *worker, job *model.Job) error {
484484
return nil
485485
}
486486

487-
func (w *ReorgContext) setDDLLabelForTopSQL(jobQuery string) {
488-
if !topsqlstate.TopSQLEnabled() || jobQuery == "" {
487+
func (w *ReorgContext) attachTopProfilingInfo(jobQuery string) {
488+
if !topsqlstate.TopProfilingEnabled() || jobQuery == "" {
489489
return
490490
}
491491

@@ -563,7 +563,7 @@ func (w *worker) prepareTxn(job *model.Job) (kv.Transaction, error) {
563563
if w.tp != generalWorker && job.IsRunning() {
564564
txn.SetDiskFullOpt(kvrpcpb.DiskFullOpt_NotAllowedOnFull)
565565
}
566-
w.setDDLLabelForTopSQL(job.ID, job.Query)
566+
w.attachTopProfilingInfo(job.ID, job.Query)
567567
w.setDDLSourceForDiagnosis(job.ID, job.Type)
568568
jobContext := w.jobContext(job.ID, job.ReorgMeta)
569569
if tagger := w.getResourceGroupTaggerForTopSQL(job.ID); tagger != nil {

pkg/executor/adapter.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ func (a *ExecStmt) PointGet(ctx context.Context) (*recordSet, error) {
365365
sessiontxn.AssertTxnManagerInfoSchema(a.Ctx, a.InfoSchema)
366366
})
367367

368-
ctx = a.observeStmtBeginForTopSQL(ctx)
368+
ctx = a.observeStmtBeginForTopProfiling(ctx)
369369
startTs, err := sessiontxn.GetTxnManager(a.Ctx).GetStmtReadTS()
370370
if err != nil {
371371
return nil, err
@@ -638,7 +638,7 @@ func (a *ExecStmt) Exec(ctx context.Context) (_ sqlexec.RecordSet, err error) {
638638
stmtCtx.ResourceGroupName = switchGroupName
639639
}
640640
}
641-
ctx = a.observeStmtBeginForTopSQL(ctx)
641+
ctx = a.observeStmtBeginForTopProfiling(ctx)
642642

643643
// Record start time before buildExecutor() to include TSO waiting time in maxExecutionTime timeout.
644644
// buildExecutor() may block waiting for TSO, so we should start the timer earlier.
@@ -1520,7 +1520,7 @@ func (a *ExecStmt) FinishExecuteStmt(txnTS uint64, err error, hasMoreResults boo
15201520
// `LowSlowQuery` and `SummaryStmt` must be called before recording `PrevStmt`.
15211521
a.LogSlowQuery(txnTS, succ, hasMoreResults)
15221522
a.SummaryStmt(succ)
1523-
a.observeStmtFinishedForTopSQL()
1523+
a.observeStmtFinishedForTopProfiling()
15241524
a.UpdatePlanCacheRuntimeInfo()
15251525
if sessVars.StmtCtx.IsTiFlash.Load() {
15261526
if succ {
@@ -2152,10 +2152,10 @@ func (a *ExecStmt) updatePrevStmt() {
21522152
}
21532153
}
21542154

2155-
func (a *ExecStmt) observeStmtBeginForTopSQL(ctx context.Context) context.Context {
2156-
if !topsqlstate.TopSQLEnabled() && IsFastPlan(a.Plan) {
2155+
func (a *ExecStmt) observeStmtBeginForTopProfiling(ctx context.Context) context.Context {
2156+
if !topsqlstate.TopProfilingEnabled() && IsFastPlan(a.Plan) {
21572157
// To reduce the performance impact on fast plan.
2158-
// Drop them does not cause notable accuracy issue in TopSQL.
2158+
// Drop them does not cause notable accuracy issue in Top Profiling.
21592159
return ctx
21602160
}
21612161

@@ -2171,8 +2171,9 @@ func (a *ExecStmt) observeStmtBeginForTopSQL(ctx context.Context) context.Contex
21712171
planDigestByte = planDigest.Bytes()
21722172
}
21732173
stats := a.Ctx.GetStmtStats()
2174-
if !topsqlstate.TopSQLEnabled() {
2175-
// Always attach the SQL and plan info uses to catch the running SQL when Top SQL is enabled in execution.
2174+
if !topsqlstate.TopProfilingEnabled() {
2175+
// Always attach the SQL and plan info uses to catch the running SQL when Top Profiling is enabled in execution.
2176+
// Note: Goroutine labels for CPU profiling are only set when TopSQL is enabled.
21762177
if stats != nil {
21772178
stats.OnExecutionBegin(sqlDigestByte, planDigestByte, vars.InPacketBytes.Load())
21782179
}
@@ -2222,12 +2223,12 @@ func (a *ExecStmt) UpdatePlanCacheRuntimeInfo() {
22222223
a.Ctx.GetSessionVars().PlanCacheValue = nil // reset
22232224
}
22242225

2225-
func (a *ExecStmt) observeStmtFinishedForTopSQL() {
2226+
func (a *ExecStmt) observeStmtFinishedForTopProfiling() {
22262227
vars := a.Ctx.GetSessionVars()
22272228
if vars == nil {
22282229
return
22292230
}
2230-
if stats := a.Ctx.GetStmtStats(); stats != nil && topsqlstate.TopSQLEnabled() {
2231+
if stats := a.Ctx.GetStmtStats(); stats != nil && topsqlstate.TopProfilingEnabled() {
22312232
sqlDigest, planDigest := a.getSQLPlanDigest()
22322233
execDuration := vars.GetTotalCostDuration()
22332234
stats.OnExecutionFinished(sqlDigest, planDigest, execDuration, vars.OutPacketBytes.Load())

pkg/executor/internal/exec/executor.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ type Executor interface {
5555
RuntimeStats() *execdetails.BasicRuntimeStats
5656

5757
HandleSQLKillerSignal() error
58-
RegisterSQLAndPlanInExecForTopSQL()
58+
RegisterSQLAndPlanInExecForTopProfiling()
5959

6060
AllChildren() []Executor
6161
SetAllChildren([]Executor)
@@ -248,9 +248,9 @@ func (e *executorStats) RuntimeStats() *execdetails.BasicRuntimeStats {
248248
return e.runtimeStats
249249
}
250250

251-
// RegisterSQLAndPlanInExecForTopSQL registers the current SQL and Plan on top sql
252-
func (e *executorStats) RegisterSQLAndPlanInExecForTopSQL() {
253-
if topsqlstate.TopSQLEnabled() && e.isSQLAndPlanRegistered.CompareAndSwap(false, true) {
251+
// RegisterSQLAndPlanInExecForTopProfiling registers the current SQL and Plan on top profiling.
252+
func (e *executorStats) RegisterSQLAndPlanInExecForTopProfiling() {
253+
if topsqlstate.TopProfilingEnabled() && e.isSQLAndPlanRegistered.CompareAndSwap(false, true) {
254254
topsql.RegisterSQL(e.normalizedSQL, e.sqlDigest, e.inRestrictedSQL)
255255
if len(e.normalizedPlan) > 0 {
256256
topsql.RegisterPlan(e.normalizedPlan, e.planDigest)
@@ -456,7 +456,7 @@ func Next(ctx context.Context, e Executor, req *chunk.Chunk) (err error) {
456456
r, ctx := tracing.StartRegionEx(ctx, reflect.TypeOf(e).String()+".Next")
457457
defer r.End()
458458

459-
e.RegisterSQLAndPlanInExecForTopSQL()
459+
e.RegisterSQLAndPlanInExecForTopProfiling()
460460
err = e.Next(ctx, req)
461461

462462
if err != nil {

pkg/executor/prepared.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ func (e *PrepareExec) Next(ctx context.Context, _ *chunk.Chunk) error {
144144
if err != nil {
145145
return err
146146
}
147-
if topsqlstate.TopSQLEnabled() {
147+
if topsqlstate.TopProfilingEnabled() {
148148
e.Ctx().GetSessionVars().StmtCtx.IsSQLRegistered.Store(true)
149149
topsql.AttachAndRegisterSQLInfo(ctx, stmt.NormalizedSQL, stmt.SQLDigest, vars.InRestrictedSQL)
150150
}

0 commit comments

Comments
 (0)