Skip to content
Prev Previous commit
Next Next commit
Next pass at CacheLib as a SecondaryCache
  • Loading branch information
mrambacher committed Dec 5, 2022
commit ec1186f8f5e27e0b6608b586e3ecae9f30d65098
3 changes: 2 additions & 1 deletion cachelib/adaptor/rocks_secondary_cache/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ find_package(cachelib REQUIRED)
set(cachelib_SOURCES CachelibWrapper.cpp PARENT_SCOPE)
set(cachelib_INCLUDE_PATHS ${CACHELIB_INCLUDE_DIR}/.. PARENT_SCOPE)
set(cachelib_COMPILE_FLAGS -Wno-error=class-memaccess -Wno-error=sign-compare -Wno-error=deprecated-declarationsPARENT_SCOPE)
set(cachelib_LIBS "cachelib_allocator" "cachelib_shm" "cachelib_navy" "cachelib_common" "thriftcpp2" "thriftfrozen2" "thriftmetadata" "thriftanyrep" "thrifttype" "thrifttyperep" "thriftannotation" "thriftprotocol" "async" "wangle" "fizz" "sodium" "rt" PARENT_SCOPE)
#set(cachelib_LIBS "cachelib_allocator" "cachelib_shm" "cachelib_navy" "cachelib_common" "thriftcpp2" "thriftfrozen2" "thriftmetadata" "thriftanyrep" "thrifttype" "thrifttyperep" "thriftannotation" "thriftprotocol" "async" "wangle" "fizz" "sodium" "rt" PARENT_SCOPE)
set(cachelib_LIBS "cachelib_allocator" "cachelib_shm" "cachelib_navy" "cachelib_common" "thriftcpp2" "thriftfrozen2" "thriftmetadata" "thriftanyrep" "thrifttype" "thrifttyperep" "thriftannotation" "thriftprotocol" "async" "wangle" "fizz" "folly" "folly_test_util" "fmtd" "glogd" "sodium" "boost_context" "rt" PARENT_SCOPE)
set(cachelib_FUNC register_CachelibObjects PARENT_SCOPE)
set(cachelib_LINK_PATHS ${CMAKE_PREFIX_PATH}/lib PARENT_SCOPE)
set(cachelib_TESTS tests/CachelibWrapperTest.cpp PARENT_SCOPE)
3 changes: 2 additions & 1 deletion cachelib/adaptor/rocks_secondary_cache/CachelibWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,8 @@ rocks_cachelib_type_info = {
} // namespace

RocksCachelibWrapper::RocksCachelibWrapper(const RocksCachelibOptions& options)
: options_(options) {
: options_(options), cache_(nullptr) {
RegisterOptions(options_, rocks_cachelib_type_info);
}

ROCKSDB_NAMESPACE::Status RocksCachelibWrapper::PrepareOptions(const ROCKSDB_NAMESPACE::ConfigOptions& opts) {
Expand Down
2 changes: 2 additions & 0 deletions cachelib/adaptor/rocks_secondary_cache/CachelibWrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ namespace facebook {
namespace rocks_secondary_cache {
// Options structure for configuring a Cachelib SecondaryCache instance
struct RocksCachelibOptions {
static const char *kName() { return "RocksCachelibOptions"; }

// A name for the use case
std::string cacheName;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

#include "plugin/cachelib/CachelibWrapper.h"
#include "cachelib/common/Utils.h"
#include "test_util/testharness.h"

#include <folly/Random.h>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
Expand Down Expand Up @@ -60,7 +62,7 @@ class CachelibWrapperTest : public ::testing::Test {
CachelibWrapperTest() : fail_create_(false) {
RocksCachelibOptions opts;

path_ = util::getUniqueTempDir("CachelibWrapperTest");
path_ = ROCKSDB_NAMESPACE::test::TmpDir();
opts.volatileSize = kVolatileSize;
opts.cacheName = "CachelibWrapperTest";
opts.fileName = path_ + "/cachelib_wrapper_test_file";
Expand Down Expand Up @@ -162,7 +164,7 @@ TEST_F(CachelibWrapperTest, BasicTest) {
ASSERT_EQ(cache()->Insert("k2", &item2, &CachelibWrapperTest::helper_),
Status::OK());

std::unique_ptr<rocksdb::SecondaryCacheResultHandle> handle;
std::unique_ptr<ROCKSDB_NAMESPACE::SecondaryCacheResultHandle> handle;
bool is_in_sec_cache{false};
handle = cache()->Lookup("k2", test_item_creator, true
#if ROCKSDB_MAJOR > 7 || (ROCKSDB_MAJOR == 7 && ROCKSDB_MINOR >= 7)
Expand Down Expand Up @@ -192,7 +194,7 @@ TEST_F(CachelibWrapperTest, BasicTest) {
}

TEST_F(CachelibWrapperTest, BasicFailTest) {
std::unique_ptr<rocksdb::SecondaryCacheResultHandle> handle;
std::unique_ptr<ROCKSDB_NAMESPACE::SecondaryCacheResultHandle> handle;
bool is_in_sec_cache{false};
handle = cache()->Lookup("k1", test_item_creator, true
#if ROCKSDB_MAJOR > 7 || (ROCKSDB_MAJOR == 7 && ROCKSDB_MINOR >= 7)
Expand Down Expand Up @@ -548,7 +550,7 @@ TEST_F(CachelibWrapperTest, LargeItemTest) {
ASSERT_EQ(cache()->Insert("k1", &item1, &CachelibWrapperTest::helper_),
Status::InvalidArgument());

std::unique_ptr<rocksdb::SecondaryCacheResultHandle> handle;
std::unique_ptr<ROCKSDB_NAMESPACE::SecondaryCacheResultHandle> handle;
bool is_in_sec_cache{false};
handle = cache()->Lookup("k1", test_item_creator, true,
#if ROCKSDB_MAJOR > 7 || (ROCKSDB_MAJOR == 7 && ROCKSDB_MINOR >= 7)
Expand All @@ -559,5 +561,49 @@ TEST_F(CachelibWrapperTest, LargeItemTest) {
handle.reset();
}

#ifndef ROCKSDB_LITE
TEST_F(CachelibWrapperTest, CreateFromString) {
ROCKSDB::NAMESPACE::ConfigOptions opts;
opts.invoke_prepare_options = true;
std::shared_ptr<ROCKSDB_NAMESPACE::SecondaryCache> scache;
std::string props = "cachename=foo;"
"filename=bar;"
"size=1000000;"
"block_size=8192;"
"region_size=4096;"
"policy=unknown;"
"probablity=0.50;"
"max_write_rate=1024;"
"admission_write_rate=2048;"
"volatile_size=2000000;"
"bucket_power=48;"
"lock_power=24;";

ASSERT_EQ(ROCKSDB_NAMESPACE::SecondaryCache::CreateFromString(opts,
props + "id=" + RocksCachelibWrapper::kClassName() +
&scache),
ROCKSDB_NAMESPACE::Status::OK());
auto rco = scache->GetOptions<RocksCachelibOptions>();
ASSERT_NE(rco, nullptr);
ASSERT_STREQ(rco->cachename, "foo");
ASSERT_STREQ(rco->filename, "bar");
ASSERT_EQ(rco->size, 1000000);
ASSERT_EQ(rco->block_size, 8192);
ASSERT_EQ(rco->region_size, 4096);
ASSERT_EQ(rco->probability, 0.5);
ASSERT_EQ(rco->admPolicy, "unknown");
ASSERT_EQ(rco->max_write_rate, 1024);
ASSERT_EQ(rco->admission_write_rate, 2048);
ASSERT_EQ(rco->volatile_size, 2000000);
ASSERT_EQ(rco->bucket_power, 48);
ASSERT_EQ(rco->lock_power, 24);

}
#endif // ROCKSDB_LITE
} // namespace rocks_secondary_cache
} // namespace facebook

int main(int argc, char** argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}