From fc4f31083939c47d658763056c5d95c3d182ee88 Mon Sep 17 00:00:00 2001 From: Luca Milanesio Date: Tue, 8 Nov 2022 01:19:11 +0000 Subject: Fix bulk loading of entries with PassthroughLoadingCache During the review of I5906a1f2 the PassthroughLoadingCache added the mechanism for bulk-loading in the getAll() method. However, the new flavour of loading was not covered by additional tests and then failed to honour the method because of the casting of a regular Map to an ImmutableMap. Rectify the situation and add one extra test to document the expected behaviour. Release-Notes: Fix bulk loading of entries for disabled caches Change-Id: I7a24deebeef7f6ae853d11c1ee458b53296e1a44 --- .../server/cache/mem/PassthroughLoadingCache.java | 4 ++-- .../cache/mem/DefaultMemoryCacheFactoryTest.java | 23 ++++++++++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/java/com/google/gerrit/server/cache/mem/PassthroughLoadingCache.java b/java/com/google/gerrit/server/cache/mem/PassthroughLoadingCache.java index 4ed8cfaf44..ded21aae5b 100644 --- a/java/com/google/gerrit/server/cache/mem/PassthroughLoadingCache.java +++ b/java/com/google/gerrit/server/cache/mem/PassthroughLoadingCache.java @@ -122,8 +122,8 @@ public class PassthroughLoadingCache implements LoadingCache { } @SuppressWarnings("unchecked") - public ImmutableMap getAllBulk(Iterable keys) throws Exception { - return (ImmutableMap) cacheLoader.loadAll(keys); + private ImmutableMap getAllBulk(Iterable keys) throws Exception { + return (ImmutableMap) ImmutableMap.copyOf(cacheLoader.loadAll(keys)); } @Override diff --git a/javatests/com/google/gerrit/server/cache/mem/DefaultMemoryCacheFactoryTest.java b/javatests/com/google/gerrit/server/cache/mem/DefaultMemoryCacheFactoryTest.java index e0c0c34a78..9b504a94f7 100644 --- a/javatests/com/google/gerrit/server/cache/mem/DefaultMemoryCacheFactoryTest.java +++ b/javatests/com/google/gerrit/server/cache/mem/DefaultMemoryCacheFactoryTest.java @@ -20,10 +20,14 @@ import static com.google.common.truth.Truth.assertThat; import com.google.common.cache.CacheLoader; import com.google.common.cache.LoadingCache; import com.google.common.cache.Weigher; +import com.google.common.collect.ImmutableMap; import com.google.gerrit.server.cache.CacheBackend; import com.google.gerrit.server.cache.CacheDef; import com.google.inject.TypeLiteral; import java.time.Duration; +import java.util.Arrays; +import java.util.List; +import java.util.Map; import java.util.concurrent.BrokenBarrierException; import java.util.concurrent.CyclicBarrier; import java.util.concurrent.ExecutionException; @@ -33,6 +37,8 @@ import java.util.concurrent.ScheduledFuture; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; import java.util.function.Function; +import java.util.stream.Collectors; +import java.util.stream.StreamSupport; import org.eclipse.jgit.lib.Config; import org.junit.Before; import org.junit.Test; @@ -90,6 +96,17 @@ public class DefaultMemoryCacheFactoryTest { assertThat(cacheValue.get()).isEqualTo(TEST_CACHE_KEY); } + @Test + public void shouldLoadAllKeysWithDisabledCache() throws Exception { + LoadingCache disabledCache = + memoryCacheFactory.build(newCacheDef(0), newCacheLoader(identity()), CacheBackend.CAFFEINE); + + List keys = Arrays.asList(1, 2); + ImmutableMap entries = disabledCache.getAll(keys); + + assertThat(entries).containsExactly(1, 1, 2, 2); + } + private void assertCacheEvictionIsNotBlocking(LoadingCache disabledCache) throws InterruptedException, BrokenBarrierException, TimeoutException, ExecutionException { ScheduledFuture cacheValue = @@ -120,6 +137,12 @@ public class DefaultMemoryCacheFactoryTest { } return v; } + + @Override + public Map loadAll(Iterable keys) throws Exception { + return StreamSupport.stream(keys.spliterator(), false) + .collect(Collectors.toMap(identity(), identity())); + } }; } -- cgit v1.2.3