<div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">On Thu, Feb 14, 2013 at 4:43 PM, Galder Zamarreño <span dir="ltr">&lt;<a href="mailto:galder@redhat.com" target="_blank">galder@redhat.com</a>&gt;</span> wrote:<br>

<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div class=""><div class="h5"><br>
<br>
On Feb 8, 2013, at 3:35 PM, Dan Berindei &lt;<a href="mailto:dan.berindei@gmail.com">dan.berindei@gmail.com</a>&gt; wrote:<br>
<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt; On Fri, Feb 8, 2013 at 3:41 PM, Galder Zamarreño &lt;<a href="mailto:galder@redhat.com">galder@redhat.com</a>&gt; wrote:<br>
&gt; Hi all,<br>
&gt;<br>
&gt; We&#39;ve got a small class loading puzzle to solve in our JSR-107 implementation.<br>
&gt;<br>
&gt; JSR-107 has a class called Caching which keeps a singleton enum reference (AFAIK, has same semantics as static) to the systemt&#39;s CacheManagerFactory, which in our case it would be InfinispanCacheManagerFactory:<br>


&gt; <a href="https://github.com/jsr107/jsr107spec/blob/master/src/main/java/javax/cache/Caching.java" target="_blank">https://github.com/jsr107/jsr107spec/blob/master/src/main/java/javax/cache/Caching.java</a><br>
&gt;<br>
&gt; A naive user of JSR-107 could decide to use this Caching class in an app server environment and get a reference to the CMF through it, which could cause major classloading issues if we don&#39;t protect ourselves.<br>


&gt;<br>
&gt; Within out CMF implementation, we need to keep some kind of mapping which given a name *and* a classloader, which can find the CacheManager instance associated to it.<br>
&gt;<br>
&gt; This poses a potential risk of a static strong reference being held indirectly on the classloader associated with the Infinispan Cache Manager (amongst other sensible components...).<br>
&gt;<br>
&gt; One way to break this strong reference is for CMF implementation to hold a weak reference on the CM as done here:<br>
&gt; <a href="https://github.com/galderz/infinispan/blob/t_2639/jsr107/src/main/java/org/infinispan/jsr107/cache/InfinispanCacheManagerFactory.java#L56" target="_blank">https://github.com/galderz/infinispan/blob/t_2639/jsr107/src/main/java/org/infinispan/jsr107/cache/InfinispanCacheManagerFactory.java#L56</a><br>


&gt;<br>
&gt; This poses a problem though in that the Infinispan Cache Manager can be evicted from memory without it&#39;s stop/shutdown method being called, leading to resources being left open (i.e. jgroups, jmx…etc).<br>
&gt;<br>
&gt; The only safe way to deal with this that I&#39;ve thought so far is to have a finalyze() method in InfinispanCacheManager (JSR-107 impl of CacheManager) that makes sure this cache manager is shut down. I&#39;m fully aware this is an expensive operation, but so far is the only way I can see in which we can avoid leaking stuff, while not affecting the actual Infinispan core module.<br>


&gt;<br>
&gt; I&#39;ve found a good example of this in <a href="https://github.com/jbossas/jboss-as/blob/master/controller-client/src/main/java/org/jboss/as/controller/client/impl/RemotingModelControllerClient.java" target="_blank">https://github.com/jbossas/jboss-as/blob/master/controller-client/src/main/java/org/jboss/as/controller/client/impl/RemotingModelControllerClient.java</a> - It even tracks creation time so that if all references to InfinispanCacheManager are lost but the ICM instance is not closed, it will print a warm message.<br>


&gt;<br>
&gt; If anyone has any other thoughts, it&#39;d be interesting to hear about them.<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt; The Caching javadoc seems to prohibit stopping the CacheManagers without user intervention (<a href="https://github.com/jsr107/jsr107spec/blob/master/src/main/java/javax/cache/Caching.java#L35" target="_blank">https://github.com/jsr107/jsr107spec/blob/master/src/main/java/javax/cache/Caching.java#L35</a>):<br>


&gt;<br>
&gt;  * Also keeps track of all CacheManagers created by the factory. Subsequent calls<br>
&gt;  * to {@link #getCacheManager()} return the same CacheManager.<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt; And in the javadoc of Caching.close() (<a href="https://github.com/jsr107/jsr107spec/blob/master/src/main/java/javax/cache/Caching.java#L153" target="_blank">https://github.com/jsr107/jsr107spec/blob/master/src/main/java/javax/cache/Caching.java#L153</a>):<br>


&gt;      * All cache managers obtained from the factory are shutdown.<br>
&gt;      * &lt;p/&gt;<br>
&gt;      * Subsequent requests from this factory will return different cache managers than would have been obtained before<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt;      * shutdown. So for example<br>
&gt;      * &lt;pre&gt;<br>
&gt;      *  CacheManager cacheManager = CacheFactory.getCacheManager();<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt;      *  assertSame(cacheManager, CacheFactory.getCacheManager());<br>
&gt;      *  CacheFactory.close();<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt;      *  assertNotSame(cacheManager, CacheFactory.getCacheManager());<br>
&gt;      * &lt;/pre&gt;<br>
&gt;<br>
&gt; We can&#39;t guarantee that getCacheManager() will return the same instance unless we keep a hard reference to it in our CacheManagerFactory. So I think the only option is to add a finalize() method to CacheManagerFactory that will stop all the CacheManagers if the user didn&#39;t explicitly call Caching.close().<br>


<br>
</div></div>A finalize() in CacheManagerFactory does not solve the problem since there&#39;s still a hard reference to the CacheManagerFactory impl from Caching, and as long as that&#39;s not cleared, finalize() won&#39;t be executed, so you&#39;re still exposed to a potential leak.<br>


<br></blockquote><div><br></div><div>Yeah, that&#39;s true.<br><br></div><div>But note that the opposite is also possible: the user can call Caching.close() from one web app and it will close all the cache managers opened from any other web app. I doubt we can protect ourselves against that...<br>

<br></div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
My initial solution in [1] didn&#39;t pass the TCK because the tests don&#39;t keep any hard reference to the cache manager, so they could literally dissapear from the collection cos there was no other hard cache manager reference.<br>


<br>
An alternative way to solve this is to have a weak hash map with classloader as weak key:<br>
<br>
   private final Map&lt;ClassLoader, Map&lt;String, InfinispanCacheManager&gt;&gt; cacheManagers =<br>
           new WeakHashMap&lt;ClassLoader, Map&lt;String, InfinispanCacheManager&gt;&gt;();<br>
<br>
However, for this to work, all other references that we keep within Infinispan of cache managers have to also be weak. I&#39;ve made this change and it all works fine now because the TCK does have a reference to the classloader.<br>


<br></blockquote><div> </div><div>I&#39;m glad to hear that it works!<br><br>I didn&#39;t really consider this option, because I thought it would be too hard to change all the classloader references. Particularly the contextClassLoader references from all the threads we create...<br>

<br></div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
With this, the user can forget to the close the cache manager, and as long as no other strong references to the classloader are kept, the Infinispan Cache Manager can be garbage collected, and it&#39;s finalize() method called resulting in proper shutdown.<br>


<br>
[1] <a href="https://github.com/galderz/infinispan/blob/t_2639/jsr107/src/main/java/org/infinispan/jsr107/cache/InfinispanCacheManagerFactory.java#L56" target="_blank">https://github.com/galderz/infinispan/blob/t_2639/jsr107/src/main/java/org/infinispan/jsr107/cache/InfinispanCacheManagerFactory.java#L56</a><br>


<div class=""><div class="h5"><br></div></div></blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div class=""><div class="h5">
&gt;<br>
&gt; Cheers<br>
&gt; Dan<br>
&gt;<br>
&gt;<br>
&gt; Cheers,<br>
&gt; --<br>
&gt; Galder Zamarreño<br>
&gt; <a href="mailto:galder@redhat.com">galder@redhat.com</a><br>
&gt; <a href="http://twitter.com/galderz" target="_blank">twitter.com/galderz</a><br>
&gt;<br>
&gt; Project Lead, Escalante<br>
&gt; <a href="http://escalante.io" target="_blank">http://escalante.io</a><br>
&gt;<br>
&gt; Engineer, Infinispan<br>
&gt; <a href="http://infinispan.org" target="_blank">http://infinispan.org</a><br>
&gt;<br>
&gt;<br>
&gt; _______________________________________________<br>
&gt; infinispan-dev mailing list<br>
&gt; <a href="mailto:infinispan-dev@lists.jboss.org">infinispan-dev@lists.jboss.org</a><br>
&gt; <a href="https://lists.jboss.org/mailman/listinfo/infinispan-dev" target="_blank">https://lists.jboss.org/mailman/listinfo/infinispan-dev</a><br>
&gt;<br>
&gt; _______________________________________________<br>
&gt; infinispan-dev mailing list<br>
&gt; <a href="mailto:infinispan-dev@lists.jboss.org">infinispan-dev@lists.jboss.org</a><br>
&gt; <a href="https://lists.jboss.org/mailman/listinfo/infinispan-dev" target="_blank">https://lists.jboss.org/mailman/listinfo/infinispan-dev</a><br>
<br>
<br>
--<br>
Galder Zamarreño<br>
<a href="mailto:galder@redhat.com">galder@redhat.com</a><br>
<a href="http://twitter.com/galderz" target="_blank">twitter.com/galderz</a><br>
<br>
Project Lead, Escalante<br>
<a href="http://escalante.io" target="_blank">http://escalante.io</a><br>
<br>
Engineer, Infinispan<br>
<a href="http://infinispan.org" target="_blank">http://infinispan.org</a><br>
<br>
<br>
_______________________________________________<br>
infinispan-dev mailing list<br>
<a href="mailto:infinispan-dev@lists.jboss.org">infinispan-dev@lists.jboss.org</a><br>
<a href="https://lists.jboss.org/mailman/listinfo/infinispan-dev" target="_blank">https://lists.jboss.org/mailman/listinfo/infinispan-dev</a><br>
</div></div></blockquote></div><br></div></div>