Skip to content

Commit

Permalink
HHH-7007 Evict all should work now in multi-region factory environments
Browse files Browse the repository at this point in the history
  • Loading branch information
galderz committed Feb 2, 2012
1 parent 5eee526 commit 13c9dbf
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,10 @@
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;
Expand All @@ -36,7 +33,6 @@
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;
Expand Down Expand Up @@ -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.
*/
Expand Down Expand Up @@ -299,7 +292,8 @@ protected HibernateTransactionManagerLookup createTransactionManagerLookup(
* {@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();
}

Expand All @@ -317,10 +311,6 @@ public Set<String> getDefinedConfigurations() {
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);
Expand All @@ -337,7 +327,8 @@ protected EmbeddedCacheManager createCacheManager(Properties properties) throws
}

private void startRegion(BaseRegion region, String regionName) {
allRegions.put(regionName, region);
getCacheCommandFactory(region.getCacheAdapter().getCache().getAdvancedCache())
.addRegion(regionName, region);
}

private Map<String, TypeOverrides> initGenericDataTypeOverrides() {
Expand Down Expand Up @@ -441,13 +432,14 @@ private AdvancedCache getCache(String regionName, String typeKey, Properties pro
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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -16,10 +19,15 @@
*/
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
Expand All @@ -34,7 +42,7 @@ public CacheRpcCommand fromStream(byte commandId, Object[] args, String cacheNam
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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -12,14 +11,12 @@
*/
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
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -13,16 +12,19 @@
*/
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;
}
Expand Down

0 comments on commit 13c9dbf

Please sign in to comment.