diff --git a/pkg/local_object_storage/writecache/storage.go b/pkg/local_object_storage/writecache/storage.go index 8992ffa1..2a27ec69 100644 --- a/pkg/local_object_storage/writecache/storage.go +++ b/pkg/local_object_storage/writecache/storage.go @@ -20,6 +20,9 @@ import ( // store represents persistent storage with in-memory LRU cache // for flushed items on top of it. type store struct { + maxFlushedMarksCount int + maxRemoveBatchSize int + flushed simplelru.LRUCache db *bbolt.DB @@ -27,11 +30,6 @@ type store struct { fsKeysToRemove []string } -const ( - maxFlushedMarksCount = 256 * 1024 * 8 - maxRemoveBatchSize = maxFlushedMarksCount / 4 -) - const dbName = "small.bolt" func (c *cache) openStore(readOnly bool) error { @@ -70,7 +68,7 @@ func (c *cache) openStore(readOnly bool) error { // Write-cache can be opened multiple times during `SetMode`. // flushed map must not be re-created in this case. if c.flushed == nil { - c.flushed, _ = lru.NewWithEvict(maxFlushedMarksCount, c.removeFlushed) + c.flushed, _ = lru.NewWithEvict(c.maxFlushedMarksCount, c.removeFlushed) } return nil } @@ -87,7 +85,7 @@ func (c *cache) removeFlushed(key, value interface{}) { c.fsKeysToRemove = append(c.fsKeysToRemove, key.(string)) } - if len(c.dbKeysToRemove)+len(c.fsKeysToRemove) >= maxRemoveBatchSize { + if len(c.dbKeysToRemove)+len(c.fsKeysToRemove) >= c.maxRemoveBatchSize { c.dbKeysToRemove = c.deleteFromDB(c.dbKeysToRemove) c.fsKeysToRemove = c.deleteFromDisk(c.fsKeysToRemove) } diff --git a/pkg/local_object_storage/writecache/writecache.go b/pkg/local_object_storage/writecache/writecache.go index c3ed2738..0d66f848 100644 --- a/pkg/local_object_storage/writecache/writecache.go +++ b/pkg/local_object_storage/writecache/writecache.go @@ -97,6 +97,12 @@ func New(opts ...Option) Cache { opts[i](&c.options) } + // Make the LRU cache contain which take approximately 3/4 of the maximum space. + // Assume small and big objects are stored in 50-50 proportion. + c.maxFlushedMarksCount = int(c.maxCacheSize/c.maxObjectSize+c.maxCacheSize/c.smallObjectSize) / 2 * 3 / 4 + // Trigger the removal when the cache is 7/8 full, so that new items can still arrive. + c.maxRemoveBatchSize = c.maxFlushedMarksCount / 8 + return c }