Skip to content

Commit

Permalink
bindinfo: fix bindinfo bugs when update cache (#13875) (#13891)
Browse files Browse the repository at this point in the history
  • Loading branch information
alivxxx authored and eurekaka committed Dec 10, 2019
1 parent 9da133f commit d006af9
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
22 changes: 20 additions & 2 deletions bindinfo/bind_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ func (s *testSuite) TearDownTest(c *C) {
}

func (s *testSuite) cleanBindingEnv(tk *testkit.TestKit) {
tk.MustExec("drop table if exists mysql.bind_info")
tk.MustExec(session.CreateBindInfoTable)
tk.MustExec("truncate table mysql.bind_info")
s.domain.BindHandle().Clear()
}

func (s *testSuite) TestBindParse(c *C) {
Expand Down Expand Up @@ -465,3 +465,21 @@ func (s *testSuite) TestErrorBind(c *C) {
c.Check(err, IsNil)
c.Check(chk.NumRows(), Equals, 0)
}

func (s *testSuite) TestBindingCache(c *C) {
tk := testkit.NewTestKit(c, s.store)
s.cleanBindingEnv(tk)
tk.MustExec("use test")
tk.MustExec("drop table if exists t")
tk.MustExec("create table t(a int, b int, index idx(a))")
tk.MustExec("create global binding for select * from t using select * from t use index(idx)")
tk.MustExec("create database tmp")
tk.MustExec("use tmp")
tk.MustExec("create table t(a int, b int, index idx(a))")
tk.MustExec("create global binding for select * from t using select * from t use index(idx)")

c.Assert(s.domain.BindHandle().Update(false), IsNil)
c.Assert(s.domain.BindHandle().Update(false), IsNil)
res := tk.MustQuery("show global bindings")
c.Assert(len(res.Rows()), Equals, 2)
}
15 changes: 13 additions & 2 deletions bindinfo/handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func NewBindHandle(ctx sessionctx.Context) *BindHandle {
func (h *BindHandle) Update(fullLoad bool) (err error) {
sql := "select original_sql, bind_sql, default_db, status, create_time, update_time, charset, collation from mysql.bind_info"
if !fullLoad {
sql += " where update_time >= \"" + h.lastUpdateTime.String() + "\""
sql += " where update_time > \"" + h.lastUpdateTime.String() + "\""
}

// No need to acquire the session context lock for ExecRestrictedSQL, it
Expand Down Expand Up @@ -358,6 +358,7 @@ func (c cache) removeDeletedBindMeta(hash string, meta *BindMeta, scope string)
}
}
}
c[hash] = metas
}

// removeStaleBindMetas removes all the stale BindMeta in the cache.
Expand All @@ -377,12 +378,15 @@ func (c cache) removeStaleBindMetas(hash string, meta *BindMeta, scope string) {
}
}
}
c[hash] = metas
}

func (c cache) copy() cache {
newCache := make(cache, len(c))
for k, v := range c {
newCache[k] = v
bindMetas := make([]*BindMeta, len(v))
copy(bindMetas, v)
newCache[k] = bindMetas
}
return newCache
}
Expand Down Expand Up @@ -445,3 +449,10 @@ func (h *BindHandle) logicalDeleteBindInfoSQL(normdOrigSQL, db string, updateTs
expression.Quote(normdOrigSQL),
expression.Quote(db))
}

// Clear resets the bind handle. It is used for test.
func (h *BindHandle) Clear() {
h.bindInfo.Store(make(cache))
h.invalidBindRecordMap.Store(make(map[string]*invalidBindRecordMap))
h.lastUpdateTime = types.ZeroTimestamp
}

0 comments on commit d006af9

Please sign in to comment.