From 2e445d3ede953c90e4e1ce69e5f53192115b9dd7 Mon Sep 17 00:00:00 2001 From: Jason Tedor Date: Tue, 13 Oct 2015 22:14:13 -0400 Subject: [PATCH] Do not pollute Cache with failed futures --- .../java/org/elasticsearch/common/cache/Cache.java | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/core/src/main/java/org/elasticsearch/common/cache/Cache.java b/core/src/main/java/org/elasticsearch/common/cache/Cache.java index f16e7d8d25d..a686ecc9645 100644 --- a/core/src/main/java/org/elasticsearch/common/cache/Cache.java +++ b/core/src/main/java/org/elasticsearch/common/cache/Cache.java @@ -340,8 +340,18 @@ public class Cache { Entry entry; try { entry = future.get(); - } catch (InterruptedException e) { - throw new ExecutionException(e); + } catch (ExecutionException | InterruptedException e) { + // if the future ended exceptionally, we do not want to pollute the cache + // however, we have to take care to ensure that the polluted entry has not already been replaced + try (ReleasableLock ignored = segment.writeLock.acquire()) { + Future> sanity = segment.map.get(key); + try { + sanity.get(); + } catch (ExecutionException | InterruptedException gotcha) { + segment.map.remove(key); + } + } + throw (e instanceof ExecutionException) ? (ExecutionException)e : new ExecutionException(e); } if (entry.value == null) { throw new ExecutionException(new NullPointerException("loader returned a null value"));