From 713e6994db876fad85684647c854dbcb4baf2ceb Mon Sep 17 00:00:00 2001 From: Chengpeng Yan <41809508+Reminiscent@users.noreply.github.com> Date: Wed, 22 Dec 2021 18:59:48 +0800 Subject: [PATCH] planner: rename pstmtPlanCacheXX to PlanCacheXX (#30909) --- executor/prepared.go | 2 +- planner/core/cache.go | 26 +++++++++++++++----------- planner/core/cache_test.go | 2 +- planner/core/common_plans.go | 18 +++++++++--------- server/driver_tidb.go | 2 +- session/session.go | 2 +- 6 files changed, 28 insertions(+), 24 deletions(-) diff --git a/executor/prepared.go b/executor/prepared.go index 3b703c75a9cf5..4f63bce491ab9 100644 --- a/executor/prepared.go +++ b/executor/prepared.go @@ -317,7 +317,7 @@ func (e *DeallocateExec) Next(ctx context.Context, req *chunk.Chunk) error { delete(vars.PreparedStmtNameToID, e.Name) if plannercore.PreparedPlanCacheEnabled() { bindSQL := planner.GetBindSQL4PlanCache(e.ctx, prepared.Stmt) - e.ctx.PreparedPlanCache().Delete(plannercore.NewPSTMTPlanCacheKey( + e.ctx.PreparedPlanCache().Delete(plannercore.NewPlanCacheKey( vars, id, prepared.SchemaVersion, bindSQL, )) } diff --git a/planner/core/cache.go b/planner/core/cache.go index 4113f3e911e88..2ef974340c063 100644 --- a/planner/core/cache.go +++ b/planner/core/cache.go @@ -66,7 +66,11 @@ func PreparedPlanCacheEnabled() bool { return isEnabled == preparedPlanCacheEnabled } -type pstmtPlanCacheKey struct { +// planCacheKey is used to access Plan Cache. We put some variables that do not affect the plan into planCacheKey, such as the sql text. +// Put the parameters that may affect the plan in planCacheValue, such as bindSQL. +// However, due to some compatibility reasons, we will temporarily keep some system variable-related values in planCacheKey. +// At the same time, because these variables have a small impact on plan, we will move them to PlanCacheValue later if necessary. +type planCacheKey struct { database string connID uint64 pstmtID uint32 @@ -81,7 +85,7 @@ type pstmtPlanCacheKey struct { } // Hash implements Key interface. -func (key *pstmtPlanCacheKey) Hash() []byte { +func (key *planCacheKey) Hash() []byte { if len(key.hash) == 0 { var ( dbBytes = hack.Slice(key.database) @@ -114,7 +118,7 @@ func (key *pstmtPlanCacheKey) Hash() []byte { // SetPstmtIDSchemaVersion implements PstmtCacheKeyMutator interface to change pstmtID and schemaVersion of cacheKey. // so we can reuse Key instead of new every time. func SetPstmtIDSchemaVersion(key kvcache.Key, pstmtID uint32, schemaVersion int64, isolationReadEngines map[kv.StoreType]struct{}) { - psStmtKey, isPsStmtKey := key.(*pstmtPlanCacheKey) + psStmtKey, isPsStmtKey := key.(*planCacheKey) if !isPsStmtKey { return } @@ -127,13 +131,13 @@ func SetPstmtIDSchemaVersion(key kvcache.Key, pstmtID uint32, schemaVersion int6 psStmtKey.hash = psStmtKey.hash[:0] } -// NewPSTMTPlanCacheKey creates a new pstmtPlanCacheKey object. -func NewPSTMTPlanCacheKey(sessionVars *variable.SessionVars, pstmtID uint32, schemaVersion int64, bindSQL string) kvcache.Key { +// NewPlanCacheKey creates a new planCacheKey object. +func NewPlanCacheKey(sessionVars *variable.SessionVars, pstmtID uint32, schemaVersion int64, bindSQL string) kvcache.Key { timezoneOffset := 0 if sessionVars.TimeZone != nil { _, timezoneOffset = time.Now().In(sessionVars.TimeZone).Zone() } - key := &pstmtPlanCacheKey{ + key := &planCacheKey{ database: sessionVars.CurrentDB, connID: sessionVars.ConnectionID, pstmtID: pstmtID, @@ -175,16 +179,16 @@ func (s FieldSlice) Equal(tps []*types.FieldType) bool { return true } -// PSTMTPlanCacheValue stores the cached Statement and StmtNode. -type PSTMTPlanCacheValue struct { +// PlanCacheValue stores the cached Statement and StmtNode. +type PlanCacheValue struct { Plan Plan OutPutNames []*types.FieldName TblInfo2UnionScan map[*model.TableInfo]bool UserVarTypes FieldSlice } -// NewPSTMTPlanCacheValue creates a SQLCacheValue. -func NewPSTMTPlanCacheValue(plan Plan, names []*types.FieldName, srcMap map[*model.TableInfo]bool, userVarTps []*types.FieldType) *PSTMTPlanCacheValue { +// NewPlanCacheValue creates a SQLCacheValue. +func NewPlanCacheValue(plan Plan, names []*types.FieldName, srcMap map[*model.TableInfo]bool, userVarTps []*types.FieldType) *PlanCacheValue { dstMap := make(map[*model.TableInfo]bool) for k, v := range srcMap { dstMap[k] = v @@ -193,7 +197,7 @@ func NewPSTMTPlanCacheValue(plan Plan, names []*types.FieldName, srcMap map[*mod for i, tp := range userVarTps { userVarTypes[i] = *tp } - return &PSTMTPlanCacheValue{ + return &PlanCacheValue{ Plan: plan, OutPutNames: names, TblInfo2UnionScan: dstMap, diff --git a/planner/core/cache_test.go b/planner/core/cache_test.go index 074d1e4cf2828..c75a4b3963713 100644 --- a/planner/core/cache_test.go +++ b/planner/core/cache_test.go @@ -28,6 +28,6 @@ func TestCacheKey(t *testing.T) { ctx.GetSessionVars().SQLMode = mysql.ModeNone ctx.GetSessionVars().TimeZone = time.UTC ctx.GetSessionVars().ConnectionID = 0 - key := NewPSTMTPlanCacheKey(ctx.GetSessionVars(), 1, 1, "") + key := NewPlanCacheKey(ctx.GetSessionVars(), 1, 1, "") require.Equal(t, []byte{0x74, 0x65, 0x73, 0x74, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x74, 0x69, 0x64, 0x62, 0x74, 0x69, 0x6b, 0x76, 0x74, 0x69, 0x66, 0x6c, 0x61, 0x73, 0x68, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, key.Hash()) } diff --git a/planner/core/common_plans.go b/planner/core/common_plans.go index d3e56600b2c25..54f30d7d998ea 100644 --- a/planner/core/common_plans.go +++ b/planner/core/common_plans.go @@ -404,7 +404,7 @@ func (e *Execute) getPhysicalPlan(ctx context.Context, sctx sessionctx.Context, var bindSQL string if prepared.UseCache { bindSQL = GetBindSQL4PlanCache(sctx, prepared.Stmt) - cacheKey = NewPSTMTPlanCacheKey(sctx.GetSessionVars(), e.ExecID, prepared.SchemaVersion, bindSQL) + cacheKey = NewPlanCacheKey(sctx.GetSessionVars(), e.ExecID, prepared.SchemaVersion, bindSQL) } tps := make([]*types.FieldType, len(e.UsingVars)) for i, param := range e.UsingVars { @@ -445,7 +445,7 @@ func (e *Execute) getPhysicalPlan(ctx context.Context, sctx sessionctx.Context, if err := e.checkPreparedPriv(ctx, sctx, preparedStmt, is); err != nil { return err } - cachedVals := cacheValue.([]*PSTMTPlanCacheValue) + cachedVals := cacheValue.([]*PlanCacheValue) for _, cachedVal := range cachedVals { if !cachedVal.UserVarTypes.Equal(tps) { continue @@ -510,30 +510,30 @@ REBUILD: // rebuild key to exclude kv.TiFlash when stmt is not read only if _, isolationReadContainTiFlash := sessVars.IsolationReadEngines[kv.TiFlash]; isolationReadContainTiFlash && !IsReadOnly(stmt, sessVars) { delete(sessVars.IsolationReadEngines, kv.TiFlash) - cacheKey = NewPSTMTPlanCacheKey(sessVars, e.ExecID, prepared.SchemaVersion, sessVars.StmtCtx.BindSQL) + cacheKey = NewPlanCacheKey(sessVars, e.ExecID, prepared.SchemaVersion, sessVars.StmtCtx.BindSQL) sessVars.IsolationReadEngines[kv.TiFlash] = struct{}{} } else { // We need to reconstruct the plan cache key based on the bindSQL. - cacheKey = NewPSTMTPlanCacheKey(sessVars, e.ExecID, prepared.SchemaVersion, sessVars.StmtCtx.BindSQL) + cacheKey = NewPlanCacheKey(sessVars, e.ExecID, prepared.SchemaVersion, sessVars.StmtCtx.BindSQL) } - cached := NewPSTMTPlanCacheValue(p, names, stmtCtx.TblInfo2UnionScan, tps) + cached := NewPlanCacheValue(p, names, stmtCtx.TblInfo2UnionScan, tps) preparedStmt.NormalizedPlan, preparedStmt.PlanDigest = NormalizePlan(p) stmtCtx.SetPlanDigest(preparedStmt.NormalizedPlan, preparedStmt.PlanDigest) if cacheVals, exists := sctx.PreparedPlanCache().Get(cacheKey); exists { hitVal := false - for i, cacheVal := range cacheVals.([]*PSTMTPlanCacheValue) { + for i, cacheVal := range cacheVals.([]*PlanCacheValue) { if cacheVal.UserVarTypes.Equal(tps) { hitVal = true - cacheVals.([]*PSTMTPlanCacheValue)[i] = cached + cacheVals.([]*PlanCacheValue)[i] = cached break } } if !hitVal { - cacheVals = append(cacheVals.([]*PSTMTPlanCacheValue), cached) + cacheVals = append(cacheVals.([]*PlanCacheValue), cached) } sctx.PreparedPlanCache().Put(cacheKey, cacheVals) } else { - sctx.PreparedPlanCache().Put(cacheKey, []*PSTMTPlanCacheValue{cached}) + sctx.PreparedPlanCache().Put(cacheKey, []*PlanCacheValue{cached}) } } err = e.setFoundInPlanCache(sctx, false) diff --git a/server/driver_tidb.go b/server/driver_tidb.go index 9a13eea632962..dfe88d5364114 100644 --- a/server/driver_tidb.go +++ b/server/driver_tidb.go @@ -167,7 +167,7 @@ func (ts *TiDBStatement) Close() error { } preparedAst := preparedObj.PreparedAst bindSQL := planner.GetBindSQL4PlanCache(ts.ctx, preparedAst.Stmt) - ts.ctx.PreparedPlanCache().Delete(core.NewPSTMTPlanCacheKey( + ts.ctx.PreparedPlanCache().Delete(core.NewPlanCacheKey( ts.ctx.GetSessionVars(), ts.id, preparedObj.PreparedAst.SchemaVersion, bindSQL)) } ts.ctx.GetSessionVars().RemovePreparedStmt(ts.id) diff --git a/session/session.go b/session/session.go index e781a902afdf7..f685b44d0be43 100644 --- a/session/session.go +++ b/session/session.go @@ -314,7 +314,7 @@ func (s *session) cleanRetryInfo() { if ok { preparedAst = preparedObj.PreparedAst bindSQL := planner.GetBindSQL4PlanCache(s, preparedAst.Stmt) - cacheKey = plannercore.NewPSTMTPlanCacheKey(s.sessionVars, firstStmtID, preparedAst.SchemaVersion, bindSQL) + cacheKey = plannercore.NewPlanCacheKey(s.sessionVars, firstStmtID, preparedAst.SchemaVersion, bindSQL) } } }