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

planner: rename pstmtPlanCacheXX to PlanCacheXX #30909

Merged
merged 5 commits into from
Dec 22, 2021
Merged
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
2 changes: 1 addition & 1 deletion executor/prepared.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
))
}
Expand Down
26 changes: 15 additions & 11 deletions planner/core/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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
}
Expand All @@ -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,
Expand Down Expand Up @@ -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.
Reminiscent marked this conversation as resolved.
Show resolved Hide resolved
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
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion planner/core/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
18 changes: 9 additions & 9 deletions planner/core/common_plans.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion server/driver_tidb.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
Expand Down