[infinispan-commits] Infinispan SVN: r304 - in trunk/core/src: test/java/org/infinispan and 2 other directories.

infinispan-commits at lists.jboss.org infinispan-commits at lists.jboss.org
Thu May 14 14:40:22 EDT 2009


Author: galder.zamarreno at jboss.com
Date: 2009-05-14 14:40:21 -0400 (Thu, 14 May 2009)
New Revision: 304

Added:
   trunk/core/src/test/java/org/infinispan/remoting/
   trunk/core/src/test/java/org/infinispan/remoting/TransportInvokeTest.java
Modified:
   trunk/core/src/main/java/org/infinispan/remoting/rpc/RpcManagerImpl.java
   trunk/core/src/test/java/org/infinispan/jmx/RpcManagerMBeanTest.java
Log:
[ISPN-77] (Avoid marshalling or calling RPCDIspatcher if sole member of cluster) Return empty list of responses directly if no one else in the cluster.

Modified: trunk/core/src/main/java/org/infinispan/remoting/rpc/RpcManagerImpl.java
===================================================================
--- trunk/core/src/main/java/org/infinispan/remoting/rpc/RpcManagerImpl.java	2009-05-14 17:30:23 UTC (rev 303)
+++ trunk/core/src/main/java/org/infinispan/remoting/rpc/RpcManagerImpl.java	2009-05-14 18:40:21 UTC (rev 304)
@@ -22,6 +22,7 @@
 import org.infinispan.util.logging.LogFactory;
 
 import java.text.NumberFormat;
+import java.util.Collections;
 import java.util.List;
 import java.util.Random;
 import java.util.concurrent.ExecutorService;
@@ -67,18 +68,25 @@
    }
 
    public List<Response> invokeRemotely(List<Address> recipients, ReplicableCommand rpcCommand, ResponseMode mode, long timeout, boolean usePriorityQueue, ResponseFilter responseFilter, boolean stateTransferEnabled) throws Exception {
-      try {
-         List<Response> result = t.invokeRemotely(recipients, rpcCommand, mode, timeout, usePriorityQueue, responseFilter, stateTransferEnabled);
-         if (isStatisticsEnabled()) replicationCount.incrementAndGet();
-         return result;
-      } catch (CacheException e) {
-         if (log.isTraceEnabled()) log.trace("replicaiton exception: ", e);
-         if (isStatisticsEnabled()) replicationFailures.incrementAndGet();
-         throw e;
-      } catch (Throwable th) {
-         log.error("unexpected error while replicating", th);
-         if (isStatisticsEnabled()) replicationFailures.incrementAndGet();
-         throw new CacheException(th);
+      List<Address> members = t.getMembers();
+      if (members.size() < 2) {
+         if (log.isDebugEnabled()) 
+            log.debug("We're the only member in the cluster; Don't invoke remotely.");
+         return Collections.emptyList();
+      } else {
+         try {
+            List<Response> result = t.invokeRemotely(recipients, rpcCommand, mode, timeout, usePriorityQueue, responseFilter, stateTransferEnabled);
+            if (isStatisticsEnabled()) replicationCount.incrementAndGet();
+            return result;
+         } catch (CacheException e) {
+            if (log.isTraceEnabled()) log.trace("replicaiton exception: ", e);
+            if (isStatisticsEnabled()) replicationFailures.incrementAndGet();
+            throw e;
+         } catch (Throwable th) {
+            log.error("unexpected error while replicating", th);
+            if (isStatisticsEnabled()) replicationFailures.incrementAndGet();
+            throw new CacheException(th);
+         }
       }
    }
 

Modified: trunk/core/src/test/java/org/infinispan/jmx/RpcManagerMBeanTest.java
===================================================================
--- trunk/core/src/test/java/org/infinispan/jmx/RpcManagerMBeanTest.java	2009-05-14 17:30:23 UTC (rev 303)
+++ trunk/core/src/test/java/org/infinispan/jmx/RpcManagerMBeanTest.java	2009-05-14 18:40:21 UTC (rev 304)
@@ -2,6 +2,7 @@
 
 import org.easymock.EasyMock;
 import static org.easymock.EasyMock.createMock;
+import static org.easymock.EasyMock.createNiceMock;
 import static org.easymock.EasyMock.replay;
 import org.infinispan.Cache;
 import org.infinispan.config.Configuration;
@@ -19,7 +20,10 @@
 
 import javax.management.MBeanServer;
 import javax.management.ObjectName;
+
+import java.util.ArrayList;
 import java.util.LinkedList;
+import java.util.List;
 
 /**
  * @author Mircea.Markus at jboss.com
@@ -111,8 +115,13 @@
       Transport originalTransport = rpcManager.getTransport();
 
       try {
+         Address mockAddress1 = createNiceMock(Address.class);
+         Address mockAddress2 = createNiceMock(Address.class);
+         List<Address> memberList = new ArrayList<Address>(2);
+         memberList.add(mockAddress1);
+         memberList.add(mockAddress2);
          Transport transport = createMock(Transport.class);
-         EasyMock.expect(transport.getMembers()).andReturn(new LinkedList<Address>()).anyTimes();
+         EasyMock.expect(transport.getMembers()).andReturn(memberList).anyTimes();
          replay(transport);
          rpcManager.setTransport(transport);
          cache1.put("a5", "b5");

Added: trunk/core/src/test/java/org/infinispan/remoting/TransportInvokeTest.java
===================================================================
--- trunk/core/src/test/java/org/infinispan/remoting/TransportInvokeTest.java	                        (rev 0)
+++ trunk/core/src/test/java/org/infinispan/remoting/TransportInvokeTest.java	2009-05-14 18:40:21 UTC (rev 304)
@@ -0,0 +1,91 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2009, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.infinispan.remoting;
+
+import static org.easymock.EasyMock.createNiceMock;
+import static org.easymock.EasyMock.createMock;
+import static org.easymock.EasyMock.expect;
+import static org.easymock.EasyMock.replay;
+import static org.easymock.EasyMock.verify;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.transaction.TransactionManager;
+
+import org.infinispan.Cache;
+import org.infinispan.config.Configuration;
+import org.infinispan.remoting.transport.Address;
+import org.infinispan.remoting.transport.Transport;
+import org.infinispan.test.MultipleCacheManagersTest;
+import org.infinispan.test.ReplListener;
+import org.infinispan.test.TestingUtil;
+import org.infinispan.transaction.lookup.DummyTransactionManagerLookup;
+import org.testng.annotations.Test;
+import org.infinispan.remoting.rpc.RpcManager;
+import org.infinispan.remoting.rpc.RpcManagerImpl;
+
+/**
+ * TransportInvokeTest.
+ * 
+ * @author Galder Zamarreño
+ * @since 4.0
+ */
+ at Test(groups = "functional", testName = "remoting.TransportInvokeTest")
+public class TransportInvokeTest extends MultipleCacheManagersTest {
+   final String key = "k", value = "v", value2 = "v2";
+   Cache cache1;
+   TransactionManager tm1;
+   ReplListener replListener1;
+
+   @Override
+   protected void createCacheManagers() throws Throwable {
+      Configuration c = getDefaultClusteredConfig(Configuration.CacheMode.REPL_SYNC);
+      c.setTransactionManagerLookupClass(DummyTransactionManagerLookup.class.getName());
+      createClusteredCaches(1, "replSync", c);
+      cache1 = cache(0, "replSync");
+      tm1 = TestingUtil.getTransactionManager(cache1);
+      replListener1 = replListener(cache1);
+   }
+   
+   public void testInvokeRemotelyWhenSingleMember() throws Exception {
+      Transport mockTransport = createMock(Transport.class);
+      RpcManagerImpl rpcManager = (RpcManagerImpl) TestingUtil.extractComponent(cache1, RpcManager.class);
+      Transport originalTransport = TestingUtil.extractComponent(cache1, Transport.class);
+      try {
+
+         Address mockAddress1 = createNiceMock(Address.class);
+         List<Address> memberList = new ArrayList<Address>(1);
+         memberList.add(mockAddress1);
+         expect(mockTransport.getMembers()).andReturn(memberList).anyTimes();
+         rpcManager.setTransport(mockTransport);
+         // Transport invoke remote should not be called.
+         replay(mockAddress1, mockTransport);
+         // now try a simple replication.  Since the RpcManager is a mock object it will not actually replicate anything.
+         cache1.put(key, value);
+         verify(mockTransport);
+
+      } finally {
+         if (rpcManager != null) rpcManager.setTransport(originalTransport);
+      }
+   }
+}




More information about the infinispan-commits mailing list