diff --git a/ehcache-impl/src/main/java/org/ehcache/impl/internal/store/tiering/TieredStore.java b/ehcache-impl/src/main/java/org/ehcache/impl/internal/store/tiering/TieredStore.java index c2b3c0bbe2..c814b54b0e 100644 --- a/ehcache-impl/src/main/java/org/ehcache/impl/internal/store/tiering/TieredStore.java +++ b/ehcache-impl/src/main/java/org/ehcache/impl/internal/store/tiering/TieredStore.java @@ -572,7 +572,10 @@ public NoopCachingTier(final AuthoritativeTier authoritativeTier) { @Override public ValueHolder getOrComputeIfAbsent(final K key, final Function> source) { final ValueHolder apply = source.apply(key); - authoritativeTier.flush(key, apply); + if (apply != null) { + //immediately flushes any entries faulted from authority as this tier has no capacity + authoritativeTier.flush(key, apply); + } return apply; } diff --git a/ehcache-impl/src/test/java/org/ehcache/impl/internal/store/tiering/TieredStoreTest.java b/ehcache-impl/src/test/java/org/ehcache/impl/internal/store/tiering/TieredStoreTest.java index 151d5d72d4..5e0461d45b 100644 --- a/ehcache-impl/src/test/java/org/ehcache/impl/internal/store/tiering/TieredStoreTest.java +++ b/ehcache-impl/src/test/java/org/ehcache/impl/internal/store/tiering/TieredStoreTest.java @@ -560,6 +560,33 @@ public void CachingTierDoesNotSeeAnyOperationDuringClear() throws StoreAccessExc ArgumentMatchers.any(), ArgumentMatchers.any()); } + @Test + public void AuthoritativeTierNullCheckDuringFlush() throws StoreAccessException, BrokenBarrierException, InterruptedException { + final TieredStore tieredStore = new TieredStore<>(stringCachingTier, stringAuthoritativeTier); + + final CyclicBarrier barrier = new CyclicBarrier(2); + + doAnswer((Answer) invocation -> { + barrier.await(); + barrier.await(); + return null; + }).when(stringAuthoritativeTier).clear(); + Thread t = new Thread(() -> { + try { + tieredStore.clear(); + } catch (Exception e) { + throw new RuntimeException(e); + } + }); + + t.start(); + barrier.await(); + tieredStore.get("foo"); + barrier.await(); + t.join(); + verify(stringAuthoritativeTier, never()).flush("foo", null); + } + @Test @SuppressWarnings("unchecked") public void testReleaseStoreFlushes() throws Exception {