Remove globalMaxCacheSize and globalCacheExpiry variables (#3826)

This patch fixes below

* Remove global variables globalMaxCacheSize and globalCacheExpiry.
* Make global variables into constant in objcache package.
This commit is contained in:
Bala FA
2017-03-03 00:04:37 +05:30
committed by Harshavardhana
parent a179fc9658
commit 208dd15245
8 changed files with 127 additions and 83 deletions

View File

@@ -6,22 +6,24 @@ package objcache
Package objcache implements in memory caching methods.
CONSTANTS
const (
// NoExpiry represents caches to be permanent and can only be deleted.
NoExpiry = time.Duration(0)
// DefaultExpiry represents three days time duration when individual entries will be expired.
DefaultExpiry = time.Duration(3 * 24 * time.Hour)
)
VARIABLES
var DefaultExpiry = time.Duration(72 * time.Hour) // 72hrs.
DefaultExpiry represents default time duration value when individual
entries will be expired.
var ErrCacheFull = errors.New("Not enough space in cache")
ErrCacheFull - cache is full.
var ErrKeyNotFoundInCache = errors.New("Key not found in cache")
ErrKeyNotFoundInCache - key not found in cache.
var NoExpiry = time.Duration(0)
NoExpiry represents caches to be permanent and can only be deleted.
TYPES
type Cache struct {

View File

@@ -1,5 +1,5 @@
/*
* Minio Cloud Storage, (C) 2016 Minio, Inc.
* Minio Cloud Storage, (C) 2016, 2017 Minio, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -27,18 +27,20 @@ import (
"time"
)
// NoExpiry represents caches to be permanent and can only be deleted.
var NoExpiry = time.Duration(0)
const (
// NoExpiry represents caches to be permanent and can only be deleted.
NoExpiry = time.Duration(0)
// DefaultExpiry represents default time duration value when individual entries will be expired.
var DefaultExpiry = time.Duration(72 * time.Hour) // 72hrs.
// DefaultExpiry represents three days time duration when individual entries will be expired.
DefaultExpiry = time.Duration(3 * 24 * time.Hour)
// DefaultBufferRatio represents default ratio used to calculate the
// individual cache entry buffer size.
var DefaultBufferRatio = uint64(10)
// defaultBufferRatio represents default ratio used to calculate the
// individual cache entry buffer size.
defaultBufferRatio = uint64(10)
// DefaultGCPercent represents default garbage collection target percentage.
var DefaultGCPercent = 20
// defaultGCPercent represents default garbage collection target percentage.
defaultGCPercent = 20
)
// buffer represents the in memory cache of a single entry.
// buffer carries value of the data and last accessed time.
@@ -87,9 +89,10 @@ type Cache struct {
// 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.
func New(maxSize uint64, expiry time.Duration) *Cache {
func New(maxSize uint64, expiry time.Duration) (c *Cache, err error) {
if maxSize == 0 {
panic("objcache: setting maximum cache size to zero is forbidden.")
err = errors.New("invalid maximum cache size")
return c, err
}
// A garbage collection is triggered when the ratio
@@ -106,20 +109,20 @@ func New(maxSize uint64, expiry time.Duration) *Cache {
// when we get to 8M.
//
// Set this value to 20% if caching is enabled.
debug.SetGCPercent(DefaultGCPercent)
debug.SetGCPercent(defaultGCPercent)
// 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 {
i := maxSize / DefaultBufferRatio
i := maxSize / defaultBufferRatio
if i == 0 {
i = maxSize
}
return i
}()
c := &Cache{
c = &Cache{
onceGC: sync.Once{},
maxSize: maxSize,
maxCacheEntrySize: maxCacheEntrySize,
@@ -134,7 +137,7 @@ func New(maxSize uint64, expiry time.Duration) *Cache {
// Start garbage collection routine to expire objects.
c.StartGC()
}
return c
return c, nil
}
// ErrKeyNotFoundInCache - key not found in cache.
@@ -177,7 +180,7 @@ func (c *Cache) Create(key string, size int64) (w io.WriteCloser, err error) {
// Change GC percent if the current cache usage
// is already 75% of the maximum allowed usage.
if c.currentSize > (75 * c.maxSize / 100) {
c.onceGC.Do(func() { debug.SetGCPercent(DefaultGCPercent - 10) })
c.onceGC.Do(func() { debug.SetGCPercent(defaultGCPercent - 10) })
}
c.mutex.Unlock()

View File

@@ -43,7 +43,11 @@ func TestObjExpiry(t *testing.T) {
// Test case 1 validates running of GC.
testCase := testCases[0]
cache := New(testCase.cacheSize, testCase.expiry)
cache, err := New(testCase.cacheSize, testCase.expiry)
if err != nil {
t.Fatalf("Unable to create new objcache")
}
cache.OnEviction = func(key string) {}
w, err := cache.Create("test", 1)
if err != nil {
@@ -126,15 +130,23 @@ func TestObjCache(t *testing.T) {
// Test 1 validating Open failure.
testCase := testCases[0]
cache := New(testCase.cacheSize, testCase.expiry)
_, err := cache.Open("test", fakeObjModTime)
cache, err := New(testCase.cacheSize, testCase.expiry)
if err != nil {
t.Fatalf("Unable to create new objcache")
}
_, err = cache.Open("test", fakeObjModTime)
if testCase.err != err {
t.Errorf("Test case 2 expected to pass, failed instead %s", err)
}
// Test 2 validating Create failure.
testCase = testCases[1]
cache = New(testCase.cacheSize, testCase.expiry)
cache, err = New(testCase.cacheSize, testCase.expiry)
if err != nil {
t.Fatalf("Unable to create new objcache")
}
_, err = cache.Create("test", 2)
if testCase.err != err {
t.Errorf("Test case 2 expected to pass, failed instead %s", err)
@@ -144,7 +156,11 @@ func TestObjCache(t *testing.T) {
// Subsequently we Close() without writing any data, to receive
// `io.ErrShortBuffer`
testCase = testCases[2]
cache = New(testCase.cacheSize, testCase.expiry)
cache, err = New(testCase.cacheSize, testCase.expiry)
if err != nil {
t.Fatalf("Unable to create new objcache")
}
w, err := cache.Create("test", 1)
if testCase.err != err {
t.Errorf("Test case 3 expected to pass, failed instead %s", err)
@@ -156,7 +172,11 @@ func TestObjCache(t *testing.T) {
// Test 4 validates Create and Close succeeds successfully caching
// the writes.
testCase = testCases[3]
cache = New(testCase.cacheSize, testCase.expiry)
cache, err = New(testCase.cacheSize, testCase.expiry)
if err != nil {
t.Fatalf("Unable to create new objcache")
}
w, err = cache.Create("test", 5)
if testCase.err != err {
t.Errorf("Test case 4 expected to pass, failed instead %s", err)
@@ -184,7 +204,11 @@ func TestObjCache(t *testing.T) {
// Test 5 validates Delete succeeds and Open fails with err
testCase = testCases[4]
cache = New(testCase.cacheSize, testCase.expiry)
cache, err = New(testCase.cacheSize, testCase.expiry)
if err != nil {
t.Fatalf("Unable to create new objcache")
}
w, err = cache.Create("test", 5)
if err != nil {
t.Errorf("Test case 5 expected to pass, failed instead %s", err)
@@ -204,7 +228,11 @@ func TestObjCache(t *testing.T) {
// Test 6 validates OnEviction being called upon Delete is being invoked.
testCase = testCases[5]
cache = New(testCase.cacheSize, testCase.expiry)
cache, err = New(testCase.cacheSize, testCase.expiry)
if err != nil {
t.Fatalf("Unable to create new objcache")
}
w, err = cache.Create("test", 5)
if err != nil {
t.Errorf("Test case 6 expected to pass, failed instead %s", err)
@@ -227,7 +255,11 @@ func TestObjCache(t *testing.T) {
// Test 7 validates rejecting requests when excess data is being saved.
testCase = testCases[6]
cache = New(testCase.cacheSize, testCase.expiry)
cache, err = New(testCase.cacheSize, testCase.expiry)
if err != nil {
t.Fatalf("Unable to create new objcache")
}
w, err = cache.Create("test1", 5)
if err != nil {
t.Errorf("Test case 7 expected to pass, failed instead %s", err)
@@ -245,7 +277,11 @@ func TestObjCache(t *testing.T) {
// Test 8 validates rejecting Writes which write excess data.
testCase = testCases[7]
cache = New(testCase.cacheSize, testCase.expiry)
cache, err = New(testCase.cacheSize, testCase.expiry)
if err != nil {
t.Fatalf("Unable to create new objcache")
}
w, err = cache.Create("test1", 5)
if err != nil {
t.Errorf("Test case 8 expected to pass, failed instead %s", err)
@@ -267,7 +303,11 @@ func TestObjCache(t *testing.T) {
// TestStateEntryPurge - tests if objCache purges stale entry and returns ErrKeyNotFoundInCache.
func TestStaleEntryPurge(t *testing.T) {
cache := New(1024, NoExpiry)
cache, err := New(1024, NoExpiry)
if err != nil {
t.Fatalf("Unable to create new objcache")
}
w, err := cache.Create("test", 5)
if err != nil {
t.Errorf("Test case expected to pass, failed instead %s", err)