Skip to content

Commit

Permalink
Remove CacheLock class, merge IgniteCache.isLocked(key) and IgniteCac…
Browse files Browse the repository at this point in the history
…he.isLockedByThread(key) to IgniteCache.isLocalLocked(key, boolean).
  • Loading branch information
Sergey Evdokimov committed Jan 23, 2015
1 parent 15cbff4 commit b9c6f3b
Show file tree
Hide file tree
Showing 15 changed files with 239 additions and 330 deletions.
38 changes: 17 additions & 21 deletions modules/core/src/main/java/org/apache/ignite/IgniteCache.java
Expand Up @@ -29,6 +29,7 @@
import javax.cache.processor.*; import javax.cache.processor.*;
import java.util.*; import java.util.*;
import java.util.concurrent.*; import java.util.concurrent.*;
import java.util.concurrent.locks.*;


/** /**
* Main entry point for all <b>Data Grid APIs.</b> You can get a named cache by calling {@link Ignite#cache(String)} * Main entry point for all <b>Data Grid APIs.</b> You can get a named cache by calling {@link Ignite#cache(String)}
Expand Down Expand Up @@ -158,48 +159,43 @@ public interface IgniteCache<K, V> extends javax.cache.Cache<K, V>, IgniteAsyncS
@Nullable public V getAndPutIfAbsent(K key, V val) throws CacheException; @Nullable public V getAndPutIfAbsent(K key, V val) throws CacheException;


/** /**
* Return a {@link CacheLock} instance associated with passed key. * Creates a {@link Lock} instance associated with passed key.
* This method does not acquire lock immediately, you have to call appropriate method on returned instance. * This method does not acquire lock immediately, you have to call appropriate method on returned instance.
* Returned lock does not support {@link Lock#newCondition()} method,
* other methods defined in {@link Lock} are supported.
* *
* @param key Key for lock. * @param key Key for lock.
* @return New lock instance associated with passed key. * @return New lock instance associated with passed key.
* @see CacheLock#lock() * @see Lock#lock()
* @see CacheLock#tryLock(long, TimeUnit) * @see Lock#tryLock(long, TimeUnit)
*/ */
public CacheLock lock(K key); public Lock lock(K key);


/** /**
* Return a {@link CacheLock} instance associated with passed keys. * Creates a {@link Lock} instance associated with passed keys.
* This method does not acquire lock immediately, you have to call appropriate method on returned instance. * This method does not acquire lock immediately, you have to call appropriate method on returned instance.
* Returned lock does not support {@link Lock#newCondition()} method,
* other methods defined in {@link Lock} are supported.
* *
* @param keys Keys for lock. * @param keys Keys for lock.
* @return New lock instance associated with passed key. * @return New lock instance associated with passed key.
* @see CacheLock#lock() * @see Lock#lock()
* @see CacheLock#tryLock(long, TimeUnit) * @see Lock#tryLock(long, TimeUnit)
*/ */
public CacheLock lockAll(Collection<? extends K> keys); public Lock lockAll(Collection<? extends K> keys);


/** /**
* Checks if any node owns a lock for this key. * Checks if specified key is locked.
* <p> * <p>
* This is a local in-VM operation and does not involve any network trips * This is a local in-VM operation and does not involve any network trips
* or access to persistent storage in any way. * or access to persistent storage in any way.
* *
* @param key Key to check. * @param key Key to check.
* @param byCurrThread If {@code true} method will check that current thread owns a lock on this key, other vise
* will check that any thread on any node owns a lock on this key.
* @return {@code True} if lock is owned by some node. * @return {@code True} if lock is owned by some node.
*/ */
public boolean isLocked(K key); public boolean isLocalLocked(K key, boolean byCurrThread);

/**
* Checks if current thread owns a lock on this key.
* <p>
* This is a local in-VM operation and does not involve any network trips
* or access to persistent storage in any way.
*
* @param key Key to check.
* @return {@code True} if key is locked by current thread.
*/
public boolean isLockedByThread(K key);


public QueryCursor<Entry<K, V>> query(QueryPredicate<K, V> filter); public QueryCursor<Entry<K, V>> query(QueryPredicate<K, V> filter);


Expand Down
57 changes: 0 additions & 57 deletions modules/core/src/main/java/org/apache/ignite/cache/CacheLock.java

This file was deleted.

Expand Up @@ -30,7 +30,7 @@
/** /**
* *
*/ */
class CacheLockImpl<K> implements CacheLock { class CacheLockImpl<K> implements Lock {
/** */ /** */
private final GridCacheProjectionEx<K, ?> delegate; private final GridCacheProjectionEx<K, ?> delegate;


Expand All @@ -46,26 +46,6 @@ class CacheLockImpl<K> implements CacheLock {
this.keys = keys; this.keys = keys;
} }


/** {@inheritDoc} */
@Override public boolean isLocked() {
for (K key : keys) {
if (!delegate.isLocked(key))
return false;
}

return true;
}

/** {@inheritDoc} */
@Override public boolean isLockedByThread() {
for (K key : keys) {
if (!delegate.isLockedByThread(key))
return false;
}

return true;
}

/** {@inheritDoc} */ /** {@inheritDoc} */
@Override public void lock() { @Override public void lock() {
try { try {
Expand Down
Expand Up @@ -36,6 +36,7 @@
import java.io.*; import java.io.*;
import java.util.*; import java.util.*;
import java.util.concurrent.*; import java.util.concurrent.*;
import java.util.concurrent.locks.*;


/** /**
* Cache proxy. * Cache proxy.
Expand Down Expand Up @@ -177,33 +178,21 @@ public GridCacheContext<K, V> context() {
} }


/** {@inheritDoc} */ /** {@inheritDoc} */
@Override public CacheLock lock(K key) throws CacheException { @Override public Lock lock(K key) throws CacheException {
return lockAll(Collections.<K>singleton(key)); return lockAll(Collections.<K>singleton(key));
} }


/** {@inheritDoc} */ /** {@inheritDoc} */
@Override public CacheLock lockAll(final Collection<? extends K> keys) { @Override public Lock lockAll(final Collection<? extends K> keys) {
return new CacheLockImpl<K>(delegate, keys); return new CacheLockImpl<K>(delegate, keys);
} }


/** {@inheritDoc} */ /** {@inheritDoc} */
@Override public boolean isLocked(K key) { @Override public boolean isLocalLocked(K key, boolean byCurrThread) {
GridCacheProjectionImpl<K, V> prev = gate.enter(prj); GridCacheProjectionImpl<K, V> prev = gate.enter(prj);


try { try {
return delegate.isLocked(key); return byCurrThread ? delegate.isLockedByThread(key) : delegate.isLocked(key);
}
finally {
gate.leave(prev);
}
}

/** {@inheritDoc} */
@Override public boolean isLockedByThread(K key) {
GridCacheProjectionImpl<K, V> prev = gate.enter(prj);

try {
return delegate.isLockedByThread(key);
} }
finally { finally {
gate.leave(prev); gate.leave(prev);
Expand Down
Expand Up @@ -3318,25 +3318,25 @@ public void testLockUnlock() throws Exception {


cache.put(key, 1); cache.put(key, 1);


assert !cache.isLocked(key); assert !cache.isLocalLocked(key, false);


cache.lock(key).lock(); cache.lock(key).lock();


lockCnt.await(); lockCnt.await();


assert cache.isLocked(key); assert cache.isLocalLocked(key, false);


cache.lock(key).unlock(); cache.lock(key).unlock();


unlockCnt.await(); unlockCnt.await();


for (int i = 0; i < 100; i++) for (int i = 0; i < 100; i++)
if (cache.isLocked(key)) if (cache.isLocalLocked(key, false))
Thread.sleep(10); Thread.sleep(10);
else else
break; break;


assert !cache.isLocked(key); assert !cache.isLocalLocked(key, false);
} }
} }


Expand All @@ -3348,25 +3348,25 @@ public void testLockAsync() throws Exception {
if (lockingEnabled()) { if (lockingEnabled()) {
IgniteCache<String, Integer> cache = jcache(); IgniteCache<String, Integer> cache = jcache();


CacheLock lock = cache.lock("key"); Lock lock = cache.lock("key");


cache.put("key", 1); cache.put("key", 1);


assert !lock.isLocked(); assert !cache.isLocalLocked("key", false);


lock.lock(); lock.lock();


assert lock.isLocked(); assert cache.isLocalLocked("key", false);


lock.unlock(); lock.unlock();


for (int i = 0; i < 100; i++) for (int i = 0; i < 100; i++)
if (lock.isLocked()) if (cache.isLocalLocked("key", false))
Thread.sleep(10); Thread.sleep(10);
else else
break; break;


assert !cache.isLocked("key"); assert !cache.isLocalLocked("key", false);
} }
} }


Expand Down Expand Up @@ -3407,14 +3407,14 @@ public void testLockWithTimeout() throws Exception {
if (lockingEnabled()) { if (lockingEnabled()) {
jcache().put("key", 1); jcache().put("key", 1);


assert !jcache().isLocked("key"); assert !jcache().isLocalLocked("key", false);


final Lock lock = jcache().lock("key"); final Lock lock = jcache().lock("key");


lock.tryLock(2000, MILLISECONDS); lock.tryLock(2000, MILLISECONDS);


assert jcache().isLocked("key"); assert jcache().isLocalLocked("key", false);
assert jcache().isLockedByThread("key"); assert jcache().isLocalLocked("key", true);


assert !forLocal(dfltIgnite).call(new Callable<Boolean>() { assert !forLocal(dfltIgnite).call(new Callable<Boolean>() {
@Override public Boolean call() throws InterruptedException { @Override public Boolean call() throws InterruptedException {
Expand Down Expand Up @@ -3464,14 +3464,14 @@ public void testLockAsyncWithTimeout() throws Exception {


cache.put("key", 1); cache.put("key", 1);


final CacheLock lock = cache.lock("key"); final Lock lock = cache.lock("key");


assert !cache.isLocked("key"); assert !cache.isLocalLocked("key", false);


lock.tryLock(1000, MILLISECONDS); lock.tryLock(1000, MILLISECONDS);


assert cache.isLocked("key"); assert cache.isLocalLocked("key", false);
assert cache.isLockedByThread("key"); assert cache.isLocalLocked("key", true);


final CountDownLatch latch = new CountDownLatch(1); final CountDownLatch latch = new CountDownLatch(1);


Expand Down Expand Up @@ -3499,21 +3499,21 @@ public void testLockAsyncWithTimeout() throws Exception {
// Let another thread start. // Let another thread start.
latch.await(); latch.await();


assert cache.isLocked("key"); assert cache.isLocalLocked("key", false);
assert cache.isLockedByThread("key"); assert cache.isLocalLocked("key", true);


lock.unlock(); lock.unlock();


assert f.get(); assert f.get();


for (int i = 0; i < 100; i++) for (int i = 0; i < 100; i++)
if (cache.isLocked("key") || cache.isLockedByThread("key")) if (cache.isLocalLocked("key", false) || cache.isLocalLocked("key", true))
Thread.sleep(10); Thread.sleep(10);
else else
break; break;


assert !cache.isLocked("key"); assert !cache.isLocalLocked("key", false);
assert !cache.isLockedByThread("key"); assert !cache.isLocalLocked("key", true);
} }
} }


Expand Down Expand Up @@ -3701,42 +3701,42 @@ public void testLockUnlockAll() throws Exception {
cache.put("key1", 1); cache.put("key1", 1);
cache.put("key2", 2); cache.put("key2", 2);


assert !cache.isLocked("key1"); assert !cache.isLocalLocked("key1", false);
assert !cache.isLocked("key2"); assert !cache.isLocalLocked("key2", false);


cache.lockAll(ImmutableSet.of("key1", "key2")).lock(); cache.lockAll(ImmutableSet.of("key1", "key2")).lock();


assert cache.isLocked("key1"); assert cache.isLocalLocked("key1", false);
assert cache.isLocked("key2"); assert cache.isLocalLocked("key2", false);


cache.lockAll(ImmutableSet.of("key1", "key2")).unlock(); cache.lockAll(ImmutableSet.of("key1", "key2")).unlock();


for (int i = 0; i < 100; i++) for (int i = 0; i < 100; i++)
if (cache.isLocked("key1") || cache.isLocked("key2")) if (cache.isLocalLocked("key1", false) || cache.isLocalLocked("key2", false))
Thread.sleep(10); Thread.sleep(10);
else else
break; break;


assert !cache.isLocked("key1"); assert !cache.isLocalLocked("key1", false);
assert !cache.isLocked("key2"); assert !cache.isLocalLocked("key2", false);


Lock lock = cache.lockAll(ImmutableSet.of("key1", "key2")); Lock lock = cache.lockAll(ImmutableSet.of("key1", "key2"));


lock.lock(); lock.lock();


assert cache.isLocked("key1"); assert cache.isLocalLocked("key1", false);
assert cache.isLocked("key2"); assert cache.isLocalLocked("key2", false);


lock.unlock(); lock.unlock();


for (int i = 0; i < 100; i++) for (int i = 0; i < 100; i++)
if (cache.isLocked("key1") || cache.isLocked("key2")) if (cache.isLocalLocked("key1", false) || cache.isLocalLocked("key2", false))
Thread.sleep(10); Thread.sleep(10);
else else
break; break;


assert !cache.isLocked("key1"); assert !cache.isLocalLocked("key1", false);
assert !cache.isLocked("key2"); assert !cache.isLocalLocked("key2", false);
} }
} }


Expand Down

0 comments on commit b9c6f3b

Please sign in to comment.