Skip to content

Commit 2e445d3

Browse files
committed
Do not pollute Cache with failed futures
1 parent 1d0b93f commit 2e445d3

File tree

1 file changed

+12
-2
lines changed
  • core/src/main/java/org/elasticsearch/common/cache

1 file changed

+12
-2
lines changed

core/src/main/java/org/elasticsearch/common/cache/Cache.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -340,8 +340,18 @@ public V computeIfAbsent(K key, CacheLoader<K, V> loader) throws ExecutionExcept
340340
Entry<K, V> entry;
341341
try {
342342
entry = future.get();
343-
} catch (InterruptedException e) {
344-
throw new ExecutionException(e);
343+
} catch (ExecutionException | InterruptedException e) {
344+
// if the future ended exceptionally, we do not want to pollute the cache
345+
// however, we have to take care to ensure that the polluted entry has not already been replaced
346+
try (ReleasableLock ignored = segment.writeLock.acquire()) {
347+
Future<Entry<K, V>> sanity = segment.map.get(key);
348+
try {
349+
sanity.get();
350+
} catch (ExecutionException | InterruptedException gotcha) {
351+
segment.map.remove(key);
352+
}
353+
}
354+
throw (e instanceof ExecutionException) ? (ExecutionException)e : new ExecutionException(e);
345355
}
346356
if (entry.value == null) {
347357
throw new ExecutionException(new NullPointerException("loader returned a null value"));

0 commit comments

Comments
 (0)