Skip to content

Commit

Permalink
add local cache stats in the integration test
Browse files Browse the repository at this point in the history
  • Loading branch information
Junchao Lyu committed Jan 15, 2020
1 parent 3ec0f5f commit c0d1f48
Show file tree
Hide file tree
Showing 6 changed files with 263 additions and 108 deletions.
43 changes: 43 additions & 0 deletions src/redis/local_cache_stats.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package redis

import (
"github.com/coocood/freecache"
stats "github.com/lyft/gostats"
)

type localCacheStats struct {
cache *freecache.Cache
evacuateCount stats.Gauge
expiredCount stats.Gauge
entryCount stats.Gauge
averageAccessTime stats.Gauge
hitCount stats.Gauge
missCount stats.Gauge
lookupCount stats.Gauge
overwriteCount stats.Gauge
}

func NewLocalCacheStats(localCache *freecache.Cache, scope stats.Scope) stats.StatGenerator {
return localCacheStats{
cache: localCache,
evacuateCount: scope.NewGauge("evacuateCount"),
expiredCount: scope.NewGauge("expiredCount"),
entryCount: scope.NewGauge("entryCount"),
averageAccessTime: scope.NewGauge("averageAccessTime"),
hitCount: scope.NewGauge("hitCount"),
missCount: scope.NewGauge("missCount"),
lookupCount: scope.NewGauge("lookupCount"),
overwriteCount: scope.NewGauge("overwriteCount"),
}
}

func (stats localCacheStats) GenerateStats() {
stats.evacuateCount.Set(uint64(stats.cache.EvacuateCount()))
stats.expiredCount.Set(uint64(stats.cache.ExpiredCount()))
stats.entryCount.Set(uint64(stats.cache.EntryCount()))
stats.averageAccessTime.Set(uint64(stats.cache.AverageAccessTime()))
stats.hitCount.Set(uint64(stats.cache.HitCount()))
stats.missCount.Set(uint64(stats.cache.MissCount()))
stats.lookupCount.Set(uint64(stats.cache.LookupCount()))
stats.overwriteCount.Set(uint64(stats.cache.OverwriteCount()))
}
12 changes: 9 additions & 3 deletions src/server/server_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package server
import (
"expvar"
"fmt"
"github.com/lyft/ratelimit/src/redis"
"io"
"net/http"
"net/http/pprof"
Expand All @@ -14,6 +15,7 @@ import (

"net"

"github.com/coocood/freecache"
"github.com/gorilla/mux"
reuseport "github.com/kavu/go_reuseport"
"github.com/lyft/goruntime/loader"
Expand Down Expand Up @@ -99,11 +101,12 @@ func (server *server) Runtime() loader.IFace {
return server.runtime
}

func NewServer(name string, store stats.Store, opts ...settings.Option) Server {
return newServer(name, store, opts...)

func NewServer(name string, store stats.Store, localCache *freecache.Cache, opts ...settings.Option) Server {
return newServer(name, store, localCache, opts...)
}

func newServer(name string, store stats.Store, opts ...settings.Option) *server {
func newServer(name string, store stats.Store, localCache *freecache.Cache, opts ...settings.Option) *server {
s := settings.NewSettings()

for _, opt := range opts {
Expand All @@ -122,6 +125,9 @@ func newServer(name string, store stats.Store, opts ...settings.Option) *server
ret.store = store
ret.scope = ret.store.Scope(name)
ret.store.AddStatGenerator(stats.NewRuntimeStats(ret.scope.Scope("go")))
if localCache != nil {
ret.store.AddStatGenerator(redis.NewLocalCacheStats(localCache, ret.scope.Scope("localcache")))
}

// setup runtime
loaderOpts := make([]loader.Option, 0, 1)
Expand Down
10 changes: 5 additions & 5 deletions src/service_cmd/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,12 @@ func (runner *Runner) Run() {
} else {
logger.SetLevel(logLevel)
}
var localCache *freecache.Cache
if s.LocalCacheSizeInBytes != 0 {
localCache = freecache.NewCache(s.LocalCacheSizeInBytes)
}

srv := server.NewServer("ratelimit", runner.statsStore, settings.GrpcUnaryInterceptor(nil))
srv := server.NewServer("ratelimit", runner.statsStore, localCache, settings.GrpcUnaryInterceptor(nil))

var perSecondPool redis.Pool
if s.RedisPerSecond {
Expand All @@ -52,10 +56,6 @@ func (runner *Runner) Run() {
var otherPool redis.Pool
otherPool = redis.NewPoolImpl(srv.Scope().Scope("redis_pool"), s.RedisTls, s.RedisAuth, s.RedisUrl, s.RedisPoolSize)

var localCache *freecache.Cache
if s.LocalCacheSizeInBytes != 0 {
localCache = freecache.NewCache(s.LocalCacheSizeInBytes)
}
service := ratelimit.NewService(
srv.Runtime(),
redis.NewRateLimitCacheImpl(
Expand Down
31 changes: 31 additions & 0 deletions test/common/common.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,42 @@
package common

import (
"sync"

pb_struct "github.com/envoyproxy/go-control-plane/envoy/api/v2/ratelimit"
pb "github.com/envoyproxy/go-control-plane/envoy/service/ratelimit/v2"
pb_legacy "github.com/lyft/ratelimit/proto/ratelimit"
)

type TestStatSink struct {
sync.Mutex
Record map[string]interface{}
}

func (s *TestStatSink) Clear() {
s.Lock()
s.Record = map[string]interface{}{}
s.Unlock()
}

func (s *TestStatSink) FlushCounter(name string, value uint64) {
s.Lock()
s.Record[name] = value
s.Unlock()
}

func (s *TestStatSink) FlushGauge(name string, value uint64) {
s.Lock()
s.Record[name] = value
s.Unlock()
}

func (s *TestStatSink) FlushTimer(name string, value float64) {
s.Lock()
s.Record[name] = value
s.Unlock()
}

func NewRateLimitRequest(domain string, descriptors [][][2]string, hitsAddend uint32) *pb.RateLimitRequest {
request := &pb.RateLimitRequest{}
request.Domain = domain
Expand Down
Loading

0 comments on commit c0d1f48

Please sign in to comment.