From 353396aa23fa79b7f609741f36ecba8ff63c204b Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Thu, 17 Nov 2022 19:59:10 +0200 Subject: [PATCH] lib/workingsetcache: expose -cacheExpireDuration command-line flag for fine-tuning of the cache expiration While at it, decrease -prevCacheRemovalPercent from 0.2 to 0.1 and increase -cacheExpireDuration from 20 minutes to 30 minutes. This is needed for https://github.com/VictoriaMetrics/VictoriaMetrics/issues/3343 --- lib/workingsetcache/cache.go | 30 ++++++++++++------------------ 1 file changed, 12 insertions(+), 18 deletions(-) diff --git a/lib/workingsetcache/cache.go b/lib/workingsetcache/cache.go index 3e271029b..be7027976 100644 --- a/lib/workingsetcache/cache.go +++ b/lib/workingsetcache/cache.go @@ -11,8 +11,12 @@ import ( "github.com/VictoriaMetrics/fastcache" ) -var prevCacheRemovalPercent = flag.Float64("prevCacheRemovalPercent", 0.2, "The previous cache is removed when the percent of requests it serves becomes lower than this value. "+ - "Higher values reduce average memory usage at the cost of higher CPU usage") +var ( + prevCacheRemovalPercent = flag.Float64("prevCacheRemovalPercent", 0.1, "Items in the previous caches are removed when the percent of requests it serves "+ + "becomes lower than this value. Higher values reduce memory usage at the cost of higher CPU usage. See also -cacheExpireDuration") + cacheExpireDuration = flag.Duration("cacheExpireDuration", 30*time.Minute, "Items are removed from in-memory caches after they aren't accessed for this duration. "+ + "Lower values may reduce memory usage at the cost of higher CPU usage. See also -prevCacheRemovalPercent") +) // Cache modes. const ( @@ -21,8 +25,6 @@ const ( whole = 2 ) -const defaultExpireDuration = 20 * time.Minute - // Cache is a cache for working set entries. // // The cache evicts inactive entries after the given expireDuration. @@ -58,14 +60,10 @@ type Cache struct { // // Stop must be called on the returned cache when it is no longer needed. func Load(filePath string, maxBytes int) *Cache { - return LoadWithExpire(filePath, maxBytes, defaultExpireDuration) + return loadWithExpire(filePath, maxBytes, *cacheExpireDuration) } -// LoadWithExpire loads the cache from filePath and limits its size to maxBytes -// and evicts inactive entires after expireDuration. -// -// Stop must be called on the returned cache when it is no longer needed. -func LoadWithExpire(filePath string, maxBytes int, expireDuration time.Duration) *Cache { +func loadWithExpire(filePath string, maxBytes int, expireDuration time.Duration) *Cache { curr := fastcache.LoadFromFileOrNew(filePath, maxBytes) var cs fastcache.Stats curr.UpdateStats(&cs) @@ -94,14 +92,10 @@ func LoadWithExpire(filePath string, maxBytes int, expireDuration time.Duration) // // Stop must be called on the returned cache when it is no longer needed. func New(maxBytes int) *Cache { - return NewWithExpire(maxBytes, defaultExpireDuration) + return newWithExpire(maxBytes, *cacheExpireDuration) } -// NewWithExpire creates new cache with the given maxBytes capacity and the given expireDuration -// for inactive entries. -// -// Stop must be called on the returned cache when it is no longer needed. -func NewWithExpire(maxBytes int, expireDuration time.Duration) *Cache { +func newWithExpire(maxBytes int, expireDuration time.Duration) *Cache { curr := fastcache.New(maxBytes / 2) prev := fastcache.New(1024) c := newCacheInternal(curr, prev, split, maxBytes) @@ -175,8 +169,8 @@ func (c *Cache) prevCacheWatcher() { minCurrRequests := uint64(1 / p) // Watch for the usage of the prev cache and drop it whenever it receives - // less than prevCacheRemovalPercent requests comparing to the curr cache during the last 30 seconds. - checkInterval := 30 * time.Second + // less than prevCacheRemovalPercent requests comparing to the curr cache during the last 60 seconds. + checkInterval := 60 * time.Second checkInterval += timeJitter(checkInterval / 10) t := time.NewTicker(checkInterval) defer t.Stop()