Skip to content

Commit

Permalink
rename lru list
Browse files Browse the repository at this point in the history
Signed-off-by: alanprot <[email protected]>
  • Loading branch information
alanprot committed Nov 21, 2024
1 parent e493ae8 commit 602f72e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
20 changes: 12 additions & 8 deletions pkg/storage/tsdb/expanded_postings_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,15 +280,17 @@ type lruCache[V any] struct {
metrics ExpandedPostingsCacheMetrics

// Fields from here should be locked
cachedMtx sync.RWMutex
cached *list.List
cachedMtx sync.RWMutex
// Keeps tracks of the last recent used keys.
// The most recent key used is placed in the back of the list while items should be evicted from the front of the list
lruList *list.List
cachedBytes int64
}

func newLruCache[V any](cfg PostingsCacheConfig, name string, metrics *ExpandedPostingsCacheMetrics, timeNow func() time.Time) *lruCache[V] {
return &lruCache[V]{
cachedValues: new(sync.Map),
cached: list.New(),
lruList: list.New(),
cfg: cfg,
timeNow: timeNow,
name: name,
Expand Down Expand Up @@ -361,7 +363,7 @@ func (c *lruCache[V]) contains(k string) bool {
}

func (c *lruCache[V]) shouldEvictHead() (string, bool) {
h := c.cached.Front()
h := c.lruList.Front()
if h == nil {
return "", false
}
Expand All @@ -382,8 +384,8 @@ func (c *lruCache[V]) shouldEvictHead() (string, bool) {
}

func (c *lruCache[V]) evictHead() {
front := c.cached.Front()
c.cached.Remove(front)
front := c.lruList.Front()
c.lruList.Remove(front)
oldestKey := front.Value.(string)
if oldest, loaded := c.cachedValues.LoadAndDelete(oldestKey); loaded {
c.cachedBytes -= oldest.(*cacheEntryPromise[V]).sizeBytes
Expand All @@ -398,13 +400,13 @@ func (c *lruCache[V]) created(key string, sizeBytes int64) *list.Element {
c.cachedMtx.Lock()
defer c.cachedMtx.Unlock()
c.cachedBytes += sizeBytes
return c.cached.PushBack(key)
return c.lruList.PushBack(key)
}

func (c *lruCache[V]) moveBack(ele *list.Element) {
c.cachedMtx.Lock()
defer c.cachedMtx.Unlock()
c.cached.MoveToBack(ele)
c.lruList.MoveToBack(ele)
}

func (c *lruCache[V]) updateSize(oldSize, newSizeBytes int64) {
Expand All @@ -425,6 +427,8 @@ type cacheEntryPromise[V any] struct {
v V
err error

// reference for the element in the LRU list
// This is used to push this cache entry to the back of the list as result as a cache hit
lElement *list.Element
}

Expand Down
8 changes: 4 additions & 4 deletions pkg/storage/tsdb/expanded_postings_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,15 @@ func TestLru(t *testing.T) {
key := RepeatStringIfNeeded(fmt.Sprintf("key%d", i), keySize)
_, hit := cache.getPromiseForKey(key, func() (int, int64, error) { return 1, 1, nil })
require.False(t, hit)
require.Equal(t, key, cache.cached.Back().Value)
require.Equal(t, key, cache.lruList.Back().Value)
assertCacheItemsCount(t, cache, i+1)
}

for i := 0; i < maxNumberOfCachedItems; i++ {
key := RepeatStringIfNeeded(fmt.Sprintf("key%d", i), keySize)
_, hit := cache.getPromiseForKey(key, func() (int, int64, error) { return 1, 1, nil })
require.True(t, hit)
require.Equal(t, key, cache.cached.Back().Value)
require.Equal(t, key, cache.lruList.Back().Value)
assertCacheItemsCount(t, cache, maxNumberOfCachedItems)
}

Expand All @@ -104,7 +104,7 @@ func TestLru(t *testing.T) {
key := RepeatStringIfNeeded(fmt.Sprintf("key_new%d", i), keySize)
_, hit := cache.getPromiseForKey(key, func() (int, int64, error) { return 1, 1, nil })
require.False(t, hit)
require.Equal(t, maxNumberOfCachedItems, cache.cached.Len())
require.Equal(t, maxNumberOfCachedItems, cache.lruList.Len())
}

for i := 0; i < maxNumberOfCachedItems; i++ {
Expand Down Expand Up @@ -244,7 +244,7 @@ func RepeatStringIfNeeded(seed string, length int) string {
}

func assertCacheItemsCount[T any](t *testing.T, cache *lruCache[T], size int) {
require.Equal(t, size, cache.cached.Len())
require.Equal(t, size, cache.lruList.Len())
count := 0
cache.cachedValues.Range(func(k, v any) bool {
count++
Expand Down

0 comments on commit 602f72e

Please sign in to comment.