HHH-10124 HHH-8732 Upgraded Ehcache to 2.10.1

This commit is contained in:
Alex Snaps 2013-12-03 14:02:03 -05:00 committed by Steve Ebersole
parent 3147a2894d
commit 0b57c4e787
7 changed files with 79 additions and 116 deletions

View File

@ -19,3 +19,7 @@ mavenPom {
def osgiDescription() { def osgiDescription() {
return mavenPom.description return mavenPom.description
} }
test {
forkEvery 1
}

View File

@ -24,7 +24,6 @@ import org.hibernate.cache.ehcache.internal.regions.EhcacheQueryResultsRegion;
import org.hibernate.cache.ehcache.internal.regions.EhcacheTimestampsRegion; import org.hibernate.cache.ehcache.internal.regions.EhcacheTimestampsRegion;
import org.hibernate.cache.ehcache.internal.strategy.EhcacheAccessStrategyFactory; import org.hibernate.cache.ehcache.internal.strategy.EhcacheAccessStrategyFactory;
import org.hibernate.cache.ehcache.internal.strategy.EhcacheAccessStrategyFactoryImpl; import org.hibernate.cache.ehcache.internal.strategy.EhcacheAccessStrategyFactoryImpl;
import org.hibernate.cache.ehcache.internal.util.HibernateEhcacheUtils;
import org.hibernate.cache.ehcache.management.impl.ProviderMBeanRegistrationHelper; import org.hibernate.cache.ehcache.management.impl.ProviderMBeanRegistrationHelper;
import org.hibernate.cache.spi.CacheDataDescription; import org.hibernate.cache.spi.CacheDataDescription;
import org.hibernate.cache.spi.CollectionRegion; import org.hibernate.cache.spi.CollectionRegion;
@ -163,7 +162,6 @@ abstract class AbstractEhcacheRegionFactory implements RegionFactory {
cache = manager.getEhcache( name ); cache = manager.getEhcache( name );
LOG.debug( "started EHCache region: " + name ); LOG.debug( "started EHCache region: " + name );
} }
HibernateEhcacheUtils.validateEhcache( cache );
return cache; return cache;
} }
catch (net.sf.ehcache.CacheException e) { catch (net.sf.ehcache.CacheException e) {

View File

@ -8,20 +8,15 @@ package org.hibernate.cache.ehcache.internal.util;
import java.net.URL; import java.net.URL;
import net.sf.ehcache.Ehcache;
import net.sf.ehcache.config.CacheConfiguration; import net.sf.ehcache.config.CacheConfiguration;
import net.sf.ehcache.config.Configuration; import net.sf.ehcache.config.Configuration;
import net.sf.ehcache.config.ConfigurationFactory; import net.sf.ehcache.config.ConfigurationFactory;
import net.sf.ehcache.config.NonstopConfiguration; import net.sf.ehcache.config.NonstopConfiguration;
import net.sf.ehcache.config.TerracottaConfiguration;
import net.sf.ehcache.config.TerracottaConfiguration.ValueMode;
import net.sf.ehcache.config.TimeoutBehaviorConfiguration.TimeoutBehaviorType; import net.sf.ehcache.config.TimeoutBehaviorConfiguration.TimeoutBehaviorType;
import org.hibernate.cache.CacheException;
import org.hibernate.cache.ehcache.EhCacheMessageLogger;
import org.jboss.logging.Logger; import org.jboss.logging.Logger;
import org.hibernate.cache.ehcache.EhCacheMessageLogger;
/** /**
* Copy of Ehcache utils into Hibernate code base * Copy of Ehcache utils into Hibernate code base
@ -59,13 +54,6 @@ public final class HibernateEhcacheUtils {
if ( config.getDefaultCacheConfiguration() != null if ( config.getDefaultCacheConfiguration() != null
&& config.getDefaultCacheConfiguration().isTerracottaClustered() ) { && config.getDefaultCacheConfiguration().isTerracottaClustered() ) {
if ( ValueMode.IDENTITY
.equals( config.getDefaultCacheConfiguration().getTerracottaConfiguration().getValueMode() ) ) {
LOG.incompatibleCacheValueMode();
config.getDefaultCacheConfiguration()
.getTerracottaConfiguration()
.setValueMode( ValueMode.SERIALIZATION.name() );
}
setupHibernateTimeoutBehavior( setupHibernateTimeoutBehavior(
config.getDefaultCacheConfiguration() config.getDefaultCacheConfiguration()
.getTerracottaConfiguration() .getTerracottaConfiguration()
@ -75,10 +63,6 @@ public final class HibernateEhcacheUtils {
for ( CacheConfiguration cacheConfig : config.getCacheConfigurations().values() ) { for ( CacheConfiguration cacheConfig : config.getCacheConfigurations().values() ) {
if ( cacheConfig.isTerracottaClustered() ) { if ( cacheConfig.isTerracottaClustered() ) {
if ( ValueMode.IDENTITY.equals( cacheConfig.getTerracottaConfiguration().getValueMode() ) ) {
LOG.incompatibleCacheValueModePerCache( cacheConfig.getName() );
cacheConfig.getTerracottaConfiguration().setValueMode( ValueMode.SERIALIZATION.name() );
}
setupHibernateTimeoutBehavior( cacheConfig.getTerracottaConfiguration().getNonstopConfiguration() ); setupHibernateTimeoutBehavior( cacheConfig.getTerracottaConfiguration().getNonstopConfiguration() );
} }
} }
@ -88,32 +72,4 @@ public final class HibernateEhcacheUtils {
private static void setupHibernateTimeoutBehavior(NonstopConfiguration nonstopConfig) { private static void setupHibernateTimeoutBehavior(NonstopConfiguration nonstopConfig) {
nonstopConfig.getTimeoutBehavior().setType( TimeoutBehaviorType.EXCEPTION.getTypeName() ); nonstopConfig.getTimeoutBehavior().setType( TimeoutBehaviorType.EXCEPTION.getTypeName() );
} }
/**
* Validates that the supplied Ehcache instance is valid for use as a Hibernate cache.
*
* @param cache The cache instance
*
* @throws CacheException If any explicit settings on the cache are not validate
*/
public static void validateEhcache(Ehcache cache) throws CacheException {
final CacheConfiguration cacheConfig = cache.getCacheConfiguration();
if ( cacheConfig.isTerracottaClustered() ) {
final TerracottaConfiguration tcConfig = cacheConfig.getTerracottaConfiguration();
switch ( tcConfig.getValueMode() ) {
case IDENTITY: {
throw new CacheException(
"The clustered Hibernate cache " + cache.getName() + " is using IDENTITY value mode.\n"
+ "Identity value mode cannot be used with Hibernate cache regions."
);
}
case SERIALIZATION:
default: {
// this is the recommended valueMode
break;
}
}
}
}
} }

View File

@ -80,11 +80,9 @@ public class EhcacheHibernate extends AbstractEmitterBean implements EhcacheHibe
@Override @Override
public void setStatisticsEnabled(boolean flag) { public void setStatisticsEnabled(boolean flag) {
if ( flag ) { if ( flag ) {
ehcacheStats.enableStats();
hibernateStats.enableStats(); hibernateStats.enableStats();
} }
else { else {
ehcacheStats.disableStats();
hibernateStats.disableStats(); hibernateStats.disableStats();
} }
statsEnabled.set( flag ); statsEnabled.set( flag );
@ -104,7 +102,6 @@ public class EhcacheHibernate extends AbstractEmitterBean implements EhcacheHibe
*/ */
@Override @Override
public void clearStats() { public void clearStats() {
ehcacheStats.clearStats();
hibernateStats.clearStats(); hibernateStats.clearStats();
} }

View File

@ -9,6 +9,7 @@ package org.hibernate.cache.ehcache.management.impl;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.concurrent.TimeUnit;
import javax.management.MBeanNotificationInfo; import javax.management.MBeanNotificationInfo;
import javax.management.NotCompliantMBeanException; import javax.management.NotCompliantMBeanException;
import javax.management.Notification; import javax.management.Notification;
@ -16,7 +17,6 @@ import javax.management.Notification;
import net.sf.ehcache.Cache; import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager; import net.sf.ehcache.CacheManager;
import net.sf.ehcache.hibernate.management.api.EhcacheStats; import net.sf.ehcache.hibernate.management.api.EhcacheStats;
import net.sf.ehcache.management.sampled.SampledCacheManager;
/** /**
* Implementation of {@link EhcacheStats} * Implementation of {@link EhcacheStats}
@ -29,7 +29,6 @@ public class EhcacheStatsImpl extends AbstractEmitterBean implements EhcacheStat
private static final long MILLIS_PER_SECOND = 1000; private static final long MILLIS_PER_SECOND = 1000;
private static final MBeanNotificationInfo NOTIFICATION_INFO; private static final MBeanNotificationInfo NOTIFICATION_INFO;
private final SampledCacheManager sampledCacheManager;
private final CacheManager cacheManager; private final CacheManager cacheManager;
private long statsSince = System.currentTimeMillis(); private long statsSince = System.currentTimeMillis();
@ -47,35 +46,14 @@ public class EhcacheStatsImpl extends AbstractEmitterBean implements EhcacheStat
* Constructor accepting the backing {@link CacheManager} * Constructor accepting the backing {@link CacheManager}
* *
* @param manager The {@link CacheManager} to expose stats for * @param manager The {@link CacheManager} to expose stats for
*
* @throws javax.management.NotCompliantMBeanException should registering the MBean fail * @throws javax.management.NotCompliantMBeanException should registering the MBean fail
*/ */
public EhcacheStatsImpl(CacheManager manager) throws NotCompliantMBeanException { public EhcacheStatsImpl(CacheManager manager) throws NotCompliantMBeanException {
super( EhcacheStats.class ); super( EhcacheStats.class );
this.sampledCacheManager = new SampledCacheManager( manager );
this.cacheManager = manager; this.cacheManager = manager;
} }
@Override
public boolean isStatisticsEnabled() {
return false;
}
@Override
public void clearStats() {
sampledCacheManager.clearStatistics();
statsSince = System.currentTimeMillis();
}
@Override
public void disableStats() {
setStatisticsEnabled( false );
}
@Override
public void enableStats() {
setStatisticsEnabled( true );
}
@Override @Override
public void flushRegionCache(String region) { public void flushRegionCache(String region) {
final Cache cache = this.cacheManager.getCache( region ); final Cache cache = this.cacheManager.getCache( region );
@ -110,7 +88,7 @@ public class EhcacheStatsImpl extends AbstractEmitterBean implements EhcacheStat
for ( String name : cacheManager.getCacheNames() ) { for ( String name : cacheManager.getCacheNames() ) {
final Cache cache = cacheManager.getCache( name ); final Cache cache = cacheManager.getCache( name );
if ( cache != null ) { if ( cache != null ) {
count += cache.getLiveCacheStatistics().getCacheHitCount(); count += cache.getStatistics().cacheHitCount();
} }
} }
return count; return count;
@ -119,7 +97,7 @@ public class EhcacheStatsImpl extends AbstractEmitterBean implements EhcacheStat
@Override @Override
public double getCacheHitRate() { public double getCacheHitRate() {
final long now = System.currentTimeMillis(); final long now = System.currentTimeMillis();
final double deltaSecs = (double) (now - statsSince) / MILLIS_PER_SECOND; final double deltaSecs = (double) ( now - statsSince ) / MILLIS_PER_SECOND;
return getCacheHitCount() / deltaSecs; return getCacheHitCount() / deltaSecs;
} }
@ -129,7 +107,7 @@ public class EhcacheStatsImpl extends AbstractEmitterBean implements EhcacheStat
for ( String name : cacheManager.getCacheNames() ) { for ( String name : cacheManager.getCacheNames() ) {
final Cache cache = cacheManager.getCache( name ); final Cache cache = cacheManager.getCache( name );
if ( cache != null ) { if ( cache != null ) {
count += cache.getSampledCacheStatistics().getCacheHitMostRecentSample(); count += cache.getStatistics().cacheHitOperation().rate().value().longValue();
} }
} }
return count; return count;
@ -141,7 +119,7 @@ public class EhcacheStatsImpl extends AbstractEmitterBean implements EhcacheStat
for ( String name : cacheManager.getCacheNames() ) { for ( String name : cacheManager.getCacheNames() ) {
final Cache cache = cacheManager.getCache( name ); final Cache cache = cacheManager.getCache( name );
if ( cache != null ) { if ( cache != null ) {
count += cache.getLiveCacheStatistics().getCacheMissCount(); count += cache.getStatistics().cacheMissCount();
} }
} }
return count; return count;
@ -150,7 +128,7 @@ public class EhcacheStatsImpl extends AbstractEmitterBean implements EhcacheStat
@Override @Override
public double getCacheMissRate() { public double getCacheMissRate() {
final long now = System.currentTimeMillis(); final long now = System.currentTimeMillis();
final double deltaSecs = (double) (now - statsSince) / MILLIS_PER_SECOND; final double deltaSecs = (double) ( now - statsSince ) / MILLIS_PER_SECOND;
return getCacheMissCount() / deltaSecs; return getCacheMissCount() / deltaSecs;
} }
@ -160,7 +138,7 @@ public class EhcacheStatsImpl extends AbstractEmitterBean implements EhcacheStat
for ( String name : cacheManager.getCacheNames() ) { for ( String name : cacheManager.getCacheNames() ) {
final Cache cache = cacheManager.getCache( name ); final Cache cache = cacheManager.getCache( name );
if ( cache != null ) { if ( cache != null ) {
count += cache.getSampledCacheStatistics().getCacheMissMostRecentSample(); count += cache.getStatistics().cacheMissOperation().rate().value().longValue();
} }
} }
return count; return count;
@ -172,7 +150,7 @@ public class EhcacheStatsImpl extends AbstractEmitterBean implements EhcacheStat
for ( String name : cacheManager.getCacheNames() ) { for ( String name : cacheManager.getCacheNames() ) {
final Cache cache = cacheManager.getCache( name ); final Cache cache = cacheManager.getCache( name );
if ( cache != null ) { if ( cache != null ) {
count += cache.getLiveCacheStatistics().getPutCount(); count += cache.getStatistics().cachePutCount();
} }
} }
return count; return count;
@ -181,7 +159,7 @@ public class EhcacheStatsImpl extends AbstractEmitterBean implements EhcacheStat
@Override @Override
public double getCachePutRate() { public double getCachePutRate() {
final long now = System.currentTimeMillis(); final long now = System.currentTimeMillis();
final double deltaSecs = (double) (now - statsSince) / MILLIS_PER_SECOND; final double deltaSecs = (double) ( now - statsSince ) / MILLIS_PER_SECOND;
return getCachePutCount() / deltaSecs; return getCachePutCount() / deltaSecs;
} }
@ -191,7 +169,7 @@ public class EhcacheStatsImpl extends AbstractEmitterBean implements EhcacheStat
for ( String name : cacheManager.getCacheNames() ) { for ( String name : cacheManager.getCacheNames() ) {
final Cache cache = cacheManager.getCache( name ); final Cache cache = cacheManager.getCache( name );
if ( cache != null ) { if ( cache != null ) {
count += cache.getSampledCacheStatistics().getCacheElementPutMostRecentSample(); count += cache.getStatistics().cachePutOperation().rate().value().longValue();
} }
} }
return count; return count;
@ -269,12 +247,16 @@ public class EhcacheStatsImpl extends AbstractEmitterBean implements EhcacheStat
for ( String name : cacheManager.getCacheNames() ) { for ( String name : cacheManager.getCacheNames() ) {
final Cache cache = cacheManager.getCache( name ); final Cache cache = cacheManager.getCache( name );
if ( cache != null ) { if ( cache != null ) {
final Double hits = cache.getStatistics().cacheHitOperation().rate().value();
final Double misses = cache.getStatistics().cacheMissNotFoundOperation().rate().value();
final Double expired = cache.getStatistics().cacheMissExpiredOperation().rate().value();
final Double puts = cache.getStatistics().cachePutOperation().rate().value();
rv.put( rv.put(
name, new int[] { name, new int[] {
(int) cache.getSampledCacheStatistics().getCacheHitMostRecentSample(), hits.intValue(),
(int) (cache.getSampledCacheStatistics().getCacheMissNotFoundMostRecentSample() misses.intValue(),
+ cache.getSampledCacheStatistics().getCacheMissExpiredMostRecentSample()), expired.intValue(),
(int) cache.getSampledCacheStatistics().getCacheElementPutMostRecentSample(), puts.intValue()
} }
); );
} }
@ -315,7 +297,7 @@ public class EhcacheStatsImpl extends AbstractEmitterBean implements EhcacheStat
} }
} }
} }
return rv.toArray( new String[ rv.size() ] ); return rv.toArray( new String[rv.size()] );
} }
@Override @Override
@ -455,27 +437,22 @@ public class EhcacheStatsImpl extends AbstractEmitterBean implements EhcacheStat
} }
} }
@Override
public void setStatisticsEnabled(boolean flag) {
for ( String cacheName : cacheManager.getCacheNames() ) {
final Cache cache = cacheManager.getCache( cacheName );
if ( cache != null ) {
cache.setStatisticsEnabled( flag );
}
}
if ( flag ) {
clearStats();
}
sendNotification( CACHE_STATISTICS_ENABLED, flag );
}
@Override @Override
public long getMaxGetTimeMillis() { public long getMaxGetTimeMillis() {
long rv = 0; long rv = 0;
for ( String cacheName : cacheManager.getCacheNames() ) { for ( String cacheName : cacheManager.getCacheNames() ) {
final Cache cache = cacheManager.getCache( cacheName ); final Cache cache = cacheManager.getCache( cacheName );
if ( cache != null ) { if ( cache != null ) {
rv = Math.max( rv, cache.getLiveCacheStatistics().getMaxGetTimeMillis() ); final Long maximum = cache.getStatistics()
.cacheGetOperation().latency().maximum().value();
if ( maximum != null ) {
rv = Math.max(
rv, TimeUnit.MILLISECONDS.convert(
maximum,
TimeUnit.NANOSECONDS
)
);
}
} }
} }
return rv; return rv;
@ -483,21 +460,39 @@ public class EhcacheStatsImpl extends AbstractEmitterBean implements EhcacheStat
@Override @Override
public long getMinGetTimeMillis() { public long getMinGetTimeMillis() {
long rv = 0; long rv = Long.MAX_VALUE;
for ( String cacheName : cacheManager.getCacheNames() ) { for ( String cacheName : cacheManager.getCacheNames() ) {
final Cache cache = cacheManager.getCache( cacheName ); final Cache cache = cacheManager.getCache( cacheName );
if ( cache != null ) { if ( cache != null ) {
rv = Math.max( rv, cache.getLiveCacheStatistics().getMinGetTimeMillis() ); final Long minimum = cache.getStatistics()
.cacheGetOperation()
.latency()
.minimum().value();
if ( minimum != null ) {
rv = Math.min(
rv, TimeUnit.MILLISECONDS.convert(
minimum,
TimeUnit.NANOSECONDS
)
);
}
} }
} }
return rv; return rv == Long.MAX_VALUE ? 0 : rv;
} }
@Override @Override
public long getMaxGetTimeMillis(String cacheName) { public long getMaxGetTimeMillis(String cacheName) {
final Cache cache = cacheManager.getCache( cacheName ); final Cache cache = cacheManager.getCache( cacheName );
if ( cache != null ) { if ( cache != null ) {
return cache.getLiveCacheStatistics().getMaxGetTimeMillis(); final Long maximum = cache.getStatistics()
.cacheGetOperation()
.latency()
.maximum().value();
return maximum == null ? 0 : TimeUnit.MILLISECONDS.convert(
maximum,
TimeUnit.NANOSECONDS
);
} }
else { else {
return 0; return 0;
@ -508,7 +503,14 @@ public class EhcacheStatsImpl extends AbstractEmitterBean implements EhcacheStat
public long getMinGetTimeMillis(String cacheName) { public long getMinGetTimeMillis(String cacheName) {
final Cache cache = cacheManager.getCache( cacheName ); final Cache cache = cacheManager.getCache( cacheName );
if ( cache != null ) { if ( cache != null ) {
return cache.getLiveCacheStatistics().getMinGetTimeMillis(); final Long minimum = cache.getStatistics()
.cacheGetOperation()
.latency()
.minimum().value();
return minimum == null ? 0 : TimeUnit.MILLISECONDS.convert(
minimum,
TimeUnit.NANOSECONDS
);
} }
else { else {
return 0; return 0;
@ -519,7 +521,14 @@ public class EhcacheStatsImpl extends AbstractEmitterBean implements EhcacheStat
public float getAverageGetTimeMillis(String region) { public float getAverageGetTimeMillis(String region) {
final Cache cache = this.cacheManager.getCache( region ); final Cache cache = this.cacheManager.getCache( region );
if ( cache != null ) { if ( cache != null ) {
return cache.getLiveCacheStatistics().getAverageGetTimeMillis(); final Double avg = cache.getStatistics()
.cacheGetOperation()
.latency()
.average().value();
return TimeUnit.MILLISECONDS.convert(
avg.longValue(),
TimeUnit.NANOSECONDS
);
} }
else { else {
return -1f; return -1f;
@ -533,6 +542,6 @@ public class EhcacheStatsImpl extends AbstractEmitterBean implements EhcacheStat
@Override @Override
public MBeanNotificationInfo[] getNotificationInfo() { public MBeanNotificationInfo[] getNotificationInfo() {
return new MBeanNotificationInfo[] {NOTIFICATION_INFO}; return new MBeanNotificationInfo[] { NOTIFICATION_INFO };
} }
} }

View File

@ -29,9 +29,8 @@ import org.hibernate.test.domain.PhoneNumber;
import org.hibernate.test.domain.VersionedItem; import org.hibernate.test.domain.VersionedItem;
import org.junit.Test; import org.junit.Test;
import org.hamcrest.CoreMatchers;
import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;
@ -169,7 +168,7 @@ public class HibernateCacheTest extends BaseNonConfigCoreFunctionalTestCase {
// check the version value in the cache... // check the version value in the cache...
SecondLevelCacheStatistics slcs = sessionFactory().getStatistics() SecondLevelCacheStatistics slcs = sessionFactory().getStatistics()
.getSecondLevelCacheStatistics( REGION_PREFIX + VersionedItem.class.getName() ); .getSecondLevelCacheStatistics( REGION_PREFIX + VersionedItem.class.getName() );
assertThat( slcs, CoreMatchers.notNullValue() ); assertNotNull(slcs);
final Map entries = slcs.getEntries(); final Map entries = slcs.getEntries();
Object entry = entries.get( item.getId() ); Object entry = entries.get( item.getId() );
Long cachedVersionValue; Long cachedVersionValue;

View File

@ -106,7 +106,7 @@ ext {
jnp_server: "org.jboss.naming:jnpserver:${jnpVersion}", jnp_server: "org.jboss.naming:jnpserver:${jnpVersion}",
c3p0: "com.mchange:c3p0:0.9.2.1", c3p0: "com.mchange:c3p0:0.9.2.1",
ehcache: "net.sf.ehcache:ehcache-core:2.4.3", ehcache: "net.sf.ehcache:ehcache:2.10.1",
proxool: "proxool:proxool:0.8.3", proxool: "proxool:proxool:0.8.3",
hikaricp: "com.zaxxer:HikariCP-java6:2.3.9" hikaricp: "com.zaxxer:HikariCP-java6:2.3.9"