HHH-7553 Use new Infinispan configuration exclusively

This commit is contained in:
Galder Zamarreño 2012-10-23 12:08:38 +02:00
parent cf9ef94f8e
commit e57c822cbc
6 changed files with 81 additions and 54 deletions

View File

@ -17,13 +17,14 @@ import org.hibernate.cache.infinispan.timestamp.ClusteredTimestampsRegionImpl;
import org.hibernate.cache.infinispan.util.Caches; import org.hibernate.cache.infinispan.util.Caches;
import org.infinispan.AdvancedCache; import org.infinispan.AdvancedCache;
import org.infinispan.commands.module.ModuleCommandFactory; import org.infinispan.commands.module.ModuleCommandFactory;
import org.infinispan.config.Configuration;
import org.infinispan.configuration.cache.CacheMode; import org.infinispan.configuration.cache.CacheMode;
import org.infinispan.configuration.cache.Configuration;
import org.infinispan.configuration.cache.ConfigurationBuilder; import org.infinispan.configuration.cache.ConfigurationBuilder;
import org.infinispan.factories.GlobalComponentRegistry; import org.infinispan.factories.GlobalComponentRegistry;
import org.infinispan.manager.DefaultCacheManager; import org.infinispan.manager.DefaultCacheManager;
import org.infinispan.manager.EmbeddedCacheManager; import org.infinispan.manager.EmbeddedCacheManager;
import org.infinispan.transaction.TransactionMode; import org.infinispan.transaction.TransactionMode;
import org.infinispan.transaction.lookup.GenericTransactionManagerLookup;
import org.infinispan.util.concurrent.IsolationLevel; import org.infinispan.util.concurrent.IsolationLevel;
import org.infinispan.configuration.parsing.ConfigurationBuilderHolder; import org.infinispan.configuration.parsing.ConfigurationBuilderHolder;
import org.infinispan.configuration.parsing.ParserRegistry; import org.infinispan.configuration.parsing.ParserRegistry;
@ -318,7 +319,7 @@ public class InfinispanRegionFactory implements RegionFactory {
dissectProperty(prefixLoc, key, properties); dissectProperty(prefixLoc, key, properties);
} }
} }
defineGenericDataTypeCacheConfigurations(settings, properties); defineGenericDataTypeCacheConfigurations(properties);
definePendingPutsCache(); definePendingPutsCache();
} catch (CacheException ce) { } catch (CacheException ce) {
throw ce; throw ce;
@ -475,45 +476,59 @@ public class InfinispanRegionFactory implements RegionFactory {
return cfgOverride; return cfgOverride;
} }
private void defineGenericDataTypeCacheConfigurations(Settings settings, Properties properties) throws CacheException { private void defineGenericDataTypeCacheConfigurations(Properties properties) {
String[] defaultGenericDataTypes = new String[]{ENTITY_KEY, COLLECTION_KEY, TIMESTAMPS_KEY, QUERY_KEY}; String[] defaultGenericDataTypes = new String[]{ENTITY_KEY, COLLECTION_KEY, TIMESTAMPS_KEY, QUERY_KEY};
for (String type : defaultGenericDataTypes) { for (String type : defaultGenericDataTypes) {
TypeOverrides override = overrideStatisticsIfPresent(typeOverrides.get(type), properties); TypeOverrides override = overrideStatisticsIfPresent(typeOverrides.get(type), properties);
String cacheName = override.getCacheName(); String cacheName = override.getCacheName();
Configuration newCacheCfg = override.createInfinispanConfiguration(); ConfigurationBuilder builder = new ConfigurationBuilder();
// Read base configuration
applyConfiguration(cacheName, builder);
// Apply overrides // Apply overrides
Configuration cacheConfig = manager.defineConfiguration(cacheName, cacheName, newCacheCfg); override.applyTo(builder);
// Configure transaction manager // Configure transaction manager
cacheConfig = configureTransactionManager(cacheConfig, cacheName, properties); configureTransactionManager(builder, cacheName, properties);
manager.defineConfiguration(cacheName, cacheName, cacheConfig); // Define configuration, validate and then apply
Configuration cfg = builder.build();
override.validateInfinispanConfiguration(cfg);
manager.defineConfiguration(cacheName, cfg);
definedConfigurations.add(cacheName); definedConfigurations.add(cacheName);
override.validateInfinispanConfiguration(cacheConfig);
} }
} }
private AdvancedCache getCache(String regionName, String typeKey, Properties properties) { private AdvancedCache getCache(String regionName, String typeKey, Properties properties) {
TypeOverrides regionOverride = typeOverrides.get(regionName); TypeOverrides regionOverride = typeOverrides.get(regionName);
if (!definedConfigurations.contains(regionName)) { if (!definedConfigurations.contains(regionName)) {
String templateCacheName = null; String templateCacheName;
Configuration regionCacheCfg = null; Configuration regionCacheCfg;
ConfigurationBuilder builder = new ConfigurationBuilder();
if (regionOverride != null) { if (regionOverride != null) {
if (log.isDebugEnabled()) log.debug("Cache region specific configuration exists: " + regionOverride); if (log.isDebugEnabled()) log.debug("Cache region specific configuration exists: " + regionOverride);
regionOverride = overrideStatisticsIfPresent(regionOverride, properties);
regionCacheCfg = regionOverride.createInfinispanConfiguration();
String cacheName = regionOverride.getCacheName(); String cacheName = regionOverride.getCacheName();
if (cacheName != null) // Region specific override with a given cache name if (cacheName != null) // Region specific override with a given cache name
templateCacheName = cacheName; templateCacheName = cacheName;
else // Region specific override without cache name, so template cache name is generic for data type. else // Region specific override without cache name, so template cache name is generic for data type.
templateCacheName = typeOverrides.get(typeKey).getCacheName(); templateCacheName = typeOverrides.get(typeKey).getCacheName();
// Read template configuration
applyConfiguration(templateCacheName, builder);
regionOverride = overrideStatisticsIfPresent(regionOverride, properties);
regionOverride.applyTo(builder);
} else { } else {
// No region specific overrides, template cache name is generic for data type. // No region specific overrides, template cache name is generic for data type.
templateCacheName = typeOverrides.get(typeKey).getCacheName(); templateCacheName = typeOverrides.get(typeKey).getCacheName();
regionCacheCfg = typeOverrides.get(typeKey).createInfinispanConfiguration(); // Read template configuration
builder.read(manager.getCacheConfiguration(templateCacheName));
// Apply overrides
typeOverrides.get(typeKey).applyTo(builder);
} }
// Configure transaction manager // Configure transaction manager
regionCacheCfg = configureTransactionManager(regionCacheCfg, templateCacheName, properties); configureTransactionManager(builder, templateCacheName, properties);
// Apply overrides // Define configuration
manager.defineConfiguration(regionName, templateCacheName, regionCacheCfg); manager.defineConfiguration(regionName, builder.build());
definedConfigurations.add(regionName); definedConfigurations.add(regionName);
} }
AdvancedCache cache = manager.getCache(regionName).getAdvancedCache(); AdvancedCache cache = manager.getCache(regionName).getAdvancedCache();
@ -523,6 +538,12 @@ public class InfinispanRegionFactory implements RegionFactory {
return createCacheWrapper(cache); return createCacheWrapper(cache);
} }
private void applyConfiguration(String cacheName, ConfigurationBuilder builder) {
Configuration cfg = manager.getCacheConfiguration(cacheName);
if (cfg != null)
builder.read(cfg);
}
private CacheCommandFactory getCacheCommandFactory(AdvancedCache cache) { private CacheCommandFactory getCacheCommandFactory(AdvancedCache cache) {
GlobalComponentRegistry globalCr = cache.getComponentRegistry() GlobalComponentRegistry globalCr = cache.getComponentRegistry()
.getGlobalComponentRegistry(); .getGlobalComponentRegistry();
@ -549,25 +570,30 @@ public class InfinispanRegionFactory implements RegionFactory {
return cache; return cache;
} }
private Configuration configureTransactionManager(Configuration regionOverrides, String templateCacheName, Properties properties) { private void configureTransactionManager(ConfigurationBuilder builder,
String cacheName, Properties properties) {
// Get existing configuration to verify whether a tm was configured or not. // Get existing configuration to verify whether a tm was configured or not.
Configuration templateConfig = manager.defineConfiguration(templateCacheName, new Configuration()); Configuration baseCfg = manager.getCacheConfiguration(cacheName);
if (templateConfig.isTransactionalCache()) { if (baseCfg != null && baseCfg.transaction().transactionMode().isTransactional()) {
String ispnTmLookupClassName = templateConfig.getTransactionManagerLookupClass(); String ispnTmLookupClassName = baseCfg.transaction().transactionManagerLookup().getClass().getName();
String hbTmLookupClassName = org.hibernate.cache.infinispan.tm.HibernateTransactionManagerLookup.class.getName(); String hbTmLookupClassName = org.hibernate.cache.infinispan.tm.HibernateTransactionManagerLookup.class.getName();
if (ispnTmLookupClassName != null && !ispnTmLookupClassName.equals(hbTmLookupClassName)) { if (GenericTransactionManagerLookup.class.getName().equals(ispnTmLookupClassName)) {
log.debug("Using default Infinispan transaction manager lookup " +
"instance (GenericTransactionManagerLookup), overriding it " +
"with Hibernate transaction manager lookup");
builder.transaction().transactionManagerLookup(transactionManagerlookup);
} else if (ispnTmLookupClassName != null && !ispnTmLookupClassName.equals(hbTmLookupClassName)) {
log.debug("Infinispan is configured [" + ispnTmLookupClassName + "] with a different transaction manager lookup " + log.debug("Infinispan is configured [" + ispnTmLookupClassName + "] with a different transaction manager lookup " +
"class than Hibernate [" + hbTmLookupClassName + "]"); "class than Hibernate [" + hbTmLookupClassName + "]");
} else { } else {
regionOverrides.setTransactionManagerLookup(transactionManagerlookup); // Infinispan TM lookup class null, so apply Hibernate one directly
builder.transaction().transactionManagerLookup(transactionManagerlookup);
} }
String useSyncProp = extractProperty(INFINISPAN_USE_SYNCHRONIZATION_PROP, properties); String useSyncProp = extractProperty(INFINISPAN_USE_SYNCHRONIZATION_PROP, properties);
boolean useSync = useSyncProp == null ? DEF_USE_SYNCHRONIZATION : Boolean.parseBoolean(useSyncProp); boolean useSync = useSyncProp == null ? DEF_USE_SYNCHRONIZATION : Boolean.parseBoolean(useSyncProp);
regionOverrides.fluent().transaction().useSynchronization(useSync); builder.transaction().useSynchronization(useSync);
} }
return regionOverrides;
} }
private TypeOverrides overrideStatisticsIfPresent(TypeOverrides override, Properties properties) { private TypeOverrides overrideStatisticsIfPresent(TypeOverrides override, Properties properties) {

View File

@ -25,7 +25,8 @@ import java.util.HashSet;
import java.util.Locale; import java.util.Locale;
import java.util.Set; import java.util.Set;
import org.infinispan.config.Configuration; import org.infinispan.configuration.cache.Configuration;
import org.infinispan.configuration.cache.ConfigurationBuilder;
import org.infinispan.eviction.EvictionStrategy; import org.infinispan.eviction.EvictionStrategy;
import org.hibernate.cache.CacheException; import org.hibernate.cache.CacheException;
@ -118,25 +119,23 @@ public class TypeOverrides {
this.isExposeStatistics = isExposeStatistics; this.isExposeStatistics = isExposeStatistics;
} }
public Configuration createInfinispanConfiguration() { public void applyTo(ConfigurationBuilder builder) {
Configuration cacheCfg = new Configuration();
if (overridden.contains("evictionStrategy")) if (overridden.contains("evictionStrategy"))
cacheCfg.fluent().eviction().strategy(evictionStrategy); builder.eviction().strategy(evictionStrategy);
if (overridden.contains("evictionWakeUpInterval")) if (overridden.contains("evictionWakeUpInterval"))
cacheCfg.fluent().expiration().wakeUpInterval(evictionWakeUpInterval); builder.expiration().wakeUpInterval(evictionWakeUpInterval);
if (overridden.contains("evictionMaxEntries")) if (overridden.contains("evictionMaxEntries"))
cacheCfg.fluent().eviction().maxEntries(evictionMaxEntries); builder.eviction().maxEntries(evictionMaxEntries);
if (overridden.contains("expirationLifespan")) if (overridden.contains("expirationLifespan"))
cacheCfg.fluent().expiration().lifespan(expirationLifespan); builder.expiration().lifespan(expirationLifespan);
if (overridden.contains("expirationMaxIdle")) if (overridden.contains("expirationMaxIdle"))
cacheCfg.fluent().expiration().maxIdle(expirationMaxIdle); builder.expiration().maxIdle(expirationMaxIdle);
if (overridden.contains("isExposeStatistics") && isExposeStatistics) if (overridden.contains("isExposeStatistics") && isExposeStatistics)
cacheCfg.fluent().jmxStatistics(); builder.jmxStatistics().enable();
return cacheCfg;
} }
public void validateInfinispanConfiguration(Configuration configuration) throws CacheException { public void validateInfinispanConfiguration(Configuration cfg) throws CacheException {
// no-op // no-op, method overriden
} }
@Override @Override

View File

@ -21,8 +21,7 @@
*/ */
package org.hibernate.cache.infinispan.timestamp; package org.hibernate.cache.infinispan.timestamp;
import org.infinispan.config.Configuration; import org.infinispan.configuration.cache.Configuration;
import org.infinispan.config.Configuration.CacheMode;
import org.infinispan.eviction.EvictionStrategy; import org.infinispan.eviction.EvictionStrategy;
import org.hibernate.cache.CacheException; import org.hibernate.cache.CacheException;
@ -35,15 +34,16 @@ import org.hibernate.cache.infinispan.TypeOverrides;
* @since 3.5 * @since 3.5
*/ */
public class TimestampTypeOverrides extends TypeOverrides { public class TimestampTypeOverrides extends TypeOverrides {
@Override @Override
public void validateInfinispanConfiguration(Configuration configuration) throws CacheException { public void validateInfinispanConfiguration(Configuration cfg) throws CacheException {
CacheMode cacheMode = configuration.getCacheMode(); if (cfg.clustering().cacheMode().isInvalidation()) {
if (cacheMode.equals(CacheMode.INVALIDATION_ASYNC) || cacheMode.equals(CacheMode.INVALIDATION_SYNC)) {
throw new CacheException("Timestamp cache cannot be configured with invalidation"); throw new CacheException("Timestamp cache cannot be configured with invalidation");
} }
EvictionStrategy strategy = configuration.getEvictionStrategy(); EvictionStrategy strategy = cfg.eviction().strategy();
if (!strategy.equals(EvictionStrategy.NONE)) { if (!strategy.equals(EvictionStrategy.NONE)) {
throw new CacheException("Timestamp cache cannot be configured with eviction"); throw new CacheException("Timestamp cache cannot be configured with eviction");
} }
} }
} }

View File

@ -242,6 +242,7 @@ public class InfinispanRegionFactoryTestCase {
assertEquals(EvictionStrategy.LIRS, cacheCfg.eviction().strategy()); assertEquals(EvictionStrategy.LIRS, cacheCfg.eviction().strategy());
assertEquals(3000, cacheCfg.expiration().wakeUpInterval()); assertEquals(3000, cacheCfg.expiration().wakeUpInterval());
assertEquals(30000, cacheCfg.eviction().maxEntries()); assertEquals(30000, cacheCfg.eviction().maxEntries());
// Max idle value comes from base XML configuration
assertEquals(100000, cacheCfg.expiration().maxIdle()); assertEquals(100000, cacheCfg.expiration().maxIdle());
CollectionRegionImpl collectionRegion = (CollectionRegionImpl) CollectionRegionImpl collectionRegion = (CollectionRegionImpl)
@ -394,7 +395,7 @@ public class InfinispanRegionFactoryTestCase {
p.setProperty("hibernate.cache.infinispan.timestamps.cfg", "timestamps-none-eviction"); p.setProperty("hibernate.cache.infinispan.timestamps.cfg", "timestamps-none-eviction");
p.setProperty("hibernate.cache.infinispan.timestamps.eviction.strategy", "NONE"); p.setProperty("hibernate.cache.infinispan.timestamps.eviction.strategy", "NONE");
p.setProperty("hibernate.cache.infinispan.timestamps.eviction.wake_up_interval", "3000"); p.setProperty("hibernate.cache.infinispan.timestamps.eviction.wake_up_interval", "3000");
p.setProperty("hibernate.cache.infinispan.timestamps.eviction.max_entries", "10000"); p.setProperty("hibernate.cache.infinispan.timestamps.eviction.max_entries", "0");
InfinispanRegionFactory factory = createRegionFactory(p); InfinispanRegionFactory factory = createRegionFactory(p);
try { try {
factory.buildTimestampsRegion(timestamps, p); factory.buildTimestampsRegion(timestamps, p);

View File

@ -69,7 +69,7 @@ public class IsolatedClassLoaderTest extends DualNodeTestCase {
private static ClassLoader originalTCCL; private static ClassLoader originalTCCL;
private static ClassLoader visibleClassesCl; // private static ClassLoader visibleClassesCl;
@BeforeClass @BeforeClass
public static void prepareClassLoader() { public static void prepareClassLoader() {
@ -200,12 +200,10 @@ public class IsolatedClassLoaderTest extends DualNodeTestCase {
if ( useNamedRegion ) { if ( useNamedRegion ) {
cacheName = "AccountRegion"; // As defined by ClassLoaderTestDAO via calls to query.setCacheRegion cacheName = "AccountRegion"; // As defined by ClassLoaderTestDAO via calls to query.setCacheRegion
// Define cache configurations for region early to avoid ending up with local caches for this region // Define cache configurations for region early to avoid ending up with local caches for this region
localManager.defineConfiguration( localManager.defineConfiguration(cacheName,
cacheName, "replicated-query", new org.infinispan.config.Configuration() localManager.getCacheConfiguration("replicated-query"));
); remoteManager.defineConfiguration(cacheName,
remoteManager.defineConfiguration( remoteManager.getCacheConfiguration("replicated-query"));
cacheName, "replicated-query", new org.infinispan.config.Configuration()
);
} }
else { else {
cacheName = "replicated-query"; cacheName = "replicated-query";

View File

@ -34,6 +34,7 @@ import javax.naming.StringRefAddr;
import javax.transaction.Status; import javax.transaction.Status;
import javax.transaction.UserTransaction; import javax.transaction.UserTransaction;
import org.infinispan.configuration.cache.ConfigurationBuilder;
import org.infinispan.transaction.lookup.JBossStandaloneJTAManagerLookup; import org.infinispan.transaction.lookup.JBossStandaloneJTAManagerLookup;
import org.infinispan.util.logging.Log; import org.infinispan.util.logging.Log;
import org.infinispan.util.logging.LogFactory; import org.infinispan.util.logging.LogFactory;
@ -84,7 +85,7 @@ public class JBossStandaloneJtaExampleTest {
jndiServer = startJndiServer(); jndiServer = startJndiServer();
ctx = createJndiContext(); ctx = createJndiContext();
// Inject configuration to initialise transaction manager from config classloader // Inject configuration to initialise transaction manager from config classloader
lookup.init( new org.infinispan.config.Configuration() ); lookup.init(new ConfigurationBuilder().build());
bindTransactionManager(); bindTransactionManager();
bindUserTransaction(); bindUserTransaction();
} }
@ -92,6 +93,8 @@ public class JBossStandaloneJtaExampleTest {
@After @After
public void tearDown() throws Exception { public void tearDown() throws Exception {
try { try {
unbind("UserTransaction", ctx);
unbind("java:/TransactionManager", ctx);
ctx.close(); ctx.close();
jndiServer.stop(); jndiServer.stop();
} }