diff --git a/cache/lru_cache.cc b/cache/lru_cache.cc index 686b386d9..bed5e5c9f 100644 --- a/cache/lru_cache.cc +++ b/cache/lru_cache.cc @@ -458,6 +458,7 @@ Cache::Handle* LRUCacheShard::Lookup( memcpy(e->key_data, key.data(), key.size()); e->value = nullptr; e->sec_handle = secondary_handle.release(); + e->total_charge = 0; e->Ref(); if (wait) { diff --git a/table/block_based/block_based_table_reader.cc b/table/block_based/block_based_table_reader.cc index b4640245a..024932a70 100644 --- a/table/block_based/block_based_table_reader.cc +++ b/table/block_based/block_based_table_reader.cc @@ -392,11 +392,16 @@ Cache::Handle* BlockBasedTable::GetEntryFromCache( cache_handle = block_cache->Lookup(key, rep_->ioptions.statistics.get()); } - if (cache_handle != nullptr) { - UpdateCacheHitMetrics(block_type, get_context, - block_cache->GetUsage(cache_handle)); - } else { - UpdateCacheMissMetrics(block_type, get_context); + // Avoid updating metrics here if the handle is not complete yet. This + // happens with MultiGet and secondary cache. So update the metrics only + // if its a miss, or a hit and value is ready + if (!cache_handle || block_cache->Value(cache_handle)) { + if (cache_handle != nullptr) { + UpdateCacheHitMetrics(block_type, get_context, + block_cache->GetUsage(cache_handle)); + } else { + UpdateCacheMissMetrics(block_type, get_context); + } } return cache_handle; diff --git a/table/block_based/block_based_table_reader_sync_and_async.h b/table/block_based/block_based_table_reader_sync_and_async.h index 634285fbb..728efc55a 100644 --- a/table/block_based/block_based_table_reader_sync_and_async.h +++ b/table/block_based/block_based_table_reader_sync_and_async.h @@ -484,14 +484,21 @@ DEFINE_SYNC_AND_ASYNC(void, BlockBasedTable::MultiGet) } results[i].UpdateCachedValue(); void* val = results[i].GetValue(); + Cache::Handle* handle = results[i].GetCacheHandle(); + // GetContext for any key will do, as the stats will be aggregated + // anyway + GetContext* get_context = sst_file_range.begin()->get_context; if (!val) { // The async cache lookup failed - could be due to an error // or a false positive. We need to read the data block from // the SST file results[i].Reset(); total_len += BlockSizeWithTrailer(block_handles[i]); + UpdateCacheMissMetrics(BlockType::kData, get_context); } else { block_handles[i] = BlockHandle::NullBlockHandle(); + UpdateCacheHitMetrics(BlockType::kData, get_context, + block_cache->GetUsage(handle)); } } }