diff --git a/src/main/java/org/elasticsearch/index/cache/id/simple/SimpleIdCache.java b/src/main/java/org/elasticsearch/index/cache/id/simple/SimpleIdCache.java index a53900bfddc..573b8f6b658 100644 --- a/src/main/java/org/elasticsearch/index/cache/id/simple/SimpleIdCache.java +++ b/src/main/java/org/elasticsearch/index/cache/id/simple/SimpleIdCache.java @@ -82,10 +82,15 @@ public class SimpleIdCache extends AbstractIndexComponent implements IdCache, Se @Override public void clear() { - for (Iterator it = idReaders.values().iterator(); it.hasNext(); ) { - SimpleIdReaderCache idReaderCache = it.next(); - it.remove(); - onRemoval(idReaderCache); + // Make a copy of the live id readers... + Map copy = new HashMap(idReaders); + for (Map.Entry entry : copy.entrySet()) { + SimpleIdReaderCache removed = idReaders.remove(entry.getKey()); + // ... and only if the id reader still exists in live readers we decrement stats, + // this will prevent double onRemoval calls + if (removed != null) { + onRemoval(removed); + } } }