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() {
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.strategy.EhcacheAccessStrategyFactory;
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.spi.CacheDataDescription;
import org.hibernate.cache.spi.CollectionRegion;
@ -163,7 +162,6 @@ abstract class AbstractEhcacheRegionFactory implements RegionFactory {
cache = manager.getEhcache( name );
LOG.debug( "started EHCache region: " + name );
}
HibernateEhcacheUtils.validateEhcache( cache );
return cache;
}
catch (net.sf.ehcache.CacheException e) {

View File

@ -8,20 +8,15 @@ package org.hibernate.cache.ehcache.internal.util;
import java.net.URL;
import net.sf.ehcache.Ehcache;
import net.sf.ehcache.config.CacheConfiguration;
import net.sf.ehcache.config.Configuration;
import net.sf.ehcache.config.ConfigurationFactory;
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 org.hibernate.cache.CacheException;
import org.hibernate.cache.ehcache.EhCacheMessageLogger;
import org.jboss.logging.Logger;
import org.hibernate.cache.ehcache.EhCacheMessageLogger;
/**
* Copy of Ehcache utils into Hibernate code base
@ -56,16 +51,9 @@ public final class HibernateEhcacheUtils {
if ( config == null ) {
return null;
}
if ( config.getDefaultCacheConfiguration() != null
&& config.getDefaultCacheConfiguration().isTerracottaClustered() ) {
if ( ValueMode.IDENTITY
.equals( config.getDefaultCacheConfiguration().getTerracottaConfiguration().getValueMode() ) ) {
LOG.incompatibleCacheValueMode();
config.getDefaultCacheConfiguration()
.getTerracottaConfiguration()
.setValueMode( ValueMode.SERIALIZATION.name() );
}
setupHibernateTimeoutBehavior(
config.getDefaultCacheConfiguration()
.getTerracottaConfiguration()
@ -75,10 +63,6 @@ public final class HibernateEhcacheUtils {
for ( CacheConfiguration cacheConfig : config.getCacheConfigurations().values() ) {
if ( cacheConfig.isTerracottaClustered() ) {
if ( ValueMode.IDENTITY.equals( cacheConfig.getTerracottaConfiguration().getValueMode() ) ) {
LOG.incompatibleCacheValueModePerCache( cacheConfig.getName() );
cacheConfig.getTerracottaConfiguration().setValueMode( ValueMode.SERIALIZATION.name() );
}
setupHibernateTimeoutBehavior( cacheConfig.getTerracottaConfiguration().getNonstopConfiguration() );
}
}
@ -88,32 +72,4 @@ public final class HibernateEhcacheUtils {
private static void setupHibernateTimeoutBehavior(NonstopConfiguration nonstopConfig) {
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
public void setStatisticsEnabled(boolean flag) {
if ( flag ) {
ehcacheStats.enableStats();
hibernateStats.enableStats();
}
else {
ehcacheStats.disableStats();
hibernateStats.disableStats();
}
statsEnabled.set( flag );
@ -104,7 +102,6 @@ public class EhcacheHibernate extends AbstractEmitterBean implements EhcacheHibe
*/
@Override
public void clearStats() {
ehcacheStats.clearStats();
hibernateStats.clearStats();
}

View File

@ -9,6 +9,7 @@ package org.hibernate.cache.ehcache.management.impl;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import javax.management.MBeanNotificationInfo;
import javax.management.NotCompliantMBeanException;
import javax.management.Notification;
@ -16,7 +17,6 @@ import javax.management.Notification;
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.hibernate.management.api.EhcacheStats;
import net.sf.ehcache.management.sampled.SampledCacheManager;
/**
* 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 MBeanNotificationInfo NOTIFICATION_INFO;
private final SampledCacheManager sampledCacheManager;
private final CacheManager cacheManager;
private long statsSince = System.currentTimeMillis();
@ -47,35 +46,14 @@ public class EhcacheStatsImpl extends AbstractEmitterBean implements EhcacheStat
* Constructor accepting the backing {@link CacheManager}
*
* @param manager The {@link CacheManager} to expose stats for
*
* @throws javax.management.NotCompliantMBeanException should registering the MBean fail
*/
public EhcacheStatsImpl(CacheManager manager) throws NotCompliantMBeanException {
super( EhcacheStats.class );
this.sampledCacheManager = new SampledCacheManager( 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
public void flushRegionCache(String region) {
final Cache cache = this.cacheManager.getCache( region );
@ -110,7 +88,7 @@ public class EhcacheStatsImpl extends AbstractEmitterBean implements EhcacheStat
for ( String name : cacheManager.getCacheNames() ) {
final Cache cache = cacheManager.getCache( name );
if ( cache != null ) {
count += cache.getLiveCacheStatistics().getCacheHitCount();
count += cache.getStatistics().cacheHitCount();
}
}
return count;
@ -119,7 +97,7 @@ public class EhcacheStatsImpl extends AbstractEmitterBean implements EhcacheStat
@Override
public double getCacheHitRate() {
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;
}
@ -129,7 +107,7 @@ public class EhcacheStatsImpl extends AbstractEmitterBean implements EhcacheStat
for ( String name : cacheManager.getCacheNames() ) {
final Cache cache = cacheManager.getCache( name );
if ( cache != null ) {
count += cache.getSampledCacheStatistics().getCacheHitMostRecentSample();
count += cache.getStatistics().cacheHitOperation().rate().value().longValue();
}
}
return count;
@ -141,7 +119,7 @@ public class EhcacheStatsImpl extends AbstractEmitterBean implements EhcacheStat
for ( String name : cacheManager.getCacheNames() ) {
final Cache cache = cacheManager.getCache( name );
if ( cache != null ) {
count += cache.getLiveCacheStatistics().getCacheMissCount();
count += cache.getStatistics().cacheMissCount();
}
}
return count;
@ -150,7 +128,7 @@ public class EhcacheStatsImpl extends AbstractEmitterBean implements EhcacheStat
@Override
public double getCacheMissRate() {
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;
}
@ -160,7 +138,7 @@ public class EhcacheStatsImpl extends AbstractEmitterBean implements EhcacheStat
for ( String name : cacheManager.getCacheNames() ) {
final Cache cache = cacheManager.getCache( name );
if ( cache != null ) {
count += cache.getSampledCacheStatistics().getCacheMissMostRecentSample();
count += cache.getStatistics().cacheMissOperation().rate().value().longValue();
}
}
return count;
@ -172,7 +150,7 @@ public class EhcacheStatsImpl extends AbstractEmitterBean implements EhcacheStat
for ( String name : cacheManager.getCacheNames() ) {
final Cache cache = cacheManager.getCache( name );
if ( cache != null ) {
count += cache.getLiveCacheStatistics().getPutCount();
count += cache.getStatistics().cachePutCount();
}
}
return count;
@ -181,7 +159,7 @@ public class EhcacheStatsImpl extends AbstractEmitterBean implements EhcacheStat
@Override
public double getCachePutRate() {
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;
}
@ -191,7 +169,7 @@ public class EhcacheStatsImpl extends AbstractEmitterBean implements EhcacheStat
for ( String name : cacheManager.getCacheNames() ) {
final Cache cache = cacheManager.getCache( name );
if ( cache != null ) {
count += cache.getSampledCacheStatistics().getCacheElementPutMostRecentSample();
count += cache.getStatistics().cachePutOperation().rate().value().longValue();
}
}
return count;
@ -269,12 +247,16 @@ public class EhcacheStatsImpl extends AbstractEmitterBean implements EhcacheStat
for ( String name : cacheManager.getCacheNames() ) {
final Cache cache = cacheManager.getCache( name );
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(
name, new int[] {
(int) cache.getSampledCacheStatistics().getCacheHitMostRecentSample(),
(int) (cache.getSampledCacheStatistics().getCacheMissNotFoundMostRecentSample()
+ cache.getSampledCacheStatistics().getCacheMissExpiredMostRecentSample()),
(int) cache.getSampledCacheStatistics().getCacheElementPutMostRecentSample(),
hits.intValue(),
misses.intValue(),
expired.intValue(),
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
@ -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
public long getMaxGetTimeMillis() {
long rv = 0;
for ( String cacheName : cacheManager.getCacheNames() ) {
final Cache cache = cacheManager.getCache( cacheName );
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;
@ -483,21 +460,39 @@ public class EhcacheStatsImpl extends AbstractEmitterBean implements EhcacheStat
@Override
public long getMinGetTimeMillis() {
long rv = 0;
long rv = Long.MAX_VALUE;
for ( String cacheName : cacheManager.getCacheNames() ) {
final Cache cache = cacheManager.getCache( cacheName );
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
public long getMaxGetTimeMillis(String cacheName) {
final Cache cache = cacheManager.getCache( cacheName );
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 {
return 0;
@ -508,7 +503,14 @@ public class EhcacheStatsImpl extends AbstractEmitterBean implements EhcacheStat
public long getMinGetTimeMillis(String cacheName) {
final Cache cache = cacheManager.getCache( cacheName );
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 {
return 0;
@ -519,7 +521,14 @@ public class EhcacheStatsImpl extends AbstractEmitterBean implements EhcacheStat
public float getAverageGetTimeMillis(String region) {
final Cache cache = this.cacheManager.getCache( region );
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 {
return -1f;
@ -533,6 +542,6 @@ public class EhcacheStatsImpl extends AbstractEmitterBean implements EhcacheStat
@Override
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.junit.Test;
import org.hamcrest.CoreMatchers;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
@ -169,7 +168,7 @@ public class HibernateCacheTest extends BaseNonConfigCoreFunctionalTestCase {
// check the version value in the cache...
SecondLevelCacheStatistics slcs = sessionFactory().getStatistics()
.getSecondLevelCacheStatistics( REGION_PREFIX + VersionedItem.class.getName() );
assertThat( slcs, CoreMatchers.notNullValue() );
assertNotNull(slcs);
final Map entries = slcs.getEntries();
Object entry = entries.get( item.getId() );
Long cachedVersionValue;

View File

@ -106,7 +106,7 @@ ext {
jnp_server: "org.jboss.naming:jnpserver:${jnpVersion}",
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",
hikaricp: "com.zaxxer:HikariCP-java6:2.3.9"