Skip to content
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 br/pkg/restore/snap_client/systable_restore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ 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(252), session.CurrentBootstrapVersion)
require.Equal(t, int64(253), session.CurrentBootstrapVersion)
}

func TestIsStatsTemporaryTable(t *testing.T) {
Expand Down
29 changes: 29 additions & 0 deletions pkg/bindinfo/binding.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import (
"fmt"
"strings"
"sync"
"sync/atomic"
"time"
"unsafe"

"github.com/pingcap/tidb/pkg/metrics"
Expand Down Expand Up @@ -77,6 +79,10 @@ type Binding struct {

// TableNames records all schema and table names in this binding statement, which are used for cross-db matching.
TableNames []*ast.TableName `json:"-"`

// UsageInfo is to track the usage information `last_used_time` of this binding
// and it will be updated when this binding is used.
UsageInfo bindingInfoUsageInfo
}

// IsBindingEnabled returns whether the binding is enabled.
Expand All @@ -90,6 +96,27 @@ func (b *Binding) size() float64 {
return float64(res)
}

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

// UpdateLastSavedAt is to update the last saved time
func (b *Binding) UpdateLastSavedAt(ts *time.Time) {
b.UsageInfo.LastSavedAt.Store(ts)
}

type bindingInfoUsageInfo struct {
// 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 atomic.Pointer[time.Time]
// LastSavedAt records the last time when this binding is saved into storage.
LastSavedAt atomic.Pointer[time.Time]
}

var (
// GetBindingHandle is a function to get the global binding handle.
// It is mainly used to resolve cycle import issue.
Expand Down Expand Up @@ -152,6 +179,8 @@ func matchSQLBinding(sctx sessionctx.Context, stmtNode ast.StmtNode, info *Bindi
}
binding, matched = globalHandle.MatchingBinding(sctx, noDBDigest, tableNames)
if matched {
// After hitting the cache, update the usage time of the bind.
binding.UpdateLastUsedAt()
return binding, matched, metrics.ScopeGlobal
}

Expand Down
14 changes: 14 additions & 0 deletions pkg/bindinfo/binding_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ type BindingCacheUpdater interface {
// LoadFromStorageToCache loads global bindings from storage to the memory cache.
LoadFromStorageToCache(fullLoad bool) (err error)

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

// LastUpdateTime returns the last update time.
LastUpdateTime() types.Time
}
Expand Down Expand Up @@ -96,6 +99,17 @@ func (u *bindingCacheUpdater) LoadFromStorageToCache(fullLoad bool) (err error)
return nil
}

// UpdateBindingUsageInfoToStorage is to update the binding usage info into storage
func (u *bindingCacheUpdater) UpdateBindingUsageInfoToStorage() error {
defer func() {
if r := recover(); r != nil {
bindingLogger().Warn("panic when update usage info for binding", zap.Any("recover", r))
}
}()
bindings := u.GetAllBindings()
return updateBindingUsageInfoToStorage(u.sPool, bindings)
}

// LastUpdateTime returns the last update time.
func (u *bindingCacheUpdater) LastUpdateTime() types.Time {
return u.lastUpdateTime.Load().(types.Time)
Expand Down
4 changes: 3 additions & 1 deletion pkg/bindinfo/binding_operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,9 @@ func (op *bindingOperator) CreateBinding(sctx sessionctx.Context, bindings []*Bi
}
_, 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 Down
6 changes: 3 additions & 3 deletions pkg/bindinfo/binding_operator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,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`', 'invalid_binding', '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`', 'invalid_binding', 'test', 'enabled', '2000-01-01 09:00:00', '2000-01-01 09:00:00', '', '','" +
bindinfo.SourceManual + "', '', '')")
tk.MustExec("use test")
tk.MustExec("drop table if exists t")
Expand Down Expand Up @@ -260,7 +260,7 @@ 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', 'enabled', '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 /*+ 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.SourceManual + "', '" + sqlDigest.String() + "', '')")
tk.MustExec("set binding disabled for select * from t where a > 10")
tk.MustExec("admin reload bindings")
Expand All @@ -272,7 +272,7 @@ func TestSetBindingStatusWithoutBindingInCache(t *testing.T) {
utilCleanBindingEnv(tk)

// 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', '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.SourceManual + "', '" + sqlDigest.String() + "', '')")
tk.MustExec("set binding enabled for select * from t where a > 10")
tk.MustExec("admin reload bindings")
Expand Down
3 changes: 2 additions & 1 deletion pkg/bindinfo/tests/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ go_test(
timeout = "moderate",
srcs = [
"bind_test.go",
"bind_usage_info_test.go",
"cross_db_binding_test.go",
"main_test.go",
],
flaky = True,
race = "on",
shard_count = 22,
shard_count = 23,
deps = [
"//pkg/bindinfo",
"//pkg/domain",
Expand Down
1 change: 0 additions & 1 deletion pkg/bindinfo/tests/bind_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ func utilCleanBindingEnv(tk *testkit.TestKit) {
func TestPrepareCacheWithBinding(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec(`set tidb_enable_prepared_plan_cache=1`)
tk.MustExec("use test")
tk.MustExec("drop table if exists t1, t2")
tk.MustExec("create table t1(a int, b int, c int, key idx_b(b), key idx_c(c))")
Expand Down
124 changes: 124 additions & 0 deletions pkg/bindinfo/tests/bind_usage_info_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
// 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 tests

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) {
checklist := []string{
"5ce1df6eadf8b24222668b1bd2e995b72d4c88e6fe9340d8b13e834703e28c32",
"5d3975ef2160c1e0517353798dac90a9914095d82c025e7cd97bd55aeb804798",
"9d3995845aef70ba086d347f38a4e14c9705e966f7c5793b9fa92194bca2bbef",
"aa3c510b94b9d680f729252ca88415794c8a4f52172c5f9e06c27bee57d08329",
}
bindinfo.UpdateBindingUsageInfoBatchSize = 2
bindinfo.MaxWriteInterval = 100 * time.Microsecond
store, dom := testkit.CreateMockStoreAndDomain(t)
bindingHandle := dom.BindingHandle()
tk := testkit.NewTestKit(t, store)

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())
checkBindinfoInMemory(t, bindingHandle, checklist)
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)
}

func resetAllLastUsedData(tk *testkit.TestKit) {
tk.MustExec(fmt.Sprintf(`update mysql.bind_info set last_used_date = null where original_sql != '%s'`, bindinfo.BuiltinPseudoSQL4BindLock))
}

func checkBindinfoInMemory(t *testing.T, bindingHandle bindinfo.BindingHandle, checklist []string) {
for _, digest := range checklist {
binding := bindingHandle.GetBinding(digest)
require.NotNil(t, binding)
lastSaved := binding.UsageInfo.LastSavedAt.Load()
if lastSaved != nil {
require.GreaterOrEqual(t, *binding.UsageInfo.LastSavedAt.Load(), *binding.UsageInfo.LastUsedAt.Load())
}
}
}
Loading
Loading