Merged BRANCHES/DEV/mward/clustering_p1 to HEAD:

41454: ALF-15881: Disabling clustering in community
   41500: ALF-15883: Move configuration files
   41503: ALF-15884: Move cluster package to enterprise repository project
   41504: ALF-15884: Move cluster package to enterprise repository project
   41519: ALF-15886: References/definition for "hazelcastInstanceFactory" must be in enterprise repo only
   41523: ALF-15886: References/definition for "hazelcastInstanceFactory" must be in enterprise repo only
   41525: ALF-15886: References/definition for "hazelcastInstanceFactory" must be in enterprise repo only
   41527: ALF-15886: References/definition for "hazelcastInstanceFactory" must be in enterprise repo only
   41530: ALF-15886: remove import for class no longer in project.
   41532: ALF-15887: LockStoreFactoryImpl must be separated into community and enterprise versions
   41535: ALF-15883: Move configuration files
   41561: ALF-15886: factory class to create key fileserver config beans.
   41578: ALF-15888: separate transactional and shared cache bean definitions.
   41623: ALF-15888: first pass at DefaultSimpleCache implementation.
   41646: ALF-15888: move ehcache-default.xml
   41651: ALF-15888: update javadoc to reflect changes
   41762: ALF-15888: improve cache test to prove that null values are stored correctly.
   41812: ALF-15888: added new cache provider for use by hibernate: DefaultCacheProvider.
   41830: ALF-15888: make DefaultSimpleCache BeanNameAware to help with debugging etc.
   41831: ALF-15888: missing file from commit - adds enterprise override capability for hibernate-cfg.properties
   41850: ALF-15888: move tickets cache to cache-context.xml
   41857: ALF-15888: make RemoteAlfrescoTicketServiceImpl cache implementation agnostic.
   41866: ALF-15888: extract caches from fileservers and web-client and provide enterprise overrides
   41881: ALF-15888: replace use of EhCacheAdapter with DefaultSimpleCache and SimpleCache interface.
   41884: ALF-15888: added DefaultSimpleCache configuration to CachingContentStore sample XML.
   41885: ALF-15888: move EhCacheAdapter to new package.
   41886: ALF-15888: correct absolute class names in config for EhCacheAdapter.
   41892: ALF-15888: fix CachingContentStore tests.
   41897: ALF-15888: move CacheTest and config to new package.
   41898: ALF-15888: remove redundant directory
   41899: ALF-15889: move EhCacheManagerFactoryBean to new package.
   41902: ALF-15889: moved EhCacheTracerJob to new package.
   41913: ALF-15889: move InternalEhCacheManagerFactoryBean to new package.
   41916: ALF-15889: move AlfrescoCacheManagerPeerProviderFactory to new package.
   41937: ALF-15889: decouple TransactionalCache from EhCache
   41966: ALF-15889: decouple RetryingTransactionHelper from ehcache.
   41989: ALF-15889: added ContextListener test.
   41996: ALF-15889: moved cache test to its own class.
   41998: ALF-15889: move ehcache jars.
   41999: ALF-15889: modify .classpath to reflect jar moves.
   42037: ALF-15889: update poms to reflect lib moves.
   42038: ALF-15889: add eclipse library reference to enterprise projects.
   42093: ALF-15916: moved core properties to enterprise
   42114: ALF-15888: externalized cache sizes to repository.properties.
   42127: ALF-16136: move re-indexing configuration
   42140: ALF-16136: move cluster check property.
   42186: ALF-15889: removing seemingly redundant test config file.
   42187: ALF-15888: tidy up config changes.
   42189: ALF-15888: cleanup config
   42190: ALF-15888: config cleanup
   42191: ALF-15888: config cleanup
   42198: ALF-16136: restored lost property



git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@42210 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Matt Ward
2012-10-01 12:17:53 +00:00
parent 131080b564
commit 511af90d5c
79 changed files with 1471 additions and 7954 deletions

View File

@@ -1,77 +0,0 @@
/*
* Copyright (C) 2005-2010 Alfresco Software Limited.
*
* This file is part of Alfresco
*
* Alfresco is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Alfresco is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
*/
package org.alfresco.repo.cache;
import java.util.Properties;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.distribution.CacheManagerPeerProvider;
import net.sf.ehcache.distribution.CacheManagerPeerProviderFactory;
import net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* Alfresco's <tt>CacheManagerPeerProviderFactory</tt> that defers to the community or
* enterprise factories.
*
* @author Derek Hulley
* @since 3.1
*/
public class AlfrescoCacheManagerPeerProviderFactory extends CacheManagerPeerProviderFactory
{
private static Log logger = LogFactory.getLog(AlfrescoCacheManagerPeerProviderFactory.class);
@Override
public CacheManagerPeerProvider createCachePeerProvider(CacheManager cacheManager, Properties properties)
{
CacheManagerPeerProviderFactory factory = null;
try
{
@SuppressWarnings("unchecked")
Class clazz = Class.forName("org.alfresco.enterprise.repo.cache.cluster.RMICacheManagerPeerProvider$Factory");
factory = (CacheManagerPeerProviderFactory) clazz.newInstance();
}
catch (ClassNotFoundException e)
{
// Entirely expected if the Enterprise-level code is not present
}
catch (Throwable e)
{
logger.error("Failed to instantiate RMICacheManagerPeerProvider factory.", e);
}
finally
{
if (factory == null)
{
// Use EHCache's default implementation
factory = new RMICacheManagerPeerProviderFactory();
}
}
if (logger.isDebugEnabled())
{
logger.debug("Using peer provider factory: " + factory.getClass().getName());
}
return factory.createCachePeerProvider(cacheManager, properties);
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,78 @@
/*
* Copyright (C) 2005-2012 Alfresco Software Limited.
*
* This file is part of Alfresco
*
* Alfresco is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Alfresco is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
*/
package org.alfresco.repo.cache;
import java.io.Serializable;
import java.util.Properties;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.cache.Cache;
import org.hibernate.cache.CacheException;
import org.hibernate.cache.CacheProvider;
import org.hibernate.cache.Timestamper;
/**
* A simple {@link CacheProvider} that allows Hibernate to create {@link DefaultSimpleCache}
* based caches.
*
* @author Matt Ward
*/
public class DefaultCacheProvider implements CacheProvider
{
private final static Log log = LogFactory.getLog(DefaultCacheProvider.class);
// TODO: setup in spring (SystemPropertiesSetterBean)
private int defaultMaxItems = 500;
@Override
public Cache buildCache(String regionName, Properties properties) throws CacheException
{
if (log.isDebugEnabled())
{
log.debug("building cache for regionName=" + regionName + ", with properties: " + properties);
}
DefaultSimpleCache<Serializable, Object> cache = new DefaultSimpleCache<Serializable, Object>();
cache.setMaxItems(defaultMaxItems);
Cache hibCache = new HibernateSimpleCacheAdapter(cache, regionName);
return hibCache;
}
@Override
public long nextTimestamp()
{
return Timestamper.next();
}
@Override
public void start(Properties properties) throws CacheException
{
}
@Override
public void stop()
{
}
@Override
public boolean isMinimalPutsEnabledByDefault()
{
return false;
}
}

View File

@@ -0,0 +1,126 @@
/*
* Copyright (C) 2005-2012 Alfresco Software Limited.
*
* This file is part of Alfresco
*
* Alfresco is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Alfresco is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
*/
package org.alfresco.repo.cache;
import java.io.Serializable;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import org.springframework.beans.factory.BeanNameAware;
/**
* {@link SimpleCache} implementation backed by a {@link LinkedHashMap}.
*
* @author Matt Ward
*/
public final class DefaultSimpleCache<K extends Serializable, V extends Object>
implements SimpleCache<K, V>, BeanNameAware
{
private final Map<K, V> map;
private int maxItems = 0;
private String cacheName;
public DefaultSimpleCache()
{
// Create a LinkedHashMap with accessOrder true, i.e. iteration order
// will be least recently accessed first. Eviction policy will therefore be LRU.
// The map will have a bounded size determined by the maxItems member variable.
map = (Map<K, V>) Collections.synchronizedMap(new LinkedHashMap<K, V>(16, 0.75f, true) {
private static final long serialVersionUID = 1L;
@Override
protected boolean removeEldestEntry(Entry<K, V> eldest)
{
return maxItems > 0 && size() > maxItems;
}
});
}
@Override
public boolean contains(K key)
{
return map.containsKey(key);
}
@Override
public Collection<K> getKeys()
{
return map.keySet();
}
@Override
public V get(K key)
{
return map.get(key);
}
@Override
public void put(K key, V value)
{
map.put(key, value);
}
@Override
public void remove(K key)
{
map.remove(key);
}
@Override
public void clear()
{
map.clear();
}
@Override
public String toString()
{
return "DefaultSimpleCache[maxItems=" + maxItems + ", cacheName=" + cacheName + "]";
}
/**
* Sets the maximum number of items that the cache will hold. Setting
* this value will cause the cache to be emptied. A value of zero
* will allow the cache to grow unbounded.
*
* @param maxItems
*/
public void setMaxItems(int maxItems)
{
synchronized(map)
{
map.clear();
this.maxItems = maxItems;
}
}
/**
* Since there are many cache instances, it is useful to be able to associate
* a name with each one.
*
* @param cacheName Set automatically by Spring, but can be set manually if required.
*/
@Override
public void setBeanName(String cacheName)
{
this.cacheName = cacheName;
}
}

View File

@@ -0,0 +1,157 @@
/*
* Copyright (C) 2005-2012 Alfresco Software Limited.
*
* This file is part of Alfresco
*
* Alfresco is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Alfresco is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
*/
package org.alfresco.repo.cache;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import java.util.Iterator;
import org.junit.Before;
import org.junit.Test;
/**
* Tests for the DefaultSimpleCache class.
*
* @author Matt Ward
*/
public class DefaultSimpleCacheTest
{
private DefaultSimpleCache<Integer, String> cache;
@Before
public void setUp()
{
cache = new DefaultSimpleCache<Integer, String>();
}
@Test
public void unboundedSizeCache()
{
cache.put(1, "1");
cache.put(2, "2");
cache.put(3, "3");
cache.put(4, "4");
cache.put(5, "5");
assertEquals("1", cache.get(1));
assertEquals("2", cache.get(2));
assertEquals("3", cache.get(3));
assertEquals("4", cache.get(4));
assertEquals("5", cache.get(5));
}
@Test
public void boundedSizeCache()
{
// We'll only keep the LAST 3 items
cache.setMaxItems(3);
cache.put(1, "1");
cache.put(2, "2");
cache.put(3, "3");
cache.put(4, "4");
cache.put(5, "5");
// Lost the first item
assertNull(cache.get(1));
assertFalse(cache.contains(1));
// Lost the second item
assertNull(cache.get(2));
assertFalse(cache.contains(2));
// Last three are still present
assertEquals("3", cache.get(3));
assertEquals("4", cache.get(4));
assertEquals("5", cache.get(5));
}
@Test
public void canStoreNullValues()
{
cache.put(2, null);
assertEquals(null, cache.get(2));
// Check that the key has an entry against it.
assertTrue(cache.contains(2));
// Ensure that a key that has not been assigned is discernable
// from a key that has been assigned a null value.
assertEquals(null, cache.get(4));
assertFalse(cache.contains(4));
}
@Test
public void canRemoveItems()
{
cache.put(1, "hello");
cache.put(2, "world");
assertEquals("hello", cache.get(1));
assertEquals("world", cache.get(2));
cache.remove(2);
assertEquals("hello", cache.get(1));
assertEquals(null, cache.get(2));
assertEquals(false, cache.contains(2));
}
@Test
public void canClearItems()
{
cache.put(1, "hello");
cache.put(2, "world");
assertEquals("hello", cache.get(1));
assertEquals("world", cache.get(2));
cache.clear();
assertEquals(null, cache.get(1));
assertEquals(false, cache.contains(1));
assertEquals(null, cache.get(2));
assertEquals(false, cache.contains(2));
}
@Test
public void canGetKeys()
{
cache.put(3, "blue");
cache.put(12, "red");
cache.put(43, "olive");
Iterator<Integer> it = cache.getKeys().iterator();
assertEquals(3, it.next().intValue());
assertEquals(12, it.next().intValue());
assertEquals(43, it.next().intValue());
assertFalse("There should be no more keys.", it.hasNext());
}
@Test
public void clearUponSetMaxItems()
{
cache.put(1, "1");
assertTrue(cache.contains(1));
cache.setMaxItems(10);
// The item should have gone.
assertFalse(cache.contains(1));
}
}

View File

@@ -1,121 +0,0 @@
/*
* Copyright (C) 2005-2010 Alfresco Software Limited.
*
* This file is part of Alfresco
*
* Alfresco is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Alfresco is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
*/
package org.alfresco.repo.cache;
import java.io.Serializable;
import java.util.Collection;
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheException;
import net.sf.ehcache.Element;
import org.alfresco.error.AlfrescoRuntimeException;
/**
* A thin adapter for <b>Ehcache</b> support.
* <p>
* Thread-safety is taken care of by the underlying <b>Ehcache</b>
* instance.
*
* @see org.springframework.cache.ehcache.EhCacheFactoryBean
* @see org.springframework.cache.ehcache.EhCacheManagerFactoryBean
*
* @author Derek Hulley
*/
public class EhCacheAdapter<K extends Serializable, V extends Object>
implements SimpleCache<K, V>
{
private net.sf.ehcache.Cache cache;
public EhCacheAdapter()
{
}
/**
* @param cache the backing Ehcache instance
*/
public void setCache(Cache cache)
{
this.cache = cache;
}
public boolean contains(K key)
{
try
{
return (cache.getQuiet(key) != null);
}
catch (CacheException e)
{
throw new AlfrescoRuntimeException("contains failed", e);
}
}
@SuppressWarnings("unchecked")
public Collection<K> getKeys()
{
return cache.getKeys();
}
@SuppressWarnings("unchecked")
public V get(K key)
{
try
{
Element element = cache.get(key);
if (element != null)
{
return (V) element.getObjectValue();
}
else
{
return null;
}
}
catch (IllegalStateException ie)
{
throw new AlfrescoRuntimeException("Failed to get from EhCache as state invalid: \n" +
" state: " + cache.getStatus() + "\n" +
" key: " + key,
ie);
}
catch (CacheException e)
{
throw new AlfrescoRuntimeException("Failed to get from EhCache: \n" +
" key: " + key,
e);
}
}
public void put(K key, V value)
{
Element element = new Element(key, value);
cache.put(element);
}
public void remove(K key)
{
cache.remove(key);
}
public void clear()
{
cache.removeAll();
}
}

View File

@@ -1,111 +0,0 @@
/*
* Copyright (C) 2005-2010 Alfresco Software Limited.
*
* This file is part of Alfresco
*
* Alfresco is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Alfresco is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
*/
package org.alfresco.repo.cache;
import java.io.IOException;
import java.net.URL;
import net.sf.ehcache.CacheException;
import net.sf.ehcache.CacheManager;
import org.alfresco.error.AlfrescoRuntimeException;
import org.alfresco.util.PropertyCheck;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.core.io.Resource;
/**
* This is virtually a copy of the Springframework version, with the exception
* that it uses the newer constructors for the <code>EHCacheManager</code>
* instances.
*
* @author Derek Hulley
*/
public class EhCacheManagerFactoryBean implements FactoryBean, InitializingBean, DisposableBean
{
static
{
// https://jira.terracotta.org/jira/browse/EHC-652
// Force old-style LruMemoryStore
// System.setProperty("net.sf.ehcache.use.classic.lru", "true");
}
protected final Log logger = LogFactory.getLog(EhCacheManagerFactoryBean.class);
private Resource configLocation;
private CacheManager cacheManager;
/**
*
* @param configLocation a resource location using the <b>file:</b> or <b>classpath:</b> prefix
*/
public void setConfigLocation(Resource configLocation)
{
this.configLocation = configLocation;
}
public void afterPropertiesSet() throws IOException, CacheException
{
PropertyCheck.mandatory(this, "configLocation", configLocation);
// Double-check the config location or EHCache will throw an NPE
try
{
URL configUrl = this.configLocation.getURL();
logger.info("Initializing EHCache CacheManager using URL: " + configLocation);
this.cacheManager = new CacheManager(configUrl);
}
catch (IOException e)
{
throw new AlfrescoRuntimeException("Unabled to read EHCache configuration file at " + configLocation, e);
}
}
public Object getObject()
{
return this.cacheManager;
}
@SuppressWarnings("unchecked")
public Class getObjectType()
{
return (this.cacheManager != null ? this.cacheManager.getClass() : CacheManager.class);
}
public boolean isSingleton()
{
return true;
}
public void destroy()
{
logger.info("Shutting down EHCache CacheManager");
if(logger.isDebugEnabled()) {
String[] caches = this.cacheManager.getCacheNames();
for(String cache : caches) {
logger.debug("Shutting down EHCache instance " + cache);
}
}
this.cacheManager.shutdown();
}
}

View File

@@ -1,250 +0,0 @@
/*
* Copyright (C) 2005-2010 Alfresco Software Limited.
*
* This file is part of Alfresco
*
* Alfresco is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Alfresco is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
*/
package org.alfresco.repo.cache;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.List;
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheException;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;
import net.sf.ehcache.Status;
import net.sf.ehcache.statistics.LiveCacheStatistics;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
/**
* Analyzes the size of EHCache caches used.
* <p>
* To activate this class, call the {@link #init()} method.
*
* @author Derek Hulley
*/
public class EhCacheTracerJob implements Job
{
private static Log logger = LogFactory.getLog(EhCacheTracerJob.class);
private CacheManager cacheManager;
/**
* Set the cache manager to analyze. The default cache manager will be analyzed
* if this property is not set.
*
* @param cacheManager optional cache manager to analyze
*/
public void setCacheManager(CacheManager cacheManager)
{
this.cacheManager = cacheManager;
}
public void execute(JobExecutionContext context) throws JobExecutionException
{
try
{
execute();
}
catch (Throwable e)
{
logger.error("Exception during execution of job", e);
}
}
private void execute() throws Exception
{
if (cacheManager == null)
{
cacheManager = InternalEhCacheManagerFactoryBean.getInstance();
}
long maxHeapSize = Runtime.getRuntime().maxMemory();
long allCachesTotalSize = 0L;
double estimatedMaxSize = 0L;
// get all the caches
String[] cacheNames = cacheManager.getCacheNames();
logger.debug("Dumping EHCache info:");
boolean analyzeAll = true;
for (String cacheName : cacheNames)
{
Cache cache = cacheManager.getCache(cacheName);
//cache.getCacheEventNotificationService().
// cache.registerCacheUsageListener(cacheUsageListener)
if (cache == null) // perhaps a temporary cache
{
continue;
}
Log cacheLogger = LogFactory.getLog(this.getClass().getName() + "." + cacheName);
// log each cache to its own logger
// dump
if (cacheLogger.isDebugEnabled())
{
CacheAnalysis analysis = new CacheAnalysis(cache);
cacheLogger.debug(analysis);
// get the size
allCachesTotalSize += analysis.getSize();
double cacheEstimatedMaxSize = analysis.getEstimatedMaxSize();
estimatedMaxSize += (Double.isNaN(cacheEstimatedMaxSize) || Double.isInfinite(cacheEstimatedMaxSize))
? 0.0
: cacheEstimatedMaxSize;
}
else
{
analyzeAll = false;
}
}
if (analyzeAll)
{
// check the size
double sizePercentage = (double)allCachesTotalSize / (double)maxHeapSize * 100.0;
double maxSizePercentage = estimatedMaxSize / (double)maxHeapSize * 100.0;
String msg = String.format(
"EHCaches currently consume %5.2f MB or %3.2f percent of system VM size. \n" +
"The estimated maximum size is %5.2f MB or %3.2f percent of system VM size.",
(double)allCachesTotalSize / 1024.0 / 1024.0,
sizePercentage,
estimatedMaxSize / 1024.0 / 1024.0,
maxSizePercentage);
logger.debug(msg);
}
}
private static class CacheAnalysis
{
private Cache cache;
private long size = 0L;
double sizeMB;
long maxSize;
long currentSize;
long hitCount;
long missCount;
double percentageFull;
double estMaxSize;
public CacheAnalysis(Cache cache) throws CacheException
{
this.cache = cache;
if (this.cache.getStatus().equals(Status.STATUS_ALIVE))
{
try
{
calculateSize();
}
catch (Throwable e)
{
// just ignore
}
}
}
public synchronized long getSize()
{
return size;
}
public synchronized double getEstimatedMaxSize()
{
return estMaxSize;
}
@SuppressWarnings("unchecked")
private synchronized void calculateSize() throws CacheException
{
// calculate the cache deep size - EHCache 1.1 is always returning 0L
List<Serializable> keys = cache.getKeys();
// only count a maximum of 1000 entities
int count = 0;
for (Serializable key : keys)
{
Element element = cache.get(key);
size += getSize(element);
count++;
if (count >= 50)
{
break;
}
}
// the size must be multiplied by the ratio of the count to actual size
size = count > 0 ? (long) ((double)size * ((double)keys.size()/(double)count)) : 0L;
sizeMB = (double)size/1024.0/1024.0;
LiveCacheStatistics statistics = cache.getLiveCacheStatistics();
maxSize = cache.getCacheConfiguration().getMaxElementsInMemory();
currentSize = cache.getMemoryStoreSize();
hitCount = statistics.getCacheHitCount();
missCount = statistics.getCacheMissCount();
percentageFull = (double)currentSize / (double)maxSize * 100.0;
estMaxSize = size / (double) currentSize * (double) maxSize;
}
private long getSize(Serializable obj)
{
ByteArrayOutputStream bout = new ByteArrayOutputStream(1024);
ObjectOutputStream oos = null;
try
{
oos = new ObjectOutputStream(bout);
oos.writeObject(obj);
return bout.size();
}
catch (IOException e)
{
logger.warn("Deep size calculation failed for cache: \n" + cache);
return 0L;
}
finally
{
try { oos.close(); } catch (IOException e) {}
}
}
public String toString()
{
double sizeMB = (double)getSize()/1024.0/1024.0;
LiveCacheStatistics statistics = cache.getLiveCacheStatistics();
long maxSize = cache.getCacheConfiguration().getMaxElementsInMemory();
long currentSize = cache.getMemoryStoreSize();
long hitCount = statistics.getCacheHitCount();
long totalMissCount = statistics.getCacheMissCount() + statistics.getCacheMissCountExpired();
double hitRatio = (double)hitCount / (double)(totalMissCount + hitCount) * 100.0;
double percentageFull = (double)currentSize / (double)maxSize * 100.0;
double estMaxSize = sizeMB / (double) currentSize * (double) maxSize;
StringBuilder sb = new StringBuilder(512);
sb.append("\n")
.append("===> EHCache: ").append(cache).append("\n")
.append(" Hit Ratio: ").append(String.format("%10.2f percent ", hitRatio ))
.append(" | Hit Count: ").append(String.format("%10d hits ", hitCount ))
.append(" | Miss Count: ").append(String.format("%10d misses ", totalMissCount )).append("\n")
.append(" Deep Size: ").append(String.format("%10.2f MB ", sizeMB ))
.append(" | Current Count: ").append(String.format("%10d entries ", currentSize )).append("\n")
.append(" Percentage used: ").append(String.format("%10.2f percent", percentageFull))
.append(" | Max Count: ").append(String.format("%10d entries ", maxSize )).append("\n")
.append(" Estimated maximum size: ").append(String.format("%10.2f MB ", estMaxSize ));
return sb.toString();
}
}
}

View File

@@ -0,0 +1,154 @@
/*
* Copyright (C) 2005-2012 Alfresco Software Limited.
*
* This file is part of Alfresco
*
* Alfresco is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Alfresco is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
*/
package org.alfresco.repo.cache;
import java.io.Serializable;
import java.util.Map;
import org.hibernate.cache.Cache;
import org.hibernate.cache.CacheException;
import org.hibernate.cache.Timestamper;
/**
* Adapts a {@link SimpleCache} instance for use as a Hibernate {@link Cache}.
*
* @author Matt Ward
*/
public class HibernateSimpleCacheAdapter implements Cache
{
private final SimpleCache<Serializable, Object> cache;
private final String regionName;
/**
* Adapt a
* @param cache
* @param regionName
*/
public HibernateSimpleCacheAdapter(SimpleCache<Serializable, Object> cache, String regionName)
{
this.cache = cache;
this.regionName = regionName;
}
@Override
public Object read(Object key) throws CacheException
{
return cache.get(serializable(key));
}
@Override
public Object get(Object key) throws CacheException
{
return cache.get(serializable(key));
}
@Override
public void put(Object key, Object value) throws CacheException
{
cache.put(serializable(key), value);
}
@Override
public void update(Object key, Object value) throws CacheException
{
cache.put(serializable(key), value);
}
@Override
public void remove(Object key) throws CacheException
{
cache.remove(serializable(key));
}
@Override
public void clear() throws CacheException
{
cache.clear();
}
@Override
public void destroy() throws CacheException
{
// NoOp
}
@Override
public void lock(Object key) throws CacheException
{
// NoOp
}
@Override
public void unlock(Object key) throws CacheException
{
// NoOp
}
@Override
public long nextTimestamp()
{
return Timestamper.next();
}
@Override
public int getTimeout()
{
return Timestamper.ONE_MS * 60000; // 1 minute
}
@Override
public String getRegionName()
{
return regionName;
}
@Override
public long getSizeInMemory()
{
return -1;
}
@Override
public long getElementCountInMemory()
{
return -1;
}
@Override
public long getElementCountOnDisk()
{
return 0;
}
@Override
public Map toMap()
{
throw new UnsupportedOperationException();
}
private Serializable serializable(Object obj)
{
if (!(obj instanceof Serializable))
{
throw new IllegalArgumentException("Object is not Serializable, class=" + obj.getClass().getName());
}
return (Serializable) obj;
}
}

View File

@@ -1,250 +0,0 @@
/*
* Copyright (C) 2005-2010 Alfresco Software Limited.
*
* This file is part of Alfresco
*
* Alfresco is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Alfresco is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
*/
package org.alfresco.repo.cache;
import java.io.FileNotFoundException;
import java.net.URL;
import java.util.Properties;
import net.sf.ehcache.CacheManager;
import org.alfresco.error.AlfrescoRuntimeException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.cache.Cache;
import org.hibernate.cache.CacheException;
import org.hibernate.cache.CacheProvider;
import org.hibernate.cache.EhCache;
import org.hibernate.cache.EhCacheProvider;
import org.hibernate.cache.Timestamper;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.util.ResourceUtils;
/**
* Alfresco-specific cache manager factory.
* <p>
* The purpose of this bean is to provide a common point from which the system-wide
* EHCache <code>CacheManager</code> singleton is created. Hibernate and Spring
* will all pick up the same <code>CacheManager</code> instance. It then becomes
* possible to initialise this instance in whichever way we require, provided it
* is done in a well-known (non-configurable) way.
* <p>
* For Alfresco purposes, there are two files that are looked for:
* <ul>
* <li><b>classpath:alfresco/extension/ehcache-custom.xml</b>, which will take precedence</li>
* <li><b>classpath:alfresco/ehcache-default.xml</b>, which is the default shipped with Alfresco</li>
* </ul>
* <p>
* The EHCache static singleton instance is used but ensuring that all access to the
* instance goes through the required initialization code first.
* <p>
* TODO: Provide mixing of config so that cache definitions in the custom file override
* those in the default file
*
* @see #getInstance()
*
* @author Derek Hulley
*/
public class InternalEhCacheManagerFactoryBean implements FactoryBean<CacheManager>, CacheProvider
{
static
{
// https://jira.terracotta.org/jira/browse/EHC-652
// Force old-style LruMemoryStore
// System.setProperty("net.sf.ehcache.use.classic.lru", "true");
}
public static final String CUSTOM_CONFIGURATION_FILE = "classpath:alfresco/extension/ehcache-custom.xml";
public static final String DEFAULT_CONFIGURATION_FILE = "classpath:alfresco/ehcache-default.xml";
private static Log logger = LogFactory.getLog(InternalEhCacheManagerFactoryBean.class);
/** keep track of the singleton status to avoid work */
private static boolean initialized;
/** the <code>CacheManager</code> */
private static CacheManager cacheManager;
/** used to ensure that the existing Hibernate logic is maintained */
private static EhCacheProvider hibernateEhCacheProvider = new EhCacheProvider();
/**
* Default constructor required by Hibernate. In fact, we anticipate several
* instances of this class to be created.
*/
public InternalEhCacheManagerFactoryBean()
{
}
/**
* News up the singleton cache manager according to the rules set out
* in the class comments.
*/
private static synchronized void initCacheManager()
{
if (initialized)
{
return;
}
try
{
boolean defaultLocation = false;
try
{
URL configUrl = ResourceUtils.getURL(CUSTOM_CONFIGURATION_FILE);
InternalEhCacheManagerFactoryBean.cacheManager = CacheManager.create(configUrl);
}
catch (FileNotFoundException e)
{
// try the alfresco default
URL configUrl = ResourceUtils.getURL(DEFAULT_CONFIGURATION_FILE);
if (configUrl == null)
{
throw new AlfrescoRuntimeException("Missing default cache config: " + DEFAULT_CONFIGURATION_FILE);
}
InternalEhCacheManagerFactoryBean.cacheManager = new CacheManager(configUrl);
defaultLocation = true;
}
// done
if (logger.isDebugEnabled())
{
logger.debug("Created EHCache CacheManager instance: \n" +
" configuration: " + (defaultLocation ? DEFAULT_CONFIGURATION_FILE : CUSTOM_CONFIGURATION_FILE));
}
initialized = true;
}
catch (Throwable e)
{
throw new AlfrescoRuntimeException("EHCache configuration failed", e);
}
}
/**
* @return Returns the properly initialized instance for Alfresco internal use
*
* @see #initCacheManager()
*/
public static CacheManager getInstance()
{
if (!InternalEhCacheManagerFactoryBean.initialized)
{
InternalEhCacheManagerFactoryBean.initCacheManager();
}
return InternalEhCacheManagerFactoryBean.cacheManager;
}
public Cache buildCache(String regionName, Properties properties) throws CacheException
{
CacheManager manager = InternalEhCacheManagerFactoryBean.getInstance();
try
{
net.sf.ehcache.Cache cache = manager.getCache(regionName);
if (cache == null)
{
logger.info("Using default cache configuration: " + regionName);
manager.addCache(regionName);
cache = manager.getCache(regionName);
logger.debug("Started EHCache region: " + regionName);
}
return new EhCache(cache);
}
catch (net.sf.ehcache.CacheException e)
{
throw new CacheException(e);
}
}
/**
* @see #hibernateEhCacheProvider
*/
public boolean isMinimalPutsEnabledByDefault()
{
return false;
}
/**
* @see #hibernateEhCacheProvider
*/
public long nextTimestamp()
{
return Timestamper.next();
}
/**
* @see #initCacheManager()
*/
public void start(Properties properties) throws CacheException
{
InternalEhCacheManagerFactoryBean.initCacheManager();
}
/**
* @see #initCacheManager()
*/
public void stop()
{
// TODO: Does this port over different Locales?
// Better to override DbPersistenceServiceFactory#close to put a marker on the thread.
if (Thread.currentThread().getName().contains("Finalizer"))
{
// Probably JBPM's finalizer code ... we rely on Spring context calls rather
return;
}
synchronized (getClass())
{
if(logger.isDebugEnabled()) {
String[] caches = InternalEhCacheManagerFactoryBean.getInstance().getCacheNames();
for(String regionName : caches) {
logger.debug("Stopped EHCache region: " + regionName);
}
}
if (initialized) // Avoid re-initialization if it has already been shut down
{
InternalEhCacheManagerFactoryBean.getInstance().shutdown();
initialized = false;
}
}
}
/**
* @return Returns the singleton cache manager
*
* @see #initCacheManager()
*/
public CacheManager getObject() throws Exception
{
return InternalEhCacheManagerFactoryBean.getInstance();
}
/**
* @return Returns the singleton cache manager type
*/
public Class<CacheManager> getObjectType()
{
return CacheManager.class;
}
/**
* @return Returns true always
*/
public boolean isSingleton()
{
return true;
}
}

View File

@@ -25,8 +25,6 @@ import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import net.sf.ehcache.CacheException;
import org.alfresco.error.AlfrescoRuntimeException;
import org.alfresco.repo.tenant.TenantService;
import org.alfresco.repo.tenant.TenantUtil;
@@ -734,7 +732,7 @@ public class TransactionalCache<K extends Serializable, V extends Object>
logger.debug("Pre-commit called for " + keys.size() + " values.");
}
}
catch (CacheException e)
catch (Throwable e)
{
throw new AlfrescoRuntimeException("Failed to transfer updates to shared cache", e);
}
@@ -795,7 +793,7 @@ public class TransactionalCache<K extends Serializable, V extends Object>
logger.debug("Post-commit called for " + keys.size() + " values.");
}
}
catch (CacheException e)
catch (Throwable e)
{
throw new AlfrescoRuntimeException("Failed to transfer updates to shared cache", e);
}
@@ -837,7 +835,7 @@ public class TransactionalCache<K extends Serializable, V extends Object>
}
}
}
catch (CacheException e)
catch (Throwable e)
{
throw new AlfrescoRuntimeException("Failed to transfer updates to shared cache", e);
}