// Copyright 2019 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 import ( "time" "github.com/pingcap/tidb/pkg/sessionctx/vardef" "github.com/pingcap/tidb/pkg/sessionctx/variable" "github.com/pingcap/tidb/pkg/util" ) // Lease influences the duration of loading bind info and handling invalid bind. var Lease = 3 * time.Second 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 = `DELETE FROM mysql.bind_info WHERE original_sql='builtin_pseudo_sql_for_bind_lock' AND _tidb_rowid NOT IN ( -- keep one arbitrary pseudo binding SELECT _tidb_rowid FROM mysql.bind_info WHERE original_sql='builtin_pseudo_sql_for_bind_lock' limit 1)` ) // BindingHandle is used to handle all sql bind operations. type BindingHandle interface { BindingCacheUpdater BindingOperator BindingPlanEvolution variable.Statistics } // bindingHandle is used to handle all sql bind operations. type bindingHandle struct { BindingCacheUpdater BindingOperator BindingPlanEvolution } // NewBindingHandle creates a new BindingHandle. func NewBindingHandle(sPool util.DestroyableSessionPool) BindingHandle { cache := NewBindingCacheUpdater(sPool) op := newBindingOperator(sPool, cache) auto := newBindingAuto(sPool) h := &bindingHandle{BindingOperator: op, BindingCacheUpdater: cache, BindingPlanEvolution: auto} variable.RegisterStatistics(h) return h } var ( lastPlanBindingUpdateTime = "last_plan_binding_update_time" ) // GetScope gets the status variables scope. func (*bindingHandle) GetScope(_ string) vardef.ScopeFlag { return vardef.ScopeSession } // Stats returns the server statistics. func (h *bindingHandle) Stats(_ *variable.SessionVars) (map[string]any, error) { m := make(map[string]any) m[lastPlanBindingUpdateTime] = h.LastUpdateTime().String() return m, nil }