diff options
| author | Chris Lu <chris.lu@gmail.com> | 2020-04-13 22:19:27 -0700 |
|---|---|---|
| committer | Chris Lu <chris.lu@gmail.com> | 2020-04-13 22:19:27 -0700 |
| commit | 2b5c4fbbf37e25adfa19b081c4adf5458b05b66c (patch) | |
| tree | 6175ca938f6430ca78d04b0b12ce58b8a20f5886 /weed/util/chunk_cache/chunk_cache.go | |
| parent | f282ed444baf6676c22df1b7c35964dd73d2c04a (diff) | |
| download | seaweedfs-2b5c4fbbf37e25adfa19b081c4adf5458b05b66c.tar.xz seaweedfs-2b5c4fbbf37e25adfa19b081c4adf5458b05b66c.zip | |
tiered caching
1/4 for small less than 1MB files. 1/4 for 1~4MB files, 1/2 for bigger than 4MB files
Diffstat (limited to 'weed/util/chunk_cache/chunk_cache.go')
| -rw-r--r-- | weed/util/chunk_cache/chunk_cache.go | 43 |
1 files changed, 29 insertions, 14 deletions
diff --git a/weed/util/chunk_cache/chunk_cache.go b/weed/util/chunk_cache/chunk_cache.go index 232e57a55..e1d4b639f 100644 --- a/weed/util/chunk_cache/chunk_cache.go +++ b/weed/util/chunk_cache/chunk_cache.go @@ -8,27 +8,27 @@ import ( ) const ( - memCacheSizeLimit = 1024 * 1024 + memCacheSizeLimit = 1024 * 1024 + onDiskCacheSizeLimit0 = memCacheSizeLimit + onDiskCacheSizeLimit1 = 4 * memCacheSizeLimit ) // a global cache for recently accessed file chunks type ChunkCache struct { - memCache *ChunkCacheInMemory - diskCache *OnDiskCacheLayer + memCache *ChunkCacheInMemory + diskCaches []*OnDiskCacheLayer sync.RWMutex } -func NewChunkCache(maxEntries int64, dir string, diskSizeMB int64, segmentCount int) *ChunkCache { - - volumeCount, volumeSize := int(diskSizeMB/30000), int64(30000) - if volumeCount < segmentCount { - volumeCount, volumeSize = segmentCount, diskSizeMB/int64(segmentCount) - } +func NewChunkCache(maxEntries int64, dir string, diskSizeMB int64) *ChunkCache { c := &ChunkCache{ - memCache: NewChunkCacheInMemory(maxEntries), - diskCache: NewOnDiskCacheLayer(dir, "cache", volumeCount, volumeSize), + memCache: NewChunkCacheInMemory(maxEntries), } + c.diskCaches = make([]*OnDiskCacheLayer, 3) + c.diskCaches[0] = NewOnDiskCacheLayer(dir, "c0_1", diskSizeMB/4, 4) + c.diskCaches[1] = NewOnDiskCacheLayer(dir, "c1_4", diskSizeMB/4, 4) + c.diskCaches[2] = NewOnDiskCacheLayer(dir, "cache", diskSizeMB/2, 4) return c } @@ -58,7 +58,14 @@ func (c *ChunkCache) doGetChunk(fileId string, chunkSize uint64) (data []byte) { return nil } - return c.diskCache.getChunk(fid.Key) + for _, diskCache := range c.diskCaches { + data := diskCache.getChunk(fid.Key) + if len(data) != 0 { + return data + } + } + + return nil } @@ -84,7 +91,13 @@ func (c *ChunkCache) doSetChunk(fileId string, data []byte) { return } - c.diskCache.setChunk(fid.Key, data) + if len(data) < onDiskCacheSizeLimit0 { + c.diskCaches[0].setChunk(fid.Key, data) + } else if len(data) < onDiskCacheSizeLimit1 { + c.diskCaches[1].setChunk(fid.Key, data) + } else { + c.diskCaches[2].setChunk(fid.Key, data) + } } @@ -94,5 +107,7 @@ func (c *ChunkCache) Shutdown() { } c.Lock() defer c.Unlock() - c.diskCache.shutdown() + for _, diskCache := range c.diskCaches { + diskCache.shutdown() + } } |
