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

session: refine record execution duration metric (#16453) #16561

Merged
merged 5 commits into from
May 20, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
32 changes: 26 additions & 6 deletions executor/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,12 +148,7 @@ func (a *recordSet) NewChunk() *chunk.Chunk {

func (a *recordSet) Close() error {
err := a.executor.Close()
// `LowSlowQuery` and `SummaryStmt` must be called before recording `PrevStmt`.
a.stmt.LogSlowQuery(a.txnStartTS, a.lastErr == nil, false)
a.stmt.SummaryStmt(a.lastErr == nil)
sessVars := a.stmt.Ctx.GetSessionVars()
pps := types.CloneRow(sessVars.PreparedParams)
sessVars.PrevStmt = FormatSQL(a.stmt.OriginText(), pps)
a.stmt.FinishExecuteStmt(a.txnStartTS, a.lastErr == nil, false)
a.stmt.logAudit()
// Detach the disk tracker from GlobalDiskUsageTracker after every execution
if stmtCtx := a.stmt.Ctx.GetSessionVars().StmtCtx; stmtCtx != nil && stmtCtx.DiskTracker != nil {
Expand Down Expand Up @@ -772,6 +767,31 @@ func FormatSQL(sql string, pps variable.PreparedParams) stringutil.StringerFunc
}
}

var (
sessionExecuteRunDurationInternal = metrics.SessionExecuteRunDuration.WithLabelValues(metrics.LblInternal)
sessionExecuteRunDurationGeneral = metrics.SessionExecuteRunDuration.WithLabelValues(metrics.LblGeneral)
)

// FinishExecuteStmt is used to record some information after `ExecStmt` execution finished:
// 1. record slow log if needed.
// 2. record summary statement.
// 3. record execute duration metric.
// 4. update the `PrevStmt` in session variable.
func (a *ExecStmt) FinishExecuteStmt(txnTS uint64, succ bool, hasMoreResults bool) {
// `LowSlowQuery` and `SummaryStmt` must be called before recording `PrevStmt`.
a.LogSlowQuery(txnTS, succ, hasMoreResults)
a.SummaryStmt(succ)
sessVars := a.Ctx.GetSessionVars()
pps := types.CloneRow(sessVars.PreparedParams)
sessVars.PrevStmt = FormatSQL(a.OriginText(), pps)
executeDuration := time.Since(sessVars.StartTime) - sessVars.DurationCompile
if sessVars.InRestrictedSQL {
sessionExecuteRunDurationInternal.Observe(executeDuration.Seconds())
} else {
sessionExecuteRunDurationGeneral.Observe(executeDuration.Seconds())
}
}

// LogSlowQuery is used to print the slow query in the log files.
func (a *ExecStmt) LogSlowQuery(txnTS uint64, succ bool, hasMoreResults bool) {
sessVars := a.Ctx.GetSessionVars()
Expand Down
110 changes: 101 additions & 9 deletions session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,6 @@ var (
transactionDurationGeneralCommit = metrics.TransactionDuration.WithLabelValues(metrics.LblGeneral, metrics.LblCommit)
transactionDurationGeneralAbort = metrics.TransactionDuration.WithLabelValues(metrics.LblGeneral, metrics.LblAbort)

sessionExecuteRunDurationInternal = metrics.SessionExecuteRunDuration.WithLabelValues(metrics.LblInternal)
sessionExecuteRunDurationGeneral = metrics.SessionExecuteRunDuration.WithLabelValues(metrics.LblGeneral)

sessionExecuteCompileDurationInternal = metrics.SessionExecuteCompileDuration.WithLabelValues(metrics.LblInternal)
sessionExecuteCompileDurationGeneral = metrics.SessionExecuteCompileDuration.WithLabelValues(metrics.LblGeneral)
sessionExecuteParseDurationInternal = metrics.SessionExecuteParseDuration.WithLabelValues(metrics.LblInternal)
Expand Down Expand Up @@ -1033,7 +1030,6 @@ func (s *session) SetProcessInfo(sql string, t time.Time, command byte, maxExecu

func (s *session) executeStatement(ctx context.Context, connID uint64, stmtNode ast.StmtNode, stmt sqlexec.Statement, recordSets []sqlexec.RecordSet, inMulitQuery bool) ([]sqlexec.RecordSet, error) {
logStmt(stmtNode, s.sessionVars)
startTime := time.Now()
recordSet, err := runStmt(ctx, s, stmt)
if err != nil {
if !kv.ErrKeyExists.Equal(err) {
Expand All @@ -1044,11 +1040,6 @@ func (s *session) executeStatement(ctx context.Context, connID uint64, stmtNode
}
return nil, err
}
if s.isInternal() {
sessionExecuteRunDurationInternal.Observe(time.Since(startTime).Seconds())
} else {
sessionExecuteRunDurationGeneral.Observe(time.Since(startTime).Seconds())
}

if inMulitQuery && recordSet == nil {
recordSet = &multiQueryNoDelayRecordSet{
Expand Down Expand Up @@ -1088,6 +1079,96 @@ func (s *session) Execute(ctx context.Context, sql string) (recordSets []sqlexec
return
}

<<<<<<< HEAD
=======
// Parse parses a query string to raw ast.StmtNode.
func (s *session) Parse(ctx context.Context, sql string) ([]ast.StmtNode, error) {
charsetInfo, collation := s.sessionVars.GetCharsetInfo()
parseStartTime := time.Now()
stmts, warns, err := s.ParseSQL(ctx, sql, charsetInfo, collation)
if err != nil {
s.rollbackOnError(ctx)

// Only print log message when this SQL is from the user.
// Mute the warning for internal SQLs.
if !s.sessionVars.InRestrictedSQL {
logutil.Logger(ctx).Warn("parse SQL failed", zap.Error(err), zap.String("SQL", sql))
}
return nil, util.SyntaxError(err)
}

durParse := time.Since(parseStartTime)
s.GetSessionVars().DurationParse = durParse
isInternal := s.isInternal()
if isInternal {
sessionExecuteParseDurationInternal.Observe(durParse.Seconds())
} else {
sessionExecuteParseDurationGeneral.Observe(durParse.Seconds())
}
for _, warn := range warns {
s.sessionVars.StmtCtx.AppendWarning(util.SyntaxWarn(warn))
}
return stmts, nil
}

func (s *session) ExecuteStmt(ctx context.Context, stmtNode ast.StmtNode) (sqlexec.RecordSet, error) {
if span := opentracing.SpanFromContext(ctx); span != nil && span.Tracer() != nil {
span1 := span.Tracer().StartSpan("session.ExecuteStmt", opentracing.ChildOf(span.Context()))
defer span1.Finish()
ctx = opentracing.ContextWithSpan(ctx, span1)
}

s.PrepareTxnCtx(ctx)
err := s.loadCommonGlobalVariablesIfNeeded()
if err != nil {
return nil, err
}

s.sessionVars.StartTime = time.Now()

// Some executions are done in compile stage, so we reset them before compile.
if err := executor.ResetContextOfStmt(s, stmtNode); err != nil {
return nil, err
}

// Transform abstract syntax tree to a physical plan(stored in executor.ExecStmt).
compiler := executor.Compiler{Ctx: s}
stmt, err := compiler.Compile(ctx, stmtNode)
if err != nil {
s.rollbackOnError(ctx)

// Only print log message when this SQL is from the user.
// Mute the warning for internal SQLs.
if !s.sessionVars.InRestrictedSQL {
logutil.Logger(ctx).Warn("compile SQL failed", zap.Error(err), zap.String("SQL", stmtNode.Text()))
}
return nil, err
}
durCompile := time.Since(s.sessionVars.StartTime)
s.GetSessionVars().DurationCompile = durCompile
if s.isInternal() {
sessionExecuteCompileDurationInternal.Observe(durCompile.Seconds())
} else {
sessionExecuteCompileDurationGeneral.Observe(durCompile.Seconds())
}
s.currentPlan = stmt.Plan

// Execute the physical plan.
logStmt(stmtNode, s.sessionVars)
recordSet, err := runStmt(ctx, s, stmt)
if err != nil {
if !kv.ErrKeyExists.Equal(err) {
logutil.Logger(ctx).Warn("run statement failed",
zap.Int64("schemaVersion", s.sessionVars.TxnCtx.SchemaVersion),
zap.Error(err),
zap.String("session", s.String()))
}
return nil, err
}
return recordSet, nil
}

>>>>>>> 7106b18... session: refine record execution duration metric (#16453)
func (s *session) execute(ctx context.Context, sql string) (recordSets []sqlexec.RecordSet, err error) {
s.PrepareTxnCtx(ctx)
connID := s.sessionVars.ConnectionID
Expand Down Expand Up @@ -1210,6 +1291,10 @@ func (s *session) CommonExec(ctx context.Context,
if err != nil {
return nil, err
}
<<<<<<< HEAD
=======
sessionExecuteCompileDurationGeneral.Observe(time.Since(s.sessionVars.StartTime).Seconds())
>>>>>>> 7106b18... session: refine record execution duration metric (#16453)
logQuery(st.OriginText(), s.sessionVars)
return runStmt(ctx, s, st)
}
Expand Down Expand Up @@ -1239,7 +1324,14 @@ func (s *session) CachedPlanExec(ctx context.Context,
OutputNames: execPlan.OutputNames(),
PsStmt: prepareStmt,
}
<<<<<<< HEAD
s.GetSessionVars().DurationCompile = time.Since(s.sessionVars.StartTime)
=======
compileDuration := time.Since(s.sessionVars.StartTime)
sessionExecuteCompileDurationGeneral.Observe(compileDuration.Seconds())
s.GetSessionVars().DurationCompile = compileDuration

>>>>>>> 7106b18... session: refine record execution duration metric (#16453)
stmt.Text = prepared.Stmt.Text()
stmtCtx.OriginalSQL = stmt.Text
stmtCtx.InitSQLDigest(prepareStmt.NormalizedSQL, prepareStmt.SQLDigest)
Expand Down
7 changes: 1 addition & 6 deletions session/tidb.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ import (
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/sessionctx"
"github.com/pingcap/tidb/sessionctx/variable"
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/util"
"github.com/pingcap/tidb/util/chunk"
"github.com/pingcap/tidb/util/logutil"
Expand Down Expand Up @@ -266,11 +265,7 @@ func runStmt(ctx context.Context, sctx sessionctx.Context, s sqlexec.Statement)
// If it is not a select statement, we record its slow log here,
// then it could include the transaction commit time.
if rs == nil {
// `LowSlowQuery` and `SummaryStmt` must be called before recording `PrevStmt`.
s.(*executor.ExecStmt).LogSlowQuery(origTxnCtx.StartTS, err == nil, false)
s.(*executor.ExecStmt).SummaryStmt(err == nil)
pps := types.CloneRow(sessVars.PreparedParams)
sessVars.PrevStmt = executor.FormatSQL(s.OriginText(), pps)
s.(*executor.ExecStmt).FinishExecuteStmt(origTxnCtx.StartTS, err == nil, false)
}
}()

Expand Down