Class SessionFailRetryLoop
- java.lang.Object
-
- org.apache.curator.SessionFailRetryLoop
-
- All Implemented Interfaces:
java.io.Closeable
,java.lang.AutoCloseable
public class SessionFailRetryLoop extends java.lang.Object implements java.io.Closeable
See
RetryLoop
for the main details on retry loops. All Curator/ZooKeeper operations should be done in a retry loop.The standard retry loop treats session failure as a type of connection failure. i.e. the fact that it is a session failure isn't considered. This can be problematic if you are performing a series of operations that rely on ephemeral nodes. If the session fails after the ephemeral node has been created, future Curator/ZooKeeper operations may succeed even though the ephemeral node has been removed by ZooKeeper.
Here's an example:
- You create an ephemeral/sequential node as a kind of lock/marker
- You perform some other operations
- The session fails for some reason
- You attempt to create a node assuming that the lock/marker still exists
- Curator will notice the session failure and try to reconnect
- In most cases, the reconnect will succeed and, thus, the node creation will succeed even though the ephemeral node will have been deleted by ZooKeeper.
The SessionFailRetryLoop prevents this type of scenario. When a session failure is detected, the thread is marked as failed which will cause all future Curator operations to fail. The SessionFailRetryLoop will then either retry the entire set of operations or fail (depending on
Canonical usage:SessionFailRetryLoop.Mode
)
SessionFailRetryLoop retryLoop = client.newSessionFailRetryLoop(mode); retryLoop.start(); try { while ( retryLoop.shouldContinue() ) { try { // do work } catch ( Exception e ) { retryLoop.takeException(e); } } } finally { retryLoop.close(); }
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
SessionFailRetryLoop.Mode
static class
SessionFailRetryLoop.SessionFailedException
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static <T> T
callWithRetry(CuratorZookeeperClient client, SessionFailRetryLoop.Mode mode, java.util.concurrent.Callable<T> proc)
Convenience utility: creates a "session fail" retry loop calling the given procvoid
close()
Must be called in a finally handler when done with the loopboolean
shouldContinue()
If true is returned, make an attempt at the set of operationsvoid
start()
SessionFailRetryLoop must be startedvoid
takeException(java.lang.Exception exception)
Pass any caught exceptions here
-
-
-
Method Detail
-
callWithRetry
public static <T> T callWithRetry(CuratorZookeeperClient client, SessionFailRetryLoop.Mode mode, java.util.concurrent.Callable<T> proc) throws java.lang.Exception
Convenience utility: creates a "session fail" retry loop calling the given proc- Type Parameters:
T
- return type- Parameters:
client
- Zookeepermode
- how to handle session failuresproc
- procedure to call with retry- Returns:
- procedure result
- Throws:
java.lang.Exception
- any non-retriable errors
-
start
public void start()
SessionFailRetryLoop must be started
-
shouldContinue
public boolean shouldContinue()
If true is returned, make an attempt at the set of operations- Returns:
- true/false
-
close
public void close()
Must be called in a finally handler when done with the loop- Specified by:
close
in interfacejava.lang.AutoCloseable
- Specified by:
close
in interfacejava.io.Closeable
-
takeException
public void takeException(java.lang.Exception exception) throws java.lang.Exception
Pass any caught exceptions here- Parameters:
exception
- the exception- Throws:
java.lang.Exception
- if not retry-able or the retry policy returned negative
-
-