From 50d62a34b390837213e16c9825f7d1c16460e335 Mon Sep 17 00:00:00 2001 From: Wenxuan Date: Mon, 5 Aug 2024 15:11:38 +0800 Subject: [PATCH] storage: Evict high priority files (#257) Signed-off-by: Wish --- dbms/src/Storages/S3/FileCache.cpp | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/dbms/src/Storages/S3/FileCache.cpp b/dbms/src/Storages/S3/FileCache.cpp index dd0ae384a99..913d257ee78 100644 --- a/dbms/src/Storages/S3/FileCache.cpp +++ b/dbms/src/Storages/S3/FileCache.cpp @@ -390,15 +390,19 @@ bool FileCache::reserveSpaceImpl(FileType reserve_for, UInt64 size, EvictMode ev // Distinguish cache priority according to file type. The larger the file type, the lower the priority. // First, try to evict files which not be used recently with the same type. => Try to evict old files. // Second, try to evict files with lower priority. => Try to evict lower priority files. +// Finally, evict files with higher priority, if space is still not sufficient. Higher priority files +// are usually smaller. If we don't evict them, it is very possible that cache is full of these higher +// priority small files and we can't effectively cache any lower-priority large files. std::vector FileCache::getEvictFileTypes(FileType evict_for) { std::vector evict_types; evict_types.push_back(evict_for); // First, try evict with the same file type. constexpr auto all_file_types = magic_enum::enum_values(); // all_file_types are sorted by enum value. // Second, try evict from the lower proirity file type. - for (auto itr = std::rbegin(all_file_types); itr != std::rend(all_file_types) && *itr > evict_for; ++itr) + for (auto itr = std::rbegin(all_file_types); itr != std::rend(all_file_types); ++itr) { - evict_types.push_back(*itr); + if (*itr != evict_for) + evict_types.push_back(*itr); } return evict_types; }