Reputation: 1947
I am using the below recipe in our code to ensure singleton JVM application
public void acquireLock(InterProcessSemaphoreMutex lock){
lock.acquire();
}
public void releaseLock(InterProcessSemaphoreMutex lock){
lock.release();
}
I am releasing lock(via above function call) in 2 conditions:
Is it ever possible that call to release() may hang?
Also should I add below predicate in releaseLock()?
if (lock.isAcquiredInThisProcess()) {
lock.release();
}
Upvotes: 2
Views: 381
Reputation: 1
This is an old post but still relevant to my issue. I have two replicas in a k8s cluster. The pods share re-entrant InterProcessMutex on ZK. Now, I want to be able to release the lock held by a thread in pod A from pod B, i.e. from one JVM requested to be release in another JVM. But, an attempt to release in the call-back passed to listener in makeRevocable() will fails since that thread is not the one that holds the lock, and of course the class is not exposing the lock's owner, so unless that metadata is also saved somewhere on ZK to be shared there's no way of knowing it. It seems to me an attempt to release would only work if it's issued somewhere within the same thread. So I find that makeRevocable() doc is rather sparse and in this regard misleading. Also, even InterProcessSemaphoreMutex will only work within the same JVM.
Upvotes: 0
Reputation: 2956
(Note: I'm the main author of Curator)
If you aren't currently connected (i.e. are in SUSPENDED/LOST) then lock.release()
will hang based on your current retry policy. However, it won't hang indefinitely and will return once the retries have been exhausted. More importantly, internally Curator guarantees that the node associated with the lock/semaphore will get deleted so you should always call this method if you intend to release the lock/semaphore. Once the connection is repaired the node will get deleted and other processes waiting to acquire can continue.
Upvotes: 2