@ -8,6 +8,7 @@
# include <algorithm>
# include <cstdint>
# include <iterator>
# include <tuple>
# include "cache/lru_cache.h"
# include "memory/jemalloc_nodump_allocator.h"
@ -111,11 +112,12 @@ class CompressedSecondaryCacheTest : public testing::Test {
// Insert and Lookup the item k1 for the second time and advise erasing it.
ASSERT_OK ( sec_cache - > Insert ( " k1 " , & item1 ,
& CompressedSecondaryCacheTest : : helper_ ) ) ;
ASSERT_EQ ( get_perf_context ( ) - > compressed_sec_cache_insert_real_count , 1 ) ;
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 ) ;
@ -282,7 +284,8 @@ class CompressedSecondaryCacheTest : public testing::Test {
sec_cache . reset ( ) ;
}
void BasicIntegrationTest ( bool sec_cache_is_compressed ) {
void BasicIntegrationTest ( bool sec_cache_is_compressed ,
bool enable_custom_split_merge ) {
CompressedSecondaryCacheOptions secondary_cache_opts ;
if ( sec_cache_is_compressed ) {
@ -297,6 +300,7 @@ class CompressedSecondaryCacheTest : public testing::Test {
secondary_cache_opts . capacity = 6000 ;
secondary_cache_opts . num_shard_bits = 0 ;
secondary_cache_opts . enable_custom_split_merge = enable_custom_split_merge ;
std : : shared_ptr < SecondaryCache > secondary_cache =
NewCompressedSecondaryCache ( secondary_cache_opts ) ;
LRUCacheOptions lru_cache_opts (
@ -680,24 +684,22 @@ class CompressedSecondaryCacheTest : public testing::Test {
std : : make_unique < CompressedSecondaryCache > ( 1000 , 0 , true , 0.5 , 0.0 ,
allocator ) ;
Random rnd ( 301 ) ;
// 8500 = 8169 + 354, so there should be 2 chunks after split.
// 8500 = 8169 + 233 + 98, so there should be 3 chunks after split.
size_t str_size { 8500 } ;
std : : string str = rnd . RandomString ( static_cast < int > ( str_size ) ) ;
size_t charge { 0 } ;
CacheValueChunk * chunks_head =
sec_cache - > SplitValueIntoChunks ( str , kLZ4Compression , charge ) ;
ASSERT_EQ ( charge , str_size + 2 * ( sizeof ( CacheValueChunk ) - 1 ) ) ;
ASSERT_EQ ( charge , str_size + 3 * ( sizeof ( CacheValueChunk ) - 1 ) ) ;
CacheValueChunk * current_chunk = chunks_head ;
ASSERT_EQ ( current_chunk - > size , 8192 - sizeof ( CacheValueChunk ) + 1 ) ;
current_chunk = current_chunk - > next ;
ASSERT_EQ ( current_chunk - > size , 354 - sizeof ( CacheValueChunk ) + 1 ) ;
ASSERT_EQ ( current_chunk - > size , 256 - sizeof ( CacheValueChunk ) + 1 ) ;
current_chunk = current_chunk - > next ;
ASSERT_EQ ( current_chunk - > size , 98 ) ;
while ( chunks_head ! = nullptr ) {
CacheValueChunk * tmp_chunk = chunks_head ;
chunks_head = chunks_head - > next ;
tmp_chunk - > Free ( ) ;
}
sec_cache - > GetDeletionCallback ( true ) ( " dummy " , chunks_head ) ;
}
void MergeChunksIntoValueTest ( ) {
@ -764,13 +766,13 @@ class CompressedSecondaryCacheTest : public testing::Test {
std : : make_unique < CompressedSecondaryCache > ( 1000 , 0 , true , 0.5 , 0.0 ,
allocator ) ;
Random rnd ( 301 ) ;
// 8500 = 8169 + 354, so there should be 2 chunks after split.
// 8500 = 8169 + 233 + 98, so there should be 3 chunks after split.
size_t str_size { 8500 } ;
std : : string str = rnd . RandomString ( static_cast < int > ( str_size ) ) ;
size_t charge { 0 } ;
CacheValueChunk * chunks_head =
sec_cache - > SplitValueIntoChunks ( str , kLZ4Compression , charge ) ;
ASSERT_EQ ( charge , str_size + 2 * ( sizeof ( CacheValueChunk ) - 1 ) ) ;
ASSERT_EQ ( charge , str_size + 3 * ( sizeof ( CacheValueChunk ) - 1 ) ) ;
CacheAllocationPtr value =
sec_cache - > MergeChunksIntoValue ( chunks_head , charge ) ;
@ -778,11 +780,7 @@ class CompressedSecondaryCacheTest : public testing::Test {
std : : string value_str { value . get ( ) , charge } ;
ASSERT_EQ ( strcmp ( value_str . data ( ) , str . data ( ) ) , 0 ) ;
while ( chunks_head ! = nullptr ) {
CacheValueChunk * tmp_chunk = chunks_head ;
chunks_head = chunks_head - > next ;
tmp_chunk - > Free ( ) ;
}
sec_cache - > GetDeletionCallback ( true ) ( " dummy " , chunks_head ) ;
}
private :
@ -799,113 +797,150 @@ Cache::CacheItemHelper CompressedSecondaryCacheTest::helper_fail_(
CompressedSecondaryCacheTest : : SaveToCallbackFail ,
CompressedSecondaryCacheTest : : DeletionCallback ) ;
TEST_F ( CompressedSecondaryCacheTest , BasicTestWithNoCompression ) {
BasicTest ( false , false ) ;
class CompressedSecCacheTestWithCompressAndAllocatorParam
: public CompressedSecondaryCacheTest ,
public : : testing : : WithParamInterface < std : : tuple < bool , bool > > {
public :
CompressedSecCacheTestWithCompressAndAllocatorParam ( ) {
sec_cache_is_compressed_ = std : : get < 0 > ( GetParam ( ) ) ;
use_jemalloc_ = std : : get < 1 > ( GetParam ( ) ) ;
}
bool sec_cache_is_compressed_ ;
bool use_jemalloc_ ;
} ;
TEST_F ( CompressedSecondaryCacheTest ,
BasicTestWithMemoryAllocatorAndNoCompression ) {
BasicTest ( false , true ) ;
TEST_P ( CompressedSecCacheTestWithCompressAndAllocatorParam , BasicTes ) {
BasicTest ( sec_cache_is_compressed_ , use_jemalloc_ ) ;
}
TEST_F ( CompressedSecondary CacheTest , BasicTestWithCompression ) {
BasicTest ( true , false ) ;
}
INSTANTIATE_TEST_CASE_P ( CompressedSecCacheTests ,
CompressedSecCacheTestWithCompressAndAllocatorParam ,
: : testing : : Combine ( testing : : Bool ( ) , testing : : Bool ( ) ) ) ;
TEST_F ( CompressedSecondaryCacheTest ,
BasicTestWithMemoryAllocatorAndCompression ) {
BasicTest ( true , true ) ;
class CompressedSecondaryCacheTestWithCompressionParam
: public CompressedSecondaryCacheTest ,
public : : testing : : WithParamInterface < bool > {
public :
CompressedSecondaryCacheTestWithCompressionParam ( ) {
sec_cache_is_compressed_ = GetParam ( ) ;
}
bool sec_cache_is_compressed_ ;
} ;
# ifndef ROCKSDB_LITE
TEST_F ( CompressedSecondaryCacheTest , BasicTestFromStringWithNoCompression ) {
std : : string sec_cache_uri =
TEST_P ( CompressedSecondaryCacheTestWithCompressionParam , BasicTestFromString ) {
std : : shared_ptr < SecondaryCache > sec_cache { nullptr } ;
std : : string sec_cache_uri ;
if ( sec_cache_is_compressed_ ) {
if ( LZ4_Supported ( ) ) {
sec_cache_uri =
" compressed_secondary_cache:// "
" capacity=2048;num_shard_bits=0;compression_type=kLZ4Compression; "
" compress_format_version=2 " ;
} else {
ROCKSDB_GTEST_SKIP ( " This test requires LZ4 support. " ) ;
sec_cache_uri =
" compressed_secondary_cache:// "
" capacity=2048;num_shard_bits=0;compression_type=kNoCompression " ;
sec_cache_is_compressed_ = false ;
}
Status s = SecondaryCache : : CreateFromString ( ConfigOptions ( ) , sec_cache_uri ,
& sec_cache ) ;
EXPECT_OK ( s ) ;
} else {
sec_cache_uri =
" compressed_secondary_cache:// "
" capacity=2048;num_shard_bits=0;compression_type=kNoCompression " ;
std : : shared_ptr < SecondaryCache > sec_cache ;
Status s = SecondaryCache : : CreateFromString ( ConfigOptions ( ) , sec_cache_uri ,
& sec_cache ) ;
EXPECT_OK ( s ) ;
BasicTestHelper ( sec_cache , /*sec_cache_is_compressed=*/ false ) ;
}
BasicTestHelper ( sec_cache , sec_cache_is_compressed_ ) ;
}
TEST_F ( CompressedSecondaryCacheTest , BasicTestFromStringWithCompression ) {
TEST_P ( CompressedSecondaryCacheTestWithCompressionParam ,
BasicTestFromStringWithSplit ) {
std : : shared_ptr < SecondaryCache > sec_cache { nullptr } ;
std : : string sec_cache_uri ;
bool sec_cache_is_compressed { true } ;
if ( sec_cache_is_compressed_ ) {
if ( LZ4_Supported ( ) ) {
sec_cache_uri =
" compressed_secondary_cache:// "
" capacity=2048;num_shard_bits=0;compression_type=kLZ4Compression; "
" compress_format_version=2 " ;
" compress_format_version=2;enable_custom_split_merge=true " ;
} else {
ROCKSDB_GTEST_SKIP ( " This test requires LZ4 support. " ) ;
sec_cache_uri =
" compressed_secondary_cache:// "
" capacity=2048;num_shard_bits=0;compression_type=kNoCompression " ;
sec_cache_is_compressed = false ;
" capacity=2048;num_shard_bits=0;compression_type=kNoCompression; "
" enable_custom_split_merge=true " ;
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 , sec_cache_is_compressed ) ;
}
# endif // ROCKSDB_LITE
TEST_F ( CompressedSecondaryCacheTest , FailsTestWithNoCompression ) {
FailsTest ( false ) ;
} else {
sec_cache_uri =
" compressed_secondary_cache:// "
" capacity=2048;num_shard_bits=0;compression_type=kNoCompression; "
" enable_custom_split_merge=true " ;
Status s = SecondaryCache : : CreateFromString ( ConfigOptions ( ) , sec_cache_uri ,
& sec_cache ) ;
EXPECT_OK ( s ) ;
}
TEST_F ( CompressedSecondaryCacheTest , FailsTestWithCompression ) {
FailsTest ( true ) ;
BasicTestHelper ( sec_cache , sec_cache_is_compressed_ ) ;
}
TEST_F ( CompressedSecondaryCacheTest , BasicIntegrationTestWithNoCompression ) {
BasicIntegrationTest ( false ) ;
}
# endif // ROCKSDB_LITE
TEST_F ( CompressedSecondaryCacheTest , BasicIntegrationTestWithCompression ) {
BasicIntegrationTest ( true ) ;
TEST_P ( CompressedSecondaryCacheTestWithCompressionParam , FailsTest ) {
FailsTest ( sec_cache_is_compressed_ ) ;
}
TEST_F ( CompressedSecondaryCacheTest ,
BasicIntegrationFailTestWithNoCompression ) {
BasicIntegrationFailTest ( false ) ;
TEST_P ( CompressedSecondaryCacheTestWithCompressionParam ,
BasicIntegrationFailTest ) {
BasicIntegrationFailTest ( sec_cache_is_compressed_ ) ;
}
TEST_F ( CompressedSecondaryCacheTest , BasicIntegrationFailTestWithCompression ) {
BasicIntegrationFailTest ( true ) ;
TEST_P ( CompressedSecondaryCacheTestWithCompressionParam ,
IntegrationSaveFailTest ) {
IntegrationSaveFailTest ( sec_cache_is_compressed_ ) ;
}
TEST_F ( CompressedSecondaryCacheTest , IntegrationSaveFailTestWithNoCompression ) {
IntegrationSaveFailTest ( false ) ;
TEST_P ( CompressedSecondaryCacheTestWithCompressionParam ,
IntegrationCreateFailTest ) {
IntegrationCreateFailTest ( sec_cache_is_compressed_ ) ;
}
TEST_F ( CompressedSecondaryCacheTest , IntegrationSaveFailTestWithCompression ) {
IntegrationSaveFailTest ( true ) ;
TEST_P ( CompressedSecondaryCacheTestWithCompressionParam ,
IntegrationFullCapacityTest ) {
IntegrationFullCapacityTest ( sec_cache_is_compressed_ ) ;
}
TEST_F ( CompressedSecondaryCacheTest ,
IntegrationCreateFailTestWithNoCompression ) {
IntegrationCreateFailTest ( false ) ;
}
INSTANTIATE_TEST_CASE_P ( CompressedSecCacheTests ,
CompressedSecondaryCacheTestWithCompressionParam ,
testing : : Bool ( ) ) ;
TEST_F ( CompressedSecondaryCacheTest , IntegrationCreateFailTestWithCompression ) {
IntegrationCreateFailTest ( true ) ;
class CompressedSecCacheTestWithCompressAndSplitParam
: public CompressedSecondaryCacheTest ,
public : : testing : : WithParamInterface < std : : tuple < bool , bool > > {
public :
CompressedSecCacheTestWithCompressAndSplitParam ( ) {
sec_cache_is_compressed_ = std : : get < 0 > ( GetParam ( ) ) ;
enable_custom_split_merge_ = std : : get < 1 > ( GetParam ( ) ) ;
}
bool sec_cache_is_compressed_ ;
bool enable_custom_split_merge_ ;
} ;
TEST_F ( CompressedSecondaryCacheTest ,
IntegrationFullCapacityTestWithNoCompression ) {
IntegrationFullCapacityTest ( false ) ;
TEST_P ( CompressedSecCacheTestWithCompressAndSplitParam , BasicIntegrationTest ) {
BasicIntegrationTest ( sec_cache_is_compressed_ , enable_custom_split_merge_ ) ;
}
TEST_F ( CompressedSecondaryCacheTest ,
IntegrationFullCapacityTestWithCompression ) {
IntegrationFullCapacityTest ( true ) ;
}
INSTANTIATE_TEST_CASE_P ( CompressedSecCacheTests ,
CompressedSecCacheTestWithCompressAndSplitParam ,
: : testing : : Combine ( testing : : Bool ( ) , testing : : Bool ( ) ) ) ;
TEST_F ( CompressedSecondaryCacheTest , SplitValueIntoChunksTest ) {
SplitValueIntoChunksTest ( ) ;