bindinfo

package
v1.1.0-beta.0...-0c22a2d Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 24, 2024 License: Apache-2.0 Imports: 38 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// Enabled is the bind info's in enabled status.
	// It is the same as the previous 'Using' status.
	// Only use 'Enabled' status in the future, not the 'Using' status.
	// The 'Using' status is preserved for compatibility.
	Enabled = "enabled"
	// Disabled is the bind info's in disabled status.
	Disabled = "disabled"
	// Using is the bind info's in use status.
	// The 'Using' status is preserved for compatibility.
	Using = "using"

	// Manual indicates the binding is created by SQL like "create binding for ...".
	Manual = "manual"
	// Builtin indicates the binding is a builtin record for internal locking purpose. It is also the status for the builtin binding.
	Builtin = "builtin"
	// History indicate the binding is created from statement summary by plan digest
	History = "history"
)
View Source
const (
	// OwnerKey is the bindinfo owner path that is saved to etcd.
	OwnerKey = "/tidb/bindinfo/owner"
	// Prompt is the prompt for bindinfo owner manager.
	Prompt = "bindinfo"
	// BuiltinPseudoSQL4BindLock is used to simulate LOCK TABLE for mysql.bind_info.
	BuiltinPseudoSQL4BindLock = "builtin_pseudo_sql_for_bind_lock"

	// LockBindInfoSQL simulates LOCK TABLE by updating a same row in each pessimistic transaction.
	LockBindInfoSQL = `UPDATE mysql.bind_info SET source= 'builtin' WHERE original_sql= 'builtin_pseudo_sql_for_bind_lock'`

	// StmtRemoveDuplicatedPseudoBinding is used to remove duplicated pseudo binding.
	// After using BR to sync bind_info between two clusters, the pseudo binding may be duplicated, and
	// BR use this statement to remove duplicated rows, and this SQL should only be executed by BR.
	StmtRemoveDuplicatedPseudoBinding = `` /* 269-byte string literal not displayed */

)
View Source
const SessionBindInfoKeyType sessionBindInfoKeyType = 0

SessionBindInfoKeyType is a variable key for store session bind info.

Variables

View Source
var GetBindingReturnNil = stringutil.StringerStr("GetBindingReturnNil")

GetBindingReturnNil is only for test

View Source
var GetBindingReturnNilAlways = stringutil.StringerStr("getBindingReturnNilAlways")

GetBindingReturnNilAlways is only for test

View Source
var GetBindingReturnNilBool atomic.Bool

GetBindingReturnNilBool is only for test

View Source
var (
	// GetGlobalBindingHandle is a function to get the global binding handle.
	// It is mainly used to resolve cycle import issue.
	GetGlobalBindingHandle func(sctx sessionctx.Context) GlobalBindingHandle
)
View Source
var Lease = 3 * time.Second

Lease influences the duration of loading bind info and handling invalid bind.

View Source
var LoadBindingNothing = stringutil.StringerStr("LoadBindingNothing")

LoadBindingNothing is only for test

Functions

func CollectTableNames

func CollectTableNames(in ast.Node) []*ast.TableName

CollectTableNames gets all table names from ast.Node. This function is mainly for binding cross-db matching. ** the return is read-only. For example:

`select * from t1 where a < 1` --> [t1]
`select * from db1.t1, t2 where a < 1` --> [db1.t1, t2]

You can see more example at the TestExtractTableName.

func GenerateBindingSQL

func GenerateBindingSQL(stmtNode ast.StmtNode, planHint string, skipCheckIfHasParam bool, defaultDB string) string

GenerateBindingSQL generates binding sqls from stmt node and plan hints.

func MatchSQLBindingForPlanCache

func MatchSQLBindingForPlanCache(sctx sessionctx.Context, stmtNode ast.StmtNode, info *BindingMatchInfo) (bindingSQL string, ignoreBinding bool)

MatchSQLBindingForPlanCache matches binding for plan cache.

Types

type Binding

type Binding struct {
	OriginalSQL string
	Db          string
	BindSQL     string
	// Status represents the status of the binding. It can only be one of the following values:
	// 1. deleted: Bindings is deleted, can not be used anymore.
	// 2. enabled, using: Binding is in the normal active mode.
	Status     string
	CreateTime types.Time
	UpdateTime types.Time
	Source     string
	Charset    string
	Collation  string
	// Hint is the parsed hints, it is used to bind hints to stmt node.
	Hint *hint.HintsSet `json:"-"`
	// ID is the string form of Hint. It would be non-empty only when the status is `Using` or `PendingVerify`.
	ID         string `json:"-"`
	SQLDigest  string
	PlanDigest string

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

Binding stores the basic bind hint info.

func MatchSQLBinding

func MatchSQLBinding(sctx sessionctx.Context, stmtNode ast.StmtNode) (binding *Binding, matched bool, scope string)

MatchSQLBinding returns the matched binding for this statement.

func (*Binding) IsBindingAvailable

func (b *Binding) IsBindingAvailable() bool

IsBindingAvailable returns whether the binding is available. The available means the binding can be used or can be converted into a usable status. It includes the 'Enabled', 'Using' and 'Disabled' status.

func (*Binding) IsBindingEnabled

func (b *Binding) IsBindingEnabled() bool

IsBindingEnabled returns whether the binding is enabled.

func (*Binding) SinceUpdateTime

func (b *Binding) SinceUpdateTime() (time.Duration, error)

SinceUpdateTime returns the duration since last update time. Export for test.

type BindingCache

type BindingCache interface {
	// MatchingBinding supports cross-db matching on bindings.
	MatchingBinding(sctx sessionctx.Context, noDBDigest string, tableNames []*ast.TableName) (binding *Binding, isMatched bool)
	// GetBinding gets the binding for the specified sqlDigest.
	GetBinding(sqlDigest string) []*Binding
	// GetAllBindings gets all the bindings in the cache.
	GetAllBindings() []*Binding
	// SetBinding sets the binding for the specified sqlDigest.
	SetBinding(sqlDigest string, bindings []*Binding) (err error)
	// RemoveBinding removes the binding for the specified sqlDigest.
	RemoveBinding(sqlDigest string)
	// SetMemCapacity sets the memory capacity for the cache.
	SetMemCapacity(capacity int64)
	// GetMemUsage gets the memory usage of the cache.
	GetMemUsage() int64
	// GetMemCapacity gets the memory capacity of the cache.
	GetMemCapacity() int64
	// Size returns the number of items in the cache.
	Size() int
	// Close closes the cache.
	Close()
}

BindingCache is the interface for the cache of the SQL plan bindings.

type BindingMatchInfo

type BindingMatchInfo struct {
	NoDBDigest string
	TableNames []*ast.TableName
}

BindingMatchInfo records necessary information for cross-db binding matching. This is mainly for plan cache to avoid normalizing the same statement repeatedly.

type GlobalBindingHandle

type GlobalBindingHandle interface {

	// MatchGlobalBinding returns the matched binding for this statement.
	MatchGlobalBinding(sctx sessionctx.Context, noDBDigest string, tableNames []*ast.TableName) (matchedBinding *Binding, isMatched bool)

	// GetAllGlobalBindings returns all bind records in cache.
	GetAllGlobalBindings() (bindings []*Binding)

	// CreateGlobalBinding creates a Bindings to the storage and the cache.
	// It replaces all the exists bindings for the same normalized SQL.
	CreateGlobalBinding(sctx sessionctx.Context, bindings []*Binding) (err error)

	// DropGlobalBinding drop Bindings to the storage and Bindings int the cache.
	DropGlobalBinding(sqlDigests []string) (deletedRows uint64, err error)

	// SetGlobalBindingStatus set a Bindings's status to the storage and bind cache.
	SetGlobalBindingStatus(newStatus, sqlDigest string) (ok bool, err error)

	// Reset is to reset the BindHandle and clean old info.
	Reset()

	// LoadFromStorageToCache loads global bindings from storage to the memory cache.
	LoadFromStorageToCache(fullLoad bool) (err error)

	// GCGlobalBinding physically removes the deleted bind records in mysql.bind_info.
	GCGlobalBinding() (err error)

	// SetBindingCacheCapacity reset the capacity for the bindingCache.
	SetBindingCacheCapacity(capacity int64)

	// GetMemUsage returns the memory usage for the bind cache.
	GetMemUsage() (memUsage int64)

	// GetMemCapacity returns the memory capacity for the bind cache.
	GetMemCapacity() (memCapacity int64)

	// Close closes the binding cache.
	CloseCache()

	variable.Statistics
}

GlobalBindingHandle is used to handle all global sql bind operations.

func NewGlobalBindingHandle

func NewGlobalBindingHandle(sPool util.SessionPool) GlobalBindingHandle

NewGlobalBindingHandle creates a new GlobalBindingHandle.

type SessionBindingHandle

type SessionBindingHandle interface {
	// CreateSessionBinding creates a binding to the cache.
	CreateSessionBinding(sctx sessionctx.Context, bindings []*Binding) (err error)

	// DropSessionBinding drops a binding by the sql digest.
	DropSessionBinding(sqlDigests []string) error

	// MatchSessionBinding returns the matched binding for this statement.
	MatchSessionBinding(sctx sessionctx.Context, noDBDigest string, tableNames []*ast.TableName) (matchedBinding *Binding, isMatched bool)

	// GetAllSessionBindings return all bindings.
	GetAllSessionBindings() (bindings []*Binding)

	// Close closes the SessionBindingHandle.
	Close()

	sessionctx.SessionStatesHandler
}

SessionBindingHandle is used to handle all session sql bind operations.

func NewSessionBindingHandle

func NewSessionBindingHandle() SessionBindingHandle

NewSessionBindingHandle creates a new SessionBindingHandle.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL