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: make LRUPlanCache.Close reenterable #43117

Merged
merged 2 commits into from
Apr 18, 2023
Merged
Changes from all 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
36 changes: 20 additions & 16 deletions planner/core/plan_cache_lru.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ type LRUPlanCache struct {
buckets map[string]map[*list.Element]struct{}
lruList *list.List
// lock make cache thread safe
lock syncutil.Mutex
lock syncutil.RWMutex
// onEvict will be called if any eviction happened, only for test use now
onEvict func(kvcache.Key, kvcache.Value)

Expand Down Expand Up @@ -90,8 +90,8 @@ func strHashKey(key kvcache.Key, deepCopy bool) string {

// Get tries to find the corresponding value according to the given key.
func (l *LRUPlanCache) Get(key kvcache.Key, opts *utilpc.PlanCacheMatchOpts) (value kvcache.Value, ok bool) {
l.lock.Lock()
defer l.lock.Unlock()
l.lock.RLock()
defer l.lock.RUnlock()

bucket, bucketExist := l.buckets[strHashKey(key, false)]
if bucketExist {
Expand Down Expand Up @@ -154,21 +154,29 @@ func (l *LRUPlanCache) Delete(key kvcache.Key) {

// DeleteAll deletes all elements from the LRU Cache.
func (l *LRUPlanCache) DeleteAll() {
if l == nil {
return
}
l.lock.Lock()
defer l.lock.Unlock()

for lru := l.lruList.Back(); lru != nil; lru = l.lruList.Back() {
l.updateInstanceMetric(nil, lru.Value.(*planCacheEntry))
l.lruList.Remove(lru)
l.size--
// update metrics
if l.sctx.GetSessionVars().EnablePreparedPlanCacheMemoryMonitor {
core_metrics.GetPlanCacheInstanceMemoryUsage().Sub(float64(l.memoryUsageTotal))
}
core_metrics.GetPlanCacheInstanceNumCounter().Sub(float64(l.size))

// reset all fields
l.size = 0
l.buckets = make(map[string]map[*list.Element]struct{}, 1)
l.lruList = list.New()
l.memoryUsageTotal = 0
}

// Size gets the current cache size.
func (l *LRUPlanCache) Size() int {
l.lock.Lock()
defer l.lock.Unlock()
l.lock.RLock()
defer l.lock.RUnlock()

return int(l.size)
}
Expand All @@ -193,18 +201,14 @@ func (l *LRUPlanCache) MemoryUsage() (sum int64) {
if l == nil {
return
}
l.lock.RLock()
defer l.lock.RUnlock()
return l.memoryUsageTotal
}

// Close do some clean work for LRUPlanCache when close the session
func (l *LRUPlanCache) Close() {
if l == nil {
return
}
if l.sctx.GetSessionVars().EnablePreparedPlanCacheMemoryMonitor {
core_metrics.GetPlanCacheInstanceMemoryUsage().Sub(float64(l.memoryUsageTotal))
}
core_metrics.GetPlanCacheInstanceNumCounter().Sub(float64(l.size))
l.DeleteAll()
}

// removeOldest removes the oldest element from the cache.
Expand Down