# IGNITE-59 Support lock, lockAll.
Project: http://git-wip-us.apache.org/repos/asf/incubator-ignite/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-ignite/commit/9d4476d9 Tree: http://git-wip-us.apache.org/repos/asf/incubator-ignite/tree/9d4476d9 Diff: http://git-wip-us.apache.org/repos/asf/incubator-ignite/diff/9d4476d9 Branch: refs/heads/ignite-54 Commit: 9d4476d988715b237665774f28ec799dc127c07f Parents: 3975c10 Author: sevdokimov <sevdoki...@gridgain.com> Authored: Mon Jan 19 13:35:02 2015 +0300 Committer: sevdokimov <sevdoki...@gridgain.com> Committed: Mon Jan 19 13:35:02 2015 +0300 ---------------------------------------------------------------------- .../main/java/org/apache/ignite/CacheLock.java | 87 ++++++++++++++++++++ .../java/org/apache/ignite/IgniteCache.java | 4 +- .../processors/cache/IgniteCacheProxy.java | 32 ++++++- 3 files changed, 118 insertions(+), 5 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/9d4476d9/modules/core/src/main/java/org/apache/ignite/CacheLock.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/CacheLock.java b/modules/core/src/main/java/org/apache/ignite/CacheLock.java new file mode 100644 index 0000000..50fc5d2 --- /dev/null +++ b/modules/core/src/main/java/org/apache/ignite/CacheLock.java @@ -0,0 +1,87 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.ignite; + +import org.apache.ignite.lang.*; +import org.apache.ignite.transactions.*; +import org.gridgain.grid.cache.*; + +import java.util.concurrent.*; +import java.util.concurrent.locks.*; + +/** + * Lock associated with some cache keys. + */ +public interface CacheLock extends Lock { + /** + * Checks if any node owns a lock for the keys associated this lock. + * <p> + * This is a local in-VM operation and does not involve any network trips + * or access to persistent storage in any way. + * + * @return {@code True} if lock is owned by some node. + * @see {@link IgniteCache#isLocked(Object)} + */ + public boolean isLocked(); + + /** + * Checks if current thread owns a lock the keys associated this lock. + * <p> + * This is a local in-VM operation and does not involve any network trips + * or access to persistent storage in any way. + * + * @return {@code True} if key is locked by current thread. + * @see {@link IgniteCache#isLockedByThread(Object)} + */ + public boolean isLockedByThread(); + + /** + * Asynchronously acquires lock on a cached object with given keys associated this lock. + * <h2 class="header">Transactions</h2> + * Locks are not transactional and should not be used from within transactions. If you do + * need explicit locking within transaction, then you should use + * {@link IgniteTxConcurrency#PESSIMISTIC} concurrency control for transaction + * which will acquire explicit locks for relevant cache operations. + * <h2 class="header">Cache Flags</h2> + * This method is not available if any of the following flags are set on projection: + * {@link GridCacheFlag#LOCAL}, {@link GridCacheFlag#READ}. + * + * @return Future for the lock operation. The future will return {@code true}. + */ + public IgniteFuture<Boolean> lockAsync(); + + /** + * Asynchronously acquires lock on a cached object with given keys associated this lock. + * <h2 class="header">Transactions</h2> + * Locks are not transactional and should not be used from within transactions. If you do + * need explicit locking within transaction, then you should use + * {@link IgniteTxConcurrency#PESSIMISTIC} concurrency control for transaction + * which will acquire explicit locks for relevant cache operations. + * <h2 class="header">Cache Flags</h2> + * This method is not available if any of the following flags are set on projection: + * {@link GridCacheFlag#LOCAL}, {@link GridCacheFlag#READ}. + * + * @param timeout Timeout in milliseconds to wait for lock to be acquired + * ({@code '0'} for no expiration, {@code -1} for immediate failure if + * lock cannot be acquired immediately). + * @param unit the time unit of the {@code timeout} argument. + * @return Future for the lock operation. The future will return {@code true} whenever locks are acquired before + * timeout is expired, {@code false} otherwise. + */ + public IgniteFuture<Boolean> lockAsync(long timeout, TimeUnit unit); +} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/9d4476d9/modules/core/src/main/java/org/apache/ignite/IgniteCache.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/IgniteCache.java b/modules/core/src/main/java/org/apache/ignite/IgniteCache.java index f4aa3d5..d900c12 100644 --- a/modules/core/src/main/java/org/apache/ignite/IgniteCache.java +++ b/modules/core/src/main/java/org/apache/ignite/IgniteCache.java @@ -180,9 +180,9 @@ public interface IgniteCache<K, V> extends javax.cache.Cache<K, V>, IgniteAsyncS */ public void removeAll(IgnitePredicate<Entry<K, V>> filter) throws CacheException; - public Lock lock(K key); + public CacheLock lock(K key); - public Lock lockAll(Set<? extends K> keys); + public CacheLock lockAll(Collection<? extends K> keys); /** * Checks if any node owns a lock for this key. http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/9d4476d9/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 0c98124..bd5d6ee 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 @@ -258,13 +258,39 @@ public class IgniteCacheProxy<K, V> extends IgniteAsyncSupportAdapter implements } /** {@inheritDoc} */ - @Override public Lock lock(K key) throws CacheException { + @Override public CacheLock lock(K key) throws CacheException { return lockAll(Collections.<K>singleton(key)); } /** {@inheritDoc} */ - @Override public Lock lockAll(final Set<? extends K> keys) { - return new Lock() { + @Override public CacheLock lockAll(final Collection<? extends K> keys) { + return new CacheLock() { + @Override public boolean isLocked() { + for (K key : keys) { + if (!delegate.isLocked(key)) + return false; + } + + return true; + } + + @Override public boolean isLockedByThread() { + for (K key : keys) { + if (!delegate.isLockedByThread(key)) + return false; + } + + return true; + } + + @Override public IgniteFuture<Boolean> lockAsync() { + return delegate.lockAllAsync(keys, 0); + } + + @Override public IgniteFuture<Boolean> lockAsync(long timeout, TimeUnit unit) { + return delegate.lockAllAsync(keys, unit.toMillis(timeout)); + } + @Override public void lock() { try { delegate.lockAll(keys, 0);