Skip to content

Commit

Permalink
cache: handle default arguments for both cache types
Browse files Browse the repository at this point in the history
Signed-off-by: Vicent Marti <vmg@strn.cat>
  • Loading branch information
vmg committed Feb 4, 2021
1 parent 9fe7cc0 commit a3feeb8
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 15 deletions.
25 changes: 25 additions & 0 deletions go/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,31 @@ package cache
// a LRU cache).
const DefaultCacheSize = SizeInEntries(5000)

// GuessCapacity returns a Capacity value for a cache instance based on the defaults in Vitess
// and the options passed by the user
func GuessCapacity(inEntries, inBytes int64) Capacity {
switch {
// If the default cache has a byte capacity, only override it if the user has explicitly
// passed a capacity in entries
case DefaultCacheSize.Bytes() != 0:
if inEntries != 0 {
return SizeInEntries(inEntries)
}
return SizeInBytes(inBytes)

// If the default cache has capacity in entries, only override it if the user has explicitly
// passed a capacity in bytes
case DefaultCacheSize.Entries() != 0:
if inBytes != 0 {
return SizeInBytes(inBytes)
}
return SizeInEntries(inEntries)

default:
panic("DefaultCacheSize is not initialized")
}
}

// const DefaultCacheSize = SizeInBytes(64 * 1024 * 1024)

// Cache is a generic interface type for a data structure that keeps recently used
Expand Down
12 changes: 2 additions & 10 deletions go/vt/vtgate/vtgate.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,11 +179,7 @@ func Init(ctx context.Context, serv srvtopo.Server, cell string, tabletTypesToWa
srvResolver := srvtopo.NewResolver(serv, gw, cell)
resolver := NewResolver(srvResolver, serv, cell, sc)
vsm := newVStreamManager(srvResolver, serv, cell)

var cacheSize cache.Capacity = cache.SizeInEntries(*queryPlanCacheSize)
if *lfuQueryPlanCacheSizeBytes != 0 {
cacheSize = cache.SizeInBytes(*lfuQueryPlanCacheSizeBytes)
}
cacheSize := cache.GuessCapacity(*queryPlanCacheSize, *lfuQueryPlanCacheSizeBytes)

rpcVTGate = &VTGate{
executor: NewExecutor(ctx, serv, cell, resolver, *normalizeQueries, *streamBufferSize, cacheSize),
Expand Down Expand Up @@ -503,11 +499,7 @@ func LegacyInit(ctx context.Context, hc discovery.LegacyHealthCheck, serv srvtop
srvResolver := srvtopo.NewResolver(serv, gw, cell)
resolver := NewResolver(srvResolver, serv, cell, sc)
vsm := newVStreamManager(srvResolver, serv, cell)

var cacheSize cache.Capacity = cache.SizeInEntries(*queryPlanCacheSize)
if *lfuQueryPlanCacheSizeBytes != 0 {
cacheSize = cache.SizeInBytes(*lfuQueryPlanCacheSizeBytes)
}
cacheSize := cache.GuessCapacity(*queryPlanCacheSize, *lfuQueryPlanCacheSizeBytes)

rpcVTGate = &VTGate{
executor: NewExecutor(ctx, serv, cell, resolver, *normalizeQueries, *streamBufferSize, cacheSize),
Expand Down
6 changes: 1 addition & 5 deletions go/vt/vttablet/tabletserver/query_engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,11 +165,7 @@ type QueryEngine struct {
// You must call this only once.
func NewQueryEngine(env tabletenv.Env, se *schema.Engine) *QueryEngine {
config := env.Config()

var cacheSize cache.Capacity = cache.SizeInEntries(config.QueryCacheSize)
if config.LFUQueryCacheSizeBytes != 0 {
cacheSize = cache.SizeInBytes(config.LFUQueryCacheSizeBytes)
}
cacheSize := cache.GuessCapacity(int64(config.QueryCacheSize), config.LFUQueryCacheSizeBytes)

qe := &QueryEngine{
env: env,
Expand Down

0 comments on commit a3feeb8

Please sign in to comment.