Files
doris/be/src/util/obj_lru_cache.h
Xinyi Zou 4b30485d62 [improvement](memory) Refactor doris cache GC (#21522)
Abstract CachePolicy, which controls the gc of all caches.
Add stale sweep to all lru caches, including page caches, etc.
I0710 18:32:35.729460 2945318 mem_info.cpp:172] End Full GC Free, Memory 3866389992 Bytes. cost(us): 112165339, details: FullGC:
  FreeTopMemoryQuery:
     - CancelCostTime: 1m51s
     - CancelTasksNum: 1
     - FindCostTime: 0.000ns
     - FreedMemory: 2.93 GB
  WorkloadGroup:
  Cache name=DataPageCache:
     - CostTime: 15.283ms
     - FreedEntrys: 9.56K
     - FreedMemory: 691.97 MB
     - PruneAllNumber: 1
     - PruneStaleNumber: 1
2023-07-11 20:21:31 +08:00

105 lines
3.3 KiB
C++

// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
#pragma once
#include "olap/lru_cache.h"
namespace doris {
// A common object cache depends on an Sharded LRU Cache.
// It has a certain capacity, which determin how many objects it can cache.
// Caller must hold a CacheHandle instance when visiting the cached object.
// TODO shouble add gc prune
class ObjLRUCache {
public:
struct ObjKey {
ObjKey(const std::string& key_) : key(key_) {}
std::string key;
};
class CacheHandle {
public:
CacheHandle() = default;
CacheHandle(Cache* cache, Cache::Handle* handle) : _cache(cache), _handle(handle) {}
~CacheHandle() {
if (_handle != nullptr) {
_cache->release(_handle);
}
}
CacheHandle(CacheHandle&& other) noexcept {
std::swap(_cache, other._cache);
std::swap(_handle, other._handle);
}
CacheHandle& operator=(CacheHandle&& other) noexcept {
std::swap(_cache, other._cache);
std::swap(_handle, other._handle);
return *this;
}
bool valid() { return _cache != nullptr && _handle != nullptr; }
Cache* cache() const { return _cache; }
void* data() const { return _cache->value(_handle); }
private:
Cache* _cache = nullptr;
Cache::Handle* _handle = nullptr;
// Don't allow copy and assign
DISALLOW_COPY_AND_ASSIGN(CacheHandle);
};
ObjLRUCache(int64_t capacity, uint32_t num_shards = kDefaultNumShards);
bool lookup(const ObjKey& key, CacheHandle* handle);
template <typename T>
void insert(const ObjKey& key, const T* value, CacheHandle* cache_handle) {
auto deleter = [](const doris::CacheKey& key, void* value) {
T* v = (T*)value;
delete v;
};
insert(key, value, cache_handle, deleter);
}
template <typename T>
void insert(const ObjKey& key, const T* value, CacheHandle* cache_handle,
void (*deleter)(const CacheKey& key, void* value)) {
if (_enabled) {
const std::string& encoded_key = key.key;
auto handle = _cache->insert(encoded_key, (void*)value, sizeof(T), deleter,
CachePriority::NORMAL, 1);
*cache_handle = CacheHandle {_cache.get(), handle};
} else {
cache_handle = nullptr;
}
}
void erase(const ObjKey& key);
private:
static constexpr uint32_t kDefaultNumShards = 16;
std::unique_ptr<Cache> _cache = nullptr;
bool _enabled;
};
} // namespace doris