Class GridCacheLockImpl
- java.lang.Object
-
- org.apache.ignite.internal.processors.datastructures.AtomicDataStructureProxy<GridCacheLockState>
-
- org.apache.ignite.internal.processors.datastructures.GridCacheLockImpl
-
- All Implemented Interfaces:
Closeable,Externalizable,Serializable,AutoCloseable,Lock,IgniteLock,IgniteChangeGlobalStateSupport,GridCacheLockEx,GridCacheRemovable
public final class GridCacheLockImpl extends AtomicDataStructureProxy<GridCacheLockState> implements GridCacheLockEx, IgniteChangeGlobalStateSupport, Externalizable
Cache reentrant lock implementation based on AbstractQueuedSynchronizer.- See Also:
- Serialized Form
-
-
Constructor Summary
Constructors Constructor Description GridCacheLockImpl()Empty constructor required byExternalizable.GridCacheLockImpl(String name, GridCacheInternalKey key, IgniteInternalCache<GridCacheInternalKey,GridCacheLockState> lockView)Constructor.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()Removes reentrant lock.intgetHoldCount()Queries the number of holds on this lock by the current thread.IgniteConditiongetOrCreateCondition(String name)Returns aConditioninstance for use with thisIgniteLockinstance.intgetWaitQueueLength(IgniteCondition condition)Returns an estimate of the number of threads on this node that are waiting on the given condition associated with this lock.booleanhasQueuedThread(Thread thread)Queries whether the given thread is waiting to acquire this lock.booleanhasQueuedThreads()Queries whether any threads on this node are waiting to acquire this lock.booleanhasWaiters(IgniteCondition condition)Queries whether any threads on this node are waiting on the given condition associated with this lock.booleanisBroken()Returns true if any node that owned the locked failed before releasing the lock.booleanisFailoverSafe()Returnstrueif this lock is safe to use after node failure.booleanisFair()Returnstrueif this lock is fair.booleanisHeldByCurrentThread()Queries if this lock is held by the current thread.booleanisLocked()Queries if this lock is held by any thread on any node.voidlock()Acquires the distributed reentrant lock.voidlockInterruptibly()Acquires the lock unless the current thread is interrupted.voidneedCheckNotRemoved()@NotNull ConditionnewCondition()This method is not supported in IgniteLock, Any invocation of this method will result in UnsupportedOperationException.voidonNodeRemoved(UUID nodeId)Callback to notify semaphore on topology changes.voidonReconnected(UUID nodeId)Callback to notify local reentrant lock instance on node reconnected.voidonStop()Callback to notify local reentrant lock instance on node stop.voidonUpdate(GridCacheLockState val)Callback to notify reentrant lock on changes.voidreadExternal(ObjectInput in)StringtoString()booleantryLock()Acquires the lock only if it is free at the time of invocation.booleantryLock(long timeout, TimeUnit unit)Acquires the lock if it is not held by another thread within the given waiting time and the current thread has not been interrupted.voidunlock()Releases the lock.voidwriteExternal(ObjectOutput out)-
Methods inherited from class org.apache.ignite.internal.processors.datastructures.AtomicDataStructureProxy
checkRemoved, checkRemovedAfterFail, invalidateLocalState, key, name, onActivate, onDeActivate, onRemoved, removed, restart, suspend
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface org.apache.ignite.internal.processors.datastructures.GridCacheLockEx
key
-
Methods inherited from interface org.apache.ignite.internal.processors.datastructures.GridCacheRemovable
onRemoved, restart, suspend
-
Methods inherited from interface org.apache.ignite.internal.processors.cluster.IgniteChangeGlobalStateSupport
onActivate, onDeActivate
-
Methods inherited from interface org.apache.ignite.IgniteLock
name, removed
-
-
-
-
Constructor Detail
-
GridCacheLockImpl
public GridCacheLockImpl()
Empty constructor required byExternalizable.
-
GridCacheLockImpl
public GridCacheLockImpl(String name, GridCacheInternalKey key, IgniteInternalCache<GridCacheInternalKey,GridCacheLockState> lockView)
Constructor.- Parameters:
name- Reentrant lock name.key- Reentrant lock key.lockView- Reentrant lock projection.
-
-
Method Detail
-
onUpdate
public void onUpdate(GridCacheLockState val)
Callback to notify reentrant lock on changes.- Specified by:
onUpdatein interfaceGridCacheLockEx- Parameters:
val- New reentrant lock state.
-
onNodeRemoved
public void onNodeRemoved(UUID nodeId)
Callback to notify semaphore on topology changes.- Specified by:
onNodeRemovedin interfaceGridCacheLockEx- Parameters:
nodeId- Id of the node that left the grid.
-
onReconnected
public void onReconnected(UUID nodeId)
Callback to notify local reentrant lock instance on node reconnected.- Specified by:
onReconnectedin interfaceGridCacheLockEx- Parameters:
nodeId- Node ID.
-
onStop
public void onStop()
Callback to notify local reentrant lock instance on node stop.- Specified by:
onStopin interfaceGridCacheLockEx
-
lock
public void lock()
Acquires the distributed reentrant lock.Acquires the lock if it is not held by another thread and returns immediately, setting the lock hold count to one.
If the current thread already holds this lock then the hold count is incremented by one and the method returns immediately.
If the lock is held by another thread then the current thread becomes disabled for thread scheduling purposes and lies dormant until one of four things happens:
- The lock is acquired by the current thread; or
- Lock is broken (any node failed while owning this lock), and lock is created in non-failoverSafe mode.
- Local node is stopped.
- Specified by:
lockin interfaceIgniteLock- Specified by:
lockin interfaceLock
-
lockInterruptibly
public void lockInterruptibly() throws IgniteInterruptedExceptionAcquires the lock unless the current thread is interrupted.Acquires the lock if it is not held by another thread and returns immediately, setting the lock hold count to one.
If the current thread already holds this lock then the hold count is incremented by one and the method returns immediately.
If the lock is held by another thread then the current thread becomes disabled for thread scheduling purposes and lies dormant until one of four things happens:
- The lock is acquired by the current thread; or
- Some other thread interrupts the current thread.
- Lock is broken (any node failed while owning this lock), and lock is created in non-failoverSafe mode.
- Local node is stopped.
If the lock is acquired by the current thread then the lock hold count is set to one.
If the current thread:
- has its interrupted status set on entry to this method; or
- is interrupted while acquiring
the lock; or
then
IgniteInterruptedExceptionis thrown and the current thread's interrupted status is cleared.
IgniteExceptionis thrown in case:- the lock is broken before or during the attempt to acquire this lock; or
- local node is stopped,
In this implementation, as this method is an explicit interruption point, preference is given to responding to the interrupt over normal or reentrant acquisition of the lock.
- Specified by:
lockInterruptiblyin interfaceIgniteLock- Specified by:
lockInterruptiblyin interfaceLock- Throws:
IgniteInterruptedException- if the current thread is interrupted
-
tryLock
public boolean tryLock()
Acquires the lock only if it is free at the time of invocation.Acquires the lock if it is available and returns immediately with the value
true. If the lock is not available then this method will return immediately with the valuefalse.A typical usage idiom for this method would be:
This usage ensures that the lock is unlocked if it was acquired, and doesn't try to unlock if the lock was not acquired. If node is stopped, or any node failed while owning the lock in non-failoverSafe mode, thenLock lock = ...; if (lock.tryLock()) { try { // manipulate protected state } finally { lock.unlock(); } } else { // perform alternative actions }IgniteExceptionis thrown.- Specified by:
tryLockin interfaceIgniteLock- Specified by:
tryLockin interfaceLock- Returns:
trueif the lock was acquired andfalseotherwise
-
tryLock
public boolean tryLock(long timeout, TimeUnit unit) throws IgniteInterruptedExceptionAcquires the lock if it is not held by another thread within the given waiting time and the current thread has not been interrupted.Acquires the lock if it is not held by another thread and returns immediately with the value
true, setting the lock hold count to one.If the current thread already holds this lock then the hold count is incremented by one and the method returns
true.If the lock is held by another thread then the current thread becomes disabled for thread scheduling purposes and lies dormant until one of five things happens:
- The lock is acquired by the current thread; or
- Some other thread interrupts the current thread; or
- Lock is broken (any node failed while owning this lock), and lock is created in non-failoverSafe mode.
- Local node is stopped.
- The specified waiting time elapses
If the lock is acquired then the value
trueis returned and the lock hold count is set to one.If the current thread:
- has its interrupted status set on entry to this method; or
- is interrupted while acquiring the lock; or
IgniteInterruptedExceptionis thrown and the current thread's interrupted status is cleared.IgniteExceptionis thrown in case:- the lock is broken before or during the attempt to acquire this lock; or
- local node is stopped,
If the specified waiting time elapses then the value
falseis returned. If the time is less than or equal to zero, the method will not wait at all.In this implementation, as this method is an explicit interruption point, preference is given to responding to the interrupt over normal or reentrant acquisition of the lock, and over reporting the elapse of the waiting time.
- Specified by:
tryLockin interfaceIgniteLock- Specified by:
tryLockin interfaceLock- Parameters:
timeout- the time to wait for the lockunit- the time unit of the timeout argument- Returns:
trueif the lock was free and was acquired by the current thread, or the lock was already held by the current thread; andfalseif the waiting time elapsed before the lock could be acquired- Throws:
IgniteInterruptedException- if the current thread is interrupted
-
unlock
public void unlock()
Releases the lock. If lock is not owned by current thread, then anIllegalMonitorStateExceptionis thrown. If lock is already broken prior to invocation of this method, and lock is created in non-failover safe mode, thenIgniteExceptionis thrown.- Specified by:
unlockin interfaceIgniteLock- Specified by:
unlockin interfaceLock
-
newCondition
@NotNull public @NotNull Condition newCondition()
Description copied from interface:IgniteLockThis method is not supported in IgniteLock, Any invocation of this method will result in UnsupportedOperationException. Correct way to obtain Condition object is through method IgniteLock.getOrCreateCondition(String)- Specified by:
newConditionin interfaceIgniteLock- Specified by:
newConditionin interfaceLock
-
getOrCreateCondition
public IgniteCondition getOrCreateCondition(String name)
Returns aConditioninstance for use with thisIgniteLockinstance.- If this lock is not held when any of the
Conditionwaiting or signalling methods are called, then anIllegalMonitorStateExceptionis thrown. - When the condition waiting methods are called the lock is released and, before they return, the lock is reacquired and the lock hold count restored to what it was when the method was called.
- If a thread is interrupted
while waiting then the wait will terminate, an
IgniteInterruptedExceptionwill be thrown, and the thread's interrupted status will be cleared. - Waiting threads are signalled in FIFO order.
- Specified by:
getOrCreateConditionin interfaceIgniteLock- Parameters:
name- Name of the distributed condition object- Returns:
- the Condition object
- If this lock is not held when any of the
-
getHoldCount
public int getHoldCount()
Queries the number of holds on this lock by the current thread.- Specified by:
getHoldCountin interfaceIgniteLock- Returns:
- the number of holds on this lock by the current thread, or zero if this lock is not held by the current thread
-
isHeldByCurrentThread
public boolean isHeldByCurrentThread()
Queries if this lock is held by the current thread.- Specified by:
isHeldByCurrentThreadin interfaceIgniteLock- Returns:
trueif current thread holds this lock andfalseotherwise
-
isLocked
public boolean isLocked()
Queries if this lock is held by any thread on any node. This method is designed for use in monitoring of the system state, not for synchronization control.- Specified by:
isLockedin interfaceIgniteLock- Returns:
trueif any thread on this or any other node holds this lock andfalseotherwise
-
hasQueuedThreads
public boolean hasQueuedThreads()
Queries whether any threads on this node are waiting to acquire this lock. Note that because cancellations may occur at any time, atruereturn does not guarantee that any other thread will ever acquire this lock. This method is designed primarily for use in monitoring of the system state.- Specified by:
hasQueuedThreadsin interfaceIgniteLock- Returns:
trueif there may be other threads on this node waiting to acquire the lock
-
hasQueuedThread
public boolean hasQueuedThread(Thread thread)
Queries whether the given thread is waiting to acquire this lock. Note that because cancellations may occur at any time, atruereturn does not guarantee that this thread will ever acquire this lock. This method is designed primarily for use in monitoring of the system state.- Specified by:
hasQueuedThreadin interfaceIgniteLock- Parameters:
thread- the thread- Returns:
trueif the given thread is queued waiting for this lock
-
hasWaiters
public boolean hasWaiters(IgniteCondition condition)
Queries whether any threads on this node are waiting on the given condition associated with this lock. Note that because timeouts and interrupts may occur at any time, atruereturn does not guarantee that a futuresignalwill awaken any threads. This method is designed primarily for use in monitoring of the system state.- Specified by:
hasWaitersin interfaceIgniteLock- Parameters:
condition- the condition- Returns:
trueif there are any waiting threads on this node
-
getWaitQueueLength
public int getWaitQueueLength(IgniteCondition condition)
Returns an estimate of the number of threads on this node that are waiting on the given condition associated with this lock. Note that because timeouts and interrupts may occur at any time, the estimate serves only as an upper bound on the actual number of waiters. This method is designed for use in monitoring of the system state, not for synchronization control.- Specified by:
getWaitQueueLengthin interfaceIgniteLock- Parameters:
condition- the condition- Returns:
- the estimated number of waiting threads on this node
-
isFailoverSafe
public boolean isFailoverSafe()
Description copied from interface:IgniteLockReturnstrueif this lock is safe to use after node failure. If not, IgniteInterruptedException is thrown on every other node after node failure.- Specified by:
isFailoverSafein interfaceIgniteLock- Returns:
trueif this reentrant lock has failoverSafe set true
-
isFair
public boolean isFair()
Description copied from interface:IgniteLockReturnstrueif this lock is fair. Fairness flag can only be set on lock creation.- Specified by:
isFairin interfaceIgniteLock- Returns:
trueif this reentrant lock has fairness flag set true.
-
isBroken
public boolean isBroken()
Returns true if any node that owned the locked failed before releasing the lock.- Specified by:
isBrokenin interfaceIgniteLock- Returns:
- true if any node failed while owning the lock since the lock on this node was initialized.
-
needCheckNotRemoved
public void needCheckNotRemoved()
- Specified by:
needCheckNotRemovedin interfaceGridCacheRemovable- Overrides:
needCheckNotRemovedin classAtomicDataStructureProxy<GridCacheLockState>
-
writeExternal
public void writeExternal(ObjectOutput out) throws IOException
- Specified by:
writeExternalin interfaceExternalizable- Throws:
IOException
-
readExternal
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException
- Specified by:
readExternalin interfaceExternalizable- Throws:
IOExceptionClassNotFoundException
-
close
public void close()
Removes reentrant lock.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Specified by:
closein interfaceIgniteLock
-
-