|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +#include "storage/cache/ann_index_ivf_list_cache.h" |
| 19 | + |
| 20 | +#include <gtest/gtest-message.h> |
| 21 | +#include <gtest/gtest-test-part.h> |
| 22 | + |
| 23 | +#include <atomic> |
| 24 | +#include <barrier> |
| 25 | +#include <cstring> |
| 26 | +#include <thread> |
| 27 | +#include <vector> |
| 28 | + |
| 29 | +#include "gtest/gtest_pred_impl.h" |
| 30 | + |
| 31 | +namespace doris { |
| 32 | + |
| 33 | +static constexpr uint32_t kNumShards = AnnIndexIVFListCache::kDefaultNumShards; |
| 34 | + |
| 35 | +class AnnIndexIVFListCacheTest : public testing::Test { |
| 36 | +public: |
| 37 | + AnnIndexIVFListCacheTest() { |
| 38 | + mem_tracker = MemTrackerLimiter::create_shared(MemTrackerLimiter::Type::GLOBAL, |
| 39 | + "AnnIndexIVFListCacheTest"); |
| 40 | + } |
| 41 | + |
| 42 | +protected: |
| 43 | + void SetUp() override { |
| 44 | + cache_ = std::make_unique<AnnIndexIVFListCache>(kNumShards * 1024 * 1024, kNumShards); |
| 45 | + } |
| 46 | + |
| 47 | + void TearDown() override { cache_.reset(); } |
| 48 | + |
| 49 | + std::shared_ptr<MemTrackerLimiter> mem_tracker; |
| 50 | + std::unique_ptr<AnnIndexIVFListCache> cache_; |
| 51 | +}; |
| 52 | + |
| 53 | +TEST_F(AnnIndexIVFListCacheTest, basic_insert_and_lookup) { |
| 54 | + AnnIndexIVFListCache::CacheKey key("test_file", 4096, 0); |
| 55 | + |
| 56 | + PageCacheHandle handle; |
| 57 | + EXPECT_FALSE(cache_->lookup(key, &handle)); |
| 58 | + |
| 59 | + auto* page = new DataPage(1024, mem_tracker); |
| 60 | + std::memset(page->data(), 0xAB, 1024); |
| 61 | + |
| 62 | + cache_->insert(key, page, &handle); |
| 63 | + Slice data = handle.data(); |
| 64 | + EXPECT_EQ(data.data, page->data()); |
| 65 | + |
| 66 | + PageCacheHandle lookup_handle; |
| 67 | + EXPECT_TRUE(cache_->lookup(key, &lookup_handle)); |
| 68 | + Slice lookup_data = lookup_handle.data(); |
| 69 | + EXPECT_EQ(static_cast<unsigned char>(lookup_data.data[0]), 0xAB); |
| 70 | +} |
| 71 | + |
| 72 | +TEST_F(AnnIndexIVFListCacheTest, cache_miss_different_key) { |
| 73 | + AnnIndexIVFListCache::CacheKey key("file_a", 4096, 0); |
| 74 | + |
| 75 | + auto* page = new DataPage(512, mem_tracker); |
| 76 | + PageCacheHandle handle; |
| 77 | + cache_->insert(key, page, &handle); |
| 78 | + |
| 79 | + { |
| 80 | + PageCacheHandle miss_handle; |
| 81 | + AnnIndexIVFListCache::CacheKey miss_key("file_b", 4096, 0); |
| 82 | + EXPECT_FALSE(cache_->lookup(miss_key, &miss_handle)); |
| 83 | + } |
| 84 | + |
| 85 | + { |
| 86 | + PageCacheHandle miss_handle; |
| 87 | + AnnIndexIVFListCache::CacheKey miss_key("file_a", 8192, 0); |
| 88 | + EXPECT_FALSE(cache_->lookup(miss_key, &miss_handle)); |
| 89 | + } |
| 90 | + |
| 91 | + { |
| 92 | + PageCacheHandle miss_handle; |
| 93 | + AnnIndexIVFListCache::CacheKey miss_key("file_a", 4096, 100); |
| 94 | + EXPECT_FALSE(cache_->lookup(miss_key, &miss_handle)); |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +TEST_F(AnnIndexIVFListCacheTest, multiple_entries) { |
| 99 | + constexpr int kCount = 32; |
| 100 | + std::vector<PageCacheHandle> handles(kCount); |
| 101 | + for (int i = 0; i < kCount; ++i) { |
| 102 | + AnnIndexIVFListCache::CacheKey key("multi", 4096, i * 1024); |
| 103 | + auto* page = new DataPage(256, mem_tracker); |
| 104 | + std::memset(page->data(), static_cast<char>(i), 256); |
| 105 | + cache_->insert(key, page, &handles[i]); |
| 106 | + } |
| 107 | + |
| 108 | + for (int i = 0; i < kCount; ++i) { |
| 109 | + AnnIndexIVFListCache::CacheKey key("multi", 4096, i * 1024); |
| 110 | + PageCacheHandle handle; |
| 111 | + EXPECT_TRUE(cache_->lookup(key, &handle)); |
| 112 | + Slice data = handle.data(); |
| 113 | + EXPECT_EQ(static_cast<unsigned char>(data.data[0]), static_cast<unsigned char>(i)); |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +// Simulates the stampede protection pattern from CachedRandomAccessReader::borrow(). |
| 118 | +// |
| 119 | +// The test exercises the double-check locking path: N threads concurrently |
| 120 | +// miss the fast-path cache lookup for the same key; only the first thread |
| 121 | +// through the mutex performs the "disk I/O" (here: allocate + fill a DataPage); |
| 122 | +// the remaining N-1 threads find the entry on re-check and skip the I/O. |
| 123 | +TEST_F(AnnIndexIVFListCacheTest, stampede_protection) { |
| 124 | + constexpr int kThreads = 8; |
| 125 | + const AnnIndexIVFListCache::CacheKey key("stampede_file", 4096, 0); |
| 126 | + |
| 127 | + std::atomic<int> io_count {0}; |
| 128 | + std::mutex io_mutex; |
| 129 | + std::barrier sync_point(kThreads); |
| 130 | + |
| 131 | + auto simulate_borrow = [&]() { |
| 132 | + PageCacheHandle handle; |
| 133 | + if (cache_->lookup(key, &handle)) { |
| 134 | + return; |
| 135 | + } |
| 136 | + |
| 137 | + sync_point.arrive_and_wait(); |
| 138 | + |
| 139 | + std::lock_guard<std::mutex> lock(io_mutex); |
| 140 | + |
| 141 | + if (cache_->lookup(key, &handle)) { |
| 142 | + return; |
| 143 | + } |
| 144 | + |
| 145 | + auto* page = new DataPage(1024, mem_tracker); |
| 146 | + std::memset(page->data(), 0xCD, 1024); |
| 147 | + io_count.fetch_add(1, std::memory_order_relaxed); |
| 148 | + cache_->insert(key, page, &handle); |
| 149 | + }; |
| 150 | + |
| 151 | + std::vector<std::thread> threads; |
| 152 | + threads.reserve(kThreads); |
| 153 | + for (int i = 0; i < kThreads; ++i) { |
| 154 | + threads.emplace_back(simulate_borrow); |
| 155 | + } |
| 156 | + for (auto& t : threads) { |
| 157 | + t.join(); |
| 158 | + } |
| 159 | + |
| 160 | + EXPECT_EQ(io_count.load(), 1); |
| 161 | + |
| 162 | + PageCacheHandle verify_handle; |
| 163 | + EXPECT_TRUE(cache_->lookup(key, &verify_handle)); |
| 164 | + Slice data = verify_handle.data(); |
| 165 | + EXPECT_EQ(static_cast<unsigned char>(data.data[0]), 0xCD); |
| 166 | +} |
| 167 | + |
| 168 | +// Variant: multiple distinct keys under contention. Each key should be |
| 169 | +// loaded exactly once even when many threads race on each key. |
| 170 | +TEST_F(AnnIndexIVFListCacheTest, stampede_protection_multiple_keys) { |
| 171 | + constexpr int kKeys = 4; |
| 172 | + constexpr int kThreadsPerKey = 4; |
| 173 | + constexpr int kTotalThreads = kKeys * kThreadsPerKey; |
| 174 | + |
| 175 | + std::atomic<int> io_counts[kKeys]; |
| 176 | + for (auto& c : io_counts) { |
| 177 | + c.store(0); |
| 178 | + } |
| 179 | + |
| 180 | + std::mutex io_mutex; |
| 181 | + std::barrier sync_point(kTotalThreads); |
| 182 | + |
| 183 | + auto simulate_borrow = [&](int key_idx) { |
| 184 | + AnnIndexIVFListCache::CacheKey key("multi_stampede", 4096, key_idx * 1024); |
| 185 | + |
| 186 | + PageCacheHandle handle; |
| 187 | + if (cache_->lookup(key, &handle)) { |
| 188 | + return; |
| 189 | + } |
| 190 | + |
| 191 | + sync_point.arrive_and_wait(); |
| 192 | + |
| 193 | + std::lock_guard<std::mutex> lock(io_mutex); |
| 194 | + |
| 195 | + if (cache_->lookup(key, &handle)) { |
| 196 | + return; |
| 197 | + } |
| 198 | + |
| 199 | + auto* page = new DataPage(512, mem_tracker); |
| 200 | + std::memset(page->data(), static_cast<char>(key_idx), 512); |
| 201 | + io_counts[key_idx].fetch_add(1, std::memory_order_relaxed); |
| 202 | + cache_->insert(key, page, &handle); |
| 203 | + }; |
| 204 | + |
| 205 | + std::vector<std::thread> threads; |
| 206 | + threads.reserve(kTotalThreads); |
| 207 | + for (int k = 0; k < kKeys; ++k) { |
| 208 | + for (int t = 0; t < kThreadsPerKey; ++t) { |
| 209 | + threads.emplace_back(simulate_borrow, k); |
| 210 | + } |
| 211 | + } |
| 212 | + for (auto& th : threads) { |
| 213 | + th.join(); |
| 214 | + } |
| 215 | + |
| 216 | + for (int k = 0; k < kKeys; ++k) { |
| 217 | + EXPECT_EQ(io_counts[k].load(), 1) << "key_idx=" << k; |
| 218 | + |
| 219 | + AnnIndexIVFListCache::CacheKey key("multi_stampede", 4096, k * 1024); |
| 220 | + PageCacheHandle handle; |
| 221 | + EXPECT_TRUE(cache_->lookup(key, &handle)); |
| 222 | + Slice data = handle.data(); |
| 223 | + EXPECT_EQ(static_cast<unsigned char>(data.data[0]), static_cast<unsigned char>(k)); |
| 224 | + } |
| 225 | +} |
| 226 | + |
| 227 | +TEST_F(AnnIndexIVFListCacheTest, singleton_lifecycle) { |
| 228 | + EXPECT_EQ(AnnIndexIVFListCache::instance(), nullptr); |
| 229 | + |
| 230 | + auto* inst = AnnIndexIVFListCache::create_global_cache(kNumShards * 2048, kNumShards); |
| 231 | + EXPECT_NE(inst, nullptr); |
| 232 | + EXPECT_EQ(AnnIndexIVFListCache::instance(), inst); |
| 233 | + |
| 234 | + AnnIndexIVFListCache::destroy_global_cache(); |
| 235 | + EXPECT_EQ(AnnIndexIVFListCache::instance(), nullptr); |
| 236 | +} |
| 237 | + |
| 238 | +} // namespace doris |
0 commit comments