[infinispan-commits] Infinispan SVN: r2393 - in trunk/core/src: main/java/org/infinispan/interceptors and 4 other directories.

infinispan-commits at lists.jboss.org infinispan-commits at lists.jboss.org
Thu Sep 16 05:15:08 EDT 2010


Author: galder.zamarreno at jboss.com
Date: 2010-09-16 05:15:08 -0400 (Thu, 16 Sep 2010)
New Revision: 2393

Added:
   trunk/core/src/test/java/org/infinispan/api/TerminatedCacheTest.java
Modified:
   trunk/core/src/main/java/org/infinispan/factories/ComponentRegistry.java
   trunk/core/src/main/java/org/infinispan/interceptors/InvocationContextInterceptor.java
   trunk/core/src/main/java/org/infinispan/lifecycle/ComponentStatus.java
   trunk/core/src/main/java/org/infinispan/manager/DefaultCacheManager.java
   trunk/core/src/test/java/org/infinispan/manager/CacheManagerTest.java
Log:
[ISPN-649] (Caches in TERMINATED mode should not allow put or any modifications) Cache and cache manager now check early on that no new operations can be invoked on them once they've transitioned to TERMINATED.

Modified: trunk/core/src/main/java/org/infinispan/factories/ComponentRegistry.java
===================================================================
--- trunk/core/src/main/java/org/infinispan/factories/ComponentRegistry.java	2010-09-16 05:48:00 UTC (rev 2392)
+++ trunk/core/src/main/java/org/infinispan/factories/ComponentRegistry.java	2010-09-16 09:15:08 UTC (rev 2393)
@@ -171,4 +171,9 @@
          cacheManagerNotifier.notifyCacheStopped(cacheName);
       }
    }
+
+   public String getCacheName() {
+      return cacheName;
+   }
+
 }

Modified: trunk/core/src/main/java/org/infinispan/interceptors/InvocationContextInterceptor.java
===================================================================
--- trunk/core/src/main/java/org/infinispan/interceptors/InvocationContextInterceptor.java	2010-09-16 05:48:00 UTC (rev 2392)
+++ trunk/core/src/main/java/org/infinispan/interceptors/InvocationContextInterceptor.java	2010-09-16 09:15:08 UTC (rev 2393)
@@ -26,8 +26,11 @@
 import org.infinispan.commands.VisitableCommand;
 import org.infinispan.context.Flag;
 import org.infinispan.context.InvocationContext;
+import org.infinispan.factories.ComponentRegistry;
 import org.infinispan.factories.annotations.Inject;
 import org.infinispan.interceptors.base.CommandInterceptor;
+import org.infinispan.lifecycle.ComponentStatus;
+import org.infinispan.manager.CacheContainer;
 
 import javax.transaction.Status;
 import javax.transaction.SystemException;
@@ -40,10 +43,12 @@
 public class InvocationContextInterceptor extends CommandInterceptor {
 
    private TransactionManager tm;
+   private ComponentRegistry componentRegistry;
 
    @Inject
-   public void init(TransactionManager tm) {
+   public void init(TransactionManager tm, ComponentRegistry componentRegistry) {
       this.tm = tm;
+      this.componentRegistry = componentRegistry;
    }
 
    @Override
@@ -54,6 +59,15 @@
    private Object handleAll(InvocationContext ctx, VisitableCommand command) throws Throwable {
       boolean suppressExceptions = false;
 
+      ComponentStatus status = componentRegistry.getStatus();
+      if (status.isTerminated()) {
+         String cacheName = componentRegistry.getCacheName();
+         String prefix = "Cache '" + cacheName + "'";
+         if (cacheName.equals(CacheContainer.DEFAULT_CACHE_NAME))
+            prefix = "Default cache";
+         throw new IllegalStateException(prefix + " is in 'TERMINATED' state and so it does not accept new invocations. Either restart it or recreate the cache container.");
+      }
+
       if (trace) log.trace("Invoked with command " + command + " and InvocationContext [" + ctx + "]");
       if (ctx == null) throw new IllegalStateException("Null context not allowed!!");
 
@@ -102,4 +116,4 @@
       }
       return status == Status.STATUS_ACTIVE || status == Status.STATUS_PREPARING;
    }
-}
\ No newline at end of file
+}

Modified: trunk/core/src/main/java/org/infinispan/lifecycle/ComponentStatus.java
===================================================================
--- trunk/core/src/main/java/org/infinispan/lifecycle/ComponentStatus.java	2010-09-16 05:48:00 UTC (rev 2392)
+++ trunk/core/src/main/java/org/infinispan/lifecycle/ComponentStatus.java	2010-09-16 09:15:08 UTC (rev 2393)
@@ -102,4 +102,8 @@
    public boolean startingUp() {
       return this == ComponentStatus.RUNNING || this == ComponentStatus.INITIALIZING || this == ComponentStatus.INSTANTIATED;
    }
+
+   public boolean isTerminated() {
+      return this == ComponentStatus.TERMINATED;
+   }
 }

Modified: trunk/core/src/main/java/org/infinispan/manager/DefaultCacheManager.java
===================================================================
--- trunk/core/src/main/java/org/infinispan/manager/DefaultCacheManager.java	2010-09-16 05:48:00 UTC (rev 2392)
+++ trunk/core/src/main/java/org/infinispan/manager/DefaultCacheManager.java	2010-09-16 09:15:08 UTC (rev 2393)
@@ -376,6 +376,7 @@
 
    private Configuration defineConfiguration(String cacheName, Configuration configOverride,
                                              Configuration defaultConfigIfNotPresent, boolean checkExisting) {
+      assertIsNotTerminated();
       if (cacheName == null || configOverride == null)
          throw new NullPointerException("Null arguments not allowed");
       if (cacheName.equals(DEFAULT_CACHE_NAME))
@@ -420,6 +421,7 @@
     */
    @SuppressWarnings("unchecked")
    public <K, V> Cache<K, V> getCache(String cacheName) {
+      assertIsNotTerminated();
       if (cacheName == null)
          throw new NullPointerException("Null arguments not allowed");
 
@@ -563,6 +565,11 @@
          return Immutables.immutableSetWrap(names);
    }
 
+   private void assertIsNotTerminated() {
+      if (globalComponentRegistry.getStatus().isTerminated())
+         throw new IllegalStateException("Cache container has been stopped and cannot be reused. Recreate the cache container.");
+   }
+
    @ManagedAttribute(description = "The status of the cache manager instance.")
    @Metric(displayName = "Cache manager status", dataType = DataType.TRAIT, displayType = DisplayType.SUMMARY)
    public String getCacheManagerStatus() {

Added: trunk/core/src/test/java/org/infinispan/api/TerminatedCacheTest.java
===================================================================
--- trunk/core/src/test/java/org/infinispan/api/TerminatedCacheTest.java	                        (rev 0)
+++ trunk/core/src/test/java/org/infinispan/api/TerminatedCacheTest.java	2010-09-16 09:15:08 UTC (rev 2393)
@@ -0,0 +1,42 @@
+package org.infinispan.api;
+
+import org.infinispan.Cache;
+import org.infinispan.manager.EmbeddedCacheManager;
+import org.infinispan.test.SingleCacheManagerTest;
+import org.infinispan.test.fwk.TestCacheManagerFactory;
+import org.testng.annotations.Test;
+
+/**
+ * Test that verifies the behaivour of Cache and CacheContainer.getCache() calls after
+ * Cache and CacheContainer instances have been stopped. This emulates redeployment
+ * scenarios under a situations where the CacheContainer is a shared resource.
+ *
+ * @author Galder Zamarre�o
+ * @since 4.2
+ */
+ at Test(groups = "functional", testName = "api.TerminatedCacheTest")
+public class TerminatedCacheTest extends SingleCacheManagerTest {
+
+   @Override
+   protected EmbeddedCacheManager createCacheManager() throws Exception {
+      return TestCacheManagerFactory.createLocalCacheManager();
+   }
+
+   @Test(expectedExceptions = IllegalStateException.class)
+   public void testCacheStopFollowedByGetCache() {
+      Cache cache = cacheManager.getCache();
+      cache.put("k", "v");
+      cache.stop();
+      Cache cache2 = cacheManager.getCache();
+      cache2.put("k", "v2");
+   }
+
+   @Test(expectedExceptions = IllegalStateException.class)
+   public void testCacheStopFollowedByCacheOp() {
+      Cache cache = cacheManager.getCache("big");
+      cache.put("k", "v");
+      cache.stop();
+      cache.put("k", "v2");
+   }
+
+}

Modified: trunk/core/src/test/java/org/infinispan/manager/CacheManagerTest.java
===================================================================
--- trunk/core/src/test/java/org/infinispan/manager/CacheManagerTest.java	2010-09-16 05:48:00 UTC (rev 2392)
+++ trunk/core/src/test/java/org/infinispan/manager/CacheManagerTest.java	2010-09-16 09:15:08 UTC (rev 2393)
@@ -150,4 +150,33 @@
       assert lazyLru.isUseLazyDeserialization();
       assert lazyLru.getEvictionStrategy() == EvictionStrategy.LRU;
    }
+
+   @Test(expectedExceptions = IllegalStateException.class)
+   public void testCacheStopManagerStopFollowedByGetCache() {
+      EmbeddedCacheManager localCacheManager = TestCacheManagerFactory.createLocalCacheManager();
+      try {
+         Cache cache = localCacheManager.getCache();
+         cache.put("k", "v");
+         cache.stop();
+         localCacheManager.stop();
+         localCacheManager.getCache();
+      } finally {
+         TestingUtil.killCacheManagers(localCacheManager);
+      }
+   }
+
+   @Test(expectedExceptions = IllegalStateException.class)
+   public void testCacheStopManagerStopFollowedByCacheOp() {
+      EmbeddedCacheManager localCacheManager = TestCacheManagerFactory.createLocalCacheManager();
+      try {
+         Cache cache = localCacheManager.getCache();
+         cache.put("k", "v");
+         cache.stop();
+         localCacheManager.stop();
+         cache.put("k", "v2");
+      } finally {
+         TestingUtil.killCacheManagers(localCacheManager);
+      }
+   }
+   
 }



More information about the infinispan-commits mailing list