From 925b8e83d0f92863ba4080b9570bec5068f66637 Mon Sep 17 00:00:00 2001 From: qw4990 Date: Tue, 17 Dec 2024 16:32:55 +0800 Subject: [PATCH 1/6] fixup --- pkg/bindinfo/global_handle.go | 30 +----------------------------- pkg/bindinfo/global_handle_test.go | 10 +++++----- pkg/bindinfo/internal/testutil.go | 2 +- pkg/executor/bind.go | 4 +--- pkg/planner/core/common_plans.go | 2 -- pkg/planner/core/planbuilder.go | 7 +------ 6 files changed, 9 insertions(+), 46 deletions(-) diff --git a/pkg/bindinfo/global_handle.go b/pkg/bindinfo/global_handle.go index a9dff9b5701ca..713cbbdc889b3 100644 --- a/pkg/bindinfo/global_handle.go +++ b/pkg/bindinfo/global_handle.go @@ -82,9 +82,6 @@ type GlobalBindingHandle interface { // Methods for memory control. - // Size returns the size of bind info cache. - Size() int - // SetBindingCacheCapacity reset the capacity for the bindingCache. SetBindingCacheCapacity(capacity int64) @@ -94,12 +91,6 @@ type GlobalBindingHandle interface { // GetMemCapacity returns the memory capacity for the bind cache. GetMemCapacity() (memCapacity int64) - // Clear resets the bind handle. It is only used for test. - Clear() - - // FlushGlobalBindings flushes the Bindings in temp maps to storage and loads them into cache. - FlushGlobalBindings() error - // Methods for Auto Capture. // CaptureBaselines is used to automatically capture plan baselines. @@ -215,7 +206,7 @@ func (h *globalBindingHandle) LoadFromStorageToCache(fullLoad bool) (err error) metrics.BindingCacheMemUsage.Set(float64(h.GetMemUsage())) metrics.BindingCacheMemLimit.Set(float64(h.GetMemCapacity())) - metrics.BindingCacheNumBindings.Set(float64(h.Size())) + metrics.BindingCacheNumBindings.Set(float64(len(h.getCache().GetAllBindings()))) }() for _, row := range rows { @@ -492,12 +483,6 @@ func (h *globalBindingHandle) AddInvalidGlobalBinding(invalidBinding Binding) { h.invalidBindings.add(invalidBinding) } -// Size returns the size of bind info cache. -func (h *globalBindingHandle) Size() int { - size := len(h.getCache().GetAllBindings()) - return size -} - // MatchGlobalBinding returns the matched binding for this statement. func (h *globalBindingHandle) MatchGlobalBinding(sctx sessionctx.Context, fuzzyDigest string, tableNames []*ast.TableName) (matchedBinding Binding, isMatched bool) { return h.getCache().FuzzyMatchingBinding(sctx, fuzzyDigest, tableNames) @@ -662,19 +647,6 @@ func (*paramMarkerChecker) Leave(in ast.Node) (ast.Node, bool) { return in, true } -// Clear resets the bind handle. It is only used for test. -func (h *globalBindingHandle) Clear() { - h.setCache(newFuzzyBindingCache(h.LoadBindingsFromStorage)) - h.setLastUpdateTime(types.ZeroTimestamp) - h.invalidBindings.reset() -} - -// FlushGlobalBindings flushes the Bindings in temp maps to storage and loads them into cache. -func (h *globalBindingHandle) FlushGlobalBindings() error { - h.DropInvalidGlobalBinding() - return h.LoadFromStorageToCache(false) -} - func (h *globalBindingHandle) callWithSCtx(wrapTxn bool, f func(sctx sessionctx.Context) error) (err error) { resource, err := h.sPool.Get() if err != nil { diff --git a/pkg/bindinfo/global_handle_test.go b/pkg/bindinfo/global_handle_test.go index 2d11516cb84f8..8268ca0ce3009 100644 --- a/pkg/bindinfo/global_handle_test.go +++ b/pkg/bindinfo/global_handle_test.go @@ -133,7 +133,7 @@ func TestBindParse(t *testing.T) { bindHandle := bindinfo.NewGlobalBindingHandle(&mockSessionPool{tk.Session()}) err := bindHandle.LoadFromStorageToCache(true) require.NoError(t, err) - require.Equal(t, 1, bindHandle.Size()) + require.Equal(t, 1, len(bindHandle.GetAllGlobalBindings())) stmt, err := parser.New().ParseOneStmt("select * from test . t", "", "") require.NoError(t, err) @@ -266,7 +266,7 @@ func TestSetBindingStatusWithoutBindingInCache(t *testing.T) { 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() + dom.BindHandle().Reset() tk.MustExec("set binding disabled for select * from t where a > 10") tk.MustExec("admin reload bindings") rows := tk.MustQuery("show global bindings").Rows() @@ -281,7 +281,7 @@ func TestSetBindingStatusWithoutBindingInCache(t *testing.T) { 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', '', '','" + bindinfo.Manual + "', '" + sqlDigest.String() + "', '')") - dom.BindHandle().Clear() + dom.BindHandle().Reset() tk.MustExec("set binding enabled for select * from t where a > 10") tk.MustExec("admin reload bindings") rows = tk.MustQuery("show global bindings").Rows() @@ -465,7 +465,7 @@ func TestGlobalBinding(t *testing.T) { bindHandle := bindinfo.NewGlobalBindingHandle(&mockSessionPool{tk.Session()}) err = bindHandle.LoadFromStorageToCache(true) require.NoError(t, err) - require.Equal(t, 1, bindHandle.Size()) + require.Equal(t, 1, len(bindHandle.GetAllGlobalBindings())) _, fuzzyDigest = norm.NormalizeStmtForBinding(stmt, norm.WithFuzz(true)) binding, matched = dom.BindHandle().MatchGlobalBinding(tk.Session(), fuzzyDigest, bindinfo.CollectTableNames(stmt)) @@ -488,7 +488,7 @@ func TestGlobalBinding(t *testing.T) { bindHandle = bindinfo.NewGlobalBindingHandle(&mockSessionPool{tk.Session()}) err = bindHandle.LoadFromStorageToCache(true) require.NoError(t, err) - require.Equal(t, 0, bindHandle.Size()) + require.Equal(t, 0, len(bindHandle.GetAllGlobalBindings())) _, fuzzyDigest = norm.NormalizeStmtForBinding(stmt, norm.WithFuzz(true)) _, matched = dom.BindHandle().MatchGlobalBinding(tk.Session(), fuzzyDigest, bindinfo.CollectTableNames(stmt)) diff --git a/pkg/bindinfo/internal/testutil.go b/pkg/bindinfo/internal/testutil.go index bddc8ce9ff997..59bfb7cfb2bd5 100644 --- a/pkg/bindinfo/internal/testutil.go +++ b/pkg/bindinfo/internal/testutil.go @@ -28,7 +28,7 @@ import ( // UtilCleanBindingEnv cleans the binding environment. func UtilCleanBindingEnv(tk *testkit.TestKit, dom *domain.Domain) { tk.MustExec("delete from mysql.bind_info where source != 'builtin'") - dom.BindHandle().Clear() + dom.BindHandle().Reset() } // UtilNormalizeWithDefaultDB normalizes the SQL and returns the normalized SQL and its digest. diff --git a/pkg/executor/bind.go b/pkg/executor/bind.go index 7cfbf32c39647..09894b82dd31d 100644 --- a/pkg/executor/bind.go +++ b/pkg/executor/bind.go @@ -49,8 +49,6 @@ func (e *SQLBindExec) Next(_ context.Context, req *chunk.Chunk) error { return e.flushBindings() case plannercore.OpCaptureBindings: e.captureBindings() - case plannercore.OpEvolveBindings: - return nil // not support yet case plannercore.OpReloadBindings: return e.reloadBindings() case plannercore.OpSetBindingStatus: @@ -159,7 +157,7 @@ func (e *SQLBindExec) createSQLBind() error { } func (e *SQLBindExec) flushBindings() error { - return domain.GetDomain(e.Ctx()).BindHandle().FlushGlobalBindings() + return domain.GetDomain(e.Ctx()).BindHandle().LoadFromStorageToCache(false) } func (e *SQLBindExec) captureBindings() { diff --git a/pkg/planner/core/common_plans.go b/pkg/planner/core/common_plans.go index 888f9677e94d8..8af04b610c7e7 100644 --- a/pkg/planner/core/common_plans.go +++ b/pkg/planner/core/common_plans.go @@ -290,8 +290,6 @@ const ( OpFlushBindings // OpCaptureBindings is used to capture plan bindings. OpCaptureBindings - // OpEvolveBindings is used to evolve plan binding. - OpEvolveBindings // OpReloadBindings is used to reload plan binding. OpReloadBindings // OpSetBindingStatus is used to set binding status. diff --git a/pkg/planner/core/planbuilder.go b/pkg/planner/core/planbuilder.go index 6d04ca7f35e90..1e0e6b157014b 100644 --- a/pkg/planner/core/planbuilder.go +++ b/pkg/planner/core/planbuilder.go @@ -1567,12 +1567,7 @@ func (b *PlanBuilder) buildAdmin(ctx context.Context, as *ast.AdminStmt) (base.P case ast.AdminCaptureBindings: return &SQLBindPlan{SQLBindOp: OpCaptureBindings}, nil case ast.AdminEvolveBindings: - var err error - // The 'baseline evolution' only work in the test environment before the feature is GA. - if !config.CheckTableBeforeDrop { - err = errors.Errorf("Cannot enable baseline evolution feature, it is not generally available now") - } - return &SQLBindPlan{SQLBindOp: OpEvolveBindings}, err + return nil, errors.Errorf("Cannot enable baseline evolution feature, it is not generally available now") case ast.AdminReloadBindings: return &SQLBindPlan{SQLBindOp: OpReloadBindings}, nil case ast.AdminReloadStatistics: From ab4e48b04ec14a2e5118b4ec2775bc2267789eec Mon Sep 17 00:00:00 2001 From: qw4990 Date: Tue, 17 Dec 2024 17:13:03 +0800 Subject: [PATCH 2/6] fixup --- pkg/bindinfo/binding.go | 2 -- pkg/bindinfo/global_handle.go | 68 ----------------------------------- pkg/domain/domain.go | 1 - pkg/planner/optimize.go | 17 --------- 4 files changed, 88 deletions(-) diff --git a/pkg/bindinfo/binding.go b/pkg/bindinfo/binding.go index 8f3586f676175..5238fe23a71b0 100644 --- a/pkg/bindinfo/binding.go +++ b/pkg/bindinfo/binding.go @@ -39,8 +39,6 @@ const ( Using = "using" // deleted is the bind info's deleted status. deleted = "deleted" - // Invalid is the bind info's invalid status. - Invalid = "invalid" // Manual indicates the binding is created by SQL like "create binding for ...". Manual = "manual" // Capture indicates the binding is captured by TiDB automatically. diff --git a/pkg/bindinfo/global_handle.go b/pkg/bindinfo/global_handle.go index 713cbbdc889b3..d861eb62eb425 100644 --- a/pkg/bindinfo/global_handle.go +++ b/pkg/bindinfo/global_handle.go @@ -18,7 +18,6 @@ import ( "context" "fmt" "strings" - "sync" "sync/atomic" "time" @@ -63,12 +62,6 @@ type GlobalBindingHandle interface { // SetGlobalBindingStatus set a Bindings's status to the storage and bind cache. SetGlobalBindingStatus(newStatus, sqlDigest string) (ok bool, err error) - // AddInvalidGlobalBinding adds Bindings which needs to be deleted into invalidBindingCache. - AddInvalidGlobalBinding(invalidBinding Binding) - - // DropInvalidGlobalBinding executes the drop Bindings tasks. - DropInvalidGlobalBinding() - // Methods for load and clear global sql bindings. // Reset is to reset the BindHandle and clean old info. @@ -109,10 +102,6 @@ type globalBindingHandle struct { // This value is used to avoid reload duplicated bindings from storage. lastUpdateTime atomic.Value - // invalidBindings indicates the invalid bindings found during querying. - // A binding will be deleted from this map, after 2 bind-lease, after it is dropped from the kv. - invalidBindings *invalidBindingCache - // syncBindingSingleflight is used to synchronize the execution of `LoadFromStorageToCache` method. syncBindingSingleflight singleflight.Group } @@ -159,7 +148,6 @@ func (h *globalBindingHandle) setCache(c FuzzyBindingCache) { // Reset is to reset the BindHandle and clean old info. func (h *globalBindingHandle) Reset() { h.lastUpdateTime.Store(types.ZeroTimestamp) - h.invalidBindings = newInvalidBindingCache() h.setCache(newFuzzyBindingCache(h.LoadBindingsFromStorage)) variable.RegisterStatistics(h) } @@ -427,62 +415,6 @@ func lockBindInfoTable(sctx sessionctx.Context) error { return err } -// invalidBindingCache is used to store invalid bindings temporarily. -type invalidBindingCache struct { - mu sync.RWMutex - m map[string]Binding // key: sqlDigest -} - -func newInvalidBindingCache() *invalidBindingCache { - return &invalidBindingCache{ - m: make(map[string]Binding), - } -} - -func (c *invalidBindingCache) add(binding Binding) { - c.mu.Lock() - defer c.mu.Unlock() - c.m[binding.SQLDigest] = binding -} - -func (c *invalidBindingCache) getAll() Bindings { - c.mu.Lock() - defer c.mu.Unlock() - bindings := make(Bindings, 0, len(c.m)) - for _, binding := range c.m { - bindings = append(bindings, binding) - } - return bindings -} - -func (c *invalidBindingCache) reset() { - c.mu.Lock() - defer c.mu.Unlock() - c.m = make(map[string]Binding) -} - -// DropInvalidGlobalBinding executes the drop Bindings tasks. -func (h *globalBindingHandle) DropInvalidGlobalBinding() { - defer func() { - if err := h.LoadFromStorageToCache(false); err != nil { - logutil.BindLogger().Warn("drop invalid global binding error", zap.Error(err)) - } - }() - - invalidBindings := h.invalidBindings.getAll() - h.invalidBindings.reset() - for _, invalidBinding := range invalidBindings { - if _, err := h.dropGlobalBinding([]string{invalidBinding.SQLDigest}); err != nil { - logutil.BindLogger().Debug("flush bind record failed", zap.Error(err)) - } - } -} - -// AddInvalidGlobalBinding adds Bindings which needs to be deleted into invalidBindings. -func (h *globalBindingHandle) AddInvalidGlobalBinding(invalidBinding Binding) { - h.invalidBindings.add(invalidBinding) -} - // MatchGlobalBinding returns the matched binding for this statement. func (h *globalBindingHandle) MatchGlobalBinding(sctx sessionctx.Context, fuzzyDigest string, tableNames []*ast.TableName) (matchedBinding Binding, isMatched bool) { return h.getCache().FuzzyMatchingBinding(sctx, fuzzyDigest, tableNames) diff --git a/pkg/domain/domain.go b/pkg/domain/domain.go index 025d539b092ab..c38bf64716b7c 100644 --- a/pkg/domain/domain.go +++ b/pkg/domain/domain.go @@ -2134,7 +2134,6 @@ func (do *Domain) globalBindHandleWorkerLoop(owner owner.Manager) { if err != nil { logutil.BgLogger().Error("update bindinfo failed", zap.Error(err)) } - bindHandle.DropInvalidGlobalBinding() // Get Global optVal, err := do.GetGlobalVar(variable.TiDBCapturePlanBaseline) if err == nil && variable.TiDBOptOn(optVal) { diff --git a/pkg/planner/optimize.go b/pkg/planner/optimize.go index 93faff2784ade..dafb491ac1c16 100644 --- a/pkg/planner/optimize.go +++ b/pkg/planner/optimize.go @@ -27,7 +27,6 @@ import ( "github.com/pingcap/tidb/pkg/domain" "github.com/pingcap/tidb/pkg/infoschema" "github.com/pingcap/tidb/pkg/kv" - "github.com/pingcap/tidb/pkg/metrics" "github.com/pingcap/tidb/pkg/parser/ast" "github.com/pingcap/tidb/pkg/parser/model" "github.com/pingcap/tidb/pkg/planner/cascades/old" @@ -320,8 +319,6 @@ func Optimize(ctx context.Context, sctx sessionctx.Context, node *resolve.NodeW, } plan, curNames, cost, err := optimize(ctx, pctx, node, is) if err != nil { - binding.Status = bindinfo.Invalid - handleInvalidBinding(ctx, pctx, scope, binding) continue } if cost < minCost { @@ -585,20 +582,6 @@ func buildLogicalPlan(ctx context.Context, sctx planctx.PlanContext, node *resol return p, nil } -func handleInvalidBinding(ctx context.Context, sctx planctx.PlanContext, level string, binding bindinfo.Binding) { - sessionHandle := sctx.Value(bindinfo.SessionBindInfoKeyType).(bindinfo.SessionBindingHandle) - err := sessionHandle.DropSessionBinding([]string{binding.SQLDigest}) - if err != nil { - logutil.Logger(ctx).Info("drop session bindings failed") - } - if level == metrics.ScopeSession { - return - } - - globalHandle := domain.GetDomain(sctx).BindHandle() - globalHandle.AddInvalidGlobalBinding(binding) -} - // setVarHintChecker checks whether the variable name in set_var hint is valid. func setVarHintChecker(varName, hint string) (ok bool, warning error) { sysVar := variable.GetSysVar(varName) From 86964e137f583575b2b68e420813a690a113fc52 Mon Sep 17 00:00:00 2001 From: qw4990 Date: Tue, 17 Dec 2024 17:17:36 +0800 Subject: [PATCH 3/6] fixup --- pkg/bindinfo/tests/bind_test.go | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) diff --git a/pkg/bindinfo/tests/bind_test.go b/pkg/bindinfo/tests/bind_test.go index 780b8f17786bb..2c1a20754062c 100644 --- a/pkg/bindinfo/tests/bind_test.go +++ b/pkg/bindinfo/tests/bind_test.go @@ -442,18 +442,8 @@ func TestErrorBind(t *testing.T) { require.NotNil(t, binding.UpdateTime) tk.MustExec("drop index index_t on t") - rs, err := tk.Exec("select * from t where i > 10") - require.NoError(t, err) - rs.Close() - - dom.BindHandle().DropInvalidGlobalBinding() - - rs, err = tk.Exec("show global bindings") - require.NoError(t, err) - chk := rs.NewChunk(nil) - err = rs.Next(context.TODO(), chk) - require.NoError(t, err) - require.Equal(t, 0, chk.NumRows()) + require.Equal(t, 1, len(tk.MustQuery(`show global bindings`).Rows())) + tk.MustQuery("select * from t where i > 10") } func TestStmtHints(t *testing.T) { From 63b93f36e8092a97806f1b373d421519155ae156 Mon Sep 17 00:00:00 2001 From: qw4990 Date: Tue, 17 Dec 2024 17:19:27 +0800 Subject: [PATCH 4/6] fixup --- pkg/bindinfo/capture_test.go | 5 ----- 1 file changed, 5 deletions(-) diff --git a/pkg/bindinfo/capture_test.go b/pkg/bindinfo/capture_test.go index c39ab547cc9e8..86e4b12d5e5cb 100644 --- a/pkg/bindinfo/capture_test.go +++ b/pkg/bindinfo/capture_test.go @@ -268,11 +268,6 @@ func TestCapturePreparedStmt(t *testing.T) { tk.MustUseIndex("select /*+ use_index(t,idx_b) */ * from t where b = 1 and c > 1", "idx_c(c)") tk.MustExec("admin flush bindings") - tk.MustExec("admin evolve bindings") - rows = tk.MustQuery("show global bindings").Rows() - require.Len(t, rows, 1) - require.Equal(t, "select * from `test` . `t` where `b` = ? and `c` > ?", rows[0][0]) - require.Equal(t, "SELECT /*+ use_index(@`sel_1` `test`.`t` `idx_c`), no_order_index(@`sel_1` `test`.`t` `idx_c`)*/ * FROM `test`.`t` WHERE `b` = ? AND `c` > ?", rows[0][1]) } func TestCapturePlanBaselineIgnoreTiFlash(t *testing.T) { From 0e37fa3a25149d74d69e8cbe77d687268db13fbe Mon Sep 17 00:00:00 2001 From: qw4990 Date: Tue, 17 Dec 2024 17:57:41 +0800 Subject: [PATCH 5/6] fixup --- pkg/planner/BUILD.bazel | 1 - 1 file changed, 1 deletion(-) diff --git a/pkg/planner/BUILD.bazel b/pkg/planner/BUILD.bazel index ca5759dfec44f..e7a992ba2a67b 100644 --- a/pkg/planner/BUILD.bazel +++ b/pkg/planner/BUILD.bazel @@ -10,7 +10,6 @@ go_library( "//pkg/domain", "//pkg/infoschema", "//pkg/kv", - "//pkg/metrics", "//pkg/parser/ast", "//pkg/parser/model", "//pkg/planner/cascades/old", From 143d5548e111f7b6d57db1918cbb06db868d9f21 Mon Sep 17 00:00:00 2001 From: qw4990 Date: Wed, 18 Dec 2024 10:58:11 +0800 Subject: [PATCH 6/6] fixup --- pkg/planner/optimize.go | 1 + 1 file changed, 1 insertion(+) diff --git a/pkg/planner/optimize.go b/pkg/planner/optimize.go index dafb491ac1c16..29eccec66fa9c 100644 --- a/pkg/planner/optimize.go +++ b/pkg/planner/optimize.go @@ -319,6 +319,7 @@ func Optimize(ctx context.Context, sctx sessionctx.Context, node *resolve.NodeW, } plan, curNames, cost, err := optimize(ctx, pctx, node, is) if err != nil { + sessVars.StmtCtx.AppendWarning(errors.Errorf("binding %s failed: %v", binding.BindSQL, err)) continue } if cost < minCost {