public class RoundRobinPolicy extends Object implements LoadBalancingPolicy
This policy queries nodes in a round-robin fashion. For a given query, if an host fail, the next one (following the round-robin order) is tried, until all hosts have been tried.
This policy is not datacenter aware and will include every known
Cassandra host in its round robin algorithm. If you use multiple
datacenter this will be inefficient and you will want to use the
DCAwareRoundRobinPolicy
load balancing policy instead.
Constructor and Description |
---|
RoundRobinPolicy()
Creates a load balancing policy that picks host to query in a round robin
fashion (on all the hosts of the Cassandra cluster).
|
Modifier and Type | Method and Description |
---|---|
HostDistance |
distance(Host host)
Return the HostDistance for the provided host.
|
void |
init(Cluster cluster,
Collection<Host> hosts)
Initialize this load balancing policy.
|
Iterator<Host> |
newQueryPlan(String loggedKeyspace,
Statement statement)
Returns the hosts to use for a new query.
|
void |
onAdd(Host host)
Called when a new node is added to the cluster.
|
void |
onDown(Host host)
Called when a node is determined to be down.
|
void |
onRemove(Host host)
Called when a node is removed from the cluster.
|
void |
onSuspected(Host host)
Called when a node is suspected to be dead.
|
void |
onUp(Host host)
Called when a node is determined to be up.
|
public RoundRobinPolicy()
public void init(Cluster cluster, Collection<Host> hosts)
LoadBalancingPolicy
Note that the driver guarantees that it will call this method exactly once per policy object and will do so before any call to another of the methods of the policy.
init
in interface LoadBalancingPolicy
cluster
- the Cluster
instance for which the policy is created.hosts
- the initial hosts to use.public HostDistance distance(Host host)
This policy consider all nodes as local. This is generally the right
thing to do in a single datacenter deployment. If you use multiple
datacenter, see DCAwareRoundRobinPolicy
instead.
distance
in interface LoadBalancingPolicy
host
- the host of which to return the distance of.host
.public Iterator<Host> newQueryPlan(String loggedKeyspace, Statement statement)
The returned plan will try each known host of the cluster. Upon each
call to this method, the i
th host of the plans returned will cycle
over all the hosts of the cluster in a round-robin fashion.
newQueryPlan
in interface LoadBalancingPolicy
loggedKeyspace
- the keyspace currently logged in on for this
query.statement
- the query for which to build the plan.public void onUp(Host host)
Host.StateListener
onUp
in interface Host.StateListener
host
- the host that has been detected up.public void onSuspected(Host host)
Host.StateListener
A node is suspected to be dead when an error occurs on one of it's
opened connection. As soon as an host is suspected, a connection attempt
to that host is immediately tried. If this succeed, then it means that
the connection was disfunctional but that the node was not really down.
If this fails however, this means the node is truly dead, onDown() is
called and further reconnection attempts are scheduled according to the
ReconnectionPolicy
in place.
When this event is triggered, it is possible to call the host
Host.getInitialReconnectionAttemptFuture()
method to wait until the
initial and immediate reconnection attempt succeed or fail.
Note that some StateListener may ignore that event. If a node that
that is suspected down turns out to be truly down (that is, the driver
cannot successfully connect to it right away), then Host.StateListener.onDown(com.datastax.driver.core.Host)
will
be called.
onSuspected
in interface Host.StateListener
public void onDown(Host host)
Host.StateListener
onDown
in interface Host.StateListener
host
- the host that has been detected down.public void onAdd(Host host)
Host.StateListener
The newly added node should be considered up.
onAdd
in interface Host.StateListener
host
- the host that has been newly added.public void onRemove(Host host)
Host.StateListener
onRemove
in interface Host.StateListener
host
- the removed host.Copyright © 2014. All rights reserved.