Author: ssievers
Date: Fri Apr 27 17:38:22 2012
New Revision: 1331512
URL: http://svn.apache.org/viewvc?rev=1331512&view=rev
Log:
SHINDIG-1746 | Update EhCache configuration to use pooled resources for cache
sizing
Added:
shindig/trunk/java/common/src/main/resources/org/apache/shindig/common/cache/ehcache/SizeOfFilter.txt
Modified:
shindig/trunk/UPGRADING
shindig/trunk/java/common/conf/shindig.properties
shindig/trunk/java/common/src/main/java/org/apache/shindig/common/cache/ehcache/EhCacheCacheProvider.java
shindig/trunk/java/common/src/main/java/org/apache/shindig/common/cache/ehcache/EhConfiguredCache.java
shindig/trunk/java/common/src/main/resources/org/apache/shindig/common/cache/ehcache/ehcacheConfig.xml
shindig/trunk/java/common/src/test/java/org/apache/shindig/common/cache/ehcache/EhCacheCacheProviderTest.java
shindig/trunk/pom.xml
Modified: shindig/trunk/UPGRADING
URL:
http://svn.apache.org/viewvc/shindig/trunk/UPGRADING?rev=1331512&r1=1331511&r2=1331512&view=diff
==============================================================================
--- shindig/trunk/UPGRADING (original)
+++ shindig/trunk/UPGRADING Fri Apr 27 17:38:22 2012
@@ -13,7 +13,7 @@ details.
* closure-compiler (new) r1592
* commons-codec 1.5 -> 1.6
* commons-lang to commons-lang3 3.1
-* ehcache 2.3.2 -> 2.5.0
+* ehcache 2.3.2 -> 2.5.2
* guava r09->11.0.1
* guice 2.0->3.0
* htmlunit 2.8->2.9
Modified: shindig/trunk/java/common/conf/shindig.properties
URL:
http://svn.apache.org/viewvc/shindig/trunk/java/common/conf/shindig.properties?rev=1331512&r1=1331511&r2=1331512&view=diff
==============================================================================
--- shindig/trunk/java/common/conf/shindig.properties (original)
+++ shindig/trunk/java/common/conf/shindig.properties Fri Apr 27 17:38:22 2012
@@ -96,12 +96,12 @@ shindig.flash.min-version = 9.0.115
# Configuration for template rewriter
shindig.template-rewrite.extension-tag-namespace=http://ns.opensocial.org/2009/extensions
-# These values provide default TTLs for HTTP responses that don't use caching
headers.
+# These values provide default TTLs (in ms) for HTTP responses that don't use
caching headers.
shindig.cache.http.defaultTtl=3600000
shindig.cache.http.negativeCacheTtl=60000
# Amount of time after which the entry in cache should be considered for a
refetch for a
-# non-userfacing internal fetch when the response is strict-no-cache.
+# non-userfacing internal fetch when the response is strict-no-cache.
shindig.cache.http.strict-no-cache-resource.refetch-after-ms=-1
# A default refresh interval for XML files, since there is no natural way for
developers to
@@ -120,7 +120,12 @@ shindig.cache.lru.httpResponses.capacity
# The location of the EhCache configuration file.
shindig.cache.ehcache.config=res://org/apache/shindig/common/cache/ehcache/ehcacheConfig.xml
-# True to enable JMX integration with cache stats
+# The location of the filter file for EhCache's SizeOfEngine
+# This gets set as a system property to be consumed by EhCache.
+# Can be a resource on the classpath or a path on the file system.
+shindig.cache.ehcache.sizeof.filter=res://org/apache/shindig/common/cache/ehcache/SizeOfFilter.txt
+
+# true to enable JMX integration.
shindig.cache.ehcache.jmx.enabled=true
# true to enable JMX stats.
Modified:
shindig/trunk/java/common/src/main/java/org/apache/shindig/common/cache/ehcache/EhCacheCacheProvider.java
URL:
http://svn.apache.org/viewvc/shindig/trunk/java/common/src/main/java/org/apache/shindig/common/cache/ehcache/EhCacheCacheProvider.java?rev=1331512&r1=1331511&r2=1331512&view=diff
==============================================================================
---
shindig/trunk/java/common/src/main/java/org/apache/shindig/common/cache/ehcache/EhCacheCacheProvider.java
(original)
+++
shindig/trunk/java/common/src/main/java/org/apache/shindig/common/cache/ehcache/EhCacheCacheProvider.java
Fri Apr 27 17:38:22 2012
@@ -41,20 +41,46 @@ import java.util.logging.Level;
import java.util.logging.Logger;
/**
- * Cache interface based on ehcache
+ * Cache interface based on ehcache.
+ *
* @see <a href="http://www.ehcache.org">http://www.ehcache.org</a>
*/
-public class EhCacheCacheProvider implements CacheProvider,
GuiceServletContextListener.CleanupCapable {
+public class EhCacheCacheProvider implements CacheProvider,
+ GuiceServletContextListener.CleanupCapable {
private static final Logger LOG =
Logger.getLogger(EhCacheCacheProvider.class.getName());
private final CacheManager cacheManager;
private final ConcurrentMap<String, Cache<?, ?>> caches = new
MapMaker().makeMap();
+ /**
+ * @param configPath
+ * the path to the EhCache configuration file
+ * @param filterPath
+ * the path to the EhCache SizeOf engine filter file
+ * @param jmxEnabled
+ * true if JMX should be enabled for EhCache, false otherwise
+ * @param withCacheStats
+ * true if cache statistics should be enabled globally, false
otherwise
+ * @param cleanupHandler
+ * cleanup handler with which to register to ensure proper cache
shutdown via
+ * {@link #cleanup()}
+ * @throws IOException
+ * if there was an issue parsing the given configuration
+ */
@Inject
public EhCacheCacheProvider(@Named("shindig.cache.ehcache.config") String
configPath,
+ @Named("shindig.cache.ehcache.sizeof.filter")
String filterPath,
@Named("shindig.cache.ehcache.jmx.enabled")
boolean jmxEnabled,
@Named("shindig.cache.ehcache.jmx.stats")
boolean withCacheStats,
GuiceServletContextListener.CleanupHandler
cleanupHandler)
throws IOException {
+ // TODO: Setting this system property is currently the only way to hook in
our own filter
+ // https://jira.terracotta.org/jira/browse/EHC-938
+ // https://jira.terracotta.org/jira/browse/EHC-924
+ // Remove res:// and file:// prefixes. EhCache can't understand them.
+ String normalizedFilterPath =
filterPath.replaceFirst(ResourceLoader.RESOURCE_PREFIX, "");
+ normalizedFilterPath =
normalizedFilterPath.replaceFirst(ResourceLoader.FILE_PREFIX, "");
+ System.getProperties().put("net.sf.ehcache.sizeof.filter",
normalizedFilterPath);
+
cacheManager = new CacheManager(getConfiguration(configPath));
create(jmxEnabled, withCacheStats);
cleanupHandler.register(this);
@@ -62,18 +88,21 @@ public class EhCacheCacheProvider implem
/**
* Read the cache configuration from the specified resource.
- * This function is intended to be overrideable to allow for programmatic
- * cache configuration.
+ *
+ * This function is intended to be overrideable to allow for programmatic
cache configuration.
+ *
* @param configPath
- * @return Configuration
+ * the path to the configuration file
+ * @return Configuration the configuration object parsed from the
configuration file
* @throws IOException
+ * if there was an error parsing the given configuration
*/
protected Configuration getConfiguration(String configPath) throws
IOException {
InputStream configStream = ResourceLoader.open(configPath);
return ConfigurationFactory.parseConfiguration(configStream);
}
- public void create(boolean jmxEnabled, boolean withCacheStats) {
+ private void create(boolean jmxEnabled, boolean withCacheStats) {
if (jmxEnabled) {
// register the cache manager with JMX
MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
@@ -82,12 +111,15 @@ public class EhCacheCacheProvider implem
}
/**
- * perform a shutdown
+ * Perform a shutdown of the underlying cache manager.
*/
public void cleanup() {
cacheManager.shutdown();
}
+ /**
+ * {@inheritDoc}
+ */
@SuppressWarnings("unchecked")
public <K, V> Cache<K, V> createCache(String name) {
if (!caches.containsKey(Preconditions.checkNotNull(name))) {
Modified:
shindig/trunk/java/common/src/main/java/org/apache/shindig/common/cache/ehcache/EhConfiguredCache.java
URL:
http://svn.apache.org/viewvc/shindig/trunk/java/common/src/main/java/org/apache/shindig/common/cache/ehcache/EhConfiguredCache.java?rev=1331512&r1=1331511&r2=1331512&view=diff
==============================================================================
---
shindig/trunk/java/common/src/main/java/org/apache/shindig/common/cache/ehcache/EhConfiguredCache.java
(original)
+++
shindig/trunk/java/common/src/main/java/org/apache/shindig/common/cache/ehcache/EhConfiguredCache.java
Fri Apr 27 17:38:22 2012
@@ -27,11 +27,23 @@ import net.sf.ehcache.Element;
/**
* Produces a cache configured from ehcache.
+ *
+ * @param <K> the type of key used to cache elements
+ * @param <V> the type of element stored in this cache
*/
public class EhConfiguredCache<K, V> implements Cache<K, V> {
private net.sf.ehcache.Cache cache;
+ /**
+ * Create a new EhCache cache with the given name for the given cache
manager if one does not
+ * already exist.
+ *
+ * @param cacheName
+ * the name to use for the cache
+ * @param cacheManager
+ * the cache manager in which to create the cache
+ */
public EhConfiguredCache(String cacheName, CacheManager cacheManager) {
synchronized (cacheManager) {
cache = cacheManager.getCache(Preconditions.checkNotNull(cacheName));
@@ -45,10 +57,16 @@ public class EhConfiguredCache<K, V> imp
}
}
+ /**
+ * {@inheritDoc}
+ */
public void addElement(K key, V value) {
cache.put(new Element(key, value));
}
+ /**
+ * {@inheritDoc}
+ */
@SuppressWarnings("unchecked")
public V getElement(K key) {
Element cacheElement = cache.get(key);
@@ -58,6 +76,9 @@ public class EhConfiguredCache<K, V> imp
return null;
}
+ /**
+ * {@inheritDoc}
+ */
@SuppressWarnings("unchecked")
public V removeElement(K key) {
Object value = getElement(key);
@@ -65,9 +86,15 @@ public class EhConfiguredCache<K, V> imp
return (V) value;
}
+ /**
+ * {@inheritDoc}
+ */
public long getCapacity() {
- return cache.getCacheConfiguration().getMaxElementsInMemory() +
- cache.getCacheConfiguration().getMaxElementsOnDisk();
+ // EhCache returns 0 to represent an unbounded cache, where the Cache
interface expects -1
+ // EhCache also returns 0 when using resource pooling as a count-based
capacity does not apply
+ long totalCapacity = cache.getCacheConfiguration().getMaxEntriesLocalHeap()
+ + cache.getCacheConfiguration().getMaxEntriesLocalDisk();
+ return totalCapacity == 0 ? -1 : totalCapacity;
}
/**
Added:
shindig/trunk/java/common/src/main/resources/org/apache/shindig/common/cache/ehcache/SizeOfFilter.txt
URL:
http://svn.apache.org/viewvc/shindig/trunk/java/common/src/main/resources/org/apache/shindig/common/cache/ehcache/SizeOfFilter.txt?rev=1331512&view=auto
==============================================================================
---
shindig/trunk/java/common/src/main/resources/org/apache/shindig/common/cache/ehcache/SizeOfFilter.txt
(added)
+++
shindig/trunk/java/common/src/main/resources/org/apache/shindig/common/cache/ehcache/SizeOfFilter.txt
Fri Apr 27 17:38:22 2012
@@ -0,0 +1,27 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+# Instead of using the @IgnoreSizeOf annotation to mark resources that should
be ignored by
+# EhCache's sizeof engine, one can list those references in this file.
+# See
http://ehcache.org/documentation/configuration/cache-size#sizing-of-cached-entries
+
+# FeatureBundle and FeatureResources that are shared and should be ignored.
+org.apache.shindig.gadgets.js.JsContent.bundle
+org.apache.shindig.gadgets.js.JsContent.resource
+
+# This is the injected singleton instance of the HttpFetcher.
+org.apache.shindig.gadgets.features.FeatureResourceLoader$UriResource.fetcher
Modified:
shindig/trunk/java/common/src/main/resources/org/apache/shindig/common/cache/ehcache/ehcacheConfig.xml
URL:
http://svn.apache.org/viewvc/shindig/trunk/java/common/src/main/resources/org/apache/shindig/common/cache/ehcache/ehcacheConfig.xml?rev=1331512&r1=1331511&r2=1331512&view=diff
==============================================================================
---
shindig/trunk/java/common/src/main/resources/org/apache/shindig/common/cache/ehcache/ehcacheConfig.xml
(original)
+++
shindig/trunk/java/common/src/main/resources/org/apache/shindig/common/cache/ehcache/ehcacheConfig.xml
Fri Apr 27 17:38:22 2012
@@ -17,17 +17,43 @@ KIND, either express or implied. See th
specific language governing permissions and limitations
under the License.
-->
+
+<!--
+ The one and only cache manager for Shindig.
+
+ Allot a modest amount of heap to be shared by all caches.
+
+ By default none of the caches will persist or overflow to disk.
+
+ Statistics are turned on for every cache by default. This affects cache
performance.
+ To turn stats off you can use shindig.cache.ehcache.jmx.stats in
shindig.properties.
+
+ See http://ehcache.org/ehcache.xsd for more information on valid
configuration.
+-->
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:noNamespaceSchemaLocation="ehcache.xsd" updateCheck="false">
+ xsi:noNamespaceSchemaLocation="ehcache.xsd"
+ updateCheck="false"
+ maxBytesLocalHeap="50m"
+ name="ShindigCM">
+
+ <!--
+ The directory where any caches configured as diskPersistent or
overflowToDisk
+ will end up.
+ -->
+ <diskStore path="java.io.tmpdir"/>
+
+ <!-- Default sizeOfPolicy for computing the size of cache elements in memory
-->
+ <sizeOfPolicy maxDepth="1000" maxDepthExceededBehavior="abort"/>
+
<!--
Mandatory Default Cache configuration. These settings will be applied to
caches
created programmatically using CacheManager.add(String cacheName).
-
+
The defaultCache has an implicit name "default" which is a reserved cache
name.
-->
<defaultCache
- maxElementsInMemory="1000"
+ statistics="true"
eternal="false"
timeToIdleSeconds="300"
timeToLiveSeconds="600"
@@ -40,14 +66,14 @@ under the License.
allow us to cache the objects indefinitely when using an LFU or LRU cache.
-->
<cache name="gadgetSpecs"
- maxElementsInMemory="1000"
+ statistics="true"
eternal="true"
overflowToDisk="false"
diskPersistent="false"
memoryStoreEvictionPolicy="LFU"/>
<cache name="messageBundles"
- maxElementsInMemory="1000"
+ statistics="true"
eternal="true"
overflowToDisk="false"
diskPersistent="false"
@@ -55,7 +81,7 @@ under the License.
<!-- Used to cache parsed HTML DOMs based on their content -->
<cache name="parsedDocuments"
- maxElementsInMemory="1000"
+ statistics="true"
eternal="true"
overflowToDisk="false"
diskPersistent="false"
@@ -63,18 +89,20 @@ under the License.
<!-- Used to cache parsed CSS DOMs based on their content -->
<cache name="parsedCss"
- maxElementsInMemory="1000"
+ statistics="true"
eternal="true"
overflowToDisk="false"
diskPersistent="false"
memoryStoreEvictionPolicy="LFU"/>
- <!--
+ <!--
This configuration is only suitable for a modest sized HTTP cache.
You should configure a shared cache for production use.
+ Give this cache 30% of the local heap by default.
-->
<cache name="httpResponses"
- maxElementsInMemory="10000"
+ maxBytesLocalHeap="30%"
+ statistics="true"
eternal="true"
overflowToDisk="false"
diskPersistent="false"
@@ -82,19 +110,33 @@ under the License.
<!-- Used to cache parsed expressions based on their content -->
<cache name="expressions"
- maxElementsInMemory="1000"
+ statistics="true"
eternal="true"
overflowToDisk="false"
diskPersistent="false"
memoryStoreEvictionPolicy="LFU"/>
- <!-- Used to cache cajoled documents based on their content -->
- <cache name="cajoledDocuments"
- maxElementsInMemory="1000"
+ <!-- Used to cache cajoled modules based on their content -->
+ <cache name="cajoledModules"
+ statistics="true"
eternal="false"
timeToIdleSeconds="300"
timeToLiveSeconds="600"
overflowToDisk="false"
diskPersistent="false"
memoryStoreEvictionPolicy="LFU"/>
+
+ <!-- Used to cache Gadget Feature JS -->
+ <cache name="FeatureJsCache"
+ statistics="true"
+ eternal="true"
+ overflowToDisk="false"
+ diskPersistent="false"
+ memoryStoreEvictionPolicy="LFU">
+ <!--
+ The elements stored in this cache are complex and the default
sizeOfPolicy
+ is insufficient
+ -->
+ <sizeOfPolicy maxDepth="5000" maxDepthExceededBehavior="abort"/>
+ </cache>
</ehcache>
Modified:
shindig/trunk/java/common/src/test/java/org/apache/shindig/common/cache/ehcache/EhCacheCacheProviderTest.java
URL:
http://svn.apache.org/viewvc/shindig/trunk/java/common/src/test/java/org/apache/shindig/common/cache/ehcache/EhCacheCacheProviderTest.java?rev=1331512&r1=1331511&r2=1331512&view=diff
==============================================================================
---
shindig/trunk/java/common/src/test/java/org/apache/shindig/common/cache/ehcache/EhCacheCacheProviderTest.java
(original)
+++
shindig/trunk/java/common/src/test/java/org/apache/shindig/common/cache/ehcache/EhCacheCacheProviderTest.java
Fri Apr 27 17:38:22 2012
@@ -35,7 +35,10 @@ public class EhCacheCacheProviderTest {
@BeforeClass
public static void setup() throws Exception {
defaultProvider = new EhCacheCacheProvider(
- "res://org/apache/shindig/common/cache/ehcache/ehcacheConfig.xml",
true, true,
+ "res://org/apache/shindig/common/cache/ehcache/ehcacheConfig.xml",
+ "org/apache/shindig/common/cache/ehcache/SizeOfFilter.txt",
+ true,
+ true,
new GuiceServletContextListener.CleanupHandler());
}
Modified: shindig/trunk/pom.xml
URL:
http://svn.apache.org/viewvc/shindig/trunk/pom.xml?rev=1331512&r1=1331511&r2=1331512&view=diff
==============================================================================
--- shindig/trunk/pom.xml (original)
+++ shindig/trunk/pom.xml Fri Apr 27 17:38:22 2012
@@ -1719,7 +1719,7 @@
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache-core</artifactId>
- <version>2.5.0</version>
+ <version>2.5.2</version>
</dependency>
<dependency>
<groupId>com.thoughtworks.xstream</groupId>