Skip to content

Commit cc9fbf4

Browse files
committed
HHH-6955 Upgrade to Infinispan 5.1.0.CR3
* Switch to autoCommit=false so that unnecessary transactions are not created. * Mark timestamps cache explicitly non-transactional. * Move away from using caches as a way to send evict all notifications. Instead, use custom commands provided by Infinispan. * Get rid of the JDBC transaction test because it does not make sense configuring JDBC transactions with Infinispan 2LC.
1 parent a41efe3 commit cc9fbf4

31 files changed

+544
-541
lines changed

hibernate-infinispan/hibernate-infinispan.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ configurations {
55
}
66

77
dependencies {
8-
infinispanVersion = '5.1.0.CR1'
8+
infinispanVersion = '5.1.0.CR3'
99
jnpVersion = '5.0.3.GA'
1010

1111
compile(project(':hibernate-core'))

hibernate-infinispan/src/main/java/org/hibernate/cache/infinispan/InfinispanRegionFactory.java

Lines changed: 67 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
package org.hibernate.cache.infinispan;
2+
23
import java.io.IOException;
34
import java.util.Collections;
45
import java.util.Enumeration;
@@ -7,7 +8,13 @@
78
import java.util.Map;
89
import java.util.Properties;
910
import java.util.Set;
11+
import java.util.concurrent.ConcurrentHashMap;
12+
import java.util.concurrent.ConcurrentMap;
1013
import javax.transaction.TransactionManager;
14+
15+
import org.hibernate.cache.infinispan.impl.BaseRegion;
16+
import org.hibernate.cache.infinispan.util.CacheCommandFactory;
17+
import org.hibernate.cache.infinispan.util.CacheCommandInitializer;
1118
import org.hibernate.cache.spi.CacheDataDescription;
1219
import org.hibernate.cache.CacheException;
1320
import org.hibernate.cache.spi.CollectionRegion;
@@ -28,8 +35,9 @@
2835
import org.hibernate.cfg.Settings;
2936
import org.hibernate.internal.util.config.ConfigurationHelper;
3037
import org.infinispan.AdvancedCache;
31-
import org.infinispan.Cache;
3238
import org.infinispan.config.Configuration;
39+
import org.infinispan.factories.ComponentRegistry;
40+
import org.infinispan.factories.GlobalComponentRegistry;
3341
import org.infinispan.manager.DefaultCacheManager;
3442
import org.infinispan.manager.EmbeddedCacheManager;
3543
import org.infinispan.util.logging.Log;
@@ -155,6 +163,9 @@ public class InfinispanRegionFactory implements RegionFactory {
155163
private org.infinispan.transaction.lookup.TransactionManagerLookup transactionManagerlookup;
156164

157165
private TransactionManager transactionManager;
166+
167+
private ConcurrentMap<String, BaseRegion> allRegions =
168+
new ConcurrentHashMap<String, BaseRegion>();
158169

159170
/**
160171
* Create a new instance using the default configuration.
@@ -174,20 +185,20 @@ public InfinispanRegionFactory(Properties props) {
174185
/** {@inheritDoc} */
175186
public CollectionRegion buildCollectionRegion(String regionName, Properties properties, CacheDataDescription metadata) throws CacheException {
176187
if (log.isDebugEnabled()) log.debug("Building collection cache region [" + regionName + "]");
177-
Cache cache = getCache(regionName, COLLECTION_KEY, properties);
188+
AdvancedCache cache = getCache(regionName, COLLECTION_KEY, properties);
178189
CacheAdapter cacheAdapter = CacheAdapterImpl.newInstance(cache);
179190
CollectionRegionImpl region = new CollectionRegionImpl(cacheAdapter, regionName, metadata, transactionManager, this);
180-
region.start();
191+
startRegion(region, regionName);
181192
return region;
182193
}
183194

184195
/** {@inheritDoc} */
185196
public EntityRegion buildEntityRegion(String regionName, Properties properties, CacheDataDescription metadata) throws CacheException {
186197
if (log.isDebugEnabled()) log.debug("Building entity cache region [" + regionName + "]");
187-
Cache cache = getCache(regionName, ENTITY_KEY, properties);
198+
AdvancedCache cache = getCache(regionName, ENTITY_KEY, properties);
188199
CacheAdapter cacheAdapter = CacheAdapterImpl.newInstance(cache);
189200
EntityRegionImpl region = new EntityRegionImpl(cacheAdapter, regionName, metadata, transactionManager, this);
190-
region.start();
201+
startRegion(region, regionName);
191202
return region;
192203
}
193204

@@ -202,10 +213,10 @@ public QueryResultsRegion buildQueryResultsRegion(String regionName, Properties
202213
if (!regionName.equals("org.hibernate.cache.internal.StandardQueryCache"))
203214
cacheName = regionName;
204215

205-
Cache cache = getCache(cacheName, QUERY_KEY, properties);
216+
AdvancedCache cache = getCache(cacheName, QUERY_KEY, properties);
206217
CacheAdapter cacheAdapter = CacheAdapterImpl.newInstance(cache);
207218
QueryResultsRegionImpl region = new QueryResultsRegionImpl(cacheAdapter, regionName, properties, transactionManager, this);
208-
region.start();
219+
startRegion(region, regionName);
209220
return region;
210221
}
211222

@@ -215,21 +226,17 @@ public QueryResultsRegion buildQueryResultsRegion(String regionName, Properties
215226
public TimestampsRegion buildTimestampsRegion(String regionName, Properties properties)
216227
throws CacheException {
217228
if (log.isDebugEnabled()) log.debug("Building timestamps cache region [" + regionName + "]");
218-
Cache cache = getCache(regionName, TIMESTAMPS_KEY, properties);
229+
AdvancedCache cache = getCache(regionName, TIMESTAMPS_KEY, properties);
219230
CacheAdapter cacheAdapter = CacheAdapterImpl.newInstance(cache);
220231
TimestampsRegionImpl region = createTimestampsRegion(cacheAdapter, regionName);
221-
region.start();
232+
startRegion(region, regionName);
222233
return region;
223234
}
224235

225236
protected TimestampsRegionImpl createTimestampsRegion(CacheAdapter cacheAdapter, String regionName) {
226237
return new TimestampsRegionImpl(cacheAdapter, regionName, transactionManager, this);
227238
}
228239

229-
protected TransactionManager getTransactionManager() {
230-
return transactionManager;
231-
}
232-
233240
/**
234241
* {@inheritDoc}
235242
*/
@@ -270,7 +277,7 @@ public void start(Settings settings, Properties properties) throws CacheExceptio
270277
Enumeration keys = properties.propertyNames();
271278
while (keys.hasMoreElements()) {
272279
String key = (String) keys.nextElement();
273-
int prefixLoc = -1;
280+
int prefixLoc;
274281
if ((prefixLoc = key.indexOf(PREFIX)) != -1) {
275282
dissectProperty(prefixLoc, key, properties);
276283
}
@@ -310,11 +317,15 @@ public Set<String> getDefinedConfigurations() {
310317
return Collections.unmodifiableSet(definedConfigurations);
311318
}
312319

320+
public BaseRegion getRegion(String regionName) {
321+
return allRegions.get(regionName);
322+
}
323+
313324
protected EmbeddedCacheManager createCacheManager(Properties properties) throws CacheException {
314325
try {
315326
String configLoc = ConfigurationHelper.getString(INFINISPAN_CONFIG_RESOURCE_PROP, properties, DEF_INFINISPAN_CONFIG_RESOURCE);
316327
EmbeddedCacheManager manager = new DefaultCacheManager(configLoc, false);
317-
String globalStats = ConfigurationHelper.extractPropertyValue(INFINISPAN_GLOBAL_STATISTICS_PROP, properties);
328+
String globalStats = extractProperty(INFINISPAN_GLOBAL_STATISTICS_PROP, properties);
318329
if (globalStats != null) {
319330
manager.getGlobalConfiguration().setExposeGlobalJmxStatistics(Boolean.parseBoolean(globalStats));
320331
}
@@ -325,6 +336,10 @@ protected EmbeddedCacheManager createCacheManager(Properties properties) throws
325336
}
326337
}
327338

339+
private void startRegion(BaseRegion region, String regionName) {
340+
allRegions.put(regionName, region);
341+
}
342+
328343
private Map<String, TypeOverrides> initGenericDataTypeOverrides() {
329344
TypeOverrides entityOverrides = new TypeOverrides();
330345
entityOverrides.setCacheName(DEF_ENTITY_RESOURCE);
@@ -342,31 +357,33 @@ private Map<String, TypeOverrides> initGenericDataTypeOverrides() {
342357
}
343358

344359
private void dissectProperty(int prefixLoc, String key, Properties properties) {
345-
TypeOverrides cfgOverride = null;
346-
int suffixLoc = -1;
360+
TypeOverrides cfgOverride;
361+
int suffixLoc;
347362
if (!key.equals(INFINISPAN_CONFIG_RESOURCE_PROP) && (suffixLoc = key.indexOf(CONFIG_SUFFIX)) != -1) {
348363
cfgOverride = getOrCreateConfig(prefixLoc, key, suffixLoc);
349-
cfgOverride.setCacheName( ConfigurationHelper.extractPropertyValue(key, properties));
364+
cfgOverride.setCacheName(extractProperty(key, properties));
350365
} else if ((suffixLoc = key.indexOf(STRATEGY_SUFFIX)) != -1) {
351366
cfgOverride = getOrCreateConfig(prefixLoc, key, suffixLoc);
352-
cfgOverride.setEvictionStrategy( ConfigurationHelper.extractPropertyValue(key, properties));
367+
cfgOverride.setEvictionStrategy(extractProperty(key, properties));
353368
} else if ((suffixLoc = key.indexOf(WAKE_UP_INTERVAL_SUFFIX)) != -1) {
354369
cfgOverride = getOrCreateConfig(prefixLoc, key, suffixLoc);
355-
cfgOverride.setEvictionWakeUpInterval(Long.parseLong( ConfigurationHelper.extractPropertyValue(key, properties)));
370+
cfgOverride.setEvictionWakeUpInterval(Long.parseLong(extractProperty(key, properties)));
356371
} else if ((suffixLoc = key.indexOf(MAX_ENTRIES_SUFFIX)) != -1) {
357372
cfgOverride = getOrCreateConfig(prefixLoc, key, suffixLoc);
358-
cfgOverride.setEvictionMaxEntries( ConfigurationHelper.getInt(key, properties, -1));
373+
cfgOverride.setEvictionMaxEntries(Integer.parseInt(extractProperty(key, properties)));
359374
} else if ((suffixLoc = key.indexOf(LIFESPAN_SUFFIX)) != -1) {
360375
cfgOverride = getOrCreateConfig(prefixLoc, key, suffixLoc);
361-
cfgOverride.setExpirationLifespan(Long.parseLong( ConfigurationHelper.extractPropertyValue(key, properties)));
376+
cfgOverride.setExpirationLifespan(Long.parseLong(extractProperty(key, properties)));
362377
} else if ((suffixLoc = key.indexOf(MAX_IDLE_SUFFIX)) != -1) {
363378
cfgOverride = getOrCreateConfig(prefixLoc, key, suffixLoc);
364-
cfgOverride.setExpirationMaxIdle(Long.parseLong( ConfigurationHelper.extractPropertyValue(key, properties)));
379+
cfgOverride.setExpirationMaxIdle(Long.parseLong(extractProperty(key, properties)));
365380
}
366-
// else if ((suffixLoc = key.indexOf(STATISTICS_SUFFIX)) != -1) {
367-
// cfgOverride = getOrCreateConfig(prefixLoc, key, suffixLoc);
368-
// cfgOverride.setExposeStatistics(Boolean.parseBoolean(PropertiesHelper.extractPropertyValue(key, properties)));
369-
// }
381+
}
382+
383+
private String extractProperty(String key, Properties properties) {
384+
String value = ConfigurationHelper.extractPropertyValue(key, properties);
385+
log.debugf("Configuration override via property %s: %s", key, value);
386+
return value;
370387
}
371388

372389
private TypeOverrides getOrCreateConfig(int prefixLoc, String key, int suffixLoc) {
@@ -395,7 +412,7 @@ private void defineGenericDataTypeCacheConfigurations(Settings settings, Propert
395412
}
396413
}
397414

398-
private Cache getCache(String regionName, String typeKey, Properties properties) {
415+
private AdvancedCache getCache(String regionName, String typeKey, Properties properties) {
399416
TypeOverrides regionOverride = typeOverrides.get(regionName);
400417
if (!definedConfigurations.contains(regionName)) {
401418
String templateCacheName = null;
@@ -420,11 +437,17 @@ private Cache getCache(String regionName, String typeKey, Properties properties)
420437
manager.defineConfiguration(regionName, templateCacheName, regionCacheCfg);
421438
definedConfigurations.add(regionName);
422439
}
423-
Cache cache = manager.getCache(regionName);
440+
AdvancedCache cache = manager.getCache(regionName).getAdvancedCache();
424441
if (!cache.getStatus().allowInvocations()) {
425442
cache.start();
426443
}
427-
return createCacheWrapper(cache.getAdvancedCache());
444+
ComponentRegistry cr = cache.getComponentRegistry();
445+
cr.getComponent(CacheCommandInitializer.class).setRegionFactory(this);
446+
GlobalComponentRegistry globalCr = cache.getComponentRegistry().getGlobalComponentRegistry();
447+
// TODO: This is a hack, make it easier to retrieve in Infinispan!
448+
((CacheCommandFactory) ((Map) globalCr.getComponent("org.infinispan.modules.command.factories"))
449+
.values().iterator().next()).setRegionFactory(this);
450+
return createCacheWrapper(cache);
428451
}
429452

430453
protected ClassLoaderAwareCache createCacheWrapper(AdvancedCache cache) {
@@ -434,24 +457,26 @@ protected ClassLoaderAwareCache createCacheWrapper(AdvancedCache cache) {
434457
private Configuration configureTransactionManager(Configuration regionOverrides, String templateCacheName, Properties properties) {
435458
// Get existing configuration to verify whether a tm was configured or not.
436459
Configuration templateConfig = manager.defineConfiguration(templateCacheName, new Configuration());
437-
String ispnTmLookupClassName = templateConfig.getTransactionManagerLookupClass();
438-
String hbTmLookupClassName = org.hibernate.cache.infinispan.tm.HibernateTransactionManagerLookup.class.getName();
439-
if (ispnTmLookupClassName != null && !ispnTmLookupClassName.equals(hbTmLookupClassName)) {
440-
log.debug("Infinispan is configured [" + ispnTmLookupClassName + "] with a different transaction manager lookup " +
441-
"class than Hibernate [" + hbTmLookupClassName + "]");
442-
} else {
443-
regionOverrides.setTransactionManagerLookup(transactionManagerlookup);
444-
}
460+
if (templateConfig.isTransactionalCache()) {
461+
String ispnTmLookupClassName = templateConfig.getTransactionManagerLookupClass();
462+
String hbTmLookupClassName = org.hibernate.cache.infinispan.tm.HibernateTransactionManagerLookup.class.getName();
463+
if (ispnTmLookupClassName != null && !ispnTmLookupClassName.equals(hbTmLookupClassName)) {
464+
log.debug("Infinispan is configured [" + ispnTmLookupClassName + "] with a different transaction manager lookup " +
465+
"class than Hibernate [" + hbTmLookupClassName + "]");
466+
} else {
467+
regionOverrides.setTransactionManagerLookup(transactionManagerlookup);
468+
}
445469

446-
String useSyncProp = ConfigurationHelper.extractPropertyValue(INFINISPAN_USE_SYNCHRONIZATION_PROP, properties);
447-
boolean useSync = useSyncProp == null ? DEF_USE_SYNCHRONIZATION : Boolean.parseBoolean(useSyncProp);
448-
regionOverrides.fluent().transaction().useSynchronization(useSync);
470+
String useSyncProp = extractProperty(INFINISPAN_USE_SYNCHRONIZATION_PROP, properties);
471+
boolean useSync = useSyncProp == null ? DEF_USE_SYNCHRONIZATION : Boolean.parseBoolean(useSyncProp);
472+
regionOverrides.fluent().transaction().useSynchronization(useSync);
473+
}
449474

450475
return regionOverrides;
451476
}
452477

453478
private TypeOverrides overrideStatisticsIfPresent(TypeOverrides override, Properties properties) {
454-
String globalStats = ConfigurationHelper.extractPropertyValue(INFINISPAN_GLOBAL_STATISTICS_PROP, properties);
479+
String globalStats = extractProperty(INFINISPAN_GLOBAL_STATISTICS_PROP, properties);
455480
if (globalStats != null) {
456481
override.setExposeStatistics(Boolean.parseBoolean(globalStats));
457482
}

hibernate-infinispan/src/main/java/org/hibernate/cache/infinispan/access/TransactionalAccessDelegate.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@
2222
* Boston, MA 02110-1301 USA
2323
*/
2424
package org.hibernate.cache.infinispan.access;
25+
2526
import javax.transaction.Transaction;
2627
import org.hibernate.cache.CacheException;
2728
import org.hibernate.cache.spi.access.CollectionRegionAccessStrategy;
2829
import org.hibernate.cache.spi.access.EntityRegionAccessStrategy;
2930
import org.hibernate.cache.spi.access.SoftLock;
3031
import org.hibernate.cache.infinispan.impl.BaseRegion;
3132
import org.hibernate.cache.infinispan.util.CacheAdapter;
32-
import org.hibernate.cache.infinispan.util.CacheHelper;
3333
import org.hibernate.cache.infinispan.util.FlagAdapter;
3434
import org.infinispan.util.logging.Log;
3535
import org.infinispan.util.logging.LogFactory;
@@ -162,7 +162,8 @@ public void evictAll() throws CacheException {
162162
}
163163
Transaction tx = region.suspend();
164164
try {
165-
CacheHelper.sendEvictAllNotification(cacheAdapter, region.getAddress());
165+
region.invalidateRegion(); // Invalidate the local region and then go remote
166+
cacheAdapter.broadcastEvictAll();
166167
} finally {
167168
region.resume(tx);
168169
}

0 commit comments

Comments
 (0)