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

Use groupcache for LRU cache #375

Merged
merged 1 commit into from
May 3, 2021
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module github.com/prometheus/statsd_exporter

require (
github.com/go-kit/kit v0.10.0
github.com/hashicorp/golang-lru v0.5.4
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da
github.com/prometheus/client_golang v1.9.0
github.com/prometheus/client_model v0.2.0
github.com/prometheus/common v0.19.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ github.com/gogo/protobuf v1.2.1/go.mod h1:hp+jE20tsWTFYpLwKvXlhS1hjn+gTNwPg2I6zV
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
github.com/golang/groupcache v0.0.0-20160516000752-02826c3e7903/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
github.com/golang/groupcache v0.0.0-20190702054246-869f871628b6/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da h1:oI5xCqsCo564l8iNU+DwB5epxmsaqB+rhGL0m5jtYqE=
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
Expand Down Expand Up @@ -123,8 +125,6 @@ github.com/hashicorp/go-version v1.2.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09
github.com/hashicorp/go.net v0.0.1/go.mod h1:hjKkEWcCURg++eb33jQU7oqQcI9XDCnUzHA0oac0k90=
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/golang-lru v0.5.4 h1:YDjusn29QI/Das2iO9M0BHnIbxPeyuCHsjMW+lJfyTc=
github.com/hashicorp/golang-lru v0.5.4/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4=
github.com/hashicorp/logutils v1.0.0/go.mod h1:QIAnNjmIWmVIIkWDTG1z5v++HQmx9WQRO+LraFDTW64=
github.com/hashicorp/mdns v1.0.0/go.mod h1:tL+uN++7HEJ6SQLQ2/p+z2pH24WQKWjBPkE0mNTz8vQ=
github.com/hashicorp/memberlist v0.1.3/go.mod h1:ajVTdAv/9Im8oMAAj5G31PhhMCZJV2pPBoIllUwCN7I=
Expand Down
52 changes: 45 additions & 7 deletions pkg/mappercache/lru/lru.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,17 @@
package lru

import (
"sync"

"github.com/prometheus/client_golang/prometheus"

lru2 "github.com/hashicorp/golang-lru"
"github.com/golang/groupcache/lru"

"github.com/prometheus/statsd_exporter/pkg/mappercache"
)

type metricMapperLRUCache struct {
cache *lru2.Cache
cache *lruCache
metrics *mappercache.CacheMetrics
}

Expand All @@ -32,10 +34,7 @@ func NewMetricMapperLRUCache(reg prometheus.Registerer, size int) (*metricMapper
}

metrics := mappercache.NewCacheMetrics(reg)
cache, err := lru2.New(size)
if err != nil {
return &metricMapperLRUCache{}, err
}
cache := newLruCache(size)

return &metricMapperLRUCache{metrics: metrics, cache: cache}, nil
}
Expand All @@ -60,6 +59,45 @@ func (m *metricMapperLRUCache) trackCacheLength() {
}

func (m *metricMapperLRUCache) Reset() {
m.cache.Purge()
m.cache.Clear()
m.metrics.CacheLength.Set(0)
}

type lruCache struct {
cache *lru.Cache
lock sync.RWMutex
}

func newLruCache(maxEntries int) *lruCache {
return &lruCache{
cache: lru.New(maxEntries),
}
}

func (l *lruCache) Get(key string) (interface{}, bool) {
l.lock.RLock()
defer l.lock.RUnlock()

return l.cache.Get(key)
}

func (l *lruCache) Add(key string, value interface{}) {
l.lock.Lock()
defer l.lock.Unlock()

l.cache.Add(key, value)
}

func (l *lruCache) Len() int {
l.lock.RLock()
defer l.lock.RUnlock()

return l.cache.Len()
}

func (l *lruCache) Clear() {
l.lock.Lock()
defer l.lock.Unlock()

l.cache.Clear()
}