Skip to content

Commit

Permalink
*: fix incorrect handling of EXECUTE stmt in plan replayer capture (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
time-and-fate authored May 10, 2023
1 parent f803438 commit 5f92a50
Show file tree
Hide file tree
Showing 8 changed files with 325 additions and 23 deletions.
1 change: 0 additions & 1 deletion domain/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ go_library(
"//meta",
"//metrics",
"//owner",
"//parser",
"//parser/ast",
"//parser/model",
"//parser/mysql",
Expand Down
18 changes: 1 addition & 17 deletions domain/plan_replayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import (
domain_metrics "github.com/pingcap/tidb/domain/metrics"
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/metrics"
"github.com/pingcap/tidb/parser"
"github.com/pingcap/tidb/parser/ast"
"github.com/pingcap/tidb/parser/terror"
"github.com/pingcap/tidb/sessionctx"
Expand Down Expand Up @@ -445,19 +444,6 @@ func (w *planReplayerTaskDumpWorker) HandleTask(task *PlanReplayerDumpTask) (suc
}
task.Zf = file
task.FileName = fileName
if task.InExecute && len(task.NormalizedSQL) > 0 {
p := parser.New()
stmts, _, err := p.ParseSQL(task.NormalizedSQL)
if err != nil {
logutil.BgLogger().Warn("[plan-replayer-capture] parse normalized sql failed",
zap.String("sql", task.NormalizedSQL),
zap.String("sqlDigest", taskKey.SQLDigest),
zap.String("planDigest", taskKey.PlanDigest),
zap.Error(err))
return false
}
task.ExecStmts = stmts
}
err = DumpPlanReplayerInfo(w.ctx, w.sctx, task)
if err != nil {
logutil.BgLogger().Warn("[plan-replayer-capture] dump task result failed",
Expand Down Expand Up @@ -551,9 +537,7 @@ type PlanReplayerDumpTask struct {
replayer.PlanReplayerTaskKey

// tmp variables stored during the query
TblStats map[int64]interface{}
InExecute bool
NormalizedSQL string
TblStats map[int64]interface{}

// variables used to dump the plan
StartTS uint64
Expand Down
14 changes: 10 additions & 4 deletions executor/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2025,10 +2025,16 @@ func sendPlanReplayerDumpTask(key replayer.PlanReplayerTaskKey, sctx sessionctx.
IsContinuesCapture: isContinuesCapture,
}
dumpTask.EncodedPlan, _ = GetEncodedPlan(stmtCtx, false)
if _, ok := stmtNode.(*ast.ExecuteStmt); ok {
nsql, _ := sctx.GetSessionVars().StmtCtx.SQLDigest()
dumpTask.InExecute = true
dumpTask.NormalizedSQL = nsql
if execStmtAst, ok := stmtNode.(*ast.ExecuteStmt); ok {
planCacheStmt, err := plannercore.GetPreparedStmt(execStmtAst, sctx.GetSessionVars())
if err != nil {
logutil.BgLogger().Warn("[plan-replayer-capture] fail to find prepared ast for dumping plan replayer",
zap.String("sqlDigest", key.SQLDigest),
zap.String("planDigest", key.PlanDigest),
zap.Error(err))
} else {
dumpTask.ExecStmts = []ast.StmtNode{planCacheStmt.PreparedAst.Stmt}
}
}
domain.GetDomain(sctx).GetPlanReplayerHandle().SendTask(dumpTask)
}
9 changes: 9 additions & 0 deletions planner/core/plan_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/pingcap/tidb/parser/ast"
"github.com/pingcap/tidb/parser/mysql"
core_metrics "github.com/pingcap/tidb/planner/core/metrics"
"github.com/pingcap/tidb/planner/util/debugtrace"
"github.com/pingcap/tidb/privilege"
"github.com/pingcap/tidb/sessionctx"
"github.com/pingcap/tidb/sessionctx/stmtctx"
Expand Down Expand Up @@ -65,6 +66,14 @@ func SetParameterValuesIntoSCtx(sctx sessionctx.Context, isNonPrep bool, markers
}
vars.PlanCacheParams.Append(val)
}
if vars.StmtCtx.EnableOptimizerDebugTrace && len(vars.PlanCacheParams.AllParamValues()) > 0 {
vals := vars.PlanCacheParams.AllParamValues()
valStrs := make([]string, len(vals))
for i, val := range vals {
valStrs[i] = val.String()
}
debugtrace.RecordAnyValuesWithNames(sctx, "Parameter datums for EXECUTE", valStrs)
}
vars.PlanCacheParams.SetForNonPrepCache(isNonPrep)
return nil
}
Expand Down
1 change: 1 addition & 0 deletions server/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ go_test(
"//util/deadlockhistory",
"//util/mock",
"//util/plancodec",
"//util/replayer",
"//util/resourcegrouptag",
"//util/rowcodec",
"//util/stmtsummary/v2:stmtsummary",
Expand Down
14 changes: 14 additions & 0 deletions server/optimize_trace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"database/sql"
"encoding/json"
"net/http"
"os"
"path/filepath"
"testing"

Expand All @@ -31,6 +32,7 @@ import (
"github.com/pingcap/tidb/testkit/testdata"
"github.com/pingcap/tidb/util/arena"
"github.com/pingcap/tidb/util/chunk"
"github.com/pingcap/tidb/util/replayer"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -131,7 +133,10 @@ func TestOptimizerDebugTrace(t *testing.T) {

tk.MustExec("plan replayer capture '0595c79f25d183319d0830ff8ca538c9054cbf407e5e27488b5dc40e4738a7c8' '*'")
tk.MustExec("plan replayer capture 'c0fcc0abbaaffcaafe21115a3c67ae5d96a188cc197559953d2865ea6852d3cc' '*'")
tk.MustExec("plan replayer capture '58fcbdd56a722c02225488c89a782cd2d626f8219c8ef8f57cd3bcdb6eb7c1b2' '*'")
require.NoError(t, cc.handleStmtPrepare(ctx, "select sum(col1) from t where col1 < ? and col1 > 100"))
tk.MustExec("prepare stmt from 'select * from t where col1 in (?, 2, 3)'")
tk.MustExec("set @a = 1")
var (
in []string
out []interface{}
Expand All @@ -152,4 +157,13 @@ func TestOptimizerDebugTrace(t *testing.T) {
})
require.Equal(t, out[i], res, cmdString)
}

prHandle := dom.GetPlanReplayerHandle()
worker := prHandle.GetWorker()
for i := 0; i < 3; i++ {
task := prHandle.DrainTask()
success := worker.HandleTask(task)
require.True(t, success)
require.NoError(t, os.Remove(filepath.Join(replayer.GetPlanReplayerDirName(), task.FileName)))
}
}
3 changes: 2 additions & 1 deletion server/testdata/optimizer_suite_in.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"cases": [
"\u0017\u0001\u0000\u0000\u0000\u0000\u0001\u0000\u0000\u0000\u0000\u0001\u0008\u0000\u007F\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000",
"\u0003create session binding for select * from t where col1 = 100 using select * from t use index () where col1 = 100",
"\u0003select * from t where col1 = 100"
"\u0003select * from t where col1 = 100",
"\u0003execute stmt using @a"
]
}
]
Loading

0 comments on commit 5f92a50

Please sign in to comment.