You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cayenne.apache.org by nt...@apache.org on 2019/12/30 07:07:08 UTC

[cayenne] 01/02: CAY-2642 EhCache memory leak due to misconfiguration - add a warning when creating new cache entry with default config

This is an automated email from the ASF dual-hosted git repository.

ntimofeev pushed a commit to branch STABLE-4.1
in repository https://gitbox.apache.org/repos/asf/cayenne.git

commit 61a8f6143c82e6a4684e4259481732bb631a1a1d
Author: Nikita Timofeev <st...@gmail.com>
AuthorDate: Thu Dec 26 11:46:33 2019 +0300

    CAY-2642 EhCache memory leak due to misconfiguration
      - add a warning when creating new cache entry with default config
    
    (cherry picked from commit 4720d65914fc474c2359c18d4e8414fe68022412)
---
 cayenne-jcache/pom.xml                             | 26 +++++++++++++++++-----
 .../apache/cayenne/jcache/JCacheQueryCache.java    |  7 ++++++
 .../jcache/unit/CacheServerRuntimeProvider.java    | 12 ++++++++++
 cayenne-jcache/src/test/resources/eh-cache.xml     | 10 +++++++++
 4 files changed, 50 insertions(+), 5 deletions(-)

diff --git a/cayenne-jcache/pom.xml b/cayenne-jcache/pom.xml
index c87b8e3..e9e26a9 100644
--- a/cayenne-jcache/pom.xml
+++ b/cayenne-jcache/pom.xml
@@ -31,8 +31,9 @@
     <packaging>jar</packaging>
 
     <properties>
-        <ehcache-version>3.3.1</ehcache-version>
         <jcache-version>1.0.0</jcache-version>
+        <ehcache-version>3.8.1</ehcache-version>
+        <jaxb-api-version>2.3.1</jaxb-api-version>
     </properties>
 
     <dependencyManagement>
@@ -47,6 +48,16 @@
                 <artifactId>cache-api</artifactId>
                 <version>${jcache-version}</version>
             </dependency>
+            <dependency>
+                <groupId>javax.xml.bind</groupId>
+                <artifactId>jaxb-api</artifactId>
+                <version>${jaxb-api-version}</version>
+            </dependency>
+            <dependency>
+                <groupId>com.sun.xml.bind</groupId>
+                <artifactId>jaxb-impl</artifactId>
+                <version>${jaxb-api-version}</version>
+            </dependency>
         </dependencies>
     </dependencyManagement>
 
@@ -89,13 +100,18 @@
             <scope>test</scope>
         </dependency>
         <dependency>
-            <groupId>org.ehcache</groupId>
-            <artifactId>ehcache</artifactId>
+            <groupId>javax.xml.bind</groupId>
+            <artifactId>jaxb-api</artifactId>
             <scope>test</scope>
         </dependency>
         <dependency>
-            <groupId>org.slf4j</groupId>
-            <artifactId>slf4j-api</artifactId>
+            <groupId>com.sun.xml.bind</groupId>
+            <artifactId>jaxb-impl</artifactId>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.ehcache</groupId>
+            <artifactId>ehcache</artifactId>
             <scope>test</scope>
         </dependency>
         <dependency>
diff --git a/cayenne-jcache/src/main/java/org/apache/cayenne/jcache/JCacheQueryCache.java b/cayenne-jcache/src/main/java/org/apache/cayenne/jcache/JCacheQueryCache.java
index b21b026..269c8ea 100644
--- a/cayenne-jcache/src/main/java/org/apache/cayenne/jcache/JCacheQueryCache.java
+++ b/cayenne-jcache/src/main/java/org/apache/cayenne/jcache/JCacheQueryCache.java
@@ -24,6 +24,8 @@ import org.apache.cayenne.cache.QueryCacheEntryFactory;
 import org.apache.cayenne.di.BeforeScopeEnd;
 import org.apache.cayenne.di.Inject;
 import org.apache.cayenne.query.QueryMetadata;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 import javax.cache.Cache;
 import javax.cache.CacheException;
@@ -39,6 +41,8 @@ import java.util.concurrent.ConcurrentHashMap;
  */
 public class JCacheQueryCache implements QueryCache {
 
+    private static final Logger LOGGER = LoggerFactory.getLogger(QueryCache.class);
+
     @Inject
     protected CacheManager cacheManager;
 
@@ -139,6 +143,9 @@ public class JCacheQueryCache implements QueryCache {
     }
 
     protected Cache createCache(String cacheName) {
+        // Cache creation here can lead to a memory leak, see CAY-2642 for details.
+        LOGGER.warn("Creating a new JCache entry '{}'. It will be unlimited by default, and that can lead to greater memory usage or even leak. " +
+                "This entry could be configured by JCache provider-specific configuration.", cacheName);
         return cacheManager.createCache(cacheName, configurationFactory.create(cacheName));
     }
 
diff --git a/cayenne-jcache/src/test/java/org/apache/cayenne/jcache/unit/CacheServerRuntimeProvider.java b/cayenne-jcache/src/test/java/org/apache/cayenne/jcache/unit/CacheServerRuntimeProvider.java
index d9df7f6..322a5a6 100644
--- a/cayenne-jcache/src/test/java/org/apache/cayenne/jcache/unit/CacheServerRuntimeProvider.java
+++ b/cayenne-jcache/src/test/java/org/apache/cayenne/jcache/unit/CacheServerRuntimeProvider.java
@@ -19,9 +19,11 @@
 
 package org.apache.cayenne.jcache.unit;
 
+import java.net.URISyntaxException;
 import java.util.ArrayList;
 import java.util.Collection;
 
+import org.apache.cayenne.CayenneRuntimeException;
 import org.apache.cayenne.dba.DbAdapter;
 import org.apache.cayenne.di.Inject;
 import org.apache.cayenne.di.Module;
@@ -45,6 +47,16 @@ public class CacheServerRuntimeProvider extends ServerRuntimeProvider {
     protected Collection<? extends Module> getExtraModules() {
         Collection<Module> modules = new ArrayList<>(super.getExtraModules());
         modules.add(new JCacheModule());
+
+        String configURI;
+        try {
+            configURI = getClass().getResource("/eh-cache.xml").toURI().toString();
+        } catch (URISyntaxException e) {
+            throw new CayenneRuntimeException("Unable to resolve ehcache config resource URI.");
+        }
+
+        modules.add(binder -> JCacheModule
+                .contributeJCacheProviderConfig(binder, configURI));
         return modules;
     }
 }
diff --git a/cayenne-jcache/src/test/resources/eh-cache.xml b/cayenne-jcache/src/test/resources/eh-cache.xml
new file mode 100644
index 0000000..60f4455
--- /dev/null
+++ b/cayenne-jcache/src/test/resources/eh-cache.xml
@@ -0,0 +1,10 @@
+<config xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
+        xmlns='http://www.ehcache.org/v3'
+        xsi:schemaLocation="http://www.ehcache.org/v3 http://www.ehcache.org/schema/ehcache-core-3.0.xsd">
+
+    <cache alias="g1">
+        <heap unit="entries">100</heap>
+    </cache>
+
+</config>
+