2019-08-13 18:35:19 +00:00
|
|
|
package workingsetcache
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
"sync/atomic"
|
|
|
|
"time"
|
|
|
|
|
2021-07-06 07:39:56 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/bytesutil"
|
2020-12-08 18:49:32 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/cgroup"
|
2019-08-13 18:35:19 +00:00
|
|
|
"github.com/VictoriaMetrics/fastcache"
|
|
|
|
)
|
|
|
|
|
2019-09-08 20:21:13 +00:00
|
|
|
// Cache modes.
|
|
|
|
const (
|
|
|
|
split = 0
|
|
|
|
switching = 1
|
|
|
|
whole = 2
|
|
|
|
)
|
|
|
|
|
2019-08-13 18:35:19 +00:00
|
|
|
// Cache is a cache for working set entries.
|
|
|
|
//
|
|
|
|
// The cache evicts inactive entries after the given expireDuration.
|
|
|
|
// Recently accessed entries survive expireDuration.
|
|
|
|
type Cache struct {
|
|
|
|
curr atomic.Value
|
|
|
|
prev atomic.Value
|
|
|
|
|
2021-12-02 23:20:04 +00:00
|
|
|
// cs holds cache stats
|
|
|
|
cs fastcache.Stats
|
|
|
|
|
2019-09-08 20:21:13 +00:00
|
|
|
// mode indicates whether to use only curr and skip prev.
|
2019-08-15 19:57:43 +00:00
|
|
|
//
|
2019-09-08 20:21:13 +00:00
|
|
|
// This flag is set to switching if curr is filled for more than 50% space.
|
2019-08-15 19:57:43 +00:00
|
|
|
// In this case using prev would result in RAM waste,
|
|
|
|
// it is better to use only curr cache with doubled size.
|
2019-09-08 20:21:13 +00:00
|
|
|
// After the process of switching, this flag will be set to whole.
|
2021-07-05 12:07:38 +00:00
|
|
|
mode uint32
|
|
|
|
|
2019-09-08 20:21:13 +00:00
|
|
|
// mu serializes access to curr, prev and mode
|
2021-07-05 12:07:38 +00:00
|
|
|
// in expirationWatcher and cacheSizeWatcher.
|
2019-08-15 19:57:43 +00:00
|
|
|
mu sync.Mutex
|
|
|
|
|
2019-08-13 18:35:19 +00:00
|
|
|
wg sync.WaitGroup
|
|
|
|
stopCh chan struct{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load 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 Load(filePath string, maxBytes int, expireDuration time.Duration) *Cache {
|
|
|
|
curr := fastcache.LoadFromFileOrNew(filePath, maxBytes)
|
2019-09-08 20:21:13 +00:00
|
|
|
var cs fastcache.Stats
|
|
|
|
curr.UpdateStats(&cs)
|
|
|
|
if cs.EntriesCount == 0 {
|
|
|
|
curr.Reset()
|
|
|
|
// The cache couldn't be loaded with maxBytes size.
|
|
|
|
// This may mean that the cache is split into curr and prev caches.
|
|
|
|
// Try loading it again with maxBytes / 2 size.
|
2021-07-05 14:11:57 +00:00
|
|
|
curr := fastcache.New(maxBytes / 2)
|
2021-07-05 12:07:38 +00:00
|
|
|
prev := fastcache.LoadFromFileOrNew(filePath, maxBytes/2)
|
2021-12-02 08:28:45 +00:00
|
|
|
c := newCacheInternal(curr, prev, split)
|
2021-07-05 12:07:38 +00:00
|
|
|
c.runWatchers(expireDuration)
|
|
|
|
return c
|
2019-09-08 20:21:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// The cache has been successfully loaded in full.
|
|
|
|
// Set its' mode to `whole`.
|
2021-07-05 12:07:38 +00:00
|
|
|
// There is no need in runWatchers call.
|
2021-07-05 14:11:57 +00:00
|
|
|
prev := fastcache.New(1024)
|
2021-12-02 08:28:45 +00:00
|
|
|
return newCacheInternal(curr, prev, whole)
|
2019-08-13 18:35:19 +00:00
|
|
|
}
|
|
|
|
|
2021-12-02 08:28:45 +00:00
|
|
|
// New creates new cache with the given maxBytes capacity and the given expireDuration
|
2019-08-13 18:35:19 +00:00
|
|
|
// for inactive entries.
|
|
|
|
//
|
|
|
|
// Stop must be called on the returned cache when it is no longer needed.
|
|
|
|
func New(maxBytes int, expireDuration time.Duration) *Cache {
|
2021-07-05 14:11:57 +00:00
|
|
|
curr := fastcache.New(maxBytes / 2)
|
|
|
|
prev := fastcache.New(1024)
|
2021-12-02 08:28:45 +00:00
|
|
|
c := newCacheInternal(curr, prev, split)
|
2021-07-05 12:07:38 +00:00
|
|
|
c.runWatchers(expireDuration)
|
|
|
|
return c
|
2019-08-13 18:35:19 +00:00
|
|
|
}
|
|
|
|
|
2021-12-02 08:28:45 +00:00
|
|
|
func newCacheInternal(curr, prev *fastcache.Cache, mode int) *Cache {
|
2019-08-13 18:35:19 +00:00
|
|
|
var c Cache
|
|
|
|
c.curr.Store(curr)
|
|
|
|
c.prev.Store(prev)
|
|
|
|
c.stopCh = make(chan struct{})
|
2021-07-05 12:07:38 +00:00
|
|
|
c.setMode(mode)
|
|
|
|
return &c
|
|
|
|
}
|
2019-08-15 19:57:43 +00:00
|
|
|
|
2021-07-05 12:07:38 +00:00
|
|
|
func (c *Cache) runWatchers(expireDuration time.Duration) {
|
2019-08-15 19:57:43 +00:00
|
|
|
c.wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer c.wg.Done()
|
2021-07-05 12:07:38 +00:00
|
|
|
c.expirationWatcher(expireDuration)
|
2019-08-15 19:57:43 +00:00
|
|
|
}()
|
2019-08-13 18:35:19 +00:00
|
|
|
c.wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer c.wg.Done()
|
2021-07-05 12:07:38 +00:00
|
|
|
c.cacheSizeWatcher()
|
2019-08-15 19:57:43 +00:00
|
|
|
}()
|
|
|
|
}
|
2019-08-13 18:35:19 +00:00
|
|
|
|
2021-07-05 12:07:38 +00:00
|
|
|
func (c *Cache) expirationWatcher(expireDuration time.Duration) {
|
2019-08-15 19:57:43 +00:00
|
|
|
t := time.NewTicker(expireDuration / 2)
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-c.stopCh:
|
|
|
|
t.Stop()
|
|
|
|
return
|
|
|
|
case <-t.C:
|
|
|
|
}
|
|
|
|
|
|
|
|
c.mu.Lock()
|
2021-07-05 12:07:38 +00:00
|
|
|
if atomic.LoadUint32(&c.mode) != split {
|
|
|
|
// Stop the expirationWatcher on non-split mode.
|
|
|
|
c.mu.Unlock()
|
|
|
|
return
|
2019-08-13 18:35:19 +00:00
|
|
|
}
|
2021-12-02 08:28:45 +00:00
|
|
|
// Expire prev cache and create fresh curr cache with the same capacity.
|
|
|
|
// Do not reuse prev cache, since it can occupy too big amounts of memory.
|
2021-07-05 12:07:38 +00:00
|
|
|
prev := c.prev.Load().(*fastcache.Cache)
|
|
|
|
prev.Reset()
|
|
|
|
curr := c.curr.Load().(*fastcache.Cache)
|
2021-12-02 08:28:45 +00:00
|
|
|
var cs fastcache.Stats
|
|
|
|
curr.UpdateStats(&cs)
|
2021-07-05 12:07:38 +00:00
|
|
|
c.prev.Store(curr)
|
2021-12-02 08:28:45 +00:00
|
|
|
curr = fastcache.New(int(cs.MaxBytesSize))
|
2021-07-05 12:07:38 +00:00
|
|
|
c.curr.Store(curr)
|
2019-08-15 19:57:43 +00:00
|
|
|
c.mu.Unlock()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-05 12:07:38 +00:00
|
|
|
func (c *Cache) cacheSizeWatcher() {
|
2019-08-15 19:57:43 +00:00
|
|
|
t := time.NewTicker(time.Minute)
|
2019-09-08 20:21:13 +00:00
|
|
|
defer t.Stop()
|
|
|
|
|
2021-12-02 08:28:45 +00:00
|
|
|
var maxBytesSize uint64
|
2019-08-15 19:57:43 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-c.stopCh:
|
|
|
|
return
|
|
|
|
case <-t.C:
|
|
|
|
}
|
|
|
|
var cs fastcache.Stats
|
|
|
|
curr := c.curr.Load().(*fastcache.Cache)
|
|
|
|
curr.UpdateStats(&cs)
|
2021-12-02 08:28:45 +00:00
|
|
|
if cs.BytesSize >= uint64(0.9*float64(cs.MaxBytesSize)) {
|
|
|
|
maxBytesSize = cs.MaxBytesSize
|
2019-09-08 20:21:13 +00:00
|
|
|
break
|
2019-08-15 19:57:43 +00:00
|
|
|
}
|
2019-09-08 20:21:13 +00:00
|
|
|
}
|
2019-08-15 19:57:43 +00:00
|
|
|
|
2021-12-02 08:28:45 +00:00
|
|
|
// curr cache size exceeds 90% of its capacity. It is better
|
2019-09-08 20:21:13 +00:00
|
|
|
// to double the size of curr cache and stop using prev cache,
|
|
|
|
// since this will result in higher summary cache capacity.
|
|
|
|
//
|
|
|
|
// Do this in the following steps:
|
|
|
|
// 1) switch to mode=switching
|
|
|
|
// 2) move curr cache to prev
|
|
|
|
// 3) create curr with the double size
|
2021-12-02 08:28:45 +00:00
|
|
|
// 4) wait until curr size exceeds maxBytesSize, i.e. it is populated with new data
|
2019-09-08 20:21:13 +00:00
|
|
|
// 5) switch to mode=whole
|
|
|
|
// 6) drop prev
|
|
|
|
|
|
|
|
c.mu.Lock()
|
2021-07-05 12:07:38 +00:00
|
|
|
c.setMode(switching)
|
2019-09-08 20:21:13 +00:00
|
|
|
prev := c.prev.Load().(*fastcache.Cache)
|
|
|
|
prev.Reset()
|
|
|
|
curr := c.curr.Load().(*fastcache.Cache)
|
|
|
|
c.prev.Store(curr)
|
2021-12-02 08:28:45 +00:00
|
|
|
c.curr.Store(fastcache.New(int(maxBytesSize * 2)))
|
2019-09-08 20:21:13 +00:00
|
|
|
c.mu.Unlock()
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-c.stopCh:
|
|
|
|
return
|
|
|
|
case <-t.C:
|
|
|
|
}
|
|
|
|
var cs fastcache.Stats
|
|
|
|
curr := c.curr.Load().(*fastcache.Cache)
|
|
|
|
curr.UpdateStats(&cs)
|
2021-12-02 08:28:45 +00:00
|
|
|
if cs.BytesSize >= maxBytesSize {
|
2019-09-08 20:21:13 +00:00
|
|
|
break
|
|
|
|
}
|
2019-08-15 19:57:43 +00:00
|
|
|
}
|
2019-09-08 20:21:13 +00:00
|
|
|
|
|
|
|
c.mu.Lock()
|
2021-07-05 12:07:38 +00:00
|
|
|
c.setMode(whole)
|
2019-09-08 20:21:13 +00:00
|
|
|
prev = c.prev.Load().(*fastcache.Cache)
|
|
|
|
prev.Reset()
|
|
|
|
c.prev.Store(fastcache.New(1024))
|
|
|
|
c.mu.Unlock()
|
2019-08-13 18:35:19 +00:00
|
|
|
}
|
|
|
|
|
2021-07-05 12:07:38 +00:00
|
|
|
// Save saves the cache to filePath.
|
2019-08-13 18:35:19 +00:00
|
|
|
func (c *Cache) Save(filePath string) error {
|
|
|
|
curr := c.curr.Load().(*fastcache.Cache)
|
2020-12-08 18:49:32 +00:00
|
|
|
concurrency := cgroup.AvailableCPUs()
|
2019-08-13 18:35:19 +00:00
|
|
|
return curr.SaveToFileConcurrent(filePath, concurrency)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop stops the cache.
|
|
|
|
//
|
|
|
|
// The cache cannot be used after the Stop call.
|
|
|
|
func (c *Cache) Stop() {
|
|
|
|
close(c.stopCh)
|
|
|
|
c.wg.Wait()
|
|
|
|
|
|
|
|
c.Reset()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reset resets the cache.
|
|
|
|
func (c *Cache) Reset() {
|
|
|
|
prev := c.prev.Load().(*fastcache.Cache)
|
|
|
|
prev.Reset()
|
|
|
|
curr := c.curr.Load().(*fastcache.Cache)
|
|
|
|
curr.Reset()
|
2021-07-01 08:49:16 +00:00
|
|
|
// Reset the mode to `split` in the hope the working set size becomes smaller after the reset.
|
2021-07-05 12:07:38 +00:00
|
|
|
c.setMode(split)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Cache) setMode(mode int) {
|
|
|
|
atomic.StoreUint32(&c.mode, uint32(mode))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Cache) loadMode() int {
|
|
|
|
return int(atomic.LoadUint32(&c.mode))
|
2019-08-13 18:35:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateStats updates fcs with cache stats.
|
|
|
|
func (c *Cache) UpdateStats(fcs *fastcache.Stats) {
|
2021-07-06 07:39:56 +00:00
|
|
|
var cs fastcache.Stats
|
2019-08-13 18:35:19 +00:00
|
|
|
curr := c.curr.Load().(*fastcache.Cache)
|
2021-07-06 07:39:56 +00:00
|
|
|
curr.UpdateStats(&cs)
|
|
|
|
fcs.Collisions += cs.Collisions
|
|
|
|
fcs.Corruptions += cs.Corruptions
|
|
|
|
fcs.EntriesCount += cs.EntriesCount
|
|
|
|
fcs.BytesSize += cs.BytesSize
|
2021-12-02 08:28:45 +00:00
|
|
|
fcs.MaxBytesSize += cs.MaxBytesSize
|
2020-04-10 08:49:06 +00:00
|
|
|
|
2021-07-06 07:39:56 +00:00
|
|
|
fcs.GetCalls += atomic.LoadUint64(&c.cs.GetCalls)
|
|
|
|
fcs.SetCalls += atomic.LoadUint64(&c.cs.SetCalls)
|
|
|
|
fcs.Misses += atomic.LoadUint64(&c.cs.Misses)
|
2019-08-13 18:35:19 +00:00
|
|
|
|
|
|
|
prev := c.prev.Load().(*fastcache.Cache)
|
2021-07-06 07:39:56 +00:00
|
|
|
cs.Reset()
|
|
|
|
prev.UpdateStats(&cs)
|
|
|
|
fcs.EntriesCount += cs.EntriesCount
|
|
|
|
fcs.BytesSize += cs.BytesSize
|
2021-12-02 08:28:45 +00:00
|
|
|
fcs.MaxBytesSize += cs.MaxBytesSize
|
2019-08-13 18:35:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get appends the found value for the given key to dst and returns the result.
|
|
|
|
func (c *Cache) Get(dst, key []byte) []byte {
|
2021-07-06 07:39:56 +00:00
|
|
|
atomic.AddUint64(&c.cs.GetCalls, 1)
|
2019-08-13 18:35:19 +00:00
|
|
|
curr := c.curr.Load().(*fastcache.Cache)
|
|
|
|
result := curr.Get(dst, key)
|
|
|
|
if len(result) > len(dst) {
|
|
|
|
// Fast path - the entry is found in the current cache.
|
|
|
|
return result
|
|
|
|
}
|
2021-07-05 12:07:38 +00:00
|
|
|
if c.loadMode() == whole {
|
2021-07-06 07:39:56 +00:00
|
|
|
// Nothing found.
|
|
|
|
atomic.AddUint64(&c.cs.Misses, 1)
|
2019-08-13 18:35:19 +00:00
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
// Search for the entry in the previous cache.
|
|
|
|
prev := c.prev.Load().(*fastcache.Cache)
|
|
|
|
result = prev.Get(dst, key)
|
|
|
|
if len(result) <= len(dst) {
|
|
|
|
// Nothing found.
|
2021-07-06 07:39:56 +00:00
|
|
|
atomic.AddUint64(&c.cs.Misses, 1)
|
2019-08-13 18:35:19 +00:00
|
|
|
return result
|
|
|
|
}
|
|
|
|
// Cache the found entry in the current cache.
|
|
|
|
curr.Set(key, result[len(dst):])
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2021-07-06 07:39:56 +00:00
|
|
|
// Has verifies whether the cache contains the given key.
|
2019-08-13 18:35:19 +00:00
|
|
|
func (c *Cache) Has(key []byte) bool {
|
2021-07-06 07:39:56 +00:00
|
|
|
atomic.AddUint64(&c.cs.GetCalls, 1)
|
2019-08-13 18:35:19 +00:00
|
|
|
curr := c.curr.Load().(*fastcache.Cache)
|
|
|
|
if curr.Has(key) {
|
|
|
|
return true
|
|
|
|
}
|
2021-07-05 12:07:38 +00:00
|
|
|
if c.loadMode() == whole {
|
2021-07-06 07:39:56 +00:00
|
|
|
atomic.AddUint64(&c.cs.Misses, 1)
|
2019-08-13 18:35:19 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
prev := c.prev.Load().(*fastcache.Cache)
|
2021-07-06 07:39:56 +00:00
|
|
|
if !prev.Has(key) {
|
|
|
|
atomic.AddUint64(&c.cs.Misses, 1)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
// Cache the found entry in the current cache.
|
|
|
|
tmpBuf := tmpBufPool.Get()
|
|
|
|
tmpBuf.B = prev.Get(tmpBuf.B, key)
|
|
|
|
curr.Set(key, tmpBuf.B)
|
|
|
|
tmpBufPool.Put(tmpBuf)
|
|
|
|
return true
|
2019-08-13 18:35:19 +00:00
|
|
|
}
|
|
|
|
|
2021-07-06 07:39:56 +00:00
|
|
|
var tmpBufPool bytesutil.ByteBufferPool
|
|
|
|
|
2019-08-13 18:35:19 +00:00
|
|
|
// Set sets the given value for the given key.
|
|
|
|
func (c *Cache) Set(key, value []byte) {
|
2021-07-06 07:39:56 +00:00
|
|
|
atomic.AddUint64(&c.cs.SetCalls, 1)
|
2019-08-13 18:35:19 +00:00
|
|
|
curr := c.curr.Load().(*fastcache.Cache)
|
|
|
|
curr.Set(key, value)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetBig appends the found value for the given key to dst and returns the result.
|
|
|
|
func (c *Cache) GetBig(dst, key []byte) []byte {
|
2021-07-06 07:39:56 +00:00
|
|
|
atomic.AddUint64(&c.cs.GetCalls, 1)
|
2019-08-13 18:35:19 +00:00
|
|
|
curr := c.curr.Load().(*fastcache.Cache)
|
|
|
|
result := curr.GetBig(dst, key)
|
|
|
|
if len(result) > len(dst) {
|
|
|
|
// Fast path - the entry is found in the current cache.
|
|
|
|
return result
|
|
|
|
}
|
2021-07-05 12:07:38 +00:00
|
|
|
if c.loadMode() == whole {
|
2021-07-06 07:39:56 +00:00
|
|
|
// Nothing found.
|
|
|
|
atomic.AddUint64(&c.cs.Misses, 1)
|
2019-08-13 18:35:19 +00:00
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
// Search for the entry in the previous cache.
|
|
|
|
prev := c.prev.Load().(*fastcache.Cache)
|
|
|
|
result = prev.GetBig(dst, key)
|
|
|
|
if len(result) <= len(dst) {
|
|
|
|
// Nothing found.
|
2021-07-06 07:39:56 +00:00
|
|
|
atomic.AddUint64(&c.cs.Misses, 1)
|
2019-08-13 18:35:19 +00:00
|
|
|
return result
|
|
|
|
}
|
|
|
|
// Cache the found entry in the current cache.
|
|
|
|
curr.SetBig(key, result[len(dst):])
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetBig sets the given value for the given key.
|
|
|
|
func (c *Cache) SetBig(key, value []byte) {
|
2021-07-06 07:39:56 +00:00
|
|
|
atomic.AddUint64(&c.cs.SetCalls, 1)
|
2019-08-13 18:35:19 +00:00
|
|
|
curr := c.curr.Load().(*fastcache.Cache)
|
|
|
|
curr.SetBig(key, value)
|
|
|
|
}
|