@ -1997,37 +1997,25 @@ TEST_F(DBSecondaryCacheTest, TestSecondaryCacheMultiGet) {
Destroy ( options ) ;
Destroy ( options ) ;
}
}
class LRU CacheWithStat : public LRU Cache {
class CacheWithStats : public CacheWrapper {
public :
public :
LRUCacheWithStat (
using CacheWrapper : : CacheWrapper ;
size_t _capacity , int _num_shard_bits , bool _strict_capacity_limit ,
double _high_pri_pool_ratio , double _low_pri_pool_ratio ,
static const char * kClassName ( ) { return " CacheWithStats " ; }
std : : shared_ptr < MemoryAllocator > _memory_allocator = nullptr ,
const char * Name ( ) const override { return kClassName ( ) ; }
bool _use_adaptive_mutex = kDefaultToAdaptiveMutex ,
CacheMetadataChargePolicy _metadata_charge_policy =
kDontChargeCacheMetadata ,
const std : : shared_ptr < SecondaryCache > & _secondary_cache = nullptr )
: LRUCache ( _capacity , _num_shard_bits , _strict_capacity_limit ,
_high_pri_pool_ratio , _low_pri_pool_ratio , _memory_allocator ,
_use_adaptive_mutex , _metadata_charge_policy ,
_secondary_cache ) {
insert_count_ = 0 ;
lookup_count_ = 0 ;
}
~ LRUCacheWithStat ( ) { }
Status Insert ( const Slice & key , Cache : : ObjectPtr value ,
Status Insert ( const Slice & key , Cache : : ObjectPtr value ,
const CacheItemHelper * helper , size_t charge ,
const CacheItemHelper * helper , size_t charge ,
Handle * * handle = nullptr ,
Handle * * handle = nullptr ,
Priority priority = Priority : : LOW ) override {
Priority priority = Priority : : LOW ) override {
insert_count_ + + ;
insert_count_ + + ;
return LRUCache : : Insert ( key , value , helper , charge , handle , priority ) ;
return target_ - > Insert ( key , value , helper , charge , handle , priority ) ;
}
}
Handle * Lookup ( const Slice & key , const CacheItemHelper * helper ,
Handle * Lookup ( const Slice & key , const CacheItemHelper * helper ,
CreateContext * create_context , Priority priority , bool wait ,
CreateContext * create_context , Priority priority , bool wait ,
Statistics * stats = nullptr ) override {
Statistics * stats = nullptr ) override {
lookup_count_ + + ;
lookup_count_ + + ;
return LRUCache : : Lookup ( key , helper , create_context , priority , wait , stats ) ;
return target_ - > Lookup ( key , helper , create_context , priority , wait , stats ) ;
}
}
uint32_t GetInsertCount ( ) { return insert_count_ ; }
uint32_t GetInsertCount ( ) { return insert_count_ ; }
@ -2038,24 +2026,18 @@ class LRUCacheWithStat : public LRUCache {
}
}
private :
private :
uint32_t insert_count_ ;
uint32_t insert_count_ = 0 ;
uint32_t lookup_count_ ;
uint32_t lookup_count_ = 0 ;
} ;
} ;
TEST_F ( DBSecondaryCacheTest , LRUCacheDumpLoadBasic ) {
TEST_F ( DBSecondaryCacheTest , LRUCacheDumpLoadBasic ) {
LRUCacheOptions cache_opts ( 1024 * 1024 /* capacity */ , 0 /* num_shard_bits */ ,
LRUCacheOptions cache_opts ( 1024 * 1024 /* capacity */ , 0 /* num_shard_bits */ ,
false /* strict_capacity_limit */ ,
false /* strict_capacity_limit */ ,
0.5 /* high_pri_pool_ratio */ ,
0.5 /* high_pri_pool_ratio */ ,
nullptr /* memory_allocator */ ,
nullptr /* memory_allocator */ ,
kDefaultToAdaptiveMutex , kDontChargeCacheMetadata ) ;
kDefaultToAdaptiveMutex , kDontChargeCacheMetadata ) ;
LRUCacheWithStat * tmp_cache = new LRUCacheWithStat (
std : : shared_ptr < CacheWithStats > cache =
cache_opts . capacity , cache_opts . num_shard_bits ,
std : : make_shared < CacheWithStats > ( NewLRUCache ( cache_opts ) ) ;
cache_opts . strict_capacity_limit , cache_opts . high_pri_pool_ratio ,
cache_opts . low_pri_pool_ratio , cache_opts . memory_allocator ,
cache_opts . use_adaptive_mutex , cache_opts . metadata_charge_policy ,
cache_opts . secondary_cache ) ;
std : : shared_ptr < Cache > cache ( tmp_cache ) ;
BlockBasedTableOptions table_options ;
BlockBasedTableOptions table_options ;
table_options . block_cache = cache ;
table_options . block_cache = cache ;
table_options . block_size = 4 * 1024 ;
table_options . block_size = 4 * 1024 ;
@ -2083,15 +2065,15 @@ TEST_F(DBSecondaryCacheTest, LRUCacheDumpLoadBasic) {
// do th eread for all the key value pairs, so all the blocks should be in
// do th eread for all the key value pairs, so all the blocks should be in
// cache
// cache
uint32_t start_insert = tmp_ cache- > GetInsertCount ( ) ;
uint32_t start_insert = cache - > GetInsertCount ( ) ;
uint32_t start_lookup = tmp_ cache- > GetLookupcount ( ) ;
uint32_t start_lookup = cache - > GetLookupcount ( ) ;
std : : string v ;
std : : string v ;
for ( int i = 0 ; i < N ; i + + ) {
for ( int i = 0 ; i < N ; i + + ) {
v = Get ( Key ( i ) ) ;
v = Get ( Key ( i ) ) ;
ASSERT_EQ ( v , value [ i ] ) ;
ASSERT_EQ ( v , value [ i ] ) ;
}
}
uint32_t dump_insert = tmp_ cache- > GetInsertCount ( ) - start_insert ;
uint32_t dump_insert = cache - > GetInsertCount ( ) - start_insert ;
uint32_t dump_lookup = tmp_ cache- > GetLookupcount ( ) - start_lookup ;
uint32_t dump_lookup = cache - > GetLookupcount ( ) - start_lookup ;
ASSERT_EQ ( 63 ,
ASSERT_EQ ( 63 ,
static_cast < int > ( dump_insert ) ) ; // the insert in the block cache
static_cast < int > ( dump_insert ) ) ; // the insert in the block cache
ASSERT_EQ ( 256 ,
ASSERT_EQ ( 256 ,
@ -2122,14 +2104,8 @@ TEST_F(DBSecondaryCacheTest, LRUCacheDumpLoadBasic) {
std : : shared_ptr < TestSecondaryCache > secondary_cache =
std : : shared_ptr < TestSecondaryCache > secondary_cache =
std : : make_shared < TestSecondaryCache > ( 2048 * 1024 ) ;
std : : make_shared < TestSecondaryCache > ( 2048 * 1024 ) ;
cache_opts . secondary_cache = secondary_cache ;
cache_opts . secondary_cache = secondary_cache ;
tmp_cache = new LRUCacheWithStat (
cache = std : : make_shared < CacheWithStats > ( NewLRUCache ( cache_opts ) ) ;
cache_opts . capacity , cache_opts . num_shard_bits ,
table_options . block_cache = cache ;
cache_opts . strict_capacity_limit , cache_opts . high_pri_pool_ratio ,
cache_opts . low_pri_pool_ratio , cache_opts . memory_allocator ,
cache_opts . use_adaptive_mutex , cache_opts . metadata_charge_policy ,
cache_opts . secondary_cache ) ;
std : : shared_ptr < Cache > cache_new ( tmp_cache ) ;
table_options . block_cache = cache_new ;
table_options . block_size = 4 * 1024 ;
table_options . block_size = 4 * 1024 ;
options . create_if_missing = true ;
options . create_if_missing = true ;
options . table_factory . reset ( NewBlockBasedTableFactory ( table_options ) ) ;
options . table_factory . reset ( NewBlockBasedTableFactory ( table_options ) ) ;
@ -2160,8 +2136,8 @@ TEST_F(DBSecondaryCacheTest, LRUCacheDumpLoadBasic) {
// After load, we do the Get again
// After load, we do the Get again
start_insert = secondary_cache - > num_inserts ( ) ;
start_insert = secondary_cache - > num_inserts ( ) ;
start_lookup = secondary_cache - > num_lookups ( ) ;
start_lookup = secondary_cache - > num_lookups ( ) ;
uint32_t cache_insert = tmp_ cache- > GetInsertCount ( ) ;
uint32_t cache_insert = cache - > GetInsertCount ( ) ;
uint32_t cache_lookup = tmp_ cache- > GetLookupcount ( ) ;
uint32_t cache_lookup = cache - > GetLookupcount ( ) ;
for ( int i = 0 ; i < N ; i + + ) {
for ( int i = 0 ; i < N ; i + + ) {
v = Get ( Key ( i ) ) ;
v = Get ( Key ( i ) ) ;
ASSERT_EQ ( v , value [ i ] ) ;
ASSERT_EQ ( v , value [ i ] ) ;
@ -2172,8 +2148,8 @@ TEST_F(DBSecondaryCacheTest, LRUCacheDumpLoadBasic) {
ASSERT_EQ ( 0 , static_cast < int > ( final_insert ) ) ;
ASSERT_EQ ( 0 , static_cast < int > ( final_insert ) ) ;
// lookup the secondary to get all blocks
// lookup the secondary to get all blocks
ASSERT_EQ ( 64 , static_cast < int > ( final_lookup ) ) ;
ASSERT_EQ ( 64 , static_cast < int > ( final_lookup ) ) ;
uint32_t block_insert = tmp_ cache- > GetInsertCount ( ) - cache_insert ;
uint32_t block_insert = cache - > GetInsertCount ( ) - cache_insert ;
uint32_t block_lookup = tmp_ cache- > GetLookupcount ( ) - cache_lookup ;
uint32_t block_lookup = cache - > GetLookupcount ( ) - cache_lookup ;
// Check the new block cache insert and lookup, should be no insert since all
// Check the new block cache insert and lookup, should be no insert since all
// blocks are from the secondary cache.
// blocks are from the secondary cache.
ASSERT_EQ ( 0 , static_cast < int > ( block_insert ) ) ;
ASSERT_EQ ( 0 , static_cast < int > ( block_insert ) ) ;
@ -2189,13 +2165,8 @@ TEST_F(DBSecondaryCacheTest, LRUCacheDumpLoadWithFilter) {
0.5 /* high_pri_pool_ratio */ ,
0.5 /* high_pri_pool_ratio */ ,
nullptr /* memory_allocator */ ,
nullptr /* memory_allocator */ ,
kDefaultToAdaptiveMutex , kDontChargeCacheMetadata ) ;
kDefaultToAdaptiveMutex , kDontChargeCacheMetadata ) ;
LRUCacheWithStat * tmp_cache = new LRUCacheWithStat (
std : : shared_ptr < CacheWithStats > cache =
cache_opts . capacity , cache_opts . num_shard_bits ,
std : : make_shared < CacheWithStats > ( NewLRUCache ( cache_opts ) ) ;
cache_opts . strict_capacity_limit , cache_opts . high_pri_pool_ratio ,
cache_opts . low_pri_pool_ratio , cache_opts . memory_allocator ,
cache_opts . use_adaptive_mutex , cache_opts . metadata_charge_policy ,
cache_opts . secondary_cache ) ;
std : : shared_ptr < Cache > cache ( tmp_cache ) ;
BlockBasedTableOptions table_options ;
BlockBasedTableOptions table_options ;
table_options . block_cache = cache ;
table_options . block_cache = cache ;
table_options . block_size = 4 * 1024 ;
table_options . block_size = 4 * 1024 ;
@ -2245,8 +2216,8 @@ TEST_F(DBSecondaryCacheTest, LRUCacheDumpLoadWithFilter) {
// do th eread for all the key value pairs, so all the blocks should be in
// do th eread for all the key value pairs, so all the blocks should be in
// cache
// cache
uint32_t start_insert = tmp_ cache- > GetInsertCount ( ) ;
uint32_t start_insert = cache - > GetInsertCount ( ) ;
uint32_t start_lookup = tmp_ cache- > GetLookupcount ( ) ;
uint32_t start_lookup = cache - > GetLookupcount ( ) ;
ReadOptions ro ;
ReadOptions ro ;
std : : string v ;
std : : string v ;
for ( int i = 0 ; i < N ; i + + ) {
for ( int i = 0 ; i < N ; i + + ) {
@ -2257,8 +2228,8 @@ TEST_F(DBSecondaryCacheTest, LRUCacheDumpLoadWithFilter) {
ASSERT_OK ( db2 - > Get ( ro , Key ( i ) , & v ) ) ;
ASSERT_OK ( db2 - > Get ( ro , Key ( i ) , & v ) ) ;
ASSERT_EQ ( v , value2 [ i ] ) ;
ASSERT_EQ ( v , value2 [ i ] ) ;
}
}
uint32_t dump_insert = tmp_ cache- > GetInsertCount ( ) - start_insert ;
uint32_t dump_insert = cache - > GetInsertCount ( ) - start_insert ;
uint32_t dump_lookup = tmp_ cache- > GetLookupcount ( ) - start_lookup ;
uint32_t dump_lookup = cache - > GetLookupcount ( ) - start_lookup ;
ASSERT_EQ ( 128 ,
ASSERT_EQ ( 128 ,
static_cast < int > ( dump_insert ) ) ; // the insert in the block cache
static_cast < int > ( dump_insert ) ) ; // the insert in the block cache
ASSERT_EQ ( 512 ,
ASSERT_EQ ( 512 ,
@ -2289,14 +2260,8 @@ TEST_F(DBSecondaryCacheTest, LRUCacheDumpLoadWithFilter) {
std : : shared_ptr < TestSecondaryCache > secondary_cache =
std : : shared_ptr < TestSecondaryCache > secondary_cache =
std : : make_shared < TestSecondaryCache > ( 2048 * 1024 ) ;
std : : make_shared < TestSecondaryCache > ( 2048 * 1024 ) ;
cache_opts . secondary_cache = secondary_cache ;
cache_opts . secondary_cache = secondary_cache ;
tmp_cache = new LRUCacheWithStat (
cache = std : : make_shared < CacheWithStats > ( NewLRUCache ( cache_opts ) ) ;
cache_opts . capacity , cache_opts . num_shard_bits ,
table_options . block_cache = cache ;
cache_opts . strict_capacity_limit , cache_opts . high_pri_pool_ratio ,
cache_opts . low_pri_pool_ratio , cache_opts . memory_allocator ,
cache_opts . use_adaptive_mutex , cache_opts . metadata_charge_policy ,
cache_opts . secondary_cache ) ;
std : : shared_ptr < Cache > cache_new ( tmp_cache ) ;
table_options . block_cache = cache_new ;
table_options . block_size = 4 * 1024 ;
table_options . block_size = 4 * 1024 ;
options . create_if_missing = true ;
options . create_if_missing = true ;
options . table_factory . reset ( NewBlockBasedTableFactory ( table_options ) ) ;
options . table_factory . reset ( NewBlockBasedTableFactory ( table_options ) ) ;
@ -2332,8 +2297,8 @@ TEST_F(DBSecondaryCacheTest, LRUCacheDumpLoadWithFilter) {
fault_fs_ - > SetFilesystemActive ( false , error_msg ) ;
fault_fs_ - > SetFilesystemActive ( false , error_msg ) ;
start_insert = secondary_cache - > num_inserts ( ) ;
start_insert = secondary_cache - > num_inserts ( ) ;
start_lookup = secondary_cache - > num_lookups ( ) ;
start_lookup = secondary_cache - > num_lookups ( ) ;
uint32_t cache_insert = tmp_ cache- > GetInsertCount ( ) ;
uint32_t cache_insert = cache - > GetInsertCount ( ) ;
uint32_t cache_lookup = tmp_ cache- > GetLookupcount ( ) ;
uint32_t cache_lookup = cache - > GetLookupcount ( ) ;
for ( int i = 0 ; i < N ; i + + ) {
for ( int i = 0 ; i < N ; i + + ) {
ASSERT_OK ( db1 - > Get ( ro , Key ( i ) , & v ) ) ;
ASSERT_OK ( db1 - > Get ( ro , Key ( i ) , & v ) ) ;
ASSERT_EQ ( v , value1 [ i ] ) ;
ASSERT_EQ ( v , value1 [ i ] ) ;
@ -2344,8 +2309,8 @@ TEST_F(DBSecondaryCacheTest, LRUCacheDumpLoadWithFilter) {
ASSERT_EQ ( 0 , static_cast < int > ( final_insert ) ) ;
ASSERT_EQ ( 0 , static_cast < int > ( final_insert ) ) ;
// lookup the secondary to get all blocks
// lookup the secondary to get all blocks
ASSERT_EQ ( 64 , static_cast < int > ( final_lookup ) ) ;
ASSERT_EQ ( 64 , static_cast < int > ( final_lookup ) ) ;
uint32_t block_insert = tmp_ cache- > GetInsertCount ( ) - cache_insert ;
uint32_t block_insert = cache - > GetInsertCount ( ) - cache_insert ;
uint32_t block_lookup = tmp_ cache- > GetLookupcount ( ) - cache_lookup ;
uint32_t block_lookup = cache - > GetLookupcount ( ) - cache_lookup ;
// Check the new block cache insert and lookup, should be no insert since all
// Check the new block cache insert and lookup, should be no insert since all
// blocks are from the secondary cache.
// blocks are from the secondary cache.
ASSERT_EQ ( 0 , static_cast < int > ( block_insert ) ) ;
ASSERT_EQ ( 0 , static_cast < int > ( block_insert ) ) ;