Skip to content

Commit

Permalink
1. adjuest the configuration values if they are in out of ranges
Browse files Browse the repository at this point in the history
2. introduce kvcache.DeleteAll() to clear the cache early
  • Loading branch information
dbjoa committed Nov 22, 2018
1 parent acd7ce5 commit 256367a
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 6 deletions.
9 changes: 6 additions & 3 deletions tidb-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -405,10 +405,13 @@ func setGlobalVars() {
if plannercore.PreparedPlanCacheEnabled() {
plannercore.PreparedPlanCacheCapacity = cfg.PreparedPlanCache.Capacity
plannercore.PreparedPlanCacheMemoryGuardRatio = cfg.PreparedPlanCache.MemoryGuardRatio
if plannercore.PreparedPlanCacheMemoryGuardRatio < 0.0 || plannercore.PreparedPlanCacheMemoryGuardRatio > 1.0 {
plannercore.PreparedPlanCacheMemoryGuardRatio = 0.1
}
plannercore.PreparedPlanCacheMaxMemory = cfg.Performance.MaxMemory
if plannercore.PreparedPlanCacheMaxMemory == 0 {
total, err := memory.MemTotal()
terror.MustNil(err)
total, err := memory.MemTotal()
terror.MustNil(err)
if plannercore.PreparedPlanCacheMaxMemory > total || plannercore.PreparedPlanCacheMaxMemory <= 0 {
plannercore.PreparedPlanCacheMaxMemory = total
}
}
Expand Down
16 changes: 13 additions & 3 deletions util/kvcache/simple_lru.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ package kvcache

import (
"container/list"
"math"

"github.com/pingcap/tidb/util/memory"
)
Expand Down Expand Up @@ -91,7 +90,8 @@ func (l *SimpleLRUCache) Put(key Key, value Value) {

memUsed, err := memory.MemUsed()
if err != nil {
memUsed = math.MaxUint64
l.DeleteAll()
return
}

for memUsed > uint64(float64(l.quota)*(1.0-l.guard)) || l.size > l.capacity {
Expand All @@ -105,7 +105,8 @@ func (l *SimpleLRUCache) Put(key Key, value Value) {
if memUsed > uint64(float64(l.quota)*(1.0-l.guard)) {
memUsed, err = memory.MemUsed()
if err != nil {
memUsed = math.MaxUint64
l.DeleteAll()
return
}
}
}
Expand All @@ -123,6 +124,15 @@ func (l *SimpleLRUCache) Delete(key Key) {
l.size--
}

// DeleteAll deletes all elements from the LRU Cache.
func (l *SimpleLRUCache) DeleteAll() {
for lru := l.cache.Back(); lru != nil; lru = l.cache.Back() {
l.cache.Remove(lru)
delete(l.elements, string(lru.Value.(*cacheEntry).key.Hash()))
l.size--
}
}

// Size gets the current cache size.
func (l *SimpleLRUCache) Size() int {
return int(l.size)
Expand Down
23 changes: 23 additions & 0 deletions util/kvcache/simple_lru_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,3 +192,26 @@ func (s *testLRUCacheSuite) TestDelete(c *C) {
_, exists = lru.Get(keys[2])
c.Assert(exists, IsTrue)
}

func (s *testLRUCacheSuite) TestDeleteAll(c *C) {
lru := NewSimpleLRUCache(3, 0.1, 32<<30)

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.DeleteAll()

for i := 0; i < 3; i++ {
value, exists := lru.Get(keys[i])
c.Assert(exists, IsFalse)
c.Assert(value, IsNil)
c.Assert(int(lru.size), Equals, 0)
}
}

0 comments on commit 256367a

Please sign in to comment.