Skip to content
Open
Show file tree
Hide file tree
Changes from 27 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 br/pkg/restore/snap_client/systable_restore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,5 +116,5 @@ func TestCheckSysTableCompatibility(t *testing.T) {
//
// The above variables are in the file br/pkg/restore/systable_restore.go
func TestMonitorTheSystemTableIncremental(t *testing.T) {
require.Equal(t, int64(220), session.CurrentBootstrapVersion)
require.Equal(t, int64(221), session.CurrentBootstrapVersion)
}
1 change: 0 additions & 1 deletion build/nogo_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -699,7 +699,6 @@
"pkg/planner/cascades": "planner/cascades code",
"pkg/store/": "store code",
"pkg/ttl/": "ttl code",
"pkg/bindinfo/": "bindinfo code",
"pkg/domain/": "domain code"
}
},
Expand Down
1 change: 1 addition & 0 deletions pkg/bindinfo/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ go_test(
name = "bindinfo_test",
timeout = "moderate",
srcs = [
"bind_usage_info_test.go",
"binding_cache_test.go",
"binding_match_test.go",
"capture_test.go",
Expand Down
119 changes: 119 additions & 0 deletions pkg/bindinfo/bind_usage_info_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
// Copyright 2025 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package bindinfo_test

import (
"fmt"
"testing"
"time"

"github.com/pingcap/tidb/pkg/bindinfo"
"github.com/pingcap/tidb/pkg/testkit"
"github.com/stretchr/testify/require"
)

func TestBindUsageInfo(t *testing.T) {
bindinfo.UpdateBindingUsageInfoBatchSize = 2
bindinfo.MaxWriteInterval = 100 * time.Microsecond
store, dom := testkit.CreateMockStoreAndDomain(t)

tk := testkit.NewTestKit(t, store)
bindingHandle := dom.BindHandle()
tk.MustExec(`use test`)
tk.MustExec(`set @@tidb_opt_enable_fuzzy_binding=1`)
tk.MustExec("create table t1(a int, b int, c int, key idx_b(b), key idx_c(c))")
tk.MustExec("create table t2(a int, b int, c int, key idx_b(b), key idx_c(c))")
tk.MustExec("create table t3(a int, b int, c int, key idx_b(b), key idx_c(c))")
tk.MustExec("create table t4(a int, b int, c int, key idx_b(b), key idx_c(c))")
tk.MustExec("create table t5(a int, b int, c int, key idx_b(b), key idx_c(c))")

tk.MustExec("prepare stmt1 from 'delete from t1 where b = 1 and c > 1';")
tk.MustExec("prepare stmt2 from 'delete t1, t2 from t1 inner join t2 on t1.b = t2.b';")
tk.MustExec("prepare stmt3 from 'update t1 set a = 1 where b = 1 and c > 1';")
tk.MustExec("execute stmt1;")
tk.MustExec("create global binding for delete from t1 where b = 1 and c > 1 using delete /*+ use_index(t1,idx_c) */ from t1 where b = 1 and c > 1")
tk.MustExec("create global binding for delete t1, t2 from t1 inner join t2 on t1.b = t2.b using delete /*+ inl_join(t1) */ t1, t2 from t1 inner join t2 on t1.b = t2.b")
tk.MustExec("create global binding for update t1 set a = 1 where b = 1 and c > 1 using update /*+ use_index(t1,idx_c) */ t1 set a = 1 where b = 1 and c > 1")
// cross database binding
tk.MustExec(`create global binding using select /*+ leading(t1, t2, t3, t4, t5) */ * from *.t1, *.t2, *.t3, *.t4, *.t5`)
tk.MustExec("select * from t1, t2, t3, t4, t5")
tk.MustExec("execute stmt1;")
origin := tk.MustQuery(fmt.Sprintf(`select sql_digest,last_used_date from mysql.bind_info where original_sql != '%s' order by sql_digest`, bindinfo.BuiltinPseudoSQL4BindLock))
origin.Check(testkit.Rows(
"5ce1df6eadf8b24222668b1bd2e995b72d4c88e6fe9340d8b13e834703e28c32 <nil>",
"5d3975ef2160c1e0517353798dac90a9914095d82c025e7cd97bd55aeb804798 <nil>",
"9d3995845aef70ba086d347f38a4e14c9705e966f7c5793b9fa92194bca2bbef <nil>",
"aa3c510b94b9d680f729252ca88415794c8a4f52172c5f9e06c27bee57d08329 <nil>"))
time.Sleep(50 * time.Microsecond)
require.NoError(t, bindingHandle.UpdateBindingUsageInfoToStorage())
result := tk.MustQuery(fmt.Sprintf(`select sql_digest,last_used_date from mysql.bind_info where original_sql != '%s' order by sql_digest`, bindinfo.BuiltinPseudoSQL4BindLock))
t.Log("result:", result.Rows())
// The last_used_date should be updated.
require.True(t, !origin.Equal(result.Rows()))
var first *testkit.Result
for range 5 {
tk.MustExec("execute stmt1;")
tk.MustExec("execute stmt2;")
tk.MustExec("execute stmt3;")
tk.MustExec("select * from t1, t2, t3, t4, t5")
time.Sleep(1 * time.Second)
// Set all last_used_date to null to simulate that the bindinfo in storage is not updated.
resetAllLastUsedData(tk)
require.NoError(t, bindingHandle.UpdateBindingUsageInfoToStorage())
tk.MustQuery(fmt.Sprintf(`select last_used_date from mysql.bind_info where original_sql != '%s' and last_used_date is null`, bindinfo.BuiltinPseudoSQL4BindLock)).Check(testkit.Rows())
result := tk.MustQuery(fmt.Sprintf(`select sql_digest,last_used_date from mysql.bind_info where original_sql != '%s' order by sql_digest`, bindinfo.BuiltinPseudoSQL4BindLock))
t.Log("result:", result.Rows())
if first == nil {
first = result
} else {
// in fact, The result of each for-loop should be the same.
require.True(t, first.Equal(result.Rows()))
}
}
// Set all last_used_date to null to simulate that the bindinfo in storage is not updated.
resetAllLastUsedData(tk)
for range 5 {
time.Sleep(1 * time.Second)
// No used, so last_used_date should not be updated.
require.NoError(t, bindingHandle.UpdateBindingUsageInfoToStorage())
tk.MustQuery(`select last_used_date from mysql.bind_info where last_used_date is not null`).Check(testkit.Rows())
}
tk.MustExec("execute stmt1;")
tk.MustExec("execute stmt2;")
tk.MustExec("execute stmt3;")
time.Sleep(1 * time.Second)
require.NoError(t, bindingHandle.UpdateBindingUsageInfoToStorage())
// it has been updated again.
rows := tk.MustQuery(
fmt.Sprintf(`select * from mysql.bind_info where original_sql != '%s' and last_used_date is not null`, bindinfo.BuiltinPseudoSQL4BindLock)).Rows()
require.Len(t, rows, 3)
// Set all last_used_date to null to simulate that the bindinfo in storage is not updated.
resetAllLastUsedData(tk)
tk.MustExec(`set @@global.tidb_enable_binding_usage=0;`)
for range 5 {
tk.MustExec("execute stmt1;")
tk.MustExec("execute stmt2;")
tk.MustExec("execute stmt3;")
tk.MustExec("select * from t1, t2, t3, t4, t5")
time.Sleep(1 * time.Second)
require.NoError(t, bindingHandle.UpdateBindingUsageInfoToStorage())
tk.MustQuery(fmt.Sprintf(`select last_used_date from mysql.bind_info where original_sql != '%s' and last_used_date is not null`, bindinfo.BuiltinPseudoSQL4BindLock)).
Check(testkit.Rows())
}
}

func resetAllLastUsedData(tk *testkit.TestKit) {
tk.MustExec(fmt.Sprintf(`update mysql.bind_info set last_used_date = null where original_sql != '%s'`, bindinfo.BuiltinPseudoSQL4BindLock))
}
20 changes: 20 additions & 0 deletions pkg/bindinfo/binding.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,13 @@ type Binding struct {

// TableNames records all schema and table names in this binding statement, which are used for fuzzy matching.
TableNames []*ast.TableName `json:"-"`
// LastUsedAt records the last time when this binding is used.
// It is nil if this binding has never been used.
// It is updated when this binding is used.
// It is used to update the `last_used_time` field in mysql.bind_info table.
LastUsedAt *time.Time `json:"-"`
// LastSavedAt records the last time when this binding is saved into storage.
LastSavedAt *time.Time `json:"-"`
}

func (b *Binding) isSame(rb *Binding) bool {
Expand Down Expand Up @@ -199,6 +206,8 @@ func merge(lBindings, rBindings Bindings) Bindings {
if lbind.isSame(&rbind) {
found = true
if rbind.UpdateTime.Compare(lbind.UpdateTime) >= 0 {
rbind.LastUsedAt = lbind.LastUsedAt
rbind.LastSavedAt = lbind.LastSavedAt
result[j] = rbind
}
break
Expand Down Expand Up @@ -235,3 +244,14 @@ func (b *Binding) size() float64 {
res := len(b.OriginalSQL) + len(b.Db) + len(b.BindSQL) + len(b.Status) + 2*int(unsafe.Sizeof(b.CreateTime)) + len(b.Charset) + len(b.Collation) + len(b.ID)
return float64(res)
}

// UpdateLastUsedAt is to update binding usage info when this binding is used.
func (b *Binding) UpdateLastUsedAt() {
now := time.Now()
b.LastUsedAt = &now
}

// UpdateLastSavedAt is to update the last saved time
func (b *Binding) UpdateLastSavedAt(ts *time.Time) {
b.LastSavedAt = ts
}
13 changes: 12 additions & 1 deletion pkg/bindinfo/binding_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ func (fbc *fuzzyBindingCache) getFromMemory(sctx sessionctx.Context, fuzzyDigest
}
leastWildcards := len(tableNames) + 1
enableFuzzyBinding := sctx.GetSessionVars().EnableFuzzyBinding
enableBindingUsage := variable.EnableBindingUsage.Load()
for _, sqlDigest := range fbc.fuzzy2SQLDigests[fuzzyDigest] {
bindings := bindingCache.GetBinding(sqlDigest)
if intest.InTest {
Expand All @@ -130,18 +131,28 @@ func (fbc *fuzzyBindingCache) getFromMemory(sctx sessionctx.Context, fuzzyDigest
}
}
if bindings != nil {
for _, binding := range bindings {
for idx, binding := range bindings {
numWildcards, matched := fuzzyMatchBindingTableName(sctx.GetSessionVars().CurrentDB, tableNames, binding.TableNames)
if matched && numWildcards > 0 && sctx != nil && !enableFuzzyBinding {
continue // fuzzy binding is disabled, skip this binding
}
if matched && numWildcards < leastWildcards {
matchedBinding = binding
if enableBindingUsage {
binding.UpdateLastUsedAt()
bindings[idx] = binding
}
isMatched = true
leastWildcards = numWildcards
break
}
}
if enableBindingUsage {
err := bindingCache.SetBinding(sqlDigest, bindings) // update the last used time
if err != nil {
logutil.BindLogger().Warn("bindingCache.SetBinding", zap.Error(err))
}
}
} else {
missingSQLDigest = append(missingSQLDigest, sqlDigest)
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/bindinfo/binding_match.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ func matchSQLBinding(sctx sessionctx.Context, stmtNode ast.StmtNode, info *Bindi
}
binding, matched = globalHandle.MatchGlobalBinding(sctx, fuzzyDigest, tableNames)
if matched {
// After hitting the cache, update the usage time of the bind.
binding.UpdateLastUsedAt()
return binding, matched, metrics.ScopeGlobal
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/bindinfo/capture_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ func TestConcurrentCapture(t *testing.T) {

// Simulate an existing binding generated by concurrent CREATE BINDING, which has not been synchronized to current tidb-server yet.
// Actually, it is more common to be generated by concurrent baseline capture, I use Manual just for simpler test verification.
tk.MustExec("insert into mysql.bind_info values('select * from `test` . `t`', 'select * from `test` . `t`', '', 'enabled', '2000-01-01 09:00:00', '2000-01-01 09:00:00', '', '','" +
tk.MustExec("insert into mysql.bind_info (original_sql, bind_sql, default_db, status, create_time, update_time, charset, collation, source, sql_digest, plan_digest)values('select * from `test` . `t`', 'select * from `test` . `t`', '', 'enabled', '2000-01-01 09:00:00', '2000-01-01 09:00:00', '', '','" +
bindinfo.Manual + "', '', '')")
tk.MustQuery("select original_sql, source from mysql.bind_info where source != 'builtin'").Check(testkit.Rows(
"select * from `test` . `t` manual",
Expand Down
33 changes: 29 additions & 4 deletions pkg/bindinfo/global_handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ type GlobalBindingHandle interface {
// CaptureBaselines is used to automatically capture plan baselines.
CaptureBaselines()

// UpdateBindingUsageInfoToStorage is to update the binding usage info into storage
UpdateBindingUsageInfoToStorage() error

variable.Statistics
}

Expand Down Expand Up @@ -255,6 +258,20 @@ func (h *globalBindingHandle) LoadFromStorageToCache(fullLoad bool) (err error)
})
}

// UpdateBindingUsageInfoToStorage is to update the binding usage info into storage
func (h *globalBindingHandle) UpdateBindingUsageInfoToStorage() error {
defer func() {
if r := recover(); r != nil {
logutil.BindLogger().Warn("panic when update usage info for binding", zap.Any("recover", r))
}
}()
if !variable.EnableBindingUsage.Load() {
return nil
}
bindings := h.GetAllGlobalBindings()
return h.updateBindingUsageInfoToStorage(bindings)
}

// CreateGlobalBinding creates a Bindings to the storage and the cache.
// It replaces all the exists bindings for the same normalized SQL.
func (h *globalBindingHandle) CreateGlobalBinding(sctx sessionctx.Context, bindings []*Binding) (err error) {
Expand Down Expand Up @@ -293,11 +310,19 @@ func (h *globalBindingHandle) CreateGlobalBinding(sctx sessionctx.Context, bindi

binding.CreateTime = now
binding.UpdateTime = now

var sqlDigest, planDigest any // null by default
if binding.SQLDigest != "" {
sqlDigest = binding.SQLDigest
}
if binding.PlanDigest != "" {
planDigest = binding.PlanDigest
}
// Insert the Bindings to the storage.
_, err = exec(
sctx,
`INSERT INTO mysql.bind_info VALUES (%?,%?, %?, %?, %?, %?, %?, %?, %?, %?, %?)`,
`INSERT INTO mysql.bind_info(
original_sql, bind_sql, default_db, status, create_time, update_time, charset, collation, source, sql_digest, plan_digest
) VALUES (%?,%?, %?, %?, %?, %?, %?, %?, %?, %?, %?)`,
binding.OriginalSQL,
binding.BindSQL,
strings.ToLower(binding.Db),
Expand All @@ -307,8 +332,8 @@ func (h *globalBindingHandle) CreateGlobalBinding(sctx sessionctx.Context, bindi
binding.Charset,
binding.Collation,
binding.Source,
binding.SQLDigest,
binding.PlanDigest,
sqlDigest,
planDigest,
)
failpoint.Inject("CreateGlobalBindingNthFail", func(val failpoint.Value) {
n := val.(int)
Expand Down
11 changes: 3 additions & 8 deletions pkg/bindinfo/global_handle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func TestBindingLastUpdateTimeWithInvalidBind(t *testing.T) {
updateTime0 := rows0[0][1]
require.Equal(t, updateTime0, "0000-00-00 00:00:00")

tk.MustExec("insert into mysql.bind_info values('select * from `test` . `t`', 'select * from `test` . `t` use index(`idx`)', 'test', 'enabled', '2000-01-01 09:00:00', '2000-01-01 09:00:00', '', '','" +
tk.MustExec("insert into mysql.bind_info (original_sql,bind_sql,default_db,status,create_time,update_time,charset,collation,source, sql_digest, plan_digest)values('select * from `test` . `t`', 'select * from `test` . `t` use index(`idx`)', 'test', 'enabled', '2000-01-01 09:00:00', '2000-01-01 09:00:00', '', '','" +
bindinfo.Manual + "', '', '')")
tk.MustExec("use test")
tk.MustExec("drop table if exists t")
Expand Down Expand Up @@ -262,11 +262,8 @@ func TestSetBindingStatusWithoutBindingInCache(t *testing.T) {

// Simulate creating bindings on other machines
_, sqlDigest := parser.NormalizeDigestForBinding("select * from `test` . `t` where `a` > ?")
tk.MustExec("insert into mysql.bind_info values('select * from `test` . `t` where `a` > ?', 'SELECT /*+ USE_INDEX(`t` `idx_a`)*/ * FROM `test`.`t` WHERE `a` > 10', 'test', 'deleted', '2000-01-01 09:00:00', '2000-01-01 09:00:00', '', '','" +
tk.MustExec("insert into mysql.bind_info (original_sql, bind_sql, default_db, status, create_time, update_time, charset, collation, source, sql_digest, plan_digest) values('select * from `test` . `t` where `a` > ?', 'SELECT /*+ USE_INDEX(`t` `idx_a`)*/ * FROM `test`.`t` WHERE `a` > 10', 'test', 'enabled', '2000-01-02 09:00:00', '2000-01-02 09:00:00', '', '','" +
bindinfo.Manual + "', '" + sqlDigest.String() + "', '')")
tk.MustExec("insert into mysql.bind_info values('select * from `test` . `t` where `a` > ?', 'SELECT /*+ USE_INDEX(`t` `idx_a`)*/ * FROM `test`.`t` WHERE `a` > 10', 'test', 'enabled', '2000-01-02 09:00:00', '2000-01-02 09:00:00', '', '','" +
bindinfo.Manual + "', '" + sqlDigest.String() + "', '')")
dom.BindHandle().Clear()
tk.MustExec("set binding disabled for select * from t where a > 10")
tk.MustExec("admin reload bindings")
rows := tk.MustQuery("show global bindings").Rows()
Expand All @@ -277,9 +274,7 @@ func TestSetBindingStatusWithoutBindingInCache(t *testing.T) {
internal.UtilCleanBindingEnv(tk, dom)

// Simulate creating bindings on other machines
tk.MustExec("insert into mysql.bind_info values('select * from `test` . `t` where `a` > ?', 'SELECT * FROM `test`.`t` WHERE `a` > 10', 'test', 'deleted', '2000-01-01 09:00:00', '2000-01-01 09:00:00', '', '','" +
bindinfo.Manual + "', '" + sqlDigest.String() + "', '')")
tk.MustExec("insert into mysql.bind_info values('select * from `test` . `t` where `a` > ?', 'SELECT * FROM `test`.`t` WHERE `a` > 10', 'test', 'disabled', '2000-01-02 09:00:00', '2000-01-02 09:00:00', '', '','" +
tk.MustExec("insert into mysql.bind_info (original_sql, bind_sql, default_db, status, create_time, update_time, charset, collation, source, sql_digest, plan_digest) values('select * from `test` . `t` where `a` > ?', 'SELECT * FROM `test`.`t` WHERE `a` > 10', 'test', 'disabled', '2000-01-02 09:00:00', '2000-01-02 09:00:00', '', '','" +
bindinfo.Manual + "', '" + sqlDigest.String() + "', '')")
dom.BindHandle().Clear()
tk.MustExec("set binding enabled for select * from t where a > 10")
Expand Down
4 changes: 2 additions & 2 deletions pkg/bindinfo/session_handle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ func TestBaselineDBLowerCase(t *testing.T) {

// Simulate existing bindings with upper case default_db.
_, sqlDigest := parser.NormalizeDigestForBinding("select * from `spm` . `t`")
tk.MustExec("insert into mysql.bind_info values('select * from `spm` . `t`', 'select * from `spm` . `t`', 'SPM', 'enabled', '2000-01-01 09:00:00', '2000-01-01 09:00:00', '', '','" +
tk.MustExec("insert into mysql.bind_info (original_sql, bind_sql, default_db, status, create_time, update_time, charset, collation, source, sql_digest, plan_digest) values('select * from `spm` . `t`', 'select * from `spm` . `t`', 'SPM', 'enabled', '2000-01-01 09:00:00', '2000-01-01 09:00:00', '', '','" +
bindinfo.Manual + "', '" + sqlDigest.String() + "', '')")
tk.MustQuery("select original_sql, default_db from mysql.bind_info where original_sql = 'select * from `spm` . `t`'").Check(testkit.Rows(
"select * from `spm` . `t` SPM",
Expand All @@ -205,7 +205,7 @@ func TestBaselineDBLowerCase(t *testing.T) {

internal.UtilCleanBindingEnv(tk, dom)
// Simulate existing bindings with upper case default_db.
tk.MustExec("insert into mysql.bind_info values('select * from `spm` . `t`', 'select * from `spm` . `t`', 'SPM', 'enabled', '2000-01-01 09:00:00', '2000-01-01 09:00:00', '', '','" +
tk.MustExec("insert into mysql.bind_info (original_sql, bind_sql, default_db, status, create_time, update_time, charset, collation, source, sql_digest, plan_digest) values('select * from `spm` . `t`', 'select * from `spm` . `t`', 'SPM', 'enabled', '2000-01-01 09:00:00', '2000-01-01 09:00:00', '', '','" +
bindinfo.Manual + "', '" + sqlDigest.String() + "', '')")
tk.MustQuery("select original_sql, default_db from mysql.bind_info where original_sql = 'select * from `spm` . `t`'").Check(testkit.Rows(
"select * from `spm` . `t` SPM",
Expand Down
Loading