HHH-7007 Evict all should work now in multi-region factory environments

This commit is contained in:
Galder Zamarreño 2012-02-02 10:58:47 +01:00
parent 5eee526234
commit 13c9dbfc21
4 changed files with 34 additions and 35 deletions

View File

@ -8,13 +8,10 @@ import java.util.HashSet;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import javax.transaction.TransactionManager;
import org.hibernate.cache.infinispan.impl.BaseRegion;
import org.hibernate.cache.infinispan.util.CacheCommandFactory;
import org.hibernate.cache.infinispan.util.CacheCommandInitializer;
import org.hibernate.cache.spi.CacheDataDescription;
import org.hibernate.cache.CacheException;
import org.hibernate.cache.spi.CollectionRegion;
@ -36,7 +33,6 @@ import org.hibernate.cfg.Settings;
import org.hibernate.internal.util.config.ConfigurationHelper;
import org.infinispan.AdvancedCache;
import org.infinispan.config.Configuration;
import org.infinispan.factories.ComponentRegistry;
import org.infinispan.factories.GlobalComponentRegistry;
import org.infinispan.manager.DefaultCacheManager;
import org.infinispan.manager.EmbeddedCacheManager;
@ -164,9 +160,6 @@ public class InfinispanRegionFactory implements RegionFactory {
private TransactionManager transactionManager;
private ConcurrentMap<String, BaseRegion> allRegions =
new ConcurrentHashMap<String, BaseRegion>();
/**
* Create a new instance using the default configuration.
*/
@ -299,7 +292,8 @@ public class InfinispanRegionFactory implements RegionFactory {
* {@inheritDoc}
*/
public void stop() {
log.debug("Stopping Infinispan CacheManager");
log.debug("Clear region references and stop Infinispan cache manager");
getCacheCommandFactory(manager.getCache().getAdvancedCache()).clearRegions();
manager.stop();
}
@ -317,10 +311,6 @@ public class InfinispanRegionFactory implements RegionFactory {
return Collections.unmodifiableSet(definedConfigurations);
}
public BaseRegion getRegion(String regionName) {
return allRegions.get(regionName);
}
protected EmbeddedCacheManager createCacheManager(Properties properties) throws CacheException {
try {
String configLoc = ConfigurationHelper.getString(INFINISPAN_CONFIG_RESOURCE_PROP, properties, DEF_INFINISPAN_CONFIG_RESOURCE);
@ -337,7 +327,8 @@ public class InfinispanRegionFactory implements RegionFactory {
}
private void startRegion(BaseRegion region, String regionName) {
allRegions.put(regionName, region);
getCacheCommandFactory(region.getCacheAdapter().getCache().getAdvancedCache())
.addRegion(regionName, region);
}
private Map<String, TypeOverrides> initGenericDataTypeOverrides() {
@ -441,13 +432,14 @@ public class InfinispanRegionFactory implements RegionFactory {
if (!cache.getStatus().allowInvocations()) {
cache.start();
}
ComponentRegistry cr = cache.getComponentRegistry();
cr.getComponent(CacheCommandInitializer.class).setRegionFactory(this);
return createCacheWrapper(cache);
}
private CacheCommandFactory getCacheCommandFactory(AdvancedCache cache) {
GlobalComponentRegistry globalCr = cache.getComponentRegistry().getGlobalComponentRegistry();
// TODO: This is a hack, make it easier to retrieve in Infinispan!
((CacheCommandFactory) ((Map) globalCr.getComponent("org.infinispan.modules.command.factories"))
.values().iterator().next()).setRegionFactory(this);
return createCacheWrapper(cache);
return (CacheCommandFactory) ((Map) globalCr.getComponent("org.infinispan.modules.command.factories"))
.values().iterator().next();
}
protected AdvancedCache createCacheWrapper(AdvancedCache cache) {

View File

@ -1,12 +1,15 @@
package org.hibernate.cache.infinispan.util;
import org.hibernate.cache.infinispan.InfinispanRegionFactory;
import org.hibernate.cache.infinispan.impl.BaseRegion;
import org.infinispan.commands.ReplicableCommand;
import org.infinispan.commands.module.ExtendedModuleCommandFactory;
import org.infinispan.commands.remote.CacheRpcCommand;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
/**
* Command factory
@ -16,10 +19,15 @@ import java.util.Map;
*/
public class CacheCommandFactory implements ExtendedModuleCommandFactory {
private InfinispanRegionFactory regionFactory;
private ConcurrentMap<String, BaseRegion> allRegions =
new ConcurrentHashMap<String, BaseRegion>();
public void setRegionFactory(InfinispanRegionFactory regionFactory) {
this.regionFactory = regionFactory;
public void addRegion(String regionName, BaseRegion region) {
allRegions.put(regionName, region);
}
public void clearRegions() {
allRegions.clear();
}
@Override
@ -34,7 +42,7 @@ public class CacheCommandFactory implements ExtendedModuleCommandFactory {
CacheRpcCommand c;
switch (commandId) {
case CacheCommandIds.EVICT_ALL:
c = new EvictAllCommand(cacheName, regionFactory);
c = new EvictAllCommand(cacheName, allRegions.get(cacheName));
break;
default:
throw new IllegalArgumentException("Not registered to handle command id " + commandId);

View File

@ -1,6 +1,5 @@
package org.hibernate.cache.infinispan.util;
import org.hibernate.cache.infinispan.InfinispanRegionFactory;
import org.infinispan.commands.ReplicableCommand;
import org.infinispan.commands.module.ModuleCommandInitializer;
@ -12,14 +11,12 @@ import org.infinispan.commands.module.ModuleCommandInitializer;
*/
public class CacheCommandInitializer implements ModuleCommandInitializer {
private InfinispanRegionFactory regionFactory;
public void setRegionFactory(InfinispanRegionFactory regionFactory) {
this.regionFactory = regionFactory;
}
public EvictAllCommand buildEvictAllCommand(String regionName) {
return new EvictAllCommand(regionName, regionFactory);
// No need to pass region factory because no information on that object
// is sent around the cluster. However, when the command factory builds
// and evict all command remotely, it does need to initialize it with
// the right region factory so that it can call it back.
return new EvictAllCommand(regionName);
}
@Override

View File

@ -1,6 +1,5 @@
package org.hibernate.cache.infinispan.util;
import org.hibernate.cache.infinispan.InfinispanRegionFactory;
import org.hibernate.cache.infinispan.impl.BaseRegion;
import org.infinispan.commands.remote.BaseRpcCommand;
import org.infinispan.context.InvocationContext;
@ -13,16 +12,19 @@ import org.infinispan.context.InvocationContext;
*/
public class EvictAllCommand extends BaseRpcCommand {
private InfinispanRegionFactory regionFactory;
private final BaseRegion region;
public EvictAllCommand(String regionName, InfinispanRegionFactory regionFactory) {
public EvictAllCommand(String regionName, BaseRegion region) {
super(regionName); // region name and cache names are the same...
this.regionFactory = regionFactory;
this.region = region;
}
public EvictAllCommand(String regionName) {
this(regionName, null);
}
@Override
public Object perform(InvocationContext ctx) throws Throwable {
BaseRegion region = regionFactory.getRegion(cacheName);
region.invalidateRegion();
return null;
}