public class DefaultConnectionPolicy extends ConnectionPolicy
ConnectionPolicy
by choosing nodes randomly.
Sorts nodes into two lists:
badNodes
. When a connection
attempt to a node is successful, it will be removed from this list and put
into the good nodes list goodNodes
and the node's connection failure
statistics will be reset using PeerNode.resetFailureCount()
.
Whenever a node is being selected for a new connection (or reconnect), it
will select one randomly from the goodNodes
list. If this list is
empty, it will select the least recently failed node from badNodes
.
At most maxRetries
retries are attempted per operation (see
Connection.connect()
,
Connection.doRPC(String, String, com.ericsson.otp.erlang.OtpErlangList)
and
Connection.doRPC(String, String, com.ericsson.otp.erlang.OtpErlangObject[])
) - the number of the current attempt will not be cached in this class. Set
the maximal number of retries using setMaxRetries(int)
.
Attention: All member's functions are synchronised as there can be a single
connection policy object used by many threads and the access to the
goodNodes
and badNodes
members are logically linked
together and operations on both need to be performed atomically. Additionally
access to PeerNode
objects are synchronised on themselves. It is
therefore important not to use any of this classes methods in blocks that
synchronise on any node object. Otherwise deadlocks might occur!!Constructor and Description |
---|
DefaultConnectionPolicy(List<PeerNode> availableRemoteNodes)
Creates a new connection policy with the given remote nodes.
|
DefaultConnectionPolicy(PeerNode remoteNode)
Creates a new connection policy working with the given remote node.
|
Modifier and Type | Method and Description |
---|---|
void |
availableNodeAdded(PeerNode newNode)
|
void |
availableNodeRemoved(PeerNode removedNode)
|
void |
availableNodesReset()
|
List<PeerNode> |
getBadNodes()
Gets a copy of the list of good nodes (contains references to the
PeerNode objects). |
List<PeerNode> |
getGoodNodes()
Gets a copy of the list of good nodes (contains references to the
PeerNode objects). |
int |
getMaxRetries()
Sets the maximal number of automatic connection retries.
|
void |
nodeConnectSuccess(PeerNode node)
Sets the node's last successful connect time stamp, resets its failure
statistics and moves it to the
goodNodes list. |
void |
nodeFailed(PeerNode node)
Sets the given node's last failed connect time stamp and moves it to the
badNodes list. |
void |
nodeFailReset(PeerNode node)
Acts upon a failure reset of the given node.
|
<E extends Exception> |
selectNode(int retry,
PeerNode failedNode,
E e)
Selects the node to (re-)connect with until the maximal number of
maxRetries has been reached. |
void |
setMaxRetries(int maxRetries)
Gets the maximal number of automatic connection retries.
|
selectNode
public DefaultConnectionPolicy(PeerNode remoteNode)
remoteNode
- the (only) available remote nodepublic DefaultConnectionPolicy(List<PeerNode> availableRemoteNodes)
availableRemoteNodes
.
Any time this list is changed, the according methods in this class should
be called, i.e. availableNodeAdded(PeerNode)
,
availableNodeRemoved(PeerNode)
, availableNodesReset()
to update the good and bad nodes lists.availableRemoteNodes
- the remote nodes available for connectionspublic void availableNodeAdded(PeerNode newNode)
goodNodes
list if it has no failures,
otherwise it will be added to badNodes
.
Attention: This method also synchronises on the node.availableNodeAdded
in class ConnectionPolicy
newNode
- the new nodepublic void availableNodeRemoved(PeerNode removedNode)
availableNodeRemoved
in class ConnectionPolicy
removedNode
- the removed nodepublic void availableNodesReset()
availableNodesReset
in class ConnectionPolicy
public void nodeFailed(PeerNode node)
badNodes
list.
Attention: This method also synchronises on the node.nodeFailed
in class ConnectionPolicy
node
- the failed nodepublic void nodeFailReset(PeerNode node)
nodeFailReset
in class ConnectionPolicy
node
- the nodepublic void nodeConnectSuccess(PeerNode node)
goodNodes
list.
Attention: This method also synchronises on the node.nodeConnectSuccess
in class ConnectionPolicy
node
- the nodepublic <E extends Exception> PeerNode selectNode(int retry, PeerNode failedNode, E e) throws E extends Exception
maxRetries
has been reached.
Throws an exception if retry > maxRetries
and thus stops further
node connection attempts. Otherwise chooses a random good node or (if
there are no good nodes) the least recently failed bad node.selectNode
in class ConnectionPolicy
E
- the type of the exception that came from the failed connection
and may be re-thrownretry
- the n'th retry (initial connect = 0, 1st reconnect = 1,...)failedNode
- the node from the previous connection attempt or null
e
- the exception that came back from the previous connection
attempt or null
E
- if thrown, automatic re-connection attempts will stopUnsupportedOperationException
- is thrown if the operation can not be performed, e.g. the
list is emptyE extends Exception
Connection.connect()
,
Connection.doRPC(String, String,
com.ericsson.otp.erlang.OtpErlangList)
,
Connection.doRPC(String, String,
com.ericsson.otp.erlang.OtpErlangObject[])
public int getMaxRetries()
public void setMaxRetries(int maxRetries)
maxRetries
- the maxRetries to set (>= 0)public List<PeerNode> getGoodNodes()
PeerNode
objects).