Skip to content

Commit

Permalink
bindinfo: support 'show binding_cache status' (#32567)
Browse files Browse the repository at this point in the history
ref #32466
  • Loading branch information
Reminiscent authored Mar 15, 2022
1 parent 678397b commit d79527b
Show file tree
Hide file tree
Showing 10 changed files with 9,306 additions and 9,149 deletions.
28 changes: 26 additions & 2 deletions bindinfo/bind_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,22 @@ func (c *bindCache) get(key bindCacheKey) []*BindRecord {
return typedValue
}

// getCopiedVal gets a copied cache item according to cache key.
// The return value can be modified.
// If you want to modify the return value, use the 'getCopiedVal' function rather than 'get' function.
// We use the copy on write way to operate the bindRecord in cache for safety and accuracy of memory usage.
func (c *bindCache) getCopiedVal(key bindCacheKey) []*BindRecord {
bindRecords := c.get(key)
if bindRecords != nil {
copiedRecords := make([]*BindRecord, len(bindRecords))
for i, bindRecord := range bindRecords {
copiedRecords[i] = bindRecord.shallowCopy()
}
return copiedRecords
}
return bindRecords
}

// set inserts an item to the cache. It's not thread-safe.
// Only other functions of the bindCache can use this function.
func (c *bindCache) set(key bindCacheKey, value []*BindRecord) bool {
Expand Down Expand Up @@ -144,7 +160,7 @@ func (c *bindCache) SetBindRecord(hash string, meta *BindRecord) {
c.lock.Lock()
defer c.lock.Unlock()
cacheKey := bindCacheKey(hash)
metas := c.get(cacheKey)
metas := c.getCopiedVal(cacheKey)
for i := range metas {
if metas[i].OriginalSQL == meta.OriginalSQL {
metas[i] = meta
Expand All @@ -158,7 +174,7 @@ func (c *bindCache) SetBindRecord(hash string, meta *BindRecord) {
func (c *bindCache) RemoveBindRecord(hash string, meta *BindRecord) {
c.lock.Lock()
defer c.lock.Unlock()
metas := c.get(bindCacheKey(hash))
metas := c.getCopiedVal(bindCacheKey(hash))
if metas == nil {
return
}
Expand Down Expand Up @@ -187,6 +203,14 @@ func (c *bindCache) SetMemCapacity(capacity int64) {
c.memCapacity = capacity
}

// GetMemUsage get the memory Usage for the cache.
// The function is thread-safe.
func (c *bindCache) GetMemUsage() int64 {
c.lock.Lock()
defer c.lock.Unlock()
return c.memTracker.BytesConsumed()
}

// GetMemCapacity get the memory capacity for the cache.
// The function is thread-safe.
func (c *bindCache) GetMemCapacity() int64 {
Expand Down
10 changes: 10 additions & 0 deletions bindinfo/handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,16 @@ func (h *BindHandle) SetBindCacheCapacity(capacity int64) {
h.bindInfo.Load().(*bindCache).SetMemCapacity(capacity)
}

// GetMemUsage returns the memory usage for the bind cache.
func (h *BindHandle) GetMemUsage() (memUsage int64) {
return h.bindInfo.Load().(*bindCache).GetMemUsage()
}

// GetMemCapacity returns the memory capacity for the bind cache.
func (h *BindHandle) GetMemCapacity() (memCapacity int64) {
return h.bindInfo.Load().(*bindCache).GetMemCapacity()
}

// newBindRecord builds BindRecord from a tuple in storage.
func (h *BindHandle) newBindRecord(row chunk.Row) (string, *BindRecord, error) {
status := row.GetString(3)
Expand Down
34 changes: 34 additions & 0 deletions executor/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ import (
"github.com/pingcap/tidb/util/format"
"github.com/pingcap/tidb/util/hack"
"github.com/pingcap/tidb/util/hint"
"github.com/pingcap/tidb/util/memory"
"github.com/pingcap/tidb/util/sem"
"github.com/pingcap/tidb/util/set"
"github.com/pingcap/tidb/util/sqlexec"
Expand Down Expand Up @@ -211,6 +212,8 @@ func (e *ShowExec) fetchAll(ctx context.Context) error {
return e.fetchShowPrivileges()
case ast.ShowBindings:
return e.fetchShowBind()
case ast.ShowBindingCacheStatus:
return e.fetchShowBindingCacheStatus(ctx)
case ast.ShowAnalyzeStatus:
e.fetchShowAnalyzeStatus()
return nil
Expand Down Expand Up @@ -339,6 +342,37 @@ func (e *ShowExec) fetchShowBind() error {
return nil
}

func (e *ShowExec) fetchShowBindingCacheStatus(ctx context.Context) error {
exec := e.ctx.(sqlexec.RestrictedSQLExecutor)

rows, _, err := exec.ExecRestrictedSQL(ctx, nil, fmt.Sprintf("SELECT count(*) FROM mysql.bind_info where status = '%s' or status = '%s';", bindinfo.Enabled, bindinfo.Using))
if err != nil {
return errors.Trace(err)
}

handle := domain.GetDomain(e.ctx).BindHandle()

bindRecords := handle.GetAllBindRecord()
numBindings := 0
for _, bindRecord := range bindRecords {
for _, binding := range bindRecord.Bindings {
if binding.IsBindingEnabled() {
numBindings++
}
}
}

memUsage := handle.GetMemUsage()
memCapacity := handle.GetMemCapacity()
e.appendRow([]interface{}{
numBindings,
rows[0].GetInt64(0),
memory.FormatBytes(memUsage),
memory.FormatBytes(memCapacity),
})
return nil
}

func (e *ShowExec) fetchShowEngines(ctx context.Context) error {
exec := e.ctx.(sqlexec.RestrictedSQLExecutor)

Expand Down
57 changes: 57 additions & 0 deletions executor/show_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1767,3 +1767,60 @@ func TestShowBindingCache(t *testing.T) {
res = tk.MustQuery("show global bindings")
require.Equal(t, 2, len(res.Rows()))
}

func TestShowBindingCacheStatus(t *testing.T) {
store, clean := testkit.CreateMockStore(t)
defer clean()

tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustQuery("show binding_cache status").Check(testkit.Rows(
"0 0 0 Bytes 64 MB"))

tk.MustExec("drop table if exists t")
tk.MustExec("create table t(a int, b int, index idx_a(a), index idx_b(b))")
result := tk.MustQuery("show global bindings")
rows := result.Rows()
require.Equal(t, len(rows), 0)
tk.MustExec("create global binding for select * from t using select * from t")

result = tk.MustQuery("show global bindings")
rows = result.Rows()
require.Equal(t, len(rows), 1)

tk.MustQuery("show binding_cache status").Check(testkit.Rows(
"1 1 159 Bytes 64 MB"))

tk.MustExec(`set global tidb_mem_quota_bind_cache = 250`)
tk.MustQuery(`select @@global.tidb_mem_quota_bind_cache`).Check(testkit.Rows("250"))
tk.MustExec("admin reload bindings;")
tk.MustExec("create global binding for select * from t where a > 1 using select * from t where a > 1")
result = tk.MustQuery("show global bindings")
rows = result.Rows()
require.Equal(t, len(rows), 1)
tk.MustQuery("show binding_cache status").Check(testkit.Rows(
"1 2 187 Bytes 250 Bytes"))

tk.MustExec("drop global binding for select * from t where a > 1")
result = tk.MustQuery("show global bindings")
rows = result.Rows()
require.Equal(t, len(rows), 0)
tk.MustQuery("show binding_cache status").Check(testkit.Rows(
"0 1 0 Bytes 250 Bytes"))

tk.MustExec("admin reload bindings")
result = tk.MustQuery("show global bindings")
rows = result.Rows()
require.Equal(t, len(rows), 1)
tk.MustQuery("show binding_cache status").Check(testkit.Rows(
"1 1 159 Bytes 250 Bytes"))

tk.MustExec("create global binding for select * from t using select * from t use index(idx_a)")

result = tk.MustQuery("show global bindings")
rows = result.Rows()
require.Equal(t, len(rows), 1)

tk.MustQuery("show binding_cache status").Check(testkit.Rows(
"1 1 198 Bytes 250 Bytes"))
}
3 changes: 3 additions & 0 deletions parser/ast/dml.go
Original file line number Diff line number Diff line change
Expand Up @@ -2589,6 +2589,7 @@ const (
ShowPrivileges
ShowErrors
ShowBindings
ShowBindingCacheStatus
ShowPumpStatus
ShowDrainerStatus
ShowOpenTables
Expand Down Expand Up @@ -2915,6 +2916,8 @@ func (n *ShowStmt) Restore(ctx *format.RestoreCtx) error {
ctx.WriteKeyWord("SESSION ")
}
ctx.WriteKeyWord("BINDINGS")
case ShowBindingCacheStatus:
ctx.WriteKeyWord("BINDING_CACHE STATUS")
case ShowPumpStatus:
ctx.WriteKeyWord("PUMP STATUS")
case ShowDrainerStatus:
Expand Down
1 change: 1 addition & 0 deletions parser/misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ var tokenMap = map[string]int{
"BIGINT": bigIntType,
"BINARY": binaryType,
"BINDING": binding,
"BINDING_CACHE": bindingCache,
"BINDINGS": bindings,
"BINLOG": binlog,
"BIT_AND": bitAnd,
Expand Down
Loading

0 comments on commit d79527b

Please sign in to comment.