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

executor: add the slow log for commit #7951

Merged
merged 6 commits into from
Oct 18, 2018
Merged
Show file tree
Hide file tree
Changes from 5 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
18 changes: 10 additions & 8 deletions executor/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func (a *recordSet) NewChunk() *chunk.Chunk {

func (a *recordSet) Close() error {
err := a.executor.Close()
a.stmt.logSlowQuery(a.txnStartTS, a.lastErr == nil)
a.stmt.LogSlowQuery(a.txnStartTS, a.lastErr == nil)
return errors.Trace(err)
}

Expand All @@ -137,8 +137,9 @@ type ExecStmt struct {

StmtNode ast.StmtNode

Ctx sessionctx.Context
startTime time.Time
Ctx sessionctx.Context
// StartTime stands for the starting time when executing the statement.
StartTime time.Time
isPreparedStmt bool
}

Expand Down Expand Up @@ -184,7 +185,7 @@ func (a *ExecStmt) RebuildPlan() (int64, error) {
// like the INSERT, UPDATE statements, it executes in this function, if the Executor returns
// result, execution is done after this function returns, in the returned ast.RecordSet Next method.
func (a *ExecStmt) Exec(ctx context.Context) (ast.RecordSet, error) {
a.startTime = time.Now()
a.StartTime = time.Now()
sctx := a.Ctx
if _, ok := a.Plan.(*plannercore.Analyze); ok && sctx.GetSessionVars().InRestrictedSQL {
oriStats, _ := sctx.GetSessionVars().GetSystemVar(variable.TiDBBuildStatsConcurrency)
Expand Down Expand Up @@ -264,7 +265,7 @@ func (a *ExecStmt) handleNoDelayExecutor(ctx context.Context, sctx sessionctx.Co
if sctx.Txn() != nil {
txnTS = sctx.Txn().StartTS()
}
a.logSlowQuery(txnTS, err == nil)
a.LogSlowQuery(txnTS, err == nil)
}()

err = e.Next(ctx, e.newFirstChunk())
Expand Down Expand Up @@ -329,13 +330,14 @@ func (a *ExecStmt) buildExecutor(ctx sessionctx.Context) (Executor, error) {
// QueryReplacer replaces new line and tab for grep result including query string.
var QueryReplacer = strings.NewReplacer("\r", " ", "\n", " ", "\t", " ")

func (a *ExecStmt) logSlowQuery(txnTS uint64, succ bool) {
// LogSlowQuery is used to print the slow query in the log files.
func (a *ExecStmt) LogSlowQuery(txnTS uint64, succ bool) {
level := log.GetLevel()
if level < log.WarnLevel {
return
}
cfg := config.GetGlobalConfig()
costTime := time.Since(a.startTime)
costTime := time.Since(a.StartTime)
threshold := time.Duration(cfg.Log.SlowThreshold) * time.Millisecond
if costTime < threshold && level < log.DebugLevel {
return
Expand Down Expand Up @@ -385,7 +387,7 @@ func (a *ExecStmt) logSlowQuery(txnTS uint64, succ bool) {
}
domain.GetDomain(a.Ctx).LogSlowQuery(&domain.SlowQueryInfo{
SQL: sql,
Start: a.startTime,
Start: a.StartTime,
Duration: costTime,
Detail: sessVars.StmtCtx.GetExecDetails(),
Succ: succ,
Expand Down
6 changes: 6 additions & 0 deletions session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,13 @@ func (s *session) doCommitWithRetry(ctx context.Context) error {
}

func (s *session) CommitTxn(ctx context.Context) error {
stmt := executor.ExecStmt{
Text: "commit",
Ctx: s,
StartTime: time.Now(),
}
err := s.doCommitWithRetry(ctx)
stmt.LogSlowQuery(s.sessionVars.TxnCtx.StartTS, err == nil)
label := metrics.LblOK
if err != nil {
label = metrics.LblError
Expand Down