Skip to content

Commit

Permalink
4
Browse files Browse the repository at this point in the history
  • Loading branch information
xinyiZzz committed Feb 20, 2024
1 parent 2cad7fa commit 2b8ff75
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 44 deletions.
31 changes: 14 additions & 17 deletions be/src/olap/lru_cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@ void LRUCache::erase(const CacheKey& key, uint32_t hash) {
}
}

std::tuple<PrunedCount, PrunedSize> LRUCache::prune() {
PrunedInfo LRUCache::prune() {
LRUHandle* to_remove_head = nullptr;
{
std::lock_guard l(_mutex);
Expand Down Expand Up @@ -472,7 +472,7 @@ std::tuple<PrunedCount, PrunedSize> LRUCache::prune() {
return {pruned_count, pruned_size};
}

std::tuple<PrunedCount, PrunedSize> LRUCache::prune_if(CachePrunePredicate pred, bool lazy_mode) {
PrunedInfo LRUCache::prune_if(CachePrunePredicate pred, bool lazy_mode) {
LRUHandle* to_remove_head = nullptr;
{
std::lock_guard l(_mutex);
Expand Down Expand Up @@ -629,27 +629,24 @@ uint64_t ShardedLRUCache::new_id() {
return _last_id.fetch_add(1, std::memory_order_relaxed);
}

std::tuple<PrunedCount, PrunedSize> ShardedLRUCache::prune() {
int64_t pruned_count = 0;
int64_t pruned_size = 0;
PrunedInfo ShardedLRUCache::prune() {
PrunedInfo pruned_info;
for (int s = 0; s < _num_shards; s++) {
auto [count, size] = _shards[s]->prune();
pruned_count += count;
pruned_size += size;
PrunedInfo info = _shards[s]->prune();
pruned_info.pruned_count += info.pruned_count;
pruned_info.pruned_size += info.pruned_size;
}
return {pruned_count, pruned_size};
return pruned_info;
}

std::tuple<PrunedCount, PrunedSize> ShardedLRUCache::prune_if(CachePrunePredicate pred,
bool lazy_mode) {
int64_t pruned_count = 0;
int64_t pruned_size = 0;
PrunedInfo ShardedLRUCache::prune_if(CachePrunePredicate pred, bool lazy_mode) {
PrunedInfo pruned_info;
for (int s = 0; s < _num_shards; s++) {
auto [count, size] = _shards[s]->prune_if(pred, lazy_mode);
pruned_count += count;
pruned_size += size;
PrunedInfo info = _shards[s]->prune_if(pred, lazy_mode);
pruned_info.pruned_count += info.pruned_count;
pruned_info.pruned_size += info.pruned_size;
}
return {pruned_count, pruned_size};
return pruned_info;
}

int64_t ShardedLRUCache::mem_consumption() {
Expand Down
33 changes: 14 additions & 19 deletions be/src/olap/lru_cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,10 @@ using CachePrunePredicate = std::function<bool(const LRUHandle*)>;
// in cache value through the specified function,
// such as last_visit_time in InvertedIndexSearcherCache::CacheValue
using CacheValueTimeExtractor = std::function<int64_t(const void*)>;
using PrunedCount = int64_t;
using PrunedSize = int64_t;
struct PrunedInfo {
int64_t pruned_count = 0;
int64_t pruned_size = 0;
};

class Cache {
public:
Expand Down Expand Up @@ -222,15 +224,12 @@ class Cache {
// encouraged to override the default implementation. A future release of
// leveldb may change prune() to a pure abstract method.
// return num of entries being pruned.
virtual std::tuple<PrunedCount, PrunedSize> prune() { return {0, 0}; }
virtual PrunedInfo prune() { return {0, 0}; }

// Same as prune(), but the entry will only be pruned if the predicate matched.
// NOTICE: the predicate should be simple enough, or the prune_if() function
// may hold lock for a long time to execute predicate.
virtual std::tuple<PrunedCount, PrunedSize> prune_if(CachePrunePredicate pred,
bool lazy_mode = false) {
return {0, 0};
}
virtual PrunedInfo prune_if(CachePrunePredicate pred, bool lazy_mode = false) { return {0, 0}; }

virtual int64_t mem_consumption() = 0;

Expand Down Expand Up @@ -260,10 +259,8 @@ struct LRUHandle {
CachePriority priority = CachePriority::NORMAL;
MemTrackerLimiter* mem_tracker;
LRUCacheType type;
// Save the last visit time of this cache entry.
// Use atomic because it may be modified by multi threads.
int64_t last_visit_time;
char key_data[1]; // Beginning of key
int64_t last_visit_time; // Save the last visit time of this cache entry.
char key_data[1]; // Beginning of key
// Note! key_data must be at the end.

CacheKey key() const {
Expand Down Expand Up @@ -355,8 +352,8 @@ class LRUCache {
Cache::Handle* lookup(const CacheKey& key, uint32_t hash);
void release(Cache::Handle* handle);
void erase(const CacheKey& key, uint32_t hash);
std::tuple<PrunedCount, PrunedSize> prune();
std::tuple<PrunedCount, PrunedSize> prune_if(CachePrunePredicate pred, bool lazy_mode = false);
PrunedInfo prune();
PrunedInfo prune_if(CachePrunePredicate pred, bool lazy_mode = false);

void set_cache_value_time_extractor(CacheValueTimeExtractor cache_value_time_extractor);
void set_cache_value_check_timestamp(bool cache_value_check_timestamp);
Expand Down Expand Up @@ -418,9 +415,8 @@ class ShardedLRUCache : public Cache {
virtual void* value(Handle* handle) override;
Slice value_slice(Handle* handle) override;
virtual uint64_t new_id() override;
std::tuple<PrunedCount, PrunedSize> prune() override;
std::tuple<PrunedCount, PrunedSize> prune_if(CachePrunePredicate pred,
bool lazy_mode = false) override;
PrunedInfo prune() override;
PrunedInfo prune_if(CachePrunePredicate pred, bool lazy_mode = false) override;
int64_t mem_consumption() override;
int64_t get_usage() override;
size_t get_total_capacity() override { return _total_capacity; };
Expand Down Expand Up @@ -490,9 +486,8 @@ class DummyLRUCache : public Cache {
void* value(Handle* handle) override;
Slice value_slice(Handle* handle) override;
uint64_t new_id() override { return 0; };
std::tuple<PrunedCount, PrunedSize> prune() override { return {0, 0}; };
std::tuple<PrunedCount, PrunedSize> prune_if(CachePrunePredicate pred,
bool lazy_mode = false) override {
PrunedInfo prune() override { return {0, 0}; };
PrunedInfo prune_if(CachePrunePredicate pred, bool lazy_mode = false) override {
return {0, 0};
};
int64_t mem_consumption() override { return 0; };
Expand Down
12 changes: 6 additions & 6 deletions be/src/runtime/memory/lru_cache_policy.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,9 @@ class LRUCachePolicy : public CachePolicy {
};

// Prune cache in lazy mode to save cpu and minimize the time holding write lock
auto [pruned_count, pruned_size] = _cache->prune_if(pred, true);
COUNTER_SET(_freed_entrys_counter, pruned_count);
COUNTER_SET(_freed_memory_counter, pruned_size);
PrunedInfo pruned_info = _cache->prune_if(pred, true);
COUNTER_SET(_freed_entrys_counter, pruned_info.pruned_count);
COUNTER_SET(_freed_memory_counter, pruned_info.pruned_size);
COUNTER_UPDATE(_prune_stale_number_counter, 1);
LOG(INFO) << fmt::format("{} prune stale {} entries, {} bytes, {} times prune",
type_string(_type), _freed_entrys_counter->value(),
Expand All @@ -112,9 +112,9 @@ class LRUCachePolicy : public CachePolicy {
_cache->mem_consumption() > CACHE_MIN_FREE_SIZE) {
COUNTER_SET(_cost_timer, (int64_t)0);
SCOPED_TIMER(_cost_timer);
auto [pruned_count, pruned_size] = _cache->prune();
COUNTER_SET(_freed_entrys_counter, pruned_count);
COUNTER_SET(_freed_memory_counter, pruned_size);
PrunedInfo pruned_info = _cache->prune();
COUNTER_SET(_freed_entrys_counter, pruned_info.pruned_count);
COUNTER_SET(_freed_memory_counter, pruned_info.pruned_size);
COUNTER_UPDATE(_prune_all_number_counter, 1);
LOG(INFO) << fmt::format(
"{} prune all {} entries, {} bytes, {} times prune, is clear: {}",
Expand Down
4 changes: 2 additions & 2 deletions be/test/olap/lru_cache_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -391,8 +391,8 @@ TEST_F(CacheTest, PruneIfLazyMode) {
auto pred3 = [](const LRUHandle* handle) -> bool {
return DecodeValue((void*)(handle->value)) <= 600;
};
auto [pruned_count, pruned_size] = cache.prune_if(pred3, true);
EXPECT_EQ(3, pruned_count);
PrunedInfo pruned_info = cache.prune_if(pred3, true);
EXPECT_EQ(3, pruned_info.pruned_count);
EXPECT_EQ(4, cache.get_usage());
}

Expand Down

0 comments on commit 2b8ff75

Please sign in to comment.