Introduce ASYNC caches and use them in FRM 10/1410/1
authorGiovanni Meo <gmeo@cisco.com>
Wed, 25 Sep 2013 07:43:39 +0000 (09:43 +0200)
committerGiovanni Meo <gmeo@cisco.com>
Wed, 25 Sep 2013 09:31:00 +0000 (11:31 +0200)
- Add the option in clustering.services contract to allocate caches
that are async
- Use them for FRM workOrder and workStatus
- Add unit tests for sync/async incompatibility

Change-Id: Icfc8d9b8a9277d97b095805cb4323a950c82ef78
Signed-off-by: Giovanni Meo <gmeo@cisco.com>
opendaylight/clustering/services/src/main/java/org/opendaylight/controller/clustering/services/IClusterServices.java
opendaylight/clustering/services_implementation/src/main/java/org/opendaylight/controller/clustering/services_implementation/internal/ClusterManager.java
opendaylight/clustering/services_implementation/src/test/java/org/opendaylight/controller/clustering/services_implementation/internal/ClusterManagerTest.java
opendaylight/forwardingrulesmanager/implementation/src/main/java/org/opendaylight/controller/forwardingrulesmanager/internal/ForwardingRulesManager.java

index 5551f23..77e300e 100644 (file)
@@ -62,7 +62,19 @@ public interface IClusterServices {
          * immediately committed in the cache.
          *
          */
-        NON_TRANSACTIONAL;
+        NON_TRANSACTIONAL,
+        /**
+         * Set on a cache that can transfer the updates asynchronously from the
+         * calling thread. The caller when doing put/clear/remove cannot expect
+         * that the operation has happened clusterwide
+         */
+        ASYNC,
+        /**
+         * Set on a cache that transfer the updates synchronously to the calling
+         * thread so when getting back the operation is supposed to have
+         * completed on all the cluster nodes. Slow but safe.
+         */
+        SYNC;
     }
 
     /**
@@ -237,6 +249,7 @@ public interface IClusterServices {
      *
      * @return true if the role is the one of standby, else false
      */
+    @Deprecated
     boolean amIStandby();
 
     /**
@@ -247,6 +260,7 @@ public interface IClusterServices {
      *
      * @return Address of the active controller
      */
+    @Deprecated
     InetAddress getActiveAddress();
 
     /**
@@ -271,6 +285,7 @@ public interface IClusterServices {
      *
      * @param i Interface that will be called when the Role Change happens
      */
+    @Deprecated
     void listenRoleChange(IListenRoleChange i)
             throws ListenRoleChangeAddException;
 
@@ -281,5 +296,6 @@ public interface IClusterServices {
      *
      * @param i Interface that will be called when the Role Change happens
      */
+    @Deprecated
     void unlistenRoleChange(IListenRoleChange i);
 }
index 4a67e76..122063a 100644 (file)
@@ -33,6 +33,7 @@ import javax.transaction.TransactionManager;
 
 import org.infinispan.Cache;
 import org.infinispan.configuration.cache.Configuration;
+import org.infinispan.configuration.cache.ConfigurationBuilder;
 import org.infinispan.configuration.global.GlobalConfigurationBuilder;
 import org.infinispan.configuration.parsing.ConfigurationBuilderHolder;
 import org.infinispan.configuration.parsing.ParserRegistry;
@@ -304,24 +305,54 @@ public class ClusterManager implements IClusterServices {
             throw new CacheExistException();
         }
 
-        // Sanity check to avoid contrasting parameters
-        if (cMode.containsAll(EnumSet.of(
-                IClusterServices.cacheMode.NON_TRANSACTIONAL,
+        // Sanity check to avoid contrasting parameters between transactional
+        // and not
+        if (cMode.containsAll(EnumSet.of(IClusterServices.cacheMode.NON_TRANSACTIONAL,
                 IClusterServices.cacheMode.TRANSACTIONAL))) {
             throw new CacheConfigException();
         }
 
-        if (cMode.contains(IClusterServices.cacheMode.NON_TRANSACTIONAL)) {
-            c = manager.getCache(realCacheName);
-            return c;
-        } else if (cMode.contains(IClusterServices.cacheMode.TRANSACTIONAL)) {
-            Configuration rc = manager
-                    .getCacheConfiguration("transactional-type");
-            manager.defineConfiguration(realCacheName, rc);
-            c = manager.getCache(realCacheName);
-            return c;
+        // Sanity check to avoid contrasting parameters between sync and async
+        if (cMode.containsAll(EnumSet.of(IClusterServices.cacheMode.SYNC, IClusterServices.cacheMode.ASYNC))) {
+            throw new CacheConfigException();
         }
-        return null;
+
+        Configuration fromTemplateConfig = null;
+        /*
+         * Fetch transactional/non-transactional templates
+         */
+        // Check if transactional
+        if (cMode.contains(IClusterServices.cacheMode.TRANSACTIONAL)) {
+            fromTemplateConfig = manager.getCacheConfiguration("transactional-type");
+        } else if (cMode.contains(IClusterServices.cacheMode.NON_TRANSACTIONAL)) {
+            fromTemplateConfig = manager.getDefaultCacheConfiguration();
+        }
+
+        // If none set the transactional property then just return null
+        if (fromTemplateConfig == null) {
+            return null;
+        }
+
+        ConfigurationBuilder builder = new ConfigurationBuilder();
+        builder.read(fromTemplateConfig);
+        /*
+         * Now evaluate async/sync
+         */
+        if (cMode.contains(IClusterServices.cacheMode.ASYNC)) {
+            builder.clustering()
+                    .cacheMode(fromTemplateConfig.clustering()
+                            .cacheMode()
+                            .toAsync());
+        } else if (cMode.contains(IClusterServices.cacheMode.SYNC)) {
+            builder.clustering()
+                    .cacheMode(fromTemplateConfig.clustering()
+                            .cacheMode()
+                            .toSync());
+        }
+
+        manager.defineConfiguration(realCacheName, builder.build());
+        c = manager.getCache(realCacheName);
+        return c;
     }
 
     @Override
index 736a2b2..ab0b63e 100644 (file)
@@ -89,8 +89,22 @@ public class ClusterManagerTest {
             Assert.assertTrue(true);
         }
 
-        // Create second cache properly this time, as non_transactional
+        // Create second cache NON_TRANSACTIONAL but with both ASYNC and SYNC,
+        // expect to complain
         cacheModeSet.remove(cacheMode.TRANSACTIONAL);
+        cacheModeSet.add(cacheMode.SYNC);
+        cacheModeSet.add(cacheMode.ASYNC);
+        try {
+            c2 = (CacheImpl<String, Integer>) cm.createCache("Container1", "Cache2", cacheModeSet);
+        } catch (CacheExistException cee) {
+            Assert.assertTrue(false);
+        } catch (CacheConfigException cce) {
+            Assert.assertTrue(true);
+        }
+
+        // Create second cache properly this time, as non_transactional and
+        // ASYNC
+        cacheModeSet.remove(cacheMode.SYNC);
         try {
             c2 = (CacheImpl<String, Integer>) cm.createCache("Container1",
                     "Cache2", cacheModeSet);
index 9c2afe4..c58410c 100644 (file)
@@ -1373,10 +1373,10 @@ public class ForwardingRulesManager implements
                     EnumSet.of(IClusterServices.cacheMode.TRANSACTIONAL));
 
             clusterContainerService.createCache(WORKSTATUSCACHE,
EnumSet.of(IClusterServices.cacheMode.NON_TRANSACTIONAL));
                   EnumSet.of(IClusterServices.cacheMode.NON_TRANSACTIONAL, IClusterServices.cacheMode.ASYNC));
 
             clusterContainerService.createCache(WORKORDERCACHE,
EnumSet.of(IClusterServices.cacheMode.NON_TRANSACTIONAL));
                   EnumSet.of(IClusterServices.cacheMode.NON_TRANSACTIONAL, IClusterServices.cacheMode.ASYNC));
 
         } catch (CacheConfigException cce) {
             log.error("CacheConfigException");