From bac6240d17904210a3362487d6bf5078da230555 Mon Sep 17 00:00:00 2001 From: kimchy Date: Fri, 1 Jul 2011 13:32:23 +0300 Subject: [PATCH] improve clearing caches in guice --- .../common/inject/InheritingState.java | 6 +- .../common/inject/InjectorImpl.java | 11 +++- .../common/inject/Injectors.java | 55 +------------------ .../elasticsearch/common/inject/State.java | 6 ++ 4 files changed, 21 insertions(+), 57 deletions(-) diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/common/inject/InheritingState.java b/modules/elasticsearch/src/main/java/org/elasticsearch/common/inject/InheritingState.java index c25cb10b368..0467e6ddac3 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/common/inject/InheritingState.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/common/inject/InheritingState.java @@ -45,7 +45,7 @@ class InheritingState implements State { private final Map, Scope> scopes = Maps.newHashMap(); private final List converters = Lists.newArrayList(); private final List listenerBindings = Lists.newArrayList(); - private final WeakKeySet blacklistedKeys = new WeakKeySet(); + private WeakKeySet blacklistedKeys = new WeakKeySet(); private final Object lock; InheritingState(State parent) { @@ -126,6 +126,10 @@ class InheritingState implements State { return blacklistedKeys.contains(key); } + @Override public void clearBlacklisted() { + blacklistedKeys = new WeakKeySet(); + } + public Object lock() { return lock; } diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/common/inject/InjectorImpl.java b/modules/elasticsearch/src/main/java/org/elasticsearch/common/inject/InjectorImpl.java index 734a2524b5d..3a4f01de886 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/common/inject/InjectorImpl.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/common/inject/InjectorImpl.java @@ -57,7 +57,7 @@ class InjectorImpl implements Injector, Lookups { /** * Just-in-time binding cache. Guarded by state.lock() */ - final Map, BindingImpl> jitBindings = Maps.newHashMap(); + Map, BindingImpl> jitBindings = Maps.newHashMap(); Lookups lookups = new DeferredLookups(this); @@ -723,7 +723,7 @@ class InjectorImpl implements Injector, Lookups { /** * Cached constructor injectors for each type */ - final ConstructorInjectorStore constructors = new ConstructorInjectorStore(this); + ConstructorInjectorStore constructors = new ConstructorInjectorStore(this); /** * Cached field and method injectors for each type. @@ -830,4 +830,11 @@ class InjectorImpl implements Injector, Lookups { .toString(); } + // ES_GUICE: clear caches + public void clearCache() { + state.clearBlacklisted(); + constructors = new ConstructorInjectorStore(this); + membersInjectorStore = new MembersInjectorStore(this, state.getTypeListenerBindings()); + jitBindings = Maps.newHashMap(); + } } diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/common/inject/Injectors.java b/modules/elasticsearch/src/main/java/org/elasticsearch/common/inject/Injectors.java index 7e6f5625eb5..37eb0065318 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/common/inject/Injectors.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/common/inject/Injectors.java @@ -19,12 +19,10 @@ package org.elasticsearch.common.inject; -import org.elasticsearch.ElasticSearchIllegalStateException; import org.elasticsearch.common.collect.Sets; import org.elasticsearch.common.inject.matcher.Matcher; import org.elasticsearch.common.inject.name.Names; -import java.lang.reflect.Field; import java.lang.reflect.Type; import java.util.Map; import java.util.Map.Entry; @@ -224,58 +222,7 @@ public class Injectors { } public static void cleanCaches(Injector injector) { - // clean blacklist, it becomes really big, and it can always get regenerated if needed - try { - Field stateField = injector.getClass().getDeclaredField("state"); - stateField.setAccessible(true); - Object state = stateField.get(injector); - if (state.getClass().getName().contains("InheritingState")) { - Field blacklistedKeysField = state.getClass().getDeclaredField("blacklistedKeys"); - blacklistedKeysField.setAccessible(true); - Object blacklistedKeys = blacklistedKeysField.get(state); - Field backingSetField = blacklistedKeys.getClass().getDeclaredField("backingSet"); - backingSetField.setAccessible(true); - ((Set) backingSetField.get(blacklistedKeys)).clear(); - } - } catch (Exception e) { - throw new ElasticSearchIllegalStateException("Failed to clear state from injector", e); - } - - // clean constructors cache - try { - Field constructorsField = injector.getClass().getDeclaredField("constructors"); - constructorsField.setAccessible(true); - Object constructors = constructorsField.get(injector); - - Field cacheField = constructors.getClass().getDeclaredField("cache"); - cacheField.setAccessible(true); - Object cache = cacheField.get(constructors); - - Field delegateField = cache.getClass().getSuperclass().getDeclaredField("delegate"); - delegateField.setAccessible(true); - ((Map) delegateField.get(cache)).clear(); - } catch (Exception e) { - throw new ElasticSearchIllegalStateException("Failed to clear constructors cache from injector", e); - } - - // clean method cache - try { - Field membersField = injector.getClass().getDeclaredField("membersInjectorStore"); - membersField.setAccessible(true); - Object members = membersField.get(injector); - if (members != null) { - Field cacheField = members.getClass().getDeclaredField("cache"); - cacheField.setAccessible(true); - Object cache = cacheField.get(members); - - Field delegateField = cache.getClass().getSuperclass().getDeclaredField("delegate"); - delegateField.setAccessible(true); - ((Map) delegateField.get(cache)).clear(); - } - } catch (Exception e) { - throw new ElasticSearchIllegalStateException("Failed to clear constructors cache from injector", e); - } - + ((InjectorImpl) injector).clearCache(); if (injector.getParent() != null) { cleanCaches(injector.getParent()); } diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/common/inject/State.java b/modules/elasticsearch/src/main/java/org/elasticsearch/common/inject/State.java index 8e3262655cd..7e443d3f384 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/common/inject/State.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/common/inject/State.java @@ -88,6 +88,9 @@ interface State { return true; } + @Override public void clearBlacklisted() { + } + public Object lock() { throw new UnsupportedOperationException(); } @@ -149,4 +152,7 @@ interface State { * to be used when reading mutable data (ie. just-in-time bindings, and binding blacklists). */ Object lock(); + + // ES_GUICE: clean blacklist keys + void clearBlacklisted(); }