Skip to content

Commit

Permalink
planner: remove unnecessary methods in global binding handler (#58350)
Browse files Browse the repository at this point in the history
ref #51347
  • Loading branch information
qw4990 authored Dec 18, 2024
1 parent 177a03c commit f3e27f0
Show file tree
Hide file tree
Showing 6 changed files with 3 additions and 101 deletions.
2 changes: 0 additions & 2 deletions pkg/bindinfo/binding.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
68 changes: 0 additions & 68 deletions pkg/bindinfo/global_handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"context"
"fmt"
"strings"
"sync"
"sync/atomic"
"time"

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
Expand Down
14 changes: 2 additions & 12 deletions pkg/bindinfo/tests/bind_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
1 change: 0 additions & 1 deletion pkg/domain/domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
1 change: 0 additions & 1 deletion pkg/planner/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ go_library(
"//pkg/domain",
"//pkg/infoschema",
"//pkg/kv",
"//pkg/metrics",
"//pkg/parser/ast",
"//pkg/parser/model",
"//pkg/planner/cascades/old",
Expand Down
18 changes: 1 addition & 17 deletions pkg/planner/optimize.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -320,8 +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 {
binding.Status = bindinfo.Invalid
handleInvalidBinding(ctx, pctx, scope, binding)
sessVars.StmtCtx.AppendWarning(errors.Errorf("binding %s failed: %v", binding.BindSQL, err))
continue
}
if cost < minCost {
Expand Down Expand Up @@ -585,20 +583,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)
Expand Down

0 comments on commit f3e27f0

Please sign in to comment.