| 1 |
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved. |
| 2 |
// This source code is licensed under both the GPLv2 (found in the |
| 3 |
// COPYING file in the root directory) and Apache 2.0 License |
| 4 |
// (found in the LICENSE.Apache file in the root directory). |
| 5 |
|
| 6 |
#include "cache/lru_cache.h" |
| 7 |
|
| 8 |
#include <string> |
| 9 |
#include <vector> |
| 10 |
|
| 11 |
#include "cache/cache_key.h" |
| 12 |
#include "db/db_test_util.h" |
| 13 |
#include "file/sst_file_manager_impl.h" |
| 14 |
#include "port/port.h" |
| 15 |
#include "port/stack_trace.h" |
| 16 |
#include "rocksdb/cache.h" |
| 17 |
#include "rocksdb/io_status.h" |
| 18 |
#include "rocksdb/sst_file_manager.h" |
| 19 |
#include "rocksdb/utilities/cache_dump_load.h" |
| 20 |
#include "test_util/testharness.h" |
| 21 |
#include "util/coding.h" |
| 22 |
#include "util/random.h" |
| 23 |
#include "utilities/cache_dump_load_impl.h" |
| 24 |
#include "utilities/fault_injection_fs.h" |
| 25 |
|
| 26 |
namespace ROCKSDB_NAMESPACE { |
| 27 |
|
| 28 |
class LRUCacheTest : public testing::Test { |
| 29 |
public: |
| 30 |
LRUCacheTest() {} |
| 31 |
~LRUCacheTest() override { DeleteCache(); } |
| 32 |
|
| 33 |
void DeleteCache() { |
| 34 |
if (cache_ != nullptr) { |
| 35 |
cache_->~LRUCacheShard(); |
| 36 |
port::cacheline_aligned_free(cache_); |
| 37 |
cache_ = nullptr; |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
void NewCache(size_t capacity, double high_pri_pool_ratio = 0.0, |
| 42 |
bool use_adaptive_mutex = kDefaultToAdaptiveMutex) { |
| 43 |
DeleteCache(); |
| 44 |
cache_ = reinterpret_cast<LRUCacheShard*>( |
| 45 |
port::cacheline_aligned_alloc(sizeof(LRUCacheShard))); |
| 46 |
new (cache_) LRUCacheShard( |
| 47 |
capacity, false /*strict_capcity_limit*/, high_pri_pool_ratio, |
| 48 |
use_adaptive_mutex, kDontChargeCacheMetadata, |
| 49 |
24 /*max_upper_hash_bits*/, nullptr /*secondary_cache*/); |
| 50 |
} |
| 51 |
|
| 52 |
void Insert(const std::string& key, |
| 53 |
Cache::Priority priority = Cache::Priority::LOW) { |
| 54 |
EXPECT_OK(cache_->Insert(key, 0 /*hash*/, nullptr /*value*/, 1 /*charge*/, |
| 55 |
nullptr /*deleter*/, nullptr /*handle*/, |
| 56 |
priority)); |
| 57 |
} |
| 58 |
|
| 59 |
void Insert(char key, Cache::Priority priority = Cache::Priority::LOW) { |
| 60 |
Insert(std::string(1, key), priority); |
| 61 |
} |
| 62 |
|
| 63 |
bool Lookup(const std::string& key) { |
| 64 |
auto handle = cache_->Lookup(key, 0 /*hash*/); |
| 65 |
if (handle) { |
| 66 |
cache_->Release(handle); |
| 67 |
return true; |
| 68 |
} |
| 69 |
return false; |
| 70 |
} |
| 71 |
|
| 72 |
bool Lookup(char key) { return Lookup(std::string(1, key)); } |
| 73 |
|
| 74 |
void Erase(const std::string& key) { cache_->Erase(key, 0 /*hash*/); } |
| 75 |
|
| 76 |
void ValidateLRUList(std::vector<std::string> keys, |
| 77 |
size_t num_high_pri_pool_keys = 0) { |
| 78 |
LRUHandle* lru; |
| 79 |
LRUHandle* lru_low_pri; |
| 80 |
cache_->TEST_GetLRUList(&lru, &lru_low_pri); |
| 81 |
LRUHandle* iter = lru; |
| 82 |
bool in_high_pri_pool = false; |
| 83 |
size_t high_pri_pool_keys = 0; |
| 84 |
if (iter == lru_low_pri) { |
| 85 |
in_high_pri_pool = true; |
| 86 |
} |
| 87 |
for (const auto& key : keys) { |
| 88 |
iter = iter->next; |
| 89 |
ASSERT_NE(lru, iter); |
| 90 |
ASSERT_EQ(key, iter->key().ToString()); |
| 91 |
ASSERT_EQ(in_high_pri_pool, iter->InHighPriPool()); |
| 92 |
if (in_high_pri_pool) { |
| 93 |
high_pri_pool_keys++; |
| 94 |
} |
| 95 |
if (iter == lru_low_pri) { |
| 96 |
ASSERT_FALSE(in_high_pri_pool); |
| 97 |
in_high_pri_pool = true; |
| 98 |
} |
| 99 |
} |
| 100 |
ASSERT_EQ(lru, iter->next); |
| 101 |
ASSERT_TRUE(in_high_pri_pool); |
| 102 |
ASSERT_EQ(num_high_pri_pool_keys, high_pri_pool_keys); |
| 103 |
} |
| 104 |
|
| 105 |
private: |
| 106 |
LRUCacheShard* cache_ = nullptr; |
| 107 |
}; |
| 108 |
|
| 109 |
TEST_F(LRUCacheTest, BasicLRU) { |
| 110 |
NewCache(5); |
| 111 |
for (char ch = 'a'; ch <= 'e'; ch++) { |
| 112 |
Insert(ch); |
| 113 |
} |
| 114 |
ValidateLRUList({"a", "b", "c", "d", "e"}); |
| 115 |
for (char ch = 'x'; ch <= 'z'; ch++) { |
| 116 |
Insert(ch); |
| 117 |
} |
| 118 |
ValidateLRUList({"d", "e", "x", "y", "z"}); |
| 119 |
ASSERT_FALSE(Lookup("b")); |
| 120 |
ValidateLRUList({"d", "e", "x", "y", "z"}); |
| 121 |
ASSERT_TRUE(Lookup("e")); |
| 122 |
ValidateLRUList({"d", "x", "y", "z", "e"}); |
| 123 |
ASSERT_TRUE(Lookup("z")); |
| 124 |
ValidateLRUList({"d", "x", "y", "e", "z"}); |
| 125 |
Erase("x"); |
| 126 |
ValidateLRUList({"d", "y", "e", "z"}); |
| 127 |
ASSERT_TRUE(Lookup("d")); |
| 128 |
ValidateLRUList({"y", "e", "z", "d"}); |
| 129 |
Insert("u"); |
| 130 |
ValidateLRUList({"y", "e", "z", "d", "u"}); |
| 131 |
Insert("v"); |
| 132 |
ValidateLRUList({"e", "z", "d", "u", "v"}); |
| 133 |
} |
| 134 |
|
| 135 |
TEST_F(LRUCacheTest, MidpointInsertion) { |
| 136 |
// Allocate 2 cache entries to high-pri pool. |
| 137 |
NewCache(5, 0.45); |
| 138 |
|
| 139 |
Insert("a", Cache::Priority::LOW); |
| 140 |
Insert("b", Cache::Priority::LOW); |
| 141 |
Insert("c", Cache::Priority::LOW); |
| 142 |
Insert("x", Cache::Priority::HIGH); |
| 143 |
Insert("y", Cache::Priority::HIGH); |
| 144 |
ValidateLRUList({"a", "b", "c", "x", "y"}, 2); |
| 145 |
|
| 146 |
// Low-pri entries inserted to the tail of low-pri list (the midpoint). |
| 147 |
// After lookup, it will move to the tail of the full list. |
| 148 |
Insert("d", Cache::Priority::LOW); |
| 149 |
ValidateLRUList({"b", "c", "d", "x", "y"}, 2); |
| 150 |
ASSERT_TRUE(Lookup("d")); |
| 151 |
ValidateLRUList({"b", "c", "x", "y", "d"}, 2); |
| 152 |
|
| 153 |
// High-pri entries will be inserted to the tail of full list. |
| 154 |
Insert("z", Cache::Priority::HIGH); |
| 155 |
ValidateLRUList({"c", "x", "y", "d", "z"}, 2); |
| 156 |
} |
| 157 |
|
| 158 |
TEST_F(LRUCacheTest, EntriesWithPriority) { |
| 159 |
// Allocate 2 cache entries to high-pri pool. |
| 160 |
NewCache(5, 0.45); |
| 161 |
|
| 162 |
Insert("a", Cache::Priority::LOW); |
| 163 |
Insert("b", Cache::Priority::LOW); |
| 164 |
Insert("c", Cache::Priority::LOW); |
| 165 |
ValidateLRUList({"a", "b", "c"}, 0); |
| 166 |
|
| 167 |
// Low-pri entries can take high-pri pool capacity if available |
| 168 |
Insert("u", Cache::Priority::LOW); |
| 169 |
Insert("v", Cache::Priority::LOW); |
| 170 |
ValidateLRUList({"a", "b", "c", "u", "v"}, 0); |
| 171 |
|
| 172 |
Insert("X", Cache::Priority::HIGH); |
| 173 |
Insert("Y", Cache::Priority::HIGH); |
| 174 |
ValidateLRUList({"c", "u", "v", "X", "Y"}, 2); |
| 175 |
|
| 176 |
// High-pri entries can overflow to low-pri pool. |
| 177 |
Insert("Z", Cache::Priority::HIGH); |
| 178 |
ValidateLRUList({"u", "v", "X", "Y", "Z"}, 2); |
| 179 |
|
| 180 |
// Low-pri entries will be inserted to head of low-pri pool. |
| 181 |
Insert("a", Cache::Priority::LOW); |
| 182 |
ValidateLRUList({"v", "X", "a", "Y", "Z"}, 2); |
| 183 |
|
| 184 |
// Low-pri entries will be inserted to head of high-pri pool after lookup. |
| 185 |
ASSERT_TRUE(Lookup("v")); |
| 186 |
ValidateLRUList({"X", "a", "Y", "Z", "v"}, 2); |
| 187 |
|
| 188 |
// High-pri entries will be inserted to the head of the list after lookup. |
| 189 |
ASSERT_TRUE(Lookup("X")); |
| 190 |
ValidateLRUList({"a", "Y", "Z", "v", "X"}, 2); |
| 191 |
ASSERT_TRUE(Lookup("Z")); |
| 192 |
ValidateLRUList({"a", "Y", "v", "X", "Z"}, 2); |
| 193 |
|
| 194 |
Erase("Y"); |
| 195 |
ValidateLRUList({"a", "v", "X", "Z"}, 2); |
| 196 |
Erase("X"); |
| 197 |
ValidateLRUList({"a", "v", "Z"}, 1); |
| 198 |
Insert("d", Cache::Priority::LOW); |
| 199 |
Insert("e", Cache::Priority::LOW); |
| 200 |
ValidateLRUList({"a", "v", "d", "e", "Z"}, 1); |
| 201 |
Insert("f", Cache::Priority::LOW); |
| 202 |
Insert("g", Cache::Priority::LOW); |
| 203 |
ValidateLRUList({"d", "e", "f", "g", "Z"}, 1); |
| 204 |
ASSERT_TRUE(Lookup("d")); |
| 205 |
ValidateLRUList({"e", "f", "g", "Z", "d"}, 2); |
| 206 |
} |
| 207 |
|
| 208 |
class TestSecondaryCache : public SecondaryCache { |
| 209 |
public: |
| 210 |
// Specifies what action to take on a lookup for a particular key |
| 211 |
enum ResultType { |
| 212 |
SUCCESS, |
| 213 |
// Fail lookup immediately |
| 214 |
FAIL, |
| 215 |
// Defer the result. It will returned after Wait/WaitAll is called |
| 216 |
DEFER, |
| 217 |
// Defer the result and eventually return failure |
| 218 |
DEFER_AND_FAIL |
| 219 |
}; |
| 220 |
|
| 221 |
using ResultMap = std::unordered_map<std::string, ResultType>; |
| 222 |
|
| 223 |
explicit TestSecondaryCache(size_t capacity) |
| 224 |
: num_inserts_(0), num_lookups_(0), inject_failure_(false) { |
| 225 |
cache_ = NewLRUCache(capacity, 0, false, 0.5, nullptr, |
| 226 |
kDefaultToAdaptiveMutex, kDontChargeCacheMetadata); |
| 227 |
} |
| 228 |
~TestSecondaryCache() override { cache_.reset(); } |
| 229 |
|
| 230 |
const char* Name() const override { return "TestSecondaryCache"; } |
| 231 |
|
| 232 |
void InjectFailure() { inject_failure_ = true; } |
| 233 |
|
| 234 |
void ResetInjectFailure() { inject_failure_ = false; } |
| 235 |
|
| 236 |
void SetDbSessionId(const std::string& db_session_id) { |
| 237 |
// NOTE: we assume the file is smaller than kMaxFileSizeStandardEncoding |
| 238 |
// for this to work, but that's safe in a test. |
| 239 |
auto base = OffsetableCacheKey("unknown", db_session_id, 1, 1); |
| 240 |
ckey_prefix_ = base.CommonPrefixSlice().ToString(); |
| 241 |
} |
| 242 |
|
| 243 |
Status Insert(const Slice& key, void* value, |
| 244 |
const Cache::CacheItemHelper* helper) override { |
| 245 |
if (inject_failure_) { |
| 246 |
return Status::Corruption("Insertion Data Corrupted"); |
| 247 |
} |
| 248 |
EXPECT_TRUE(IsDbSessionLowerAsKeyPrefix(key)); |
| 249 |
size_t size; |
| 250 |
char* buf; |
| 251 |
Status s; |
| 252 |
|
| 253 |
num_inserts_++; |
| 254 |
size = (*helper->size_cb)(value); |
| 255 |
buf = new char[size + sizeof(uint64_t)]; |
| 256 |
EncodeFixed64(buf, size); |
| 257 |
s = (*helper->saveto_cb)(value, 0, size, buf + sizeof(uint64_t)); |
| 258 |
if (!s.ok()) { |
| 259 |
delete[] buf; |
| 260 |
return s; |
| 261 |
} |
| 262 |
return cache_->Insert(key, buf, size, |
| 263 |
[](const Slice& /*key*/, void* val) -> void { |
| 264 |
delete[] static_cast<char*>(val); |
| 265 |
}); |
| 266 |
} |
| 267 |
|
| 268 |
std::unique_ptr<SecondaryCacheResultHandle> Lookup( |
| 269 |
const Slice& key, const Cache::CreateCallback& create_cb, |
| 270 |
bool /*wait*/) override { |
| 271 |
std::string key_str = key.ToString(); |
| 272 |
TEST_SYNC_POINT_CALLBACK("TestSecondaryCache::Lookup", &key_str); |
| 273 |
|
| 274 |
std::unique_ptr<SecondaryCacheResultHandle> secondary_handle; |
| 275 |
ResultType type = ResultType::SUCCESS; |
| 276 |
auto iter = result_map_.find(key.ToString()); |
| 277 |
if (iter != result_map_.end()) { |
| 278 |
type = iter->second; |
| 279 |
} |
| 280 |
if (type == ResultType::FAIL) { |
| 281 |
return secondary_handle; |
| 282 |
} |
| 283 |
|
| 284 |
Cache::Handle* handle = cache_->Lookup(key); |
| 285 |
num_lookups_++; |
| 286 |
if (handle) { |
| 287 |
void* value = nullptr; |
| 288 |
size_t charge = 0; |
| 289 |
Status s; |
| 290 |
if (type != ResultType::DEFER_AND_FAIL) { |
| 291 |
char* ptr = (char*)cache_->Value(handle); |
| 292 |
size_t size = DecodeFixed64(ptr); |
| 293 |
ptr += sizeof(uint64_t); |
| 294 |
s = create_cb(ptr, size, &value, &charge); |
| 295 |
} |
| 296 |
if (s.ok()) { |
| 297 |
secondary_handle.reset(new TestSecondaryCacheResultHandle( |
| 298 |
cache_.get(), handle, value, charge, type)); |
| 299 |
} else { |
| 300 |
cache_->Release(handle); |
| 301 |
} |
| 302 |
} |
| 303 |
return secondary_handle; |
| 304 |
} |
| 305 |
|
| 306 |
void Erase(const Slice& /*key*/) override {} |
| 307 |
|
| 308 |
void WaitAll(std::vector<SecondaryCacheResultHandle*> handles) override { |
| 309 |
for (SecondaryCacheResultHandle* handle : handles) { |
| 310 |
TestSecondaryCacheResultHandle* sec_handle = |
| 311 |
static_cast<TestSecondaryCacheResultHandle*>(handle); |
| 312 |
sec_handle->SetReady(); |
| 313 |
} |
| 314 |
} |
| 315 |
|
| 316 |
std::string GetPrintableOptions() const override { return ""; } |
| 317 |
|
| 318 |
void SetResultMap(ResultMap&& map) { result_map_ = std::move(map); } |
| 319 |
|
| 320 |
uint32_t num_inserts() { return num_inserts_; } |
| 321 |
|
| 322 |
uint32_t num_lookups() { return num_lookups_; } |
| 323 |
|
| 324 |
bool IsDbSessionLowerAsKeyPrefix(const Slice& key) { |
| 325 |
return key.starts_with(ckey_prefix_); |
| 326 |
} |
| 327 |
|
| 328 |
private: |
| 329 |
class TestSecondaryCacheResultHandle : public SecondaryCacheResultHandle { |
| 330 |
public: |
| 331 |
TestSecondaryCacheResultHandle(Cache* cache, Cache::Handle* handle, |
| 332 |
void* value, size_t size, ResultType type) |
| 333 |
: cache_(cache), |
| 334 |
handle_(handle), |
| 335 |
value_(value), |
| 336 |
size_(size), |
| 337 |
is_ready_(true) { |
| 338 |
if (type != ResultType::SUCCESS) { |
| 339 |
is_ready_ = false; |
| 340 |
} |
| 341 |
} |
| 342 |
|
| 343 |
~TestSecondaryCacheResultHandle() override { cache_->Release(handle_); } |
| 344 |
|
| 345 |
bool IsReady() override { return is_ready_; } |
| 346 |
|
| 347 |
void Wait() override {} |
| 348 |
|
| 349 |
void* Value() override { |
| 350 |
assert(is_ready_); |
| 351 |
return value_; |
| 352 |
} |
| 353 |
|
| 354 |
size_t Size() override { return Value() ? size_ : 0; } |
| 355 |
|
| 356 |
void SetReady() { is_ready_ = true; } |
| 357 |
|
| 358 |
private: |
| 359 |
Cache* cache_; |
| 360 |
Cache::Handle* handle_; |
| 361 |
void* value_; |
| 362 |
size_t size_; |
| 363 |
bool is_ready_; |
| 364 |
}; |
| 365 |
|
| 366 |
std::shared_ptr<Cache> cache_; |
| 367 |
uint32_t num_inserts_; |
| 368 |
uint32_t num_lookups_; |
| 369 |
bool inject_failure_; |
| 370 |
std::string ckey_prefix_; |
| 371 |
ResultMap result_map_; |
| 372 |
}; |
| 373 |
|
| 374 |
class DBSecondaryCacheTest : public DBTestBase { |
| 375 |
public: |
| 376 |
DBSecondaryCacheTest() |
| 377 |
: DBTestBase("db_secondary_cache_test", /*env_do_fsync=*/true) { |
| 378 |
fault_fs_.reset(new FaultInjectionTestFS(env_->GetFileSystem())); |
| 379 |
fault_env_.reset(new CompositeEnvWrapper(env_, fault_fs_)); |
| 380 |
} |
| 381 |
|
| 382 |
std::shared_ptr<FaultInjectionTestFS> fault_fs_; |
| 383 |
std::unique_ptr<Env> fault_env_; |
| 384 |
}; |
| 385 |
|
| 386 |
class LRUSecondaryCacheTest : public LRUCacheTest { |
| 387 |
public: |
| 388 |
LRUSecondaryCacheTest() : fail_create_(false) {} |
| 389 |
~LRUSecondaryCacheTest() {} |
| 390 |
|
| 391 |
protected: |
| 392 |
class TestItem { |
| 393 |
public: |
| 394 |
TestItem(const char* buf, size_t size) : buf_(new char[size]), size_(size) { |
| 395 |
memcpy(buf_.get(), buf, size); |
| 396 |
} |
| 397 |
~TestItem() {} |
| 398 |
|
| 399 |
char* Buf() { return buf_.get(); } |
| 400 |
size_t Size() { return size_; } |
| 401 |
std::string ToString() { return std::string(Buf(), Size()); } |
| 402 |
|
| 403 |
private: |
| 404 |
std::unique_ptr<char[]> buf_; |
| 405 |
size_t size_; |
| 406 |
}; |
| 407 |
|
| 408 |
static size_t SizeCallback(void* obj) { |
| 409 |
return reinterpret_cast<TestItem*>(obj)->Size(); |
| 410 |
} |
| 411 |
|
| 412 |
static Status SaveToCallback(void* from_obj, size_t from_offset, |
| 413 |
size_t length, void* out) { |
| 414 |
TestItem* item = reinterpret_cast<TestItem*>(from_obj); |
| 415 |
char* buf = item->Buf(); |
| 416 |
EXPECT_EQ(length, item->Size()); |
| 417 |
EXPECT_EQ(from_offset, 0); |
| 418 |
memcpy(out, buf, length); |
| 419 |
return Status::OK(); |
| 420 |
} |
| 421 |
|
| 422 |
static void DeletionCallback(const Slice& /*key*/, void* obj) { |
| 423 |
delete reinterpret_cast<TestItem*>(obj); |
| 424 |
} |
| 425 |
|
| 426 |
static Cache::CacheItemHelper helper_; |
| 427 |
|
| 428 |
static Status SaveToCallbackFail(void* /*obj*/, size_t /*offset*/, |
| 429 |
size_t /*size*/, void* /*out*/) { |
| 430 |
return Status::NotSupported(); |
| 431 |
} |
| 432 |
|
| 433 |
static Cache::CacheItemHelper helper_fail_; |
| 434 |
|
| 435 |
Cache::CreateCallback test_item_creator = |
| 436 |
[&](void* buf, size_t size, void** out_obj, size_t* charge) -> Status { |
| 437 |
if (fail_create_) { |
| 438 |
return Status::NotSupported(); |
| 439 |
} |
| 440 |
*out_obj = reinterpret_cast<void*>(new TestItem((char*)buf, size)); |
| 441 |
*charge = size; |
| 442 |
return Status::OK(); |
| 443 |
}; |
| 444 |
|
| 445 |
void SetFailCreate(bool fail) { fail_create_ = fail; } |
| 446 |
|
| 447 |
private: |
| 448 |
bool fail_create_; |
| 449 |
}; |
| 450 |
|
| 451 |
Cache::CacheItemHelper LRUSecondaryCacheTest::helper_( |
| 452 |
LRUSecondaryCacheTest::SizeCallback, LRUSecondaryCacheTest::SaveToCallback, |
| 453 |
LRUSecondaryCacheTest::DeletionCallback); |
| 454 |
|
| 455 |
Cache::CacheItemHelper LRUSecondaryCacheTest::helper_fail_( |
| 456 |
LRUSecondaryCacheTest::SizeCallback, |
| 457 |
LRUSecondaryCacheTest::SaveToCallbackFail, |
| 458 |
LRUSecondaryCacheTest::DeletionCallback); |
| 459 |
|
| 460 |
TEST_F(LRUSecondaryCacheTest, BasicTest) { |
| 461 |
LRUCacheOptions opts(1024, 0, false, 0.5, nullptr, kDefaultToAdaptiveMutex, |
| 462 |
kDontChargeCacheMetadata); |
| 463 |
std::shared_ptr<TestSecondaryCache> secondary_cache = |
| 464 |
std::make_shared<TestSecondaryCache>(2048); |
| 465 |
opts.secondary_cache = secondary_cache; |
| 466 |
std::shared_ptr<Cache> cache = NewLRUCache(opts); |
| 467 |
std::shared_ptr<Statistics> stats = CreateDBStatistics(); |
| 468 |
|
| 469 |
Random rnd(301); |
| 470 |
std::string str1 = rnd.RandomString(1020); |
| 471 |
TestItem* item1 = new TestItem(str1.data(), str1.length()); |
| 472 |
ASSERT_OK(cache->Insert("k1", item1, &LRUSecondaryCacheTest::helper_, |
| 473 |
str1.length())); |
| 474 |
std::string str2 = rnd.RandomString(1020); |
| 475 |
TestItem* item2 = new TestItem(str2.data(), str2.length()); |
| 476 |
// k1 should be demoted to NVM |
| 477 |
ASSERT_OK(cache->Insert("k2", item2, &LRUSecondaryCacheTest::helper_, |
| 478 |
str2.length())); |
| 479 |
|
| 480 |
get_perf_context()->Reset(); |
| 481 |
Cache::Handle* handle; |
| 482 |
handle = |
| 483 |
cache->Lookup("k2", &LRUSecondaryCacheTest::helper_, test_item_creator, |
| 484 |
Cache::Priority::LOW, true, stats.get()); |
| 485 |
ASSERT_NE(handle, nullptr); |
| 486 |
cache->Release(handle); |
| 487 |
// This lookup should promote k1 and demote k2 |
| 488 |
handle = |
| 489 |
cache->Lookup("k1", &LRUSecondaryCacheTest::helper_, test_item_creator, |
| 490 |
Cache::Priority::LOW, true, stats.get()); |
| 491 |
ASSERT_NE(handle, nullptr); |
| 492 |
cache->Release(handle); |
| 493 |
ASSERT_EQ(secondary_cache->num_inserts(), 2u); |
| 494 |
ASSERT_EQ(secondary_cache->num_lookups(), 1u); |
| 495 |
ASSERT_EQ(stats->getTickerCount(SECONDARY_CACHE_HITS), |
| 496 |
secondary_cache->num_lookups()); |
| 497 |
PerfContext perf_ctx = *get_perf_context(); |
| 498 |
ASSERT_EQ(perf_ctx.secondary_cache_hit_count, secondary_cache->num_lookups()); |
| 499 |
|
| 500 |
cache.reset(); |
| 501 |
secondary_cache.reset(); |
| 502 |
} |
| 503 |
|
| 504 |
TEST_F(LRUSecondaryCacheTest, BasicFailTest) { |
| 505 |
LRUCacheOptions opts(1024, 0, false, 0.5, nullptr, kDefaultToAdaptiveMutex, |
| 506 |
kDontChargeCacheMetadata); |
| 507 |
std::shared_ptr<TestSecondaryCache> secondary_cache = |
| 508 |
std::make_shared<TestSecondaryCache>(2048); |
| 509 |
opts.secondary_cache = secondary_cache; |
| 510 |
std::shared_ptr<Cache> cache = NewLRUCache(opts); |
| 511 |
|
| 512 |
Random rnd(301); |
| 513 |
std::string str1 = rnd.RandomString(1020); |
| 514 |
TestItem* item1 = new TestItem(str1.data(), str1.length()); |
| 515 |
ASSERT_NOK(cache->Insert("k1", item1, nullptr, str1.length())); |
| 516 |
ASSERT_OK(cache->Insert("k1", item1, &LRUSecondaryCacheTest::helper_, |
| 517 |
str1.length())); |
| 518 |
|
| 519 |
Cache::Handle* handle; |
| 520 |
handle = cache->Lookup("k2", nullptr, test_item_creator, Cache::Priority::LOW, |
| 521 |
true); |
| 522 |
ASSERT_EQ(handle, nullptr); |
| 523 |
handle = cache->Lookup("k2", &LRUSecondaryCacheTest::helper_, |
| 524 |
test_item_creator, Cache::Priority::LOW, false); |
| 525 |
ASSERT_EQ(handle, nullptr); |
| 526 |
|
| 527 |
cache.reset(); |
| 528 |
secondary_cache.reset(); |
| 529 |
} |
| 530 |
|
| 531 |
TEST_F(LRUSecondaryCacheTest, SaveFailTest) { |
| 532 |
LRUCacheOptions opts(1024, 0, false, 0.5, nullptr, kDefaultToAdaptiveMutex, |
| 533 |
kDontChargeCacheMetadata); |
| 534 |
std::shared_ptr<TestSecondaryCache> secondary_cache = |
| 535 |
std::make_shared<TestSecondaryCache>(2048); |
| 536 |
opts.secondary_cache = secondary_cache; |
| 537 |
std::shared_ptr<Cache> cache = NewLRUCache(opts); |
| 538 |
|
| 539 |
Random rnd(301); |
| 540 |
std::string str1 = rnd.RandomString(1020); |
| 541 |
TestItem* item1 = new TestItem(str1.data(), str1.length()); |
| 542 |
ASSERT_OK(cache->Insert("k1", item1, &LRUSecondaryCacheTest::helper_fail_, |
| 543 |
str1.length())); |
| 544 |
std::string str2 = rnd.RandomString(1020); |
| 545 |
TestItem* item2 = new TestItem(str2.data(), str2.length()); |
| 546 |
// k1 should be demoted to NVM |
| 547 |
ASSERT_OK(cache->Insert("k2", item2, &LRUSecondaryCacheTest::helper_fail_, |
| 548 |
str2.length())); |
| 549 |
|
| 550 |
Cache::Handle* handle; |
| 551 |
handle = cache->Lookup("k2", &LRUSecondaryCacheTest::helper_fail_, |
| 552 |
test_item_creator, Cache::Priority::LOW, true); |
| 553 |
ASSERT_NE(handle, nullptr); |
| 554 |
cache->Release(handle); |
| 555 |
// This lookup should fail, since k1 demotion would have failed |
| 556 |
handle = cache->Lookup("k1", &LRUSecondaryCacheTest::helper_fail_, |
| 557 |
test_item_creator, Cache::Priority::LOW, true); |
| 558 |
ASSERT_EQ(handle, nullptr); |
| 559 |
// Since k1 didn't get promoted, k2 should still be in cache |
| 560 |
handle = cache->Lookup("k2", &LRUSecondaryCacheTest::helper_fail_, |
| 561 |
test_item_creator, Cache::Priority::LOW, true); |
| 562 |
ASSERT_NE(handle, nullptr); |
| 563 |
cache->Release(handle); |
| 564 |
ASSERT_EQ(secondary_cache->num_inserts(), 1u); |
| 565 |
ASSERT_EQ(secondary_cache->num_lookups(), 1u); |
| 566 |
|
| 567 |
cache.reset(); |
| 568 |
secondary_cache.reset(); |
| 569 |
} |
| 570 |
|
| 571 |
TEST_F(LRUSecondaryCacheTest, CreateFailTest) { |
| 572 |
LRUCacheOptions opts(1024, 0, false, 0.5, nullptr, kDefaultToAdaptiveMutex, |
| 573 |
kDontChargeCacheMetadata); |
| 574 |
std::shared_ptr<TestSecondaryCache> secondary_cache = |
| 575 |
std::make_shared<TestSecondaryCache>(2048); |
| 576 |
opts.secondary_cache = secondary_cache; |
| 577 |
std::shared_ptr<Cache> cache = NewLRUCache(opts); |
| 578 |
|
| 579 |
Random rnd(301); |
| 580 |
std::string str1 = rnd.RandomString(1020); |
| 581 |
TestItem* item1 = new TestItem(str1.data(), str1.length()); |
| 582 |
ASSERT_OK(cache->Insert("k1", item1, &LRUSecondaryCacheTest::helper_, |
| 583 |
str1.length())); |
| 584 |
std::string str2 = rnd.RandomString(1020); |
| 585 |
TestItem* item2 = new TestItem(str2.data(), str2.length()); |
| 586 |
// k1 should be demoted to NVM |
| 587 |
ASSERT_OK(cache->Insert("k2", item2, &LRUSecondaryCacheTest::helper_, |
| 588 |
str2.length())); |
| 589 |
|
| 590 |
Cache::Handle* handle; |
| 591 |
SetFailCreate(true); |
| 592 |
handle = cache->Lookup("k2", &LRUSecondaryCacheTest::helper_, |
| 593 |
test_item_creator, Cache::Priority::LOW, true); |
| 594 |
ASSERT_NE(handle, nullptr); |
| 595 |
cache->Release(handle); |
| 596 |
// This lookup should fail, since k1 creation would have failed |
| 597 |
handle = cache->Lookup("k1", &LRUSecondaryCacheTest::helper_, |
| 598 |
test_item_creator, Cache::Priority::LOW, true); |
| 599 |
ASSERT_EQ(handle, nullptr); |
| 600 |
// Since k1 didn't get promoted, k2 should still be in cache |
| 601 |
handle = cache->Lookup("k2", &LRUSecondaryCacheTest::helper_, |
| 602 |
test_item_creator, Cache::Priority::LOW, true); |
| 603 |
ASSERT_NE(handle, nullptr); |
| 604 |
cache->Release(handle); |
| 605 |
ASSERT_EQ(secondary_cache->num_inserts(), 1u); |
| 606 |
ASSERT_EQ(secondary_cache->num_lookups(), 1u); |
| 607 |
|
| 608 |
cache.reset(); |
| 609 |
secondary_cache.reset(); |
| 610 |
} |
| 611 |
|
| 612 |
TEST_F(LRUSecondaryCacheTest, FullCapacityTest) { |
| 613 |
LRUCacheOptions opts(1024, 0, /*_strict_capacity_limit=*/true, 0.5, nullptr, |
| 614 |
kDefaultToAdaptiveMutex, kDontChargeCacheMetadata); |
| 615 |
std::shared_ptr<TestSecondaryCache> secondary_cache = |
| 616 |
std::make_shared<TestSecondaryCache>(2048); |
| 617 |
opts.secondary_cache = secondary_cache; |
| 618 |
std::shared_ptr<Cache> cache = NewLRUCache(opts); |
| 619 |
|
| 620 |
Random rnd(301); |
| 621 |
std::string str1 = rnd.RandomString(1020); |
| 622 |
TestItem* item1 = new TestItem(str1.data(), str1.length()); |
| 623 |
ASSERT_OK(cache->Insert("k1", item1, &LRUSecondaryCacheTest::helper_, |
| 624 |
str1.length())); |
| 625 |
std::string str2 = rnd.RandomString(1020); |
| 626 |
TestItem* item2 = new TestItem(str2.data(), str2.length()); |
| 627 |
// k1 should be demoted to NVM |
| 628 |
ASSERT_OK(cache->Insert("k2", item2, &LRUSecondaryCacheTest::helper_, |
| 629 |
str2.length())); |
| 630 |
|
| 631 |
Cache::Handle* handle; |
| 632 |
handle = cache->Lookup("k2", &LRUSecondaryCacheTest::helper_, |
| 633 |
test_item_creator, Cache::Priority::LOW, true); |
| 634 |
ASSERT_NE(handle, nullptr); |
| 635 |
// k1 promotion should fail due to the block cache being at capacity, |
| 636 |
// but the lookup should still succeed |
| 637 |
Cache::Handle* handle2; |
| 638 |
handle2 = cache->Lookup("k1", &LRUSecondaryCacheTest::helper_, |
| 639 |
test_item_creator, Cache::Priority::LOW, true); |
| 640 |
ASSERT_NE(handle2, nullptr); |
| 641 |
// Since k1 didn't get inserted, k2 should still be in cache |
| 642 |
cache->Release(handle); |
| 643 |
cache->Release(handle2); |
| 644 |
handle = cache->Lookup("k2", &LRUSecondaryCacheTest::helper_, |
| 645 |
test_item_creator, Cache::Priority::LOW, true); |
| 646 |
ASSERT_NE(handle, nullptr); |
| 647 |
cache->Release(handle); |
| 648 |
ASSERT_EQ(secondary_cache->num_inserts(), 1u); |
| 649 |
ASSERT_EQ(secondary_cache->num_lookups(), 1u); |
| 650 |
|
| 651 |
cache.reset(); |
| 652 |
secondary_cache.reset(); |
| 653 |
} |
| 654 |
|
| 655 |
// In this test, the block cache size is set to 4096, after insert 6 KV-pairs |
| 656 |
// and flush, there are 5 blocks in this SST file, 2 data blocks and 3 meta |
| 657 |
// blocks. block_1 size is 4096 and block_2 size is 2056. The total size |
| 658 |
// of the meta blocks are about 900 to 1000. Therefore, in any situation, |
| 659 |
// if we try to insert block_1 to the block cache, it will always fails. Only |
| 660 |
// block_2 will be successfully inserted into the block cache. |
| 661 |
TEST_F(DBSecondaryCacheTest, TestSecondaryCacheCorrectness1) { |
| 662 |
LRUCacheOptions opts(4 * 1024, 0, false, 0.5, nullptr, |
| 663 |
kDefaultToAdaptiveMutex, kDontChargeCacheMetadata); |
| 664 |
std::shared_ptr<TestSecondaryCache> secondary_cache( |
| 665 |
new TestSecondaryCache(2048 * 1024)); |
| 666 |
opts.secondary_cache = secondary_cache; |
| 667 |
std::shared_ptr<Cache> cache = NewLRUCache(opts); |
| 668 |
BlockBasedTableOptions table_options; |
| 669 |
table_options.block_cache = cache; |
| 670 |
table_options.block_size = 4 * 1024; |
| 671 |
Options options = GetDefaultOptions(); |
| 672 |
options.create_if_missing = true; |
| 673 |
options.table_factory.reset(NewBlockBasedTableFactory(table_options)); |
| 674 |
options.env = fault_env_.get(); |
| 675 |
fault_fs_->SetFailGetUniqueId(true); |
| 676 |
|
| 677 |
// Set the file paranoid check, so after flush, the file will be read |
| 678 |
// all the blocks will be accessed. |
| 679 |
options.paranoid_file_checks = true; |
| 680 |
DestroyAndReopen(options); |
| 681 |
std::string session_id; |
| 682 |
ASSERT_OK(db_->GetDbSessionId(session_id)); |
| 683 |
secondary_cache->SetDbSessionId(session_id); |
| 684 |
Random rnd(301); |
| 685 |
const int N = 6; |
| 686 |
for (int i = 0; i < N; i++) { |
| 687 |
std::string p_v = rnd.RandomString(1007); |
| 688 |
ASSERT_OK(Put(Key(i), p_v)); |
| 689 |
} |
| 690 |
|
| 691 |
ASSERT_OK(Flush()); |
| 692 |
// After Flush is successful, RocksDB will do the paranoid check for the new |
| 693 |
// SST file. Meta blocks are always cached in the block cache and they |
| 694 |
// will not be evicted. When block_2 is cache miss and read out, it is |
| 695 |
// inserted to the block cache. Note that, block_1 is never successfully |
| 696 |
// inserted to the block cache. Here are 2 lookups in the secondary cache |
| 697 |
// for block_1 and block_2 |
| 698 |
ASSERT_EQ(secondary_cache->num_inserts(), 0u); |
| 699 |
ASSERT_EQ(secondary_cache->num_lookups(), 2u); |
| 700 |
|
| 701 |
Compact("a", "z"); |
| 702 |
// Compaction will create the iterator to scan the whole file. So all the |
| 703 |
// blocks are needed. Meta blocks are always cached. When block_1 is read |
| 704 |
// out, block_2 is evicted from block cache and inserted to secondary |
| 705 |
// cache. |
| 706 |
ASSERT_EQ(secondary_cache->num_inserts(), 1u); |
| 707 |
ASSERT_EQ(secondary_cache->num_lookups(), 3u); |
| 708 |
|
| 709 |
std::string v = Get(Key(0)); |
| 710 |
ASSERT_EQ(1007, v.size()); |
| 711 |
// The first data block is not in the cache, similarly, trigger the block |
| 712 |
// cache Lookup and secondary cache lookup for block_1. But block_1 will not |
| 713 |
// be inserted successfully due to the size. Currently, cache only has |
| 714 |
// the meta blocks. |
| 715 |
ASSERT_EQ(secondary_cache->num_inserts(), 1u); |
| 716 |
ASSERT_EQ(secondary_cache->num_lookups(), 4u); |
| 717 |
|
| 718 |
v = Get(Key(5)); |
| 719 |
ASSERT_EQ(1007, v.size()); |
| 720 |
// The second data block is not in the cache, similarly, trigger the block |
| 721 |
// cache Lookup and secondary cache lookup for block_2 and block_2 is found |
| 722 |
// in the secondary cache. Now block cache has block_2 |
| 723 |
ASSERT_EQ(secondary_cache->num_inserts(), 1u); |
| 724 |
ASSERT_EQ(secondary_cache->num_lookups(), 5u); |
| 725 |
|
| 726 |
v = Get(Key(5)); |
| 727 |
ASSERT_EQ(1007, v.size()); |
| 728 |
// block_2 is in the block cache. There is a block cache hit. No need to |
| 729 |
// lookup or insert the secondary cache. |
| 730 |
ASSERT_EQ(secondary_cache->num_inserts(), 1u); |
| 731 |
ASSERT_EQ(secondary_cache->num_lookups(), 5u); |
| 732 |
|
| 733 |
v = Get(Key(0)); |
| 734 |
ASSERT_EQ(1007, v.size()); |
| 735 |
// Lookup the first data block, not in the block cache, so lookup the |
| 736 |
// secondary cache. Also not in the secondary cache. After Get, still |
| 737 |
// block_1 is will not be cached. |
| 738 |
ASSERT_EQ(secondary_cache->num_inserts(), 1u); |
| 739 |
ASSERT_EQ(secondary_cache->num_lookups(), 6u); |
| 740 |
|
| 741 |
v = Get(Key(0)); |
| 742 |
ASSERT_EQ(1007, v.size()); |
| 743 |
// Lookup the first data block, not in the block cache, so lookup the |
| 744 |
// secondary cache. Also not in the secondary cache. After Get, still |
| 745 |
// block_1 is will not be cached. |
| 746 |
ASSERT_EQ(secondary_cache->num_inserts(), 1u); |
| 747 |
ASSERT_EQ(secondary_cache->num_lookups(), 7u); |
| 748 |
|
| 749 |
Destroy(options); |
| 750 |
} |
| 751 |
|
| 752 |
// In this test, the block cache size is set to 6100, after insert 6 KV-pairs |
| 753 |
// and flush, there are 5 blocks in this SST file, 2 data blocks and 3 meta |
| 754 |
// blocks. block_1 size is 4096 and block_2 size is 2056. The total size |
| 755 |
// of the meta blocks are about 900 to 1000. Therefore, we can successfully |
| 756 |
// insert and cache block_1 in the block cache (this is the different place |
| 757 |
// from TestSecondaryCacheCorrectness1) |
| 758 |
TEST_F(DBSecondaryCacheTest, TestSecondaryCacheCorrectness2) { |
| 759 |
LRUCacheOptions opts(6100, 0, false, 0.5, nullptr, kDefaultToAdaptiveMutex, |
| 760 |
kDontChargeCacheMetadata); |
| 761 |
std::shared_ptr<TestSecondaryCache> secondary_cache( |
| 762 |
new TestSecondaryCache(2048 * 1024)); |
| 763 |
opts.secondary_cache = secondary_cache; |
| 764 |
std::shared_ptr<Cache> cache = NewLRUCache(opts); |
| 765 |
BlockBasedTableOptions table_options; |
| 766 |
table_options.block_cache = cache; |
| 767 |
table_options.block_size = 4 * 1024; |
| 768 |
Options options = GetDefaultOptions(); |
| 769 |
options.create_if_missing = true; |
| 770 |
options.table_factory.reset(NewBlockBasedTableFactory(table_options)); |
| 771 |
options.paranoid_file_checks = true; |
| 772 |
options.env = fault_env_.get(); |
| 773 |
fault_fs_->SetFailGetUniqueId(true); |
| 774 |
DestroyAndReopen(options); |
| 775 |
std::string session_id; |
| 776 |
ASSERT_OK(db_->GetDbSessionId(session_id)); |
| 777 |
secondary_cache->SetDbSessionId(session_id); |
| 778 |
Random rnd(301); |
| 779 |
const int N = 6; |
| 780 |
for (int i = 0; i < N; i++) { |
| 781 |
std::string p_v = rnd.RandomString(1007); |
| 782 |
ASSERT_OK(Put(Key(i), p_v)); |
| 783 |
} |
| 784 |
|
| 785 |
ASSERT_OK(Flush()); |
| 786 |
// After Flush is successful, RocksDB will do the paranoid check for the new |
| 787 |
// SST file. Meta blocks are always cached in the block cache and they |
| 788 |
// will not be evicted. When block_2 is cache miss and read out, it is |
| 789 |
// inserted to the block cache. Thefore, block_1 is evicted from block |
| 790 |
// cache and successfully inserted to the secondary cache. Here are 2 |
| 791 |
// lookups in the secondary cache for block_1 and block_2. |
| 792 |
ASSERT_EQ(secondary_cache->num_inserts(), 1u); |
| 793 |
ASSERT_EQ(secondary_cache->num_lookups(), 2u); |
| 794 |
|
| 795 |
Compact("a", "z"); |
| 796 |
// Compaction will create the iterator to scan the whole file. So all the |
| 797 |
// blocks are needed. After Flush, only block_2 is cached in block cache |
| 798 |
// and block_1 is in the secondary cache. So when read block_1, it is |
| 799 |
// read out from secondary cache and inserted to block cache. At the same |
| 800 |
// time, block_2 is inserted to secondary cache. Now, secondary cache has |
| 801 |
// both block_1 and block_2. After compaction, block_1 is in the cache. |
| 802 |
ASSERT_EQ(secondary_cache->num_inserts(), 2u); |
| 803 |
ASSERT_EQ(secondary_cache->num_lookups(), 3u); |
| 804 |
|
| 805 |
std::string v = Get(Key(0)); |
| 806 |
ASSERT_EQ(1007, v.size()); |
| 807 |
// This Get needs to access block_1, since block_1 is cached in block cache |
| 808 |
// there is no secondary cache lookup. |
| 809 |
ASSERT_EQ(secondary_cache->num_inserts(), 2u); |
| 810 |
ASSERT_EQ(secondary_cache->num_lookups(), 3u); |
| 811 |
|
| 812 |
v = Get(Key(5)); |
| 813 |
ASSERT_EQ(1007, v.size()); |
| 814 |
// This Get needs to access block_2 which is not in the block cache. So |
| 815 |
// it will lookup the secondary cache for block_2 and cache it in the |
| 816 |
// block_cache. |
| 817 |
ASSERT_EQ(secondary_cache->num_inserts(), 2u); |
| 818 |
ASSERT_EQ(secondary_cache->num_lookups(), 4u); |
| 819 |
|
| 820 |
v = Get(Key(5)); |
| 821 |
ASSERT_EQ(1007, v.size()); |
| 822 |
// This Get needs to access block_2 which is already in the block cache. |
| 823 |
// No need to lookup secondary cache. |
| 824 |
ASSERT_EQ(secondary_cache->num_inserts(), 2u); |
| 825 |
ASSERT_EQ(secondary_cache->num_lookups(), 4u); |
| 826 |
|
| 827 |
v = Get(Key(0)); |
| 828 |
ASSERT_EQ(1007, v.size()); |
| 829 |
// This Get needs to access block_1, since block_1 is not in block cache |
| 830 |
// there is one econdary cache lookup. Then, block_1 is cached in the |
| 831 |
// block cache. |
| 832 |
ASSERT_EQ(secondary_cache->num_inserts(), 2u); |
| 833 |
ASSERT_EQ(secondary_cache->num_lookups(), 5u); |
| 834 |
|
| 835 |
v = Get(Key(0)); |
| 836 |
ASSERT_EQ(1007, v.size()); |
| 837 |
// This Get needs to access block_1, since block_1 is cached in block cache |
| 838 |
// there is no secondary cache lookup. |
| 839 |
ASSERT_EQ(secondary_cache->num_inserts(), 2u); |
| 840 |
ASSERT_EQ(secondary_cache->num_lookups(), 5u); |
| 841 |
|
| 842 |
Destroy(options); |
| 843 |
} |
| 844 |
|
| 845 |
// The block cache size is set to 1024*1024, after insert 6 KV-pairs |
| 846 |
// and flush, there are 5 blocks in this SST file, 2 data blocks and 3 meta |
| 847 |
// blocks. block_1 size is 4096 and block_2 size is 2056. The total size |
| 848 |
// of the meta blocks are about 900 to 1000. Therefore, we can successfully |
| 849 |
// cache all the blocks in the block cache and there is not secondary cache |
| 850 |
// insertion. 2 lookup is needed for the blocks. |
| 851 |
TEST_F(DBSecondaryCacheTest, NoSecondaryCacheInsertion) { |
| 852 |
LRUCacheOptions opts(1024 * 1024, 0, false, 0.5, nullptr, |
| 853 |
kDefaultToAdaptiveMutex, kDontChargeCacheMetadata); |
| 854 |
std::shared_ptr<TestSecondaryCache> secondary_cache( |
| 855 |
new TestSecondaryCache(2048 * 1024)); |
| 856 |
opts.secondary_cache = secondary_cache; |
| 857 |
std::shared_ptr<Cache> cache = NewLRUCache(opts); |
| 858 |
BlockBasedTableOptions table_options; |
| 859 |
table_options.block_cache = cache; |
| 860 |
table_options.block_size = 4 * 1024; |
| 861 |
Options options = GetDefaultOptions(); |
| 862 |
options.create_if_missing = true; |
| 863 |
options.paranoid_file_checks = true; |
| 864 |
options.table_factory.reset(NewBlockBasedTableFactory(table_options)); |
| 865 |
options.env = fault_env_.get(); |
| 866 |
fault_fs_->SetFailGetUniqueId(true); |
| 867 |
|
| 868 |
DestroyAndReopen(options); |
| 869 |
std::string session_id; |
| 870 |
ASSERT_OK(db_->GetDbSessionId(session_id)); |
| 871 |
secondary_cache->SetDbSessionId(session_id); |
| 872 |
Random rnd(301); |
| 873 |
const int N = 6; |
| 874 |
for (int i = 0; i < N; i++) { |
| 875 |
std::string p_v = rnd.RandomString(1000); |
| 876 |
ASSERT_OK(Put(Key(i), p_v)); |
| 877 |
} |
| 878 |
|
| 879 |
ASSERT_OK(Flush()); |
| 880 |
// After Flush is successful, RocksDB will do the paranoid check for the new |
| 881 |
// SST file. Meta blocks are always cached in the block cache and they |
| 882 |
// will not be evicted. Now, block cache is large enough, it cache |
| 883 |
// both block_1 and block_2. When first time read block_1 and block_2 |
| 884 |
// there are cache misses. So 2 secondary cache lookups are needed for |
| 885 |
// the 2 blocks |
| 886 |
ASSERT_EQ(secondary_cache->num_inserts(), 0u); |
| 887 |
ASSERT_EQ(secondary_cache->num_lookups(), 2u); |
| 888 |
|
| 889 |
Compact("a", "z"); |
| 890 |
// Compaction will iterate the whole SST file. Since all the data blocks |
| 891 |
// are in the block cache. No need to lookup the secondary cache. |
| 892 |
ASSERT_EQ(secondary_cache->num_inserts(), 0u); |
| 893 |
ASSERT_EQ(secondary_cache->num_lookups(), 2u); |
| 894 |
|
| 895 |
std::string v = Get(Key(0)); |
| 896 |
ASSERT_EQ(1000, v.size()); |
| 897 |
// Since the block cache is large enough, all the blocks are cached. we |
| 898 |
// do not need to lookup the seondary cache. |
| 899 |
ASSERT_EQ(secondary_cache->num_inserts(), 0u); |
| 900 |
ASSERT_EQ(secondary_cache->num_lookups(), 2u); |
| 901 |
|
| 902 |
Destroy(options); |
| 903 |
} |
| 904 |
|
| 905 |
TEST_F(DBSecondaryCacheTest, SecondaryCacheIntensiveTesting) { |
| 906 |
LRUCacheOptions opts(8 * 1024, 0, false, 0.5, nullptr, |
| 907 |
kDefaultToAdaptiveMutex, kDontChargeCacheMetadata); |
| 908 |
std::shared_ptr<TestSecondaryCache> secondary_cache( |
| 909 |
new TestSecondaryCache(2048 * 1024)); |
| 910 |
opts.secondary_cache = secondary_cache; |
| 911 |
std::shared_ptr<Cache> cache = NewLRUCache(opts); |
| 912 |
BlockBasedTableOptions table_options; |
| 913 |
table_options.block_cache = cache; |
| 914 |
table_options.block_size = 4 * 1024; |
| 915 |
Options options = GetDefaultOptions(); |
| 916 |
options.create_if_missing = true; |
| 917 |
options.table_factory.reset(NewBlockBasedTableFactory(table_options)); |
| 918 |
options.env = fault_env_.get(); |
| 919 |
fault_fs_->SetFailGetUniqueId(true); |
| 920 |
DestroyAndReopen(options); |
| 921 |
std::string session_id; |
| 922 |
ASSERT_OK(db_->GetDbSessionId(session_id)); |
| 923 |
secondary_cache->SetDbSessionId(session_id); |
| 924 |
Random rnd(301); |
| 925 |
const int N = 256; |
| 926 |
for (int i = 0; i < N; i++) { |
| 927 |
std::string p_v = rnd.RandomString(1000); |
| 928 |
ASSERT_OK(Put(Key(i), p_v)); |
| 929 |
} |
| 930 |
ASSERT_OK(Flush()); |
| 931 |
Compact("a", "z"); |
| 932 |
|
| 933 |
Random r_index(47); |
| 934 |
std::string v; |
| 935 |
for (int i = 0; i < 1000; i++) { |
| 936 |
uint32_t key_i = r_index.Next() % N; |
| 937 |
v = Get(Key(key_i)); |
| 938 |
} |
| 939 |
|
| 940 |
// We have over 200 data blocks there will be multiple insertion |
| 941 |
// and lookups. |
| 942 |
ASSERT_GE(secondary_cache->num_inserts(), 1u); |
| 943 |
ASSERT_GE(secondary_cache->num_lookups(), 1u); |
| 944 |
|
| 945 |
Destroy(options); |
| 946 |
} |
| 947 |
|
| 948 |
// In this test, the block cache size is set to 4096, after insert 6 KV-pairs |
| 949 |
// and flush, there are 5 blocks in this SST file, 2 data blocks and 3 meta |
| 950 |
// blocks. block_1 size is 4096 and block_2 size is 2056. The total size |
| 951 |
// of the meta blocks are about 900 to 1000. Therefore, in any situation, |
| 952 |
// if we try to insert block_1 to the block cache, it will always fails. Only |
| 953 |
// block_2 will be successfully inserted into the block cache. |
| 954 |
TEST_F(DBSecondaryCacheTest, SecondaryCacheFailureTest) { |
| 955 |
LRUCacheOptions opts(4 * 1024, 0, false, 0.5, nullptr, |
| 956 |
kDefaultToAdaptiveMutex, kDontChargeCacheMetadata); |
| 957 |
std::shared_ptr<TestSecondaryCache> secondary_cache( |
| 958 |
new TestSecondaryCache(2048 * 1024)); |
| 959 |
opts.secondary_cache = secondary_cache; |
| 960 |
std::shared_ptr<Cache> cache = NewLRUCache(opts); |
| 961 |
BlockBasedTableOptions table_options; |
| 962 |
table_options.block_cache = cache; |
| 963 |
table_options.block_size = 4 * 1024; |
| 964 |
Options options = GetDefaultOptions(); |
| 965 |
options.create_if_missing = true; |
| 966 |
options.paranoid_file_checks = true; |
| 967 |
options.table_factory.reset(NewBlockBasedTableFactory(table_options)); |
| 968 |
options.env = fault_env_.get(); |
| 969 |
fault_fs_->SetFailGetUniqueId(true); |
| 970 |
DestroyAndReopen(options); |
| 971 |
std::string session_id; |
| 972 |
ASSERT_OK(db_->GetDbSessionId(session_id)); |
| 973 |
secondary_cache->SetDbSessionId(session_id); |
| 974 |
Random rnd(301); |
| 975 |
const int N = 6; |
| 976 |
for (int i = 0; i < N; i++) { |
| 977 |
std::string p_v = rnd.RandomString(1007); |
| 978 |
ASSERT_OK(Put(Key(i), p_v)); |
| 979 |
} |
| 980 |
|
| 981 |
ASSERT_OK(Flush()); |
| 982 |
// After Flush is successful, RocksDB will do the paranoid check for the new |
| 983 |
// SST file. Meta blocks are always cached in the block cache and they |
| 984 |
// will not be evicted. When block_2 is cache miss and read out, it is |
| 985 |
// inserted to the block cache. Note that, block_1 is never successfully |
| 986 |
// inserted to the block cache. Here are 2 lookups in the secondary cache |
| 987 |
// for block_1 and block_2 |
| 988 |
ASSERT_EQ(secondary_cache->num_inserts(), 0u); |
| 989 |
ASSERT_EQ(secondary_cache->num_lookups(), 2u); |
| 990 |
|
| 991 |
// Fail the insertion, in LRU cache, the secondary insertion returned status |
| 992 |
// is not checked, therefore, the DB will not be influenced. |
| 993 |
secondary_cache->InjectFailure(); |
| 994 |
Compact("a", "z"); |
| 995 |
// Compaction will create the iterator to scan the whole file. So all the |
| 996 |
// blocks are needed. Meta blocks are always cached. When block_1 is read |
| 997 |
// out, block_2 is evicted from block cache and inserted to secondary |
| 998 |
// cache. |
| 999 |
ASSERT_EQ(secondary_cache->num_inserts(), 0u); |
| 1000 |
ASSERT_EQ(secondary_cache->num_lookups(), 3u); |
| 1001 |
|
| 1002 |
std::string v = Get(Key(0)); |
| 1003 |
ASSERT_EQ(1007, v.size()); |
| 1004 |
// The first data block is not in the cache, similarly, trigger the block |
| 1005 |
// cache Lookup and secondary cache lookup for block_1. But block_1 will not |
| 1006 |
// be inserted successfully due to the size. Currently, cache only has |
| 1007 |
// the meta blocks. |
| 1008 |
ASSERT_EQ(secondary_cache->num_inserts(), 0u); |
| 1009 |
ASSERT_EQ(secondary_cache->num_lookups(), 4u); |
| 1010 |
|
| 1011 |
v = Get(Key(5)); |
| 1012 |
ASSERT_EQ(1007, v.size()); |
| 1013 |
// The second data block is not in the cache, similarly, trigger the block |
| 1014 |
// cache Lookup and secondary cache lookup for block_2 and block_2 is found |
| 1015 |
// in the secondary cache. Now block cache has block_2 |
| 1016 |
ASSERT_EQ(secondary_cache->num_inserts(), 0u); |
| 1017 |
ASSERT_EQ(secondary_cache->num_lookups(), 5u); |
| 1018 |
|
| 1019 |
v = Get(Key(5)); |
| 1020 |
ASSERT_EQ(1007, v.size()); |
| 1021 |
// block_2 is in the block cache. There is a block cache hit. No need to |
| 1022 |
// lookup or insert the secondary cache. |
| 1023 |
ASSERT_EQ(secondary_cache->num_inserts(), 0u); |
| 1024 |
ASSERT_EQ(secondary_cache->num_lookups(), 5u); |
| 1025 |
|
| 1026 |
v = Get(Key(0)); |
| 1027 |
ASSERT_EQ(1007, v.size()); |
| 1028 |
// Lookup the first data block, not in the block cache, so lookup the |
| 1029 |
// secondary cache. Also not in the secondary cache. After Get, still |
| 1030 |
// block_1 is will not be cached. |
| 1031 |
ASSERT_EQ(secondary_cache->num_inserts(), 0u); |
| 1032 |
ASSERT_EQ(secondary_cache->num_lookups(), 6u); |
| 1033 |
|
| 1034 |
v = Get(Key(0)); |
| 1035 |
ASSERT_EQ(1007, v.size()); |
| 1036 |
// Lookup the first data block, not in the block cache, so lookup the |
| 1037 |
// secondary cache. Also not in the secondary cache. After Get, still |
| 1038 |
// block_1 is will not be cached. |
| 1039 |
ASSERT_EQ(secondary_cache->num_inserts(), 0u); |
| 1040 |
ASSERT_EQ(secondary_cache->num_lookups(), 7u); |
| 1041 |
secondary_cache->ResetInjectFailure(); |
| 1042 |
|
| 1043 |
Destroy(options); |
| 1044 |
} |
| 1045 |
|
| 1046 |
TEST_F(LRUSecondaryCacheTest, BasicWaitAllTest) { |
| 1047 |
LRUCacheOptions opts(1024, 2, false, 0.5, nullptr, kDefaultToAdaptiveMutex, |
| 1048 |
kDontChargeCacheMetadata); |
| 1049 |
std::shared_ptr<TestSecondaryCache> secondary_cache = |
| 1050 |
std::make_shared<TestSecondaryCache>(32 * 1024); |
| 1051 |
opts.secondary_cache = secondary_cache; |
| 1052 |
std::shared_ptr<Cache> cache = NewLRUCache(opts); |
| 1053 |
const int num_keys = 32; |
| 1054 |
|
| 1055 |
Random rnd(301); |
| 1056 |
std::vector<std::string> values; |
| 1057 |
for (int i = 0; i < num_keys; ++i) { |
| 1058 |
std::string str = rnd.RandomString(1020); |
| 1059 |
values.emplace_back(str); |
| 1060 |
TestItem* item = new TestItem(str.data(), str.length()); |
| 1061 |
ASSERT_OK(cache->Insert("k" + std::to_string(i), item, |
| 1062 |
&LRUSecondaryCacheTest::helper_, str.length())); |
| 1063 |
} |
| 1064 |
// Force all entries to be evicted to the secondary cache |
| 1065 |
cache->SetCapacity(0); |
| 1066 |
ASSERT_EQ(secondary_cache->num_inserts(), 32u); |
| 1067 |
cache->SetCapacity(32 * 1024); |
| 1068 |
|
| 1069 |
secondary_cache->SetResultMap( |
| 1070 |
{{"k3", TestSecondaryCache::ResultType::DEFER}, |
| 1071 |
{"k4", TestSecondaryCache::ResultType::DEFER_AND_FAIL}, |
| 1072 |
{"k5", TestSecondaryCache::ResultType::FAIL}}); |
| 1073 |
std::vector<Cache::Handle*> results; |
| 1074 |
for (int i = 0; i < 6; ++i) { |
| 1075 |
results.emplace_back( |
| 1076 |
cache->Lookup("k" + std::to_string(i), &LRUSecondaryCacheTest::helper_, |
| 1077 |
test_item_creator, Cache::Priority::LOW, false)); |
| 1078 |
} |
| 1079 |
cache->WaitAll(results); |
| 1080 |
for (int i = 0; i < 6; ++i) { |
| 1081 |
if (i == 4) { |
| 1082 |
ASSERT_EQ(cache->Value(results[i]), nullptr); |
| 1083 |
} else if (i == 5) { |
| 1084 |
ASSERT_EQ(results[i], nullptr); |
| 1085 |
continue; |
| 1086 |
} else { |
| 1087 |
TestItem* item = static_cast<TestItem*>(cache->Value(results[i])); |
| 1088 |
ASSERT_EQ(item->ToString(), values[i]); |
| 1089 |
} |
| 1090 |
cache->Release(results[i]); |
| 1091 |
} |
| 1092 |
|
| 1093 |
cache.reset(); |
| 1094 |
secondary_cache.reset(); |
| 1095 |
} |
| 1096 |
|
| 1097 |
// In this test, we have one KV pair per data block. We indirectly determine |
| 1098 |
// the cache key associated with each data block (and thus each KV) by using |
| 1099 |
// a sync point callback in TestSecondaryCache::Lookup. We then control the |
| 1100 |
// lookup result by setting the ResultMap. |
| 1101 |
TEST_F(DBSecondaryCacheTest, TestSecondaryCacheMultiGet) { |
| 1102 |
LRUCacheOptions opts(1 << 20, 0, false, 0.5, nullptr, kDefaultToAdaptiveMutex, |
| 1103 |
kDontChargeCacheMetadata); |
| 1104 |
std::shared_ptr<TestSecondaryCache> secondary_cache( |
| 1105 |
new TestSecondaryCache(2048 * 1024)); |
| 1106 |
opts.secondary_cache = secondary_cache; |
| 1107 |
std::shared_ptr<Cache> cache = NewLRUCache(opts); |
| 1108 |
BlockBasedTableOptions table_options; |
| 1109 |
table_options.block_cache = cache; |
| 1110 |
table_options.block_size = 4 * 1024; |
| 1111 |
table_options.cache_index_and_filter_blocks = false; |
| 1112 |
Options options = GetDefaultOptions(); |
| 1113 |
options.create_if_missing = true; |
| 1114 |
options.table_factory.reset(NewBlockBasedTableFactory(table_options)); |
| 1115 |
options.paranoid_file_checks = true; |
| 1116 |
DestroyAndReopen(options); |
| 1117 |
Random rnd(301); |
| 1118 |
const int N = 8; |
| 1119 |
std::vector<std::string> keys; |
| 1120 |
for (int i = 0; i < N; i++) { |
| 1121 |
std::string p_v = rnd.RandomString(4000); |
| 1122 |
keys.emplace_back(p_v); |
| 1123 |
ASSERT_OK(Put(Key(i), p_v)); |
| 1124 |
} |
| 1125 |
|
| 1126 |
ASSERT_OK(Flush()); |
| 1127 |
// After Flush is successful, RocksDB does the paranoid check for the new |
| 1128 |
// SST file. This will try to lookup all data blocks in the secondary |
| 1129 |
// cache. |
| 1130 |
ASSERT_EQ(secondary_cache->num_inserts(), 0u); |
| 1131 |
ASSERT_EQ(secondary_cache->num_lookups(), 8u); |
| 1132 |
|
| 1133 |
cache->SetCapacity(0); |
| 1134 |
ASSERT_EQ(secondary_cache->num_inserts(), 8u); |
| 1135 |
cache->SetCapacity(1 << 20); |
| 1136 |
|
| 1137 |
std::vector<std::string> cache_keys; |
| 1138 |
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->SetCallBack( |
| 1139 |
"TestSecondaryCache::Lookup", [&cache_keys](void* key) -> void { |
| 1140 |
cache_keys.emplace_back(*(static_cast<std::string*>(key))); |
| 1141 |
}); |
| 1142 |
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->EnableProcessing(); |
| 1143 |
for (int i = 0; i < N; ++i) { |
| 1144 |
std::string v = Get(Key(i)); |
| 1145 |
ASSERT_EQ(4000, v.size()); |
| 1146 |
ASSERT_EQ(v, keys[i]); |
| 1147 |
} |
| 1148 |
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->DisableProcessing(); |
| 1149 |
ASSERT_EQ(secondary_cache->num_lookups(), 16u); |
| 1150 |
cache->SetCapacity(0); |
| 1151 |
cache->SetCapacity(1 << 20); |
| 1152 |
|
| 1153 |
ASSERT_EQ(Get(Key(2)), keys[2]); |
| 1154 |
ASSERT_EQ(Get(Key(7)), keys[7]); |
| 1155 |
secondary_cache->SetResultMap( |
| 1156 |
{{cache_keys[3], TestSecondaryCache::ResultType::DEFER}, |
| 1157 |
{cache_keys[4], TestSecondaryCache::ResultType::DEFER_AND_FAIL}, |
| 1158 |
{cache_keys[5], TestSecondaryCache::ResultType::FAIL}}); |
| 1159 |
|
| 1160 |
std::vector<std::string> mget_keys( |
| 1161 |
{Key(0), Key(1), Key(2), Key(3), Key(4), Key(5), Key(6), Key(7)}); |
| 1162 |
std::vector<PinnableSlice> values(mget_keys.size()); |
| 1163 |
std::vector<Status> s(keys.size()); |
| 1164 |
std::vector<Slice> key_slices; |
| 1165 |
for (const std::string& key : mget_keys) { |
| 1166 |
key_slices.emplace_back(key); |
| 1167 |
} |
| 1168 |
uint32_t num_lookups = secondary_cache->num_lookups(); |
| 1169 |
dbfull()->MultiGet(ReadOptions(), dbfull()->DefaultColumnFamily(), |
| 1170 |
key_slices.size(), key_slices.data(), values.data(), |
| 1171 |
s.data(), false); |
| 1172 |
ASSERT_EQ(secondary_cache->num_lookups(), num_lookups + 5); |
| 1173 |
for (int i = 0; i < N; ++i) { |
| 1174 |
ASSERT_OK(s[i]); |
| 1175 |
ASSERT_EQ(values[i].ToString(), keys[i]); |
| 1176 |
values[i].Reset(); |
| 1177 |
} |
| 1178 |
Destroy(options); |
| 1179 |
} |
| 1180 |
|
| 1181 |
class LRUCacheWithStat : public LRUCache { |
| 1182 |
public: |
| 1183 |
LRUCacheWithStat( |
| 1184 |
size_t _capacity, int _num_shard_bits, bool _strict_capacity_limit, |
| 1185 |
double _high_pri_pool_ratio, |
| 1186 |
std::shared_ptr<MemoryAllocator> _memory_allocator = nullptr, |
| 1187 |
bool _use_adaptive_mutex = kDefaultToAdaptiveMutex, |
| 1188 |
CacheMetadataChargePolicy _metadata_charge_policy = |
| 1189 |
kDontChargeCacheMetadata, |
| 1190 |
const std::shared_ptr<SecondaryCache>& _secondary_cache = nullptr) |
| 1191 |
: LRUCache(_capacity, _num_shard_bits, _strict_capacity_limit, |
| 1192 |
_high_pri_pool_ratio, _memory_allocator, _use_adaptive_mutex, |
| 1193 |
_metadata_charge_policy, _secondary_cache) { |
| 1194 |
insert_count_ = 0; |
| 1195 |
lookup_count_ = 0; |
| 1196 |
} |
| 1197 |
~LRUCacheWithStat() {} |
| 1198 |
|
| 1199 |
Status Insert(const Slice& key, void* value, size_t charge, DeleterFn deleter, |
| 1200 |
Handle** handle, Priority priority) override { |
| 1201 |
insert_count_++; |
| 1202 |
return LRUCache::Insert(key, value, charge, deleter, handle, priority); |
| 1203 |
} |
| 1204 |
Status Insert(const Slice& key, void* value, const CacheItemHelper* helper, |
| 1205 |
size_t chargge, Handle** handle = nullptr, |
| 1206 |
Priority priority = Priority::LOW) override { |
| 1207 |
insert_count_++; |
| 1208 |
return LRUCache::Insert(key, value, helper, chargge, handle, priority); |
| 1209 |
} |
| 1210 |
Handle* Lookup(const Slice& key, Statistics* stats) override { |
| 1211 |
lookup_count_++; |
| 1212 |
return LRUCache::Lookup(key, stats); |
| 1213 |
} |
| 1214 |
Handle* Lookup(const Slice& key, const CacheItemHelper* helper, |
| 1215 |
const CreateCallback& create_cb, Priority priority, bool wait, |
| 1216 |
Statistics* stats = nullptr) override { |
| 1217 |
lookup_count_++; |
| 1218 |
return LRUCache::Lookup(key, helper, create_cb, priority, wait, stats); |
| 1219 |
} |
| 1220 |
|
| 1221 |
uint32_t GetInsertCount() { return insert_count_; } |
| 1222 |
uint32_t GetLookupcount() { return lookup_count_; } |
| 1223 |
void ResetCount() { |
| 1224 |
insert_count_ = 0; |
| 1225 |
lookup_count_ = 0; |
| 1226 |
} |
| 1227 |
|
| 1228 |
private: |
| 1229 |
uint32_t insert_count_; |
| 1230 |
uint32_t lookup_count_; |
| 1231 |
}; |
| 1232 |
|
| 1233 |
#ifndef ROCKSDB_LITE |
| 1234 |
|
| 1235 |
TEST_F(DBSecondaryCacheTest, LRUCacheDumpLoadBasic) { |
| 1236 |
LRUCacheOptions cache_opts(1024 * 1024, 0, false, 0.5, nullptr, |
| 1237 |
kDefaultToAdaptiveMutex, kDontChargeCacheMetadata); |
| 1238 |
LRUCacheWithStat* tmp_cache = new LRUCacheWithStat( |
| 1239 |
cache_opts.capacity, cache_opts.num_shard_bits, |
| 1240 |
cache_opts.strict_capacity_limit, cache_opts.high_pri_pool_ratio, |
| 1241 |
cache_opts.memory_allocator, cache_opts.use_adaptive_mutex, |
| 1242 |
cache_opts.metadata_charge_policy, cache_opts.secondary_cache); |
| 1243 |
std::shared_ptr<Cache> cache(tmp_cache); |
| 1244 |
BlockBasedTableOptions table_options; |
| 1245 |
table_options.block_cache = cache; |
| 1246 |
table_options.block_size = 4 * 1024; |
| 1247 |
Options options = GetDefaultOptions(); |
| 1248 |
options.create_if_missing = true; |
| 1249 |
options.table_factory.reset(NewBlockBasedTableFactory(table_options)); |
| 1250 |
options.env = fault_env_.get(); |
| 1251 |
DestroyAndReopen(options); |
| 1252 |
fault_fs_->SetFailGetUniqueId(true); |
| 1253 |
|
| 1254 |
Random rnd(301); |
| 1255 |
const int N = 256; |
| 1256 |
std::vector<std::string> value; |
| 1257 |
char buf[1000]; |
| 1258 |
memset(buf, 'a', 1000); |
| 1259 |
value.resize(N); |
| 1260 |
for (int i = 0; i < N; i++) { |
| 1261 |
// std::string p_v = rnd.RandomString(1000); |
| 1262 |
std::string p_v(buf, 1000); |
| 1263 |
value[i] = p_v; |
| 1264 |
ASSERT_OK(Put(Key(i), p_v)); |
| 1265 |
} |
| 1266 |
ASSERT_OK(Flush()); |
| 1267 |
Compact("a", "z"); |
| 1268 |
|
| 1269 |
// do th eread for all the key value pairs, so all the blocks should be in |
| 1270 |
// cache |
| 1271 |
uint32_t start_insert = tmp_cache->GetInsertCount(); |
| 1272 |
uint32_t start_lookup = tmp_cache->GetLookupcount(); |
| 1273 |
std::string v; |
| 1274 |
for (int i = 0; i < N; i++) { |
| 1275 |
v = Get(Key(i)); |
| 1276 |
ASSERT_EQ(v, value[i]); |
| 1277 |
} |
| 1278 |
uint32_t dump_insert = tmp_cache->GetInsertCount() - start_insert; |
| 1279 |
uint32_t dump_lookup = tmp_cache->GetLookupcount() - start_lookup; |
| 1280 |
ASSERT_EQ(63, |
| 1281 |
static_cast<int>(dump_insert)); // the insert in the block cache |
| 1282 |
ASSERT_EQ(256, |
| 1283 |
static_cast<int>(dump_lookup)); // the lookup in the block cache |
| 1284 |
// We have enough blocks in the block cache |
| 1285 |
|
| 1286 |
CacheDumpOptions cd_options; |
| 1287 |
cd_options.clock = fault_env_->GetSystemClock().get(); |
| 1288 |
std::string dump_path = db_->GetName() + "/cache_dump"; |
| 1289 |
std::unique_ptr<CacheDumpWriter> dump_writer; |
| 1290 |
Status s = NewToFileCacheDumpWriter(fault_fs_, FileOptions(), dump_path, |
| 1291 |
&dump_writer); |
| 1292 |
ASSERT_OK(s); |
| 1293 |
std::unique_ptr<CacheDumper> cache_dumper; |
| 1294 |
s = NewDefaultCacheDumper(cd_options, cache, std::move(dump_writer), |
| 1295 |
&cache_dumper); |
| 1296 |
ASSERT_OK(s); |
| 1297 |
std::vector<DB*> db_list; |
| 1298 |
db_list.push_back(db_); |
| 1299 |
s = cache_dumper->SetDumpFilter(db_list); |
| 1300 |
ASSERT_OK(s); |
| 1301 |
s = cache_dumper->DumpCacheEntriesToWriter(); |
| 1302 |
ASSERT_OK(s); |
| 1303 |
cache_dumper.reset(); |
| 1304 |
|
| 1305 |
// we have a new cache it is empty, then, before we do the Get, we do the |
| 1306 |
// dumpload |
| 1307 |
std::shared_ptr<TestSecondaryCache> secondary_cache = |
| 1308 |
std::make_shared<TestSecondaryCache>(2048 * 1024); |
| 1309 |
cache_opts.secondary_cache = secondary_cache; |
| 1310 |
tmp_cache = new LRUCacheWithStat( |
| 1311 |
cache_opts.capacity, cache_opts.num_shard_bits, |
| 1312 |
cache_opts.strict_capacity_limit, cache_opts.high_pri_pool_ratio, |
| 1313 |
cache_opts.memory_allocator, cache_opts.use_adaptive_mutex, |
| 1314 |
cache_opts.metadata_charge_policy, cache_opts.secondary_cache); |
| 1315 |
std::shared_ptr<Cache> cache_new(tmp_cache); |
| 1316 |
table_options.block_cache = cache_new; |
| 1317 |
table_options.block_size = 4 * 1024; |
| 1318 |
options.create_if_missing = true; |
| 1319 |
options.table_factory.reset(NewBlockBasedTableFactory(table_options)); |
| 1320 |
options.env = fault_env_.get(); |
| 1321 |
|
| 1322 |
// start to load the data to new block cache |
| 1323 |
start_insert = secondary_cache->num_inserts(); |
| 1324 |
start_lookup = secondary_cache->num_lookups(); |
| 1325 |
std::unique_ptr<CacheDumpReader> dump_reader; |
| 1326 |
s = NewFromFileCacheDumpReader(fault_fs_, FileOptions(), dump_path, |
| 1327 |
&dump_reader); |
| 1328 |
ASSERT_OK(s); |
| 1329 |
std::unique_ptr<CacheDumpedLoader> cache_loader; |
| 1330 |
s = NewDefaultCacheDumpedLoader(cd_options, table_options, secondary_cache, |
| 1331 |
std::move(dump_reader), &cache_loader); |
| 1332 |
ASSERT_OK(s); |
| 1333 |
s = cache_loader->RestoreCacheEntriesToSecondaryCache(); |
| 1334 |
ASSERT_OK(s); |
| 1335 |
uint32_t load_insert = secondary_cache->num_inserts() - start_insert; |
| 1336 |
uint32_t load_lookup = secondary_cache->num_lookups() - start_lookup; |
| 1337 |
// check the number we inserted |
| 1338 |
ASSERT_EQ(64, static_cast<int>(load_insert)); |
| 1339 |
ASSERT_EQ(0, static_cast<int>(load_lookup)); |
| 1340 |
ASSERT_OK(s); |
| 1341 |
|
| 1342 |
Reopen(options); |
| 1343 |
|
| 1344 |
// After load, we do the Get again |
| 1345 |
start_insert = secondary_cache->num_inserts(); |
| 1346 |
start_lookup = secondary_cache->num_lookups(); |
| 1347 |
uint32_t cache_insert = tmp_cache->GetInsertCount(); |
| 1348 |
uint32_t cache_lookup = tmp_cache->GetLookupcount(); |
| 1349 |
for (int i = 0; i < N; i++) { |
| 1350 |
v = Get(Key(i)); |
| 1351 |
ASSERT_EQ(v, value[i]); |
| 1352 |
} |
| 1353 |
uint32_t final_insert = secondary_cache->num_inserts() - start_insert; |
| 1354 |
uint32_t final_lookup = secondary_cache->num_lookups() - start_lookup; |
| 1355 |
// no insert to secondary cache |
| 1356 |
ASSERT_EQ(0, static_cast<int>(final_insert)); |
| 1357 |
// lookup the secondary to get all blocks |
| 1358 |
ASSERT_EQ(64, static_cast<int>(final_lookup)); |
| 1359 |
uint32_t block_insert = tmp_cache->GetInsertCount() - cache_insert; |
| 1360 |
uint32_t block_lookup = tmp_cache->GetLookupcount() - cache_lookup; |
| 1361 |
// Check the new block cache insert and lookup, should be no insert since all |
| 1362 |
// blocks are from the secondary cache. |
| 1363 |
ASSERT_EQ(0, static_cast<int>(block_insert)); |
| 1364 |
ASSERT_EQ(256, static_cast<int>(block_lookup)); |
| 1365 |
|
| 1366 |
fault_fs_->SetFailGetUniqueId(false); |
| 1367 |
Destroy(options); |
| 1368 |
} |
| 1369 |
|
| 1370 |
TEST_F(DBSecondaryCacheTest, LRUCacheDumpLoadWithFilter) { |
| 1371 |
LRUCacheOptions cache_opts(1024 * 1024, 0, false, 0.5, nullptr, |
| 1372 |
kDefaultToAdaptiveMutex, kDontChargeCacheMetadata); |
| 1373 |
LRUCacheWithStat* tmp_cache = new LRUCacheWithStat( |
| 1374 |
cache_opts.capacity, cache_opts.num_shard_bits, |
| 1375 |
cache_opts.strict_capacity_limit, cache_opts.high_pri_pool_ratio, |
| 1376 |
cache_opts.memory_allocator, cache_opts.use_adaptive_mutex, |
| 1377 |
cache_opts.metadata_charge_policy, cache_opts.secondary_cache); |
| 1378 |
std::shared_ptr<Cache> cache(tmp_cache); |
| 1379 |
BlockBasedTableOptions table_options; |
| 1380 |
table_options.block_cache = cache; |
| 1381 |
table_options.block_size = 4 * 1024; |
| 1382 |
Options options = GetDefaultOptions(); |
| 1383 |
options.create_if_missing = true; |
| 1384 |
options.table_factory.reset(NewBlockBasedTableFactory(table_options)); |
| 1385 |
options.env = fault_env_.get(); |
| 1386 |
std::string dbname1 = test::PerThreadDBPath("db_1"); |
| 1387 |
ASSERT_OK(DestroyDB(dbname1, options)); |
| 1388 |
DB* db1 = nullptr; |
| 1389 |
ASSERT_OK(DB::Open(options, dbname1, &db1)); |
| 1390 |
std::string dbname2 = test::PerThreadDBPath("db_2"); |
| 1391 |
ASSERT_OK(DestroyDB(dbname2, options)); |
| 1392 |
DB* db2 = nullptr; |
| 1393 |
ASSERT_OK(DB::Open(options, dbname2, &db2)); |
| 1394 |
fault_fs_->SetFailGetUniqueId(true); |
| 1395 |
|
| 1396 |
// write the KVs to db1 |
| 1397 |
Random rnd(301); |
| 1398 |
const int N = 256; |
| 1399 |
std::vector<std::string> value1; |
| 1400 |
WriteOptions wo; |
| 1401 |
char buf[1000]; |
| 1402 |
memset(buf, 'a', 1000); |
| 1403 |
value1.resize(N); |
| 1404 |
for (int i = 0; i < N; i++) { |
| 1405 |
std::string p_v(buf, 1000); |
| 1406 |
value1[i] = p_v; |
| 1407 |
ASSERT_OK(db1->Put(wo, Key(i), p_v)); |
| 1408 |
} |
| 1409 |
ASSERT_OK(db1->Flush(FlushOptions())); |
| 1410 |
Slice bg("a"); |
| 1411 |
Slice ed("b"); |
| 1412 |
ASSERT_OK(db1->CompactRange(CompactRangeOptions(), &bg, &ed)); |
| 1413 |
|
| 1414 |
// Write the KVs to DB2 |
| 1415 |
std::vector<std::string> value2; |
| 1416 |
memset(buf, 'b', 1000); |
| 1417 |
value2.resize(N); |
| 1418 |
for (int i = 0; i < N; i++) { |
| 1419 |
std::string p_v(buf, 1000); |
| 1420 |
value2[i] = p_v; |
| 1421 |
ASSERT_OK(db2->Put(wo, Key(i), p_v)); |
| 1422 |
} |
| 1423 |
ASSERT_OK(db2->Flush(FlushOptions())); |
| 1424 |
ASSERT_OK(db2->CompactRange(CompactRangeOptions(), &bg, &ed)); |
| 1425 |
|
| 1426 |
// do th eread for all the key value pairs, so all the blocks should be in |
| 1427 |
// cache |
| 1428 |
uint32_t start_insert = tmp_cache->GetInsertCount(); |
| 1429 |
uint32_t start_lookup = tmp_cache->GetLookupcount(); |
| 1430 |
ReadOptions ro; |
| 1431 |
std::string v; |
| 1432 |
for (int i = 0; i < N; i++) { |
| 1433 |
ASSERT_OK(db1->Get(ro, Key(i), &v)); |
| 1434 |
ASSERT_EQ(v, value1[i]); |
| 1435 |
} |
| 1436 |
for (int i = 0; i < N; i++) { |
| 1437 |
ASSERT_OK(db2->Get(ro, Key(i), &v)); |
| 1438 |
ASSERT_EQ(v, value2[i]); |
| 1439 |
} |
| 1440 |
uint32_t dump_insert = tmp_cache->GetInsertCount() - start_insert; |
| 1441 |
uint32_t dump_lookup = tmp_cache->GetLookupcount() - start_lookup; |
| 1442 |
ASSERT_EQ(128, |
| 1443 |
static_cast<int>(dump_insert)); // the insert in the block cache |
| 1444 |
ASSERT_EQ(512, |
| 1445 |
static_cast<int>(dump_lookup)); // the lookup in the block cache |
| 1446 |
// We have enough blocks in the block cache |
| 1447 |
|
| 1448 |
CacheDumpOptions cd_options; |
| 1449 |
cd_options.clock = fault_env_->GetSystemClock().get(); |
| 1450 |
std::string dump_path = db1->GetName() + "/cache_dump"; |
| 1451 |
std::unique_ptr<CacheDumpWriter> dump_writer; |
| 1452 |
Status s = NewToFileCacheDumpWriter(fault_fs_, FileOptions(), dump_path, |
| 1453 |
&dump_writer); |
| 1454 |
ASSERT_OK(s); |
| 1455 |
std::unique_ptr<CacheDumper> cache_dumper; |
| 1456 |
s = NewDefaultCacheDumper(cd_options, cache, std::move(dump_writer), |
| 1457 |
&cache_dumper); |
| 1458 |
ASSERT_OK(s); |
| 1459 |
std::vector<DB*> db_list; |
| 1460 |
db_list.push_back(db1); |
| 1461 |
s = cache_dumper->SetDumpFilter(db_list); |
| 1462 |
ASSERT_OK(s); |
| 1463 |
s = cache_dumper->DumpCacheEntriesToWriter(); |
| 1464 |
ASSERT_OK(s); |
| 1465 |
cache_dumper.reset(); |
| 1466 |
|
| 1467 |
// we have a new cache it is empty, then, before we do the Get, we do the |
| 1468 |
// dumpload |
| 1469 |
std::shared_ptr<TestSecondaryCache> secondary_cache = |
| 1470 |
std::make_shared<TestSecondaryCache>(2048 * 1024); |
| 1471 |
cache_opts.secondary_cache = secondary_cache; |
| 1472 |
tmp_cache = new LRUCacheWithStat( |
| 1473 |
cache_opts.capacity, cache_opts.num_shard_bits, |
| 1474 |
cache_opts.strict_capacity_limit, cache_opts.high_pri_pool_ratio, |
| 1475 |
cache_opts.memory_allocator, cache_opts.use_adaptive_mutex, |
| 1476 |
cache_opts.metadata_charge_policy, cache_opts.secondary_cache); |
| 1477 |
std::shared_ptr<Cache> cache_new(tmp_cache); |
| 1478 |
table_options.block_cache = cache_new; |
| 1479 |
table_options.block_size = 4 * 1024; |
| 1480 |
options.create_if_missing = true; |
| 1481 |
options.table_factory.reset(NewBlockBasedTableFactory(table_options)); |
| 1482 |
options.env = fault_env_.get(); |
| 1483 |
|
| 1484 |
// Start the cache loading process |
| 1485 |
start_insert = secondary_cache->num_inserts(); |
| 1486 |
start_lookup = secondary_cache->num_lookups(); |
| 1487 |
std::unique_ptr<CacheDumpReader> dump_reader; |
| 1488 |
s = NewFromFileCacheDumpReader(fault_fs_, FileOptions(), dump_path, |
| 1489 |
&dump_reader); |
| 1490 |
ASSERT_OK(s); |
| 1491 |
std::unique_ptr<CacheDumpedLoader> cache_loader; |
| 1492 |
s = NewDefaultCacheDumpedLoader(cd_options, table_options, secondary_cache, |
| 1493 |
std::move(dump_reader), &cache_loader); |
| 1494 |
ASSERT_OK(s); |
| 1495 |
s = cache_loader->RestoreCacheEntriesToSecondaryCache(); |
| 1496 |
ASSERT_OK(s); |
| 1497 |
uint32_t load_insert = secondary_cache->num_inserts() - start_insert; |
| 1498 |
uint32_t load_lookup = secondary_cache->num_lookups() - start_lookup; |
| 1499 |
// check the number we inserted |
| 1500 |
ASSERT_EQ(64, static_cast<int>(load_insert)); |
| 1501 |
ASSERT_EQ(0, static_cast<int>(load_lookup)); |
| 1502 |
ASSERT_OK(s); |
| 1503 |
|
| 1504 |
ASSERT_OK(db1->Close()); |
| 1505 |
delete db1; |
| 1506 |
ASSERT_OK(DB::Open(options, dbname1, &db1)); |
| 1507 |
|
| 1508 |
// After load, we do the Get again. To validate the cache, we do not allow any |
| 1509 |
// I/O, so we set the file system to false. |
| 1510 |
IOStatus error_msg = IOStatus::IOError("Retryable IO Error"); |
| 1511 |
fault_fs_->SetFilesystemActive(false, error_msg); |
| 1512 |
start_insert = secondary_cache->num_inserts(); |
| 1513 |
start_lookup = secondary_cache->num_lookups(); |
| 1514 |
uint32_t cache_insert = tmp_cache->GetInsertCount(); |
| 1515 |
uint32_t cache_lookup = tmp_cache->GetLookupcount(); |
| 1516 |
for (int i = 0; i < N; i++) { |
| 1517 |
ASSERT_OK(db1->Get(ro, Key(i), &v)); |
| 1518 |
ASSERT_EQ(v, value1[i]); |
| 1519 |
} |
| 1520 |
uint32_t final_insert = secondary_cache->num_inserts() - start_insert; |
| 1521 |
uint32_t final_lookup = secondary_cache->num_lookups() - start_lookup; |
| 1522 |
// no insert to secondary cache |
| 1523 |
ASSERT_EQ(0, static_cast<int>(final_insert)); |
| 1524 |
// lookup the secondary to get all blocks |
| 1525 |
ASSERT_EQ(64, static_cast<int>(final_lookup)); |
| 1526 |
uint32_t block_insert = tmp_cache->GetInsertCount() - cache_insert; |
| 1527 |
uint32_t block_lookup = tmp_cache->GetLookupcount() - cache_lookup; |
| 1528 |
// Check the new block cache insert and lookup, should be no insert since all |
| 1529 |
// blocks are from the secondary cache. |
| 1530 |
ASSERT_EQ(0, static_cast<int>(block_insert)); |
| 1531 |
ASSERT_EQ(256, static_cast<int>(block_lookup)); |
| 1532 |
fault_fs_->SetFailGetUniqueId(false); |
| 1533 |
fault_fs_->SetFilesystemActive(true); |
| 1534 |
delete db1; |
| 1535 |
delete db2; |
| 1536 |
ASSERT_OK(DestroyDB(dbname1, options)); |
| 1537 |
ASSERT_OK(DestroyDB(dbname2, options)); |
| 1538 |
} |
| 1539 |
|
| 1540 |
// Test the option not to use the secondary cache in a certain DB. |
| 1541 |
TEST_F(DBSecondaryCacheTest, TestSecondaryCacheOptionBasic) { |
| 1542 |
LRUCacheOptions opts(4 * 1024, 0, false, 0.5, nullptr, |
| 1543 |
kDefaultToAdaptiveMutex, kDontChargeCacheMetadata); |
| 1544 |
std::shared_ptr<TestSecondaryCache> secondary_cache( |
| 1545 |
new TestSecondaryCache(2048 * 1024)); |
| 1546 |
opts.secondary_cache = secondary_cache; |
| 1547 |
std::shared_ptr<Cache> cache = NewLRUCache(opts); |
| 1548 |
BlockBasedTableOptions table_options; |
| 1549 |
table_options.block_cache = cache; |
| 1550 |
table_options.block_size = 4 * 1024; |
| 1551 |
Options options = GetDefaultOptions(); |
| 1552 |
options.create_if_missing = true; |
| 1553 |
options.table_factory.reset(NewBlockBasedTableFactory(table_options)); |
| 1554 |
options.env = fault_env_.get(); |
| 1555 |
fault_fs_->SetFailGetUniqueId(true); |
| 1556 |
options.lowest_used_cache_tier = CacheTier::kVolatileTier; |
| 1557 |
|
| 1558 |
// Set the file paranoid check, so after flush, the file will be read |
| 1559 |
// all the blocks will be accessed. |
| 1560 |
options.paranoid_file_checks = true; |
| 1561 |
DestroyAndReopen(options); |
| 1562 |
std::string session_id; |
| 1563 |
ASSERT_OK(db_->GetDbSessionId(session_id)); |
| 1564 |
secondary_cache->SetDbSessionId(session_id); |
| 1565 |
Random rnd(301); |
| 1566 |
const int N = 6; |
| 1567 |
for (int i = 0; i < N; i++) { |
| 1568 |
std::string p_v = rnd.RandomString(1007); |
| 1569 |
ASSERT_OK(Put(Key(i), p_v)); |
| 1570 |
} |
| 1571 |
|
| 1572 |
ASSERT_OK(Flush()); |
| 1573 |
|
| 1574 |
for (int i = 0; i < N; i++) { |
| 1575 |
std::string p_v = rnd.RandomString(1007); |
| 1576 |
ASSERT_OK(Put(Key(i + 70), p_v)); |
| 1577 |
} |
| 1578 |
|
| 1579 |
ASSERT_OK(Flush()); |
| 1580 |
|
| 1581 |
// Flush will trigger the paranoid check and read blocks. But only block cache |
| 1582 |
// will be read. No operations for secondary cache. |
| 1583 |
ASSERT_EQ(secondary_cache->num_inserts(), 0u); |
| 1584 |
ASSERT_EQ(secondary_cache->num_lookups(), 0u); |
| 1585 |
|
| 1586 |
Compact("a", "z"); |
| 1587 |
|
| 1588 |
// Compaction will also insert and evict blocks, no operations to the block |
| 1589 |
// cache. No operations for secondary cache. |
| 1590 |
ASSERT_EQ(secondary_cache->num_inserts(), 0u); |
| 1591 |
ASSERT_EQ(secondary_cache->num_lookups(), 0u); |
| 1592 |
|
| 1593 |
std::string v = Get(Key(0)); |
| 1594 |
ASSERT_EQ(1007, v.size()); |
| 1595 |
|
| 1596 |
// Check the data in first block. Cache miss, direclty read from SST file. |
| 1597 |
ASSERT_EQ(secondary_cache->num_inserts(), 0u); |
| 1598 |
ASSERT_EQ(secondary_cache->num_lookups(), 0u); |
| 1599 |
|
| 1600 |
v = Get(Key(5)); |
| 1601 |
ASSERT_EQ(1007, v.size()); |
| 1602 |
|
| 1603 |
// Check the second block. |
| 1604 |
ASSERT_EQ(secondary_cache->num_inserts(), 0u); |
| 1605 |
ASSERT_EQ(secondary_cache->num_lookups(), 0u); |
| 1606 |
|
| 1607 |
v = Get(Key(5)); |
| 1608 |
ASSERT_EQ(1007, v.size()); |
| 1609 |
|
| 1610 |
// block cache hit |
| 1611 |
ASSERT_EQ(secondary_cache->num_inserts(), 0u); |
| 1612 |
ASSERT_EQ(secondary_cache->num_lookups(), 0u); |
| 1613 |
|
| 1614 |
v = Get(Key(70)); |
| 1615 |
ASSERT_EQ(1007, v.size()); |
| 1616 |
|
| 1617 |
// Check the first block in the second SST file. Cache miss and trigger SST |
| 1618 |
// file read. No operations for secondary cache. |
| 1619 |
ASSERT_EQ(secondary_cache->num_inserts(), 0u); |
| 1620 |
ASSERT_EQ(secondary_cache->num_lookups(), 0u); |
| 1621 |
|
| 1622 |
v = Get(Key(75)); |
| 1623 |
ASSERT_EQ(1007, v.size()); |
| 1624 |
|
| 1625 |
// Check the second block in the second SST file. Cache miss and trigger SST |
| 1626 |
// file read. No operations for secondary cache. |
| 1627 |
ASSERT_EQ(secondary_cache->num_inserts(), 0u); |
| 1628 |
ASSERT_EQ(secondary_cache->num_lookups(), 0u); |
| 1629 |
|
| 1630 |
Destroy(options); |
| 1631 |
} |
| 1632 |
|
| 1633 |
// We disable the secondary cache in DBOptions at first. Close and reopen the DB |
| 1634 |
// with new options, which set the lowest_used_cache_tier to |
| 1635 |
// kNonVolatileBlockTier. So secondary cache will be used. |
| 1636 |
TEST_F(DBSecondaryCacheTest, TestSecondaryCacheOptionChange) { |
| 1637 |
LRUCacheOptions opts(4 * 1024, 0, false, 0.5, nullptr, |
| 1638 |
kDefaultToAdaptiveMutex, kDontChargeCacheMetadata); |
| 1639 |
std::shared_ptr<TestSecondaryCache> secondary_cache( |
| 1640 |
new TestSecondaryCache(2048 * 1024)); |
| 1641 |
opts.secondary_cache = secondary_cache; |
| 1642 |
std::shared_ptr<Cache> cache = NewLRUCache(opts); |
| 1643 |
BlockBasedTableOptions table_options; |
| 1644 |
table_options.block_cache = cache; |
| 1645 |
table_options.block_size = 4 * 1024; |
| 1646 |
Options options = GetDefaultOptions(); |
| 1647 |
options.create_if_missing = true; |
| 1648 |
options.table_factory.reset(NewBlockBasedTableFactory(table_options)); |
| 1649 |
options.env = fault_env_.get(); |
| 1650 |
fault_fs_->SetFailGetUniqueId(true); |
| 1651 |
options.lowest_used_cache_tier = CacheTier::kVolatileTier; |
| 1652 |
|
| 1653 |
// Set the file paranoid check, so after flush, the file will be read |
| 1654 |
// all the blocks will be accessed. |
| 1655 |
options.paranoid_file_checks = true; |
| 1656 |
DestroyAndReopen(options); |
| 1657 |
std::string session_id; |
| 1658 |
ASSERT_OK(db_->GetDbSessionId(session_id)); |
| 1659 |
secondary_cache->SetDbSessionId(session_id); |
| 1660 |
Random rnd(301); |
| 1661 |
const int N = 6; |
| 1662 |
for (int i = 0; i < N; i++) { |
| 1663 |
std::string p_v = rnd.RandomString(1007); |
| 1664 |
ASSERT_OK(Put(Key(i), p_v)); |
| 1665 |
} |
| 1666 |
|
| 1667 |
ASSERT_OK(Flush()); |
| 1668 |
|
| 1669 |
for (int i = 0; i < N; i++) { |
| 1670 |
std::string p_v = rnd.RandomString(1007); |
| 1671 |
ASSERT_OK(Put(Key(i + 70), p_v)); |
| 1672 |
} |
| 1673 |
|
| 1674 |
ASSERT_OK(Flush()); |
| 1675 |
|
| 1676 |
// Flush will trigger the paranoid check and read blocks. But only block cache |
| 1677 |
// will be read. |
| 1678 |
ASSERT_EQ(secondary_cache->num_inserts(), 0u); |
| 1679 |
ASSERT_EQ(secondary_cache->num_lookups(), 0u); |
| 1680 |
|
| 1681 |
Compact("a", "z"); |
| 1682 |
|
| 1683 |
// Compaction will also insert and evict blocks, no operations to the block |
| 1684 |
// cache. |
| 1685 |
ASSERT_EQ(secondary_cache->num_inserts(), 0u); |
| 1686 |
ASSERT_EQ(secondary_cache->num_lookups(), 0u); |
| 1687 |
|
| 1688 |
std::string v = Get(Key(0)); |
| 1689 |
ASSERT_EQ(1007, v.size()); |
| 1690 |
|
| 1691 |
// Check the data in first block. Cache miss, direclty read from SST file. |
| 1692 |
ASSERT_EQ(secondary_cache->num_inserts(), 0u); |
| 1693 |
ASSERT_EQ(secondary_cache->num_lookups(), 0u); |
| 1694 |
|
| 1695 |
v = Get(Key(5)); |
| 1696 |
ASSERT_EQ(1007, v.size()); |
| 1697 |
|
| 1698 |
// Check the second block. |
| 1699 |
ASSERT_EQ(secondary_cache->num_inserts(), 0u); |
| 1700 |
ASSERT_EQ(secondary_cache->num_lookups(), 0u); |
| 1701 |
|
| 1702 |
v = Get(Key(5)); |
| 1703 |
ASSERT_EQ(1007, v.size()); |
| 1704 |
|
| 1705 |
// block cache hit |
| 1706 |
ASSERT_EQ(secondary_cache->num_inserts(), 0u); |
| 1707 |
ASSERT_EQ(secondary_cache->num_lookups(), 0u); |
| 1708 |
|
| 1709 |
// Change the option to enable secondary cache after we Reopen the DB |
| 1710 |
options.lowest_used_cache_tier = CacheTier::kNonVolatileBlockTier; |
| 1711 |
Reopen(options); |
| 1712 |
|
| 1713 |
v = Get(Key(70)); |
| 1714 |
ASSERT_EQ(1007, v.size()); |
| 1715 |
|
| 1716 |
// Enable the secondary cache, trigger lookup of the first block in second SST |
| 1717 |
ASSERT_EQ(secondary_cache->num_inserts(), 0u); |
| 1718 |
ASSERT_EQ(secondary_cache->num_lookups(), 1u); |
| 1719 |
|
| 1720 |
v = Get(Key(75)); |
| 1721 |
ASSERT_EQ(1007, v.size()); |
| 1722 |
|
| 1723 |
// trigger lookup of the second block in second SST |
| 1724 |
ASSERT_EQ(secondary_cache->num_inserts(), 0u); |
| 1725 |
ASSERT_EQ(secondary_cache->num_lookups(), 2u); |
| 1726 |
Destroy(options); |
| 1727 |
} |
| 1728 |
|
| 1729 |
// Two DB test. We create 2 DBs sharing the same block cache and secondary |
| 1730 |
// cache. We diable the secondary cache option for DB2. |
| 1731 |
TEST_F(DBSecondaryCacheTest, TestSecondaryCacheOptionTwoDB) { |
| 1732 |
LRUCacheOptions opts(4 * 1024, 0, false, 0.5, nullptr, |
| 1733 |
kDefaultToAdaptiveMutex, kDontChargeCacheMetadata); |
| 1734 |
std::shared_ptr<TestSecondaryCache> secondary_cache( |
| 1735 |
new TestSecondaryCache(2048 * 1024)); |
| 1736 |
opts.secondary_cache = secondary_cache; |
| 1737 |
std::shared_ptr<Cache> cache = NewLRUCache(opts); |
| 1738 |
BlockBasedTableOptions table_options; |
| 1739 |
table_options.block_cache = cache; |
| 1740 |
table_options.block_size = 4 * 1024; |
| 1741 |
Options options = GetDefaultOptions(); |
| 1742 |
options.create_if_missing = true; |
| 1743 |
options.table_factory.reset(NewBlockBasedTableFactory(table_options)); |
| 1744 |
options.env = fault_env_.get(); |
| 1745 |
options.paranoid_file_checks = true; |
| 1746 |
std::string dbname1 = test::PerThreadDBPath("db_t_1"); |
| 1747 |
ASSERT_OK(DestroyDB(dbname1, options)); |
| 1748 |
DB* db1 = nullptr; |
| 1749 |
ASSERT_OK(DB::Open(options, dbname1, &db1)); |
| 1750 |
std::string dbname2 = test::PerThreadDBPath("db_t_2"); |
| 1751 |
ASSERT_OK(DestroyDB(dbname2, options)); |
| 1752 |
DB* db2 = nullptr; |
| 1753 |
Options options2 = options; |
| 1754 |
options2.lowest_used_cache_tier = CacheTier::kVolatileTier; |
| 1755 |
ASSERT_OK(DB::Open(options2, dbname2, &db2)); |
| 1756 |
fault_fs_->SetFailGetUniqueId(true); |
| 1757 |
|
| 1758 |
// Set the file paranoid check, so after flush, the file will be read |
| 1759 |
// all the blocks will be accessed. |
| 1760 |
std::string session_id; |
| 1761 |
ASSERT_OK(db1->GetDbSessionId(session_id)); |
| 1762 |
secondary_cache->SetDbSessionId(session_id); |
| 1763 |
|
| 1764 |
WriteOptions wo; |
| 1765 |
Random rnd(301); |
| 1766 |
const int N = 6; |
| 1767 |
for (int i = 0; i < N; i++) { |
| 1768 |
std::string p_v = rnd.RandomString(1007); |
| 1769 |
ASSERT_OK(db1->Put(wo, Key(i), p_v)); |
| 1770 |
} |
| 1771 |
|
| 1772 |
ASSERT_EQ(secondary_cache->num_inserts(), 0u); |
| 1773 |
ASSERT_EQ(secondary_cache->num_lookups(), 0u); |
| 1774 |
ASSERT_OK(db1->Flush(FlushOptions())); |
| 1775 |
|
| 1776 |
ASSERT_EQ(secondary_cache->num_inserts(), 0u); |
| 1777 |
ASSERT_EQ(secondary_cache->num_lookups(), 2u); |
| 1778 |
|
| 1779 |
for (int i = 0; i < N; i++) { |
| 1780 |
std::string p_v = rnd.RandomString(1007); |
| 1781 |
ASSERT_OK(db2->Put(wo, Key(i), p_v)); |
| 1782 |
} |
| 1783 |
|
| 1784 |
// No change in the secondary cache, since it is disabled in DB2 |
| 1785 |
ASSERT_EQ(secondary_cache->num_inserts(), 0u); |
| 1786 |
ASSERT_EQ(secondary_cache->num_lookups(), 2u); |
| 1787 |
ASSERT_OK(db2->Flush(FlushOptions())); |
| 1788 |
ASSERT_EQ(secondary_cache->num_inserts(), 1u); |
| 1789 |
ASSERT_EQ(secondary_cache->num_lookups(), 2u); |
| 1790 |
|
| 1791 |
Slice bg("a"); |
| 1792 |
Slice ed("b"); |
| 1793 |
ASSERT_OK(db1->CompactRange(CompactRangeOptions(), &bg, &ed)); |
| 1794 |
ASSERT_OK(db2->CompactRange(CompactRangeOptions(), &bg, &ed)); |
| 1795 |
|
| 1796 |
ASSERT_EQ(secondary_cache->num_inserts(), 1u); |
| 1797 |
ASSERT_EQ(secondary_cache->num_lookups(), 2u); |
| 1798 |
|
| 1799 |
ReadOptions ro; |
| 1800 |
std::string v; |
| 1801 |
ASSERT_OK(db1->Get(ro, Key(0), &v)); |
| 1802 |
ASSERT_EQ(1007, v.size()); |
| 1803 |
|
| 1804 |
// DB 1 has lookup block 1 and it is miss in block cache, trigger secondary |
| 1805 |
// cache lookup |
| 1806 |
ASSERT_EQ(secondary_cache->num_inserts(), 1u); |
| 1807 |
ASSERT_EQ(secondary_cache->num_lookups(), 3u); |
| 1808 |
|
| 1809 |
ASSERT_OK(db1->Get(ro, Key(5), &v)); |
| 1810 |
ASSERT_EQ(1007, v.size()); |
| 1811 |
|
| 1812 |
// DB 1 lookup the second block and it is miss in block cache, trigger |
| 1813 |
// secondary cache lookup |
| 1814 |
ASSERT_EQ(secondary_cache->num_inserts(), 1u); |
| 1815 |
ASSERT_EQ(secondary_cache->num_lookups(), 4u); |
| 1816 |
|
| 1817 |
ASSERT_OK(db2->Get(ro, Key(0), &v)); |
| 1818 |
ASSERT_EQ(1007, v.size()); |
| 1819 |
|
| 1820 |
// For db2, it is not enabled with secondary cache, so no search in the |
| 1821 |
// secondary cache |
| 1822 |
ASSERT_EQ(secondary_cache->num_inserts(), 1u); |
| 1823 |
ASSERT_EQ(secondary_cache->num_lookups(), 4u); |
| 1824 |
|
| 1825 |
ASSERT_OK(db2->Get(ro, Key(5), &v)); |
| 1826 |
ASSERT_EQ(1007, v.size()); |
| 1827 |
|
| 1828 |
// For db2, it is not enabled with secondary cache, so no search in the |
| 1829 |
// secondary cache |
| 1830 |
ASSERT_EQ(secondary_cache->num_inserts(), 1u); |
| 1831 |
ASSERT_EQ(secondary_cache->num_lookups(), 4u); |
| 1832 |
|
| 1833 |
fault_fs_->SetFailGetUniqueId(false); |
| 1834 |
fault_fs_->SetFilesystemActive(true); |
| 1835 |
delete db1; |
| 1836 |
delete db2; |
| 1837 |
ASSERT_OK(DestroyDB(dbname1, options)); |
| 1838 |
ASSERT_OK(DestroyDB(dbname2, options)); |
| 1839 |
} |
| 1840 |
|
| 1841 |
#endif // ROCKSDB_LITE |
| 1842 |
|
| 1843 |
} // namespace ROCKSDB_NAMESPACE |
| 1844 |
|
| 1845 |
int main(int argc, char** argv) { |
| 1846 |
::testing::InitGoogleTest(&argc, argv); |
| 1847 |
return RUN_ALL_TESTS(); |
| 1848 |
} |