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 9bd4ea9b783b1..c1ed7eb5969cc 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 implements Lock { /** */ private final Collection keys; + /** */ + private int cntr; + + /** */ + private volatile Thread lockedThread; + /** * @param delegate Delegate. * @param keys Keys. @@ -50,12 +56,25 @@ class CacheLockImpl 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 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 implements Lock { if (Thread.interrupted()) throw new InterruptedException(); - try { - if (time <= 0) - return delegate.lockAll(keys, -1); + if (time <= 0) + return tryLock(); - IgniteFuture fut = null; + try { + IgniteFuture 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 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) { 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 993a615fe6bdf..4df983a36e863 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 GridCacheContext context() { /** {@inheritDoc} */ @Override public Lock lock(K key) throws CacheException { - return lockAll(Collections.singleton(key)); + return lockAll(Collections.singleton(key)); } /** {@inheritDoc} */ @Override public Lock lockAll(final Collection keys) { - return new CacheLockImpl(delegate, keys); + return new CacheLockImpl<>(delegate, keys); } /** {@inheritDoc} */ 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 fcc52ff7e314d..715baed6d9845 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 void testLockUnlock() throws Exception { 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 void testLockUnlockAll() throws Exception { 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 void testLockUnlockAll() throws Exception { 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)) 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 3e07da5e9b99a..1309c439694ff 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.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 void testReentry() throws Exception { public void testInterruptLock() throws InterruptedException { final IgniteCache 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 void testInterruptLock() throws InterruptedException { @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 void testInterruptLock() throws InterruptedException { t.interrupt(); - cache.lock(1).unlock(); + lock.unlock(); t.join(); @@ -210,7 +214,9 @@ public void testInterruptLock() throws InterruptedException { public void testInterruptLockWithTimeout() throws InterruptedException { final IgniteCache 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 void testInterruptLockWithTimeout() throws InterruptedException { t.join(); - cache.lock(2).unlock(); + lock2.unlock(); assertFalse(cache.isLocalLocked(1, false)); assertFalse(cache.isLocalLocked(2, false)); @@ -249,7 +255,9 @@ public void testManyLockReentries() throws IgniteCheckedException { 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 void testManyLockReentries() throws IgniteCheckedException { 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 void testManyLockReentries() throws IgniteCheckedException { 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 void testLockMultithreaded() throws Exception { final CountDownLatch l2 = new CountDownLatch(1); final CountDownLatch l3 = new CountDownLatch(1); + final Lock lock = cache.lock(1); + GridTestThread t1 = new GridTestThread(new Callable() { /** {@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 void testLockMultithreaded() throws Exception { 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 void testLockMultithreaded() throws Exception { info("Latch1 released."); - assert !cache.lock(1).tryLock(); + assert !lock.tryLock(); if (!cache.isLocalLocked(1, false)) throw new IllegalArgumentException(); @@ -360,7 +372,7 @@ public void testLockMultithreaded() throws Exception { l3.await(); - assert cache.lock(1).tryLock(); + assert lock.tryLock(); try { info("Locked cache for key 1"); @@ -371,7 +383,7 @@ public void testLockMultithreaded() throws Exception { 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 void testBasicOpsWithReentry() throws Exception { 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 void testBasicOpsWithReentry() throws Exception { assert cache.isLocalLocked(key, false); } finally { - cache.lock(key).unlock(); + lock.unlock(); ignite.events().stopLocalListen(lsnr, EVTS_CACHE); } @@ -533,11 +547,11 @@ public void testBasicOpsWithReentry() throws Exception { public void testMultiLocks() throws Exception { IgniteCache cache = ignite.jcache(null); - Set keys = new HashSet<>(); + Collection 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 void testMultiLocks() throws Exception { 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); 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 87be338905d69..5ce38f9f87aa0 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 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 void testMvccFinishPartitionsContinuousLockAcquireRelease() throws Except IgniteCache cache = grid.jcache(null); - cache.lock(key).lock(); + Lock lock = cache.lock(key); + + lock.lock(); long start = System.currentTimeMillis(); @@ -244,17 +247,21 @@ public void testMvccFinishPartitionsContinuousLockAcquireRelease() throws Except } }); - 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 @@ private long runLock(String key, int keyPart, Collection waitParts) thr IgniteCache cache = grid.jcache(null); - cache.lock(key).lock(); + Lock lock = cache.lock(key); + + lock.lock(); long start; try { @@ -302,7 +311,7 @@ private long runLock(String key, int keyPart, Collection waitParts) thr + fut.isDone() + ']'; } finally { - cache.lock(key).unlock(); + lock.unlock(); } latch.await(); 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 49a3da0b48f5e..f1637df041511 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 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 @@ private void checkGroupLockWithExternalLock(final IgniteTxConcurrency concurrenc 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 @@ private void checkSanityCheckDisabled(final IgniteTxConcurrency concurrency) thr 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 @@ private void checkSanityCheckDisabled(final IgniteTxConcurrency concurrency) thr } } finally { - cache.lock(key1).unlock(); + lock.unlock(); } } 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 f92005c866539..7121ee1a5a06e 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 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 void testLockAndTx() throws Exception { 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 void testLockAndTx() throws Exception { error("Failed lock thread", e); } finally { - c.lock(CNTR_KEY).unlock(); + lock.unlock(); } } })); @@ -224,8 +227,10 @@ public void testLockAndTx1() throws Exception { 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 void testLockAndTx1() throws Exception { error("Failed lock thread", e); } finally { - c.lock(CNTR_KEY).unlock(); + lock.unlock(); } } })); 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 e89d26a225810..ab6b5fb1bdf32 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 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 @@ private void checkLockUnlock(Integer key) throws Exception { 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)); 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 08630920ba0d9..c6686cf0b70c9 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 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 void testLockSingleThread() throws Exception { 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 void testLockSingleThread() throws Exception { info("Put " + k + '=' + k + " key pair into cache."); } finally { - cache1.lock(k).unlock(); + lock.unlock(); info("Unlocked key: " + k); } @@ -209,11 +212,13 @@ public void testLock() throws Exception { final CountDownLatch l1 = new CountDownLatch(1); final CountDownLatch l2 = new CountDownLatch(1); + final Lock lock = cache1.lock(kv); + GridTestThread t1 = new GridTestThread(new Callable() { @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 void testLock() throws Exception { finally { Thread.sleep(1000); - cache1.lockAll(Collections.singleton(kv)).unlock(); + lock.unlock(); info("Unlocked key in thread 1: " + kv); } @@ -252,7 +257,7 @@ public void testLock() throws Exception { l1.await(); - cache2.lock(kv).lock(); + lock.lock(); try { String v = cache2.get(kv); @@ -262,7 +267,7 @@ public void testLock() throws Exception { 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 void testLockAndPut() throws Exception { GridTestThread t1 = new GridTestThread(new Callable() { @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 void testLockAndPut() throws Exception { info("Woke up from sleep."); } finally { - cache1.lockAll(Collections.singleton(1)).unlock(); + lock.unlock(); info("Unlocked cache key: 1"); } @@ -390,7 +397,9 @@ public void testLockTimeoutTwoThreads() throws Exception { @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 void testLockTimeoutTwoThreads() throws Exception { info("Before unlock keys in thread 1: " + keys); - cache1.lockAll(keys).unlock(); + lock.unlock(); info("Unlocked entry for keys."); } @@ -442,7 +451,12 @@ public void testLockTimeoutTwoThreads() throws Exception { info("Before unlock keys in thread 2: " + keys); - cache1.lockAll(keys).unlock(); + GridTestUtils.assertThrows(null, new Callable() { + @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) 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 e9d0e3c871ed4..bc53d0bdd79ff 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.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 @@ private void checkUnlocked(IgniteCache cache, Iterable public void testBasicLock() throws Exception { IgniteCache 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 void testMultiNodeLock() throws Exception { IgniteCache cache1 = ignite1.jcache(null); IgniteCache 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 void testMultiNodeLock() throws Exception { 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 void testMultiNodeLock() throws Exception { 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 void testMultiNodeLockWithKeyLists() throws Exception { IgniteCache cache1 = ignite1.jcache(null); IgniteCache cache2 = ignite2.jcache(null); - Set keys1 = new HashSet<>(); - Set keys2 = new HashSet<>(); + Collection keys1 = Lists.newArrayList(1, 2, 3); + Collection 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 void testMultiNodeLockWithKeyLists() throws Exception { assert !cache2.isLocalLocked(4, true); } finally { - cache1.lockAll(keys1).unlock(); + lock1_1.unlock(); } checkUnlocked(cache1, keys1); @@ -408,7 +417,7 @@ public void testMultiNodeLockWithKeyLists() throws Exception { 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 void testMultiNodeLockWithKeyLists() throws Exception { 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 void testMultiNodeLockWithKeyLists() throws Exception { 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 void testMultiNodeLockWithKeyLists() throws Exception { public void testLockReentry() throws IgniteCheckedException { IgniteCache 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 void testLockMultithreaded() throws Exception { final CountDownLatch l1 = new CountDownLatch(1); final CountDownLatch l2 = new CountDownLatch(1); + final Lock lock1 = cache.lock(1); + GridTestThread t1 = new GridTestThread(new Callable() { /** {@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 void testLockMultithreaded() throws Exception { 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 void testLockMultithreaded() throws Exception { 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 void testLockMultithreaded() throws Exception { info("Latch1 released."); - assert !cache.lock(1).tryLock(); + assert !lock1.tryLock(); info("Tried to lock cache for key1"); @@ -550,7 +567,7 @@ public void testLockMultithreaded() throws Exception { info("Released latch2"); - cache.lock(1).lock(); + lock1.lock(); try { info("Locked cache for key 1"); @@ -560,7 +577,7 @@ public void testLockMultithreaded() throws Exception { info("Checked that cache is locked for key 1"); } finally { - cache.lockAll(Collections.singleton(1)).unlock(); + lock1.unlock(); info("Unlocked cache for key 1"); } 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 1f8e401660820..34359917e34a5 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 @@ private void checkLockLocked(boolean loc) throws Exception { 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 @@ private void checkLockLocked(boolean loc) throws Exception { 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 void testExplicitLocks() throws Exception { try { IgniteCache 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 void testExplicitLocksDistributed() throws Exception { IgniteCache 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 void testExplicitLocksDistributed() throws Exception { 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(); 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 97e47641ef8e1..1a3d2b1d9f422 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 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 @@ private void checkSingleLockReentry(int key) throws Throwable { 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 @@ private void checkSingleLockReentry(int key) throws Throwable { 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 @@ private void checkSingleLockReentry(int key) throws Throwable { assertTrue(near.isLocalLocked(key, true)); } finally { - near.lock(key).unlock(); + lock.unlock(); } assertTrue(near.isLocalLocked(key, false)); @@ -788,7 +791,7 @@ private void checkSingleLockReentry(int key) throws Throwable { throw t; } finally { - near.lock(key).unlock(); + lock.unlock(); } assertFalse(near(0).isLockedNearOnly(key)); 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 c7427e65bab4a..d39715d5e8833 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 void testOptimisticTxWriteThrough() throws Exception { public void testSingleLockPut() throws Exception { IgniteCache near = jcache(); - near.lock(1).lock(); + Lock lock = near.lock(1); + + lock.lock(); try { near.put(1, "1"); @@ -200,7 +202,7 @@ public void testSingleLockPut() throws Exception { assertEquals("1", one); } finally { - near.lock(1).unlock(); + lock.unlock(); } } @@ -228,7 +230,7 @@ public void testSingleLock() throws Exception { assertTrue(near.isLocalLocked(1, true)); } finally { - near.lock(1).unlock(); + lock.unlock(); } assertFalse(near.isLocalLocked(1, false)); @@ -239,7 +241,9 @@ public void testSingleLock() throws Exception { public void testSingleLockReentry() throws Exception { IgniteCache near = jcache(); - near.lock(1).lock(); + Lock lock = near.lock(1); + + lock.lock(); try { near.put(1, "1"); @@ -250,7 +254,7 @@ public void testSingleLockReentry() throws Exception { 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 void testSingleLockReentry() throws Exception { 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)); 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 79111e981b7b6..fde4e58946829 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 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 void testExplicitLockReaders() throws Exception { IgniteCache 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 void testExplicitLockReaders() throws Exception { assertNull(near(2).peekEx(key2)); } finally { - cache.lock(key2).lock(); + lock2.lock(); } } finally { - cache.lock(key1).unlock(); + lock1.unlock(); } } } 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 27cae1948719c..95f2419277fad 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 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 @@ private void doTest() throws Exception { IgniteCache 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)); 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 af0f865c60429..9b8d6486d02b8 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.jetbrains.annotations.*; import java.util.concurrent.*; +import java.util.concurrent.locks.*; import static org.apache.ignite.cache.CacheMode.*; @@ -83,7 +84,9 @@ public void testLockReentry() throws IgniteCheckedException { 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 void testLockReentry() throws IgniteCheckedException { 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 void testLockReentry() throws IgniteCheckedException { 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 void testLock() throws Throwable { final CountDownLatch latch2 = new CountDownLatch(1); final CountDownLatch latch3 = new CountDownLatch(1); + final Lock lock = cache.lock(1); + GridTestThread t1 = new GridTestThread(new Callable() { @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 void testLock() throws Throwable { info("Waited for latch 2"); } finally { - cache.lock(1).unlock(); + lock.unlock(); info("Unlocked entry for key 1."); @@ -173,7 +178,7 @@ public void testLock() throws Throwable { 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 void testLock() throws Throwable { 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 void testLock() throws Throwable { 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 void testLockAndPut() throws Throwable { GridTestThread t1 = new GridTestThread(new Callable() { @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 void testLockAndPut() throws Throwable { info("Woke up from sleep."); } finally { - cache.lock(1).unlock(); + lock.unlock(); info("Unlocked cache key: 1"); } 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 5cf7ce0ea41f9..a5f9402a0e965 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 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 void testBasicLocks() throws Throwable { GridTestUtils.runMultiThreaded(new Callable() { /** {@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 void testBasicLocks() throws Throwable { info("Unlocking key from thread: " + thread()); - cache.lock(1).unlock(); + lock.unlock(); info("Unlocked key from thread: " + thread()); @@ -112,7 +115,9 @@ public void testMultiLocks() throws Throwable { @Override public Object call() throws Exception { Set 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 void testMultiLocks() throws Throwable { info("Unlocking key from thread: " + thread()); - cache.lockAll(keys).unlock(); + lock.unlock(); info("Unlocked keys from thread: " + thread()); @@ -142,7 +147,9 @@ public void testSlidingKeysLocks() throws Throwable { Set 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 void testSlidingKeysLocks() throws Throwable { 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 void testSingleLockTimeout() throws Exception { final CountDownLatch l1 = new CountDownLatch(1); final CountDownLatch l2 = new CountDownLatch(1); + final Lock lock = cache.lock(1); + GridTestThread t1 = new GridTestThread(new Callable() { /** {@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 void testSingleLockTimeout() throws Exception { l2.await(); - cache.lock(1).unlock(); + lock.unlock(); assert !cache.isLocalLocked(1, true); assert !cache.isLocalLocked(1, false); @@ -197,7 +206,7 @@ public void testSingleLockTimeout() throws Exception { 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 void testMultiLockTimeout() throws Exception { 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 void testMultiLockTimeout() throws Exception { l2.await(); - cache.lockAll(keys1).unlock(); + lock.unlock(); for (Integer key : keys1) { assert !cache.isLocalLocked(key, false); 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 2b976efb316b0..f38971831de7c 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 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 void testLock() throws Exception { @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 void testLock() throws Exception { finally { Thread.sleep(1000); - cache1.lockAll(Collections.singleton(kv)).unlock(); + lock.unlock(); info("Unlocked key in thread 1: " + kv); } @@ -161,7 +164,9 @@ public void testLock() throws Exception { l1.await(); - cache2.lock(kv).lock(); + Lock lock = cache2.lock(kv); + + lock.lock(); try { String v = cache2.get(kv); @@ -170,7 +175,7 @@ public void testLock() throws Exception { assertEquals(Integer.toString(kv), v); } finally { - cache2.lockAll(Collections.singleton(kv)).unlock(); + lock.unlock(); info("Unlocked key in thread 2: " + kv); }