From 9ad2dcca2b49b11e85c09df9ade18fb417e4e755 Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Sun, 27 Sep 2020 11:42:51 -0700 Subject: [PATCH] more tests --- weed/util/chunk_cache/chunk_cache.go | 18 +++++++------- .../chunk_cache/chunk_cache_on_disk_test.go | 24 +++++++++++++++++-- 2 files changed, 31 insertions(+), 11 deletions(-) diff --git a/weed/util/chunk_cache/chunk_cache.go b/weed/util/chunk_cache/chunk_cache.go index b4687d037..8830353cc 100644 --- a/weed/util/chunk_cache/chunk_cache.go +++ b/weed/util/chunk_cache/chunk_cache.go @@ -29,9 +29,9 @@ func NewTieredChunkCache(maxEntries int64, dir string, diskSizeInUnit int64, uni c.diskCaches = make([]*OnDiskCacheLayer, 3) c.onDiskCacheSizeLimit0 = uint64(unitSize) c.onDiskCacheSizeLimit1 = 4 * c.onDiskCacheSizeLimit0 - c.diskCaches[0] = NewOnDiskCacheLayer(dir, "c0_1", diskSizeInUnit*unitSize/4, 4) - c.diskCaches[1] = NewOnDiskCacheLayer(dir, "c1_4", diskSizeInUnit*unitSize/4, 4) - c.diskCaches[2] = NewOnDiskCacheLayer(dir, "cache", diskSizeInUnit*unitSize/2, 4) + c.diskCaches[0] = NewOnDiskCacheLayer(dir, "c0_2", diskSizeInUnit*unitSize/8, 2) + c.diskCaches[1] = NewOnDiskCacheLayer(dir, "c1_3", diskSizeInUnit*unitSize/4+diskSizeInUnit*unitSize/8, 3) + c.diskCaches[2] = NewOnDiskCacheLayer(dir, "c2_2", diskSizeInUnit*unitSize/2, 2) return c } @@ -49,7 +49,7 @@ func (c *TieredChunkCache) GetChunk(fileId string, minSize uint64) (data []byte) func (c *TieredChunkCache) doGetChunk(fileId string, minSize uint64) (data []byte) { - if minSize < c.onDiskCacheSizeLimit0 { + if minSize <= c.onDiskCacheSizeLimit0 { data = c.memCache.GetChunk(fileId) if len(data) >= int(minSize) { return data @@ -62,13 +62,13 @@ func (c *TieredChunkCache) doGetChunk(fileId string, minSize uint64) (data []byt return nil } - if minSize < c.onDiskCacheSizeLimit0 { + if minSize <= c.onDiskCacheSizeLimit0 { data = c.diskCaches[0].getChunk(fid.Key) if len(data) >= int(minSize) { return data } } - if minSize < c.onDiskCacheSizeLimit1 { + if minSize <= c.onDiskCacheSizeLimit1 { data = c.diskCaches[1].getChunk(fid.Key) if len(data) >= int(minSize) { return data @@ -99,7 +99,7 @@ func (c *TieredChunkCache) SetChunk(fileId string, data []byte) { func (c *TieredChunkCache) doSetChunk(fileId string, data []byte) { - if len(data) < int(c.onDiskCacheSizeLimit0) { + if len(data) <= int(c.onDiskCacheSizeLimit0) { c.memCache.SetChunk(fileId, data) } @@ -109,9 +109,9 @@ func (c *TieredChunkCache) doSetChunk(fileId string, data []byte) { return } - if len(data) < int(c.onDiskCacheSizeLimit0) { + if len(data) <= int(c.onDiskCacheSizeLimit0) { c.diskCaches[0].setChunk(fid.Key, data) - } else if len(data) < int(c.onDiskCacheSizeLimit1) { + } else if len(data) <= int(c.onDiskCacheSizeLimit1) { c.diskCaches[1].setChunk(fid.Key, data) } else { c.diskCaches[2].setChunk(fid.Key, data) diff --git a/weed/util/chunk_cache/chunk_cache_on_disk_test.go b/weed/util/chunk_cache/chunk_cache_on_disk_test.go index edcd23e62..4b179e5d3 100644 --- a/weed/util/chunk_cache/chunk_cache_on_disk_test.go +++ b/weed/util/chunk_cache/chunk_cache_on_disk_test.go @@ -34,9 +34,22 @@ func TestOnDisk(t *testing.T) { size: uint64(len(buff)), } cache.SetChunk(testData[i].fileId, testData[i].data) + + // read back right after write + data := cache.GetChunk(testData[i].fileId, testData[i].size) + if bytes.Compare(data, testData[i].data) != 0 { + t.Errorf("failed to write to and read from cache: %d", i) + } } - for i := 0; i < writeCount; i++ { + for i := 0; i < 2; i++ { + data := cache.GetChunk(testData[i].fileId, testData[i].size) + if bytes.Compare(data, testData[i].data) == 0 { + t.Errorf("old cache should have been purged: %d", i) + } + } + + for i := 2; i < writeCount; i++ { data := cache.GetChunk(testData[i].fileId, testData[i].size) if bytes.Compare(data, testData[i].data) != 0 { t.Errorf("failed to write to and read from cache: %d", i) @@ -47,7 +60,14 @@ func TestOnDisk(t *testing.T) { cache = NewTieredChunkCache(0, tmpDir, totalDiskSizeInKB, 1024) - for i := 0; i < writeCount; i++ { + for i := 0; i < 2; i++ { + data := cache.GetChunk(testData[i].fileId, testData[i].size) + if bytes.Compare(data, testData[i].data) == 0 { + t.Errorf("old cache should have been purged: %d", i) + } + } + + for i := 2; i < writeCount; i++ { data := cache.GetChunk(testData[i].fileId, testData[i].size) if bytes.Compare(data, testData[i].data) != 0 { t.Errorf("failed to write to and read from cache: %d", i)