Skip to content

*: tiny cleanup LogSlowQuery #62989

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
30 changes: 8 additions & 22 deletions pkg/executor/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -1652,8 +1652,6 @@ func (a *ExecStmt) LogSlowQuery(txnTS uint64, succ bool, hasMoreResults bool) {
if (!enable || costTime < threshold) && !force {
return
}
sql := FormatSQL(a.GetTextToLog(true))
_, digest := stmtCtx.SQLDigest()

var indexNames string
if len(stmtCtx.IndexNames) > 0 {
Expand All @@ -1678,9 +1676,8 @@ func (a *ExecStmt) LogSlowQuery(txnTS uint64, succ bool, hasMoreResults bool) {

stmtDetail, tikvExecDetail, ruDetails := execdetails.GetExecDetailsFromContext(a.GoCtx)
execDetail := stmtCtx.GetExecDetails()
copTaskInfo := stmtCtx.CopTasksDetails()
memMax := sessVars.MemTracker.MaxConsumed()
diskMax := sessVars.DiskTracker.MaxConsumed()
sql := FormatSQL(a.GetTextToLog(true))
_, digest := stmtCtx.SQLDigest()
_, planDigest := GetPlanDigest(stmtCtx)

binaryPlan := ""
Expand Down Expand Up @@ -1708,15 +1705,11 @@ func (a *ExecStmt) LogSlowQuery(txnTS uint64, succ bool, hasMoreResults bool) {
SQL: sql.String(),
Digest: digest.String(),
TimeTotal: costTime,
TimeParse: sessVars.DurationParse,
TimeCompile: sessVars.DurationCompile,
TimeOptimize: sessVars.DurationOptimization,
TimeWaitTS: sessVars.DurationWaitTS,
IndexNames: indexNames,
CopTasks: copTaskInfo,
CopTasks: stmtCtx.CopTasksDetails(),
ExecDetail: execDetail,
MemMax: memMax,
DiskMax: diskMax,
MemMax: sessVars.MemTracker.MaxConsumed(),
DiskMax: sessVars.DiskTracker.MaxConsumed(),
Succ: succ,
Plan: getPlanTree(stmtCtx),
PlanDigest: planDigest.String(),
Expand All @@ -1736,9 +1729,7 @@ func (a *ExecStmt) LogSlowQuery(txnTS uint64, succ bool, hasMoreResults bool) {
IsSyncStatsFailed: stmtCtx.IsSyncStatsFailed,
Warnings: variable.CollectWarningsForSlowLog(stmtCtx),
ResourceGroupName: sessVars.StmtCtx.ResourceGroupName,
RRU: ruDetails.RRU(),
WRU: ruDetails.WRU(),
WaitRUDuration: ruDetails.RUWaitDuration(),
RUDetails: ruDetails,
CPUUsages: sessVars.SQLCPUUsages.GetCPUUsages(),
StorageKV: stmtCtx.IsTiKV.Load(),
StorageMPP: stmtCtx.IsTiFlash.Load(),
Expand Down Expand Up @@ -1790,7 +1781,7 @@ func (a *ExecStmt) LogSlowQuery(txnTS uint64, succ bool, hasMoreResults bool) {
Digest: digest.String(),
Start: sessVars.StartTime,
Duration: costTime,
Detail: stmtCtx.GetExecDetails(),
Detail: execDetail,
Succ: succ,
ConnID: sessVars.ConnectionID,
SessAlias: sessVars.SessionAlias,
Expand Down Expand Up @@ -1902,11 +1893,6 @@ func GetPlanDigest(stmtCtx *stmtctx.StatementContext) (string, *parser.Digest) {
return normalized, planDigest
}

// GetEncodedPlan returned same as getEncodedPlan
func GetEncodedPlan(stmtCtx *stmtctx.StatementContext, genHint bool) (encodedPlan, hintStr string) {
return getEncodedPlan(stmtCtx, genHint)
}

// getEncodedPlan gets the encoded plan, and generates the hint string if indicated.
func getEncodedPlan(stmtCtx *stmtctx.StatementContext, genHint bool) (encodedPlan, hintStr string) {
var hintSet bool
Expand Down Expand Up @@ -2329,7 +2315,7 @@ func sendPlanReplayerDumpTask(key replayer.PlanReplayerTaskKey, sctx sessionctx.
IsCapture: true,
IsContinuesCapture: isContinuesCapture,
}
dumpTask.EncodedPlan, _ = GetEncodedPlan(stmtCtx, false)
dumpTask.EncodedPlan, _ = getEncodedPlan(stmtCtx, false)
if execStmtAst, ok := stmtNode.(*ast.ExecuteStmt); ok {
planCacheStmt, err := plannercore.GetPreparedStmt(execStmtAst, sctx.GetSessionVars())
if err != nil {
Expand Down
13 changes: 6 additions & 7 deletions pkg/sessionctx/variable/session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,17 +284,18 @@ func TestSlowLogFormat(t *testing.T) {
WaitPDRespDuration: (11 * time.Second).Nanoseconds(),
BackoffDuration: (12 * time.Second).Nanoseconds(),
}
ruDetails := util.NewRUDetailsWith(50.0, 100.56, 134*time.Millisecond)
seVar.DurationParse = time.Duration(10)
seVar.DurationCompile = time.Duration(10)
seVar.DurationOptimization = time.Duration(10)
seVar.DurationWaitTS = time.Duration(3)
logItems := &variable.SlowQueryLogItems{
TxnTS: txnTS,
KeyspaceName: "keyspace_a",
KeyspaceID: 1,
SQL: sql,
Digest: digest.String(),
TimeTotal: costTime,
TimeParse: time.Duration(10),
TimeCompile: time.Duration(10),
TimeOptimize: time.Duration(10),
TimeWaitTS: time.Duration(3),
IndexNames: "[t1:a,t2:b]",
CopTasks: copTasks,
ExecDetail: execDetail,
Expand All @@ -319,9 +320,7 @@ func TestSlowLogFormat(t *testing.T) {
IsWriteCacheTable: true,
UsedStats: &stmtctx.UsedStatsInfo{},
ResourceGroupName: "rg1",
RRU: 50.0,
WRU: 100.56,
WaitRUDuration: 134 * time.Millisecond,
RUDetails: ruDetails,
StorageKV: true,
StorageMPP: false,
}
Expand Down
28 changes: 11 additions & 17 deletions pkg/sessionctx/variable/slow_log.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,10 +243,6 @@ type SlowQueryLogItems struct {
SQL string
Digest string
TimeTotal time.Duration
TimeParse time.Duration
TimeCompile time.Duration
TimeOptimize time.Duration
TimeWaitTS time.Duration
IndexNames string
Succ bool
IsExplicitTxn bool
Expand Down Expand Up @@ -274,9 +270,7 @@ type SlowQueryLogItems struct {
Warnings []JSONSQLWarnForSlowLog
// resource information
ResourceGroupName string
RRU float64
WRU float64
WaitRUDuration time.Duration
RUDetails *util.RUDetails
MemMax int64
DiskMax int64
CPUUsages ppcpuusage.CPUUsages
Expand Down Expand Up @@ -341,8 +335,8 @@ func (s *SessionVars) SlowLogFormat(logItems *SlowQueryLogItems) string {
buf.WriteString("\n")
}
writeSlowLogItem(&buf, SlowLogQueryTimeStr, strconv.FormatFloat(logItems.TimeTotal.Seconds(), 'f', -1, 64))
writeSlowLogItem(&buf, SlowLogParseTimeStr, strconv.FormatFloat(logItems.TimeParse.Seconds(), 'f', -1, 64))
writeSlowLogItem(&buf, SlowLogCompileTimeStr, strconv.FormatFloat(logItems.TimeCompile.Seconds(), 'f', -1, 64))
writeSlowLogItem(&buf, SlowLogParseTimeStr, strconv.FormatFloat(s.DurationParse.Seconds(), 'f', -1, 64))
writeSlowLogItem(&buf, SlowLogCompileTimeStr, strconv.FormatFloat(s.DurationCompile.Seconds(), 'f', -1, 64))

buf.WriteString(SlowLogRowPrefixStr + fmt.Sprintf("%v%v%v", SlowLogRewriteTimeStr,
SlowLogSpaceMarkStr, strconv.FormatFloat(logItems.RewriteInfo.DurationRewrite.Seconds(), 'f', -1, 64)))
Expand All @@ -352,8 +346,8 @@ func (s *SessionVars) SlowLogFormat(logItems *SlowQueryLogItems) string {
}
buf.WriteString("\n")

writeSlowLogItem(&buf, SlowLogOptimizeTimeStr, strconv.FormatFloat(logItems.TimeOptimize.Seconds(), 'f', -1, 64))
writeSlowLogItem(&buf, SlowLogWaitTSTimeStr, strconv.FormatFloat(logItems.TimeWaitTS.Seconds(), 'f', -1, 64))
writeSlowLogItem(&buf, SlowLogOptimizeTimeStr, strconv.FormatFloat(s.DurationOptimization.Seconds(), 'f', -1, 64))
writeSlowLogItem(&buf, SlowLogWaitTSTimeStr, strconv.FormatFloat(s.DurationWaitTS.Seconds(), 'f', -1, 64))

if execDetailStr := logItems.ExecDetail.String(); len(execDetailStr) > 0 {
buf.WriteString(SlowLogRowPrefixStr + execDetailStr + "\n")
Expand Down Expand Up @@ -482,14 +476,14 @@ func (s *SessionVars) SlowLogFormat(logItems *SlowQueryLogItems) string {
if logItems.ResourceGroupName != "" {
writeSlowLogItem(&buf, SlowLogResourceGroup, logItems.ResourceGroupName)
}
if logItems.RRU > 0.0 {
writeSlowLogItem(&buf, SlowLogRRU, strconv.FormatFloat(logItems.RRU, 'f', -1, 64))
if rru := logItems.RUDetails.RRU(); rru > 0.0 {
writeSlowLogItem(&buf, SlowLogRRU, strconv.FormatFloat(rru, 'f', -1, 64))
}
if logItems.WRU > 0.0 {
writeSlowLogItem(&buf, SlowLogWRU, strconv.FormatFloat(logItems.WRU, 'f', -1, 64))
if wru := logItems.RUDetails.WRU(); wru > 0.0 {
writeSlowLogItem(&buf, SlowLogWRU, strconv.FormatFloat(wru, 'f', -1, 64))
}
if logItems.WaitRUDuration > time.Duration(0) {
writeSlowLogItem(&buf, SlowLogWaitRUDuration, strconv.FormatFloat(logItems.WaitRUDuration.Seconds(), 'f', -1, 64))
if waitRUDuration := logItems.RUDetails.RUWaitDuration(); waitRUDuration > time.Duration(0) {
writeSlowLogItem(&buf, SlowLogWaitRUDuration, strconv.FormatFloat(waitRUDuration.Seconds(), 'f', -1, 64))
}
if logItems.CPUUsages.TidbCPUTime > time.Duration(0) {
writeSlowLogItem(&buf, SlowLogTidbCPUUsageDuration, strconv.FormatFloat(logItems.CPUUsages.TidbCPUTime.Seconds(), 'f', -1, 64))
Expand Down
3 changes: 2 additions & 1 deletion pkg/util/execdetails/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,10 @@ func GetExecDetailsFromContext(ctx context.Context) (stmtDetail StmtExecDetails,
if tikvExecDetailRaw != nil {
tikvExecDetail = *(tikvExecDetailRaw.(*util.ExecDetails))
}
ruDetails = util.NewRUDetails()
if ruDetailsVal := ctx.Value(util.RUDetailsCtxKey); ruDetailsVal != nil {
ruDetails = ruDetailsVal.(*util.RUDetails)
} else {
ruDetails = util.NewRUDetails()
}

return
Expand Down