You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@curator.apache.org by "kulkarni.swarnim@gmail.com" <ku...@gmail.com> on 2013/07/18 03:20:27 UTC
Handling failure to acquire lock
Hello,
I am trying to use the InterProcessMutex to lock a shared resource on HDFS.
My question is how are we supposed to handle cases where a client fails to
acquire the mutex lock?
I am using the method here[1] to acquire the lock. The javadoc states that
an "Exception" is thrown if there were ZK errors or interruptions while
attempting to acquire the lock. I am assuming this includes some
recoverable exceptions as well.
Is it wise to wrap this up in a RetryLoop? Something like:
RetryLoop retryLoop = client.newRetryLoop();
while ( retryLoop.shouldContinue() )
{
try
{
lock.acquireLock(); // attempt to acquire the lock
}
catch ( Exception e )
{
retryLoop.takeException(e);
}
}
Thanks,
[1]
http://curator.incubator.apache.org/apidocs/org/apache/curator/framework/recipes/locks/InterProcessMutex.html#acquire(long,
java.util.concurrent.TimeUnit)
--
Swarnim
Re: Handling failure to acquire lock
Posted by Jordan Zimmerman <jo...@jordanzimmerman.com>.
Retry loops are handled automatically by Curator internally. If you don't pass a timeout to lock.acquireLock(), it will block indefinitely. If you want to continue lock attempts even in the face of other exceptions, just make an infinite loop:
for(;;)
{
try
{
lock.acquireLock(); // attempt to acquire the lock
}
catch ( Exception e )
{
log.error(e, …);
}
}
This wouldn't be a very robust way to go, but it works.
-JZ
On Jul 17, 2013, at 6:20 PM, "kulkarni.swarnim@gmail.com" <ku...@gmail.com> wrote:
> Hello,
>
> I am trying to use the InterProcessMutex to lock a shared resource on HDFS. My question is how are we supposed to handle cases where a client fails to acquire the mutex lock?
>
> I am using the method here[1] to acquire the lock. The javadoc states that an "Exception" is thrown if there were ZK errors or interruptions while attempting to acquire the lock. I am assuming this includes some recoverable exceptions as well.
>
> Is it wise to wrap this up in a RetryLoop? Something like:
>
> RetryLoop retryLoop = client.newRetryLoop();
> while ( retryLoop.shouldContinue() )
> {
> try
> {
> lock.acquireLock(); // attempt to acquire the lock
> }
> catch ( Exception e )
> {
> retryLoop.takeException(e);
> }
> }
>
> Thanks,
>
> [1] http://curator.incubator.apache.org/apidocs/org/apache/curator/framework/recipes/locks/InterProcessMutex.html#acquire(long, java.util.concurrent.TimeUnit)
>
> --
> Swarnim