public class DCAwareRoundRobinPolicy extends Object implements LoadBalancingPolicy, CloseableLoadBalancingPolicy
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
RoundRobinPolicy
, but its DC awareness incurs a slight overhead
so the latter should be preferred to this policy in that case.
Modifier and Type | Class and Description |
---|---|
static class |
DCAwareRoundRobinPolicy.Builder
Helper class to build the policy.
|
Constructor and Description |
---|
DCAwareRoundRobinPolicy()
Deprecated.
builder() is the recommended way to build instances of this class. This constructor was preserved for
backward-compatibility purposes, it is equivalent to DCAwareRoundRobinPolicy.builder().build() . |
DCAwareRoundRobinPolicy(String localDc)
Deprecated.
builder() is the recommended way to build instances of this class. This constructor was preserved for
backward-compatibility purposes, it is equivalent to DCAwareRoundRobinPolicy.builder().withLocalDc(localDc).build() . |
DCAwareRoundRobinPolicy(String localDc,
int usedHostsPerRemoteDc)
Deprecated.
builder() is the recommended way to build instances of this class. This constructor was preserved for
backward-compatibility purposes, it is equivalent to
DCAwareRoundRobinPolicy.builder().withLocalDc(localDc).withUsedHostsPerRemoteDc(usedHostsPerRemoteDc).build() . |
DCAwareRoundRobinPolicy(String localDc,
int usedHostsPerRemoteDc,
boolean allowRemoteDCsForLocalConsistencyLevel)
Deprecated.
builder() is the recommended way to build instances of this class. This constructor was preserved for
backward-compatibility purposes, it is equivalent to
DCAwareRoundRobinPolicy.builder().withLocalDc(localDc).withUsedHostsPerRemoteDc(usedHostsPerRemoteDc).allowRemoteDCsForLocalConsistencyLevel().build() . |
Modifier and Type | Method and Description |
---|---|
static DCAwareRoundRobinPolicy.Builder |
builder()
Returns a builder to create a new instance.
|
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 |
onSuspected(Host host) |
void |
onUp(Host host)
Called when a node is determined to be up.
|
@Deprecated public DCAwareRoundRobinPolicy()
builder()
is the recommended way to build instances of this class. This constructor was preserved for
backward-compatibility purposes, it is equivalent to DCAwareRoundRobinPolicy.builder().build()
.public DCAwareRoundRobinPolicy(String localDc)
builder()
is the recommended way to build instances of this class. This constructor was preserved for
backward-compatibility purposes, it is equivalent to DCAwareRoundRobinPolicy.builder().withLocalDc(localDc).build()
.public DCAwareRoundRobinPolicy(String localDc, int usedHostsPerRemoteDc)
builder()
is the recommended way to build instances of this class. This constructor was preserved for
backward-compatibility purposes, it is equivalent to
DCAwareRoundRobinPolicy.builder().withLocalDc(localDc).withUsedHostsPerRemoteDc(usedHostsPerRemoteDc).build()
.public DCAwareRoundRobinPolicy(String localDc, int usedHostsPerRemoteDc, boolean allowRemoteDCsForLocalConsistencyLevel)
builder()
is the recommended way to build instances of this class. This constructor was preserved for
backward-compatibility purposes, it is equivalent to
DCAwareRoundRobinPolicy.builder().withLocalDc(localDc).withUsedHostsPerRemoteDc(usedHostsPerRemoteDc).allowRemoteDCsForLocalConsistencyLevel().build()
.public static DCAwareRoundRobinPolicy.Builder builder()
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(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 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)
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.public void close()
CloseableLoadBalancingPolicy
close
in interface CloseableLoadBalancingPolicy
Copyright © 2012–2015. All rights reserved.