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