Class CircuitBreakingConnectionStateListener
- All Implemented Interfaces:
ConnectionStateListener
A proxy for connection state listeners that adds circuit breaking behavior. During network outages ZooKeeper can become very noisy sending connection/disconnection events in rapid succession. Curator recipes respond to these messages by resetting state, etc. E.g. LeaderLatch must delete its lock node and try to recreate it in order to try to re-obtain leadership, etc.
This noisy herding can be avoided by using the circuit breaking listener. When it
receives ConnectionState.SUSPENDED
, the circuit
becomes "open" (based on the provided RetryPolicy
) and will ignore
future connection state changes until RetryPolicy timeout has elapsed. Note: however, if the connection
goes from ConnectionState.SUSPENDED
to
ConnectionState.LOST
the first LOST state is sent.
When the circuit is closed, all connection state changes are forwarded to the managed listener. When the first disconnected state is received, the circuit becomes open. The state change that caused the circuit to open is sent to the managed listener and the RetryPolicy will be used to get a delay amount. While the delay is active, the circuit breaker will store state changes but will not forward them to the managed listener (except, however, the first time the state changes from SUSPENDED to LOST). When the delay elapses, if the connection has been restored, the circuit closes and forwards the new state to the managed listener. If the connection has not been restored, the RetryPolicy is checked again. If the RetryPolicy indicates another retry is allowed the process repeats. If, however, the RetryPolicy indicates that retries are exhausted then the circuit closes - if the current state is different than the state that caused the circuit to open it is forwarded to the managed listener.
NOTE: You should not use this listener directly. Instead, set ConnectionStateListenerManagerFactory.circuitBreaking(org.apache.curator.RetryPolicy)
in the CuratorFrameworkFactory.Builder.connectionStateListenerManagerFactory(ConnectionStateListenerManagerFactory)
.
E.g.
ConnectionStateListenerManagerFactory factory = ConnectionStateListenerManagerFactory.circuitBreaking(...retry policy for circuit breaking...);
CuratorFramework client = CuratorFrameworkFactory.builder()
.connectionStateListenerManagerFactory(factory)
... etc ...
.build();
// all connection state listeners set for "client" will get circuit breaking behavior
-
Constructor Summary
ConstructorDescriptionCircuitBreakingConnectionStateListener
(CuratorFramework client, ConnectionStateListener listener, RetryPolicy retryPolicy) CircuitBreakingConnectionStateListener
(CuratorFramework client, ConnectionStateListener listener, RetryPolicy retryPolicy, ScheduledExecutorService service) -
Method Summary
Modifier and TypeMethodDescriptionboolean
isOpen()
Returns true if the circuit is openvoid
stateChanged
(CuratorFramework client, ConnectionState newState) Called when there is a state change in the connectionMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface org.apache.curator.framework.state.ConnectionStateListener
doNotProxy
-
Constructor Details
-
CircuitBreakingConnectionStateListener
public CircuitBreakingConnectionStateListener(CuratorFramework client, ConnectionStateListener listener, RetryPolicy retryPolicy) - Parameters:
client
- Curator instancelistener
- listener to manageretryPolicy
- breaking policy to use
-
CircuitBreakingConnectionStateListener
public CircuitBreakingConnectionStateListener(CuratorFramework client, ConnectionStateListener listener, RetryPolicy retryPolicy, ScheduledExecutorService service) - Parameters:
client
- Curator instancelistener
- listener to manageretryPolicy
- breaking policy to useservice
- scheduler to use
-
-
Method Details
-
stateChanged
Description copied from interface:ConnectionStateListener
Called when there is a state change in the connection- Specified by:
stateChanged
in interfaceConnectionStateListener
- Parameters:
client
- the clientnewState
- the new state
-
isOpen
public boolean isOpen()Returns true if the circuit is open- Returns:
- true/false
-