|
@ -3,6 +3,7 @@ package chunk_cache |
|
|
import ( |
|
|
import ( |
|
|
"bytes" |
|
|
"bytes" |
|
|
"fmt" |
|
|
"fmt" |
|
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/util/mem" |
|
|
"math/rand" |
|
|
"math/rand" |
|
|
"testing" |
|
|
"testing" |
|
|
) |
|
|
) |
|
@ -18,7 +19,7 @@ func TestOnDisk(t *testing.T) { |
|
|
type test_data struct { |
|
|
type test_data struct { |
|
|
data []byte |
|
|
data []byte |
|
|
fileId string |
|
|
fileId string |
|
|
size uint64 |
|
|
|
|
|
|
|
|
size int |
|
|
} |
|
|
} |
|
|
testData := make([]*test_data, writeCount) |
|
|
testData := make([]*test_data, writeCount) |
|
|
for i := 0; i < writeCount; i++ { |
|
|
for i := 0; i < writeCount; i++ { |
|
@ -27,29 +28,35 @@ func TestOnDisk(t *testing.T) { |
|
|
testData[i] = &test_data{ |
|
|
testData[i] = &test_data{ |
|
|
data: buff, |
|
|
data: buff, |
|
|
fileId: fmt.Sprintf("1,%daabbccdd", i+1), |
|
|
fileId: fmt.Sprintf("1,%daabbccdd", i+1), |
|
|
size: uint64(len(buff)), |
|
|
|
|
|
|
|
|
size: len(buff), |
|
|
} |
|
|
} |
|
|
cache.SetChunk(testData[i].fileId, testData[i].data) |
|
|
cache.SetChunk(testData[i].fileId, testData[i].data) |
|
|
|
|
|
|
|
|
// read back right after write
|
|
|
// read back right after write
|
|
|
data := cache.GetChunk(testData[i].fileId, testData[i].size) |
|
|
|
|
|
|
|
|
data := mem.Allocate(testData[i].size) |
|
|
|
|
|
cache.ReadChunkAt(data, testData[i].fileId, 0) |
|
|
if bytes.Compare(data, testData[i].data) != 0 { |
|
|
if bytes.Compare(data, testData[i].data) != 0 { |
|
|
t.Errorf("failed to write to and read from cache: %d", i) |
|
|
t.Errorf("failed to write to and read from cache: %d", i) |
|
|
} |
|
|
} |
|
|
|
|
|
mem.Free(data) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
for i := 0; i < 2; i++ { |
|
|
for i := 0; i < 2; i++ { |
|
|
data := cache.GetChunk(testData[i].fileId, testData[i].size) |
|
|
|
|
|
|
|
|
data := mem.Allocate(testData[i].size) |
|
|
|
|
|
cache.ReadChunkAt(data, testData[i].fileId, 0) |
|
|
if bytes.Compare(data, testData[i].data) == 0 { |
|
|
if bytes.Compare(data, testData[i].data) == 0 { |
|
|
t.Errorf("old cache should have been purged: %d", i) |
|
|
t.Errorf("old cache should have been purged: %d", i) |
|
|
} |
|
|
} |
|
|
|
|
|
mem.Free(data) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
for i := 2; i < writeCount; i++ { |
|
|
for i := 2; i < writeCount; i++ { |
|
|
data := cache.GetChunk(testData[i].fileId, testData[i].size) |
|
|
|
|
|
|
|
|
data := mem.Allocate(testData[i].size) |
|
|
|
|
|
cache.ReadChunkAt(data, testData[i].fileId, 0) |
|
|
if bytes.Compare(data, testData[i].data) != 0 { |
|
|
if bytes.Compare(data, testData[i].data) != 0 { |
|
|
t.Errorf("failed to write to and read from cache: %d", i) |
|
|
t.Errorf("failed to write to and read from cache: %d", i) |
|
|
} |
|
|
} |
|
|
|
|
|
mem.Free(data) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
cache.Shutdown() |
|
|
cache.Shutdown() |
|
@ -57,10 +64,12 @@ func TestOnDisk(t *testing.T) { |
|
|
cache = NewTieredChunkCache(2, tmpDir, totalDiskSizeInKB, 1024) |
|
|
cache = NewTieredChunkCache(2, tmpDir, totalDiskSizeInKB, 1024) |
|
|
|
|
|
|
|
|
for i := 0; i < 2; i++ { |
|
|
for i := 0; i < 2; i++ { |
|
|
data := cache.GetChunk(testData[i].fileId, testData[i].size) |
|
|
|
|
|
|
|
|
data := mem.Allocate(testData[i].size) |
|
|
|
|
|
cache.ReadChunkAt(data, testData[i].fileId, 0) |
|
|
if bytes.Compare(data, testData[i].data) == 0 { |
|
|
if bytes.Compare(data, testData[i].data) == 0 { |
|
|
t.Errorf("old cache should have been purged: %d", i) |
|
|
t.Errorf("old cache should have been purged: %d", i) |
|
|
} |
|
|
} |
|
|
|
|
|
mem.Free(data) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
for i := 2; i < writeCount; i++ { |
|
|
for i := 2; i < writeCount; i++ { |
|
@ -83,10 +92,12 @@ func TestOnDisk(t *testing.T) { |
|
|
*/ |
|
|
*/ |
|
|
continue |
|
|
continue |
|
|
} |
|
|
} |
|
|
data := cache.GetChunk(testData[i].fileId, testData[i].size) |
|
|
|
|
|
|
|
|
data := mem.Allocate(testData[i].size) |
|
|
|
|
|
cache.ReadChunkAt(data, testData[i].fileId, 0) |
|
|
if bytes.Compare(data, testData[i].data) != 0 { |
|
|
if bytes.Compare(data, testData[i].data) != 0 { |
|
|
t.Errorf("failed to write to and read from cache: %d", i) |
|
|
t.Errorf("failed to write to and read from cache: %d", i) |
|
|
} |
|
|
} |
|
|
|
|
|
mem.Free(data) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
cache.Shutdown() |
|
|
cache.Shutdown() |
|
|