Add factory method for creating persistent cache that is accessible from public

Summary:
Currently there is no mechanism to create persistent cache from
headers. Adding a simple factory method to create a simple persistent cache with
default or NVM optimized settings.

note: Any idea to test this factory is appreciated.

Test Plan: None

Reviewers: sdong

Subscribers: andrewkr, dhruba, leveldb

Differential Revision: https://reviews.facebook.net/D64527
This commit is contained in:
krad 2016-09-29 18:02:13 -07:00
parent be1f1092c9
commit e91b4d0cf6
4 changed files with 54 additions and 2 deletions

View File

@ -9,7 +9,9 @@
#include <stdint.h>
#include <memory>
#include <string>
#include "rocksdb/env.h"
#include "rocksdb/slice.h"
#include "rocksdb/statistics.h"
#include "rocksdb/status.h"
@ -46,4 +48,10 @@ class PersistentCache {
virtual bool IsCompressed() = 0;
};
// Factor method to create a new persistent cache
Status NewPersistentCache(Env* const env, const std::string& path,
const uint64_t size,
const std::shared_ptr<Logger>& log,
const bool optimized_for_nvm,
std::shared_ptr<PersistentCache>* cache);
} // namespace rocksdb

View File

@ -359,6 +359,36 @@ bool BlockCacheTier::Reserve(const size_t size) {
return true;
}
Status NewPersistentCache(Env* const env, const std::string& path,
const uint64_t size,
const std::shared_ptr<Logger>& log,
const bool optimized_for_nvm,
std::shared_ptr<PersistentCache>* cache) {
if (!cache) {
return Status::IOError("invalid argument cache");
}
auto opt = PersistentCacheConfig(env, path, size, log);
if (optimized_for_nvm) {
// the default settings are optimized for SSD
// NVM devices are better accessed with 4K direct IO and written with
// parallelism
opt.enable_direct_writes = true;
opt.writer_qdepth = 4;
opt.writer_dispatch_size = 4 * 1024;
}
auto pcache = std::make_shared<BlockCacheTier>(opt);
Status s = pcache->Open();
if (!s.ok()) {
return s;
}
*cache = pcache;
return s;
}
} // namespace rocksdb
#endif // ifndef ROCKSDB_LITE

View File

@ -49,8 +49,8 @@ class BlockCacheTier : public PersistentCacheTier {
}
virtual ~BlockCacheTier() {
// By contract, the user should have called stop before destroying the
// object
// Close is re-entrant so we can call close even if it is already closed
Close();
assert(!insert_th_.joinable());
}

View File

@ -235,6 +235,19 @@ static void UniqueIdCallback(void* arg) {
}
#endif
TEST_F(PersistentCacheTierTest, FactoryTest) {
for (auto nvm_opt : {true, false}) {
ASSERT_FALSE(cache_);
auto log = std::make_shared<ConsoleLogger>();
std::shared_ptr<PersistentCache> cache;
ASSERT_OK(NewPersistentCache(Env::Default(), path_,
/*size=*/1 * 1024 * 1024 * 1024, log, nvm_opt,
&cache));
ASSERT_TRUE(cache);
cache.reset();
}
}
PersistentCacheDBTest::PersistentCacheDBTest() : DBTestBase("/cache_test") {
#ifdef OS_LINUX
rocksdb::SyncPoint::GetInstance()->EnableProcessing();
@ -403,6 +416,7 @@ TEST_F(PersistentCacheDBTest, TieredCacheTest) {
RunTest(std::bind(&MakeTieredCache, dbname_));
}
#endif
} // namespace rocksdb
int main(int argc, char** argv) {