Skip to content

Commit

Permalink
statistics: revise statscache lru implementation (#34683)
Browse files Browse the repository at this point in the history
ref #34052
  • Loading branch information
Yisaer authored May 24, 2022
1 parent 9d0d459 commit b08e89e
Showing 1 changed file with 38 additions and 21 deletions.
59 changes: 38 additions & 21 deletions statistics/handle/lru_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,10 @@ func newStatsLruCache(c int64) *statsInnerCache {
type innerItemLruCache struct {
capacity int64
trackingCost int64
elements map[int64]map[int64]*list.Element
cache *list.List
onEvict func(tblID int64)
// elements maintains tableID -> isIndex -> columnID/indexID -> *lruCacheItem
elements map[int64]map[bool]map[int64]*list.Element
cache *list.List
onEvict func(tblID int64)
}

func newInnerLruCache(c int64) *innerItemLruCache {
Expand All @@ -65,12 +66,13 @@ func newInnerLruCache(c int64) *innerItemLruCache {
return &innerItemLruCache{
capacity: c,
cache: list.New(),
elements: make(map[int64]map[int64]*list.Element, 0),
elements: make(map[int64]map[bool]map[int64]*list.Element, 0),
}
}

type lruCacheItem struct {
tblID int64
isIndex bool
id int64
innerItem statistics.TableCacheItem
innerMemUsage statistics.CacheItemMemoryUsage
Expand Down Expand Up @@ -98,7 +100,7 @@ func (s *statsInnerCache) GetByQuery(tblID int64) (*statistics.Table, bool) {
}
// move index element
for idxID := range element.tbl.Indices {
s.lru.get(tblID, idxID)
s.lru.get(tblID, idxID, true)
}
return element.tbl, true
}
Expand Down Expand Up @@ -158,16 +160,16 @@ func (s *statsInnerCache) updateIndices(tblID int64, tbl *statistics.Table, tblM
}
for idxID, index := range tbl.Indices {
idxMem := tblMemUsage.IndicesMemUsage[idxID]
s.lru.put(tblID, idxID, index, idxMem, true, needMove)
s.lru.put(tblID, idxID, true, index, idxMem, true, needMove)
}
for _, idxID := range deletedIdx {
s.lru.del(tblID, idxID)
s.lru.del(tblID, idxID, true)
}
return
}
for idxID, idx := range tbl.Indices {
idxMem := tblMemUsage.IndicesMemUsage[idxID]
s.lru.put(tblID, idxID, idx, idxMem, true, needMove)
s.lru.put(tblID, idxID, true, idx, idxMem, true, needMove)
}
}

Expand All @@ -181,7 +183,7 @@ func (s *statsInnerCache) Del(tblID int64) {
}
// remove indices
for idxID := range element.tbl.Indices {
s.lru.del(tblID, idxID)
s.lru.del(tblID, idxID, true)
}
delete(s.elements, tblID)
}
Expand Down Expand Up @@ -300,21 +302,26 @@ func (s *statsInnerCache) onEvict(tblID int64) {
func (s *statsInnerCache) freshTableCost(tblID int64, element *lruMapElement) {
element.tblMemUsage = element.tbl.MemoryUsage()
for idxID, idx := range element.tbl.Indices {
s.lru.put(tblID, idxID, idx, element.tblMemUsage.IndicesMemUsage[idxID], true, false)
s.lru.put(tblID, idxID, true, idx, element.tblMemUsage.IndicesMemUsage[idxID], true, false)
}
}

func (s *statsInnerCache) capacity() int64 {
return s.lru.capacity
}

func (c *innerItemLruCache) get(tblID, id int64) (*lruCacheItem, bool) {
func (c *innerItemLruCache) get(tblID, id int64, isIndex bool) (*lruCacheItem, bool) {
v, ok := c.elements[tblID]
if !ok {
missCounter.Inc()
return nil, false
}
ele, ok := v[id]
isIndexSet, ok := v[isIndex]
if !ok {
missCounter.Inc()
return nil, false
}
ele, ok := isIndexSet[id]
if !ok {
missCounter.Inc()
return nil, false
Expand All @@ -324,32 +331,41 @@ func (c *innerItemLruCache) get(tblID, id int64) (*lruCacheItem, bool) {
return ele.Value.(*lruCacheItem), true
}

func (c *innerItemLruCache) del(tblID, id int64) {
func (c *innerItemLruCache) del(tblID, id int64, isIndex bool) {
v, ok := c.elements[tblID]
if !ok {
return
}
ele, ok := v[id]
isindexSet, ok := v[isIndex]
if !ok {
return
}
ele, ok := isindexSet[id]
if !ok {
return
}
delCounter.Inc()
delete(c.elements[tblID], id)
delete(c.elements[tblID][isIndex], id)
c.cache.Remove(ele)
}

func (c *innerItemLruCache) put(tblID, id int64, item statistics.TableCacheItem, itemMem statistics.CacheItemMemoryUsage,
func (c *innerItemLruCache) put(tblID, id int64, isIndex bool, item statistics.TableCacheItem, itemMem statistics.CacheItemMemoryUsage,
needEvict, needMove bool) {
defer func() {
updateCounter.Inc()
if needEvict {
c.evictIfNeeded()
}
}()
v, ok := c.elements[tblID]
isIndexSet, ok := c.elements[tblID]
if !ok {
c.elements[tblID] = make(map[bool]map[int64]*list.Element)
isIndexSet = c.elements[tblID]
}
v, ok := isIndexSet[isIndex]
if !ok {
c.elements[tblID] = make(map[int64]*list.Element)
v = c.elements[tblID]
c.elements[tblID][isIndex] = make(map[int64]*list.Element)
v = c.elements[tblID][isIndex]
}
element, exist := v[id]
if exist {
Expand All @@ -368,6 +384,7 @@ func (c *innerItemLruCache) put(tblID, id int64, item statistics.TableCacheItem,
id: id,
innerItem: item,
innerMemUsage: itemMem,
isIndex: isIndex,
}
newElement := c.cache.PushFront(newItem)
v[id] = newElement
Expand All @@ -387,7 +404,7 @@ func (c *innerItemLruCache) evictIfNeeded() {
c.calculateCost(newMem, oldMem)
// remove from lru
c.cache.Remove(curr)
delete(c.elements[item.tblID], item.id)
delete(c.elements[item.tblID][item.isIndex], item.id)
if c.onEvict != nil {
c.onEvict(item.tblID)
}
Expand All @@ -404,7 +421,7 @@ func (c *innerItemLruCache) copy() *innerItemLruCache {
curr := c.cache.Back()
for curr != nil {
item := curr.Value.(*lruCacheItem)
newLRU.put(item.tblID, item.id, item.innerItem, item.innerMemUsage, false, false)
newLRU.put(item.tblID, item.id, item.isIndex, item.innerItem, item.innerMemUsage, false, false)
curr = curr.Prev()
}
return newLRU
Expand Down

0 comments on commit b08e89e

Please sign in to comment.