Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

*: support select/ explain select using bind info #10284

Merged
merged 21 commits into from
Apr 29, 2019
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
156 changes: 156 additions & 0 deletions bindinfo/bind_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,3 +280,159 @@ func (s *testSuite) TestSessionBinding(c *C) {
c.Check(bindData.OriginalSQL, Equals, "select * from t where i > ?")
c.Check(bindData.Status, Equals, "deleted")
}

func (s *testSuite) TestGlobalAndSessionBindingBothExist(c *C) {
tk := testkit.NewTestKit(c, s.store)
s.cleanBindingEnv(tk)
tk.MustExec("use test")
tk.MustExec("drop table if exists t1")
tk.MustExec("drop table if exists t2")
tk.MustExec("create table t1(id int)")
tk.MustExec("create table t2(id int)")

tk.MustQuery("explain SELECT /*+ TIDB_SMJ(t1, t2) */ * from t1,t2 where t1.id = t2.id").Check(testkit.Rows(
"MergeJoin_7 12487.50 root inner join, left key:test.t1.id, right key:test.t2.id",
"├─Sort_11 9990.00 root test.t1.id:asc",
"│ └─TableReader_10 9990.00 root data:Selection_9",
"│ └─Selection_9 9990.00 cop not(isnull(test.t1.id))",
"│ └─TableScan_8 10000.00 cop table:t1, range:[-inf,+inf], keep order:false, stats:pseudo",
"└─Sort_15 9990.00 root test.t2.id:asc",
" └─TableReader_14 9990.00 root data:Selection_13",
" └─Selection_13 9990.00 cop not(isnull(test.t2.id))",
" └─TableScan_12 10000.00 cop table:t2, range:[-inf,+inf], keep order:false, stats:pseudo",
))

tk.MustQuery("explain SELECT /*+ TIDB_INLJ(t1, t2) */ * from t1,t2 where t1.id = t2.id").Check(testkit.Rows(
iamzhoug37 marked this conversation as resolved.
Show resolved Hide resolved
"HashLeftJoin_7 12487.50 root inner join, inner:TableReader_14, equal:[eq(test.t1.id, test.t2.id)]",
"├─TableReader_11 9990.00 root data:Selection_10",
"│ └─Selection_10 9990.00 cop not(isnull(test.t1.id))",
"│ └─TableScan_9 10000.00 cop table:t1, range:[-inf,+inf], keep order:false, stats:pseudo",
"└─TableReader_14 9990.00 root data:Selection_13",
" └─Selection_13 9990.00 cop not(isnull(test.t2.id))",
" └─TableScan_12 10000.00 cop table:t2, range:[-inf,+inf], keep order:false, stats:pseudo",
))

tk.MustExec("create global binding for SELECT * from t1,t2 where t1.id = t2.id using SELECT /*+ TIDB_SMJ(t1, t2) */ * from t1,t2 where t1.id = t2.id")

tk.MustQuery("explain SELECT * from t1,t2 where t1.id = t2.id").Check(testkit.Rows(
"MergeJoin_7 12487.50 root inner join, left key:test.t1.id, right key:test.t2.id",
"├─Sort_11 9990.00 root test.t1.id:asc",
"│ └─TableReader_10 9990.00 root data:Selection_9",
"│ └─Selection_9 9990.00 cop not(isnull(test.t1.id))",
"│ └─TableScan_8 10000.00 cop table:t1, range:[-inf,+inf], keep order:false, stats:pseudo",
"└─Sort_15 9990.00 root test.t2.id:asc",
" └─TableReader_14 9990.00 root data:Selection_13",
" └─Selection_13 9990.00 cop not(isnull(test.t2.id))",
" └─TableScan_12 10000.00 cop table:t2, range:[-inf,+inf], keep order:false, stats:pseudo",
))

tk.MustExec("drop global binding for SELECT * from t1,t2 where t1.id = t2.id")
iamzhoug37 marked this conversation as resolved.
Show resolved Hide resolved
}

func (s *testSuite) TestComplexSqlBinding(c *C) {
tk := testkit.NewTestKit(c, s.store)
s.cleanBindingEnv(tk)
tk.MustExec("use test")
tk.MustExec("drop table if exists t1")
tk.MustExec("drop table if exists t2")
tk.MustExec("create table t1(id int)")
tk.MustExec("create table t2(id int)")
tk.MustExec("create index index_t1 on t1(id)")
tk.MustExec("create index index_t1 on t2(id)")

tk.MustQuery("explain SELECT * from t1,t2 where t1.id = t2.id").Check(testkit.Rows(
"MergeJoin_8 12487.50 root inner join, left key:test.t1.id, right key:test.t2.id",
"├─IndexReader_20 9990.00 root index:IndexScan_19",
"│ └─IndexScan_19 9990.00 cop table:t1, index:id, range:[-inf,+inf], keep order:true, stats:pseudo",
"└─IndexReader_22 9990.00 root index:IndexScan_21",
" └─IndexScan_21 9990.00 cop table:t2, index:id, range:[-inf,+inf], keep order:true, stats:pseudo",
))

tk.MustExec("create global binding for SELECT * from t1,t2 where t1.id = t2.id using select * from t1 use index(index_t1), t2 use index(index_t2) where t1.id = t2.id")

tk.MustQuery("explain SELECT * from t1,t2 where t1.id = t2.id").Check(testkit.Rows(
iamzhoug37 marked this conversation as resolved.
Show resolved Hide resolved
"MergeJoin_8 12487.50 root inner join, left key:test.t1.id, right key:test.t2.id",
"├─IndexReader_20 9990.00 root index:IndexScan_19",
"│ └─IndexScan_19 9990.00 cop table:t1, index:id, range:[-inf,+inf], keep order:true, stats:pseudo",
"└─IndexReader_22 9990.00 root index:IndexScan_21",
" └─IndexScan_21 9990.00 cop table:t2, index:id, range:[-inf,+inf], keep order:true, stats:pseudo",
))

tk.MustExec("drop global binding for SELECT * from t1,t2 where t1.id = t2.id")
}

func (s *testSuite) TestExplain(c *C) {
tk := testkit.NewTestKit(c, s.store)
s.cleanBindingEnv(tk)
tk.MustExec("use test")
tk.MustExec("drop table if exists t1")
tk.MustExec("drop table if exists t2")
tk.MustExec("create table t1(id int)")
tk.MustExec("create table t2(id int)")

tk.MustQuery("explain SELECT /*+ TIDB_SMJ(t1, t2) */ * from t1,t2 where t1.id = t2.id").Check(testkit.Rows(
"MergeJoin_7 12487.50 root inner join, left key:test.t1.id, right key:test.t2.id",
"├─Sort_11 9990.00 root test.t1.id:asc",
"│ └─TableReader_10 9990.00 root data:Selection_9",
"│ └─Selection_9 9990.00 cop not(isnull(test.t1.id))",
"│ └─TableScan_8 10000.00 cop table:t1, range:[-inf,+inf], keep order:false, stats:pseudo",
"└─Sort_15 9990.00 root test.t2.id:asc",
" └─TableReader_14 9990.00 root data:Selection_13",
" └─Selection_13 9990.00 cop not(isnull(test.t2.id))",
" └─TableScan_12 10000.00 cop table:t2, range:[-inf,+inf], keep order:false, stats:pseudo",
))

tk.MustExec("create global binding for SELECT * from t1,t2 where t1.id = t2.id using SELECT /*+ TIDB_SMJ(t1, t2) */ * from t1,t2 where t1.id = t2.id")

tk.MustQuery("explain SELECT * from t1,t2 where t1.id = t2.id").Check(testkit.Rows(
iamzhoug37 marked this conversation as resolved.
Show resolved Hide resolved
"MergeJoin_7 12487.50 root inner join, left key:test.t1.id, right key:test.t2.id",
"├─Sort_11 9990.00 root test.t1.id:asc",
"│ └─TableReader_10 9990.00 root data:Selection_9",
"│ └─Selection_9 9990.00 cop not(isnull(test.t1.id))",
"│ └─TableScan_8 10000.00 cop table:t1, range:[-inf,+inf], keep order:false, stats:pseudo",
"└─Sort_15 9990.00 root test.t2.id:asc",
" └─TableReader_14 9990.00 root data:Selection_13",
" └─Selection_13 9990.00 cop not(isnull(test.t2.id))",
" └─TableScan_12 10000.00 cop table:t2, range:[-inf,+inf], keep order:false, stats:pseudo",
))

tk.MustExec("drop global binding for SELECT * from t1,t2 where t1.id = t2.id")
}

func (s *testSuite) TestErrorBind(c *C) {
tk := testkit.NewTestKit(c, s.store)
s.cleanBindingEnv(tk)
tk.MustExec("use test")
tk.MustExec("drop table if exists t")
tk.MustExec("drop table if exists t1")
tk.MustExec("create table t(i int, s varchar(20))")
tk.MustExec("create table t1(i int, s varchar(20))")
tk.MustExec("create index index_t on t(i,s)")

_, err := tk.Exec("create global binding for select * from t where i>100 using select * from t use index(index_t) where i>100")
c.Assert(err, IsNil, Commentf("err %v", err))

bindData := s.domain.BindHandle().GetBindRecord("select * from t where i > ?", "test")
c.Check(bindData, NotNil)
c.Check(bindData.OriginalSQL, Equals, "select * from t where i > ?")
c.Check(bindData.BindSQL, Equals, "select * from t use index(index_t) where i>100")
c.Check(bindData.Db, Equals, "test")
c.Check(bindData.Status, Equals, "using")
c.Check(bindData.Charset, NotNil)
c.Check(bindData.Collation, NotNil)
c.Check(bindData.CreateTime, NotNil)
c.Check(bindData.UpdateTime, NotNil)

tk.MustExec("drop index index_t on t")
_, err = tk.Exec("select * from t where i > 10")
c.Check(err, IsNil)

s.domain.BindHandle().HandleDropBindRecord()

rs, err := tk.Exec("show global bindings")
c.Assert(err, IsNil)
chk := rs.NewRecordBatch()
err = rs.Next(context.TODO(), chk)
c.Check(err, IsNil)
c.Check(chk.NumRows(), Equals, 0)
}
8 changes: 5 additions & 3 deletions bindinfo/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,18 @@ import (
)

const (
// using is the bind info's in use status.
using = "using"
// Using is the bind info's in use status.
Using = "using"
// deleted is the bind info's deleted status.
deleted = "deleted"
// Invalid is the bind info's invalid status.
Invalid = "invalid"
)

// 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
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.
Expand Down
66 changes: 63 additions & 3 deletions bindinfo/handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"go.uber.org/zap"
"sync"
"sync/atomic"
"time"

"github.com/pingcap/parser"
"github.com/pingcap/parser/mysql"
Expand Down Expand Up @@ -60,15 +61,26 @@ type BindHandle struct {
atomic.Value
}

dropBindRecordMap struct {
qw4990 marked this conversation as resolved.
Show resolved Hide resolved
iamzhoug37 marked this conversation as resolved.
Show resolved Hide resolved
iamzhoug37 marked this conversation as resolved.
Show resolved Hide resolved
sync.Mutex
atomic.Value
}

parser *parser.Parser
lastUpdateTime types.Time
}

type droppedBindRecord struct {
iamzhoug37 marked this conversation as resolved.
Show resolved Hide resolved
bindRecord *BindRecord
droppedTime time.Time
}

// NewBindHandle creates a new BindHandle.
func NewBindHandle(ctx sessionctx.Context, parser *parser.Parser) *BindHandle {
handle := &BindHandle{parser: parser}
handle.sctx.Context = ctx
handle.bindInfo.Value.Store(make(cache, 32))
handle.dropBindRecordMap.Value.Store(make(map[string]*droppedBindRecord))
return handle
}

Expand Down Expand Up @@ -106,7 +118,7 @@ func (h *BindHandle) Update(fullLoad bool) (err error) {
}

newCache.removeStaleBindMetas(hash, meta)
if meta.Status == using {
if meta.Status == Using {
newCache[hash] = append(newCache[hash], meta)
}
}
Expand Down Expand Up @@ -163,7 +175,7 @@ func (h *BindHandle) AddBindRecord(record *BindRecord) (err error) {
Fsp: 3,
}
record.UpdateTime = record.CreateTime
record.Status = using
record.Status = Using
record.BindSQL = h.getEscapeCharacter(record.BindSQL)

// insert the BindRecord to the storage.
Expand Down Expand Up @@ -217,6 +229,46 @@ func (h *BindHandle) DropBindRecord(record *BindRecord) (err error) {
return err
}

// HandleDropBindRecord execute the drop bindRecord task.
func (h *BindHandle) HandleDropBindRecord() {
iamzhoug37 marked this conversation as resolved.
Show resolved Hide resolved
h.dropBindRecordMap.Lock()
dropBindRecordMap := copyDroppedBindRecordMap(h.dropBindRecordMap.Load().(map[string]*droppedBindRecord))
iamzhoug37 marked this conversation as resolved.
Show resolved Hide resolved
for key, droppedBindRecord := range dropBindRecordMap {
if droppedBindRecord.droppedTime.IsZero() {
err := h.DropBindRecord(droppedBindRecord.bindRecord)
if err != nil {
logutil.Logger(context.Background()).Error("handleDropBindRecord failed", zap.Error(err))
iamzhoug37 marked this conversation as resolved.
Show resolved Hide resolved
}
droppedBindRecord.droppedTime = time.Now()
continue
}

if time.Since(droppedBindRecord.droppedTime) > 6*time.Second {
XuHuaiyu marked this conversation as resolved.
Show resolved Hide resolved
delete(dropBindRecordMap, key)
}
}
h.dropBindRecordMap.Store(dropBindRecordMap)
h.dropBindRecordMap.Unlock()
iamzhoug37 marked this conversation as resolved.
Show resolved Hide resolved
}

// AddDropBindRecordTask add bindRecord to dropBindRecordMap when the bindRecord need to be deleted.
func (h *BindHandle) AddDropBindRecordTask(dropBindRecord *BindRecord) {
iamzhoug37 marked this conversation as resolved.
Show resolved Hide resolved
key := dropBindRecord.OriginalSQL + ":" + dropBindRecord.Db
if _, ok := h.dropBindRecordMap.Value.Load().(map[string]*droppedBindRecord)[key]; ok {
return
}
h.dropBindRecordMap.Lock()
if _, ok := h.dropBindRecordMap.Value.Load().(map[string]*droppedBindRecord)[key]; ok {
return
qw4990 marked this conversation as resolved.
Show resolved Hide resolved
}
newMap := copyDroppedBindRecordMap(h.dropBindRecordMap.Value.Load().(map[string]*droppedBindRecord))
newMap[key] = &droppedBindRecord{
bindRecord: dropBindRecord,
}
h.dropBindRecordMap.Store(newMap)
h.dropBindRecordMap.Unlock()
}

// Size return the size of bind info cache.
func (h *BindHandle) Size() int {
size := 0
Expand Down Expand Up @@ -246,7 +298,7 @@ func (h *BindHandle) newBindMeta(record *BindRecord) (hash string, meta *BindMet
if err != nil {
return "", nil, err
}
meta = &BindMeta{BindRecord: record, ast: stmtNodes[0]}
meta = &BindMeta{BindRecord: record, Ast: stmtNodes[0]}
return hash, meta, nil
}

Expand Down Expand Up @@ -328,6 +380,14 @@ func (c cache) copy() cache {
return newCache
}

func copyDroppedBindRecordMap(oldMap map[string]*droppedBindRecord) map[string]*droppedBindRecord {
iamzhoug37 marked this conversation as resolved.
Show resolved Hide resolved
newMap := make(map[string]*droppedBindRecord, len(oldMap))
for k, v := range oldMap {
newMap[k] = v
}
return newMap
}

func (c cache) getBindRecord(normdOrigSQL, db string) *BindMeta {
hash := parser.DigestHash(normdOrigSQL)
bindRecords := c[hash]
Expand Down
3 changes: 1 addition & 2 deletions bindinfo/session_handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,12 @@ func (h *SessionHandle) newBindMeta(record *BindRecord) (hash string, meta *Bind
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.
func (h *SessionHandle) AddBindRecord(record *BindRecord) error {
record.Status = using
record.CreateTime = types.Time{
Time: types.FromGoTime(time.Now()),
Type: mysql.TypeDatetime,
Expand Down
16 changes: 15 additions & 1 deletion domain/domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -792,7 +792,7 @@ func (do *Domain) LoadBindInfoLoop(ctx sessionctx.Context, parser *parser.Parser
}

duration := 3 * time.Second
do.wg.Add(1)
do.wg.Add(2)
go func() {
defer do.wg.Done()
defer recoverInDomain("loadBindInfoLoop", false)
Expand All @@ -808,6 +808,20 @@ func (do *Domain) LoadBindInfoLoop(ctx sessionctx.Context, parser *parser.Parser
}
}
}()

handleInvaildTaskDuration := 3 * time.Second
go func() {
iamzhoug37 marked this conversation as resolved.
Show resolved Hide resolved
defer do.wg.Done()
defer recoverInDomain("loadBindInfoLoop-dropInvalidBindInfo", false)
for {
select {
case <-do.exit:
return
case <-time.After(handleInvaildTaskDuration):
}
do.bindHandle.HandleDropBindRecord()
}
}()
return nil
}

Expand Down
1 change: 1 addition & 0 deletions executor/bind.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ func (e *SQLBindExec) createSQLBind() error {
Db: e.ctx.GetSessionVars().CurrentDB,
Charset: e.charset,
Collation: e.collation,
Status: bindinfo.Using,
}
if !e.isGlobal {
handle := e.ctx.Value(bindinfo.SessionBindInfoKeyType).(*bindinfo.SessionHandle)
Expand Down
Loading