From bbf69511ee383870f00f8c1c755714c93a97c562 Mon Sep 17 00:00:00 2001 From: yangsiyu Date: Mon, 22 Dec 2025 20:10:08 +0800 Subject: [PATCH] [fix](cache) always create data and index page cache to avoid null pointer --- be/src/olap/page_cache.cpp | 14 +-- be/src/runtime/memory/lru_cache_policy.h | 8 +- be/test/olap/page_cache_test.cpp | 104 +++++++++++++++++++++++ 3 files changed, 116 insertions(+), 10 deletions(-) diff --git a/be/src/olap/page_cache.cpp b/be/src/olap/page_cache.cpp index 7e1129ba4de6bc..24e3b844dbbc13 100644 --- a/be/src/olap/page_cache.cpp +++ b/be/src/olap/page_cache.cpp @@ -82,19 +82,21 @@ StoragePageCache* StoragePageCache::create_global_cache(size_t capacity, StoragePageCache::StoragePageCache(size_t capacity, int32_t index_cache_percentage, int64_t pk_index_cache_capacity, uint32_t num_shards) : _index_cache_percentage(index_cache_percentage) { + size_t data_page_capacity = 0; + size_t index_page_capacity = 0; if (index_cache_percentage == 0) { - _data_page_cache = std::make_unique(capacity, num_shards); + data_page_capacity = capacity; } else if (index_cache_percentage == 100) { - _index_page_cache = std::make_unique(capacity, num_shards); + index_page_capacity = capacity; } else if (index_cache_percentage > 0 && index_cache_percentage < 100) { - _data_page_cache = std::make_unique( - capacity * (100 - index_cache_percentage) / 100, num_shards); - _index_page_cache = std::make_unique( - capacity * index_cache_percentage / 100, num_shards); + data_page_capacity = capacity * (100 - index_cache_percentage) / 100; + index_page_capacity = capacity * index_cache_percentage / 100; } else { CHECK(false) << "invalid index page cache percentage"; } + _data_page_cache = std::make_unique(data_page_capacity, num_shards); + _index_page_cache = std::make_unique(index_page_capacity, num_shards); _pk_index_page_cache = std::make_unique(pk_index_cache_capacity, num_shards); } diff --git a/be/src/runtime/memory/lru_cache_policy.h b/be/src/runtime/memory/lru_cache_policy.h index 7aa0b746715586..c5dbec0878b7de 100644 --- a/be/src/runtime/memory/lru_cache_policy.h +++ b/be/src/runtime/memory/lru_cache_policy.h @@ -74,11 +74,11 @@ class LRUCachePolicy : public CachePolicy { void reset_cache() { _cache.reset(); } bool check_capacity(size_t capacity, uint32_t num_shards) { - if (capacity < num_shards) { + if (capacity == 0 || capacity < num_shards) { LOG(INFO) << fmt::format( - "{} lru cache capacity({} B) less than num_shards({}), init failed, will be " - "disabled.", - type_string(type()), capacity, num_shards); + "{} lru cache capacity({} B) {} num_shards({}), will be disabled.", + type_string(type()), capacity, capacity == 0 ? "is 0, ignore" : "less than", + num_shards); _enable_prune = false; return false; } diff --git a/be/test/olap/page_cache_test.cpp b/be/test/olap/page_cache_test.cpp index a6b9300c1057f7..17011231b536ac 100644 --- a/be/test/olap/page_cache_test.cpp +++ b/be/test/olap/page_cache_test.cpp @@ -286,4 +286,108 @@ TEST_F(StoragePageCacheTest, mixed_pages) { } } +TEST_F(StoragePageCacheTest, zero_index_cache_percentage) { + StoragePageCache cache(kNumShards * 2048, 0, 0, kNumShards); + + StoragePageCache::CacheKey data_key("data", 0, 0); + StoragePageCache::CacheKey index_key("index", 0, 0); + + segment_v2::PageTypePB page_type_data = segment_v2::DATA_PAGE; + segment_v2::PageTypePB page_type_index = segment_v2::INDEX_PAGE; + + { + PageCacheHandle handle; + auto* data = new DataPage(1024, true, page_type_data); + cache.insert(data_key, data, &handle, page_type_data, false); + auto found = cache.lookup(data_key, &handle, page_type_data); + EXPECT_TRUE(found); + } + + { + PageCacheHandle handle; + auto* index = new DataPage(1024, true, page_type_index); + cache.insert(index_key, index, &handle, page_type_index, false); + auto found = cache.lookup(index_key, &handle, page_type_index); + EXPECT_FALSE(found); + } +} + +TEST_F(StoragePageCacheTest, full_index_cache_percentage) { + StoragePageCache cache(kNumShards * 2048, 100, 0, kNumShards); + + StoragePageCache::CacheKey data_key("data", 0, 0); + StoragePageCache::CacheKey index_key("index", 0, 0); + + segment_v2::PageTypePB page_type_data = segment_v2::DATA_PAGE; + segment_v2::PageTypePB page_type_index = segment_v2::INDEX_PAGE; + + { + PageCacheHandle handle; + auto* data = new DataPage(1024, true, page_type_data); + cache.insert(data_key, data, &handle, page_type_data, false); + auto found = cache.lookup(data_key, &handle, page_type_data); + EXPECT_FALSE(found); + } + + { + PageCacheHandle handle; + auto* index = new DataPage(1024, true, page_type_index); + cache.insert(index_key, index, &handle, page_type_index, false); + auto found = cache.lookup(index_key, &handle, page_type_index); + EXPECT_TRUE(found); + } +} + +TEST_F(StoragePageCacheTest, zero_total_capacity) { + StoragePageCache cache(0, 50, 0, kNumShards); + + StoragePageCache::CacheKey data_key("data", 0, 0); + StoragePageCache::CacheKey index_key("index", 0, 0); + + segment_v2::PageTypePB page_type_data = segment_v2::DATA_PAGE; + segment_v2::PageTypePB page_type_index = segment_v2::INDEX_PAGE; + + { + PageCacheHandle handle; + auto* data = new DataPage(1024, true, page_type_data); + cache.insert(data_key, data, &handle, page_type_data, false); + auto found = cache.lookup(data_key, &handle, page_type_data); + EXPECT_FALSE(found); + } + + { + PageCacheHandle handle; + auto* index = new DataPage(1024, true, page_type_index); + cache.insert(index_key, index, &handle, page_type_index, false); + auto found = cache.lookup(index_key, &handle, page_type_index); + EXPECT_FALSE(found); + } +} + +TEST_F(StoragePageCacheTest, capacity_less_than_num_shards) { + StoragePageCache cache(10, 50, 0, kNumShards); + + StoragePageCache::CacheKey data_key("data", 0, 0); + StoragePageCache::CacheKey index_key("index", 0, 0); + + segment_v2::PageTypePB page_type_data = segment_v2::DATA_PAGE; + segment_v2::PageTypePB page_type_index = segment_v2::INDEX_PAGE; + + { + PageCacheHandle handle; + auto* data = new DataPage(1024, true, page_type_data); + cache.insert(data_key, data, &handle, page_type_data, false); + auto found = cache.lookup(data_key, &handle, page_type_data); + EXPECT_FALSE(found); + } + + { + PageCacheHandle handle; + auto* index = new DataPage(1024, true, page_type_index); + cache.insert(index_key, index, &handle, page_type_index, false); + auto found = cache.lookup(index_key, &handle, page_type_index); + EXPECT_FALSE(found); + } +} + } // namespace doris