@ -84,7 +84,9 @@ class CompressedSecondaryCacheTest : public testing::Test {
void SetFailCreate ( bool fail ) { fail_create_ = fail ; }
void BasicTestHelper ( std : : shared_ptr < SecondaryCache > sec_cache ) {
void BasicTestHelper ( std : : shared_ptr < SecondaryCache > sec_cache ,
bool sec_cache_is_compressed ) {
get_perf_context ( ) - > Reset ( ) ;
bool is_in_sec_cache { true } ;
// Lookup an non-existent key.
std : : unique_ptr < SecondaryCacheResultHandle > handle0 = sec_cache - > Lookup (
@ -98,18 +100,31 @@ class CompressedSecondaryCacheTest : public testing::Test {
// A dummy handle is inserted if the item is inserted for the first time.
ASSERT_OK ( sec_cache - > Insert ( " k1 " , & item1 ,
& CompressedSecondaryCacheTest : : helper_ ) ) ;
ASSERT_EQ ( get_perf_context ( ) - > compressed_sec_cache_insert_dummy_count , 1 ) ;
ASSERT_EQ ( get_perf_context ( ) - > compressed_sec_cache_uncompressed_bytes , 0 ) ;
ASSERT_EQ ( get_perf_context ( ) - > compressed_sec_cache_compressed_bytes , 0 ) ;
std : : unique_ptr < SecondaryCacheResultHandle > handle1_1 = sec_cache - > Lookup (
" k1 " , test_item_creator , true , /*advise_erase=*/ false , is_in_sec_cache ) ;
ASSERT_EQ ( handle1_1 , nullptr ) ;
// Insert and Lookup the item k1 for the second time.
// Insert and Lookup the item k1 for the second time and advise erasing it .
ASSERT_OK ( sec_cache - > Insert ( " k1 " , & item1 ,
& CompressedSecondaryCacheTest : : helper_ ) ) ;
std : : unique_ptr < SecondaryCacheResultHandle > handle1_2 = sec_cache - > Lookup (
" k1 " , test_item_creator , true , /*advise_erase=*/ true , is_in_sec_cache ) ;
ASSERT_NE ( handle1_2 , nullptr ) ;
ASSERT_FALSE ( is_in_sec_cache ) ;
ASSERT_EQ ( get_perf_context ( ) - > compressed_sec_cache_insert_real_count , 1 ) ;
if ( sec_cache_is_compressed ) {
ASSERT_EQ ( get_perf_context ( ) - > compressed_sec_cache_uncompressed_bytes ,
1000 ) ;
ASSERT_EQ ( get_perf_context ( ) - > compressed_sec_cache_compressed_bytes ,
1007 ) ;
} else {
ASSERT_EQ ( get_perf_context ( ) - > compressed_sec_cache_uncompressed_bytes , 0 ) ;
ASSERT_EQ ( get_perf_context ( ) - > compressed_sec_cache_compressed_bytes , 0 ) ;
}
std : : unique_ptr < TestItem > val1 =
std : : unique_ptr < TestItem > ( static_cast < TestItem * > ( handle1_2 - > Value ( ) ) ) ;
@ -126,15 +141,26 @@ class CompressedSecondaryCacheTest : public testing::Test {
TestItem item2 ( str2 . data ( ) , str2 . length ( ) ) ;
ASSERT_OK ( sec_cache - > Insert ( " k2 " , & item2 ,
& CompressedSecondaryCacheTest : : helper_ ) ) ;
ASSERT_EQ ( get_perf_context ( ) - > compressed_sec_cache_insert_dummy_count , 2 ) ;
std : : unique_ptr < SecondaryCacheResultHandle > handle2_1 = sec_cache - > Lookup (
" k2 " , test_item_creator , true , /*advise_erase=*/ false , is_in_sec_cache ) ;
ASSERT_EQ ( handle2_1 , nullptr ) ;
ASSERT_OK ( sec_cache - > Insert ( " k2 " , & item2 ,
& CompressedSecondaryCacheTest : : helper_ ) ) ;
ASSERT_EQ ( get_perf_context ( ) - > compressed_sec_cache_insert_real_count , 2 ) ;
if ( sec_cache_is_compressed ) {
ASSERT_EQ ( get_perf_context ( ) - > compressed_sec_cache_uncompressed_bytes ,
2000 ) ;
ASSERT_EQ ( get_perf_context ( ) - > compressed_sec_cache_compressed_bytes ,
2014 ) ;
} else {
ASSERT_EQ ( get_perf_context ( ) - > compressed_sec_cache_uncompressed_bytes , 0 ) ;
ASSERT_EQ ( get_perf_context ( ) - > compressed_sec_cache_compressed_bytes , 0 ) ;
}
std : : unique_ptr < SecondaryCacheResultHandle > handle2_2 = sec_cache - > Lookup (
" k2 " , test_item_creator , true , /*advise_erase=*/ false , is_in_sec_cache ) ;
ASSERT_EQ ( handle2_1 , nullptr ) ;
ASSERT_N E ( handle2_2 , nullptr ) ;
std : : unique_ptr < TestItem > val2 =
std : : unique_ptr < TestItem > ( static_cast < TestItem * > ( handle2_2 - > Value ( ) ) ) ;
ASSERT_NE ( val2 , nullptr ) ;
@ -156,6 +182,7 @@ class CompressedSecondaryCacheTest : public testing::Test {
if ( ! LZ4_Supported ( ) ) {
ROCKSDB_GTEST_SKIP ( " This test requires LZ4 support. " ) ;
opts . compression_type = CompressionType : : kNoCompression ;
sec_cache_is_compressed = false ;
}
} else {
opts . compression_type = CompressionType : : kNoCompression ;
@ -177,7 +204,7 @@ class CompressedSecondaryCacheTest : public testing::Test {
std : : shared_ptr < SecondaryCache > sec_cache =
NewCompressedSecondaryCache ( opts ) ;
BasicTestHelper ( sec_cache ) ;
BasicTestHelper ( sec_cache , sec_cache_is_compressed ) ;
}
void FailsTest ( bool sec_cache_is_compressed ) {
@ -262,6 +289,7 @@ class CompressedSecondaryCacheTest : public testing::Test {
if ( ! LZ4_Supported ( ) ) {
ROCKSDB_GTEST_SKIP ( " This test requires LZ4 support. " ) ;
secondary_cache_opts . compression_type = CompressionType : : kNoCompression ;
sec_cache_is_compressed = false ;
}
} else {
secondary_cache_opts . compression_type = CompressionType : : kNoCompression ;
@ -280,6 +308,7 @@ class CompressedSecondaryCacheTest : public testing::Test {
std : : shared_ptr < Cache > cache = NewLRUCache ( lru_cache_opts ) ;
std : : shared_ptr < Statistics > stats = CreateDBStatistics ( ) ;
get_perf_context ( ) - > Reset ( ) ;
Random rnd ( 301 ) ;
std : : string str1 = rnd . RandomString ( 1001 ) ;
TestItem * item1_1 = new TestItem ( str1 . data ( ) , str1 . length ( ) ) ;
@ -292,6 +321,9 @@ class CompressedSecondaryCacheTest : public testing::Test {
// k1's dummy item.
ASSERT_OK ( cache - > Insert (
" k2 " , item2_1 , & CompressedSecondaryCacheTest : : helper_ , str2 . length ( ) ) ) ;
ASSERT_EQ ( get_perf_context ( ) - > compressed_sec_cache_insert_dummy_count , 1 ) ;
ASSERT_EQ ( get_perf_context ( ) - > compressed_sec_cache_uncompressed_bytes , 0 ) ;
ASSERT_EQ ( get_perf_context ( ) - > compressed_sec_cache_compressed_bytes , 0 ) ;
std : : string str3 = rnd . RandomString ( 1024 ) ;
TestItem * item3_1 = new TestItem ( str3 . data ( ) , str3 . length ( ) ) ;
@ -299,24 +331,46 @@ class CompressedSecondaryCacheTest : public testing::Test {
// k1's dummy item and k2's dummy item.
ASSERT_OK ( cache - > Insert (
" k3 " , item3_1 , & CompressedSecondaryCacheTest : : helper_ , str3 . length ( ) ) ) ;
ASSERT_EQ ( get_perf_context ( ) - > compressed_sec_cache_insert_dummy_count , 2 ) ;
// After this Insert, primary cache contains k1 and secondary cache contains
// k1's dummy item, k2's dummy item, and k3's dummy item.
TestItem * item1_2 = new TestItem ( str1 . data ( ) , str1 . length ( ) ) ;
ASSERT_OK ( cache - > Insert (
" k1 " , item1_2 , & CompressedSecondaryCacheTest : : helper_ , str1 . length ( ) ) ) ;
ASSERT_EQ ( get_perf_context ( ) - > compressed_sec_cache_insert_dummy_count , 3 ) ;
// After this Insert, primary cache contains k2 and secondary cache contains
// k1's item, k2's dummy item, and k3's dummy item.
TestItem * item2_2 = new TestItem ( str2 . data ( ) , str2 . length ( ) ) ;
ASSERT_OK ( cache - > Insert (
" k2 " , item2_2 , & CompressedSecondaryCacheTest : : helper_ , str2 . length ( ) ) ) ;
ASSERT_EQ ( get_perf_context ( ) - > compressed_sec_cache_insert_real_count , 1 ) ;
if ( sec_cache_is_compressed ) {
ASSERT_EQ ( get_perf_context ( ) - > compressed_sec_cache_uncompressed_bytes ,
str1 . length ( ) ) ;
ASSERT_EQ ( get_perf_context ( ) - > compressed_sec_cache_compressed_bytes ,
1008 ) ;
} else {
ASSERT_EQ ( get_perf_context ( ) - > compressed_sec_cache_uncompressed_bytes , 0 ) ;
ASSERT_EQ ( get_perf_context ( ) - > compressed_sec_cache_compressed_bytes , 0 ) ;
}
// After this Insert, primary cache contains k3 and secondary cache contains
// k1's item and k2's item.
TestItem * item3_2 = new TestItem ( str3 . data ( ) , str3 . length ( ) ) ;
ASSERT_OK ( cache - > Insert (
" k3 " , item3_2 , & CompressedSecondaryCacheTest : : helper_ , str3 . length ( ) ) ) ;
ASSERT_EQ ( get_perf_context ( ) - > compressed_sec_cache_insert_real_count , 2 ) ;
if ( sec_cache_is_compressed ) {
ASSERT_EQ ( get_perf_context ( ) - > compressed_sec_cache_uncompressed_bytes ,
str1 . length ( ) + str2 . length ( ) ) ;
ASSERT_EQ ( get_perf_context ( ) - > compressed_sec_cache_compressed_bytes ,
2027 ) ;
} else {
ASSERT_EQ ( get_perf_context ( ) - > compressed_sec_cache_uncompressed_bytes , 0 ) ;
ASSERT_EQ ( get_perf_context ( ) - > compressed_sec_cache_compressed_bytes , 0 ) ;
}
Cache : : Handle * handle ;
handle = cache - > Lookup ( " k3 " , & CompressedSecondaryCacheTest : : helper_ ,
@ -340,6 +394,7 @@ class CompressedSecondaryCacheTest : public testing::Test {
test_item_creator , Cache : : Priority : : LOW , true ,
stats . get ( ) ) ;
ASSERT_NE ( handle , nullptr ) ;
ASSERT_EQ ( get_perf_context ( ) - > block_cache_standalone_handle_count , 1 ) ;
TestItem * val1_1 = static_cast < TestItem * > ( cache - > Value ( handle ) ) ;
ASSERT_NE ( val1_1 , nullptr ) ;
ASSERT_EQ ( memcmp ( val1_1 - > Buf ( ) , str1 . data ( ) , str1 . size ( ) ) , 0 ) ;
@ -351,6 +406,7 @@ class CompressedSecondaryCacheTest : public testing::Test {
test_item_creator , Cache : : Priority : : LOW , true ,
stats . get ( ) ) ;
ASSERT_NE ( handle , nullptr ) ;
ASSERT_EQ ( get_perf_context ( ) - > block_cache_standalone_handle_count , 1 ) ;
cache - > Release ( handle ) ;
// k2 is still in secondary cache.
@ -358,6 +414,7 @@ class CompressedSecondaryCacheTest : public testing::Test {
test_item_creator , Cache : : Priority : : LOW , true ,
stats . get ( ) ) ;
ASSERT_NE ( handle , nullptr ) ;
ASSERT_EQ ( get_perf_context ( ) - > block_cache_standalone_handle_count , 2 ) ;
cache - > Release ( handle ) ;
cache . reset ( ) ;
@ -770,11 +827,12 @@ TEST_F(CompressedSecondaryCacheTest, BasicTestFromStringWithNoCompression) {
Status s = SecondaryCache : : CreateFromString ( ConfigOptions ( ) , sec_cache_uri ,
& sec_cache ) ;
EXPECT_OK ( s ) ;
BasicTestHelper ( sec_cache ) ;
BasicTestHelper ( sec_cache , /*sec_cache_is_compressed=*/ false ) ;
}
TEST_F ( CompressedSecondaryCacheTest , BasicTestFromStringWithCompression ) {
std : : string sec_cache_uri ;
bool sec_cache_is_compressed { true } ;
if ( LZ4_Supported ( ) ) {
sec_cache_uri =
" compressed_secondary_cache:// "
@ -785,13 +843,14 @@ TEST_F(CompressedSecondaryCacheTest, BasicTestFromStringWithCompression) {
sec_cache_uri =
" compressed_secondary_cache:// "
" capacity=2048;num_shard_bits=0;compression_type=kNoCompression " ;
sec_cache_is_compressed = false ;
}
std : : shared_ptr < SecondaryCache > sec_cache ;
Status s = SecondaryCache : : CreateFromString ( ConfigOptions ( ) , sec_cache_uri ,
& sec_cache ) ;
EXPECT_OK ( s ) ;
BasicTestHelper ( sec_cache ) ;
BasicTestHelper ( sec_cache , sec_cache_is_compressed ) ;
}
# endif // ROCKSDB_LITE