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: standardize some terminologies in binding pkg #48880

Merged
merged 3 commits into from
Nov 24, 2023
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
18 changes: 9 additions & 9 deletions pkg/bindinfo/bind_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,12 @@ func (c *bindCache) delete(key bindCacheKey) bool {
// GetBindRecord gets the BindRecord from the cache.
// The return value is not read-only, but it shouldn't be changed in the caller functions.
// The function is thread-safe.
func (c *bindCache) GetBindRecord(hash, normdOrigSQL, _ string) *BindRecord {
func (c *bindCache) GetBindRecord(sqlDigest, normalizedSQL, _ string) *BindRecord {
c.lock.Lock()
defer c.lock.Unlock()
bindRecords := c.get(bindCacheKey(hash))
bindRecords := c.get(bindCacheKey(sqlDigest))
for _, bindRecord := range bindRecords {
if bindRecord.OriginalSQL == normdOrigSQL {
if bindRecord.OriginalSQL == normalizedSQL {
return bindRecord
}
}
Expand Down Expand Up @@ -180,10 +180,10 @@ func (c *bindCache) GetAllBindRecords() []*BindRecord {

// SetBindRecord sets the BindRecord to the cache.
// The function is thread-safe.
func (c *bindCache) SetBindRecord(hash string, meta *BindRecord) (err error) {
func (c *bindCache) SetBindRecord(sqlDigest string, meta *BindRecord) (err error) {
c.lock.Lock()
defer c.lock.Unlock()
cacheKey := bindCacheKey(hash)
cacheKey := bindCacheKey(sqlDigest)
metas := c.getCopiedVal(cacheKey)
for i := range metas {
if metas[i].OriginalSQL == meta.OriginalSQL {
Expand All @@ -196,10 +196,10 @@ func (c *bindCache) SetBindRecord(hash string, meta *BindRecord) (err error) {

// RemoveBindRecord removes the BindRecord which has same originSQL with specified BindRecord.
// The function is thread-safe.
func (c *bindCache) RemoveBindRecord(hash string, meta *BindRecord) {
func (c *bindCache) RemoveBindRecord(sqlDigest string, meta *BindRecord) {
c.lock.Lock()
defer c.lock.Unlock()
metas := c.getCopiedVal(bindCacheKey(hash))
metas := c.getCopiedVal(bindCacheKey(sqlDigest))
if metas == nil {
return
}
Expand All @@ -211,14 +211,14 @@ func (c *bindCache) RemoveBindRecord(hash string, meta *BindRecord) {
metas = append(metas[:i], metas[i+1:]...)
}
if len(metas) == 0 {
c.delete(bindCacheKey(hash))
c.delete(bindCacheKey(sqlDigest))
return
}
}
}
// This function can guarantee the memory usage for the cache will never grow up.
// So we don't need to handle the return value here.
_, _ = c.set(bindCacheKey(hash), metas)
_, _ = c.set(bindCacheKey(sqlDigest), metas)
}

// SetMemCapacity sets the memory capacity for the cache.
Expand Down
12 changes: 6 additions & 6 deletions pkg/bindinfo/capture_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,8 +328,8 @@ func TestBindingSource(t *testing.T) {
// Test Source for SQL created sql
tk.MustExec("create global binding for select * from t where a > 10 using select * from t ignore index(idx_a) where a > 10")
bindHandle := dom.BindHandle()
sql, hash := internal.UtilNormalizeWithDefaultDB(t, "select * from t where a > ?")
bindData := bindHandle.GetBindRecord(hash, sql, "test")
sql, sqlDigest := internal.UtilNormalizeWithDefaultDB(t, "select * from t where a > ?")
bindData := bindHandle.GetBindRecord(sqlDigest, sql, "test")
require.NotNil(t, bindData)
require.Equal(t, "select * from `test` . `t` where `a` > ?", bindData.OriginalSQL)
require.Len(t, bindData.Bindings, 1)
Expand All @@ -340,8 +340,8 @@ func TestBindingSource(t *testing.T) {
tk.MustExec("set @@tidb_evolve_plan_baselines=1")
tk.MustQuery("select * from t where a > 10")
bindHandle.SaveEvolveTasksToStore()
sql, hash = internal.UtilNormalizeWithDefaultDB(t, "select * from t where a > ?")
bindData = bindHandle.GetBindRecord(hash, sql, "test")
sql, sqlDigest = internal.UtilNormalizeWithDefaultDB(t, "select * from t where a > ?")
bindData = bindHandle.GetBindRecord(sqlDigest, sql, "test")
require.NotNil(t, bindData)
require.Equal(t, "select * from `test` . `t` where `a` > ?", bindData.OriginalSQL)
require.Len(t, bindData.Bindings, 2)
Expand All @@ -361,8 +361,8 @@ func TestBindingSource(t *testing.T) {
tk.MustExec("select * from t ignore index(idx_a) where a < 10")
tk.MustExec("admin capture bindings")
bindHandle.CaptureBaselines()
sql, hash = internal.UtilNormalizeWithDefaultDB(t, "select * from t where a < ?")
bindData = bindHandle.GetBindRecord(hash, sql, "test")
sql, sqlDigest = internal.UtilNormalizeWithDefaultDB(t, "select * from t where a < ?")
bindData = bindHandle.GetBindRecord(sqlDigest, sql, "test")
require.NotNil(t, bindData)
require.Equal(t, "select * from `test` . `t` where `a` < ?", bindData.OriginalSQL)
require.Len(t, bindData.Bindings, 1)
Expand Down
40 changes: 20 additions & 20 deletions pkg/bindinfo/handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ func (h *BindHandle) Update(fullLoad bool) (err error) {
if row.GetString(0) == BuiltinPseudoSQL4BindLock {
continue
}
hash, meta, err := h.newBindRecord(row)
sqlDigest, meta, err := h.newBindRecord(row)

// Update lastUpdateTime to the newest one.
// Even if this one is an invalid bind.
Expand All @@ -194,17 +194,17 @@ func (h *BindHandle) Update(fullLoad bool) (err error) {
continue
}

oldRecord := newCache.GetBindRecord(hash, meta.OriginalSQL, meta.Db)
oldRecord := newCache.GetBindRecord(sqlDigest, meta.OriginalSQL, meta.Db)
newRecord := merge(oldRecord, meta).removeDeletedBindings()
if len(newRecord.Bindings) > 0 {
err = newCache.SetBindRecord(hash, newRecord)
err = newCache.SetBindRecord(sqlDigest, newRecord)
if err != nil {
memExceededErr = err
}
} else {
newCache.RemoveBindRecord(hash, newRecord)
newCache.RemoveBindRecord(sqlDigest, newRecord)
}
updateMetrics(metrics.ScopeGlobal, oldRecord, newCache.GetBindRecord(hash, meta.OriginalSQL, meta.Db), true)
updateMetrics(metrics.ScopeGlobal, oldRecord, newCache.GetBindRecord(sqlDigest, meta.OriginalSQL, meta.Db), true)
}
if memExceededErr != nil {
// When the memory capacity of bing_cache is not enough,
Expand Down Expand Up @@ -682,9 +682,9 @@ func (h *BindHandle) Size() int {
return size
}

// GetBindRecord returns the BindRecord of the (normdOrigSQL,db) if BindRecord exist.
func (h *BindHandle) GetBindRecord(hash, normdOrigSQL, db string) *BindRecord {
return h.bindInfo.Load().(*bindCache).GetBindRecord(hash, normdOrigSQL, db)
// GetBindRecord returns the BindRecord of the (normalizedSQL,db) if BindRecord exist.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we update this to (sqlDigest, normalizedSQL, db)?

func (h *BindHandle) GetBindRecord(sqlDigest, normalizedSQL, db string) *BindRecord {
return h.bindInfo.Load().(*bindCache).GetBindRecord(sqlDigest, normalizedSQL, db)
}

// GetBindRecordBySQLDigest returns the BindRecord of the sql digest.
Expand Down Expand Up @@ -736,23 +736,23 @@ func (h *BindHandle) newBindRecord(row chunk.Row) (string, *BindRecord, error) {
Db: strings.ToLower(row.GetString(2)),
Bindings: []Binding{hint},
}
hash := parser.DigestNormalized(bindRecord.OriginalSQL)
sqlDigest := parser.DigestNormalized(bindRecord.OriginalSQL)
h.sctx.Lock()
defer h.sctx.Unlock()
h.sctx.GetSessionVars().CurrentDB = bindRecord.Db
err := bindRecord.prepareHints(h.sctx.Context)
return hash.String(), bindRecord, err
return sqlDigest.String(), bindRecord, err
}

// setBindRecord sets the BindRecord to the cache, if there already exists a BindRecord,
// it will be overridden.
func (h *BindHandle) setBindRecord(hash string, meta *BindRecord) {
func (h *BindHandle) setBindRecord(sqlDigest string, meta *BindRecord) {
newCache, err0 := h.bindInfo.Value.Load().(*bindCache).Copy()
if err0 != nil {
logutil.BgLogger().Warn("BindHandle.setBindRecord", zap.String("category", "sql-bind"), zap.Error(err0))
}
oldRecord := newCache.GetBindRecord(hash, meta.OriginalSQL, meta.Db)
err1 := newCache.SetBindRecord(hash, meta)
oldRecord := newCache.GetBindRecord(sqlDigest, meta.OriginalSQL, meta.Db)
err1 := newCache.SetBindRecord(sqlDigest, meta)
if err1 != nil && err0 == nil {
logutil.BgLogger().Warn("BindHandle.setBindRecord", zap.String("category", "sql-bind"), zap.Error(err1))
}
Expand All @@ -762,14 +762,14 @@ func (h *BindHandle) setBindRecord(hash string, meta *BindRecord) {

// appendBindRecord adds the BindRecord to the cache, all the stale BindRecords are
// removed from the cache after this operation.
func (h *BindHandle) appendBindRecord(hash string, meta *BindRecord) {
func (h *BindHandle) appendBindRecord(sqlDigest string, meta *BindRecord) {
newCache, err0 := h.bindInfo.Value.Load().(*bindCache).Copy()
if err0 != nil {
logutil.BgLogger().Warn("BindHandle.appendBindRecord", zap.String("category", "sql-bind"), zap.Error(err0))
}
oldRecord := newCache.GetBindRecord(hash, meta.OriginalSQL, meta.Db)
oldRecord := newCache.GetBindRecord(sqlDigest, meta.OriginalSQL, meta.Db)
newRecord := merge(oldRecord, meta)
err1 := newCache.SetBindRecord(hash, newRecord)
err1 := newCache.SetBindRecord(sqlDigest, newRecord)
if err1 != nil && err0 == nil {
// Only need to handle the error once.
logutil.BgLogger().Warn("BindHandle.appendBindRecord", zap.String("category", "sql-bind"), zap.Error(err1))
Expand All @@ -779,15 +779,15 @@ func (h *BindHandle) appendBindRecord(hash string, meta *BindRecord) {
}

// removeBindRecord removes the BindRecord from the cache.
func (h *BindHandle) removeBindRecord(hash string, meta *BindRecord) {
func (h *BindHandle) removeBindRecord(sqlDigest string, meta *BindRecord) {
newCache, err := h.bindInfo.Value.Load().(*bindCache).Copy()
if err != nil {
logutil.BgLogger().Warn("", zap.String("category", "sql-bind"), zap.Error(err))
}
oldRecord := newCache.GetBindRecord(hash, meta.OriginalSQL, meta.Db)
newCache.RemoveBindRecord(hash, meta)
oldRecord := newCache.GetBindRecord(sqlDigest, meta.OriginalSQL, meta.Db)
newCache.RemoveBindRecord(sqlDigest, meta)
h.bindInfo.Value.Store(newCache)
updateMetrics(metrics.ScopeGlobal, oldRecord, newCache.GetBindRecord(hash, meta.OriginalSQL, meta.Db), false)
updateMetrics(metrics.ScopeGlobal, oldRecord, newCache.GetBindRecord(sqlDigest, meta.OriginalSQL, meta.Db), false)
}

func copyBindRecordUpdateMap(oldMap map[string]*bindRecordUpdate) map[string]*bindRecordUpdate {
Expand Down
18 changes: 9 additions & 9 deletions pkg/bindinfo/handle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ func TestBindingLastUpdateTime(t *testing.T) {
bindHandle := bindinfo.NewBindHandle(tk.Session())
err := bindHandle.Update(true)
require.NoError(t, err)
sql, hash := parser.NormalizeDigest("select * from test . t0")
bindData := bindHandle.GetBindRecord(hash.String(), sql, "test")
sql, sqlDigest := parser.NormalizeDigest("select * from test . t0")
bindData := bindHandle.GetBindRecord(sqlDigest.String(), sql, "test")
require.Equal(t, 1, len(bindData.Bindings))
bind := bindData.Bindings[0]
updateTime := bind.UpdateTime.String()
Expand Down Expand Up @@ -132,8 +132,8 @@ func TestBindParse(t *testing.T) {
require.NoError(t, err)
require.Equal(t, 1, bindHandle.Size())

sql, hash := parser.NormalizeDigest("select * from test . t")
bindData := bindHandle.GetBindRecord(hash.String(), sql, "test")
sql, sqlDigest := parser.NormalizeDigest("select * from test . t")
bindData := bindHandle.GetBindRecord(sqlDigest.String(), sql, "test")
require.NotNil(t, bindData)
require.Equal(t, "select * from `test` . `t`", bindData.OriginalSQL)
bind := bindData.Bindings[0]
Expand Down Expand Up @@ -488,9 +488,9 @@ func TestGlobalBinding(t *testing.T) {
require.NoError(t, err)
require.Equal(t, testSQL.memoryUsage, pb.GetGauge().GetValue())

sql, hash := internal.UtilNormalizeWithDefaultDB(t, testSQL.querySQL)
sql, sqlDigest := internal.UtilNormalizeWithDefaultDB(t, testSQL.querySQL)

bindData := dom.BindHandle().GetBindRecord(hash, sql, "test")
bindData := dom.BindHandle().GetBindRecord(sqlDigest, sql, "test")
require.NotNil(t, bindData)
require.Equal(t, testSQL.originSQL, bindData.OriginalSQL)
bind := bindData.Bindings[0]
Expand Down Expand Up @@ -523,7 +523,7 @@ func TestGlobalBinding(t *testing.T) {
require.NoError(t, err)
require.Equal(t, 1, bindHandle.Size())

bindData = bindHandle.GetBindRecord(hash, sql, "test")
bindData = bindHandle.GetBindRecord(sqlDigest, sql, "test")
require.NotNil(t, bindData)
require.Equal(t, testSQL.originSQL, bindData.OriginalSQL)
bind = bindData.Bindings[0]
Expand All @@ -538,7 +538,7 @@ func TestGlobalBinding(t *testing.T) {
_, err = tk.Exec("drop global " + testSQL.dropSQL)
require.Equal(t, uint64(1), tk.Session().AffectedRows())
require.NoError(t, err)
bindData = dom.BindHandle().GetBindRecord(hash, sql, "test")
bindData = dom.BindHandle().GetBindRecord(sqlDigest, sql, "test")
require.Nil(t, bindData)

err = metrics.BindTotalGauge.WithLabelValues(metrics.ScopeGlobal, bindinfo.Enabled).Write(pb)
Expand All @@ -554,7 +554,7 @@ func TestGlobalBinding(t *testing.T) {
require.NoError(t, err)
require.Equal(t, 0, bindHandle.Size())

bindData = bindHandle.GetBindRecord(hash, sql, "test")
bindData = bindHandle.GetBindRecord(sqlDigest, sql, "test")
require.Nil(t, bindData)

rs, err = tk.Exec("show global bindings")
Expand Down
16 changes: 8 additions & 8 deletions pkg/bindinfo/session_handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ func NewSessionBindHandle() *SessionHandle {

// appendBindRecord adds the BindRecord to the cache, all the stale bindMetas are
// removed from the cache after this operation.
func (h *SessionHandle) appendBindRecord(hash string, meta *BindRecord) {
oldRecord := h.ch.GetBindRecord(hash, meta.OriginalSQL, meta.Db)
err := h.ch.SetBindRecord(hash, meta)
func (h *SessionHandle) appendBindRecord(sqlDigest string, meta *BindRecord) {
oldRecord := h.ch.GetBindRecord(sqlDigest, meta.OriginalSQL, meta.Db)
err := h.ch.SetBindRecord(sqlDigest, meta)
if err != nil {
logutil.BgLogger().Warn("SessionHandle.appendBindRecord", zap.String("category", "sql-bind"), zap.Error(err))
}
Expand Down Expand Up @@ -76,8 +76,8 @@ func (h *SessionHandle) CreateBindRecord(sctx sessionctx.Context, record *BindRe
// DropBindRecord drops a BindRecord in the cache.
func (h *SessionHandle) DropBindRecord(originalSQL, db string, binding *Binding) error {
db = strings.ToLower(db)
hash := parser.DigestNormalized(originalSQL).String()
oldRecord := h.GetBindRecord(hash, originalSQL, db)
sqlDigest := parser.DigestNormalized(originalSQL).String()
oldRecord := h.GetBindRecord(sqlDigest, originalSQL, db)
var newRecord *BindRecord
record := &BindRecord{OriginalSQL: originalSQL, Db: db}
if binding != nil {
Expand All @@ -88,7 +88,7 @@ func (h *SessionHandle) DropBindRecord(originalSQL, db string, binding *Binding)
} else {
newRecord = record
}
err := h.ch.SetBindRecord(hash, newRecord)
err := h.ch.SetBindRecord(sqlDigest, newRecord)
if err != nil {
// Should never reach here, just return an error for safety
return err
Expand All @@ -107,8 +107,8 @@ func (h *SessionHandle) DropBindRecordByDigest(sqlDigest string) error {
}

// GetBindRecord return the BindMeta of the (normdOrigSQL,db) if BindMeta exist.
func (h *SessionHandle) GetBindRecord(hash, normdOrigSQL, db string) *BindRecord {
return h.ch.GetBindRecord(hash, normdOrigSQL, db)
func (h *SessionHandle) GetBindRecord(sqlDigest, normdOrigSQL, db string) *BindRecord {
return h.ch.GetBindRecord(sqlDigest, normdOrigSQL, db)
}

// GetBindRecordBySQLDigest return all BindMeta corresponding to sqlDigest.
Expand Down
6 changes: 3 additions & 3 deletions pkg/bindinfo/session_handle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ func TestSessionBinding(t *testing.T) {
require.Equal(t, testSQL.memoryUsage, pb.GetGauge().GetValue())

handle := tk.Session().Value(bindinfo.SessionBindInfoKeyType).(*bindinfo.SessionHandle)
hash := parser.DigestNormalized(testSQL.originSQL).String()
bindData := handle.GetBindRecord(hash, testSQL.originSQL, "test")
sqlDigest := parser.DigestNormalized(testSQL.originSQL).String()
bindData := handle.GetBindRecord(sqlDigest, testSQL.originSQL, "test")
require.NotNil(t, bindData)
require.Equal(t, testSQL.originSQL, bindData.OriginalSQL)
bind := bindData.Bindings[0]
Expand Down Expand Up @@ -155,7 +155,7 @@ func TestSessionBinding(t *testing.T) {

_, err = tk.Exec("drop session " + testSQL.dropSQL)
require.NoError(t, err)
bindData = handle.GetBindRecord(hash, testSQL.originSQL, "test")
bindData = handle.GetBindRecord(sqlDigest, testSQL.originSQL, "test")
require.NotNil(t, bindData)
require.Equal(t, testSQL.originSQL, bindData.OriginalSQL)
require.Len(t, bindData.Bindings, 0)
Expand Down
6 changes: 3 additions & 3 deletions pkg/planner/optimize.go
Original file line number Diff line number Diff line change
Expand Up @@ -670,12 +670,12 @@ func getBindRecord(ctx sessionctx.Context, stmt ast.StmtNode) (*bindinfo.BindRec
if ctx.Value(bindinfo.SessionBindInfoKeyType) == nil {
return nil, "", nil
}
stmtNode, normalizedSQL, hash, err := ExtractSelectAndNormalizeDigest(stmt, ctx.GetSessionVars().CurrentDB, true)
stmtNode, normalizedSQL, sqlDigest, err := ExtractSelectAndNormalizeDigest(stmt, ctx.GetSessionVars().CurrentDB, true)
if err != nil || stmtNode == nil {
return nil, "", err
}
sessionHandle := ctx.Value(bindinfo.SessionBindInfoKeyType).(*bindinfo.SessionHandle)
bindRecord := sessionHandle.GetBindRecord(hash, normalizedSQL, "")
bindRecord := sessionHandle.GetBindRecord(sqlDigest, normalizedSQL, "")
if bindRecord != nil {
if bindRecord.HasEnabledBinding() {
return bindRecord, metrics.ScopeSession, nil
Expand All @@ -686,7 +686,7 @@ func getBindRecord(ctx sessionctx.Context, stmt ast.StmtNode) (*bindinfo.BindRec
if globalHandle == nil {
return nil, "", nil
}
bindRecord = globalHandle.GetBindRecord(hash, normalizedSQL, "")
bindRecord = globalHandle.GetBindRecord(sqlDigest, normalizedSQL, "")
return bindRecord, metrics.ScopeGlobal, nil
}

Expand Down