ignite-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sevdoki...@apache.org
Subject [03/22] incubator-ignite git commit: IGNITE-59 Refactoring CacheLock.
Date Wed, 28 Jan 2015 10:18:51 GMT
IGNITE-59 Refactoring CacheLock.


Project: http://git-wip-us.apache.org/repos/asf/incubator-ignite/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-ignite/commit/dabe040c
Tree: http://git-wip-us.apache.org/repos/asf/incubator-ignite/tree/dabe040c
Diff: http://git-wip-us.apache.org/repos/asf/incubator-ignite/diff/dabe040c

Branch: refs/heads/sprint-1
Commit: dabe040c96279bbba749756d64cdb6b9a660decc
Parents: 6accb8c
Author: Sergey Evdokimov <sergey.evdokimov@jetbrains.com>
Authored: Sun Jan 25 16:37:25 2015 +0300
Committer: Sergey Evdokimov <sergey.evdokimov@jetbrains.com>
Committed: Sun Jan 25 16:37:25 2015 +0300

----------------------------------------------------------------------
 .../processors/cache/CacheLockImpl.java         | 90 +++++++++++++++++---
 .../processors/cache/IgniteCacheProxy.java      |  4 +-
 .../cache/GridCacheAbstractFullApiSelfTest.java | 18 ++--
 .../cache/GridCacheBasicApiAbstractTest.java    | 64 ++++++++------
 .../GridCacheFinishPartitionsSelfTest.java      | 25 ++++--
 .../GridCacheGroupLockAbstractSelfTest.java     | 13 ++-
 .../cache/GridCacheNestedTxAbstractTest.java    | 13 ++-
 .../GridCacheOffHeapTieredAbstractSelfTest.java |  7 +-
 .../distributed/GridCacheLockAbstractTest.java  | 36 +++++---
 .../GridCacheMultiNodeLockAbstractTest.java     | 81 +++++++++++-------
 .../dht/GridCacheColocatedDebugTest.java        | 28 +++---
 .../near/GridCacheNearMultiNodeSelfTest.java    | 11 ++-
 .../near/GridCacheNearOneNodeSelfTest.java      | 18 ++--
 .../near/GridCacheNearReadersSelfTest.java      | 13 ++-
 .../GridCacheEvictionLockUnlockSelfTest.java    |  7 +-
 .../cache/local/GridCacheLocalLockSelfTest.java | 29 ++++---
 .../GridCacheLocalMultithreadedSelfTest.java    | 33 ++++---
 .../tcp/GridCacheDhtLockBackupSelfTest.java     | 13 ++-
 18 files changed, 338 insertions(+), 165 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/dabe040c/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/CacheLockImpl.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/CacheLockImpl.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/CacheLockImpl.java
index 9bd4ea9..c1ed7eb 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/CacheLockImpl.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/CacheLockImpl.java
@@ -37,6 +37,12 @@ class CacheLockImpl<K> implements Lock {
     /** */
     private final Collection<? extends K> keys;
 
+    /** */
+    private int cntr;
+
+    /** */
+    private volatile Thread lockedThread;
+
     /**
      * @param delegate Delegate.
      * @param keys Keys.
@@ -50,12 +56,25 @@ class CacheLockImpl<K> implements Lock {
     @Override public void lock() {
         try {
             delegate.lockAll(keys, 0);
+
+            incrementLockCounter();
         }
         catch (IgniteCheckedException e) {
             throw new CacheException(e.getMessage(), e);
         }
     }
 
+    /**
+     *
+     */
+    private void incrementLockCounter() {
+        assert (lockedThread == null && cntr == 0) || (lockedThread == Thread.currentThread() && cntr > 0);
+
+        lockedThread = Thread.currentThread();
+
+        cntr++;
+    }
+
     /** {@inheritDoc} */
     @Override public void lockInterruptibly() throws InterruptedException {
         tryLock(Long.MAX_VALUE, TimeUnit.MILLISECONDS);
@@ -64,7 +83,12 @@ class CacheLockImpl<K> implements Lock {
     /** {@inheritDoc} */
     @Override public boolean tryLock() {
         try {
-            return delegate.lockAll(keys, -1);
+            boolean res = delegate.lockAll(keys, -1);
+
+            if (res)
+                incrementLockCounter();
+
+            return res;
         }
         catch (IgniteCheckedException e) {
             throw new CacheException(e.getMessage(), e);
@@ -76,27 +100,31 @@ class CacheLockImpl<K> implements Lock {
         if (Thread.interrupted())
             throw new InterruptedException();
 
-        try {
-            if (time <= 0)
-                return delegate.lockAll(keys, -1);
+        if (time <= 0)
+            return tryLock();
 
-            IgniteFuture<Boolean> fut = null;
+        try {
+            IgniteFuture<Boolean> fut = delegate.lockAllAsync(keys, unit.toMillis(time));
 
             try {
-                fut = delegate.lockAllAsync(keys, unit.toMillis(time));
+                boolean res = fut.get();
 
-                return fut.get();
+                if (res)
+                    incrementLockCounter();
+
+                return res;
             }
             catch (IgniteInterruptedException e) {
-                if (fut != null) {
-                    if (!fut.cancel()) {
-                        if (fut.isDone()) {
-                            Boolean res = fut.get();
+                if (!fut.cancel()) {
+                    if (fut.isDone()) {
+                        Boolean res = fut.get();
+
+                        Thread.currentThread().interrupt();
 
-                            Thread.currentThread().interrupt();
+                        if (res)
+                            incrementLockCounter();
 
-                            return res;
-                        }
+                        return res;
                     }
                 }
 
@@ -111,9 +139,43 @@ class CacheLockImpl<K> implements Lock {
         }
     }
 
+    /**
+     *
+     */
+    private boolean isKeysLocked() {
+        for (K key : keys) {
+            if (delegate.isLocked(key))
+                return true;
+        }
+
+        return false;
+    }
+
     /** {@inheritDoc} */
     @Override public void unlock() {
         try {
+            Thread lockedThread = this.lockedThread;
+
+            if (lockedThread != Thread.currentThread()) {
+                if (lockedThread == null) {
+                    if (isKeysLocked()) {
+                        throw new IllegalStateException("Failed to unlock keys, looks like lock has been obtain on " +
+                            "another instance of Lock, that was returned by IgniteCache.lock(key). You have to call " +
+                            "lock() and unlock() methods on the same instance of Lock [keys=" + keys + ']');
+                    }
+                } else {
+                    throw new IllegalStateException("Failed to unlock cache keys, keys are locked by another thread " +
+                        "any threads [keys=" + keys + ", lockOwnerThread=" + lockedThread.getName() + ']');
+                }
+            }
+
+            assert cntr > 0;
+
+            cntr--;
+
+            if (cntr == 0)
+                this.lockedThread = null;
+
             delegate.unlockAll(keys);
         }
         catch (IgniteCheckedException e) {

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/dabe040c/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/IgniteCacheProxy.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/IgniteCacheProxy.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/IgniteCacheProxy.java
index 993a615..4df983a 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/IgniteCacheProxy.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/IgniteCacheProxy.java
@@ -179,12 +179,12 @@ public class IgniteCacheProxy<K, V> extends IgniteAsyncSupportAdapter implements
 
     /** {@inheritDoc} */
     @Override public Lock lock(K key) throws CacheException {
-        return lockAll(Collections.<K>singleton(key));
+        return lockAll(Collections.singleton(key));
     }
 
     /** {@inheritDoc} */
     @Override public Lock lockAll(final Collection<? extends K> keys) {
-        return new CacheLockImpl<K>(delegate, keys);
+        return new CacheLockImpl<>(delegate, keys);
     }
 
     /** {@inheritDoc} */

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/dabe040c/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheAbstractFullApiSelfTest.java
----------------------------------------------------------------------
diff --git a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheAbstractFullApiSelfTest.java b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheAbstractFullApiSelfTest.java
index fcc52ff..715baed 100644
--- a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheAbstractFullApiSelfTest.java
+++ b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheAbstractFullApiSelfTest.java
@@ -3323,13 +3323,15 @@ public abstract class GridCacheAbstractFullApiSelfTest extends GridCacheAbstract
 
             assert !cache.isLocalLocked(key, false);
 
-            cache.lock(key).lock();
+            Lock lock = cache.lock(key);
+
+            lock.lock();
 
             lockCnt.await();
 
             assert cache.isLocalLocked(key, false);
 
-            cache.lock(key).unlock();
+            lock.unlock();
 
             unlockCnt.await();
 
@@ -3707,12 +3709,14 @@ public abstract class GridCacheAbstractFullApiSelfTest extends GridCacheAbstract
             assert !cache.isLocalLocked("key1", false);
             assert !cache.isLocalLocked("key2", false);
 
-            cache.lockAll(ImmutableSet.of("key1", "key2")).lock();
+            Lock lock1_2 = cache.lockAll(ImmutableSet.of("key1", "key2"));
+
+            lock1_2.lock();
 
             assert cache.isLocalLocked("key1", false);
             assert cache.isLocalLocked("key2", false);
 
-            cache.lockAll(ImmutableSet.of("key1", "key2")).unlock();
+            lock1_2.unlock();
 
             for (int i = 0; i < 100; i++)
                 if (cache.isLocalLocked("key1", false) || cache.isLocalLocked("key2", false))
@@ -3723,14 +3727,12 @@ public abstract class GridCacheAbstractFullApiSelfTest extends GridCacheAbstract
             assert !cache.isLocalLocked("key1", false);
             assert !cache.isLocalLocked("key2", false);
 
-            Lock lock = cache.lockAll(ImmutableSet.of("key1", "key2"));
-
-            lock.lock();
+            lock1_2.lock();
 
             assert cache.isLocalLocked("key1", false);
             assert cache.isLocalLocked("key2", false);
 
-            lock.unlock();
+            lock1_2.unlock();
 
             for (int i = 0; i < 100; i++)
                 if (cache.isLocalLocked("key1", false) || cache.isLocalLocked("key2", false))

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/dabe040c/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheBasicApiAbstractTest.java
----------------------------------------------------------------------
diff --git a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheBasicApiAbstractTest.java b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheBasicApiAbstractTest.java
index 3e07da5..1309c43 100644
--- a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheBasicApiAbstractTest.java
+++ b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheBasicApiAbstractTest.java
@@ -17,6 +17,7 @@
 
 package org.apache.ignite.internal.processors.cache;
 
+import com.beust.jcommander.internal.*;
 import org.apache.ignite.*;
 import org.apache.ignite.cache.*;
 import org.apache.ignite.configuration.*;
@@ -29,6 +30,7 @@ import org.apache.ignite.internal.util.typedef.internal.*;
 import org.apache.ignite.testframework.*;
 import org.apache.ignite.testframework.junits.common.*;
 import org.jetbrains.annotations.*;
+import org.jetbrains.annotations.Nullable;
 
 import javax.cache.expiry.*;
 import java.util.*;
@@ -168,7 +170,9 @@ public abstract class GridCacheBasicApiAbstractTest extends GridCommonAbstractTe
     public void testInterruptLock() throws InterruptedException {
         final IgniteCache<Integer, String> cache = ignite.jcache(null);
 
-        cache.lock(1).lock();
+        final Lock lock = cache.lock(1);
+
+        lock.lock();
 
         final AtomicBoolean isOk = new AtomicBoolean(false);
 
@@ -176,13 +180,13 @@ public abstract class GridCacheBasicApiAbstractTest extends GridCommonAbstractTe
             @Override public void run() {
                 assertFalse(cache.isLocalLocked(1, true));
 
-                cache.lock(1).lock();
+                lock.lock();
 
                 try {
                     assertTrue(cache.isLocalLocked(1, true));
                 }
                 finally {
-                    cache.lock(1).unlock();
+                    lock.unlock();
                 }
 
                 assertTrue(Thread.currentThread().isInterrupted());
@@ -197,7 +201,7 @@ public abstract class GridCacheBasicApiAbstractTest extends GridCommonAbstractTe
 
         t.interrupt();
 
-        cache.lock(1).unlock();
+        lock.unlock();
 
         t.join();
 
@@ -210,7 +214,9 @@ public abstract class GridCacheBasicApiAbstractTest extends GridCommonAbstractTe
     public void testInterruptLockWithTimeout() throws InterruptedException {
         final IgniteCache<Integer, String> cache = ignite.jcache(null);
 
-        cache.lock(2).lock();
+        Lock lock2 = cache.lock(2);
+
+        lock2.lock();
 
         final AtomicBoolean isOk = new AtomicBoolean(false);
 
@@ -233,7 +239,7 @@ public abstract class GridCacheBasicApiAbstractTest extends GridCommonAbstractTe
 
         t.join();
 
-        cache.lock(2).unlock();
+        lock2.unlock();
 
         assertFalse(cache.isLocalLocked(1, false));
         assertFalse(cache.isLocalLocked(2, false));
@@ -249,7 +255,9 @@ public abstract class GridCacheBasicApiAbstractTest extends GridCommonAbstractTe
 
         Integer key = 1;
 
-        cache.lock(key).lock();
+        Lock lock = cache.lock(key);
+
+        lock.lock();
 
         try {
             assert cache.get(key) == null;
@@ -259,7 +267,7 @@ public abstract class GridCacheBasicApiAbstractTest extends GridCommonAbstractTe
             assert cache.isLocalLocked(key, false);
             assert cache.isLocalLocked(key, true);
 
-            cache.lock(key).lock();
+            lock.lock();
 
             assert cache.isLocalLocked(key, false);
             assert cache.isLocalLocked(key, true);
@@ -268,14 +276,14 @@ public abstract class GridCacheBasicApiAbstractTest extends GridCommonAbstractTe
                 assert "1".equals(cache.getAndRemove(key));
             }
             finally {
-                cache.lock(key).unlock();
+                lock.unlock();
             }
 
             assert cache.isLocalLocked(key, false);
             assert cache.isLocalLocked(key, true);
         }
         finally {
-            cache.lock(key).unlock();
+            lock.unlock();
 
             assert !cache.isLocalLocked(key, false);
             assert !cache.isLocalLocked(key, true);
@@ -292,12 +300,14 @@ public abstract class GridCacheBasicApiAbstractTest extends GridCommonAbstractTe
         final CountDownLatch l2 = new CountDownLatch(1);
         final CountDownLatch l3 = new CountDownLatch(1);
 
+        final Lock lock = cache.lock(1);
+
         GridTestThread t1 = new GridTestThread(new Callable<Object>() {
             /** {@inheritDoc} */
             @Nullable @Override public Object call() throws Exception {
                 info("Before lock for.key 1");
 
-                cache.lock(1).lock();
+                lock.lock();
 
                 info("After lock for key 1");
 
@@ -310,22 +320,24 @@ public abstract class GridCacheBasicApiAbstractTest extends GridCommonAbstractTe
                     info("Let thread2 proceed.");
 
                     // Reentry.
-                    assert cache.lock(1).tryLock();
+                    assert lock.tryLock();
 
                     // Nested lock.
-                    assert cache.lock(2).tryLock();
+                    Lock lock2 = cache.lock(2);
+
+                    assert lock2.tryLock();
 
                     l2.await();
 
-                    cache.lock(1).unlock();
+                    lock.unlock();
 
                     // Unlock in reverse order.
-                    cache.lock(2).unlock();
+                    lock2.unlock();
 
                     info("Waited for latch 2");
                 }
                 finally {
-                    cache.lock(1).unlock();
+                    lock.unlock();
 
                     info("Unlocked entry for key 1.");
                 }
@@ -345,7 +357,7 @@ public abstract class GridCacheBasicApiAbstractTest extends GridCommonAbstractTe
 
                 info("Latch1 released.");
 
-                assert !cache.lock(1).tryLock();
+                assert !lock.tryLock();
 
                 if (!cache.isLocalLocked(1, false))
                     throw new IllegalArgumentException();
@@ -360,7 +372,7 @@ public abstract class GridCacheBasicApiAbstractTest extends GridCommonAbstractTe
 
                 l3.await();
 
-                assert cache.lock(1).tryLock();
+                assert lock.tryLock();
 
                 try {
                     info("Locked cache for key 1");
@@ -371,7 +383,7 @@ public abstract class GridCacheBasicApiAbstractTest extends GridCommonAbstractTe
                     info("Checked that cache is locked for key 1");
                 }
                 finally {
-                    cache.lock(1).unlock();
+                    lock.unlock();
 
                     info("Unlocked cache for key 1");
                 }
@@ -467,7 +479,9 @@ public abstract class GridCacheBasicApiAbstractTest extends GridCommonAbstractTe
 
         assert !cache.containsKey(key);
 
-        cache.lock(key).lock();
+        Lock lock = cache.lock(key);
+
+        lock.lock();
 
         CountDownLatch latch = new CountDownLatch(1);
 
@@ -518,7 +532,7 @@ public abstract class GridCacheBasicApiAbstractTest extends GridCommonAbstractTe
             assert cache.isLocalLocked(key, false);
         }
         finally {
-            cache.lock(key).unlock();
+            lock.unlock();
 
             ignite.events().stopLocalListen(lsnr, EVTS_CACHE);
         }
@@ -533,11 +547,11 @@ public abstract class GridCacheBasicApiAbstractTest extends GridCommonAbstractTe
     public void testMultiLocks() throws Exception {
         IgniteCache<Integer, String> cache = ignite.jcache(null);
 
-        Set<Integer> keys = new HashSet<>();
+        Collection<Integer> keys = Lists.newArrayList(1, 2, 3);
 
-        Collections.addAll(keys, 1, 2, 3);
+        Lock lock = cache.lockAll(keys);
 
-        cache.lockAll(keys).lock();
+        lock.lock();
 
         assert cache.isLocalLocked(1, false);
         assert cache.isLocalLocked(2, false);
@@ -547,7 +561,7 @@ public abstract class GridCacheBasicApiAbstractTest extends GridCommonAbstractTe
         assert cache.isLocalLocked(2, true);
         assert cache.isLocalLocked(3, true);
 
-        cache.lockAll(keys).unlock();
+        lock.unlock();
 
         assert !cache.isLocalLocked(1, false);
         assert !cache.isLocalLocked(2, false);

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/dabe040c/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheFinishPartitionsSelfTest.java
----------------------------------------------------------------------
diff --git a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheFinishPartitionsSelfTest.java b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheFinishPartitionsSelfTest.java
index 87be338..5ce38f9 100644
--- a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheFinishPartitionsSelfTest.java
+++ b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheFinishPartitionsSelfTest.java
@@ -29,6 +29,7 @@ import org.apache.ignite.testframework.*;
 import java.util.*;
 import java.util.concurrent.*;
 import java.util.concurrent.atomic.*;
+import java.util.concurrent.locks.*;
 
 import static org.apache.ignite.cache.CacheAtomicityMode.*;
 import static org.apache.ignite.cache.CacheMode.*;
@@ -224,7 +225,9 @@ public class GridCacheFinishPartitionsSelfTest extends GridCacheAbstractSelfTest
 
         IgniteCache<Integer, String> cache = grid.jcache(null);
 
-        cache.lock(key).lock();
+        Lock lock = cache.lock(key);
+
+        lock.lock();
 
         long start = System.currentTimeMillis();
 
@@ -244,17 +247,21 @@ public class GridCacheFinishPartitionsSelfTest extends GridCacheAbstractSelfTest
             }
         });
 
-        cache.lock(key + 1).lock();
+        Lock lock1 = cache.lock(key + 1);
+
+        lock1.lock();
 
-        cache.lock(key).unlock();
+        lock.unlock();
 
-        cache.lock(key + 2).lock();
+        Lock lock2 = cache.lock(key + 2);
 
-        cache.lock(key + 1).unlock();
+        lock2.lock();
+
+        lock1.unlock();
 
         assert !fut.isDone() : "Failed waiting for locks";
 
-        cache.lock(key + 2).unlock();
+        lock2.unlock();
 
         latch.await();
     }
@@ -276,7 +283,9 @@ public class GridCacheFinishPartitionsSelfTest extends GridCacheAbstractSelfTest
 
         IgniteCache<String, String> cache = grid.jcache(null);
 
-        cache.lock(key).lock();
+        Lock lock = cache.lock(key);
+
+        lock.lock();
 
         long start;
         try {
@@ -302,7 +311,7 @@ public class GridCacheFinishPartitionsSelfTest extends GridCacheAbstractSelfTest
                 + fut.isDone() + ']';
         }
         finally {
-            cache.lock(key).unlock();
+            lock.unlock();
         }
 
         latch.await();

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/dabe040c/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheGroupLockAbstractSelfTest.java
----------------------------------------------------------------------
diff --git a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheGroupLockAbstractSelfTest.java b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheGroupLockAbstractSelfTest.java
index 49a3da0..f1637df 100644
--- a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheGroupLockAbstractSelfTest.java
+++ b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheGroupLockAbstractSelfTest.java
@@ -42,6 +42,7 @@ import javax.cache.configuration.*;
 import java.util.*;
 import java.util.concurrent.*;
 import java.util.concurrent.atomic.*;
+import java.util.concurrent.locks.*;
 
 import static org.apache.ignite.events.IgniteEventType.*;
 import static org.apache.ignite.cache.CacheAtomicityMode.*;
@@ -395,14 +396,16 @@ public abstract class GridCacheGroupLockAbstractSelfTest extends GridCommonAbstr
         IgniteFuture<?> fut = multithreadedAsync(new Runnable() {
             @Override public void run() {
                 try {
-                    cache.lock(key1).lock();
+                    Lock lock = cache.lock(key1);
+
+                    lock.lock();
 
                     try {
                         lockLatch.countDown();
                         unlockLatch.await();
                     }
                     finally {
-                        cache.lock(key1).unlock();
+                        lock.unlock();
                     }
                 }
                 catch (CacheException e) {
@@ -478,7 +481,9 @@ public abstract class GridCacheGroupLockAbstractSelfTest extends GridCommonAbstr
                 assertEquals("For index: " + i, "val1", gCache.peek(key1));
         }
 
-        cache.lock(key1).lock();
+        Lock lock = cache.lock(key1);
+
+        lock.lock();
 
         try {
             try (IgniteTx tx = grid.transactions().txStartAffinity(null, affinityKey, concurrency, READ_COMMITTED, 0, 1)) {
@@ -497,7 +502,7 @@ public abstract class GridCacheGroupLockAbstractSelfTest extends GridCommonAbstr
             }
         }
         finally {
-            cache.lock(key1).unlock();
+            lock.unlock();
         }
     }
 

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/dabe040c/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheNestedTxAbstractTest.java
----------------------------------------------------------------------
diff --git a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheNestedTxAbstractTest.java b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheNestedTxAbstractTest.java
index f92005c..7121ee1 100644
--- a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheNestedTxAbstractTest.java
+++ b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheNestedTxAbstractTest.java
@@ -30,6 +30,7 @@ import org.apache.ignite.testframework.junits.common.*;
 import java.util.*;
 import java.util.concurrent.*;
 import java.util.concurrent.atomic.*;
+import java.util.concurrent.locks.*;
 
 import static org.apache.ignite.transactions.IgniteTxConcurrency.*;
 import static org.apache.ignite.transactions.IgniteTxIsolation.*;
@@ -171,8 +172,10 @@ public class GridCacheNestedTxAbstractTest extends GridCommonAbstractTest {
             threads.add(new Thread(new Runnable() {
                 @Override public void run() {
 
+                    Lock lock = c.lock(CNTR_KEY);
+
                     try {
-                        c.lock(CNTR_KEY).lock();
+                        lock.lock();
 
                         int cntr = c.get(CNTR_KEY);
 
@@ -184,7 +187,7 @@ public class GridCacheNestedTxAbstractTest extends GridCommonAbstractTest {
                         error("Failed lock thread", e);
                     }
                     finally {
-                        c.lock(CNTR_KEY).unlock();
+                        lock.unlock();
                     }
                 }
             }));
@@ -224,8 +227,10 @@ public class GridCacheNestedTxAbstractTest extends GridCommonAbstractTest {
             threads.add(new Thread(new Runnable() {
                 @Override public void run() {
 
+                    Lock lock = c.lock(CNTR_KEY);
+
                     try {
-                        c.lock(CNTR_KEY).lock();
+                        lock.lock();
 
                         int cntr = c.get(CNTR_KEY);
 
@@ -257,7 +262,7 @@ public class GridCacheNestedTxAbstractTest extends GridCommonAbstractTest {
                         error("Failed lock thread", e);
                     }
                     finally {
-                        c.lock(CNTR_KEY).unlock();
+                        lock.unlock();
                     }
                 }
             }));

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/dabe040c/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheOffHeapTieredAbstractSelfTest.java
----------------------------------------------------------------------
diff --git a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheOffHeapTieredAbstractSelfTest.java b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheOffHeapTieredAbstractSelfTest.java
index e89d26a..ab6b5fb 100644
--- a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheOffHeapTieredAbstractSelfTest.java
+++ b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheOffHeapTieredAbstractSelfTest.java
@@ -28,6 +28,7 @@ import org.junit.*;
 
 import javax.cache.processor.*;
 import java.util.*;
+import java.util.concurrent.locks.*;
 
 import static org.apache.ignite.cache.CacheAtomicWriteOrderMode.*;
 import static org.apache.ignite.cache.CacheAtomicityMode.*;
@@ -531,11 +532,13 @@ public abstract class GridCacheOffHeapTieredAbstractSelfTest extends GridCacheAb
 
         assertNull(c.localPeek(key));
 
-        c.lock(key).lock();
+        Lock lock = c.lock(key);
+
+        lock.lock();
 
         assertTrue(c.isLocalLocked(key, false));
 
-        c.lock(key).unlock();
+        lock.unlock();
 
         assertFalse(c.isLocalLocked(key, false));
 

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/dabe040c/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/distributed/GridCacheLockAbstractTest.java
----------------------------------------------------------------------
diff --git a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/distributed/GridCacheLockAbstractTest.java b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/distributed/GridCacheLockAbstractTest.java
index 0863092..c6686cf 100644
--- a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/distributed/GridCacheLockAbstractTest.java
+++ b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/distributed/GridCacheLockAbstractTest.java
@@ -30,6 +30,7 @@ import org.jetbrains.annotations.*;
 
 import java.util.*;
 import java.util.concurrent.*;
+import java.util.concurrent.locks.*;
 
 import static org.apache.ignite.cache.CacheAtomicityMode.*;
 import static org.apache.ignite.cache.CacheDistributionMode.*;
@@ -176,7 +177,9 @@ public abstract class GridCacheLockAbstractTest extends GridCommonAbstractTest {
 
         info("Before lock for key: " + k);
 
-        cache1.lock(k).lock();
+        Lock lock = cache1.lock(k);
+
+        lock.lock();
 
         info("After lock for key: " + k);
 
@@ -190,7 +193,7 @@ public abstract class GridCacheLockAbstractTest extends GridCommonAbstractTest {
             info("Put " + k + '=' + k + " key pair into cache.");
         }
         finally {
-            cache1.lock(k).unlock();
+            lock.unlock();
 
             info("Unlocked key: " + k);
         }
@@ -209,11 +212,13 @@ public abstract class GridCacheLockAbstractTest extends GridCommonAbstractTest {
         final CountDownLatch l1 = new CountDownLatch(1);
         final CountDownLatch l2 = new CountDownLatch(1);
 
+        final Lock lock = cache1.lock(kv);
+
         GridTestThread t1 = new GridTestThread(new Callable<Object>() {
             @Nullable @Override public Object call() throws Exception {
                 info("Before lock for key: " + kv);
 
-                cache1.lock(kv).lock();
+                lock.lock();
 
                 info("After lock for key: " + kv);
 
@@ -232,7 +237,7 @@ public abstract class GridCacheLockAbstractTest extends GridCommonAbstractTest {
                 finally {
                     Thread.sleep(1000);
 
-                    cache1.lockAll(Collections.singleton(kv)).unlock();
+                    lock.unlock();
 
                     info("Unlocked key in thread 1: " + kv);
                 }
@@ -252,7 +257,7 @@ public abstract class GridCacheLockAbstractTest extends GridCommonAbstractTest {
 
                 l1.await();
 
-                cache2.lock(kv).lock();
+                lock.lock();
 
                 try {
                     String v = cache2.get(kv);
@@ -262,7 +267,7 @@ public abstract class GridCacheLockAbstractTest extends GridCommonAbstractTest {
                     assertEquals(Integer.toString(kv), v);
                 }
                 finally {
-                    cache2.lockAll(Collections.singleton(kv)).unlock();
+                    lock.unlock();
 
                     info("Unlocked key in thread 2: " + kv);
                 }
@@ -297,7 +302,9 @@ public abstract class GridCacheLockAbstractTest extends GridCommonAbstractTest {
 
         GridTestThread t1 = new GridTestThread(new Callable<Object>() {
             @Nullable @Override public Object call() throws Exception {
-                cache1.lock(1).lock();
+                Lock lock = cache1.lock(1);
+
+                lock.lock();
 
                 info("Locked cache key: 1");
 
@@ -321,7 +328,7 @@ public abstract class GridCacheLockAbstractTest extends GridCommonAbstractTest {
                     info("Woke up from sleep.");
                 }
                 finally {
-                    cache1.lockAll(Collections.singleton(1)).unlock();
+                    lock.unlock();
 
                     info("Unlocked cache key: 1");
                 }
@@ -390,7 +397,9 @@ public abstract class GridCacheLockAbstractTest extends GridCommonAbstractTest {
                 @Nullable @Override public Object call() throws Exception {
                     info("Before lock for keys.");
 
-                    cache1.lockAll(keys).lock();
+                    Lock lock = cache1.lockAll(keys);
+
+                    lock.lock();
 
                     info("After lock for keys.");
 
@@ -418,7 +427,7 @@ public abstract class GridCacheLockAbstractTest extends GridCommonAbstractTest {
 
                         info("Before unlock keys in thread 1: " + keys);
 
-                        cache1.lockAll(keys).unlock();
+                        lock.unlock();
 
                         info("Unlocked entry for keys.");
                     }
@@ -442,7 +451,12 @@ public abstract class GridCacheLockAbstractTest extends GridCommonAbstractTest {
 
                         info("Before unlock keys in thread 2: " + keys);
 
-                        cache1.lockAll(keys).unlock();
+                        GridTestUtils.assertThrows(null, new Callable<Object>() {
+                            @Override public Object call() throws Exception {
+                                cache1.lockAll(keys).unlock();
+                                return null;
+                            }
+                        }, IllegalStateException.class, null);
 
                         // The keys should still be locked.
                         for (Integer key : keys)

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/dabe040c/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/distributed/GridCacheMultiNodeLockAbstractTest.java
----------------------------------------------------------------------
diff --git a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/distributed/GridCacheMultiNodeLockAbstractTest.java b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/distributed/GridCacheMultiNodeLockAbstractTest.java
index e9d0e3c..bc53d0b 100644
--- a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/distributed/GridCacheMultiNodeLockAbstractTest.java
+++ b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/distributed/GridCacheMultiNodeLockAbstractTest.java
@@ -17,6 +17,7 @@
 
 package org.apache.ignite.internal.processors.cache.distributed;
 
+import com.beust.jcommander.internal.*;
 import org.apache.ignite.*;
 import org.apache.ignite.cache.*;
 import org.apache.ignite.configuration.*;
@@ -31,9 +32,11 @@ import org.apache.ignite.internal.processors.cache.distributed.near.*;
 import org.apache.ignite.testframework.*;
 import org.apache.ignite.testframework.junits.common.*;
 import org.jetbrains.annotations.*;
+import org.jetbrains.annotations.Nullable;
 
 import java.util.*;
 import java.util.concurrent.*;
+import java.util.concurrent.locks.*;
 
 import static org.apache.ignite.events.IgniteEventType.*;
 
@@ -283,12 +286,14 @@ public abstract class GridCacheMultiNodeLockAbstractTest extends GridCommonAbstr
     public void testBasicLock() throws Exception {
         IgniteCache<Integer, String> cache = ignite1.jcache(null);
 
-        cache.lock(1).lock();
+        Lock lock = cache.lock(1);
+
+        lock.lock();
 
         assert cache.isLocalLocked(1, false);
         assert cache.isLocalLocked(1, true);
 
-        cache.lockAll(Collections.singleton(1)).unlock();
+        lock.unlock();
 
         checkUnlocked(cache, 1);
     }
@@ -321,7 +326,9 @@ public abstract class GridCacheMultiNodeLockAbstractTest extends GridCommonAbstr
         IgniteCache<Integer, String> cache1 = ignite1.jcache(null);
         IgniteCache<Integer, String> cache2 = ignite2.jcache(null);
 
-        cache1.lock(1).lock();
+        Lock lock1_1 = cache1.lock(1);
+
+        lock1_1.lock();
 
         assert cache1.isLocalLocked(1, false) : entries(1);
         assert cache1.isLocalLocked(1, true);
@@ -329,19 +336,21 @@ public abstract class GridCacheMultiNodeLockAbstractTest extends GridCommonAbstr
         assert cache2.isLocalLocked(1, false) : entries(1);
         assert !cache2.isLocalLocked(1, true);
 
+        Lock lock2_1 = cache2.lock(1);
+
         try {
-            assert !cache2.lock(1).tryLock();
+            assert !lock2_1.tryLock();
 
             assert cache2.isLocalLocked(1, false) : entries(1);
             assert !cache2.isLocalLocked(1, true);
         }
         finally {
-            cache1.lock(1).unlock();
+            lock1_1.unlock();
 
             checkUnlocked(cache1, 1);
         }
 
-        cache2.lock(1).lock();
+        lock2_1.lock();
 
         assert cache2.isLocalLocked(1, false) : entries(1);
         assert cache2.isLocalLocked(1, true);
@@ -354,13 +363,13 @@ public abstract class GridCacheMultiNodeLockAbstractTest extends GridCommonAbstr
         addListener(ignite1, new UnlockListener(latch, 1));
 
         try {
-            assert !cache1.lock(1).tryLock();
+            assert !lock1_1.tryLock();
 
             assert cache1.isLocalLocked(1, false) : entries(1);
             assert !cache1.isLocalLocked(1, true);
         }
         finally {
-            cache2.lockAll(Collections.singleton(1)).unlock();
+            lock2_1.unlock();
         }
 
         latch.await();
@@ -376,18 +385,18 @@ public abstract class GridCacheMultiNodeLockAbstractTest extends GridCommonAbstr
         IgniteCache<Integer, String> cache1 = ignite1.jcache(null);
         IgniteCache<Integer, String> cache2 = ignite2.jcache(null);
 
-        Set<Integer> keys1 = new HashSet<>();
-        Set<Integer> keys2 = new HashSet<>();
+        Collection<Integer> keys1 = Lists.newArrayList(1, 2, 3);
+        Collection<Integer> keys2 = Lists.newArrayList(2, 3, 4);
 
-        Collections.addAll(keys1, 1, 2, 3);
-        Collections.addAll(keys2, 2, 3, 4);
+        Lock lock1_1 = cache1.lockAll(keys1);
+        Lock lock2_2 = cache2.lockAll(keys2);
 
-        cache1.lockAll(keys1).lock();
+        lock1_1.lock();
 
         checkLocked(cache1, keys1);
 
         try {
-            assert !cache2.lockAll(keys2).tryLock();
+            assert !lock2_2.tryLock();
 
             assert cache2.isLocalLocked(2, false);
             assert cache2.isLocalLocked(3, false);
@@ -400,7 +409,7 @@ public abstract class GridCacheMultiNodeLockAbstractTest extends GridCommonAbstr
             assert !cache2.isLocalLocked(4, true);
         }
         finally {
-            cache1.lockAll(keys1).unlock();
+            lock1_1.unlock();
         }
 
         checkUnlocked(cache1, keys1);
@@ -408,7 +417,7 @@ public abstract class GridCacheMultiNodeLockAbstractTest extends GridCommonAbstr
         checkUnlocked(cache1, keys2);
         checkUnlocked(cache2, 4);
 
-        cache2.lockAll(keys2).lock();
+        lock2_2.lock();
 
         CountDownLatch latch1 = new CountDownLatch(keys2.size());
         CountDownLatch latch2 = new CountDownLatch(1);
@@ -416,12 +425,14 @@ public abstract class GridCacheMultiNodeLockAbstractTest extends GridCommonAbstr
         addListener(ignite2, new UnlockListener(latch2, 1));
         addListener(ignite1, (new UnlockListener(latch1, keys2)));
 
+        Lock lock1_ = cache1.lock(1);
+
         try {
             checkLocked(cache2, keys2);
 
             checkUnlocked(cache2, 1);
 
-            assert cache1.lock(1).tryLock();
+            assert lock1_.tryLock();
 
             checkLocked(cache1, 1);
 
@@ -430,9 +441,9 @@ public abstract class GridCacheMultiNodeLockAbstractTest extends GridCommonAbstr
             checkRemoteLocked(cache2, 1);
         }
         finally {
-            cache2.lockAll(keys2).unlock();
+            lock2_2.unlock();
 
-            cache1.lockAll(Collections.singleton(1)).unlock();
+            lock1_.unlock();
         }
 
         latch1.await();
@@ -450,21 +461,23 @@ public abstract class GridCacheMultiNodeLockAbstractTest extends GridCommonAbstr
     public void testLockReentry() throws IgniteCheckedException {
         IgniteCache<Integer, String> cache = ignite1.jcache(null);
 
-        cache.lock(1).lock();
+        Lock lock = cache.lock(1);
+
+        lock.lock();
 
         try {
             checkLocked(cache, 1);
 
-            cache.lock(1).lock();
+            lock.lock();
 
             checkLocked(cache, 1);
 
-            cache.lockAll(Collections.singleton(1)).unlock();
+            lock.unlock();
 
             checkLocked(cache, 1);
         }
         finally {
-            cache.lockAll(Collections.singleton(1)).unlock();
+            lock.unlock();
         }
 
         checkUnlocked(cache, 1);
@@ -479,12 +492,14 @@ public abstract class GridCacheMultiNodeLockAbstractTest extends GridCommonAbstr
         final CountDownLatch l1 = new CountDownLatch(1);
         final CountDownLatch l2 = new CountDownLatch(1);
 
+        final Lock lock1 = cache.lock(1);
+
         GridTestThread t1 = new GridTestThread(new Callable<Object>() {
             /** {@inheritDoc} */
             @Nullable @Override public Object call() throws Exception {
                 info("Before lock for.key 1");
 
-                cache.lock(1).lock();
+                lock1.lock();
 
                 info("After lock for key 1");
 
@@ -496,23 +511,25 @@ public abstract class GridCacheMultiNodeLockAbstractTest extends GridCommonAbstr
                     info("Let thread2 proceed.");
 
                     // Reentry.
-                    cache.lock(1).lock();
+                    lock1.lock();
 
                     checkLocked(cache, 1);
 
                     // Nested lock.
-                    assert cache.lock(2).tryLock();
+                    Lock lock2 = cache.lock(2);
+
+                    assert lock2.tryLock();
 
                     checkLocked(cache, 2);
 
                     // Unlock reentry.
-                    cache.lockAll(Collections.singleton(1)).unlock();
+                    lock1.unlock();
 
                     // Outer should still be owned.
                     checkLocked(cache, 1);
 
                     // Unlock in reverse order.
-                    cache.lockAll(Collections.singleton(2)).unlock();
+                    lock2.unlock();
 
                     checkUnlocked(cache, 2);
 
@@ -521,7 +538,7 @@ public abstract class GridCacheMultiNodeLockAbstractTest extends GridCommonAbstr
                     info("Waited for latch 2");
                 }
                 finally {
-                    cache.lockAll(Collections.singleton(1)).unlock();
+                    lock1.unlock();
 
                     info("Unlocked entry for key 1.");
                 }
@@ -542,7 +559,7 @@ public abstract class GridCacheMultiNodeLockAbstractTest extends GridCommonAbstr
 
                 info("Latch1 released.");
 
-                assert !cache.lock(1).tryLock();
+                assert !lock1.tryLock();
 
                 info("Tried to lock cache for key1");
 
@@ -550,7 +567,7 @@ public abstract class GridCacheMultiNodeLockAbstractTest extends GridCommonAbstr
 
                 info("Released latch2");
 
-                cache.lock(1).lock();
+                lock1.lock();
 
                 try {
                     info("Locked cache for key 1");
@@ -560,7 +577,7 @@ public abstract class GridCacheMultiNodeLockAbstractTest extends GridCommonAbstr
                     info("Checked that cache is locked for key 1");
                 }
                 finally {
-                    cache.lockAll(Collections.singleton(1)).unlock();
+                    lock1.unlock();
 
                     info("Unlocked cache for key 1");
                 }

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/dabe040c/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/distributed/dht/GridCacheColocatedDebugTest.java
----------------------------------------------------------------------
diff --git a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/distributed/dht/GridCacheColocatedDebugTest.java b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/distributed/dht/GridCacheColocatedDebugTest.java
index 1f8e401..3435991 100644
--- a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/distributed/dht/GridCacheColocatedDebugTest.java
+++ b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/distributed/dht/GridCacheColocatedDebugTest.java
@@ -376,11 +376,11 @@ public class GridCacheColocatedDebugTest extends GridCommonAbstractTest {
             final CountDownLatch lockLatch = new CountDownLatch(1);
             final CountDownLatch unlockLatch = new CountDownLatch(1);
 
+            final Lock lock = g0.jcache(null).lock(key);
+
             IgniteFuture<?> unlockFut = multithreadedAsync(new Runnable() {
                 @Override public void run() {
                     try {
-                        Lock lock = g0.jcache(null).lock(key);
-
                         lock.lock();
 
                         try {
@@ -404,8 +404,6 @@ public class GridCacheColocatedDebugTest extends GridCommonAbstractTest {
             assert g0.jcache(null).isLocalLocked(key, false);
             assert !g0.jcache(null).isLocalLocked(key, true) : "Key can not be locked by current thread.";
 
-            Lock lock = g0.jcache(null).lock(key);
-
             assert !lock.tryLock();
 
             assert g0.cache(null).isLocked(key);
@@ -897,13 +895,15 @@ public class GridCacheColocatedDebugTest extends GridCommonAbstractTest {
         try {
             IgniteCache<Object, Object> cache = jcache();
 
-            cache.lock(1).lock();
+            Lock lock = cache.lock(1);
+
+            lock.lock();
 
             assertNull(cache.getAndPut(1, "key1"));
             assertEquals("key1", cache.getAndPut(1, "key2"));
             assertEquals("key2", cache.get(1));
 
-            cache.lock(1).unlock();
+            lock.unlock();
         }
         finally {
             stopAllGrids();
@@ -929,9 +929,13 @@ public class GridCacheColocatedDebugTest extends GridCommonAbstractTest {
 
             IgniteCache<Object, Object> cache = jcache(0);
 
-            cache.lock(k0).lock();
-            cache.lock(k1).lock();
-            cache.lock(k2).lock();
+            Lock lock0 = cache.lock(k0);
+            Lock lock1 = cache.lock(k1);
+            Lock lock2 = cache.lock(k2);
+
+            lock0.lock();
+            lock1.lock();
+            lock2.lock();
 
             cache.put(k0, "val0");
 
@@ -941,9 +945,9 @@ public class GridCacheColocatedDebugTest extends GridCommonAbstractTest {
             assertEquals("val1", cache.get(k1));
             assertEquals("val2", cache.get(k2));
 
-            cache.lock(k0).unlock();
-            cache.lock(k1).unlock();
-            cache.lock(k2).unlock();
+            lock0.unlock();
+            lock1.unlock();
+            lock2.unlock();
         }
         finally {
             stopAllGrids();

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/dabe040c/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/distributed/near/GridCacheNearMultiNodeSelfTest.java
----------------------------------------------------------------------
diff --git a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/distributed/near/GridCacheNearMultiNodeSelfTest.java b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/distributed/near/GridCacheNearMultiNodeSelfTest.java
index 97e4764..1a3d2b1 100644
--- a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/distributed/near/GridCacheNearMultiNodeSelfTest.java
+++ b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/distributed/near/GridCacheNearMultiNodeSelfTest.java
@@ -40,6 +40,7 @@ import javax.cache.configuration.*;
 import java.util.*;
 import java.util.concurrent.*;
 import java.util.concurrent.atomic.*;
+import java.util.concurrent.locks.*;
 
 import static org.apache.ignite.cache.CacheAtomicityMode.*;
 import static org.apache.ignite.cache.CacheMode.*;
@@ -752,7 +753,9 @@ public class GridCacheNearMultiNodeSelfTest extends GridCommonAbstractTest {
 
         String val = Integer.toString(key);
 
-        near.lock(key).lock();
+        Lock lock = near.lock(key);
+
+        lock.lock();
 
         try {
             near.put(key, val);
@@ -763,7 +766,7 @@ public class GridCacheNearMultiNodeSelfTest extends GridCommonAbstractTest {
             assertTrue(near.isLocalLocked(key, false));
             assertTrue(near.isLocalLocked(key, true));
 
-            near.lock(key).lock(); // Reentry.
+            lock.lock(); // Reentry.
 
             try {
                 assertEquals(val, near.get(key));
@@ -776,7 +779,7 @@ public class GridCacheNearMultiNodeSelfTest extends GridCommonAbstractTest {
                 assertTrue(near.isLocalLocked(key, true));
             }
             finally {
-                near.lock(key).unlock();
+                lock.unlock();
             }
 
             assertTrue(near.isLocalLocked(key, false));
@@ -788,7 +791,7 @@ public class GridCacheNearMultiNodeSelfTest extends GridCommonAbstractTest {
             throw t;
         }
         finally {
-            near.lock(key).unlock();
+            lock.unlock();
         }
 
         assertFalse(near(0).isLockedNearOnly(key));

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/dabe040c/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/distributed/near/GridCacheNearOneNodeSelfTest.java
----------------------------------------------------------------------
diff --git a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/distributed/near/GridCacheNearOneNodeSelfTest.java b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/distributed/near/GridCacheNearOneNodeSelfTest.java
index c7427e6..d39715d 100644
--- a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/distributed/near/GridCacheNearOneNodeSelfTest.java
+++ b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/distributed/near/GridCacheNearOneNodeSelfTest.java
@@ -188,7 +188,9 @@ public class GridCacheNearOneNodeSelfTest extends GridCommonAbstractTest {
     public void testSingleLockPut() throws Exception {
         IgniteCache<Integer, String> near = jcache();
 
-        near.lock(1).lock();
+        Lock lock = near.lock(1);
+
+        lock.lock();
 
         try {
             near.put(1, "1");
@@ -200,7 +202,7 @@ public class GridCacheNearOneNodeSelfTest extends GridCommonAbstractTest {
             assertEquals("1", one);
         }
         finally {
-            near.lock(1).unlock();
+            lock.unlock();
         }
     }
 
@@ -228,7 +230,7 @@ public class GridCacheNearOneNodeSelfTest extends GridCommonAbstractTest {
             assertTrue(near.isLocalLocked(1, true));
         }
         finally {
-            near.lock(1).unlock();
+            lock.unlock();
         }
 
         assertFalse(near.isLocalLocked(1, false));
@@ -239,7 +241,9 @@ public class GridCacheNearOneNodeSelfTest extends GridCommonAbstractTest {
     public void testSingleLockReentry() throws Exception {
         IgniteCache<Integer, String> near = jcache();
 
-        near.lock(1).lock();
+        Lock lock = near.lock(1);
+
+        lock.lock();
 
         try {
             near.put(1, "1");
@@ -250,7 +254,7 @@ public class GridCacheNearOneNodeSelfTest extends GridCommonAbstractTest {
             assertTrue(near.isLocalLocked(1, false));
             assertTrue(near.isLocalLocked(1, true));
 
-            near.lock(1).lock(); // Reentry.
+            lock.lock(); // Reentry.
 
             try {
                 assertEquals("1", near.get(1));
@@ -263,14 +267,14 @@ public class GridCacheNearOneNodeSelfTest extends GridCommonAbstractTest {
                 assertTrue(near.isLocalLocked(1, true));
             }
             finally {
-                near.lock(1).unlock();
+                lock.unlock();
             }
 
             assertTrue(near.isLocalLocked(1, false));
             assertTrue(near.isLocalLocked(1, true));
         }
         finally {
-            near.lock(1).unlock();
+            lock.unlock();
         }
 
         assertFalse(near.isLocalLocked(1, false));

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/dabe040c/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/distributed/near/GridCacheNearReadersSelfTest.java
----------------------------------------------------------------------
diff --git a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/distributed/near/GridCacheNearReadersSelfTest.java b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/distributed/near/GridCacheNearReadersSelfTest.java
index 79111e9..fde4e58 100644
--- a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/distributed/near/GridCacheNearReadersSelfTest.java
+++ b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/distributed/near/GridCacheNearReadersSelfTest.java
@@ -32,6 +32,7 @@ import org.apache.ignite.testframework.junits.common.*;
 
 import java.util.*;
 import java.util.concurrent.atomic.*;
+import java.util.concurrent.locks.*;
 
 import static org.apache.ignite.cache.CacheAtomicityMode.*;
 import static org.apache.ignite.cache.CacheDistributionMode.*;
@@ -517,11 +518,15 @@ public class GridCacheNearReadersSelfTest extends GridCommonAbstractTest {
 
         IgniteCache<Integer, String> cache = jcache(0);
 
-        cache.lock(key1).lock();
+        Lock lock1 = cache.lock(key1);
+
+        lock1.lock();
 
         try {
             // Nested lock.
-            cache.lock(key2).lock();
+            Lock lock2 = cache.lock(key2);
+
+            lock2.lock();
 
             try {
                 assertNull(cache.getAndPut(key1, val1));
@@ -574,11 +579,11 @@ public class GridCacheNearReadersSelfTest extends GridCommonAbstractTest {
                 assertNull(near(2).peekEx(key2));
             }
             finally {
-                cache.lock(key2).lock();
+                lock2.lock();
             }
         }
         finally {
-            cache.lock(key1).unlock();
+            lock1.unlock();
         }
     }
 }

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/dabe040c/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/GridCacheEvictionLockUnlockSelfTest.java
----------------------------------------------------------------------
diff --git a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/GridCacheEvictionLockUnlockSelfTest.java b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/GridCacheEvictionLockUnlockSelfTest.java
index 27cae19..95f2419 100644
--- a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/GridCacheEvictionLockUnlockSelfTest.java
+++ b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/GridCacheEvictionLockUnlockSelfTest.java
@@ -30,6 +30,7 @@ import org.apache.ignite.testframework.junits.common.*;
 
 import java.util.concurrent.*;
 import java.util.concurrent.atomic.*;
+import java.util.concurrent.locks.*;
 
 import static java.util.concurrent.TimeUnit.*;
 import static org.apache.ignite.cache.CacheAtomicityMode.*;
@@ -124,8 +125,10 @@ public class GridCacheEvictionLockUnlockSelfTest extends GridCommonAbstractTest
 
                 IgniteCache<Object, Object> cache = jcache(i);
 
-                cache.lock("key").lock();
-                cache.lock("key").unlock();
+                Lock lock = cache.lock("key");
+
+                lock.lock();
+                lock.unlock();
 
                 assertTrue(evictLatch.await(3, SECONDS));
 

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/dabe040c/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/local/GridCacheLocalLockSelfTest.java
----------------------------------------------------------------------
diff --git a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/local/GridCacheLocalLockSelfTest.java b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/local/GridCacheLocalLockSelfTest.java
index af0f865..9b8d648 100644
--- a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/local/GridCacheLocalLockSelfTest.java
+++ b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/local/GridCacheLocalLockSelfTest.java
@@ -27,6 +27,7 @@ import org.apache.ignite.testframework.junits.common.*;
 import org.jetbrains.annotations.*;
 
 import java.util.concurrent.*;
+import java.util.concurrent.locks.*;
 
 import static org.apache.ignite.cache.CacheMode.*;
 
@@ -83,7 +84,9 @@ public class GridCacheLocalLockSelfTest extends GridCommonAbstractTest {
         assert !cache.isLocalLocked(1, false);
         assert !cache.isLocalLocked(1, true);
 
-        cache.lock(1).lock();
+        Lock lock = cache.lock(1);
+
+        lock.lock();
 
         assert cache.isLocalLocked(1, false);
         assert cache.isLocalLocked(1, true);
@@ -94,7 +97,7 @@ public class GridCacheLocalLockSelfTest extends GridCommonAbstractTest {
             assert "1".equals(cache.get(1));
 
             // Reentry.
-            cache.lock(1).lock();
+            lock.lock();
 
             assert cache.isLocalLocked(1, false);
             assert cache.isLocalLocked(1, true);
@@ -103,14 +106,14 @@ public class GridCacheLocalLockSelfTest extends GridCommonAbstractTest {
                 assert "1".equals(cache.getAndRemove(1));
             }
             finally {
-                cache.lock(1).unlock();
+                lock.unlock();
             }
 
             assert cache.isLocalLocked(1, false);
             assert cache.isLocalLocked(1, true);
         }
         finally {
-            cache.lock(1).unlock();
+            lock.unlock();
         }
 
         assert !cache.isLocalLocked(1, false);
@@ -127,12 +130,14 @@ public class GridCacheLocalLockSelfTest extends GridCommonAbstractTest {
         final CountDownLatch latch2 = new CountDownLatch(1);
         final CountDownLatch latch3 = new CountDownLatch(1);
 
+        final Lock lock = cache.lock(1);
+
         GridTestThread t1 = new GridTestThread(new Callable<Object>() {
             @SuppressWarnings({"CatchGenericClass"})
             @Nullable @Override public Object call() throws Exception {
                 info("Before lock for.key 1");
 
-                cache.lock(1).lock();
+                lock.lock();
 
                 info("After lock for key 1");
 
@@ -153,7 +158,7 @@ public class GridCacheLocalLockSelfTest extends GridCommonAbstractTest {
                     info("Waited for latch 2");
                 }
                 finally {
-                    cache.lock(1).unlock();
+                    lock.unlock();
 
                     info("Unlocked entry for key 1.");
 
@@ -173,7 +178,7 @@ public class GridCacheLocalLockSelfTest extends GridCommonAbstractTest {
 
                 info("Latch1 released.");
 
-                assert !cache.lock(1).tryLock();
+                assert !lock.tryLock();
 
                 assert cache.isLocalLocked(1, false);
                 assert !cache.isLocalLocked(1, true);
@@ -186,7 +191,7 @@ public class GridCacheLocalLockSelfTest extends GridCommonAbstractTest {
 
                 latch3.await();
 
-                assert cache.lock(1).tryLock();
+                assert lock.tryLock();
 
                 assert cache.isLocalLocked(1, false);
                 assert cache.isLocalLocked(1, true);
@@ -208,7 +213,7 @@ public class GridCacheLocalLockSelfTest extends GridCommonAbstractTest {
                     info("Checked that cache is locked for key 1");
                 }
                 finally {
-                    cache.lock(1).unlock();
+                    lock.unlock();
 
                     info("Unlocked cache for key 1");
                 }
@@ -243,7 +248,9 @@ public class GridCacheLocalLockSelfTest extends GridCommonAbstractTest {
 
         GridTestThread t1 = new GridTestThread(new Callable<Object>() {
             @Nullable @Override public Object call() throws Exception {
-                cache.lock(1).lock();
+                Lock lock = cache.lock(1);
+
+                lock.lock();
 
                 info("Locked cache key: 1");
 
@@ -267,7 +274,7 @@ public class GridCacheLocalLockSelfTest extends GridCommonAbstractTest {
                     info("Woke up from sleep.");
                 }
                 finally {
-                    cache.lock(1).unlock();
+                    lock.unlock();
 
                     info("Unlocked cache key: 1");
                 }

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/dabe040c/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/local/GridCacheLocalMultithreadedSelfTest.java
----------------------------------------------------------------------
diff --git a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/local/GridCacheLocalMultithreadedSelfTest.java b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/local/GridCacheLocalMultithreadedSelfTest.java
index 5cf7ce0..a5f9402 100644
--- a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/local/GridCacheLocalMultithreadedSelfTest.java
+++ b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/local/GridCacheLocalMultithreadedSelfTest.java
@@ -29,6 +29,7 @@ import org.apache.ignite.testframework.junits.common.*;
 import java.util.*;
 import java.util.concurrent.*;
 import java.util.concurrent.atomic.*;
+import java.util.concurrent.locks.*;
 
 import static org.apache.ignite.cache.CacheMode.*;
 
@@ -86,7 +87,9 @@ public class GridCacheLocalMultithreadedSelfTest extends GridCommonAbstractTest
         GridTestUtils.runMultiThreaded(new Callable<Object>() {
             /** {@inheritDoc} */
             @Override public Object call() throws Exception {
-                assert cache.lock(1).tryLock(1000L, TimeUnit.MILLISECONDS);
+                Lock lock = cache.lock(1);
+
+                assert lock.tryLock(1000L, TimeUnit.MILLISECONDS);
 
                 info("Locked key from thread: " + thread());
 
@@ -94,7 +97,7 @@ public class GridCacheLocalMultithreadedSelfTest extends GridCommonAbstractTest
 
                 info("Unlocking key from thread: " + thread());
 
-                cache.lock(1).unlock();
+                lock.unlock();
 
                 info("Unlocked key from thread: " + thread());
 
@@ -112,7 +115,9 @@ public class GridCacheLocalMultithreadedSelfTest extends GridCommonAbstractTest
             @Override public Object call() throws Exception {
                 Set<Integer> keys = Sets.newHashSet(1, 2, 3);
 
-                cache.lockAll(keys).lock();
+                Lock lock = cache.lockAll(keys);
+
+                lock.lock();
 
                 info("Locked keys from thread [keys=" + keys + ", thread=" + thread() + ']');
 
@@ -120,7 +125,7 @@ public class GridCacheLocalMultithreadedSelfTest extends GridCommonAbstractTest
 
                 info("Unlocking key from thread: " + thread());
 
-                cache.lockAll(keys).unlock();
+                lock.unlock();
 
                 info("Unlocked keys from thread: " + thread());
 
@@ -142,7 +147,9 @@ public class GridCacheLocalMultithreadedSelfTest extends GridCommonAbstractTest
 
                 Set<Integer> keys = Sets.newHashSet(idx, idx + 1, idx + 2, idx + 3);
 
-                cache.lockAll(keys).lock();
+                Lock lock = cache.lockAll(keys);
+
+                lock.lock();
 
                 info("Locked keys from thread [keys=" + keys + ", thread=" + thread() + ']');
 
@@ -150,7 +157,7 @@ public class GridCacheLocalMultithreadedSelfTest extends GridCommonAbstractTest
 
                 info("Unlocking key from thread [keys=" + keys + ", thread=" + thread() + ']');
 
-                cache.lockAll(keys).unlock();
+                lock.unlock();
 
                 info("Unlocked keys from thread [keys=" + keys + ", thread=" + thread() + ']');
 
@@ -166,12 +173,14 @@ public class GridCacheLocalMultithreadedSelfTest extends GridCommonAbstractTest
         final CountDownLatch l1 = new CountDownLatch(1);
         final CountDownLatch l2 = new CountDownLatch(1);
 
+        final Lock lock = cache.lock(1);
+
         GridTestThread t1 = new GridTestThread(new Callable<Object>() {
             /** {@inheritDoc} */
             @Override public Object call() throws Exception {
                 assert !cache.isLocalLocked(1, false);
 
-                cache.lock(1).lock();
+                lock.lock();
 
                 assert cache.isLocalLocked(1, true);
                 assert cache.isLocalLocked(1, false);
@@ -180,7 +189,7 @@ public class GridCacheLocalMultithreadedSelfTest extends GridCommonAbstractTest
 
                 l2.await();
 
-                cache.lock(1).unlock();
+                lock.unlock();
 
                 assert !cache.isLocalLocked(1, true);
                 assert !cache.isLocalLocked(1, false);
@@ -197,7 +206,7 @@ public class GridCacheLocalMultithreadedSelfTest extends GridCommonAbstractTest
                 assert cache.isLocalLocked(1, false);
                 assert !cache.isLocalLocked(1, true);
 
-                assert !cache.lock(1).tryLock(100L, TimeUnit.MILLISECONDS);
+                assert !lock.tryLock(100L, TimeUnit.MILLISECONDS);
 
                 assert cache.isLocalLocked(1, false);
                 assert !cache.isLocalLocked(1, true);
@@ -245,7 +254,9 @@ public class GridCacheLocalMultithreadedSelfTest extends GridCommonAbstractTest
 
                 Collections.addAll(keys1, idx, idx + 1, idx + 2, idx + 3);
 
-                cache.lockAll(keys1).lock();
+                Lock lock = cache.lockAll(keys1);
+
+                lock.lock();
 
                 for (Integer key : keys1) {
                     assert cache.isLocalLocked(key, false) : "Failed to acquire lock for key: " + key;
@@ -256,7 +267,7 @@ public class GridCacheLocalMultithreadedSelfTest extends GridCommonAbstractTest
 
                 l2.await();
 
-                cache.lockAll(keys1).unlock();
+                lock.unlock();
 
                 for (Integer key : keys1) {
                     assert !cache.isLocalLocked(key, false);

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/dabe040c/modules/core/src/test/java/org/apache/ignite/spi/communication/tcp/GridCacheDhtLockBackupSelfTest.java
----------------------------------------------------------------------
diff --git a/modules/core/src/test/java/org/apache/ignite/spi/communication/tcp/GridCacheDhtLockBackupSelfTest.java b/modules/core/src/test/java/org/apache/ignite/spi/communication/tcp/GridCacheDhtLockBackupSelfTest.java
index 2b976ef..f389718 100644
--- a/modules/core/src/test/java/org/apache/ignite/spi/communication/tcp/GridCacheDhtLockBackupSelfTest.java
+++ b/modules/core/src/test/java/org/apache/ignite/spi/communication/tcp/GridCacheDhtLockBackupSelfTest.java
@@ -37,6 +37,7 @@ import org.jetbrains.annotations.*;
 
 import java.util.*;
 import java.util.concurrent.*;
+import java.util.concurrent.locks.*;
 
 import static org.apache.ignite.cache.CachePreloadMode.*;
 import static org.apache.ignite.cache.CacheWriteSynchronizationMode.*;
@@ -125,7 +126,9 @@ public class GridCacheDhtLockBackupSelfTest extends GridCommonAbstractTest {
             @Nullable @Override public Object call() throws Exception {
                 info("Before lock for key: " + kv);
 
-                cache1.lock(kv).lock();
+                Lock lock = cache1.lock(kv);
+
+                lock.lock();
 
                 info("After lock for key: " + kv);
 
@@ -144,7 +147,7 @@ public class GridCacheDhtLockBackupSelfTest extends GridCommonAbstractTest {
                 finally {
                     Thread.sleep(1000);
 
-                    cache1.lockAll(Collections.singleton(kv)).unlock();
+                    lock.unlock();
 
                     info("Unlocked key in thread 1: " + kv);
                 }
@@ -161,7 +164,9 @@ public class GridCacheDhtLockBackupSelfTest extends GridCommonAbstractTest {
 
                 l1.await();
 
-                cache2.lock(kv).lock();
+                Lock lock = cache2.lock(kv);
+
+                lock.lock();
 
                 try {
                     String v = cache2.get(kv);
@@ -170,7 +175,7 @@ public class GridCacheDhtLockBackupSelfTest extends GridCommonAbstractTest {
                     assertEquals(Integer.toString(kv), v);
                 }
                 finally {
-                    cache2.lockAll(Collections.singleton(kv)).unlock();
+                    lock.unlock();
 
                     info("Unlocked key in thread 2: " + kv);
                 }


Mime
View raw message