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

planner: cleanup prepare cache when client send deallocate #8332

Merged
merged 7 commits into from
Nov 20, 2018
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
5 changes: 5 additions & 0 deletions executor/prepared.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,11 @@ func (e *DeallocateExec) Next(ctx context.Context, chk *chunk.Chunk) error {
return errors.Trace(plannercore.ErrStmtNotFound)
}
delete(vars.PreparedStmtNameToID, e.Name)
if plannercore.PreparedPlanCacheEnabled() {
e.ctx.PreparedPlanCache().Delete(plannercore.NewPSTMTPlanCacheKey(
vars, id, vars.PreparedStmts[id].SchemaVersion,
))
}
delete(vars.PreparedStmts, id)
return nil
}
Expand Down
34 changes: 34 additions & 0 deletions executor/prepared_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -567,3 +567,37 @@ func (s *testSuite) TestPreparedDelete(c *C) {
result.Check(nil)
}
}

func (s *testSuite) TestPrepareDealloc(c *C) {
orgEnable := plannercore.PreparedPlanCacheEnabled()
orgCapacity := plannercore.PreparedPlanCacheCapacity
defer func() {
plannercore.SetPreparedPlanCache(orgEnable)
plannercore.PreparedPlanCacheCapacity = orgCapacity
}()
plannercore.SetPreparedPlanCache(true)
plannercore.PreparedPlanCacheCapacity = 3

tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("drop table if exists prepare_test")
tk.MustExec("create table prepare_test (id int PRIMARY KEY, c1 int)")

c.Assert(tk.Se.PreparedPlanCache().Size(), Equals, 0)
tk.MustExec(`prepare stmt1 from 'select * from prepare_test'`)
tk.MustExec("execute stmt1")
tk.MustExec(`prepare stmt2 from 'select * from prepare_test'`)
tk.MustExec("execute stmt2")
tk.MustExec(`prepare stmt3 from 'select * from prepare_test'`)
tk.MustExec("execute stmt3")
tk.MustExec(`prepare stmt4 from 'select * from prepare_test'`)
tk.MustExec("execute stmt4")
c.Assert(tk.Se.PreparedPlanCache().Size(), Equals, 3)

tk.MustExec("deallocate prepare stmt1")
c.Assert(tk.Se.PreparedPlanCache().Size(), Equals, 3)
tk.MustExec("deallocate prepare stmt2")
tk.MustExec("deallocate prepare stmt3")
tk.MustExec("deallocate prepare stmt4")
c.Assert(tk.Se.PreparedPlanCache().Size(), Equals, 0)
}
18 changes: 16 additions & 2 deletions planner/core/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,14 @@ type pstmtPlanCacheKey struct {

// Hash implements Key interface.
func (key *pstmtPlanCacheKey) Hash() []byte {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By the way, how about refactor kv.SimpleCache use []byte as key ? (not in this pr)
I don't see any benefit of its Key definition. @lysu

if key.hash == nil {
if len(key.hash) == 0 {
var (
dbBytes = hack.Slice(key.database)
bufferSize = len(dbBytes) + 8*6
)
key.hash = make([]byte, 0, bufferSize)
if key.hash == nil {
key.hash = make([]byte, 0, bufferSize)
}
key.hash = append(key.hash, dbBytes...)
key.hash = codec.EncodeInt(key.hash, int64(key.connID))
key.hash = codec.EncodeInt(key.hash, int64(key.pstmtID))
Expand All @@ -87,6 +89,18 @@ func (key *pstmtPlanCacheKey) Hash() []byte {
return key.hash
}

// SetPstmtIDSchemaVersion implements PstmtCacheKeyMutator interface to change pstmtID and schemaVersion of cacheKey.
// so we can reuse Key instead of new every time.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why we need to reuse Key?

func SetPstmtIDSchemaVersion(key kvcache.Key, pstmtID uint32, schemaVersion int64) {
psStmtKey, isPsStmtKey := key.(*pstmtPlanCacheKey)
if !isPsStmtKey {
return
}
psStmtKey.pstmtID = pstmtID
psStmtKey.schemaVersion = schemaVersion
psStmtKey.hash = psStmtKey.hash[:0]
}

// NewPSTMTPlanCacheKey creates a new pstmtPlanCacheKey object.
func NewPSTMTPlanCacheKey(sessionVars *variable.SessionVars, pstmtID uint32, schemaVersion int64) kvcache.Key {
timezoneOffset := 0
Expand Down
30 changes: 25 additions & 5 deletions session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,12 +162,32 @@ func (s *session) getMembufCap() int {
}

func (s *session) cleanRetryInfo() {
if !s.sessionVars.RetryInfo.Retrying {
retryInfo := s.sessionVars.RetryInfo
for _, stmtID := range retryInfo.DroppedPreparedStmtIDs {
delete(s.sessionVars.PreparedStmts, stmtID)
if s.sessionVars.RetryInfo.Retrying {
return
}

retryInfo := s.sessionVars.RetryInfo
defer retryInfo.Clean()
if len(retryInfo.DroppedPreparedStmtIDs) == 0 {
return
}

planCacheEnabled := plannercore.PreparedPlanCacheEnabled()
var cacheKey kvcache.Key
if planCacheEnabled {
firstStmtID := retryInfo.DroppedPreparedStmtIDs[0]
cacheKey = plannercore.NewPSTMTPlanCacheKey(
s.sessionVars, firstStmtID, s.sessionVars.PreparedStmts[firstStmtID].SchemaVersion,
)
}
for i, stmtID := range retryInfo.DroppedPreparedStmtIDs {
if planCacheEnabled {
if i > 0 {
plannercore.SetPstmtIDSchemaVersion(cacheKey, stmtID, s.sessionVars.PreparedStmts[stmtID].SchemaVersion)
}
s.PreparedPlanCache().Delete(cacheKey)
}
retryInfo.Clean()
delete(s.sessionVars.PreparedStmts, stmtID)
}
}

Expand Down
17 changes: 17 additions & 0 deletions util/kvcache/simple_lru.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,20 @@ func (l *SimpleLRUCache) Put(key Key, value Value) {
l.size--
}
}

// Delete deletes the key-value pair from the LRU Cache.
func (l *SimpleLRUCache) Delete(key Key) {
k := string(key.Hash())
element := l.elements[k]
if element == nil {
return
}
l.cache.Remove(element)
delete(l.elements, k)
l.size--
}

// Size gets the current cache size.
func (l *SimpleLRUCache) Size() int {
return int(l.size)
}
26 changes: 26 additions & 0 deletions util/kvcache/simple_lru_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,29 @@ func (s *testLRUCacheSuite) TestGet(c *C) {
c.Assert(value, Equals, vals[i])
}
}

func (s *testLRUCacheSuite) TestDelete(c *C) {
lru := NewSimpleLRUCache(3)

keys := make([]*mockCacheKey, 3)
vals := make([]int64, 3)

for i := 0; i < 3; i++ {
keys[i] = newMockHashKey(int64(i))
vals[i] = int64(i)
lru.Put(keys[i], vals[i])
}
c.Assert(int(lru.size), Equals, 3)

lru.Delete(keys[1])
value, exists := lru.Get(keys[1])
c.Assert(exists, IsFalse)
c.Assert(value, IsNil)
c.Assert(int(lru.size), Equals, 2)

_, exists = lru.Get(keys[0])
c.Assert(exists, IsTrue)

_, exists = lru.Get(keys[2])
c.Assert(exists, IsTrue)
}