HHH-7763 No need to clear caches when these are going to be stopped

* Make sure failures in stop won't affect other crucial stop operations
that could leave nodes unstopped.
This commit is contained in:
Galder Zamarreño 2012-11-07 16:53:01 +01:00
parent 4294e0faee
commit 49ea5d65df
2 changed files with 515 additions and 533 deletions

View File

@ -25,18 +25,15 @@ package org.hibernate.test.cache.infinispan;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.concurrent.Callable;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cache.infinispan.InfinispanRegionFactory; import org.hibernate.cache.infinispan.InfinispanRegionFactory;
import org.hibernate.cache.infinispan.collection.CollectionRegionImpl; import org.hibernate.cache.infinispan.collection.CollectionRegionImpl;
import org.hibernate.cache.infinispan.entity.EntityRegionImpl; import org.hibernate.cache.infinispan.entity.EntityRegionImpl;
import org.hibernate.cache.infinispan.util.Caches;
import org.hibernate.cache.spi.CacheDataDescription; import org.hibernate.cache.spi.CacheDataDescription;
import org.hibernate.cfg.Configuration; import org.hibernate.cfg.Configuration;
import org.hibernate.boot.registry.internal.StandardServiceRegistryImpl; import org.hibernate.boot.registry.internal.StandardServiceRegistryImpl;
import org.hibernate.test.cache.infinispan.util.CacheTestUtil; import org.hibernate.test.cache.infinispan.util.CacheTestUtil;
import org.infinispan.context.Flag;
/** /**
* Defines the environment for a node. * Defines the environment for a node.
@ -44,6 +41,7 @@ import org.infinispan.context.Flag;
* @author Steve Ebersole * @author Steve Ebersole
*/ */
public class NodeEnvironment { public class NodeEnvironment {
private final Configuration configuration; private final Configuration configuration;
private StandardServiceRegistryImpl serviceRegistry; private StandardServiceRegistryImpl serviceRegistry;
@ -117,38 +115,39 @@ public class NodeEnvironment {
} }
public void release() throws Exception { public void release() throws Exception {
try {
if (entityRegionMap != null) { if (entityRegionMap != null) {
for ( final EntityRegionImpl region : entityRegionMap.values() ) { for (EntityRegionImpl region : entityRegionMap.values()) {
Caches.withinTx(region.getTransactionManager(), new Callable<Void>() { try {
@Override
public Void call() throws Exception {
region.getCache().withFlags(Flag.CACHE_MODE_LOCAL).clear();
return null;
}
});
region.getCache().stop(); region.getCache().stop();
} catch (Exception e) {
// Ignore...
}
} }
entityRegionMap.clear(); entityRegionMap.clear();
} }
if (collectionRegionMap != null) { if (collectionRegionMap != null) {
for ( final CollectionRegionImpl collectionRegion : collectionRegionMap.values() ) { for (CollectionRegionImpl reg : collectionRegionMap.values()) {
Caches.withinTx(collectionRegion.getTransactionManager(), new Callable<Void>() { try {
@Override reg.getCache().stop();
public Void call() throws Exception { } catch (Exception e) {
collectionRegion.getCache().withFlags(Flag.CACHE_MODE_LOCAL).clear(); // Ignore...
return null;
} }
});
collectionRegion.getCache().stop();
} }
collectionRegionMap.clear(); collectionRegionMap.clear();
} }
} finally {
try {
if (regionFactory != null) { if (regionFactory != null) {
// Currently the RegionFactory is shutdown by its registration with the CacheTestSetup from CacheTestUtil when built // Currently the RegionFactory is shutdown by its registration
// with the CacheTestSetup from CacheTestUtil when built
regionFactory.stop(); regionFactory.stop();
} }
} finally {
if (serviceRegistry != null) { if (serviceRegistry != null) {
serviceRegistry.destroy(); serviceRegistry.destroy();
} }
} }
} }
}
}

View File

@ -61,6 +61,7 @@ import static org.junit.Assert.assertTrue;
* @since 3.5 * @since 3.5
*/ */
public abstract class AbstractEntityRegionAccessStrategyTestCase extends AbstractNonFunctionalTestCase { public abstract class AbstractEntityRegionAccessStrategyTestCase extends AbstractNonFunctionalTestCase {
private static final Logger log = Logger.getLogger(AbstractEntityRegionAccessStrategyTestCase.class); private static final Logger log = Logger.getLogger(AbstractEntityRegionAccessStrategyTestCase.class);
public static final String REGION_NAME = "test/com.foo.test"; public static final String REGION_NAME = "test/com.foo.test";
@ -136,13 +137,16 @@ public abstract class AbstractEntityRegionAccessStrategyTestCase extends Abstrac
@After @After
public void releaseResources() throws Exception { public void releaseResources() throws Exception {
try {
if (localEnvironment != null) { if (localEnvironment != null) {
localEnvironment.release(); localEnvironment.release();
} }
} finally {
if (remoteEnvironment != null) { if (remoteEnvironment != null) {
remoteEnvironment.release(); remoteEnvironment.release();
} }
} }
}
protected abstract AccessType getAccessType(); protected abstract AccessType getAccessType();
@ -192,11 +196,12 @@ public abstract class AbstractEntityRegionAccessStrategyTestCase extends Abstrac
} }
/** /**
* Simulate 2 nodes, both start, tx do a get, experience a cache miss, then 'read from db.' First * Simulate 2 nodes, both start, tx do a get, experience a cache miss, then
* does a putFromLoad, then an update. Second tries to do a putFromLoad with stale data (i.e. it * 'read from db.' First does a putFromLoad, then an update. Second tries to
* took longer to read from the db). Both commit their tx. Then both start a new tx and get. * do a putFromLoad with stale data (i.e. it took longer to read from the db).
* First should see the updated data; second should either see the updated data (isInvalidation() * Both commit their tx. Then both start a new tx and get. First should see
* == false) or null (isInvalidation() == true). * the updated data; second should either see the updated data
* (isInvalidation() == false) or null (isInvalidation() == true).
* *
* @param useMinimalAPI * @param useMinimalAPI
* @throws Exception * @throws Exception
@ -224,25 +229,21 @@ public abstract class AbstractEntityRegionAccessStrategyTestCase extends Abstrac
if (useMinimalAPI) { if (useMinimalAPI) {
localAccessStrategy.putFromLoad(KEY, VALUE1, txTimestamp, new Integer(1), true); localAccessStrategy.putFromLoad(KEY, VALUE1, txTimestamp, new Integer(1), true);
} } else {
else {
localAccessStrategy.putFromLoad(KEY, VALUE1, txTimestamp, new Integer(1)); localAccessStrategy.putFromLoad(KEY, VALUE1, txTimestamp, new Integer(1));
} }
localAccessStrategy.update(KEY, VALUE2, new Integer(2), new Integer(1)); localAccessStrategy.update(KEY, VALUE2, new Integer(2), new Integer(1));
BatchModeTransactionManager.getInstance().commit(); BatchModeTransactionManager.getInstance().commit();
} } catch (Exception e) {
catch (Exception e) {
log.error("node1 caught exception", e); log.error("node1 caught exception", e);
node1Exception = e; node1Exception = e;
rollback(); rollback();
} } catch (AssertionFailedError e) {
catch (AssertionFailedError e) {
node1Failure = e; node1Failure = e;
rollback(); rollback();
} } finally {
finally {
// Let node2 write // Let node2 write
writeLatch2.countDown(); writeLatch2.countDown();
completionLatch.countDown(); completionLatch.countDown();
@ -268,23 +269,19 @@ public abstract class AbstractEntityRegionAccessStrategyTestCase extends Abstrac
if (useMinimalAPI) { if (useMinimalAPI) {
remoteAccessStrategy.putFromLoad(KEY, VALUE1, txTimestamp, new Integer(1), true); remoteAccessStrategy.putFromLoad(KEY, VALUE1, txTimestamp, new Integer(1), true);
} } else {
else {
remoteAccessStrategy.putFromLoad(KEY, VALUE1, txTimestamp, new Integer(1)); remoteAccessStrategy.putFromLoad(KEY, VALUE1, txTimestamp, new Integer(1));
} }
BatchModeTransactionManager.getInstance().commit(); BatchModeTransactionManager.getInstance().commit();
} } catch (Exception e) {
catch (Exception e) {
log.error("node2 caught exception", e); log.error("node2 caught exception", e);
node2Exception = e; node2Exception = e;
rollback(); rollback();
} } catch (AssertionFailedError e) {
catch (AssertionFailedError e) {
node2Failure = e; node2Failure = e;
rollback(); rollback();
} } finally {
finally {
completionLatch.countDown(); completionLatch.countDown();
} }
} }
@ -306,8 +303,7 @@ public abstract class AbstractEntityRegionAccessStrategyTestCase extends Abstrac
if (isUsingInvalidation()) { if (isUsingInvalidation()) {
// no data version to prevent the PFER; we count on db locks preventing this // no data version to prevent the PFER; we count on db locks preventing this
assertEquals("Expected node2 value", VALUE1, remoteAccessStrategy.get(KEY, txTimestamp)); assertEquals("Expected node2 value", VALUE1, remoteAccessStrategy.get(KEY, txTimestamp));
} } else {
else {
// The node1 update is replicated, preventing the node2 PFER // The node1 update is replicated, preventing the node2 PFER
assertEquals("Correct node2 value", VALUE2, remoteAccessStrategy.get(KEY, txTimestamp)); assertEquals("Correct node2 value", VALUE2, remoteAccessStrategy.get(KEY, txTimestamp));
} }
@ -339,17 +335,14 @@ public abstract class AbstractEntityRegionAccessStrategyTestCase extends Abstrac
commitLatch.await(); commitLatch.await();
BatchModeTransactionManager.getInstance().commit(); BatchModeTransactionManager.getInstance().commit();
} } catch (Exception e) {
catch (Exception e) {
log.error("node1 caught exception", e); log.error("node1 caught exception", e);
node1Exception = e; node1Exception = e;
rollback(); rollback();
} } catch (AssertionFailedError e) {
catch (AssertionFailedError e) {
node1Failure = e; node1Failure = e;
rollback(); rollback();
} } finally {
finally {
completionLatch.countDown(); completionLatch.countDown();
} }
} }
@ -376,17 +369,14 @@ public abstract class AbstractEntityRegionAccessStrategyTestCase extends Abstrac
); );
BatchModeTransactionManager.getInstance().commit(); BatchModeTransactionManager.getInstance().commit();
} } catch (Exception e) {
catch (Exception e) {
log.error("node1 caught exception", e); log.error("node1 caught exception", e);
node1Exception = e; node1Exception = e;
rollback(); rollback();
} } catch (AssertionFailedError e) {
catch (AssertionFailedError e) {
node1Failure = e; node1Failure = e;
rollback(); rollback();
} } finally {
finally {
commitLatch.countDown(); commitLatch.countDown();
completionLatch.countDown(); completionLatch.countDown();
} }
@ -442,17 +432,14 @@ public abstract class AbstractEntityRegionAccessStrategyTestCase extends Abstrac
log.debug("Await commit"); log.debug("Await commit");
commitLatch.await(); commitLatch.await();
BatchModeTransactionManager.getInstance().commit(); BatchModeTransactionManager.getInstance().commit();
} } catch (Exception e) {
catch (Exception e) {
log.error("node1 caught exception", e); log.error("node1 caught exception", e);
node1Exception = e; node1Exception = e;
rollback(); rollback();
} } catch (AssertionFailedError e) {
catch (AssertionFailedError e) {
node1Failure = e; node1Failure = e;
rollback(); rollback();
} } finally {
finally {
if (!readerUnlocked) { if (!readerUnlocked) {
readLatch.countDown(); readLatch.countDown();
} }
@ -478,17 +465,14 @@ public abstract class AbstractEntityRegionAccessStrategyTestCase extends Abstrac
assertEquals("Correct value", expected, localAccessStrategy.get(KEY, txTimestamp)); assertEquals("Correct value", expected, localAccessStrategy.get(KEY, txTimestamp));
BatchModeTransactionManager.getInstance().commit(); BatchModeTransactionManager.getInstance().commit();
} } catch (Exception e) {
catch (Exception e) {
log.error("node1 caught exception", e); log.error("node1 caught exception", e);
node1Exception = e; node1Exception = e;
rollback(); rollback();
} } catch (AssertionFailedError e) {
catch (AssertionFailedError e) {
node1Failure = e; node1Failure = e;
rollback(); rollback();
} } finally {
finally {
commitLatch.countDown(); commitLatch.countDown();
log.debug("Completion latch countdown"); log.debug("Completion latch countdown");
completionLatch.countDown(); completionLatch.countDown();
@ -625,8 +609,7 @@ public abstract class AbstractEntityRegionAccessStrategyTestCase extends Abstrac
protected void rollback() { protected void rollback() {
try { try {
BatchModeTransactionManager.getInstance().rollback(); BatchModeTransactionManager.getInstance().rollback();
} } catch (Exception e) {
catch (Exception e) {
log.error(e.getMessage(), e); log.error(e.getMessage(), e);
} }
} }