[infinispan-commits] Infinispan SVN: r2472 - in branches/4.2.x/core/src: main/java/org/infinispan/commands/control and 2 other directories.

infinispan-commits at lists.jboss.org infinispan-commits at lists.jboss.org
Sat Oct 2 14:50:56 EDT 2010


Author: manik.surtani at jboss.com
Date: 2010-10-02 14:50:56 -0400 (Sat, 02 Oct 2010)
New Revision: 2472

Added:
   branches/4.2.x/core/src/test/java/org/infinispan/lock/APITest.java
Modified:
   branches/4.2.x/core/src/main/java/org/infinispan/AdvancedCache.java
   branches/4.2.x/core/src/main/java/org/infinispan/CacheDelegate.java
   branches/4.2.x/core/src/main/java/org/infinispan/commands/control/LockControlCommand.java
   branches/4.2.x/core/src/main/java/org/infinispan/interceptors/InvocationContextInterceptor.java
   branches/4.2.x/core/src/main/java/org/infinispan/interceptors/LockingInterceptor.java
Log:
[ISPN-664] (AdvancedCache.lock() should return the status of acquiring a lock)

Modified: branches/4.2.x/core/src/main/java/org/infinispan/AdvancedCache.java
===================================================================
--- branches/4.2.x/core/src/main/java/org/infinispan/AdvancedCache.java	2010-10-02 18:45:29 UTC (rev 2471)
+++ branches/4.2.x/core/src/main/java/org/infinispan/AdvancedCache.java	2010-10-02 18:50:56 UTC (rev 2472)
@@ -108,8 +108,9 @@
     * A key can be locked eagerly in the context of a transaction only
     *
     * @param key the key to lock
+    * @return true if the lock acquisition attempt was successful; false otherwise.
     */
-   void lock(K key);
+   boolean lock(K key);
 
    /**
     * Locks collections of keys eagerly across cache nodes in a cluster.
@@ -118,8 +119,9 @@
     * 
     * 
     * @param keys collection of keys to lock
+    * @return true if the lock acquisition attempt was successful for <i>all</i> keys; false otherwise. 
     */
-   void lock(Collection<? extends K> keys);
+   boolean lock(Collection<? extends K> keys);
 
    RpcManager getRpcManager();
 

Modified: branches/4.2.x/core/src/main/java/org/infinispan/CacheDelegate.java
===================================================================
--- branches/4.2.x/core/src/main/java/org/infinispan/CacheDelegate.java	2010-10-02 18:45:29 UTC (rev 2471)
+++ branches/4.2.x/core/src/main/java/org/infinispan/CacheDelegate.java	2010-10-02 18:50:56 UTC (rev 2472)
@@ -285,16 +285,16 @@
       return ctx;
    }
 
-   public void lock(K key) {
+   public boolean lock(K key) {
       assertKeyNotNull(key);
-      lock(Collections.singletonList(key));
+      return lock(Collections.singletonList(key));
    }
 
-   public void lock(Collection<? extends K> keys) {
+   public boolean lock(Collection<? extends K> keys) {
       if (keys == null || keys.isEmpty())
          throw new IllegalArgumentException("Cannot lock empty list of keys");
       LockControlCommand command = commandsFactory.buildLockControlCommand(keys, false);
-      invoker.invoke(getInvocationContext(false), command);
+      return (Boolean) invoker.invoke(getInvocationContext(false), command);
    }
 
    @ManagedOperation(description = "Starts the cache.")

Modified: branches/4.2.x/core/src/main/java/org/infinispan/commands/control/LockControlCommand.java
===================================================================
--- branches/4.2.x/core/src/main/java/org/infinispan/commands/control/LockControlCommand.java	2010-10-02 18:45:29 UTC (rev 2471)
+++ branches/4.2.x/core/src/main/java/org/infinispan/commands/control/LockControlCommand.java	2010-10-02 18:50:56 UTC (rev 2472)
@@ -153,7 +153,7 @@
       if (transaction == null) {
          if (unlock) {
             if (log.isTraceEnabled()) {
-               log.trace("Unlock for in-existing transaction: " + globalTx + ". Not doing anything.");
+               log.trace("Unlock for non-existant transaction " + globalTx + ". Not doing anything.");
             }
             return null;
          }
@@ -209,6 +209,7 @@
       this.unlock = unlock;
    }
 
+   @Override
    public boolean equals(Object o) {
       if (this == o)
          return true;
@@ -221,6 +222,7 @@
       return keys.equals(that.keys) && Util.safeEquals(singleKey, that.singleKey) && (unlock == that.unlock);
    }
 
+   @Override
    public int hashCode() {
       int result = super.hashCode();
       result = 31 * result + (keys != null ? keys.hashCode() : 0);

Modified: branches/4.2.x/core/src/main/java/org/infinispan/interceptors/InvocationContextInterceptor.java
===================================================================
--- branches/4.2.x/core/src/main/java/org/infinispan/interceptors/InvocationContextInterceptor.java	2010-10-02 18:45:29 UTC (rev 2471)
+++ branches/4.2.x/core/src/main/java/org/infinispan/interceptors/InvocationContextInterceptor.java	2010-10-02 18:50:56 UTC (rev 2472)
@@ -24,8 +24,10 @@
 
 import org.infinispan.CacheException;
 import org.infinispan.commands.VisitableCommand;
+import org.infinispan.commands.control.LockControlCommand;
 import org.infinispan.context.Flag;
 import org.infinispan.context.InvocationContext;
+import org.infinispan.context.impl.TxInvocationContext;
 import org.infinispan.factories.ComponentRegistry;
 import org.infinispan.factories.annotations.Inject;
 import org.infinispan.interceptors.base.CommandInterceptor;
@@ -56,6 +58,12 @@
       return handleAll(ctx, command);
    }
 
+   @Override
+   public Object visitLockControlCommand(TxInvocationContext ctx, LockControlCommand lcc) throws Throwable {
+      Object retval = handleAll(ctx, lcc);
+      return retval == null ? false : retval;
+   }
+
    private Object handleAll(InvocationContext ctx, VisitableCommand command) throws Throwable {
       boolean suppressExceptions = false;
 

Modified: branches/4.2.x/core/src/main/java/org/infinispan/interceptors/LockingInterceptor.java
===================================================================
--- branches/4.2.x/core/src/main/java/org/infinispan/interceptors/LockingInterceptor.java	2010-10-02 18:45:29 UTC (rev 2471)
+++ branches/4.2.x/core/src/main/java/org/infinispan/interceptors/LockingInterceptor.java	2010-10-02 18:50:56 UTC (rev 2472)
@@ -143,7 +143,7 @@
          if (c.isUnlock()) {
             lockManager.releaseLocks(ctx);
             if (log.isTraceEnabled()) log.trace("Lock released for: " + ctx.getLockOwner());
-            return null;
+            return false;
          }
 
          for (Object key : c.getKeys()) {
@@ -158,7 +158,9 @@
             Object result = invokeNextInterceptor(ctx, c);
             try {
                lockKeysForRemoteTx(ctx, c);
+               result = true;
             } catch (Throwable e) {
+               result = false;
                //if anything happen during locking then unlock remote
                c.setUnlock(true);
                invokeNextInterceptor(ctx, c);
@@ -167,13 +169,16 @@
             return result;
          } else {
             lockKeysForRemoteTx(ctx, c);
-            if (shouldInvokeOnCluster || c.isExplicit())
-               return invokeNextInterceptor(ctx, c);
-            else
-               return null;
+            if (shouldInvokeOnCluster || c.isExplicit()) {
+               invokeNextInterceptor(ctx, c);
+               return true;
+            } else {
+               return true;
+            }
          }
       } catch (Throwable te) {
-            return cleanLocksAndRethrow(ctx, te);
+         cleanLocksAndRethrow(ctx, te);
+         return false;
       } finally {
          if (ctx.isInTxScope()) {
             doAfterCall(ctx);

Added: branches/4.2.x/core/src/test/java/org/infinispan/lock/APITest.java
===================================================================
--- branches/4.2.x/core/src/test/java/org/infinispan/lock/APITest.java	                        (rev 0)
+++ branches/4.2.x/core/src/test/java/org/infinispan/lock/APITest.java	2010-10-02 18:50:56 UTC (rev 2472)
@@ -0,0 +1,120 @@
+package org.infinispan.lock;
+
+import org.infinispan.Cache;
+import org.infinispan.config.Configuration;
+import org.infinispan.context.Flag;
+import org.infinispan.manager.CacheManager;
+import org.infinispan.manager.EmbeddedCacheManager;
+import org.infinispan.test.MultipleCacheManagersTest;
+import org.infinispan.test.fwk.CleanupAfterMethod;
+import org.infinispan.test.fwk.TestCacheManagerFactory;
+import org.infinispan.util.concurrent.TimeoutException;
+import org.testng.annotations.Test;
+
+import javax.transaction.NotSupportedException;
+import javax.transaction.SystemException;
+import javax.transaction.Transaction;
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.Set;
+
+import static org.infinispan.context.Flag.FAIL_SILENTLY;
+
+
+ at Test(testName = "lock.APITest", groups = "functional")
+ at CleanupAfterMethod
+public class APITest extends MultipleCacheManagersTest {
+   EmbeddedCacheManager cm1, cm2;
+
+   @Override
+   protected void createCacheManagers() throws Throwable {
+      Configuration cfg = getDefaultClusteredConfig(Configuration.CacheMode.REPL_SYNC, true);
+      cfg.setLockAcquisitionTimeout(100);
+      cm1 = TestCacheManagerFactory.createClusteredCacheManager(cfg);
+      cm2 = TestCacheManagerFactory.createClusteredCacheManager(cfg);
+      registerCacheManager(cm1, cm2);
+      cm1.getCache();
+      cm2.getCache();
+   }
+
+   public void testLockSuccess() throws SystemException, NotSupportedException {
+      Cache<String, String> cache1 = cache(0);
+
+      cache1.put("k", "v");
+      tm(0).begin();
+      assert cache1.getAdvancedCache().lock("k");
+      tm(0).rollback();
+   }
+
+   @Test (expectedExceptions = TimeoutException.class)
+   public void testLockFailure() throws SystemException, NotSupportedException {
+      Cache<String, String> cache1 = cache(0), cache2 = cache(1);
+
+      cache1.put("k", "v");
+      tm(1).begin();
+      cache2.put("k", "v2");
+      Transaction t = tm(1).suspend();
+
+      tm(0).begin();
+      cache1.getAdvancedCache().lock("k");
+      tm(0).rollback();
+   }
+
+   public void testSilentLockFailure() throws SystemException, NotSupportedException {
+      Cache<String, String> cache1 = cache(0), cache2 = cache(1);
+
+      cache1.put("k", "v");
+      tm(1).begin();
+      cache2.put("k", "v2");
+      Transaction t = tm(1).suspend();
+
+      tm(0).begin();
+      assert !cache1.getAdvancedCache().withFlags(FAIL_SILENTLY).lock("k");
+      tm(0).rollback();
+   }
+
+   public void testMultiLockSuccess() throws SystemException, NotSupportedException {
+      Cache<String, String> cache1 = cache(0);
+
+      cache1.put("k1", "v");
+      cache1.put("k2", "v");
+      cache1.put("k3", "v");
+
+      tm(0).begin();
+      assert cache1.getAdvancedCache().lock(Arrays.asList("k1", "k2", "k3"));
+      tm(0).rollback();
+   }
+
+   @Test (expectedExceptions = TimeoutException.class)   
+   public void testMultiLockFailure() throws SystemException, NotSupportedException {
+      Cache<String, String> cache1 = cache(0), cache2 = cache(1);
+
+      cache1.put("k1", "v");
+      cache1.put("k2", "v");
+      cache1.put("k3", "v");
+
+      tm(1).begin();
+      cache2.put("k3", "v2");
+      Transaction t = tm(1).suspend();
+
+      tm(0).begin();
+      cache1.getAdvancedCache().lock(Arrays.asList("k1", "k2", "k3"));
+      tm(0).rollback();
+   }
+
+   public void testSilentMultiLockFailure() throws SystemException, NotSupportedException {
+      Cache<String, String> cache1 = cache(0), cache2 = cache(1);
+
+      cache1.put("k1", "v");
+      cache1.put("k2", "v");
+      cache1.put("k3", "v");
+
+      tm(1).begin();
+      cache2.put("k3", "v2");
+      Transaction t = tm(1).suspend();
+
+      tm(0).begin();
+      assert !cache1.getAdvancedCache().withFlags(FAIL_SILENTLY).lock(Arrays.asList("k1", "k2", "k3"));
+      tm(0).rollback();
+   }
+}



More information about the infinispan-commits mailing list