com.datastax.driver.core.policies
Class DCAwareRoundRobinPolicy

java.lang.Object
  extended by com.datastax.driver.core.policies.DCAwareRoundRobinPolicy
All Implemented Interfaces:
Host.StateListener, LoadBalancingPolicy

public class DCAwareRoundRobinPolicy
extends Object
implements LoadBalancingPolicy

A data-center aware Round-robin load balancing policy.

This policy provides round-robin queries over the node of the local datacenter. It also includes in the query plans returned a configurable number of hosts in the remote datacenters, but those are always tried after the local nodes. In other words, this policy guarantees that no host in a remote datacenter will be queried unless no host in the local datacenter can be reached.

If used with a single datacenter, this policy is equivalent to the LoadBalancingPolicy.RoundRobin policy, but its DC awareness incurs a slight overhead so the LoadBalancingPolicy.RoundRobin policy could be prefered to this policy in that case.


Constructor Summary
DCAwareRoundRobinPolicy(String localDc)
          Creates a new datacenter aware round robin policy given the name of the local datacenter.
DCAwareRoundRobinPolicy(String localDc, int usedHostsPerRemoteDc)
          Creates a new DCAwareRoundRobin policy given the name of the local datacenter and that uses the provided number of host per remote datacenter as failover for the local hosts.
 
Method Summary
 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(Query query)
          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 onUp(Host host)
          Called when a node is determined to be up.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DCAwareRoundRobinPolicy

public DCAwareRoundRobinPolicy(String localDc)
Creates a new datacenter aware round robin policy given the name of the local datacenter.

The name of the local datacenter provided must be the local datacenter name as known by Cassandra.

The policy created will ignore all remote hosts. In other words, this is equivalent to new DCAwareRoundRobinPolicy(localDc, 0).

Parameters:
localDc - the name of the local datacenter (as known by Cassandra).

DCAwareRoundRobinPolicy

public DCAwareRoundRobinPolicy(String localDc,
                               int usedHostsPerRemoteDc)
Creates a new DCAwareRoundRobin policy given the name of the local datacenter and that uses the provided number of host per remote datacenter as failover for the local hosts.

The name of the local datacenter provided must be the local datacenter name as known by Cassandra.

Parameters:
localDc - the name of the local datacenter (as known by Cassandra).
usedHostsPerRemoteDc - the number of host per remote datacenter that policies created by the returned factory should consider. Created policies distance method will return a HostDistance.REMOTE distance for only usedHostsPerRemoteDc hosts per remote datacenter. Other hosts of the remote datacenters will be ignored (and thus no connections to them will be maintained).
Method Detail

init

public void init(Cluster cluster,
                 Collection<Host> hosts)
Description copied from interface: LoadBalancingPolicy
Initialize this load balancing policy.

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.

Specified by:
init in interface LoadBalancingPolicy
Parameters:
cluster - the Cluster instance for which the policy is created.
hosts - the initial hosts to use.

distance

public HostDistance distance(Host host)
Return the HostDistance for the provided host.

This policy consider nodes in the local datacenter as LOCAL. For each remote datacenter, it considers a configurable number of hosts as REMOTE and the rest is IGNORED.

To configure how many host in each remote datacenter is considered REMOTE, see DCAwareRoundRobinPolicy(String, int).

Specified by:
distance in interface LoadBalancingPolicy
Parameters:
host - the host of which to return the distance of.
Returns:
the HostDistance to host.

newQueryPlan

public Iterator<Host> newQueryPlan(Query query)
Returns the hosts to use for a new query.

The returned plan will always try each known host in the local datacenter first, and then, if none of the local host is reachable, will try up to a configurable number of other host per remote datacenter. The order of the local node in the returned query plan will follow a Round-robin algorithm.

Specified by:
newQueryPlan in interface LoadBalancingPolicy
Parameters:
query - the query for which to build the plan.
Returns:
a new query plan, i.e. an iterator indicating which host to try first for querying, which one to use as failover, etc...

onUp

public void onUp(Host host)
Description copied from interface: Host.StateListener
Called when a node is determined to be up.

Specified by:
onUp in interface Host.StateListener
Parameters:
host - the host that has been detected up.

onDown

public void onDown(Host host)
Description copied from interface: Host.StateListener
Called when a node is determined to be down.

Specified by:
onDown in interface Host.StateListener
Parameters:
host - the host that has been detected down.

onAdd

public void onAdd(Host host)
Description copied from interface: Host.StateListener
Called when a new node is added to the cluster.

The newly added node should be considered up.

Specified by:
onAdd in interface Host.StateListener
Parameters:
host - the host that has been newly added.

onRemove

public void onRemove(Host host)
Description copied from interface: Host.StateListener
Called when a node is removed from the cluster.

Specified by:
onRemove in interface Host.StateListener
Parameters:
host - the removed host.


Copyright © 2013. All Rights Reserved.