-
Notifications
You must be signed in to change notification settings - Fork 165
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
add prioritized cache wrapper #142
Conversation
25fc0ff
to
c4ad7f0
Compare
@yiwu-arbug sorry for a little late, I have update this PR to add a wrapper called PrioritizedCache, has two function |
56fa43f
to
3798713
Compare
Codecov Report
@@ Coverage Diff @@
## master #142 +/- ##
==========================================
+ Coverage 85.49% 85.52% +0.02%
==========================================
Files 45 45
Lines 3419 3419
==========================================
+ Hits 2923 2924 +1
+ Misses 496 495 -1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for late review.
src/prioritized_cache.h
Outdated
@@ -0,0 +1,41 @@ | |||
#pragma once |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please move this header to include/titan/
src/prioritized_cache.h
Outdated
@@ -0,0 +1,41 @@ | |||
#pragma once | |||
#include "rocksdb/cache.h" | |||
#include "rocksdb/env.h" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: env.h is not needed.
src/prioritized_cache.h
Outdated
namespace rocksdb { | ||
namespace titandb { | ||
|
||
class PrioritizedCache { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry I didn't explain clearly again. What I want is something like this. Do you mind update the code? Thanks.
class PrioritizedCache: public Cache {
public:
PrioritizedCache(std::shared_ptr<Cache> cache, Cache::Priority priority)
: cache_(cache), priority_(priority) {}
Status Insert(..., Cache::Priority /*priority*/) override {
return Insert(..., priority_);
}
private:
std::shared_ptr<Cache> cache_;
Cache::Priority priority_;
};
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With it we can use the same underlying LRUCache for both rocksdb block_cache and titan blob_cache, but forcing block_cache to always insert with high-pri, and titan always insert with low-pri. This is because the data in rocksdb is the index for titan values, so they are more valuable and we want them to in cache more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, I got it, I will update the code soon.
0825993
to
f420771
Compare
@yiwu-arbug I have updated the code, please review again |
0eba4d4
to
62093a8
Compare
include/titan/prioritized_cache.h
Outdated
namespace rocksdb { | ||
namespace titandb { | ||
|
||
class PrioritizedCache : Cache { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/Cache
/public Cache
/
and implement all other methods in Cache
interface, by passing arguments directly to underlying cache_
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok, I have updated again now, please review again @yiwu-arbug
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you need to implement all methods (Lookup
, Release
, Erase
, etc) in the Cache
interface.
62093a8
to
52ad7c3
Compare
Mind adding some very simple unit test? Like have a mock underlying Cache object that checks what's the priority pass to it on Insert. |
Signed-off-by: haoxiang47 <[email protected]>
52ad7c3
to
199513e
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great! The test is also great, but I leave some comments there.
|
||
// here we set insert Priority to high, to check if it will | ||
// insert to low in fact | ||
auto lo_ok = low_cache.Insert(low_cache_key, (void*)&cacahe_value, cache->GetCapacity(), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can just set charge=1 instead of cache->GetCapacity()
. Same below.
auto v = reinterpret_cast<Slice*>(cache->Value(low_handle)); | ||
ASSERT_EQ(cacahe_value.data(), v->data()); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should verify the low pri item getting evicted after an high pri item being inserted.
Slice low_cache_key = "low_test_key"; | ||
|
||
LRUCacheOptions options; | ||
options.capacity = high_cache_key.size() + cacahe_value.size(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let set capacity=1 for this test. See the other comments.
ASSERT_TRUE(hi_ok.ok()); | ||
|
||
auto high_handle = cache->Lookup(high_cache_key); | ||
if (high_handle) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Assert high_handle != nullptr.
ASSERT_EQ(cacahe_value.data(), v->data()); | ||
} | ||
|
||
auto low_handle = cache->Lookup(low_cache_key); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
assert low_handle == nulllptr.
&DeleteCacheValue<Slice>, &cache_handle, Cache::Priority::LOW); | ||
ASSERT_TRUE(hi_ok.ok()); | ||
|
||
auto high_handle = cache->Lookup(high_cache_key); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lookup through high_cache
instead of cache
.
ASSERT_EQ(cacahe_value.data(), v->data()); | ||
} | ||
|
||
auto low_handle = cache->Lookup(low_cache_key); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lookup through low_cache
instead of cache
.
auto high_handle = cache->Lookup(high_cache_key); | ||
if (high_handle) { | ||
auto v = reinterpret_cast<Slice*>(cache->Value(high_handle)); | ||
ASSERT_EQ(cacahe_value.data(), v->data()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should release the handle here, otherwise there's memory leak: high_cache->Release(high_handle)
. This should fix CI.
@@ -0,0 +1,117 @@ | |||
#pragma once |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add the header to new files:
// Copyright 2020 TiKV Project Authors. Licensed under Apache-2.0.
As blob-specific cache priority is introduced, close it tikv/rocksdb#354 |
add a prioritized cache wrapper, for this issue: #127