public class DCAwareRoundRobinPolicy extends Object implements LoadBalancingPolicy
This policy provides round-robin queries over the node of the local data center. It also includes in the query plans returned a configurable number of hosts in the remote data centers, but those are always tried after the local nodes. In other words, this policy guarantees that no host in a remote data center will be queried unless no host in the local data center can be reached.
If used with a single data center, this policy is equivalent to the
LoadBalancingPolicy.RoundRobin policy, but its DC awareness
incurs a slight overhead so the LoadBalancingPolicy.RoundRobin
policy could be preferred to this policy in that case.
| Constructor and Description |
|---|
DCAwareRoundRobinPolicy()
Creates a new datacenter aware round robin policy that auto-discover
the local data-center.
|
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.
|
DCAwareRoundRobinPolicy(String localDc,
int usedHostsPerRemoteDc,
boolean allowRemoteDCsForLocalConsistencyLevel)
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.
|
| Modifier and Type | Method and Description |
|---|---|
void |
close()
Gets invoked at cluster shutdown.
|
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 |
onUp(Host host)
Called when a node is determined to be up.
|
public DCAwareRoundRobinPolicy()
If this constructor is used, the data-center used as local will the
data-center of the first Cassandra node the driver connects to. This
will always be ok if all the contact points use at Cluster
creation are in the local data-center. If it's not the case, you should
provide the local data-center name yourself by using one of the other
constructor of this class.
This constructor is a shortcut for new DCAwareRoundRobinPolicy(null),
and as such will ignore all hosts in remote data-centers.
public DCAwareRoundRobinPolicy(String localDc)
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).
localDc - the name of the local datacenter (as known by
Cassandra). If this is null, the policy will default to the
data-center of the first node connected to.public DCAwareRoundRobinPolicy(String localDc, int usedHostsPerRemoteDc)
The name of the local datacenter provided must be the local datacenter name as known by Cassandra.
If usedHostsPerRemoteDc > 0, then if for a query no host
in the local datacenter can be reached and if the consistency
level of the query is not LOCAL_ONE or LOCAL_QUORUM,
then up to usedHostsPerRemoteDc host per remote data-center
will be tried by the policy as a fallback. Please note that no
remote host will be used for LOCAL_ONE and LOCAL_QUORUM
since this would change the meaning of the consistency level (and
thus somewhat break the consistency contract).
localDc - the name of the local datacenter (as known by
Cassandra). If this is null, the policy will default to the
data-center of the first node connected to.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).public DCAwareRoundRobinPolicy(String localDc, int usedHostsPerRemoteDc, boolean allowRemoteDCsForLocalConsistencyLevel)
This constructor is equivalent to DCAwareRoundRobinPolicy(String, int)
but allows to override the policy of never using remote data-center
nodes for LOCAL_ONE and LOCAL_QUORUM queries. It is
however inadvisable to do so in almost all cases, as this would
potentially break consistency guarantees and if you are fine with that,
it's probably better to use a weaker consitency like ONE, TWO or THREE. As such, this constructor should generally
be avoided in favor of DCAwareRoundRobinPolicy(String, int).
Use it only if you know and understand what you do.
localDc - the name of the local datacenter (as known by
Cassandra). If this is null, the policy will default to the
data-center of the first node connected to.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).allowRemoteDCsForLocalConsistencyLevel - whether or not the
policy may return remote host when building query plan for query
having consitency LOCAL_ONE and LOCAL_QUORUM.public void init(Cluster cluster, Collection<Host> hosts)
LoadBalancingPolicyNote 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 LoadBalancingPolicycluster - the Cluster instance for which the policy is created.hosts - the initial hosts to use.public HostDistance distance(Host 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).
distance in interface LoadBalancingPolicyhost - the host of which to return the distance of.host.public Iterator<Host> newQueryPlan(String loggedKeyspace, Statement statement)
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.
newQueryPlan in interface LoadBalancingPolicyloggedKeyspace - the keyspace currently logged in on for this
query.statement - the query for which to build the plan.public void onUp(Host host)
LoadBalancingPolicyonUp in interface LoadBalancingPolicyhost - the host that has been detected up.public void onDown(Host host)
LoadBalancingPolicyonDown in interface LoadBalancingPolicyhost - the host that has been detected down.public void onAdd(Host host)
LoadBalancingPolicyThe newly added node should be considered up.
onAdd in interface LoadBalancingPolicyhost - the host that has been newly added.public void onRemove(Host host)
LoadBalancingPolicyonRemove in interface LoadBalancingPolicyhost - the removed host.public void close()
LoadBalancingPolicyclose in interface LoadBalancingPolicyCopyright © 2012–2015. All rights reserved.