public class DCAwareRoundRobinPolicy extends Object implements LoadBalancingPolicy
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 and Description |
---|
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 |
---|---|
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.
|
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).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).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).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)
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 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 LoadBalancingPolicy
host
- the host of which to return the distance of.host
.public Iterator<Host> newQueryPlan(Query 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.
newQueryPlan
in interface LoadBalancingPolicy
query
- 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 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.