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

add prioritized cache wrapper #142

Closed
wants to merge 1 commit into from

Conversation

haoxiang47
Copy link

add a prioritized cache wrapper, for this issue: #127

@haoxiang47 haoxiang47 force-pushed the add-prioritized-cache branch 2 times, most recently from 25fc0ff to c4ad7f0 Compare February 21, 2020 15:02
@haoxiang47
Copy link
Author

@yiwu-arbug sorry for a little late, I have update this PR to add a wrapper called PrioritizedCache, has two function HighPriCacheInsert and LowPriCacheInsert which can insert high and low cache priorized regardless of user provided option, and a interface GetCache to return the Cache ptr

@haoxiang47 haoxiang47 force-pushed the add-prioritized-cache branch 2 times, most recently from 56fa43f to 3798713 Compare February 23, 2020 09:28
@codecov-io
Copy link

codecov-io commented Feb 23, 2020

Codecov Report

Merging #142 into master will increase coverage by 0.02%.
The diff coverage is n/a.

@@            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

Copy link
Collaborator

@yiwu-arbug yiwu-arbug left a 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.

@@ -0,0 +1,41 @@
#pragma once
Copy link
Collaborator

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/

@@ -0,0 +1,41 @@
#pragma once
#include "rocksdb/cache.h"
#include "rocksdb/env.h"
Copy link
Collaborator

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.

namespace rocksdb {
namespace titandb {

class PrioritizedCache {
Copy link
Collaborator

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_;
};

Copy link
Collaborator

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.

Copy link
Author

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.

@haoxiang47 haoxiang47 force-pushed the add-prioritized-cache branch 2 times, most recently from 0825993 to f420771 Compare February 29, 2020 09:34
@haoxiang47
Copy link
Author

@yiwu-arbug I have updated the code, please review again

@haoxiang47 haoxiang47 force-pushed the add-prioritized-cache branch 2 times, most recently from 0eba4d4 to 62093a8 Compare February 29, 2020 15:45
namespace rocksdb {
namespace titandb {

class PrioritizedCache : Cache {
Copy link
Collaborator

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_.

Copy link
Author

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

Copy link
Collaborator

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.

src/prioritized_cache.cc Outdated Show resolved Hide resolved
@haoxiang47 haoxiang47 force-pushed the add-prioritized-cache branch from 62093a8 to 52ad7c3 Compare March 2, 2020 15:29
@haoxiang47 haoxiang47 requested a review from yiwu-arbug March 2, 2020 16:21
@yiwu-arbug
Copy link
Collaborator

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.

@haoxiang47 haoxiang47 force-pushed the add-prioritized-cache branch from 52ad7c3 to 199513e Compare March 24, 2020 15:22
Copy link
Collaborator

@yiwu-arbug yiwu-arbug left a 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(),
Copy link
Collaborator

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());
}

Copy link
Collaborator

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();
Copy link
Collaborator

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) {
Copy link
Collaborator

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);
Copy link
Collaborator

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);
Copy link
Collaborator

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);
Copy link
Collaborator

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());
Copy link
Collaborator

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
Copy link
Collaborator

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.

@Connor1996
Copy link
Member

As blob-specific cache priority is introduced, close it tikv/rocksdb#354

@Connor1996 Connor1996 closed this Jan 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants