2016-07-04 01:58:21 +02:00
|
|
|
/*
|
2017-03-02 19:34:37 +01:00
|
|
|
* Minio Cloud Storage, (C) 2016, 2017 Minio, Inc.
|
2016-07-04 01:58:21 +02:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Package objcache implements in memory caching methods.
|
|
|
|
package objcache
|
|
|
|
|
|
|
|
import (
|
2016-07-06 10:29:49 +02:00
|
|
|
"bytes"
|
2016-07-04 01:58:21 +02:00
|
|
|
"errors"
|
|
|
|
"io"
|
2016-12-09 05:35:07 +01:00
|
|
|
"runtime/debug"
|
2016-07-04 01:58:21 +02:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2017-03-02 19:34:37 +01:00
|
|
|
const (
|
|
|
|
// NoExpiry represents caches to be permanent and can only be deleted.
|
|
|
|
NoExpiry = time.Duration(0)
|
2016-07-09 05:34:27 +02:00
|
|
|
|
2017-03-02 19:34:37 +01:00
|
|
|
// DefaultExpiry represents three days time duration when individual entries will be expired.
|
|
|
|
DefaultExpiry = time.Duration(3 * 24 * time.Hour)
|
2016-07-09 05:34:27 +02:00
|
|
|
|
2017-03-02 19:34:37 +01:00
|
|
|
// defaultBufferRatio represents default ratio used to calculate the
|
|
|
|
// individual cache entry buffer size.
|
|
|
|
defaultBufferRatio = uint64(10)
|
2016-12-09 05:35:07 +01:00
|
|
|
|
2017-03-02 19:34:37 +01:00
|
|
|
// defaultGCPercent represents default garbage collection target percentage.
|
|
|
|
defaultGCPercent = 20
|
|
|
|
)
|
2016-12-09 05:35:07 +01:00
|
|
|
|
2016-07-09 05:34:27 +02:00
|
|
|
// buffer represents the in memory cache of a single entry.
|
|
|
|
// buffer carries value of the data and last accessed time.
|
|
|
|
type buffer struct {
|
|
|
|
value []byte // Value of the entry.
|
|
|
|
lastAccessed time.Time // Represents time when value was last accessed.
|
|
|
|
}
|
2016-07-04 01:58:21 +02:00
|
|
|
|
|
|
|
// Cache holds the required variables to compose an in memory cache system
|
|
|
|
// which also provides expiring key mechanism and also maxSize.
|
|
|
|
type Cache struct {
|
|
|
|
// Mutex is used for handling the concurrent
|
|
|
|
// read/write requests for cache
|
2016-07-09 05:34:27 +02:00
|
|
|
mutex sync.Mutex
|
2016-07-04 01:58:21 +02:00
|
|
|
|
2016-12-09 05:35:07 +01:00
|
|
|
// Once is used for resetting GC once after
|
|
|
|
// peak cache usage.
|
|
|
|
onceGC sync.Once
|
|
|
|
|
2016-07-04 01:58:21 +02:00
|
|
|
// maxSize is a total size for overall cache
|
|
|
|
maxSize uint64
|
|
|
|
|
2016-12-09 05:35:07 +01:00
|
|
|
// maxCacheEntrySize is a total size per key buffer.
|
|
|
|
maxCacheEntrySize uint64
|
|
|
|
|
2016-07-04 01:58:21 +02:00
|
|
|
// currentSize is a current size in memory
|
|
|
|
currentSize uint64
|
|
|
|
|
|
|
|
// OnEviction - callback function for eviction
|
2016-07-09 05:34:27 +02:00
|
|
|
OnEviction func(key string)
|
2016-07-04 01:58:21 +02:00
|
|
|
|
2016-07-09 05:34:27 +02:00
|
|
|
// totalEvicted counter to keep track of total expirys
|
2016-07-04 01:58:21 +02:00
|
|
|
totalEvicted int
|
|
|
|
|
2016-07-06 10:29:49 +02:00
|
|
|
// map of objectName and its contents
|
2016-07-09 05:34:27 +02:00
|
|
|
entries map[string]*buffer
|
2016-07-04 01:58:21 +02:00
|
|
|
|
2016-07-09 05:34:27 +02:00
|
|
|
// Expiry in time duration.
|
2016-07-04 01:58:21 +02:00
|
|
|
expiry time.Duration
|
2016-07-09 05:34:27 +02:00
|
|
|
|
|
|
|
// Stop garbage collection routine, stops any running GC routine.
|
|
|
|
stopGC chan struct{}
|
2016-07-04 01:58:21 +02:00
|
|
|
}
|
|
|
|
|
2016-12-09 05:35:07 +01:00
|
|
|
// New - Return a new cache with a given default expiry
|
|
|
|
// duration. If the expiry duration is less than one
|
|
|
|
// (or NoExpiry), the items in the cache never expire
|
|
|
|
// (by default), and must be deleted manually.
|
2017-03-02 19:34:37 +01:00
|
|
|
func New(maxSize uint64, expiry time.Duration) (c *Cache, err error) {
|
2016-12-07 01:09:26 +01:00
|
|
|
if maxSize == 0 {
|
2017-03-02 19:34:37 +01:00
|
|
|
err = errors.New("invalid maximum cache size")
|
|
|
|
return c, err
|
2016-12-07 01:09:26 +01:00
|
|
|
}
|
2016-12-09 05:35:07 +01:00
|
|
|
|
|
|
|
// A garbage collection is triggered when the ratio
|
|
|
|
// of freshly allocated data to live data remaining
|
|
|
|
// after the previous collection reaches this percentage.
|
|
|
|
//
|
|
|
|
// - https://golang.org/pkg/runtime/debug/#SetGCPercent
|
|
|
|
//
|
|
|
|
// This means that by default GC is triggered after
|
|
|
|
// we've allocated an extra amount of memory proportional
|
|
|
|
// to the amount already in use.
|
|
|
|
//
|
|
|
|
// If gcpercent=100 and we're using 4M, we'll gc again
|
|
|
|
// when we get to 8M.
|
|
|
|
//
|
|
|
|
// Set this value to 20% if caching is enabled.
|
2017-03-02 19:34:37 +01:00
|
|
|
debug.SetGCPercent(defaultGCPercent)
|
2016-12-09 05:35:07 +01:00
|
|
|
|
|
|
|
// Max cache entry size - indicates the
|
|
|
|
// maximum buffer per key that can be held in
|
|
|
|
// memory. Currently this value is 1/10th
|
|
|
|
// the size of requested cache size.
|
|
|
|
maxCacheEntrySize := func() uint64 {
|
2017-03-02 19:34:37 +01:00
|
|
|
i := maxSize / defaultBufferRatio
|
2016-12-09 05:35:07 +01:00
|
|
|
if i == 0 {
|
|
|
|
i = maxSize
|
|
|
|
}
|
|
|
|
return i
|
|
|
|
}()
|
2017-03-02 19:34:37 +01:00
|
|
|
c = &Cache{
|
2016-12-09 05:35:07 +01:00
|
|
|
onceGC: sync.Once{},
|
|
|
|
maxSize: maxSize,
|
|
|
|
maxCacheEntrySize: maxCacheEntrySize,
|
|
|
|
entries: make(map[string]*buffer),
|
|
|
|
expiry: expiry,
|
2016-07-04 01:58:21 +02:00
|
|
|
}
|
2016-07-09 05:34:27 +02:00
|
|
|
// We have expiry start the janitor routine.
|
|
|
|
if expiry > 0 {
|
2016-12-09 05:35:07 +01:00
|
|
|
// Initialize a new stop GC channel.
|
|
|
|
c.stopGC = make(chan struct{})
|
2016-07-09 05:34:27 +02:00
|
|
|
|
|
|
|
// Start garbage collection routine to expire objects.
|
2016-12-09 05:35:07 +01:00
|
|
|
c.StartGC()
|
2016-07-09 05:34:27 +02:00
|
|
|
}
|
2017-03-02 19:34:37 +01:00
|
|
|
return c, nil
|
2016-07-04 01:58:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// ErrKeyNotFoundInCache - key not found in cache.
|
|
|
|
var ErrKeyNotFoundInCache = errors.New("Key not found in cache")
|
|
|
|
|
|
|
|
// ErrCacheFull - cache is full.
|
|
|
|
var ErrCacheFull = errors.New("Not enough space in cache")
|
|
|
|
|
2016-07-09 05:34:27 +02:00
|
|
|
// ErrExcessData - excess data was attempted to be written on cache.
|
|
|
|
var ErrExcessData = errors.New("Attempted excess write on cache")
|
|
|
|
|
2016-07-06 10:29:49 +02:00
|
|
|
// Create - validates if object size fits with in cache size limit and returns a io.WriteCloser
|
|
|
|
// to which object contents can be written and finally Close()'d. During Close() we
|
|
|
|
// checks if the amount of data written is equal to the size of the object, in which
|
|
|
|
// case it saves the contents to object cache.
|
|
|
|
func (c *Cache) Create(key string, size int64) (w io.WriteCloser, err error) {
|
2016-07-09 05:34:27 +02:00
|
|
|
// Recovers any panic generated and return errors appropriately.
|
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
// Recover any panic and return ErrCacheFull.
|
|
|
|
err = ErrCacheFull
|
|
|
|
}
|
|
|
|
}() // Do not crash the server.
|
2016-07-04 01:58:21 +02:00
|
|
|
|
|
|
|
valueLen := uint64(size)
|
2016-12-09 05:35:07 +01:00
|
|
|
// Check if the size of the object is > 1/10th the size
|
|
|
|
// of the cache, if yes then we ignore it.
|
|
|
|
if valueLen > c.maxCacheEntrySize {
|
|
|
|
return nil, ErrCacheFull
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the incoming size is going to exceed
|
|
|
|
// the effective cache size, if yes return error
|
|
|
|
// instead.
|
|
|
|
c.mutex.Lock()
|
|
|
|
if c.currentSize+valueLen > c.maxSize {
|
|
|
|
c.mutex.Unlock()
|
2016-07-09 05:34:27 +02:00
|
|
|
return nil, ErrCacheFull
|
2016-07-04 01:58:21 +02:00
|
|
|
}
|
2016-12-09 05:35:07 +01:00
|
|
|
// Change GC percent if the current cache usage
|
|
|
|
// is already 75% of the maximum allowed usage.
|
|
|
|
if c.currentSize > (75 * c.maxSize / 100) {
|
2017-03-02 19:34:37 +01:00
|
|
|
c.onceGC.Do(func() { debug.SetGCPercent(defaultGCPercent - 10) })
|
2016-12-09 05:35:07 +01:00
|
|
|
}
|
|
|
|
c.mutex.Unlock()
|
2016-07-06 10:29:49 +02:00
|
|
|
|
2016-12-09 05:35:07 +01:00
|
|
|
cbuf := &cappedWriter{
|
|
|
|
offset: 0,
|
|
|
|
cap: size,
|
|
|
|
buffer: make([]byte, size),
|
|
|
|
}
|
2016-07-06 10:29:49 +02:00
|
|
|
|
|
|
|
// Function called on close which saves the object contents
|
|
|
|
// to the object cache.
|
2016-07-09 05:34:27 +02:00
|
|
|
onClose := func() error {
|
2016-07-06 10:29:49 +02:00
|
|
|
c.mutex.Lock()
|
|
|
|
defer c.mutex.Unlock()
|
2016-12-09 05:35:07 +01:00
|
|
|
if size != cbuf.offset {
|
|
|
|
cbuf.Reset() // Reset resets the buffer to be empty.
|
2016-07-06 10:29:49 +02:00
|
|
|
// Full object not available hence do not save buf to object cache.
|
2016-07-09 05:34:27 +02:00
|
|
|
return io.ErrShortBuffer
|
|
|
|
}
|
2016-07-06 10:29:49 +02:00
|
|
|
// Full object available in buf, save it to cache.
|
2016-07-09 05:34:27 +02:00
|
|
|
c.entries[key] = &buffer{
|
2016-12-09 05:35:07 +01:00
|
|
|
value: cbuf.buffer,
|
2016-07-09 05:34:27 +02:00
|
|
|
lastAccessed: time.Now().UTC(), // Save last accessed time.
|
|
|
|
}
|
|
|
|
// Account for the memory allocated above.
|
|
|
|
c.currentSize += uint64(size)
|
|
|
|
return nil
|
2016-07-06 10:29:49 +02:00
|
|
|
}
|
|
|
|
|
2016-12-09 05:35:07 +01:00
|
|
|
// Object contents that is written - cappedWriter.Write(data)
|
2016-07-06 10:29:49 +02:00
|
|
|
// will be accumulated in buf which implements io.Writer.
|
2016-12-09 05:35:07 +01:00
|
|
|
cbuf.onClose = onClose
|
|
|
|
return cbuf, nil
|
2016-07-04 01:58:21 +02:00
|
|
|
}
|
|
|
|
|
2016-07-05 10:04:50 +02:00
|
|
|
// Open - open the in-memory file, returns an in memory read seeker.
|
|
|
|
// returns an error ErrNotFoundInCache, if the key does not exist.
|
2016-09-24 04:55:28 +02:00
|
|
|
// Returns ErrKeyNotFoundInCache if entry's lastAccessedTime is older
|
|
|
|
// than objModTime.
|
2017-02-12 02:17:58 +01:00
|
|
|
func (c *Cache) Open(key string, objModTime time.Time) (io.ReaderAt, error) {
|
2016-07-04 01:58:21 +02:00
|
|
|
// Entry exists, return the readable buffer.
|
2016-07-09 05:34:27 +02:00
|
|
|
c.mutex.Lock()
|
|
|
|
defer c.mutex.Unlock()
|
|
|
|
buf, ok := c.entries[key]
|
2016-07-04 01:58:21 +02:00
|
|
|
if !ok {
|
|
|
|
return nil, ErrKeyNotFoundInCache
|
|
|
|
}
|
2016-09-24 04:55:28 +02:00
|
|
|
// Check if buf is recent copy of the object on disk.
|
|
|
|
if buf.lastAccessed.Before(objModTime) {
|
|
|
|
c.delete(key)
|
|
|
|
return nil, ErrKeyNotFoundInCache
|
|
|
|
}
|
2016-07-09 05:34:27 +02:00
|
|
|
buf.lastAccessed = time.Now().UTC()
|
|
|
|
return bytes.NewReader(buf.value), nil
|
2016-07-04 01:58:21 +02:00
|
|
|
}
|
|
|
|
|
2016-07-09 05:34:27 +02:00
|
|
|
// Delete - delete deletes an entry from the cache.
|
2016-07-04 01:58:21 +02:00
|
|
|
func (c *Cache) Delete(key string) {
|
|
|
|
c.mutex.Lock()
|
2016-07-09 05:34:27 +02:00
|
|
|
c.delete(key)
|
|
|
|
c.mutex.Unlock()
|
|
|
|
if c.OnEviction != nil {
|
|
|
|
c.OnEviction(key)
|
|
|
|
}
|
|
|
|
}
|
2016-07-04 01:58:21 +02:00
|
|
|
|
2016-07-09 05:34:27 +02:00
|
|
|
// gc - garbage collect all the expired entries from the cache.
|
|
|
|
func (c *Cache) gc() {
|
|
|
|
var evictedEntries []string
|
|
|
|
c.mutex.Lock()
|
|
|
|
for k, v := range c.entries {
|
|
|
|
if c.expiry > 0 && time.Now().UTC().Sub(v.lastAccessed) > c.expiry {
|
|
|
|
c.delete(k)
|
|
|
|
evictedEntries = append(evictedEntries, k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
c.mutex.Unlock()
|
|
|
|
for _, k := range evictedEntries {
|
|
|
|
if c.OnEviction != nil {
|
|
|
|
c.OnEviction(k)
|
|
|
|
}
|
2016-07-04 01:58:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-09 05:34:27 +02:00
|
|
|
// StopGC sends a message to the expiry routine to stop
|
|
|
|
// expiring cached entries. NOTE: once this is called, cached
|
2016-12-09 05:35:07 +01:00
|
|
|
// entries will not be expired, be careful if you are using this.
|
2016-07-09 05:34:27 +02:00
|
|
|
func (c *Cache) StopGC() {
|
|
|
|
if c.stopGC != nil {
|
|
|
|
c.stopGC <- struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-09 05:35:07 +01:00
|
|
|
// StartGC starts running a routine ticking at expiry interval,
|
|
|
|
// on each interval this routine does a sweep across the cache
|
|
|
|
// entries and garbage collects all the expired entries.
|
|
|
|
func (c *Cache) StartGC() {
|
2016-07-09 05:34:27 +02:00
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
// Wait till cleanup interval and initiate delete expired entries.
|
|
|
|
case <-time.After(c.expiry / 4):
|
|
|
|
c.gc()
|
|
|
|
// Stop the routine, usually called by the user of object cache during cleanup.
|
|
|
|
case <-c.stopGC:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Deletes a requested entry from the cache.
|
|
|
|
func (c *Cache) delete(key string) {
|
2016-12-09 05:35:07 +01:00
|
|
|
if _, ok := c.entries[key]; ok {
|
|
|
|
deletedSize := uint64(len(c.entries[key].value))
|
2016-07-09 05:34:27 +02:00
|
|
|
delete(c.entries, key)
|
2016-12-09 05:35:07 +01:00
|
|
|
c.currentSize -= deletedSize
|
2016-07-09 05:34:27 +02:00
|
|
|
c.totalEvicted++
|
2016-07-04 01:58:21 +02:00
|
|
|
}
|
|
|
|
}
|