Skip to content

Commit

Permalink
* : support show session binding (pingcap#10285)
Browse files Browse the repository at this point in the history
  • Loading branch information
iamzhoug37 authored and ngaut committed Apr 28, 2019
1 parent 9630d57 commit 168776d
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 35 deletions.
17 changes: 17 additions & 0 deletions bindinfo/bind_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,4 +256,21 @@ func (s *testSuite) TestSessionBinding(c *C) {
err = rs.Next(context.TODO(), chk)
c.Check(err, IsNil)
c.Check(chk.NumRows(), Equals, 0)

rs, err = tk.Exec("show session bindings")
c.Assert(err, IsNil)
chk = rs.NewRecordBatch()
err = rs.Next(context.TODO(), chk)
c.Check(err, IsNil)
c.Check(chk.NumRows(), Equals, 1)
row := chk.GetRow(0)
c.Check(row.GetString(0), Equals, "select * from t where i > ?")
c.Check(row.GetString(1), Equals, "select * from t use index(index_t) where i>99")
c.Check(row.GetString(2), Equals, "test")
c.Check(row.GetString(3), Equals, "using")
c.Check(row.GetTime(4), NotNil)
c.Check(row.GetTime(5), NotNil)
c.Check(row.GetString(6), NotNil)
c.Check(row.GetString(7), NotNil)

}
8 changes: 4 additions & 4 deletions bindinfo/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ const (
deleted = "deleted"
)

// bindMeta stores the basic bind info and bindSql astNode.
type bindMeta struct {
// BindMeta stores the basic bind info and bindSql astNode.
type BindMeta struct {
*BindRecord
ast ast.StmtNode //ast will be used to do query sql bind check
}

// cache is a k-v map, key is original sql, value is a slice of bindMeta.
type cache map[string][]*bindMeta
// cache is a k-v map, key is original sql, value is a slice of BindMeta.
type cache map[string][]*BindMeta

// BindRecord represents a sql bind record retrieved from the storage.
type BindRecord struct {
Expand Down
42 changes: 21 additions & 21 deletions bindinfo/handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func (h *BindHandle) Update(fullLoad bool) (err error) {
return nil
}

// AddBindRecord adds a BindRecord to the storage and bindMeta to the cache.
// AddBindRecord adds a BindRecord to the storage and BindMeta to the cache.
func (h *BindHandle) AddBindRecord(record *BindRecord) (err error) {
exec, _ := h.sctx.Context.(sqlexec.SQLExecutor)
h.sctx.Lock()
Expand All @@ -137,7 +137,7 @@ func (h *BindHandle) AddBindRecord(record *BindRecord) (err error) {
return
}

// update the bindMeta to the cache.
// update the BindMeta to the cache.
hash, meta, err1 := h.newBindMeta(record)
if err1 != nil {
err = err1
Expand Down Expand Up @@ -171,7 +171,7 @@ func (h *BindHandle) AddBindRecord(record *BindRecord) (err error) {
return err
}

// DropBindRecord drops a BindRecord to the storage and bindMeta int the cache.
// DropBindRecord drops a BindRecord to the storage and BindMeta int the cache.
func (h *BindHandle) DropBindRecord(record *BindRecord) (err error) {
exec, _ := h.sctx.Context.(sqlexec.SQLExecutor)
h.sctx.Lock()
Expand Down Expand Up @@ -226,8 +226,8 @@ func (h *BindHandle) Size() int {
return size
}

// GetBindRecord return the bindMeta of the (normdOrigSQL,db) if bindMeta exist.
func (h *BindHandle) GetBindRecord(normdOrigSQL, db string) *bindMeta {
// GetBindRecord return the BindMeta of the (normdOrigSQL,db) if BindMeta exist.
func (h *BindHandle) GetBindRecord(normdOrigSQL, db string) *BindMeta {
hash := parser.DigestHash(normdOrigSQL)
bindRecords := h.bindInfo.Load().(cache)[hash]
if bindRecords != nil {
Expand All @@ -241,33 +241,33 @@ func (h *BindHandle) GetBindRecord(normdOrigSQL, db string) *bindMeta {
}

// GetAllBindRecord return all bind record in cache.
func (h *BindHandle) GetAllBindRecord() (bindRecords []*bindMeta) {
func (h *BindHandle) GetAllBindRecord() (bindRecords []*BindMeta) {
bindRecordMap := h.bindInfo.Load().(cache)
for _, bindRecord := range bindRecordMap {
bindRecords = append(bindRecords, bindRecord...)
}
return bindRecords
}

func (h *BindHandle) newBindMeta(record *BindRecord) (hash string, meta *bindMeta, err error) {
func (h *BindHandle) newBindMeta(record *BindRecord) (hash string, meta *BindMeta, err error) {
hash = parser.DigestHash(record.OriginalSQL)
stmtNodes, _, err := h.parser.Parse(record.BindSQL, record.Charset, record.Collation)
if err != nil {
return "", nil, err
}
meta = &bindMeta{BindRecord: record, ast: stmtNodes[0]}
meta = &BindMeta{BindRecord: record, ast: stmtNodes[0]}
return hash, meta, nil
}

func (h *BindHandle) newBindMetaWithoutAst(record *BindRecord) (hash string, meta *bindMeta) {
func (h *BindHandle) newBindMetaWithoutAst(record *BindRecord) (hash string, meta *BindMeta) {
hash = parser.DigestHash(record.OriginalSQL)
meta = &bindMeta{BindRecord: record}
meta = &BindMeta{BindRecord: record}
return hash, meta
}

// appendBindMeta addes the bindMeta to the cache, all the stale bindMetas are
// appendBindMeta addes the BindMeta to the cache, all the stale bindMetas are
// removed from the cache after this operation.
func (h *BindHandle) appendBindMeta(hash string, meta *bindMeta) {
func (h *BindHandle) appendBindMeta(hash string, meta *BindMeta) {
// Make sure there is only one goroutine writes the cache.
h.bindInfo.Lock()
newCache := h.bindInfo.Value.Load().(cache).copy()
Expand All @@ -280,8 +280,8 @@ func (h *BindHandle) appendBindMeta(hash string, meta *bindMeta) {
newCache[hash] = append(newCache[hash], meta)
}

// removeBindMeta removes the bindMeta from the cache.
func (h *BindHandle) removeBindMeta(hash string, meta *bindMeta) {
// removeBindMeta removes the BindMeta from the cache.
func (h *BindHandle) removeBindMeta(hash string, meta *BindMeta) {
h.bindInfo.Lock()
newCache := h.bindInfo.Value.Load().(cache).copy()
defer func() {
Expand All @@ -292,8 +292,8 @@ func (h *BindHandle) removeBindMeta(hash string, meta *bindMeta) {
newCache.removeDeletedBindMeta(hash, meta)
}

// removeDeletedBindMeta removes all the bindMeta which originSQL and db are the same with the parameter's meta.
func (c cache) removeDeletedBindMeta(hash string, meta *bindMeta) {
// removeDeletedBindMeta removes all the BindMeta which originSQL and db are the same with the parameter's meta.
func (c cache) removeDeletedBindMeta(hash string, meta *BindMeta) {
metas, ok := c[hash]
if !ok {
return
Expand All @@ -310,8 +310,8 @@ func (c cache) removeDeletedBindMeta(hash string, meta *bindMeta) {
}
}

// removeStaleBindMetas removes all the stale bindMeta in the cache.
func (c cache) removeStaleBindMetas(hash string, meta *bindMeta) {
// removeStaleBindMetas removes all the stale BindMeta in the cache.
func (c cache) removeStaleBindMetas(hash string, meta *BindMeta) {
metas, ok := c[hash]
if !ok {
return
Expand All @@ -337,13 +337,13 @@ func (c cache) copy() cache {
return newCache
}

// isStale checks whether this bindMeta is stale compared with the other bindMeta.
func (m *bindMeta) isStale(other *bindMeta) bool {
// isStale checks whether this BindMeta is stale compared with the other BindMeta.
func (m *BindMeta) isStale(other *BindMeta) bool {
return m.OriginalSQL == other.OriginalSQL && m.Db == other.Db &&
m.UpdateTime.Compare(other.UpdateTime) <= 0
}

func (m *bindMeta) isSame(other *bindMeta) bool {
func (m *BindMeta) isSame(other *BindMeta) bool {
return m.OriginalSQL == other.OriginalSQL && m.Db == other.Db
}

Expand Down
24 changes: 16 additions & 8 deletions bindinfo/session_handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,25 +34,25 @@ func NewSessionBindHandle(parser *parser.Parser) *SessionHandle {
return sessionHandle
}

// appendBindMeta addes the bindMeta to the cache, all the stale bindMetas are
// appendBindMeta addes the BindMeta to the cache, all the stale bindMetas are
// removed from the cache after this operation.
func (h *SessionHandle) appendBindMeta(hash string, meta *bindMeta) {
func (h *SessionHandle) appendBindMeta(hash string, meta *BindMeta) {
// Make sure there is only one goroutine writes the cache.
h.ch.removeStaleBindMetas(hash, meta)
h.ch[hash] = append(h.ch[hash], meta)
}

func (h *SessionHandle) newBindMeta(record *BindRecord) (hash string, meta *bindMeta, err error) {
func (h *SessionHandle) newBindMeta(record *BindRecord) (hash string, meta *BindMeta, err error) {
hash = parser.DigestHash(record.OriginalSQL)
stmtNodes, _, err := h.parser.Parse(record.BindSQL, record.Charset, record.Collation)
if err != nil {
return "", nil, err
}
meta = &bindMeta{BindRecord: record, ast: stmtNodes[0]}
meta = &BindMeta{BindRecord: record, ast: stmtNodes[0]}
return hash, meta, nil
}

// AddBindRecord new a BindRecord with bindMeta, add it to the cache.
// AddBindRecord new a BindRecord with BindMeta, add it to the cache.
func (h *SessionHandle) AddBindRecord(record *BindRecord) error {
record.Status = using
record.CreateTime = types.Time{
Expand All @@ -62,16 +62,16 @@ func (h *SessionHandle) AddBindRecord(record *BindRecord) error {
}
record.UpdateTime = record.CreateTime

// update the bindMeta to the cache.
// update the BindMeta to the cache.
hash, meta, err := h.newBindMeta(record)
if err == nil {
h.appendBindMeta(hash, meta)
}
return err
}

// GetBindRecord return the bindMeta of the (normdOrigSQL,db) if bindMeta exist.
func (h *SessionHandle) GetBindRecord(normdOrigSQL, db string) *bindMeta {
// GetBindRecord return the BindMeta of the (normdOrigSQL,db) if BindMeta exist.
func (h *SessionHandle) GetBindRecord(normdOrigSQL, db string) *BindMeta {
hash := parser.DigestHash(normdOrigSQL)
bindRecords := h.ch[hash]
if bindRecords != nil {
Expand All @@ -84,6 +84,14 @@ func (h *SessionHandle) GetBindRecord(normdOrigSQL, db string) *bindMeta {
return nil
}

// GetAllBindRecord return all session bind info.
func (h *SessionHandle) GetAllBindRecord() (bindRecords []*BindMeta) {
for _, bindRecord := range h.ch {
bindRecords = append(bindRecords, bindRecord...)
}
return bindRecords
}

// sessionBindInfoKeyType is a dummy type to avoid naming collision in context.
type sessionBindInfoKeyType int

Expand Down
8 changes: 6 additions & 2 deletions executor/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
"github.com/pingcap/tidb-tools/pkg/etcd"
"github.com/pingcap/tidb-tools/pkg/utils"
"github.com/pingcap/tidb-tools/tidb-binlog/node"
"github.com/pingcap/tidb/bindinfo"
"github.com/pingcap/tidb/config"
"github.com/pingcap/tidb/domain"
"github.com/pingcap/tidb/infoschema"
Expand Down Expand Up @@ -182,10 +183,13 @@ func (e *ShowExec) fetchAll() error {
}

func (e *ShowExec) fetchShowBind() error {
var bindRecords []*bindinfo.BindMeta
if !e.GlobalScope {
return errors.New("show non-global bind sql is not supported")
handle := e.ctx.Value(bindinfo.SessionBindInfoKeyType).(*bindinfo.SessionHandle)
bindRecords = handle.GetAllBindRecord()
} else {
bindRecords = domain.GetDomain(e.ctx).BindHandle().GetAllBindRecord()
}
bindRecords := domain.GetDomain(e.ctx).BindHandle().GetAllBindRecord()
for _, bindData := range bindRecords {
e.appendRow([]interface{}{
bindData.OriginalSQL,
Expand Down

0 comments on commit 168776d

Please sign in to comment.