AcquireResult acquire ( LockInvocationKey key , boolean wait ) { LockEndpoint endpoint = key . endpoint ( ) ; UUID invocationUid = key . invocationUid ( ) ; RaftLockOwnershipState memorized = ownerInvocationRefUids . get ( Tuple2 . of ( endpoint , invocationUid ) ) ; if ( memorized != null ) { AcquireStatus status = memorized . isLocked ( ) ? SUCCESSFUL : FAILED ; return new AcquireResult ( status , memorized . getFence ( ) , Collections . < LockInvocationKey > emptyList ( ) ) ; } if ( owner == null ) { owner = key ; } if ( endpoint . equals ( owner . endpoint ( ) ) ) { if ( lockCount == lockCountLimit ) { ownerInvocationRefUids . put ( Tuple2 . of ( endpoint , invocationUid ) , NOT_LOCKED ) ; return AcquireResult . failed ( Collections . < LockInvocationKey > emptyList ( ) ) ; } lockCount ++ ; ownerInvocationRefUids . put ( Tuple2 . of ( endpoint , invocationUid ) , lockOwnershipState ( ) ) ; return AcquireResult . acquired ( owner . commitIndex ( ) ) ; } // we must cancel waits keys of previous invocation of the endpoint // before adding a new wait key or even if we will not wait Collection < LockInvocationKey > cancelledWaitKeys = cancelWaitKeys ( endpoint , invocationUid ) ; if ( wait ) { addWaitKey ( endpoint , key ) ; return AcquireResult . waitKeyAdded ( cancelledWaitKeys ) ; } return AcquireResult . failed ( cancelledWaitKeys ) ; }
Assigns the lock to the endpoint if the lock is not held . Lock count is incremented if the endpoint already holds the lock . If some other endpoint holds the lock and the second argument is true a wait key is created and added to the wait queue . Lock count is not incremented if the lock request is a retry of the lock holder . If the lock request is a retry of a lock endpoint that resides in the wait queue with the same invocation uid a retry wait key wait key is attached to the original wait key . If the lock request is a new request of a lock endpoint that resides in the wait queue with a different invocation uid the existing wait key is cancelled because it means the caller has stopped waiting for response of the previous invocation . If the invocation uid is same with one of the previous invocations of the current lock owner memorized result of the previous invocation is returned .