Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Cache a customizable class #13024

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 15 additions & 11 deletions cache/cache.cc
Original file line number Diff line number Diff line change
Expand Up @@ -133,19 +133,23 @@ Status Cache::CreateFromString(const ConfigOptions& config_options,
std::shared_ptr<Cache>* result) {
Status status;
std::shared_ptr<Cache> cache;
if (value.find('=') == std::string::npos) {
cache = NewLRUCache(ParseSizeT(value));
} else {
LRUCacheOptions cache_opts;
status = OptionTypeInfo::ParseStruct(config_options, "",
&lru_cache_options_type_info, "",
value, &cache_opts);
if (value.find("://") == std::string::npos) {
if (value.find('=') == std::string::npos) {
cache = NewLRUCache(ParseSizeT(value));
} else {
LRUCacheOptions cache_opts;
status = OptionTypeInfo::ParseStruct(config_options, "",
&lru_cache_options_type_info, "",
value, &cache_opts);
if (status.ok()) {
cache = NewLRUCache(cache_opts);
}
}
if (status.ok()) {
cache = NewLRUCache(cache_opts);
result->swap(cache);
}
}
if (status.ok()) {
result->swap(cache);
} else {
status = LoadSharedObject<Cache>(config_options, value, result);
}
return status;
}
Expand Down
4 changes: 3 additions & 1 deletion include/rocksdb/advanced_cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class Statistics;
//
// INTERNAL: See typed_cache.h for convenient wrappers on top of this API.
// New virtual functions must also be added to CacheWrapper below.
class Cache {
class Cache : public Customizable {
public: // types hidden from API client
// Opaque handle to an entry stored in the cache.
struct Handle {};
Expand Down Expand Up @@ -190,6 +190,8 @@ class Cache {
// Destroys all remaining entries by calling the associated "deleter"
virtual ~Cache() {}

static const char* Type() { return "Cache"; }

// Creates a new Cache based on the input value string and returns the result.
// Currently, this method can be used to create LRUCaches only
// @param config_options
Expand Down
31 changes: 31 additions & 0 deletions options/customizable_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1396,6 +1396,19 @@ class MockFilterPolicy : public FilterPolicy {
}
};

class MockCache : public CacheWrapper {
public:
static const char* kClassName() { return "MockCache"; }
const char* Name() const override { return kClassName(); }

MockCache()
: CacheWrapper(NewLRUCache(LRUCacheOptions(100, 0, false, 0.0))) {}

bool IsInstanceOf(const std::string& name) const override {
return name.find(Name()) == 0;
}
};

static int RegisterLocalObjects(ObjectLibrary& library,
const std::string& /*arg*/) {
size_t num_types;
Expand Down Expand Up @@ -1519,6 +1532,15 @@ static int RegisterLocalObjects(ObjectLibrary& library,
return guard->get();
});

library.AddFactory<Cache>(
ObjectLibrary::PatternEntry(MockCache::kClassName())
.AddSeparator("://", /*at_least_one=*/false),
[](const std::string& /*uri*/, std::unique_ptr<Cache>* guard,
std::string* /* errmsg */) {
guard->reset(new MockCache());
return guard->get();
});

return static_cast<int>(library.GetFactoryCount(&num_types));
}
} // namespace
Expand Down Expand Up @@ -2111,6 +2133,15 @@ TEST_F(LoadCustomizableTest, LoadFlushBlockPolicyFactoryTest) {
}
}

TEST_F(LoadCustomizableTest, LoadCacheTest) {
if (RegisterTests("Test")) {
std::string uri(MockCache::kClassName());
uri.append("://");
auto cache = ExpectCreateShared<Cache>(uri);
ASSERT_TRUE(cache->IsInstanceOf(MockCache::kClassName()));
}
}

} // namespace ROCKSDB_NAMESPACE
int main(int argc, char** argv) {
::testing::InitGoogleTest(&argc, argv);
Expand Down
11 changes: 10 additions & 1 deletion tools/db_bench_tool.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1753,6 +1753,7 @@ DEFINE_bool(read_with_latest_user_timestamp, true,
"If true, always use the current latest timestamp for read. If "
"false, choose a random timestamp from the past.");

DEFINE_string(cache_uri, "", "Full URI for creating a custom cache object");
DEFINE_string(secondary_cache_uri, "",
"Full URI for creating a custom secondary cache object");
static class std::shared_ptr<ROCKSDB_NAMESPACE::SecondaryCache> secondary_cache;
Expand Down Expand Up @@ -3138,7 +3139,15 @@ class Benchmark {
}

std::shared_ptr<Cache> block_cache;
if (FLAGS_cache_type == "clock_cache") {
if (!FLAGS_cache_uri.empty()) {
Status s = Cache::CreateFromString(ConfigOptions(), FLAGS_cache_uri,
&block_cache);
if (block_cache == nullptr) {
fprintf(stderr, "No cache registered matching string: %s status=%s\n",
FLAGS_cache_uri.c_str(), s.ToString().c_str());
exit(1);
}
} else if (FLAGS_cache_type == "clock_cache") {
fprintf(stderr, "Old clock cache implementation has been removed.\n");
exit(1);
} else if (EndsWith(FLAGS_cache_type, "hyper_clock_cache")) {
Expand Down
1 change: 1 addition & 0 deletions unreleased_history/new_features/customizable_cache.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Make Cache a customizable class that can be instantiated by the object registry.
Loading