diff --git a/tidb-server/main.go b/tidb-server/main.go index f2d19187fe049..a1062d9fa59a8 100644 --- a/tidb-server/main.go +++ b/tidb-server/main.go @@ -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 } } diff --git a/util/kvcache/simple_lru.go b/util/kvcache/simple_lru.go index 364f0d9c3839b..7120e3a5abb7c 100644 --- a/util/kvcache/simple_lru.go +++ b/util/kvcache/simple_lru.go @@ -15,7 +15,6 @@ package kvcache import ( "container/list" - "math" "github.com/pingcap/tidb/util/memory" ) @@ -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 { @@ -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 } } } @@ -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) diff --git a/util/kvcache/simple_lru_test.go b/util/kvcache/simple_lru_test.go index 55b30f9b48a92..9ddf9ae52b046 100644 --- a/util/kvcache/simple_lru_test.go +++ b/util/kvcache/simple_lru_test.go @@ -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) + } +}