ClickHouse/src/Interpreters/Cache/LRUFileCachePriority.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

110 lines
3.4 KiB
C++
Raw Normal View History

2022-08-26 17:23:46 +00:00
#include <Interpreters/Cache/LRUFileCachePriority.h>
2022-08-16 09:32:00 +00:00
#include <Common/CurrentMetrics.h>
namespace CurrentMetrics
{
extern const Metric FilesystemCacheSize;
extern const Metric FilesystemCacheElements;
}
2022-06-29 13:28:19 +00:00
namespace DB
{
namespace ErrorCodes
{
extern const int LOGICAL_ERROR;
}
2022-06-29 14:45:34 +00:00
IFileCachePriority::WriteIterator LRUFileCachePriority::add(const Key & key, size_t offset, size_t size, std::lock_guard<std::mutex> &)
2022-06-29 13:28:19 +00:00
{
#ifndef NDEBUG
for (const auto & entry : queue)
{
if (entry.key == key && entry.offset == offset)
throw Exception(
ErrorCodes::LOGICAL_ERROR,
"Attempt to add duplicate queue entry to queue. (Key: {}, offset: {}, size: {})",
2022-08-24 10:40:44 +00:00
entry.key.toString(), entry.offset, entry.size);
2022-06-29 13:28:19 +00:00
}
#endif
2022-08-16 09:32:00 +00:00
2022-06-29 13:28:19 +00:00
auto iter = queue.insert(queue.end(), FileCacheRecord(key, offset, size));
cache_size += size;
2022-08-16 09:32:00 +00:00
CurrentMetrics::add(CurrentMetrics::FilesystemCacheSize, size);
CurrentMetrics::add(CurrentMetrics::FilesystemCacheElements);
2022-09-23 13:32:05 +00:00
LOG_TRACE(log, "Added entry into LRU queue, key: {}, offset: {}", key.toString(), offset);
2022-08-24 10:40:44 +00:00
2022-06-29 13:28:19 +00:00
return std::make_shared<LRUFileCacheIterator>(this, iter);
}
2022-06-29 14:45:34 +00:00
bool LRUFileCachePriority::contains(const Key & key, size_t offset, std::lock_guard<std::mutex> &)
2022-06-29 13:28:19 +00:00
{
for (const auto & record : queue)
{
if (key == record.key && offset == record.offset)
return true;
}
return false;
}
2022-06-29 14:45:34 +00:00
void LRUFileCachePriority::removeAll(std::lock_guard<std::mutex> &)
2022-06-29 13:28:19 +00:00
{
2022-08-16 09:32:00 +00:00
CurrentMetrics::sub(CurrentMetrics::FilesystemCacheSize, cache_size);
CurrentMetrics::sub(CurrentMetrics::FilesystemCacheElements, queue.size());
2022-09-23 13:32:05 +00:00
LOG_TRACE(log, "Removed all entries from LRU queue");
2022-08-24 10:40:44 +00:00
2022-06-29 13:28:19 +00:00
queue.clear();
cache_size = 0;
}
2022-08-16 09:32:00 +00:00
LRUFileCachePriority::LRUFileCacheIterator::LRUFileCacheIterator(
LRUFileCachePriority * cache_priority_, LRUFileCachePriority::LRUQueueIterator queue_iter_)
: cache_priority(cache_priority_), queue_iter(queue_iter_)
{
}
2022-06-29 14:45:34 +00:00
IFileCachePriority::ReadIterator LRUFileCachePriority::getLowestPriorityReadIterator(std::lock_guard<std::mutex> &)
2022-06-29 13:28:19 +00:00
{
2022-08-10 15:19:26 +00:00
return std::make_unique<const LRUFileCacheIterator>(this, queue.begin());
2022-06-29 13:28:19 +00:00
}
2022-06-29 14:45:34 +00:00
IFileCachePriority::WriteIterator LRUFileCachePriority::getLowestPriorityWriteIterator(std::lock_guard<std::mutex> &)
2022-06-29 13:28:19 +00:00
{
return std::make_shared<LRUFileCacheIterator>(this, queue.begin());
}
2022-06-29 14:45:34 +00:00
size_t LRUFileCachePriority::getElementsNum(std::lock_guard<std::mutex> &) const
2022-06-29 13:28:19 +00:00
{
return queue.size();
}
2022-08-16 09:32:00 +00:00
void LRUFileCachePriority::LRUFileCacheIterator::removeAndGetNext(std::lock_guard<std::mutex> &)
{
cache_priority->cache_size -= queue_iter->size;
CurrentMetrics::sub(CurrentMetrics::FilesystemCacheSize, queue_iter->size);
CurrentMetrics::sub(CurrentMetrics::FilesystemCacheElements);
2022-09-23 13:32:05 +00:00
LOG_TRACE(cache_priority->log, "Removed entry from LRU queue, key: {}, offset: {}", queue_iter->key.toString(), queue_iter->offset);
2022-08-24 10:40:44 +00:00
2022-08-16 09:32:00 +00:00
queue_iter = cache_priority->queue.erase(queue_iter);
}
void LRUFileCachePriority::LRUFileCacheIterator::incrementSize(size_t size_increment, std::lock_guard<std::mutex> &)
{
cache_priority->cache_size += size_increment;
CurrentMetrics::add(CurrentMetrics::FilesystemCacheSize, size_increment);
queue_iter->size += size_increment;
}
void LRUFileCachePriority::LRUFileCacheIterator::use(std::lock_guard<std::mutex> &)
{
queue_iter->hits++;
cache_priority->queue.splice(cache_priority->queue.end(), cache_priority->queue, queue_iter);
}
2022-06-29 13:28:19 +00:00
};